var dbTop = 0;
var dbProceed = "true";
var beanNames = new Array();
var methodNames = new Array();
var methodParms = new Array();
var errorMessages = new Array();
var formName = "";
var executeSubmit = "false";
var executeLocationChange = "false";
var locationPage = "";
var executePageOpening = "false";
var pageToOpen = "";
var pageHeight = 1;
var pageWidth = 1;
var pageParms = "";
var windowName = "";
var essentialsId = ""+Math.random();

function callMethod(b,m,p,em){
	try{
		if( m=="runUpdate" || m=="runUpdate2" || m=="Utils.runUpdate" || m=="Utils.runUpdate2" ){
			p = dashEncode(p);
		}
	  if(dbProceed == "true"){
	    continueDBProcess(b,m,p,em);
	  }else{
	    for(var i=dbTop;i>0;i--){
	      beanNames[i] = beanNames[i-1];
	      methodNames[i] = methodNames[i-1];
	      methodParms[i] = methodParms[i-1];
	      errorMessages[i] = errorMessages[i-1];
	    }
	    beanNames[0] = b;
	    methodNames[0] = m;
	    methodParms[0] = p;
	    errorMessages[0] = em;
	    dbTop++;
	  }
	}catch(error){
		alert("An error occured and below is the error message.\n\n" + error.message);
	}
}

function continueDBProcess(b,m,p,em){
	try{
	  dbProceed = "false";
	  with(frames["MethodCallerFrame"].document.methodCallerForm){
	    beanName.value = b;
	    methodName.value = m;
	    parameters.value = p;
	    errorMessage.value = em;
	    forwardToPage.value = "MethodCaller.jsp?essentialsId="+essentialsId;
	    action = "CallMethodAndForward.jsp";
	    submit();
	  }
	}catch(error){
		alert("An error occured and below is the error message.\n\n" + error.message);
	}
}

function dbDone(id){
	if(essentialsId != id){
		return false;
	}else{
		try{
			if(dbTop == 0){
				dbProceed = "true";
				if(executePageOpening == "true"){
					openPage(pageToOpen,pageHeight,pageWidth,pageParms,windowName);
				}
				if(executeSubmit == "true"){
					mySubmit(formName);
				}
				if(executeLocationChange == "true"){
					goTo(locationPage);
				}
			}else{
				var b = beanNames[dbTop-1];
				var m = methodNames[dbTop-1];
				var p = methodParms[dbTop-1];
				var em = errorMessages[dbTop-1];
				dbTop--;
				continueDBProcess(b,m,p,em);
			}
		}catch(error){
			alert("An error occured and below is the error message.\n\n" + error.message);
		}
		return true;
	}
}

function mySubmit(fn){
	try{
	  if(dbProceed == "true"){
	    with(fn){
	      submit();
	    }
	  }else{
	    formName = fn;
	    executeSubmit = "true";
	  }
	}catch(error){
		alert("An error occured and below is the error message.\n\n" + error.message);
	}
}

function openPage(page, h, w, parms,winName) {
	try{
	  if(dbProceed == "true"){
	    if (h <= 1) { h = Math.floor(screen.height * h); }
	    if (w <= 1) { w = Math.floor(screen.width * w); }
	    var left = Math.floor( (screen.width - w) / 2);
	    var top = Math.floor( (screen.height - h) / 2);
	    var winParms = "top=" + top + ",left=" + left + ",height=" + h + ",width=" + w;
	    if (parms != "") { winParms += "," + parms; }
	    var win = window.open(page,winName,winParms);    
	    win.focus();
	  }else{
	    pageToOpen = page;
	    pageHeight = h;
	    pageWidth = w;
	    pageParms = parms;
	    windowName = winName;
	    executePageOpening = "true";
	  }
	}catch(error){
		alert("An error occurred when attempting to open a new window.  Most likely you have a popup blocker blocking the window from opening.  Either disable the popup blocker or have it allow popups from this site.  You can also hold down the ctrl key, then click while holding the ctrl key, and that usually overrides the popup blocker.");
	}
}

function submitTo(page,formName){
	try{
	  with(formName){
	    action = page;
	    mySubmit(formName);
	  }
	}catch(error){
		alert("An error occured and below is the error message.\n\n" + error.message);
	}
}

function goTo(page){
	try{
	  if(dbProceed == "true"){
	    location.href = page;
	  }else{
	    locationPage = page;
	    executeLocationChange = "true";
	  }
	}catch(error){
		alert("An error occured and below is the error message.\n\n" + error.message);
	}
}

function replace(str,s1,s2){
	try{
	  if(str==null || str==""){return str;}
	  var i = str.indexOf(s1);
	  while(i > -1 && i < str.length-s1.length){
	    str = str.substring(0,i) + s2 + str.substring(i+s1.length,str.length);
	    i = str.indexOf(s1,i+s2.length);
	  }
	  if(i>=str.length-s1.length){
	    str = str.substring(0,str.length-1) + s2;
	  }
	  return str;
	}catch(error){
		alert("An error occured and below is the error message.\n\n" + error.message);
	}
}

function removeBadChars(str){
	try{
		return replace(replace(replace(str,"'","`"),"\"","`"),"~","-");
	}catch(error){
		alert("An error occured and below is the error message.\n\n" + error.message);
	}
}

function containsBadChars(str){
	try{
		var arr = str.split("'");
		if(arr.length > 1){
			return true;
		}
		arr = str.split("\"");
		if(arr.length > 1){
			return true;
		}
		arr = str.split("~");
		if(arr.length > 1){
			return true;
		}
		return false;
	}catch(error){
		alert("An error occured and below is the error message.\n\n" + error.message);
	}
}

function checkLength(component,length){
	try{
	  if(component.value.length > length){
	    alert("You are only allowed to enter " + length + " characters");
	    component.value = component.value.substring(0,length);
	  }
	}catch(error){
		alert("An error occured and below is the error message.\n\n" + error.message);
	}
}

function setFocus(component,val){
	try{
	  component.focus();
	  if(val != ""){
	    component.value = val;
	  }
	}catch(error){
		alert("An error occured and below is the error message.\n\n" + error.message);
	}
}

function isValidDouble(value,left,right){
	try{
		var exp;
		if(value==null){
			return false;
		}else if(left=="0" && right=="0"){
			return value=="";
		}else if(left=="0"){
			exp = new RegExp("^\\.\\d{1," + right + "}$");
		}else if(right=="0"){
			exp = new RegExp("^\\d{0," + left + "}$");
		}else{
			exp = new RegExp("^\\d{0," + left + "}$|^\\d{0," + left + "}\\.\\d{1," + right + "}$");
		}
		return value.search(exp) >= 0;
	}catch(error){
		alert("An error occured and below is the error message.\n\n" + error.message);
	}
}

function compareNumbers(n1,n2){
	try{
		if(parseFloat(n1) < parseFloat(n2)){
			return -1;
		}else if(parseFloat(n1)==parseFloat(n2)){
			return 0;
		}else{
			return 1;
		}
	}catch(error){
		alert("An error occured and below is the error message.\n\n" + error.message);
	}
}

function getCurrentDateTimeString(){
	var calendar = new Date();
	var currentMonth = ""+(calendar.getMonth()+1);
	var currentDay = ""+calendar.getDate();
	var currentYear = ""+calendar.getFullYear();
	var currentHour = ""+calendar.getHours();
	var currentMinute = ""+calendar.getMinutes();
	while(currentMonth.length < 2){ currentMonth = "0" + currentMonth; }
	while(currentDay.length < 2){ currentDay = "0" + currentDay; }
	while(currentYear.length < 4){ currentYear = "0" + currentYear; }
	while(currentHour.length < 2){ currentHour = "0" + currentHour; }
	while(currentMinute.length < 2){ currentMinute = "0" + currentMinute; }
	return currentYear + "" + currentMonth + "" + currentDay + "" + currentHour + "" + currentMinute;
}

function rearrangeDate(dt){
	try{
	  var arr = dt.split("/");
	  return arr[2]+"/"+arr[0]+"/"+arr[1];
	}catch(error){
		alert("An error occured and below is the error message.\n\n" + error.message);
	}
}

function displayLoadingMessage(){
	try{
	  document.getElementById("loadingMessage").innerHTML = "<font style='color:red'>FINDING PAGE...</font>";
	  document.getElementById("loadingMessage").style.visibility = "visible";
	}catch(error){
		alert("An error occured and below is the error message.\n\n" + error.message);
	}
}

function lookup(val,arr,delim){
	return lookupWithNdx(val,arr,delim,1);
}

function lookupWithNdx(val,arr,delim,ndx){
	try{
		for(var i=0;i<arr.length;i++){
			var arr2 = arr[i].split(delim);
			if(arr2[0]==val){
				if(ndx < arr2.length){
					return arr2[ndx];
				}else{
					return "index beyond array size";
				}
			}
		}
		return "Value Not Found";
	}catch(error){
		alert("An error occured and below is the error message.\n\n" + error.message);
	}
}

function displayMessage(divName,message){
	try{
	  document.getElementById(divName).innerHTML = "<font style='color:red'>" + message + "</font>";
	  document.getElementById(divName).style.visibility = "visible";
	}catch(error){
		alert("An error occured and below is the error message.\n\n" + error.message);
	}
}

function getElement(fName,elementName){
	var i = 0;
	while(i < 3){
		i++;
		try{
			if(i==1 && fName.length > 0){
				window.document.forms[fName].elements[elementName].value;
				return window.document.forms[fName].elements[elementName];
			}else if(i==2){
				document.getElementById(elementName).value;
				return document.getElementById(elementName);
			}else if(i==3){
				document.all[elementName].value;
				return document.all[elementName];
			}
		}catch(error){}
	}
}

function changeSelectBoxValue(selectBox,value){
	for(var i=0;i<selectBox.options.length;i++){
		if(selectBox.options[i].value==value){
			selectBox.options[i].selected = true;
			return;
		}
	}
}

function isValidBoutString(boutBox){
	
	boutBox.value = replace(boutBox.value," ","");

	var boutNoRegExp = /^\s*\d{1,5}\s*$|^\s*\d{1,5}\s*\-\s*\d{1,5}\s*$|^((\s*\d{1,5}\s*)|(\s*\d{1,5}\s*\-\s*\d{1,5}\s*))(,((\s*\d{1,5}\s*)|(\s*\d{1,5}\s*\-\s*\d{1,5}\s*)))*,((\s*\d{1,5}\s*)|(\s*\d{1,5}\s*\-\s*\d{1,5}\s*))$/;
	if(boutBox.value==""){	/* if the user did not enter any bouts. */
		/* ok */
	}else if(boutBox.value.search(boutNoRegExp)==-1){	/* if the user did not enter a valid bout number string */
		alert("You have entered invalid syntax.\nPlease try again.\nFor Example: 1-12 or 1,3,5... or 1-12,18,19,24-26 are all valid.");
		boutBox.focus();
		return false;
	}else{
		if(boutBox.value.search(/,/) >= 0){	/* if the user entered a comma. */
			var boutArr = boutBox.value.split(/\s*,\s*/);	/* split the users entry using commas. */
			for(var i=0;i<boutArr.length;i++){	/* for each part of the users entry seperated by a comma. */
				if(boutArr[i].search(/\-/) >= 0){	/* if it contains a '-' */
					var boutRange = boutArr[i].split(/\s*\-\s*/);	/* split it using a dash. */
					if(parseInt(boutRange[0]) > parseInt(boutRange[1])){	/* make sure the left bout number is not greater than the right one */
						alert("You have entered a range where the left value is larger than the right one.");
						boutBox.focus();
						return false;
					}
				}
			}
		}else if(boutBox.value.search(/\-/) >= 0){	/* if the users entry did not contain commas but contains a dash. */
			var boutRange = boutBox.value.split(/\s*\-\s*/);	/* split it using a dash */
			if(parseInt(boutRange[0]) > parseInt(boutRange[1])){	/* make sure the left bout number is not greater than the right one. */
				alert("You have entered a range where the left value is larger than the right one.");
				boutBox.focus();
				return false;
			}
		}
	}
	return true;
}

function isValidBoutString2(boutStr){
	boutStr = replace(boutStr," ","");

	var boutNoRegExp = /^\s*\d{1,5}\s*$|^\s*\d{1,5}\s*\-\s*\d{1,5}\s*$|^((\s*\d{1,5}\s*)|(\s*\d{1,5}\s*\-\s*\d{1,5}\s*))(,((\s*\d{1,5}\s*)|(\s*\d{1,5}\s*\-\s*\d{1,5}\s*)))*,((\s*\d{1,5}\s*)|(\s*\d{1,5}\s*\-\s*\d{1,5}\s*))$/;
	if(boutStr==""){	/* if the user did not enter any bouts. */
		/* ok */
	}else if(boutStr.search(boutNoRegExp)==-1){	/* if the user did not enter a valid bout number string */
		return "You have entered invalid syntax.\nPlease try again.\nFor Example: 1-12 or 1,3,5... or 1-12,18,19,24-26 are all valid.";
	}else{
		if(boutStr.search(/,/) >= 0){	/* if the user entered a comma. */
			var boutArr = boutStr.split(/\s*,\s*/);	/* split the users entry using commas. */
			for(var i=0;i<boutArr.length;i++){	/* for each part of the users entry seperated by a comma. */
				if(boutArr[i].search(/\-/) >= 0){	/* if it contains a '-' */
					var boutRange = boutArr[i].split(/\s*\-\s*/);	/* split it using a dash. */
					if(parseInt(boutRange[0]) > parseInt(boutRange[1])){	/* make sure the left bout number is not greater than the right one */
						return "You have entered a range where the left value is larger than the right one.";
					}
				}
			}
		}else if(boutStr.search(/\-/) >= 0){	/* if the users entry did not contain commas but contains a dash. */
			var boutRange = boutStr.split(/\s*\-\s*/);	/* split it using a dash */
			if(parseInt(boutRange[0]) > parseInt(boutRange[1])){	/* make sure the left bout number is not greater than the right one. */
				return "You have entered a range where the left value is larger than the right one.";
			}
		}
	}
	return "";
}

function expandBoutString(str){
	var bouts = "";
	var arr = str.split(",");
	for(var a=0;a<arr.length;a++){
		var token = arr[a];
		var arr2 = token.split("-");
		if(arr2.length==2){
			var low = parseInt(arr2[0]);
			var high = parseInt(arr2[1]);
			for(var i=low;i<=high;i++){
				bouts += i + ",";
			}
		}else{
			bouts += token + ",";
		}
	}
	if(bouts.length > 0){
		return bouts.substring(0,bouts.length-1);
	}else{
		return "";
	}
}

function mouseOverLeftMenuItem(item){
	item.style.backgroundColor = "black";
	item.style.color = "white";
}

function mouseOffLeftMenuItem(item){
	item.style.backgroundColor = "";
	item.style.color = "#000080";
}

function getMatchedCharCount(s1,s2){
	if(s1==null || s2==null || s1.length==0 || s2.length==0){
		return 0;
	}
	return getMatchedCharCount2(s1.toLowerCase(),s2.toLowerCase());
}
function getMatchedCharCount2(s1,s2){
	if(s1==null || s2==null || s1.length==0 || s2.length==0){
		return 0;
	}else if(s1.substring(0,1)==s2.substring(0,1)){
		return 1 + getMatchedCharCount( s1.substring(1,s1.length) , s2.substring(1,s2.length) );
	}
	if(s1.length > s2.length){
		return getMatchedCharCount( s1.substring(1,s1.length) , s2 );
	}else{
		return getMatchedCharCount( s1 , s2.substring(1,s2.length) );
	}
}

function getWindowInnerHeight(){
	var h = -1;
	try{
		h = document.body.clientHeight;
	}catch(e1){
		try{
			h = window.innerHeight;
		}catch(e2){
			try{
				h = screen.availHeight;
			}catch(e3){
				h = 300;
			}
		}
	}
	return h;
}

function getMultipleSelectedValues(boxId){
	try{
		var result = "";
		var box = document.getElementById(boxId);
		for(var i=0;i<box.options.length;i++){
			if(box.options[i].selected && box.options[i].value!=""){
				result += "," + box.options[i].value;
			}
		}
		if(result!=""){
			return result.substring(1,result.length);	
		}
	}catch(e){
		alert(e);
	}
	return "";
}

function selectMultipleValues(boxId,values,delim){
	try{
		var arr = values.split(delim);
		var box = document.getElementById(boxId);
		for(var i=0;i<box.options.length;i++){
			var found = false;
			for(var j=0;j<arr.length;j++){
				if(arr[j]==box.options[i].value){
					box.options[i].selected = true;
					found = true;
					break;
				}
			}
			if(!found){
				box.options[i].selected = false;
			}
		}
	}catch(e){
		alert(e);
	}
}

function round(x,dec){
	var factor = 1;
	for(var i=0;i<dec;i++){
		factor *= 10;
	}
	return Math.round(x*factor)/factor;
}

function moneyFormat(m){
	return moneyFormatStr(""+round(m,2));
}

function moneyFormatStr(str){
	var periodIndex = str.indexOf(".");
	if(periodIndex < 0){
		return "$" + str + ".00";
	}else{
		str += "00";
		return "$" + str.substring(0,periodIndex+3);
	}
}

function validEmailAddress(email){
	return email.search(/^\S+@{1}\S+\.\S+$/) >= 0;
}

function parseWeight(theBox){
	try{
		var v = theBox.value;
		var s = -1;
		var e = -1;
		var isInt = false;
		for(var i=0;i<v.length;i++){
			var d = v.substring(i,i+1);
			if(d=="0" || d=="1" || d=="2" || d=="3" || d=="4" || d=="5" || d=="6" || d=="7" || d=="8" || d=="9" || d=="."){
				s = i;
				for(j=i;j<v.length;j++){
					d = v.substring(j,j+1);
					if(d=="0" || d=="1" || d=="2" || d=="3" || d=="4" || d=="5" || d=="6" || d=="7" || d=="8" || d=="9" || d=="."){
						e = j;
					}else{
						break;
					}
				}
				break;
			}
		}
		if(s > -1 && e > -1){
			theBox.value = v.substring(s,e+1);
		}
	}catch(e){}
}

function dashEncode(s1){
	if(s1==null){
		return null;
	}
	s1 = s1.replace(/[\r]+/g, "");
	var s2 = "";
	for( var i=0; i<s1.length; i++){
		s2 += s1.charAt(i) + "_";
	}
	s2 = s2.replace(/[\n]+/g, "[endLine]");
	return "[dashDecode]"+s2;
}

var bodyClickCount = 0;

function bodyClicked(){
	if(bodyClickCount==0){
		setTimeout("checkBodyClickCount()",5000);
	}
	bodyClickCount++;
}

function checkBodyClickCount(){
	if(bodyClickCount > 10){
		bodyClickCount = 0;
		if(confirm("You are about to open this page in a new window.  Click [Ok] if you would like to do that.")){
			var win = window.open(location.href, "newWin", "top=0,left=0,resizable=yes,scrollbars=yes,menubar=yes");
			win.focus();
		}
	}
	bodyClickCount = 0;
}

function myPrompt(msg,val){
	try{
		var str = prompt(msg + "  Type '" + val + "' to continue.","");
		if(str != null){
			if(str.toLowerCase()==val.toLowerCase()){
				return true;	
			}
			alert("You did not type '" + val + "' correctly.  Please try again.");
		}
	}catch(error){
		alert("An error occured when trying to prompt you for information.  Make sure you are allowing scripted windows.  Look in the upper left corner of your screen for a message about scripted windows being blocked.");
	}
	return false;
}