//** HOW TO USE THIS FUNCTION:
//** use the "validate" attribute to validate the input value as a particular type of
//** value (number, month, year, phone, etc)
//** use the "required" attribute (set to "true") to make the input value required
//** i.e. <input type="text" validate="date" required="true" name="DOB" id="DOB">
//** at this point, this function only validates elements in a form named "frmMain"
//****************************************************************************************
var err;


function validateForm() {
	//** pageValidation will be customized for each page (and must be included), 
	//** so validateForm() can remain generic

	var aReturn=frmMain.getElementsByTagName("INPUT");
	var inputCount = aReturn.length - 1;
	//alert("inputCount = " + inputCount);


	//********** TEST FOR REQUIRED FIELDS **************
	for(var icount = 0; icount <= inputCount; icount++) { 

		if (aReturn[icount].type == "text" || aReturn[icount].type == "textarea") {
			//alert("number " + icount + " is text or textarea");
			//alert(aReturn[icount].name + " is text or textarea");
			if (typeof(aReturn[icount].required) != "undefined")  {
				if (aReturn[icount].required == "true") {
					if (isPopulated(aReturn[icount].name) != true) {
						// break; would take you out of the loop
						return false;
					}
				}
			}
		}
		else {
			//alert("number " + icount + " is NOT text");
			//alert(aReturn[icount].name + " is NOT text");
		}
	}

	var inputCount = aReturn.length - 1;

	//alert("all required fields present, ready to validate");
	//************** VALIDATE *********************
	for(var icount = 0; icount <= inputCount; icount++) { 
		//alert(aReturn[icount].name);
		if (aReturn[icount].type == "text") {
			if (typeof(aReturn[icount].validate) != "undefined")  {

				switch (aReturn[icount].validate){
				   case "text":
						//alert ("this is text");
						break;		
						//** without 'break' the statement basically becomes true, so
						//** all of the ensuing Case statements will execute

						//	if (validateText(aReturn[icount].name) != true) {
						//		return;
						//	}
				   case "phone":
						if (validatePhone(aReturn[icount].name) == false) {
							return false;
						}
						break;
				   case "email":
						if (validateEmail(aReturn[icount].name) == false) {
							return false;
						}
						break;
				   case "number":
						if (validateNumber(aReturn[icount].name) == false) {
							return false;
						}
						break;
				   //case "date":
					//	if (validateDate(aReturn[icount].name) == false) {
					//		return false;
					//	}
					//	break;
				   case "month":
						if (validateMonth(aReturn[icount].name) == false) {
							return false;
						}
						break;
				   case "year":
						if (validateYear(aReturn[icount].name) == false) {
							return false;
						}
						break;
				   default: 
						//alert ("this is other");
						break;
				}


				//if (aReturn[icount].validate == "text") {
				//	if (validateText(aReturn[icount].name) != true) {
				//		return;
				//	}
				//}
			}
		}
		else {
			//alert("number " + icount + " is NOT text");
		}
	}
}
//****************************************************************************************

function isPopulated(fieldname) {
	if (frmMain.elements[fieldname].value.length == 0) {
		alert("The '" + fieldname.toUpperCase( ) + "' field is required");
		return false;
	}
	else {
		return true;
	}
}

//****************************************************************************************
function validateNumber(fieldname) {
	//alert(value);
	var value = frmMain.elements[fieldname].value;
	//alert("isNaN = " + isNaN(value));
	if (isNaN(value) == true) {
		alert("The '" + fieldname.toUpperCase( ) + "' field must be numeric");
		return false;
	}
	//** return true if it makes it this far
	return true;
}

//****************************************************************************************
function validateEmail(fieldname) {
	//alert(value);
	var value = frmMain.elements[fieldname].value;
	var bolDot;
	var bolAt;
	var good = ".";
	for(var icount = 0; icount <= value.length - 1; icount++) { 
		var char = value.substr(icount, 1);
		var pos = good.indexOf(char);
		//alert("icount = " + icount + ", char = " + char + ", pos = " + pos);
		if (pos >= 0) {
			//alert("dot found");
			bolDot = true;
		}
		
	}

	var good = "@";
	for(var icount = 0; icount <= value.length - 1; icount++) { 
		var char = value.substr(icount, 1);
		var pos = good.indexOf(char);
		//alert("icount = " + icount + ", char = " + char + ", pos = " + pos);
		if (pos >= 0) {
			//alert("@ found");
			bolAt = true;
		}
		
	}

	//** return false if we don't find the character
	if (bolDot != true || bolAt != true) {
		alert("The '" + fieldname.toUpperCase( ) + "' field must be a valid email address");
		return false;
	}
	
	//** return true if it makes it this far
	return true;
}

//****************************************************************************************
function validatePhone(fieldname) {
	//alert(value);
	//** strip out all non-numeric characters from the field
	var value = frmMain.elements[fieldname].value;
	var good = "0123456789.";
	for(var icount = 0; icount <= value.length - 1; icount++) { 
		var char = value.substr(icount, 1);
		var pos = good.indexOf(char);
		//alert("icount = " + icount + ", char = " + char + ", pos = " + pos);
		if (pos == -1) {
			//alert(value.substr(0, icount));
			//alert(value.substr(icount + 1));
			value = value.substr(0, icount) + value.substr(icount + 1); 
			//alert("The '" + fieldname.toUpperCase( ) + "' field value is now " + value);
			icount = icount - 1;
		}
	}
	//** make sure the phone is 10 characters long
	if (value.length != 10) {
		alert("The '" + fieldname.toUpperCase( ) 
			+ "' field value must be a valid 10-digit phone number (including area code).");		
		return false;
	}
	//** return true if it makes it this far
	//alert("The '" + fieldname.toUpperCase( ) + "' field value is now " + value);
	return true;
}

//****************************************************************************************
function validateMonth(fieldname) {
	var value = frmMain.elements[fieldname].value;
	if (value > 0 && value < 13) {
		return true;	
	}
	else {
		alert("The '" + fieldname.toUpperCase( ) + "' field must be a valid month.");
		return false;
	} 
}

//****************************************************************************************
function validateYear(fieldname) {
	var value = frmMain.elements[fieldname].value;
	if (value > 1997 && value < 2098) {
		return true;	
	}
	else {
		alert("The '" + fieldname.toUpperCase( ) + "' field must be a valid year (between 1998 and 2097).");
		return false;
	} 
}

//****************************************************************************************
function validateDate(fieldname) {
	value = frmMain.elements[fieldname].value;
	origValue = value;
	alert(fieldname + " value = " + value);
	value = Date.parse(value);
	alert ("Date.parse(" + origValue + ") = " + value);
	if (isNaN(value) || value < 0) {
		alert("The '" + fieldname.toUpperCase( ) + "' field must be a valid date.");
		return false;
	}	
	return true;
}

//****************************************************************************************
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	
	if (dtStr.length < 1)
	   {
	   return false
	   }
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}


//****************************************************************************************
function LeapYear(intYear) {
	if (intYear % 100 == 0) {
		if (intYear % 400 == 0) { return true; }
	}
	else {
		if ((intYear % 4) == 0) { return true; }
	}
	return false;
}
