
var appname = navigator.appName;
var	appversion = navigator.appVersion;
var	IsAgentCapable;
var bContinue = true;
		
iVer = navigator.appVersion.substring(0,1);
if ((appname == "Netscape") && ( iVer >= 3 )){
	IsAgentCapable = true;
}
if ((appname == "Microsoft Internet Explorer") && (iVer >= 4)){
	IsAgentCapable = true;
}
	
		
function inputClass(name, type, min, max, exact, label, req){
	this.name = name;
	this.type = type.toUpperCase();
	this.max = max;
	this.min = min;
	this.exact = exact;
	this.label = label;
	this.req = req;
}


function confirmDelete(sRecordType) {
	// ***** CONFIRM DELETE
	
	bDelete = window.confirm("Are you sure you want to Delete this " + sRecordType + ". You will not be able to undo this deletion");
	if (bDelete == true) {
		bContinue = true;
	}
	else {
		bContinue = false;
	}
}


function validateForm(objForm) {
	if (bContinue == false) {
		return false;
	}
		
	// ***** GET THE VALUES FOR EACH FORM INPUT *****
	// ***** LOCATE EACH ELEMENT BY SCROLLING ALL FORM ELEMENTS AND MATCH ON NAME *****
	for (i=0; i<aInputs.length; i++)
	{	
		for(j=0; j<objForm.length; j++) 
		{
			if (aInputs[i].name == objForm.elements[j].name) {		
				aInputs[i].value = objForm.elements[j].value;
				
				if (aInputs[i].value.replace(/ /g, "").length == 0) {
					aInputs[i].value = ""
				}
				
				if (i < aInputs.length-1) {
					i++;
				}
				else
					break;
			}
		}
	}
	
	// ***** CALL VALIDATION PROCEDURE *****	
	var isValid = (validateCntl(objForm));	
	
	if (objForm.target.length > 0) {
		return true;
	}
	else if (isValid == true) {
		// ***** FORM FIELDS ARE VALID - SHOW HOUR GLASS AND DISABLE SUBMIT BUTTONS SO USER CAN'T RESUBMIT *****
		
		window.document.body.style.cursor="wait";
	
		for (i=0; i<objForm.length; i++) {
			 if (objForm.item(i).type == "submit") {
						
				// ***** NOTE: HIDE SUBMIT BUTTON INSTEAD OF DISABLING SINCE THE FORM PROCESSING LOGIC MAY NEED TO GET THOSE VALUES *****
				// ***** THE BUTTON THE USER SELECTS INDICATES THEIR INTENTION WHEN MULITIPLE BUTTON EXIST *****	
				var sHTML = objForm.item(i).outerHTML;
				sHTML = sHTML.replace(objForm.item(i).name, "cmdTemp");
				sHTML = sHTML.replace(/submit/g, "button");
				sHTML = sHTML.replace(/>/g, " disabled = true>");
			
				objForm.item(i).style.display = "none";	
				// ***** DISPLAY A COPY OF THE BUTTON THAT CAN BE DISABLED *****
				objForm.item(i).insertAdjacentHTML("afterEnd", sHTML);
			}
		}
		
		return true;
		
	}
	else {
		// ***** FORM IS INVALID - DO NOT ALLOW SUBMIT *****
		return false;
	}	
}


function validateCntl(objForm) {
	for (i=0; i<aInputs.length; i++)
	{
		sFldVal = aInputs[i].value;

		bReqTestPassed = false;
		
		
		// ***** EVALUATE REQUIRED CONDITION *****
		if (aInputs[i].req == true) {		
			if (sFldVal == null || sFldVal.length == 0) {
				if (aInputs[i].type == "LIST") {
					sMsgAction = "selected";
				}
				else
					sMsgAction = "filled-in";
					
				window.alert("'" + aInputs[i].label + "' must be " + sMsgAction + " in order to proceed.");
				
				// ***** SHOW THE USER TO THE OFFENDING FIELD *****
				try {
					document.getElementById(aInputs[i].name).focus();
				}
				catch(err)
				{
				}
				bReqTestPassed = true;
				return false;
			}	
		}
		
		// ***** EVALUATE TYPE CONDITION *****
		if (aInputs[i].type == "TEXT" && bReqTestPassed == false){
			// ***** EVALUATE LENGTH CONDITION *****
			if (aInputs[i].exact != null){
				if (sFldVal.length > aInputs[i].exact) {
					window.alert("'" + aInputs[i].label + "' must be exactly " + aInputs[i].max + " characters long in order to proceed.");
					document.getElementById(aInputs[i].name).focus();
					return false;
				}
			}
			if (aInputs[i].max != null){
				if (sFldVal.length > aInputs[i].max) {
					window.alert("'" + aInputs[i].label + "' must be no longer than " + aInputs[i].max + " in order to proceed.");
					document.getElementById(aInputs[i].name).focus();
					return false;
				}
			}
			if (aInputs[i].min != null){
				if (sFldVal.length < aInputs[i].min) {
					window.alert("'" + aInputs[i].label + "' must be at least " + aInputs[i].min + " characters long in order to proceed.");
					document.getElementById(aInputs[i].name).focus();
					return false;
				}
			}
		}
		
		// ***** ZIP CODE TYPE *****
		if (aInputs[i].type == "ZIPCODE") {
			if (sFldVal.length > 0) {
				sZipCode = sFldVal;
				
				if (parseInt(sZipCode) < 0 || sZipCode.substring(0, 1) == "+") {
					window.alert("'" + aInputs[i].label + "' must be a valid zip code (ex. 90808) in order to proceed.");
					document.getElementById(aInputs[i].name).focus();
					return false;
				}
				if (sZipCode.substring(0, 1) == 0) {
					sZipCode = 1 + sZipCode.substring(1, sZipCode.length) 
				}
				
				if (sZipCode.length == 5) {
					if (parseInt(sZipCode) != sZipCode) {
						window.alert("'" + aInputs[i].label + "' must be a valid 5 digit zip code (ex. 90808) in order to proceed.");
						document.getElementById(aInputs[i].name).focus();
						return false;
					}
				}
				else if (sZipCode.length == 10) {
					if (parseInt(sZipCode.substring(0, 5)) != sZipCode.substring(0, 5)) {
						window.alert("'" + aInputs[i].label + "' must be a valid digit zip code (ex. 90808) in order to proceed.");
						document.getElementById(aInputs[i].name).focus();
						return false;
					}
					if (parseInt(sZipCode.substring(6, 10)) != sZipCode.substring(6, 10)) {
						window.alert("'" + aInputs[i].label + "' must be a valid digit zip code (ex. 90808-0021) in order to proceed.");
						document.getElementById(aInputs[i].name).focus();
						return false;
					}
				} 
				else {
					window.alert("'" + aInputs[i].label + "' must be a valid digit zip code (ex. 90808) in order to proceed.");
					document.getElementById(aInputs[i].name).focus();
					return false;
				}
			}
		}
		
		// ***** NUMBER TYPE *****
		if ((aInputs[i].type == "NUMBER" || aInputs[i].type == "CURRENCY" || aInputs[i].type == "INTEGER") && bReqTestPassed == false){
			if (sFldVal.length > 0) {
			// ***** REMOVE $ FROM CURRENCY FIELD *****
				if (aInputs[i].type == "CURRENCY") {
					if (sFldVal.charAt(0) == "$") {
						sFldVal = sFldVal.substring(1, sFldVal.length);
					}
					// ***** REMOVE COMMAS *****
					sFldVal = sFldVal.replace(/,/g, "");
				}
				if (aInputs[i].type == "INTEGER") {
					if (parseInt(sFldVal) != sFldVal) {
						if (aInputs[i].min != null) {
							iMin = aInputs[i].min;
						}
						// ***** SET MIN AND MAX FOR DISPLAY PURPOSES ONLY *****
						else {
							iMin = "1";
						}
						if (aInputs[i].max != null) {
							iMax = aInputs[i].max;
						}
						else {
							iMax = "100";
						}
						window.alert("'" + aInputs[i].label + "' must be an integer number (ex. " + iMin + ",..." + iMax + ") in order to proceed.");
						document.getElementById(aInputs[i].name).focus();
						return false;
					}
				}
				//if (isNaN(parseFloat(sFldVal))) {
				if (isNaN(sFldVal)) {
					window.alert("'" + aInputs[i].label + "' must be a number in order to proceed.");
					document.getElementById(aInputs[i].name).focus();
					return false;
				}
				else {
					// ***** EVALUATE SIZE CONDITION *****
					if (aInputs[i].exact != null){
						if (sFldVal != aInputs[i].exact) {
							window.alert("'" + aInputs[i].label + "' must be exactly " + aInputs[i].max + " in order to proceed.");
							document.getElementById(aInputs[i].name).focus();
							return false;
						}
					}
					if (aInputs[i].max != null){
						if (sFldVal > aInputs[i].max) {
							window.alert("'" + aInputs[i].label + "' must be no larger than " + aInputs[i].max + " in order to proceed.");
							document.getElementById(aInputs[i].name).focus();
							return false;
						}
					}
					if (aInputs[i].min != null){
						if (sFldVal < aInputs[i].min) {
							window.alert("'" + aInputs[i].label + "' must be at least " + aInputs[i].min + " in order to proceed.");
							document.getElementById(aInputs[i].name).focus();
							return false;
						}
					}
				}
			}
		}
			
		if (aInputs[i].type == "DATE" && bReqTestPassed == false){
			
			if (IsAgentCapable == true) 
			{ 
				var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
					
				// TO REQUIRE A 4 DIGIT YEAR ENTRY, USE THIS LINE INSTEAD:
				// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
	
				// IS THE FORMAT OK?
				var matchArray = sFldVal.match(datePat); 
				
				if (sFldVal.length > 0) {
					if (matchArray == null) {
						window.alert("'" + aInputs[i].label + "' is not in a valid date.")
						return false;
					}
				
					month = matchArray[1]; // parse date into variables
					day = matchArray[3];
					year = matchArray[4];
				
					if (month < 1 || month > 12) { // check month range
						alert("'" + aInputs[i].label + "': Month must be between 1 and 12.");
						return false;
					}
					if (day < 1 || day > 31) {
						alert("Day must be between 1 and 31.");
						return false;
					}
					if ((month==4 || month==6 || month==9 || month==11) && day==31) {
						window.alert("'" + aInputs[i].label + "': Month "+month+" doesn't have 31 days!")
						return false;
					}
					if (month == 2) { 
						// check for february 29th
						var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
						if (day>29 || (day==29 && !isleap)) {
							window.alert("'" + aInputs[i].label + "': February " + year + " doesn't have " + day + " days!");
							return false;
						}
					}
					if (year.length == 2) { 
						year = "20" + year;
						sFldVal = month + "/" + day  + "/" + year;
					}
					
					if (year.length == 2) { 
						year = "20" + year;
						sFldVal = month + "/" + day  + "/" + year;
					}
					
					if (year < 1900) { 
						window.alert("'" + aInputs[i].label + "': " + year + " is too far in the past!");
						return false;
					}
					
					if (year > 2079) { 
						window.alert("'" + aInputs[i].label + "': " + sFldVal + " is too far in the future!");
						return false;
					}
					
					
					var dDateInput = new Date(sFldVal);
					
					// ***** EVALUATE SIZE CONDITION *****
					if (aInputs[i].exact != null){
						var dDateCompare = new Date(aInputs[i].exact);
						if (dDateInput != dDateCompare) {
							window.alert("'" + aInputs[i].label + "' must be exactly " + aInputs[i].max + " in order to proceed.");
							document.getElementById(aInputs[i].name).focus();
							return false;
						}
					}
					if (aInputs[i].max != null){
						var dDateCompare = new Date(aInputs[i].max);
						if (dDateInput > dDateCompare) {
							window.alert("'" + aInputs[i].label + "' must be before " + aInputs[i].max + " in order to proceed.");
							document.getElementById(aInputs[i].name).focus();
							return false;
						}
					}
					if (aInputs[i].min != null){
						var dDateCompare = new Date(aInputs[i].min);
						if (dDateInput < dDateCompare) {
							window.alert("'" + aInputs[i].label + "' must be after " + aInputs[i].min + " in order to proceed.");
							document.getElementById(aInputs[i].name).focus();
							return false;
						}
					}
				}
			}
		}
		if (aInputs[i].type == "RADIO"){
			bChecked = false;
			
			if (aInputs[i].req == true) {
				for (j=0; j<document.all.length; j++) {
					
					if (document.all[j].name == aInputs[i].name){
						if (document.all[j].checked == true) {
							bChecked = true;
							break;
						}
					}
				}
				if (bChecked == false) {
					window.alert("Please choose a '" + aInputs[i].label + "' in order to proceed.");
					return false;
				}
			}		
		}
		if (aInputs[i].type == "CENSUS TRACT"){
			var CensusTractValid = 1;
			
			CensusTractValid = CheckCensusTract(sFldVal);
			
			if (CensusTractValid == 0) {
				window.alert("Census Track must be numeric in the following format: SSCCCTTTT.TT")
				return false;
			}
		}
		if (aInputs[i].type == "PHONE"){
			var PhoneValid = 1;
			
			PhoneValid = CheckPhoneNumber(sFldVal);
			
			if (PhoneValid == 0) {
				window.alert("Phone number is not the correct format")
				return false;
			}
		}
	}
	return true;
}

function CheckPhoneNumber(TheNumber) {
	var valid = 1
	var GoodChars = "0123456789()-+ "
	var i = 0
	
	for (i =0; i <= TheNumber.length -1; i++) {
		if (GoodChars.indexOf(TheNumber.charAt(i)) == -1) {
			valid = 0
		} // End if statement
	} // End for loop
	return true;
}

function CheckCensusTract(TheNumber) {
	var valid = 1
	var GoodChars = "0123456789."
	var i = 0
	
	if (TheNumber=="") {
		// return true if number is empty - let 'validateForm' handle required fields
		valid = 1;
		return valid;
	}
	
	if (TheNumber.length != 12)  {
		alert("Census Tract must be exactly 12 characters long")
		valid = 0;
	}
	
	for (i =0; i <= TheNumber.length -1; i++) {
		if (GoodChars.indexOf(TheNumber.charAt(i)) == -1) {
			//alert(TheNumber.charAt(i) + " must be a number.")
			valid = 0;
		} 
	} 

	if (TheNumber.charAt(9) != ".") {
		alert("Census Tract: Position 10 must be a period ('.')")
		valid = 0;
	} 
		
	return valid;
}


