var window_handle_HELP = null;
function getHelp(sFolder, sSection)
{
	w = 635;
	h = 500;

	windowprops='width=' + w + ',height=' + h + ',location=no,scrollbars=yes,menubars=no,toolbars=no,resizable=yes';
	window_handle_HELP=window.open('Help.asp?Folder=' + sFolder + '&Section=' + sSection + '#' + sSection,'popupHelp',windowprops);
								
	var xPos = (screen.availWidth-w) * 0.75;
	var yPos = (screen.availHeight-h) * 0.25;
							  
	window_handle_HELP.moveTo(xPos,yPos);
	window_handle_HELP.focus();
}

function Trim(strValue)
{
	return TrimLeading(TrimTrailing(strValue));
}

function TrimLeading(strValue)
{
	if(strValue != "") 
	{
		var strchar = strValue.charAt(0);
		while(strchar == ' ')
		{
			strValue = strValue.substr(1);
			strchar = strValue.charAt(0);
		}
	}

	return strValue;
}

function TrimTrailing(strValue)
{
	if(strValue != "") 
	{
		var strchar = strValue.charAt(strValue.length - 1);
		while(strchar == ' ')
		{
			strValue = strValue.substr(0,strValue.length - 1);
			strchar = strValue.charAt(strValue.length - 1);
		}
	}

	return strValue;
}


function isEmail(sString) 
{
  var supported = 0;
  if (window.RegExp) 
  {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return (sString.indexOf(".") > 2) && (sString.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(sString) && r2.test(sString));
}

function isPhoneNumber(strFormElement)
{
	var OKPhone = "0123456789().- ";
	for (i = 0; i < strFormElement.length; i++)
	{
		var theChar = strFormElement.charAt(i);
		if ((OKPhone.indexOf(theChar) == -1) || (strFormElement.length < 7))
			return false;
	}
	
	return true;
}

function isInteger(strValue)
{
	var OKDigit = "-0123456789";
	for (i=0; i<strValue.length; i++)
	{
		var theChar = strValue.charAt(i);
		if (OKDigit.indexOf(theChar) == -1)
			return false;
	}
	
	return (strValue >= -2147483648) && (strValue <= 2147483647);
}

function isIntegerInRange(strValue, LBound, UBound)
{
	if (!isInteger(Trim(strValue))) return false;

	var num = parseInt(Trim(strValue),10);
	return ((num >= LBound) && (num <= UBound));
}

function isDigit(c)
{
	return ((c >= "0") && (c <= "9"))
}


// Public Date function, checks the date string, in seperate components for validity such as february not
// having more than 28 or 29 days and returns the date value in db acceptable format complete with four
// digit years. Also if year is under 30 returns the year in 2000 format
function ValidateDate(field,formname,label,nullable) {
	var gField = document.forms[formname][field];
	var inputStr = TrimLeading(gField.value);
	var ValDate = true;
	
	switch (inputStr){
		case "":
			if (!nullable){
				alert("Please enter a valid date in the " + label + " field.");
				ValDate = false;
			}else{
				ValDate = true;
			}
			break;
		default:
			// Replace all dashes (-) with slashes (/)
			inputStr = inputStr.replace(/-/g,"/");
				
			var delim1 = inputStr.indexOf("/");
			var delim2 = inputStr.lastIndexOf("/");
			if (delim1 != -1 && delim1 == delim2) {
				alert("The date entry in the " + label + " field is not in an acceptable format.\nYou can enter dates in the following formats: mm/dd/yyyy, or mm-dd-yyyy.");
				ValDate = false;
				break;
			}
			// Seperate the parts of the date for further analysis and computation
			if (delim1 != -1) {
				var mm = parseInt(inputStr.substring(0,delim1),10);
				var dd = parseInt(inputStr.substring(delim1 + 1,delim2),10);
	//
	//Changes made to correct for years longer than 4 digits - eric
	//
				//var yyyy = parseInt(inputStr.substring(delim2 + 1, inputStr.length),10);
				var yyyy = parseInt(inputStr.substring(delim2 + 1, delim2 + 5),10);
			} else {
				var mm = parseInt(inputStr.substring(0,2),10);
				var dd = parseInt(inputStr.substring(2,4),10);
	//
	//Changes made to correct for years longer than 4 digits - eric
	//
				//var yyyy = parseInt(inputStr.substring(4,inputStr.length),10);
				var yyyy = parseInt(inputStr.substring(delim2 + 1, delim2 + 5),10);
			}
				
			if (isNaN(mm) || isNaN(dd) || isNaN(yyyy)) {
				alert("The date entry in the " + label + " field is not in an acceptable format.\nYou can enter dates in the following formats: mmddyyyy, mm/dd/yyyy, or mm-dd-yyyy.");
				ValDate = false;
				break;
			}
			if (mm < 1 || mm > 12) {
				alert("The date in the " + label + " field is invalid.\nMonths must be entered between the range of 01 (January) and 12 (December).");
				ValDate = false;
				break;
			}
			if (dd < 1 || dd > 31) {
				alert("The date in the " + label + " field is invalid.\nDays must be entered between the range of 01 and a maximum of 31 (depending on the month and year).");
				ValDate = false;
				break;
			}
			if (yyyy < 100) {
				if (yyyy >= 50) {
					yyyy += 1900;
				} else {
					yyyy += 2000;
				}
	//
	//added this case for SQL error on entering years less than 1900 but greater than 100 - eric
	//
			}else if(yyyy < 1900){
			  alert("The date in the " + label + " field is invalid.\nYear entered must be later than 1900.");
			  ValDate = false;
			  break;
			}
			  
			if (!checkMonthLength(mm,dd,label)) {
				ValDate = false;
				break;
			}
			if (mm == 2) {
				if (!checkLeapMonth(mm,dd,yyyy,label)) {
					ValDate = false;
					break;
				}
			}
			break;	
	}
	if (!ValDate){
		gField.focus();
		gField.select();
	}else{
		// Concatenate the date parts back together in acceptable format (four digit year and slashes)
		if (inputStr != ""){
			gField.value = new String((mm) + "/" + (dd) + "/" + yyyy);
		}
	}
	return ValDate;
}
	
// Private Month Length function checks the length of a month and determines whether the value is in the 
// specified range such as 30 days for November, 31 for December
function checkMonthLength(mm,dd,label) {
	var months = new Array("","January","February","March","April","May","June","July","August","September","October","November","December");
	if ((mm == 4 || mm == 6 || mm == 9 || mm == 11) && dd > 30) {
		alert("Invalid date in the " + label + " field.\n" + months[mm] + " has only 30 days.");
		return false;
	} else if (dd > 31) {
		alert("Invalid date in the " + label + " field.\n" + months[mm] + " has only 31 days.");
		return false;
	}
	return true;
}
	
// Private Leap Month function checks the month for Feburary and determines whether it is a leap year 
// returning 29 days or not
function checkLeapMonth(mm,dd,yyyy,label) {
	var NumDays = FebruaryDays(yyyy);
	if (dd > NumDays){
		alert("Invalid date in the " + label + " field.\nFebruary of " + yyyy + " has only " + NumDays + " days.");
		return false;
	}
	return true;
}
	
function FebruaryDays(yyyy){
	if (yyyy % 4 == 0){
		if (yyyy % 100 == 0){
			if (yyyy % 400 == 0){
				return 29;
			}else{
				return 28;
			}
		}else{
			return 29;
		}
	}else{
		return 28;
	} 
}



function isTime(strTime, formname, field)
{
	var gField = document.forms[formname][field];
	var theString = new String(strTime);
		
	// the string must have either two (hours and minutes) or three
	// (hours, minutes and seconds) elements, delimited by ":";
	// split the string into an array of elements
	var theElements = theString.split(':');
	if(theElements.length < 2 || theElements.length > 2)
		return false;
		
	// convert the elements to String objects, which will be needed later,
	// stripping whitespace
	var firstElement = new String(theElements[0])
	firstElement = Trim(firstElement);
	var middleElement;
	if(theElements.length == 3)
	{
		middleElement = new String(theElements[1])
		middleElement = Trim(middleElement);
	}
	var lastElement = new String(theElements[theElements.length - 1])
	lastElement = Trim(lastElement);


	// the first element (hours) must be an integer between 0 and 23
	if(!isInteger(firstElement))
		return false;
	if(!isIntegerInRange(firstElement, 0, 23))
		return false;

		
	// are there three elements?
	if(theElements.length == 3)
	{
		// the middle element (minutes) must be an integer between 0 and 59
		if(!isInteger(middleElement))
			return false;
		if(!isIntegerInRange(middleElement, 0, 59))
			return false;
	}
			
	// the first one or two characters of the last element (either minutes
	// and optional am/pm indicator or seconds and am/pm indicator) must
	// be digits
	if(!isDigit(lastElement.charAt(0)))
		return false;
		
	// the first character is a digit; split the last element into the minutes
	// or seconds value and the indicator; depending on the second character
	var lastValue;
	var ampmIndicator;
	var therest;

	if(isDigit(lastElement.charAt(1)))
	{
		lastValue = new String(lastElement.substring(0, 2));
		if(lastElement.length >= 3)
		{
			ampmIndicator = new String(Trim(lastElement.substring(3, 5))); // lastElement.length)));
			if(lastElement.length >= 5)
				therest = new String(Trim(lastElement.substring(5, lastElement.length)));
			else
				therest = "";
		}
		else
		{
			ampmIndicator = new String();
			therest = new String();
		}
	}
	else
	{
		lastValue = new String(lastElement.substring(0, 1));
		if(lastElement.length >= 2)
		{
			ampmIndicator = new String(trim(lastElement.substring(2, 4))); // lastElement.length)));
			if(lastElement.length >= 4)
				therest = new String(Trim(lastElement.substring(4, lastElement.length)));
			else
				therest = "";
		}
		else
		{
			ampmIndicator = new String();
			therest = new String();
		}
	}
	ampmIndicator = ampmIndicator.toUpperCase();
	therest = " " + therest.toUpperCase();
	
	// the last value must be between 0 and 59
	if(!isIntegerInRange(lastValue, 0, 59))
		return false;
		
	// check the am/pm indicator, if there is one
	if(ampmIndicator.length > 0)
	{
		if(!(ampmIndicator == "AM" || ampmIndicator == "PM"))
			return false;
	}
				

	// reformat the time - efischer 05/07/2002
	sMiddle = lastElement.split(" ")[0];
	if(ampmIndicator == "")
		(firstElement >= 12)?ampmIndicator="PM":ampmIndicator="AM";
	else
		(firstElement >= 12 && ampmIndicator=="AM")?ampmIndicator="PM":ampmIndicator=ampmIndicator;
		
	if(firstElement > 12)
		gField.value = new String(firstElement-12 + ":" + sMiddle + " " + ampmIndicator + therest);
	else
		gField.value = new String(firstElement + ":" + sMiddle + " " + ampmIndicator + therest);
	// end reformat time



	// valid time
	return true;
}
