document.write("<div class='focusDiv' id='focusDiv'></div>");

function leaveBox(e){
	if(e.value==e.title || e.value==""){
		e.value = e.title;
		e.className = "unchanged";
	}else{
		e.className = "changed";
	}
	document.getElementById("focusDiv").style.visibility = "hidden";
}

function focusBox(e){
	e.className = "focus";
	if(e.value==e.title){
		e.value = "";
	}
	var f = document.getElementById("focusDiv");
	f.style.visibility = "visible";
	eWidth = getWidth(e);
	f.innerHTML = "<table class='focusTable'><tr><td style='width:" + (parseInt(eWidth)-8) + "' nowrap>" + e.title + "</td></tr></table>";
	f.style.left = getX(e);
	f.style.top =  getY(e)-24;
}

function leaveSelect(e){
	if(e.selectedIndex >= 0 && e.options[e.selectedIndex].value!=""){
		e.className = "changed";
	}else{
		e.className = "unchanged";
	}
	document.getElementById("focusDiv").style.visibility = "hidden";
}

function focusSelect(e){
	//e.className = "focus";
	var f = document.getElementById("focusDiv");
	f.style.visibility = "visible";
	eWidth = getWidth(e);
	f.innerHTML = "<table class='focusTable'><tr><td style='width:" + (parseInt(eWidth)-8) + "' nowrap>" + e.title + "</td></tr></table>";
	f.style.left = getX(e);
	f.style.top =  getY(e)-24;
}

function leaveStartDate(prefix){
	var sm = document.getElementById(prefix + "1Month");
	var sd = document.getElementById(prefix + "1Day");
	var sy = document.getElementById(prefix + "1Year");
	var em = document.getElementById(prefix + "2Month");
	var ed = document.getElementById(prefix + "2Day");
	var ey = document.getElementById(prefix + "2Year");	
	if( sm.value!="" && sm.value!=sm.title && (em.value=="" || em.value==em.title) ){
		if( sd.value!="" && sd.value!=sd.title && (ed.value=="" || ed.value==ed.title) ){
			if( sy.value!="" && sy.value!=sy.title && (ey.value=="" || ey.value==ey.title) ){
				em.value = sm.value;
				ed.value = sd.value;
				ey.value = sy.value;
			}
		}
	}
}

function getY( oElement ){
	var iReturnValue = 0;
	while( oElement != null ) {
		iReturnValue += oElement.offsetTop;
		oElement = oElement.offsetParent;
	}
	return iReturnValue;
}

function getX( oElement ){
	var iReturnValue = 0;
	while( oElement != null ) {
		iReturnValue += oElement.offsetLeft;
		oElement = oElement.offsetParent;
	}
	return iReturnValue;
}

function getWidth( oElement ){
	if(oElement.currentStyle){
		return oElement.currentStyle["width"];
	}else{
		return oElement.offsetWidth;
	}
}

function getHeight( oElement ){
	return oElement.offsetHeight;
}

function validatePassword(pwBox1, pwBox2, minLen, maxLen ){
	
	if( pwBox1.title != "" && pwBox1.title==pwBox1.value){
		pwBox1.value = "";
	}	
	
	if( minLen > 0 && pwBox1.value.length < minLen ){
		alert("The password you enter must be at least " + minLen + " characters.");
		pwBox1.focus();
		return false;
	}
	
	if(maxLen >= minLen && pwBox1.value.length > maxLen){
		alert("The password you enter can be at most " + maxLen + " characters.");
		pwBox1.focus();
		return false;
	}
	
	if(containsBadChars2(pwBox1.value)){
		alert("The password you enter cannot contain any of the following characters ' or \" or ~");
		pwBox1.focus();
		return false;		
	}

	if( pwBox2.title != "" && pwBox2.title==pwBox2.value){
		pwBox2.value = "";
	}
	
	if(pwBox1.value != pwBox2.value){
		alert("The passwords you entered did not match.  Please try again.");
		pwBox2.focus();
		return false;		
	}
	
	return true;
	
}

function validateString(theBox, minLen, maxLen){
	var theTitle = theBox.title;
	if(theTitle!=""){
		theTitle = " for " + theTitle;
	}
	
	// if the element was given a title
	if( theBox.title != "" ){
		if(theBox.title==theBox.value){
			theBox.value = "";
		}
	}

	// if there is a min length and the value is less than that length
	if(minLen > 0 && theBox.value.length < minLen){
		alert("The value you enter" + theTitle + " must be at least " + minLen + " characters.");
		theBox.focus();
		return false;
	}
	
	// if there is a max length and the value is greater than that length
	if(maxLen >= minLen && theBox.value.length > maxLen){
		alert("The value you enter" + theTitle + " can be at most " + maxLen + " characters.");
		theBox.focus();
		return false;
	}
	
	theBox.value = removeBadChars2(theBox.value);
	
	return true;
}

function validateInteger(theBox, minLen, maxLen, minVal, maxVal){
	
	var theTitle = theBox.title;
	if(theTitle!=""){
		theTitle = " for " + theTitle;
	}
	
	// if the element was given a title
	if( theBox.title != "" ){
		if(theBox.title==theBox.value){
			theBox.value = "";
		}
	}
	
	var initLen = theBox.value.length;
	
	theBox.value = removeLeadingZeros(theBox.value);
	
	// if the value is not a valid number
	if( theBox.value != "" && !isValidDouble2(theBox.value,100,0) ){
		alert("You must enter a valid single number" + theTitle);
		theBox.focus();
		return false;
	}

	// if there is a min length and the value is less than that length
	if(minLen > 0 && theBox.value.length < minLen){
		alert("The value you enter" + theTitle + " must be at least " + minLen + " digits.");
		theBox.focus();
		return false;
	}
	
	// if there is a max length and the value is greater than that length
	if(maxLen >= minLen && theBox.value.length > maxLen){
		alert("The value you enter" + theTitle + " can be at most " + maxLen + " digits.");
		theBox.focus();
		return false;
	}
	
	// if there is a min value and the value is less than that value
	if(theBox.value != "" && minVal > 0 && parseInt(theBox.value) < minVal ){
		alert("The value you enter" + theTitle + " must be greater than or equal to " + minVal + ".");
		theBox.focus();
		return false;		
	}
	
	// if there is a max value and the value is greater than that length
	if(theBox.value != "" && maxVal >= minVal && parseInt(theBox.value) > maxVal){
		alert("The value you enter" + theTitle + " must be less than or equal to " + maxVal + ".");
		theBox.focus();
		return false;
	}
	
	theBox.value = padLeft(theBox.value,initLen,"0");
	
	return true;
	
}

function validateDouble(theBox, required, minLenLeft, maxLenLeft, minLenRight, maxLenRight, minVal, maxVal){
	var theTitle = theBox.title;
	if(theTitle!=""){
		theTitle = " for " + theTitle;
	}
	
	// if the element was given a title
	if( theBox.title != "" ){
		if(theBox.title==theBox.value){
			theBox.value = "";
		}
	}
	
	// if the value is not a valid double
	if( theBox.value != "" && !isValidDouble2(theBox.value,maxLenLeft,maxLenRight) ){
		alert("You must enter a number" + theTitle + " with at most " + maxLenLeft + " digits left of the decimal, and " + maxLenRight + " to the right of it.");
		theBox.focus();
		return false;
	}
	
	theBox.value = removeLeadingZeros(theBox.value);
	
	var l = "";
	var r = "";
	var arr = theBox.value.split(".");
	if(arr != null){
		l = arr.length > 0 ? arr[0] : "";
		r = arr.length > 1 ? arr[1] : "";
	}
	
	if(required && r.length + l.length == 0){
		alert("You must enter a value" + theTitle + ".");
		theBox.focus();
		return false;
	}
	
	// if there is a min length and the value is less than that length
	if(minLenLeft > 0 && l.length < minLenLeft){
		alert("The value you enter" + theTitle + " must have at least " + minLenLeft + " digits left of the decimal.");
		theBox.focus();
		return false;
	}	
	
	// if there is a min length and the value is less than that length
	if(minLenRight > 0 && r.length < minLenRight){
		alert("The value you enter" + theTitle + " must have at least " + minLenRight + " digits right of the decimal.");
		theBox.focus();
		return false;
	}
	
	// if there is a min value and the value is less than that value
	if(theBox.value != "" && minVal > 0 && parseFloat(theBox.value) < minVal ){
		alert("The value you enter" + theTitle + " must be greater than or equal to " + minVal + ".");
		theBox.focus();
		return false;		
	}
	
	// if there is a max value and the value is greater than that length
	if(theBox.value != "" && maxVal >= minVal && parseFloat(theBox.value) > maxVal){
		alert("The value you enter" + theTitle + " must be less than or equal to " + maxVal + ".");
		theBox.focus();
		return false;
	}	
	
	return true;
	
}

function validateEmail(theBox,required){
	// if the element was given a title
	if( theBox.title != "" ){
		if(theBox.title==theBox.value){
			theBox.value = "";
		}
	}
	if( required ){
		if(theBox.value=="" || !validEmailAddress(theBox.value)){
			alert("You must enter a valid email address");
			theBox.focus();
			return false;
		}
	}else{
		if(theBox.value!="" && !validEmailAddress(theBox.value)){
			alert("You must enter a valid email address");
			theBox.focus();
			return false;		
		}
	}
	theBox.value = removeBadChars2(theBox.value);
	return true;
}

function validEmailAddress(email){
	return email.search(/^\S+@{1}\S+\.\S+$/) >= 0;
}

function validatePhone(theBox,required){
	// if the element was given a title
	if( theBox.title != "" ){
		if(theBox.title==theBox.value){
			theBox.value = "";
		}
	}
	theBox.value = getInteger(theBox.value);
	var valid = validateInteger(theBox, (required?10:0), 10, 0, -1);
	if(valid && theBox.value != ""){
		theBox.value = "(" + theBox.value.substring(0,3) + ") " + theBox.value.substring(3,6) + "-" + theBox.value.substring(6,10);
	}
	return valid;
}

function validateSelect(theBox){
	var theTitle = theBox.title;
	if(theTitle!=""){
		theTitle = " for " + theTitle;
	}	
	if(theBox.title==theBox.value || theBox.value==""){
		alert("You must select a value" + theTitle);
		theBox.focus();
		return false;
	}
	return true;
}

function validateDateTime(prefix, required){
	var month = document.getElementById(prefix+"Month");
	if( month.title != "" ){
		if(month.title==month.value){
			month.value = "";
		}
	}
	var day = document.getElementById(prefix+"Day");
	if( day.title != "" ){
		if(day.title==day.value){
			day.value = "";
		}
	}
	var year = document.getElementById(prefix+"Year");
	if( year.title != "" ){
		if(year.title==year.value){
			year.value = "";
		}
	}
	var hour = document.getElementById(prefix+"Hour");
	if( hour.title != "" ){
		if(hour.title==hour.value){
			hour.value = "";
		}
	}
	var min = document.getElementById(prefix+"Min");
	if( min.title != "" ){
		if(min.title==min.value){
			min.value = "";
		}
	}
	var amPm = document.getElementById(prefix+"AmPm");
	
	if(required){
		if( !validateInteger(month,1,2,1,12) || !validateInteger(day,1,2,1,31) || !validateInteger(year,4,4,1900,3000) || !validateInteger(hour,1,2,1,12) || !validateInteger(min,1,2,0,59) || !validateSelect(amPm,true) ){
			return false;
		}else{
			month.value = padLeft(month.value,2,"0");
			day.value = padLeft(day.value,2,"0");
			hour.value = padLeft(hour.value,2,"0");
			min.value = padLeft(min.value,2,"0");
		}
	}else if(month.value+day.value+year.value+hour.value+min.value != ""){
		if( !validateInteger(month,1,2,1,12) || !validateInteger(day,1,2,1,31) || !validateInteger(year,4,4,1900,3000) || !validateInteger(hour,1,2,1,12) || !validateInteger(min,1,2,0,59) || !validateSelect(amPm,true) ){
			return false;
		}else{
			month.value = padLeft(month.value,2,"0");
			day.value = padLeft(day.value,2,"0");
			hour.value = padLeft(hour.value,2,"0");
			min.value = padLeft(min.value,2,"0");
		}
	}
	
	return true;
}

function validateDate(prefix, required){
	var month = document.getElementById(prefix+"Month");
	if( month.title != "" ){
		if(month.title==month.value){
			month.value = "";
		}
	}
	var day = document.getElementById(prefix+"Day");
	if( day.title != "" ){
		if(day.title==day.value){
			day.value = "";
		}
	}
	var year = document.getElementById(prefix+"Year");
	if( year.title != "" ){
		if(year.title==year.value){
			year.value = "";
		}
	}
	if(required){
		if( !validateInteger(month,1,2,1,12) || !validateInteger(day,1,2,1,31) || !validateInteger(year,4,4,1900,3000) ){
			return false;
		}else{
			month.value = padLeft(month.value,2,"0");
			day.value = padLeft(day.value,2,"0");
		}
	}else if(month.value+day.value+year.value != ""){
		if( !validateInteger(month,1,2,1,12) || !validateInteger(day,1,2,1,31) || !validateInteger(year,4,4,1900,3000) ){
			return false;
		}else{
			month.value = padLeft(month.value,2,"0");
			day.value = padLeft(day.value,2,"0");
		}
	}
	
	return true;
}

function getHour(hour,amPm){
	if(hour==""){ return ""; }
	hour = removeLeadingZeros(hour);
	var h = parseInt(hour);
	if(amPm=="am"){
		if(hour==12){
			return "00";
		}else{
			return padLeft(""+h,2,"0");
		}
	}else{
		if(hour==12){
			return "12";
		}else{
			return padLeft(""+(h+12),2,"0");
		} 
	}
}

function validateRange(theBox,required){
	var theTitle = theBox.title;
	if(theTitle!=""){
		theTitle = " for " + theTitle;
	}
	
	// if the element was given a title
	if( theBox.title != "" ){
		if(theBox.title==theBox.value){
			theBox.value = "";
		}
	}

	if(required && theBox.value==""){
		alert("You must enter a value" + theTitle);
		theBox.focus();
		return false;
	}
	
	var str = isValidRangeString(theBox.value);
	if(str!=""){
		alert(str);
		theBox.focus();
		return false;
	}
	
	return true;
}

function isValidRangeString(rangeStr){
	rangeStr = replace2(rangeStr," ","");

	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(rangeStr==""){	// if the user did not enter any bouts.
		// ok
	}else if(rangeStr.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(rangeStr.search(/,/) >= 0){	// if the user entered a comma.
			var boutArr = rangeStr.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(rangeStr.search(/\-/) >= 0){	// if the users entry did not contain commas but contains a dash.
			var boutRange = rangeStr.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 replace2(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 removeBadChars2(str){
	try{
		return replace2(replace2(replace2(str,"'","`"),"\"","`"),"~","-");
	}catch(error){
		alert("An error occured and below is the error message.\n\n" + error.message);
	}
}

function containsBadChars2(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 padLeft(str,len,padder){
	while(str.length < len){
		str = padder + str;
	}
	return str;
}

function padRight(str,len,padder){
	while(str.length < len){
		str += padder;
	}
	return str;
}

function removeLeadingZeros(str){
	while(str.length > 1){
		if(str.substring(0,1)=="0"){
			str = str.substring(1);
		}else{
			break;
		}
	}
	return str;
}

function isValidDouble2(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 getDateText(prefix){
	var y = document.getElementById(prefix+"Year");
	var m = document.getElementById(prefix+"Month");
	var d = document.getElementById(prefix+"Day");
	if(y==null || m==null || d==null){
		return "";
	}else{
		return y.value + m.value + d.value;
	}
}

function getInteger(str){
	var result = "";
	for(var i=0;i<str.length;i++){
		var d = str.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"){
			result += d;
		}
	}
	return result;
}

function getDouble(str){
	var arr = str.split(".");
	if(arr.length > 1){
		var left = parseDigits(arr[0]);
		var right = parseDigits(arr[1]);
		if(right.length==0){
			return left;
		}else{
			return left + "." + right;
		}
	}else if(arr.length==1){
		return parseDigits(arr[0]);
	}else{
		return "";
	}
}