/*

	RiteTech LLC
	inc/inc_js.cfm
	Created: 6/21/2008
	SMB 
*/

// trim function
function trim(s) {
	// Remove leading spaces and carriage returns
	while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r')){ 
		s = s.substring(1,s.length);
	}
	// Remove trailing spaces and carriage returns
	while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r')){                         
		s = s.substring(0,s.length-1); 
	}
	return s;
}

// function to create validation rule
function validationRule(ruleType, fieldName, alertText, custom) {
	this.ruleType = ruleType;
	this.fieldName = fieldName;
	this.alertText = alertText;
	this.custom = custom;
}	

// function to see if a text field has value
function valueText(fieldName) {
	var thisField = document.getElementById(fieldName);
	var thisVal = trim(thisField.value);
	if (thisVal == "") {
		return false;
	} else {
		return true;
	}
}

// function to see if a select field has been selected
function valueSelect(fieldName) {
	var thisField = document.getElementById(fieldName);
	if (thisField.selectedIndex == 0) {
		return false;
	} else {
		return true;
	}
}

// function to see if a text field has a valid email
function validEmail(fieldName) {
	var thisFieldID = document.getElementById(fieldName);
	var thisVal = trim(thisFieldID.value);
	rePattern = /^([\w\d\-\.\']+)@{1}(([\w\d\-])+\.)+(([a-zA-Z\d]{2,4})?)$/;
	if (thisVal != "" && !rePattern.test(thisVal)) {
		return false;
	} else {
		return true;
	}
}

// function to see if a text field has a valid integer
function validInt(fieldName) {
	var thisFieldID = document.getElementById(fieldName);
	var thisVal = trim(thisFieldID.value);
	rePattern = /(^-?\d\d*$)/;
	if (thisVal != "" && !rePattern.test(thisVal)) {
		return false;
	} else {
		return true;
	}
}

// function to see if a text field has a valid number
function validNum(fieldName) {
	var thisFieldID = document.getElementById(fieldName);
	var thisVal = trim(thisFieldID.value);
	if (isNaN(thisVal)) {
		return false;
	} else {
		return true;
	}
}

// function to see if a file has a .csv extension
function fileCSV(fieldName) {
	var thisFieldID = document.getElementById(fieldName);
	var docName = thisFieldID.value;
	var extArray = docName.split(".");
	var extNameTx = extArray[extArray.length - 1];
	if (extNameTx.toLowerCase( ) != "csv") {
		return false;
	} else {
		return true;
	}
}

// function to see if a file has a valid extension
function fileValid(fieldName, extList) {
	var thisFieldID = document.getElementById(fieldName);
	var docName = thisFieldID.value;
	var extArray = docName.split(".");
	var extNameTx = extArray[extArray.length - 1];
	var thisPass = false;
	// Loop over the valid extensions to test
	var validArray = extList.split(",");
	for (t=0; t<validArray.length; t++) {
		if (extNameTx.toLowerCase( ) == validArray[t].toLowerCase ( )) {
			thisPass = true;
			break;
		}
	}
	return thisPass;
}

// function to see if a text field has a valid date
function validDate(fieldName) {
	var thisFieldID = document.getElementById(fieldName);
	var thisVal = thisFieldID.value;	
	var rePattern = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;
	// Not valid?
	if (thisVal != "" && !rePattern.test(thisVal)) {
		return false;
	// Matches, further testing needed
	} else {
		var thisPass = true;
		while (thisPass == true) {
			// Break up
			var dateArray = thisVal.split("/");
			if (dateArray[0].substr(0,1) == 0) {
				dateArray[0] = dateArray[0].substr(1,1);
			}
			var thisMonth = parseInt(dateArray[0]); 
			var thisDay = parseInt(dateArray[1]);
			var thisYear = parseInt(dateArray[2]);
			
			// Too far in the past
			if (thisYear < 1900) {
				thisPass = false;
				break;
			}
			
			// Create month array
			var dayArray = new Array();
			dayArray[0] = 0;
			dayArray[1] = 31;
			dayArray[2] = 28;
			dayArray[3] = 31;
			dayArray[4] = 30;
			dayArray[5] = 31;
			dayArray[6] = 30;
			dayArray[7] = 31;
			dayArray[8] = 31;
			dayArray[9] = 30;
			dayArray[10] = 31;
			dayArray[11] = 30;
			dayArray[12] = 31;
			
			// Month is not feb and day matches
			if (thisMonth != 2 && dayArray[thisMonth] < thisDay) {
				thisPass = false;
				break;
			}
			
			// February and leap years
			if (thisMonth == 2) {
				if (thisDay <= 28 || (thisYear % 4 == 0 && (thisYear % 100 != 0 || thisYear % 400 == 0))) {
					thisPass = true;
					break;
				} else {
					thisPass = false;
					break;
				}
			}
			break;
		}
		return thisPass;
	}
}

// Compares two dates
function dateCompare(fieldName, compareDate, compareType) {
	var thisFieldID = document.getElementById(fieldName);
	var thisVal = thisFieldID.value;	
	var thisPass = true;
	while (thisPass == true) {
		// valid date?
		if (validDate(fieldName) == false) {
			thisPass = false;
			break;
		}
		// Break up
		var dateArray = thisVal.split("/");
		var thisMonth = parseInt(dateArray[0]) - 1; 
		var thisDay = parseInt(dateArray[1]);
		var thisYear = parseInt(dateArray[2]);
		var date1 = new Date(thisYear, thisMonth, thisDay);
		date1.setHours(0, 0, 0);
		var dateArray = compareDate.split("/");
		var thisMonth = parseInt(dateArray[0]-1); 
		var thisDay = parseInt(dateArray[1]);
		var thisYear = parseInt(dateArray[2]);
		var date2 = new Date(thisYear, thisMonth, thisDay);
		date2.setHours(0, 0, 0);
		//alert(date1.toDateString() + " " + date2.toDateString());
		//alert(date1 < date2);
		// test the compare type
		if (compareType == "gte" && date1 < date2) {
			thisPass = false;
			break;
		}
		
		break;
	}
	return thisPass;
}




// function to validate the form
function CheckForm(x, validate) {
	var Passed = true;
	while (Passed == true) {
		// loop over validation
		for (i=0; i<validate.length; i++) {
			var thisField = document.getElementById(validate[i].fieldName);
			// text field required
			if (validate[i].ruleType == "textReq") {
				if (valueText(validate[i].fieldName) == false) {
					alert(validate[i].alertText);
					thisField.focus();
					Passed = false;
					break;
					break;
				}
			}
			// email value
			if (validate[i].ruleType == "emailValue") {
				if (validEmail(validate[i].fieldName) == false) {
					alert(validate[i].alertText);
					thisField.focus();
					Passed = false;
					break;
					break;
				}
			}
			// integer value
			if (validate[i].ruleType == "intValue") {
				if (validInt(validate[i].fieldName) == false) {
					alert(validate[i].alertText);
					thisField.focus();
					Passed = false;
					break;
					break;
				}
			}
			// numeric value
			if (validate[i].ruleType == "numValue") {
				if (validNum(validate[i].fieldName) == false) {
					alert(validate[i].alertText);
					thisField.focus();
					Passed = false;
					break;
					break;
				}
			}
			// date value
			if (validate[i].ruleType == "dateValue") {
				if (validDate(validate[i].fieldName) == false) {
					alert(validate[i].alertText);
					thisField.focus();
					Passed = false;
					break;
					break;
				}
			}
			// text area required
			if (validate[i].ruleType == "textAreaReq") {
				var thisField = document.getElementById(validate[i].fieldName);
				var thisVal = thisField.value;
				if (trim(thisVal) == "") {
					alert(validate[i].alertText);
					thisField.focus();
					Passed = false;
					break;
					break;
				}
			}
			// text area length
			if (validate[i].ruleType == "textAreaMax") {
				var thisField = document.getElementById(validate[i].fieldName);
				var thisLen = thisField.value.length;
				if (thisLen > validate[i].custom) {
					alert(validate[i].alertText);
					thisField.focus();
					Passed = false;
					break;
					break;
				}
			}
			// select required
			if (validate[i].ruleType == "selectReq") {
				if (valueSelect(validate[i].fieldName) == false) {
					alert(validate[i].alertText);
					thisField.focus();
					Passed = false;
					break;
					break;
				}
			}
			// radio required
			if (validate[i].ruleType == "radioReq") {
				var thisField = document.getElementsByName(validate[i].fieldName);
				var len = thisField.length;
				var oneChecked = false;
				for (q=0; q < len; q++) {
					if (thisField[q].checked == true) {
						oneChecked = true;
						break;
					}
				}
				if (oneChecked == false) {
					alert(validate[i].alertText);
					Passed = false;
					break;
					break;
				}
			}
			// text fields matching
			if (validate[i].ruleType == "textMatch") {
				var thisFieldA = document.getElementById(validate[i].fieldName + "1")
				var thisFieldB = document.getElementById(validate[i].fieldName + "2")
				var thisValA = trim(thisFieldA.value);
				var thisValB = trim(thisFieldB.value);
				if (thisValA != thisValB) {
					alert(validate[i].alertText);
					thisFieldA.focus();
					Passed = false;
					break;
					break;
				}
			}
			// File: CSV
			if (validate[i].ruleType == "fileCSV") {
				if (fileCSV(validate[i].fieldName) == false) {
					alert(validate[i].alertText);
					thisField.focus();
					Passed = false;
					break;
					break;
				}
			}
			// File: Valid Ext
			if (validate[i].ruleType == "fileValid") {
				if (valueText(validate[i].fieldName) == true && fileValid(validate[i].fieldName, validate[i].custom) == false) {
					alert(validate[i].alertText);
					thisField.focus();
					Passed = false;
					break;
					break;
				}
			}
			// Captcha
			if (validate[i].ruleType == "captcha") {
				var thisSum = parseInt(validate[i].custom[0]) + parseInt(validate[i].custom[1]);
				var thisField = document.getElementById(validate[i].fieldName);
				var fieldValue = thisField.value;
				if (thisSum != fieldValue) {
					alert(validate[i].alertText);
					thisField.focus();
					Passed = false;
					break;
					break;
				}
			}
			// custom
			if (validate[i].ruleType == "custom") {
				//alert("custom");
				//var len = thisField.length;
				//var thisVal = trim(thisField.value);
				//var oneChecked = false;
				//for (i=0; i < len; i++) {
				//	if (thisField[i].checked == true) {
				//		oneChecked = true;
				//		break;
				//	}
				//}
				//if (oneChecked == false) {
				//	alert(validate[i].alertText);
				//	Passed = false;
				//	break;
				//	break;
				//}
			}
		}	
		break;
	}
	return Passed;
}
