
function IsInt(textObj) {
	var isError = false;
	var newValue = textObj.value;
	var newLength = newValue.length;
	if (newValue == "N/A") {
		return false;
	}
	for(var i = 0; i != newLength; i++) {
		aChar = newValue.substring(i,i+1);
		if(aChar < "0" || aChar > "9") {
				isError = true;
			}
	}
	if (newValue == "0") isError = true;
	if (isError) {
		alert("Please enter a whole number equal to or greater than 1.");
		textObj.value = "";
		textObj.focus();
	}
	return true;
}


function CheckDate(objName) {
var datefield = objName;
	if (ChkDate(objName) == false) {
		//datefield.select();		
		alert("This date is invalid.  Please enter a valid date.");		
		datefield.value = "";
		datefield.focus();
		return false;
	}
	else {
		return true;
	}
}

function ChkDate(objName) {
	var strDatestyle = "US"; //United States date style
	var strDate;
	var strDateArray;
	var strDay;
	var strMonth;
	var strYear;
	var intday;
	var intMonth;
	var intYear;
	var booFound = false;
	var datefield = objName;
	var strSeparatorArray = new Array("-"," ","/",".");
	var intElementNr;
	var err = 0;
	var strMonthArray = new Array(12);
	strMonthArray[0] = "Jan";
	strMonthArray[1] = "Feb";
	strMonthArray[2] = "Mar";
	strMonthArray[3] = "Apr";
	strMonthArray[4] = "May";
	strMonthArray[5] = "Jun";
	strMonthArray[6] = "Jul";
	strMonthArray[7] = "Aug";
	strMonthArray[8] = "Sep";
	strMonthArray[9] = "Oct";
	strMonthArray[10] = "Nov";
	strMonthArray[11] = "Dec";
	strDate = datefield.value + "";
	if (strDate.length < 1) {
		return true;
	}

	for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {	
		if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
			strDateArray = strDate.split(strSeparatorArray[intElementNr]);	
			if ((strDateArray[2] == null) || (strDateArray[2] == "")) {
				var TDate;
				TDate = new Date();		
				CurYear = TDate.getYear();		
				strDateArray[2] = CurYear
			}	
			if (strDateArray.length != 3) {	
				err = 1;
				return false;

			}
			else {	
				strDay = strDateArray[0];
				strMonth = strDateArray[1];
				strYear = strDateArray[2];
			}
			booFound = true;
		}
	}

	if (booFound == false) {
		if (strDate.length>5) {
			strDay = strDate.substr(0, 2);
			strMonth = strDate.substr(2, 2);
			strYear = strDate.substr(4);
		}
		else {
				return false;
		}
	}

	if (strYear.length == 2) {
		intYear = parseInt(strYear, 10);
		if (intYear > 50){
			strYear = '19' + strYear;
		}
		else {
			strYear = '20' + strYear;
		}
	}
	if (strYear.length == 3 || strYear.length == 1 || strYear.length > 4 || strYear > 2079) {
		return false;
	}

	// US style
	if (strDatestyle == "US") {
		strTemp = strDay;
		strDay = strMonth;
		strMonth = strTemp;
	}
	intday = parseInt(strDay, 10);
	if (isNaN(intday)) {
		err = 2;
		return false;
	}
	intMonth = parseInt(strMonth, 10);
	if (isNaN(intMonth)) {
		for (i = 0;i<12;i++) {
			if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
				intMonth = i+1;
				strMonth = strMonthArray[i];
				i = 12;
			}
		}
		if (isNaN(intMonth)) {
			err = 3;
			return false;
		}
	}
	intYear = parseInt(strYear, 10);
	if (isNaN(intYear)) {
		err = 4;
		return false;
	}
	if (intMonth>12 || intMonth<1) {
		err = 5;
		return false;
	}
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
		err = 6;
		return false;
	}
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
		err = 7;
		return false;
	}
	if (intMonth == 2) {
		if (intday < 1) {
			err = 8;
			return false;
		}
		if (LeapYear(intYear) == true) {
			if (intday > 29) {
				err = 9;
				return false;
			}
		}
		else {
			if (intday > 28) {
				err = 10;
				return false;
			}
		}
	}
	if (strDatestyle == "US") {
		datefield.value = intMonth + "/" + intday + "/" + strYear;
	}
	else {
		datefield.value = intday + " " + strMonthArray[intMonth-1] + " " + strYear;
	}
	return true;
}

function LeapYear(intYear) {
	if (intYear % 100 == 0) {
		if (intYear % 400 == 0) { 
			return true; 
		}
	}
	else {
		if ((intYear % 4) == 0) {
			return true; 
		}
	}
	return false;
}

function FutureDate(dt) {
	var d = new Date(dt.value);
	var now = new Date();
	var yr = parseInt(now.getYear())+ 1900;
	var Useryr = parseInt(d.getYear())+ 1900;	
	var today = new Date(now.getYear(),now.getMonth(),now.getDate());
	var checkDate = new Date(d.getYear(),d.getMonth(),d.getDate());		
	
	if (checkDate < today) {		
		alert("Please enter a future date.");				   
		dt.focus();
		return false;
	}
	return true;
}


function Trim_String(strValue) { //Trims white spaces in a string
	var ichar, icount;
	ichar = strValue.length - 1;
	icount = -1;
	while (strValue.charAt(ichar)==' ' && ichar > icount)
		--ichar;
	if (ichar!=(strValue.length-1))
		strValue = strValue.slice(0,ichar+1);
	ichar = 0;
	icount = strValue.length - 1;
	while (strValue.charAt(ichar)==' ' && ichar < icount)
		++ichar;
	if (ichar!=0)
		strValue = strValue.slice(ichar,strValue.length);
	return strValue;
}

function Validate_Phone(theField) {
	if (theField.value == "") {
		return true;
	}
	var checkOK = "0123456789";
	var checkStr = theField.value;
	var safeStr = "";
	len = 0;
	
	for (i = 0; i < checkStr.length; i++) {
		ch = checkStr.charAt(i)
		for (j = 0; j < checkOK.length; j++)
		{
			if (ch == checkOK.charAt(j)) {
				safeStr+=ch;
				len++;
			}
		}
	}
	//if (len > 9) {
		safeStr = "(" + safeStr.slice(0, 3) + ")" + " " + safeStr.slice(3, 6) + "-" + safeStr.slice(6, 10)// + " " + safeStr.slice(9, safeStr.length)
		theField.value = safeStr
	//}	
	if (len < 10 || len > 10) {
		alert("Please enter all 10 digits for the phone or fax number");		
		theField.select();
		theField.focus();
		return false;
	}
	return true;
}

function NumOnly(oTxt) {
	if (!/^[\d\.]+$/.test(oTxt.value) || isNaN(oTxt.value)) {
		oTxt.value = oTxt.value.substring(0,oTxt.value.length-1);
	}
}

function VerifyEmail(oTxt){
	if(oTxt.value != "") {
		if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(oTxt.value))) {
			alert("Please enter a valid email address.");			
			oTxt.focus ();
			submitFlag = false;
		}												
	}	
}

function VerifyMultiEmail(oTxt){
	var checkEmail = oTxt.value;
	if(checkEmail != "") {		
		if ((checkEmail.indexOf('@') < 0) || ((checkEmail.charAt(checkEmail.length-4) != '.') && (checkEmail.charAt(checkEmail.length-3) != '.'))) {
			alert("Please enter a valid email address.");			
			oTxt.focus();
			submitFlag = false;
		}												
	}	
}

function CheckDecimals(fieldName, fieldValue) {

	var decallowed = 2;  // how many decimals are allowed?
	var fieldValue = fieldName.value;
	
	if (isNaN(fieldValue)) {
		alert("That does not appear to be a valid number.");
		fieldName.select();
		fieldName.focus();
		return false;
	}
	else {
		if (fieldValue.indexOf('.') == -1) fieldValue += ".";
		dectext = fieldValue.substring(fieldValue.indexOf('.')+1, fieldValue.length);

		if (dectext.length > decallowed) {
			alert ("Please enter a number with up to " + decallowed + " decimal places.");
			fieldName.select();
			fieldName.focus();
			return false;
		}
		else {
			//alert ("That number validated successfully.");
			return true;
		}
	}
}

function CheckBlank(textObj) {
	var isError = false;
	var newValue = textObj.value;
	if (newValue == '') {
		alert("This field can't be empty.");		
		textObj.focus();
		return false;	
	}	
}

function CheckZero(textObj) {
	var isError = false;
	var newValue = textObj.value;
	if (newValue == '0') {
		alert("Please enter a whole number equal to or greater than 1.");
		textObj.focus();
		return false;		
	}	
}

//validates that the entry is a positive or negative number
function isNumber(elem) {
	var str = elem.value;
	var re = /^[-]?\d*\.?\d*$/;
	str = str.toString(  );
	if (!str.match(re)) {
		alert("Enter only numbers into the field.");
		elem.focus();
		return false;
	}
	return true;
}

function numAndCommaOnly(oTxt){
	if (!/^[\d\.\,]+$/.test(oTxt.value)){
		oTxt.value = oTxt.value.substring(0,oTxt.value.length-1);
	}
}

function validateZIP(field) {
	var valid = "0123456789-";
	var hyphencount = 0;
	var fieldval = field.value;
	
	if (fieldval.length > 0 ) {
		if (fieldval.length!=5 && fieldval.length!=10) {
			alert("Please enter your 5 digit or 5 digit + 4 zip code.");
			field.focus();
			return false;
		}
		for (var i=0; i < fieldval.length; i++) {
			temp = "" + fieldval.substring(i, i+1);
			if (temp == "-") hyphencount++;
			if (valid.indexOf(temp) == "-1") {
				alert("Invalid characters in your zip code.  Please try again.");
				field.focus();
				return false;
			}
			if ((hyphencount > 1) || ((fieldval.length==10) && "" + fieldval.charAt(5)!="-")) {
				alert("The hyphen character should be used with a properly formatted 5 digit + four zip code, like '12345-6789'. Please try again.");
				field.focus();
				return false;
			}
		}
	}
	return true;
}
