// Setup browser-specific variables
var navigatorName = navigator.appName;
var isIE;
var isNetscape;
var appVersion = parseInt(navigator.appVersion);

if (navigatorName == 'Microsoft Internet Explorer') {
	isIE = true;
	isNetscape = false;
}else{
	isIE = false;
	isNetscape = true;
}


// Setup machine-specific variables
var platform = navigator.platform;

	function GetKeyCode(eventParams) {
		if (isIE == true){
			return eventParams.keyCode;
		}else{
			return eventParams.which;
		}
	}
	
	function GetDocumentObjectReference(elementID) {
		return eval("document." + elementID);
	}

	function fnInitForm(frmName){
		if(findDOM(frmName,0)) {
			oForm = self.document.forms[frmName];
			fnGetFormFocus();
		}
	}

// Sets the cursor on the first available form field	
	function fnGetFormFocus(){
		for (i = 0; i < oForm.elements.length; i++){
			var fld = oForm.elements[i];
			if(! oForm.elements[i].disabled){
				if(fld.type == "text" || fld.type == "textarea"){
					fld.focus();
					break;
				}
			}
		}
	}
	
// Set background color when input errors 
	var inputWithNoBorder = 0;
	function fnInputError(fld, msg) {
		alert(msg);
		fld.style.backgroundColor = '#FFFFCC';
		var e; try {if (inputWithNoBorder && fld.parentNode.tagName == "P" && (fld.type == "text" || fld.type == "textarea" || fld.type == "password")) {fld.style.borderTopColor = fld.parentNode.style.backgroundColor = '#FFFFCC';}} catch(e){};
		fld.focus();
		return false;
	}
	
//Clears Background highlight on blur
	function fnInputErrorClear(fld){
		fld.style.backgroundColor = '#FFFFFF';
		var e; try {if (inputWithNoBorder && fld.parentNode.tagName == "P" && (fld.type == "text" || fld.type == "textarea" || fld.type == "password")) {fld.style.borderTopColor = fld.parentNode.style.backgroundColor = '#FFFFFF';}} catch(e){};
		return true;
	}
	
//Validate the form input
	function fnValidateForm(frmName){
		oForm = self.document.forms[frmName];
		for (var i = 0; i < oForm.elements.length; i++){
			var fld = oForm.elements[i];
			if(!fld.disabled){
				var fldValue = oForm.elements[i].value;
				var fldName = oForm.elements[i].name;
				var fldNameRequired = false;
				var s = (fldName.length - 4);
				if(fldName.substr(s,4) == '_req'){
					fldNameRequired = true;
				}
				if (fld.type == "text" || fld.type == "textarea"){
					fldValue = trimWhitespace(fldValue);
					if(fldNameRequired && fldValue==''){ 
						return fnInputError(fld, '"' + fld.id + '" is a required field.  Please complete ALL required fields then resubmit this form.  Thank you!');
					}else{
						if(fldValue.length > 0){
							if( fldName.substr(0,4)=='int_' ){
								fld.value = JustNumbers(fldValue,0);
								//return fnInputError(fld, '"' + fld.id + '" may only contain whole numbers greater than zero.  Please try again and then resubmit this form.  Thank you!');
							}else if( fldName.substr(0,4)=='num_' ){
								fld.value = JustNumbers(fldValue,1);
								//return fnInputError(fld, '"' + fld.id + '" may only contain numbers or fractions.  Please try again and then resubmit this form.  Thank you!');
							}
						}
					}
					fld.value = fldValue;
				}
				if(fld.type=="password" && fldNameRequired ){
					if(fldValue.length == 0){
						return fnInputError(fld, fld.id + ' is a required field.  Please complete ALL required fields then resubmit this form.  Thank you!');
					}else if( fldValue.length < 3 || fldValue.length > 32 ){
						return fnInputError(fld, 'Passwords must be between 3 and 32 characters.  Please try again and then resubmit this form.  Thank you!');
					//}else if(!isAlphanumeric(fldValue, true)){
						//return fnInputError(fld, 'Passwords may only contain numbers or letters and no special characters  Please try again and then resubmit this form.  Thank you!');
					}
				}
				if( fld.type == "select-one" || fld.type == "select-multiple" || fld.type == "select" ) {
					if(fldNameRequired && fldValue==''){ 
						return fnInputError(fld, '"' + fld.id + '" is a required field.  Please complete ALL required fields then resubmit this form.  Thank you!');
					}
				}
				if(fld.type == "file" && fldNameRequired && fldValue.length == 0){
					return fnInputError(fld, '"' + fld.id + '" is a required field.  Please complete ALL required fields then resubmit this form.  Thank you!');
				}
			}
		}
		switch(frmName){
			case 'aboutyou':
				if(oForm.Upload.value == "0"){
					if(oForm.int_Agreement.type == 'radio'){
						if(oForm.int_Agreement[0].checked == false){
							if(! confirm('To continue with the application process, you must read and agree with, the Consent Agreement.  If you choose not to agree, you will not be able to proceed.\nIf you agree please click OK, if you disagree please click Cancel.')){
								oForm.x_ConsentAgreement.focus();
								return false;
							}else{
								oForm.int_Agreement[0].checked = true;
							}
						}
					}

					var myvalue = oForm.vch_LoginName_req.value;
					if (myvalue.indexOf(' ') > 0)
					{
						alert('No spaces allowed.');
						oForm.vch_LoginName_req.focus();
						return false;
					}


					if(oForm.vch_LoginPassword_req.value != oForm.x_LoginPassword_req.value){
						alert('The passwords do not match.');
						oForm.vch_LoginPassword_req.focus();
						return false;
					}
				}
			  break;
			default:
				return true;
			  break;
		}
	}

// Functions for form input validation:

// Check that an email address is valid based on RFC 821 (?)
	function isValidEmail(address) {
		if (address != '' && address.search) {
		  if (address.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) return true;
		  else return false;
		}
	   // allow empty strings to return true - screen these with either a 'required' test or a 'length' test
	   else return true;
	}
	
// Check that an email address has the form something@something.something
// This is a stricter standard than RFC 821 (?) which allows addresses like postmaster@localhost
	function isValidEmailStrict(address) {
		if (isValidEmail(address) == false) return false;
		var domain = address.substring(address.indexOf('@') + 1);
		if (domain.indexOf('.') == -1) return false;
		if (domain.indexOf('.') == 0 || domain.indexOf('.') == domain.length - 1) return false;
		return true;
	}
	
// Check that a string contains only numbers and one decimal
	function isNumeric(str){
		var reFloatF1 = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/; 
		return reFloatF1.test(str);
	}
	
// Check that a string contains only numbers
	function isInteger( string ) {
		if (string.search) {
			if ( string.search(/\D/) != -1) return false;
		}
	}
	
	function isAlphanumeric( string, ignoreWhiteSpace ) {
		if (string.search) {
			if ((ignoreWhiteSpace && string.search(/[^\w\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\W/) != -1)) return false;
		}
		return true;
	}
	
//Validates numeric input and size limit and sets focus to next field
	function autofocus( field, limit, next ) {
		var oForm = document.forms[0];
		field.value = JustNumbers(field.value);
		if(field.value.length == limit){
			oForm.elements[next].focus();
		}
	}
	
// Check that a US zip code is valid
	function isValidZipcode( zipcode ) {
		zipcode = removeSpaces(zipcode);
		if (!(zipcode.length == 5 || zipcode.length == 9 || zipcode.length == 10)) return false;
		if ((zipcode.length == 5 || zipcode.length == 9) && !isInteger(zipcode)) return false;
		if (zipcode.length == 10 && zipcode.search && zipcode.search(/^\d{5}-\d{4}$/) == -1) return false;
	   return true;
	}
	
// Check that a Canadian postal code is valid
	function isValidPostalcode( postalcode ) {
		if (postalcode.search) {
			postalcode = removeSpaces(postalcode);
			if (postalcode.length == 6 && postalcode.search(/^[a-zA-Z]\d[a-zA-Z]\d[a-zA-Z]\d$/) != -1) return true;
			else if (postalcode.length == 7 && postalcode.search(/^[a-zA-Z]\d[a-zA-Z]-\d[a-zA-Z]\d$/) != -1) return true;
			else return false;
		}
		return true;
	}
	
//Limit the input in a textarea
	function textCounter(field, countfieldName, maxlimit, val) {
		var fldLen = field.value.length
		var oCountField = findDOM(countfieldName,0);
		if (fldLen > maxlimit) {
			field.value = field.value.substring(0, maxlimit-1);
			alert('You have entered the maximum allowed characters.');
			field.focus();
		} else {
			if(parseInt('0'+val)==0){
				oCountField.innerText = maxlimit - fldLen;
			}else{
				oCountField.innerText = fldLen;
			}
		}
	}
	
	function textCounterHidden( field, maxlimit ) {
		if (field.value.length > maxlimit) {
			field.value = field.value.substring(0, maxlimit-1);
			alert('You have entered the maximum of '+maxlimit+' characters.');
			field.focus();
		}
	}
	
// Remove leading and trailing whitespace from a string
	function trimWhitespace( string ) {
		var newString  = '';
		var substring  = '';
		beginningFound = false;
		
		// copy characters over to a new string
		// retain whitespace characters if they are between other characters
		for (var i = 0; i < string.length; i++) {
			
			// copy non-whitespace characters
			if (string.charAt(i) != ' ' && string.charCodeAt(i) != 9) {
				
				// if the temporary string contains some whitespace characters, copy them first
				if (substring != '') {
					newString += substring;
					substring = '';
				}
				newString += string.charAt(i);
				if (beginningFound == false) beginningFound = true;
			}
			
			// hold whitespace characters in a temporary string if they follow a non-whitespace character
			else if (beginningFound == true) substring += string.charAt(i);
		}
		return newString;
	}
	
// Remove all spaces from a string
	function removeSpaces( string ) {
		var newString = '';
		for (var i = 0; i < string.length; i++) {
			if (string.charAt(i) != ' ') newString += string.charAt(i);
		}
		return newString;
	}
	
//Return strings with numeric 0-9 only
	function JustNumbers ( argstr, num ){
		var newstr = new String("");
		var nZero = String ("0").charCodeAt(0);
		var nNine = String("9").charCodeAt(0);
		var nDeci = String(".").charCodeAt(0);
		var nCh;
	
		for (i = 0; i < argstr.length; i++ ){
			nCh = argstr.charCodeAt(i)
			if ( num == 1 ) {
				if ( ( (nCh >= nZero) && (nCh <= nNine) ) || (nCh == nDeci)  ){
					newstr = newstr + argstr.charAt(i)
				}
			}else{
				if ( (nCh >= nZero) && (nCh <= nNine) ){
					newstr = newstr + argstr.charAt(i)
				}
			}
		}
		return newstr	
	}
	
//Credit Card validation functions;

	function SumIntString( argstr ){
		var i = argstr.length - 1
		var nSum = new Number(0)
		
		for (i; i >= 0; i--){
			nSum = parseInt(argstr.charAt(i)) + nSum		
		}	
		return parseInt(nSum)
	}
	
	function ValidateCreditCard( strCardNumber ){
		var strOddNumbers = new String("")
		var strEvenNumbers = new String("")
		var strDoubled = new String("")
		var i = strCardNumber.length
		var i2
		var isValid = new Boolean (0)
		var nOddNumbers 
		var nEvenNumbers
		var nSumDigits
		var nDoubled
		
		if(strCardNumber.length < 13) {
			isValid = false;
		}else{
		//seperate odd and even digits into two strings	
			for (i; i >= 0; i--)
			{
				if (((strCardNumber.length-i) % 2) == 1)
				{
					strOddNumbers += strCardNumber.charAt(i)
				}
				 else
				{
					strEvenNumbers += strCardNumber.charAt(i)
				}		
			}
		//simply sum the odds	
			nOddNumbers = SumIntString(strOddNumbers)
		//but sum the double of the evens	
			nEvenNumbers = 0
			for (i = 0; i < strEvenNumbers.length; i++)
			{
				nDoubled = parseInt(strEvenNumbers.charAt(i)) * 2
				strDoubled = nDoubled.toString()
				nEvenNumbers = SumIntString(strDoubled) + nEvenNumbers
			}
		//add the sum of the two strings (odds and evens)
			nSumDigits = parseInt(nOddNumbers) + parseInt(nEvenNumbers)
		//divisible by 10? yes then valid, no then invalid	
			isValid = ((nSumDigits % 10) == 0)
		}
		return isValid
	}
