var agent=navigator.userAgent.toLowerCase();
function moveObject(objectId, newXCoordinate, newYCoordinate) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
  styleObject.left = newXCoordinate;
  styleObject.top = newYCoordinate;
  return true;
    } else {
  // we couldn't find the object, so we can't very well move it
  return false;
    }
} // moveObject

function switchIfDone(the_form, this_div, next_div) {

  var complete = true;
  for (var loop=0; loop < the_form.elements.length; loop++) {
    if (the_form.elements[loop].value == "") {
      complete = false;
    }
  }
  if ((complete == true) && (next_div == "finished")) {
    submitTheInfo();
  } 
  else if (complete == true) {
    switchDiv(this_div, next_div);
  } else {
    alert('please complete the form before moving on');
  }
}

function switchDiv(this_div, next_div) {
  if (getStyleObject(this_div) && getStyleObject(next_div)) {
    setVisDisp(this_div, false);
    setVisDisp(next_div, true);
  }
}

function escapeChars(theString) {
   var regexp = /&/g ;
   var regexp2 = /#/g ;
   var regexp3 = /\r\n/g ;
   var regexp4 = /\n/g ;
   var regexp5 = /\"/g ;
   theString = theString.replace(regexp, "%^");
   theString = theString.replace(regexp2, "%*");
   theString = theString.replace(regexp3, "<br>");
   theString = theString.replace(regexp4, "<br>");
   theString = theString.replace(regexp5, "%)");
   return theString;
}

function submitTheInfo() {
/*
 var submission_string="";

 for (var form_loop=0; form_loop<document.forms.length; form_loop++) { 
     if(document.forms[form_loop].name != "hiddenform") {
       var el = document.forms[form_loop].elements;

       for (var elems=0; elems<document.forms[form_loop].length;elems++) {
         if (document.forms[form_loop].elements[elems].name != "") {
           if ( document.forms[form_loop].elements[elems].type == "checkbox") {
             submission_string += document.forms[form_loop].elements[elems].name + "=" +
             document.forms[form_loop].elements[elems].checked + "&";
           } else if( document.forms[form_loop].elements[elems].type == "radio") { 
             var radiogroup = el[el[elems].name];
             for (var j=0; j<radiogroup.length; ++j) {
               if(radiogroup[j].checked) {
                 submission_string += el[elems].name + "=" +
                 document.forms[form_loop].elements[elems + j].value + "&";
                 elems = elems + radiogroup.length - 1;
               }
             }
           } else {
             submission_string += document.forms[form_loop].elements[elems].name + "=" +
             escapeChars(document.forms[form_loop].elements[elems].value) + "&";
           }
         }

       }
     }
  }

  getFormObject("hiddenform","variablestring").value = submission_string;
  alert(submission_string);

  // the next two lines are written for debugging - 
  // to put the script into action
  // comment out the setVisDisp() line
  // and uncomment the document.hidden.form.submit() line
  //

  getForm("hiddenform").submit(); 
  //setVisDisp("hiddenstuff",true);*/
}


function hideAll(hidden_div) {
  setVisDisp("hidden_div",false);
}


function checkboxcheck(entry,HiddenDiv) { 
  if(entry) {
     if (entry.checked) {
       setVisDisp(HiddenDiv,true);
     }
     else {
       setVisDisp(HiddenDiv,false);
     }
  }
}


function isNumber(inputVal) {
  oneDecimal = false;
  inputStr = inputVal.toString();
  for (var i=0; i<inputStr.length; i++) {
    var oneChar =inputStr.charAt(i);
    if (oneChar == "." && !oneDecimal) {
      oneDecimal = true;
      continue;
    }
    if (oneChar < "0" || oneChar > "9")  {
      if (oneChar != ",") {
        alert("Please make sure entries are numbers.");
        return false;
      }
    }
  }
  return true;
}

/*
* Restricts the onkeypress event of the textbox with the given id to only numeric input.
* Optional 3rd, 4th and 5th arguments are the number of decimal places (0 - inf),
* and the min and max values of the textbox, respectively. To leave these
* unrestricted, either do not specify them, or specify them as "".
*/
function okpRestrictNumber(event, id) {
	var decimals='', maxval='', minval='';
   var isIE = navigator.userAgent.toLowerCase().indexOf("msie") > -1;
	if(typeof(arguments[2])!="undefined")
		decimals=arguments[2];
	if(typeof(arguments[3])!="undefined")
		minval=arguments[3];
	if(typeof(arguments[4])!="undefined")
		maxval=arguments[4];
		
	if(isIE)
		iKeyCode = event.keyCode;
	else
		iKeyCode = event.which;
	var tbox = document.getElementById(id);
	var thisval = tbox.value;
	
	var caretStart, caretEnd;
	if(isIE && document.selection){
		tbox.selectionStart=Math.abs(document.selection.createRange().moveStart("character", -1000000));
		tbox.selectionEnd=Math.abs(document.selection.createRange().moveEnd("character", -1000000));
	}
	caretStart = tbox.selectionStart;
	caretEnd = tbox.selectionEnd;
	
	var newval = '0';
	var dec=thisval.indexOf('.')
	var neg=thisval.indexOf('-')

	if(decimals == 0){
		if( (iKeyCode >= 32 && iKeyCode < 48 && (iKeyCode != 45 || neg != -1)) ||  
			 iKeyCode > 57	|| iKeyCode == 36)
			return false;
	}
	else {
		if((iKeyCode >= 41 && iKeyCode < 46 && (iKeyCode != 45 || neg != -1)) ||
			 iKeyCode == 47 ||
			 (iKeyCode >= 33 && iKeyCode< 41) ||
			 iKeyCode > 57 ||
			 (iKeyCode == 46 && dec > -1 && (dec < caretStart || dec >= caretEnd)) )
			return false;
		if(decimals != ''){
			if( dec == -1 && thisval.length-caretEnd > decimals )
				return false;
			if( dec > -1 && thisval.length-dec > decimals 
				&& iKeyCode >= 32 && (caretStart > dec && caretStart == caretEnd))
				return false;
		}
	}
	if(minval !== '' && minval >= 0 && iKeyCode == 45) return false;


	if(iKeyCode-48 >= 0) newval=thisval.substring(0,caretStart)+(iKeyCode-48).toString()+thisval.substring(caretEnd, thisval.length);
	if(newval == '' || !isNumeric(newval)) newval='0';
	newval=parseFloat(newval);
	if(maxval != ''){
		if(newval > maxval) return false;
	}
	if(minval != ''){
		if(newval < minval && newval != '') return false;
	}
	
	return true;
}

/*
 * Returns true if inputVal is a number, false otherwise
 */
function isNumeric(inputVal) {
  oneDecimal = false
  inputStr = inputVal.toString()
  for (var i=0; i<inputStr.length; i++) {
    var oneChar =inputStr.charAt(i)
    if (oneChar == "." && !oneDecimal) {
      oneDecimal = true
      continue
    }
    if (oneChar < "0" || oneChar > "9")  {
      if (oneChar != "," && oneChar != "-") {
        return false
      }
    }
  }
  return true
}

function checkIt(inputvar) {
   inputStr = inputvar.value
   if (isNumber(inputStr)) {
   } else {
     inputvar.focus()
     inputvar.select()
   }
}  

function funcChangeDiv(inputvar,selectedvalue,HiddenDiv) { 
   if(inputvar) {
      if (inputvar.selectedIndex == selectedvalue) {
         setVisDisp(HiddenDiv,true);
      } else {
         setVisDisp(HiddenDiv,false);
      }
   }
}

function funcradChangeDiv(selectedvalue,HiddenDiv) { 
   if (selectedvalue == "true") {
      setVisDisp(HiddenDiv,true);
   } else {
      setVisDisp(HiddenDiv,false);
   }
}

function funcNotChangeDiv(inputvar,selectedvalue,HiddenDiv) { 
   if(inputvar) {
      if (inputvar.selectedIndex != selectedvalue) {
        setVisDisp(HiddenDiv,true);
      } else {
        setVisDisp(HiddenDiv,false);
      }
   }
}

function redisplay(db,openingornot,divname) {
   for (var i=1; i<db.length; i++) {
      setVisDisp(db[i].divname, false);
   }
   if (openingornot == 'true') {
      setVisDisp(db[1].divname, true);
   }  
   setVisDisp(divname, true);
}


function formatCurrency(num,dollarSign) {
   num = num.toString().replace(/\$|\,/g,'');
   if(isNaN(num)){
      num="0";
      //alert("Oops!  That does not appear to be a valid number.  Please try again."); 
      return (num);
   } else {
      sign = (num == (num = Math.abs(num)));
      num = Math.floor(num*100+0.50000000001);
      cents = num%100;
      num = Math.floor(num/100).toString();
      if(cents<10) cents = "0" + cents;
      for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) {
         num = num.substring(0,num.length-(4*i+3))+','+
               num.substring(num.length-(4*i+3));
      }
      if (dollarSign == true)	{
        return (((sign)?'':'-') + '$' + num + '.' + cents);
      } else {
         return (((sign)?'':'-') + num + '.' + cents);
      }
   }
}
function setStatus(msg) {
  status = msg;
}
function bookmark(){
   /***** Compatibility? *********/
   window.external.AddFavorite(window.location.href,window.document.title);
}
function printDocument() {
   if(window.print) {
      window.print();
   } else if(agent.indexOf("mac") != -1) {
      alert("Press Cmd-p to print your document.");
   } else {
      alert("Press Ctrl-p to print your document.");
   }
}
// This function returns true if the event is a keypress of 0-9 or a .
function onlyNumeric(event) {
   var strUserAgent = navigator.userAgent.toLowerCase(); 
   var isIE = strUserAgent.indexOf("msie") > -1;
   if(isIE)
      iKeyCode = event.keyCode;
   else
      iKeyCode = event.which;
   if(
         (iKeyCode >= 32 && iKeyCode <= 45) ||
         (iKeyCode == 47) ||
         iKeyCode >= 58
     )
     return false;
   return true;

}
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}
function changeFullCase(str, type){
   if (type == "upperCase") return str.toUpperCase();
   else return str.toLowerCase();
}
function changeCase(str, type, change) {
   if(typeof(str) == "undefined" || str == null || str.length < 1) return "";
   if (change == "all"){
      return changeFullCase(str, type);
   }
   else if (change == "first"){
      return changeFullCase(str.substring(0,1), type) + str.substring(1,str.length);
   }
   else{
      var val = "";
      var trimVal, start, words;
      if(change == "sentence"){
         words = str.split('.');
      }
      else{
         words = str.split(' ');
      }
      for(var c=0; c < words.length; c++) {
         trimVal = words[c].replace(/^\s+/,"");
         start = words[c].length - trimVal.length;
         val += words[c].substring(0, start) + changeFullCase(words[c].substring(start,start+1), type) + words[c].substring(start+1,words[c].length);
         if(c != words.length - 1){
            if(change == "sentence"){
               val += ".";
            }
            else{
               val += " ";
            }
         }
      }
      return val;
   }
}
function parseNum(input){
   var numStr = new String(input); 
   numStr = numStr.replace(/[^0-9\.-]/g, ''); 

   var num = parseFloat(numStr);
   if(isNaN(num)) num = 0;
   return num;
}

function entryTab(source,contentID) {
	var tabs=source.parentNode.parentNode.getElementsByTagName("A");
	for(var i=0;i<tabs.length;i++) {
		if(tabs[i]==source) {
			tabs[i].setAttribute("class","selected");
			tabs[i].setAttribute("className","selected");
		} else {
			tabs[i].setAttribute("class","");
			tabs[i].setAttribute("className","");
		}
	}
	var divs=source.parentNode.parentNode.parentNode.getElementsByTagName("DIV");
	for(var i=0;i<divs.length;i++) {
		if(divs[i].className=="entryTabPage") {
			if(divs[i].id==contentID) divs[i].style.display="block";
			else divs[i].style.display="none";
		}
	}
	source.blur();
	return false;
}
function checkEmail(emailStr){
   if(emailStr.length == 0) return true;
   emailStr = emailStr.toLowerCase();
	var output=(arguments.length==2 && arguments[1]);
   var checkTLD=true;
   var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
   var emailPat=/^(.+)@(.+)$/;
   var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
   var validChars="\[^\\s" + specialChars + "\]";
   var quotedUser="(\"[^\"]*\")";
   var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
   var atom=validChars + '+';
   var word="(" + atom + "|" + quotedUser + ")";
   var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
   var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
   var matchArray=emailStr.match(emailPat);
   if (matchArray==null) {
      if(output) alert("This does not appear to be a valid email address, such as someuser@hotmail.com (check @ and .\'s)'.");
      return false;
   }
   var user=matchArray[1];
   var domain=matchArray[2];
   for (i=0; i<user.length; i++) {
      if (user.charCodeAt(i)>127 || user.charCodeAt(i) == 38) {
         if(output) alert("This does not appear to be a valid email address.  The user name (the part before the @ symbol) contains invalid characters.");
         return false;
      }
   }
   for (i=0; i<domain.length; i++) {
      if (domain.charCodeAt(i)>127 || domain.charCodeAt(i) == 38) {
         if(output) alert("This does not appear to be a valid email address.  The domain (the part after the @ symbol) contains invalid characters.");
         return false;
      }
	}
	if(user.length > 64) {
		if(output) alert("This does not appear to be a valid email address.  The user name (the part before the @ symbol) is longer than 64 characters.");
      return false;
	}
	if(domain.length > 255) {
		if(output) alert("This does not appear to be a valid email address.  The domain (the part after the @ symbol) is longer than 255 characters.");
      return false;
	}
   if (user.match(userPat)==null) {
      if(output) alert("This does not appear to be a valid email address.  The username doesn't seem to be valid.");
      return false;
   }
   var IPArray=domain.match(ipDomainPat);
   if (IPArray!=null) {
      for (var i=0;i<=IPArray.length;i++) {
         if (IPArray[i]>255) {
            if(output) alert("This does not appear to be a valid email address.  The IP address does not seem to be valid.");
            return false;
         }
      }
      return true;
   }
   var atomPat=new RegExp("^" + atom + "$");
   var domArr=domain.split(".");
   var len=domArr.length;
   for (i=0;i<len;i++) {
      if (domArr[i].search(atomPat)==-1) {
         if(output) alert("This does not appear to be a valid email address.  The domain name does not seem to be valid.");
         return false;
      }
		if (domArr[i].length > 63) {
         if(output) alert("This does not appear to be a valid email address.  The domain name does not seem to be valid.");
         return false;
      }
	}
   if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {
      if(output) alert("This does not appear to be a valid email address.  The address must end in a well-known domain or two letter country.");
      return false;
   }
   if (len<2) {
      if(output) alert("This does not appear to be a valid email address.  This address is missing a hostname: the part after the @ sign.");
      return false;
   }
   return true;
}
function showItems(id, numToShow, totalNum){
   for(var i=0;i<totalNum;++i){
      sD(id+'__'+i, i<numToShow || numToShow<=0);
   }
}
function ShowToolTip (toolTipManagerID, content, element, position, width, horizOffset, vertOffset, overridePos){
   var oToolTipManager = $find(toolTipManagerID);
   var oToolTip = oToolTipManager.getToolTipByElement(element);
   if(oToolTip == null){
      oToolTip = oToolTipManager.createToolTip(element);
      if(overridePos){
         oToolTip.set_width(width);
         oToolTip.set_offsetX(horizOffset);
         oToolTip.set_offsetY(vertOffset);
      }
      else{
         if(content.length < 100){
            oToolTip.set_width(null);
         }
      }
      oToolTip.set_position(GetToolTipPosition(position));
      oToolTip.set_content(content);
      oToolTip.show();
      var tableElement = oToolTip._tableElement
      if(tableElement && overridePos){   
         tableElement.style.width = width + "px";   
      }   
   } //if(oToolTip == null)
   else{
      oToolTip.set_content(content);
   }
}
function GetToolTipPosition(s){
   sl = s.toLowerCase();
   retVal = 0;
   if(sl.indexOf("top") >= 0){
      retVal += 10;
   }
   else if(sl.indexOf("bottom") >= 0){
      retVal += 30;
   }
   else{
      retVal += 20;
   }
   if(sl.indexOf("left") >= 0){
      retVal += 1;
   }
   else if(sl.indexOf("right") >= 0){
      retVal += 3;
   }
   else{
      retVal += 2;
   }
   return retVal;
}

