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 registerEvent(element,evtName,evtFunc) {
   evtName = evtName.replace(/^on/,"");
   if(element.attachEvent) {
      element.attachEvent("on"+evtName,evtFunc);
   } else if(element.addEventListener) {
      element.addEventListener(evtName,evtFunc,false);
   } else {
   }
}
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(number){
   var num = Number(number);
   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(email){
   if(email.length == 0) return true;
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   return reg.test(email);
}
function showItems(id, numToShow, totalNum){
   for(var i=0;i<totalNum;++i){
      sD(id+'__'+i, i<numToShow || numToShow<=0);
   }
}
