function ValidateCode(inputString){
	if (inputString == "") return(true);

	// Explicitely allowed characters
	var allowedChars = new RegExp("[-a-zA-Z0-9.?_\\\\/ ]+");

	// See if inputString contains only legal characters
	var matchResults = allowedChars.exec(inputString);
	if (matchResults == null) return(false);
	if (matchResults.index != 0) return(false);
	if (matchResults.length != 1) return(false);
	if (matchResults[0] != inputString) return(false);
	
	// The inputString is valid
	return(true);
}

function ValidateNumber(inputString){
	if (inputString == "") return(true);
	// Explicitely allowed characters
	var formatRegExp = /^-{0,1}[,0-9]*\.{0,1}\d*$/;
	// See if inputString contains only legal characters.
	var matchResults = formatRegExp.test(inputString);
	return(matchResults);
}

function CheckValue(callingId){
	var formField = getFormElement(callingId);
	// Validate new value
	if (ValidateCode(formField.value)){
		// Code is good, submit the form to do server-side validation
		// and field updating.
		if (formField.value != ""){
		var submitId = "Search" + callingId;
		var submitField = getFormElement(submitId);
		submitField.click();
		}
	} else {
		// Value isn't possibly a valid code. Tell the user and clear
		// the field.
		alert("The code you entered contains illegal characters.\n" +
			"Codes contain only letters, numbers, and dashes.\n" +
			"The value you entered is: '" + formField.value + "'");
		formField.value = "";
		var descId = callingId + "Desc";
		var descField = getFormElement(descId);
		//descField.innerHTML = "&nbsp;";
	}
	return;
}

function SubmitForm(callingId) {
	var formField = getFormElement(callingId);
	var formObj = formField.form;
	formObj.submit();
	return(true);
}

function CheckIntValue(callingId){
	if (isNaN(callingId)) {
		alert("'" + callingId + "' is not a valid integer. Please enter a valid integer");
		callingId = "";
		return false;
	}  else {
		return true;
	}
}

function CheckIntAndNullValue(callingId, fieldName){
	if (callingId == "") {
		alert("'" + fieldName + "' cannot be null. Please enter a valid integer");
		callingId = "";
		return false;
	} else {
		if (isNaN(callingId)) {
			alert("'" + callingId + "' is not a valid integer. Please enter a valid integer");
			callingId = "";
			return false;
		}  else {
			return true;
		}
	}
}

function CheckDateValue(callingId){
	var isGood = true;
	var formData = getFormElement(callingId);
	formData.value = formData.value.replace(/^\s*|\s*$/g,"");
	var formField = formData.value;
	
	
	if(formData.value == "")
		return true;
		
		
	// Validate new value
	var formatRegExp = /^\d{1,2}\/\d{1,2}\/(\d{4}|\d{2})$/;
	var formatResults = formatRegExp.test(formField);
	if (!formatResults) {
		isGood = false;
	} else {
		// Date format is good. Check the year value.
		var dateParts = formField.split("/");
		if ((dateParts[2] < 1950 || dateParts[2] > 3000) && (dateParts[2] < 0 || dateParts[2] > 99)) {
			isGood = false;
		} else {
			// Check the month value:
			if (dateParts[0] < 1 || dateParts[0] > 12) {
				isGood = false;
			} else {
				// Check the day value:
				var DaysInMonths = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
				if (dateParts[1] < 1 || dateParts[1] > DaysInMonths[dateParts[0] - 1]) {
					// Check for leap year:
					if (dateParts[1] != 29 || dateParts[0] != 2 ||
						(dateParts[2] % 4 != 0)) {
						isGood = false;
					}
				}
			}
		}
		
	}
	if (!isGood) {
		// Value isn't a valid date. Tell the user and clear
		// the field.
		if (formData.value != "")
		{
			alert("'" + formField + "' is not a valid date. Please enter a valid date" +
				"in the format: MM/DD/YYYY");
			formData.value = "";
		}
	}
	return(isGood);
}

function TextCheckValue(callingId) {
	var formField = getFormElement(callingId);
	// Submit form with the "Search" + callingId button.
	// If it doesn't exist, just submit the form.
	var submitId = "Search" + callingId;
	var submitField = getFormElement(submitId);
	if (submitField != null && submitField != undefined) {
		//submitField.click();
	} else {
		var formObj = formField.form;
		//formObj.submit();
	}
	return;
}
function populateDesc(formElt, formTxt) {
	if (confirm('You changed the request code, overwrite the Request Comments?\n OK = Yes, Cancel = No'))
	{
		var formField = getFormElement('Description');
		var formEltParts = "";
		var formDesc = getFormElement('CommentsDesc');
		formDesc.value = '';
		var posColon = formTxt.indexOf(':');
		if (posColon != -1) 
		{
			if(formTxt.length <= posColon + 2)
				{formField.value = '';}
			else{
			formField.value = formTxt.substring(posColon+2, formTxt.length);}
		}
		document.Form2.writeComments.value = 1;
	}
	else
	{
		document.Form2.writeComments.value = 2;
	}
	document.Form2.submit();
}

function populateDesc2(formElt, formTxt) {
	document.Form2.writeComments.value = 2;
	document.Form2.submit();
}

function TextCheckNumber(callingId) {
	var formField = getFormElement(callingId);

	// Validate new number
	if (ValidateNumber(formField.value)){
		// Number is good, submit the form to do server-side validation
		// and field updating. Use the "Search" + callingID button. If
		// it doesn't exist, just submit the form.
		var submitId = "Search" + callingId;
		var submitField = getFormElement(submitId);
		if (submitField != null && submitField != undefined) {
			//submitField.click();
		} else {
			var formObj = formField.form;
			//formObj.submit();
		}
	} else {
		// Value isn't possibly a valid number. Tell the user and clear
		// the field.
		alert("The data you entered isn't a valid number: '" + formField.value + "'");
		formField.value = "";
		var descId = callingId + "Desc";
		var descField = getFormElement(descId);
		descField.innerHTML = "&nbsp;";
	}
	return;
}
function TextCheckDesc(callingId) {
	var formField = getFormElement(callingId);
	// Submit the form.
	var formObj = formField.form;
	alert(formobj);
	//formObj.submit();
	return;
}

function getFormElement(elementId) {
	var allForms = document.forms;
	var numForms = allForms.length;
	var currentForm;
	var allElements, numElements, currentElement;
	var i, j;
	for (i = 0; i < numForms; i++) {
		currentForm = allForms[i];
		allElements = currentForm.elements;
		numElements = allElements.length;
		for (j = 0; j < numElements; j++) {
			currentElement = allElements[j];
			if (String(currentElement.name).toUpperCase() == String(elementId).toUpperCase()) {
				return(currentElement);
			}
		}
	}
	return(null);
}

function CheckTimeValue(callingId){
	var isGood = true;
	var formData = getFormElement(callingId);
	formData.value = formData.value.replace(/^\s*|\s*$/g,"");
	formData.value = formData.value.toUpperCase();
	
	var formField = formData.value;
	var formatRegExp1 = /^\d{1,2}\:\d{1,2}\:\d{1,2}\s[A|P](M)$/;
	var formatResults1 = formatRegExp1.test(formField);
	if(formField == "")
		return true;
		
	if (!formatResults1) {
		isGood = false;
	} else {
		// Time format is good. Check the seconds value.
		var timeParts = formField.split(":");
		if ((timeParts[2] >= 60) || (timeParts[2] < 0) || (timeParts[1] >= 60) || (timeParts[1] < 0) || (timeParts[0] > 12) || (timeParts[0] < 0 )) {
			isGood = false;
		} else {
			// Check AM and PM
			var twoParts = formField.split(" ");
			if (twoParts[1] != "AM" && twoParts[1] != "PM") {
				isGood = false;
			} 
		}
	}
	
	if (!isGood) {
		alert("'" + formField + "' is not a valid time. Please enter a valid time in the format: HH:MM:SS AM/PM");
		formData.value = "HH:MM:SS AM/PM";
	}
	return (isGood); 
}

function emailCheck (callingId) {

var formField = getFormElement(callingId);
if(!formField)
{
	return;
}
var emailStr = formField.value;
if( emailStr == "" )
	return true;
/* The following pattern is used to check if the entered e-mail address
   fits the user@domain format.  It also is used to separate the username
   from the domain. */
var emailPat=/^(.+)@(.+)$/
/* The following string represents the pattern for matching all special
   characters.  We don't want to allow special characters in the address. 
   These characters include ( ) < > @ , ; : \ " . [ ]    */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
/* The following string represents the range of characters allowed in a 
   username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"
/* The following pattern applies if the "user" is a quoted string (in
   which case, there are no rules about which characters are allowed
   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
   is a legal e-mail address. */
var quotedUser="(\"[^\"]*\")"
/* The following pattern applies for domains that are IP addresses,
   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
   e-mail address. NOTE: The square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
/* The following string represents an atom (basically a series of
   non-special characters.) */
var atom=validChars + '+'
/* The following string represents one word in the typical username.
   For example, in john.doe@somewhere.com, john and doe are words.
   Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")"
// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


/* Finally, let's start trying to figure out if the supplied address is
   valid. */

/* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
	alert("Email address seems incorrect (check @ and .'s)")
	formField.select();
    formField.focus();
    return false
    //return true
}
var user=matchArray[1]
var domain=matchArray[2]

if (user.match(specialChars)) {
	alert("The username has invalid characters in it.")
	formField.select();
    formField.focus();
    return false
}

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    alert("The username doesn't seem to be valid.")
    formField.select();
    formField.focus();
    return false
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        alert("Destination IP address is invalid.")
		formField.select();
		formField.focus();
		return false
	    }
    }
    return true
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert("The domain name doesn't seem to be valid.")
    formField.select();
    formField.focus();
    return false
}

/* domain name seems valid, but now make sure that it ends in a
   three-letter word (like com, edu, gov) or a two-letter word,
   representing country (uk, nl), and that there's a hostname preceding 
   the domain or country. */

/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
	// the address must end in a two letter or three letter word.
	alert("The address must end in a three-letter domain, or two letter country.")
	formField.select();
	formField.focus();
	return false
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="This address is missing a hostname."
   alert(errStr)
   formField.select();
   formField.focus();
   return false
}

// If we've gotten this far, everything's valid!
return true;
}
