// the following functions provide input masking capabilities
var cbCharsRe = /[cvxz]/i;
var kbCharsRe = /[\x08\x0D\x00\x03\x16\x18\x1A]/;
var numCharsRe = /\d/;
var doubleRe = /[\d\.]+/;

/* Check copy/paste/cut/undo keystrokes */
function chkClipboard(e, ch) {
	if (e.which) {
		return e.ctrlKey && cbCharsRe.test(ch);
	} else {
		return false;
	}
}

/* Validate the keystroke against a regular expression */
function kc(e, allowRe) {
	var ch = getKeyChar(e);
  	return (allowRe.test(ch) || kbCharsRe.test(ch) || chkClipboard(e, ch));
}

/* Validate the keystroke is a numeric value */
function cn(e) {
  	return kc(e, numCharsRe);
}

/* Validate the keystroke is a double (floating point) value */
function cdb(formValue, e) {
  	var isValid = kc(e, doubleRe);
  	if(isValid) {
		var ch = getKeyChar(e);
		if(ch == ".") {
			if(formValue.indexOf(".") >= 0) {
				return false;
			} else {
				return true;
			}
		} else {
			return true;
		}
  	} else {
     	return false;
  	}
}

/* Get the keystroke character from an event */
function getKeyChar(e) {
	var key;
	if(window.event) {
		key = e.keyCode; // for IE, e.keyCode or window.event.keyCode
	} else if(e.which) {
		key = e.which; // netscape, firefox
	}
	var ch = String.fromCharCode(key);
	return ch;
}

/*
 * Disable the enter key, for instance, to disable Enter submitting forms.
 * This can be used for an entire page by setting:
 * <body onkeypress="return disableEnterKey(event);">
 * or for a single input field by setting:
 * <input type="text" name="a" onkeypress="return disableEnterKey(event);">
 */
function disableEnterKey(e) {
   var key;
   if(window.event) {
      key = e.keyCode; // for IE, e.keyCode or window.event.keyCode
   } else if(e.which) {
      key = e.which; // netscape, firefox
   }     

   return (key != 13);
}

var isDHTML = 0;
var isLayers = 0;
var isAll = 0;
var isID = 0;

if (document.getElementById) {isID = 1; isDHTML = 1;}
else {
browserVersion = parseInt(navigator.appVersion);
if ((navigator.appName.indexOf('Netscape') != -1) && (browserVersion == 4)) {isLayers = 1; isDHTML = 1;}
else {
if (document.all) {isAll = 1; isDHTML = 1;}
}}

function findDOM(objectID,withStyle) {
var menuArea = "menuArea";
   if (withStyle == 1) {
      if (isID) { return (document.getElementById(objectID).style) ; }
      else { 
         if (isAll) { return (document.all[objectID].style); }
      else {
         if (isLayers) { return (document.layers[menuArea].layers[objectID]); }
      };}
   }
   else {
      if (isID) { return (document.getElementById(objectID)) ; }
      else { 
         if (isAll) { return (document.all[objectID]); }
      else {
         if (isLayers) { return (document.layers[menuArea].layers[objectID]); }
      };}
   }
}

function displayPop(url, name, width, height) {
   var myWin;
   if (document.layers)
      myWin = window.open(url, name, "width=" + width + ",height=" + height + ",resizable=yes,scrollbars=yes,toolbar=no,screenX=20,screenY=20");
   else
      myWin = window.open(url, name, "width=" + width + ",height=" + height + ",resizable=yes,scrollbars=yes,toolbar=no,top=20,left=20");
   myWin.focus();
}

function newWindow(url)
{
	   var newWin = window.open(url, '', '' );
}

/* Make hover menus work in stupid, broken IE */
sfHover = function() {
   var navTabs = document.getElementById("nav-tabs");
   var sfEls = navTabs && navTabs.getElementsByTagName("TD");
   if (sfEls) {
      for (var i=0; i<sfEls.length; i++) {
         sfEls[i].onmouseover=function() {
            this.className+=" sfhover";
         }
         sfEls[i].onmouseout=function() {
            this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
         }
      }
   }
}
if (window.attachEvent) window.attachEvent("onload", sfHover);


var ytpStr = '<object width="560" height="340" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"> \
                <param name="movie" value="http://www.youtube.com/v/VIDKEY&hl=en_US&fs=1&rel=0&color1=0x006699&color2=0x54abd6&showinfo=0&autoplay=1"></param> \
                <param name="allowFullScreen" value="true"></param> \
                <param name="allowscriptaccess" value="always"></param> \
                <embed src="http://www.youtube.com/v/VIDKEY&hl=en_US&fs=1&rel=0&color1=0x006699&color2=0x54abd6&showinfo=0&autoplay=1" \
                       type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="340"> \
                </embed> \
              </object>';

var openVideoItem = null;
var tocLock = false;
function tocFold(q) {
	if (!tocLock) {
		tocLock = true;
      var item = $(q).parent('div.q-title').parent('div.q');
      var isVideo = item.hasClass('video');
      var open = item.hasClass('open');
      if (!open) { // open it
         if (isVideo) {
            if (openVideoItem) { // close currently open vid
               tocItem(openVideoItem, false, true);
               openVideoItem.children('.q-video').find('object').remove();
            }
            var vidkey = item.find('.vidkey').attr('name');
            item.find('.q-video').append(ytpStr.replace(/VIDKEY/g, vidkey));
         }
         tocItem(item, true);
         if (isVideo) { openVideoItem = item; }
      } else { // close it
         tocItem(item, false);
         if (isVideo) {
            item.find('.q-video').find('object').remove();
            openVideoItem = null;
         }
      }
	   // set a timeout 100 milliseconds over the "duration" value above
	  	setTimeout(function(){tocLock = false;}, 400);
	}
}
function tocItem(item, open, noBlind) {
   var content = item.children('div.q-content');
   if (open) {
      item.removeClass('closed').addClass('open');
      item.children('.q-title').find('.ind').attr('src', '/images/web/arrow-dn.gif');
      if (noBlind) {
         content.show();
      } else {
         content.show('blind');
      }
   } else {
      item.removeClass('open').addClass('closed');
      item.children('.q-title').find('.ind').attr('src', '/images/web/arrow-rt.gif');
      if (noBlind) {
         content.hide();
      } else {
        content.hide('blind');
      }
   }
}



