<!--
/* *************************************************************
* RDW CONTAINS THE MASTER COPY : EDIT THAT ONE AND COPY HERE   *
* **************************************************************
* set some global variables that will be checked later         *
***************************************************************/
var msg = "";                 // an output message
var missing = "";             // for missing required fields
var invNum = "";              // for invalid numeric fields
var outOfRange = "";          // less than min or more than max
var invZIP = "";              // for invalid zip codes
var invPhone = "";            // for invalid phone numbers
var invState = "";            // for invalid state fields
var invDate = "";             // for invalid dates
var invEmail = "";            // for email addresses

/* *************************************************************
* The main validation function, calls other sub-functions      *
***************************************************************/
function validate(frm) {
	for(i=0; i<frm.elements.length; i++) {     // loop through form elements
		var el = frm.elements[i];

		var elementName = el.name;
		elementName = elementName.replace(/_/, " ");
		elementName = elementName.replace(/_/, " ");
		elementName = elementName.replace(/_/, " ");

		if(el.required) {                      // if element has required property
			if(isEmpty(el)) {                  // test to see if field is empty
				missing += "\n   - " + elementName + " is a required field";
			}
		}
		if(el.numeric) {
			if(notNumeric(el)) {
				invNum += "\n   - " + elementName + " must be a number";
			}
		}		
		if(el.minVal) {
			if(parseFloat(el.value) <= el.minVal) {
				outOfRange +=  "\n   - " + elementName + " must be larger than " + el.minVal + ", you entered " + el.value;
			}
		}
		if(el.maxVal) {
			if(parseFloat(el.value) >= el.maxVal) {
				outOfRange +=  "\n   - " + elementName + " must be smaller than " + el.maxVal + ", you entered " + el.value
			}
		}
		if(el.zip && el.value.length !=0) {
			if(invalidZIP(el.value)) {
				invZIP += "\n  - " + el.value + " is not a valid zip code for " + elementName;
			}
		}
		if(el.phone && el.value.length !=0) {
			if(invalidPhone(el) || el.value.length > 14) {
				invPhone += "\n  - " + el.value + " is not a valid phone number for " + elementName;
			}
		}
		if(el.state && el.value.length != 0) {
			if(invalidState(el)) {
				invState += "\n  - " + el.value + " is not a valid two-letter state abbreviation for " + elementName;
			}
		}
		if(el.date && el.value.length != 0) {
			var dateMsg = ValidateDate(el.value);
			      
			if(dateMsg.length > 0) {
				invDate += "\n  - " + elementName + dateMsg;
			}
		}
		if(el.email && el.value.length !=0) {
			if(invalidEmail(el.value)) {
				invEmail += "\n  - " + el.value + " is not a valid email address for " + elementName;
			}
		}
	}
	  
	// build output message
	if(missing.length !=0 || invNum.length != 0 || outOfRange.length != 0 || 
	   invZIP.length != 0 || invPhone.length != 0 || invState.length != 0 || 
	   invDate.length != 0 || invEmail.length != 0) {
		if(missing.length !=0) {
			msg += "\n\nThe following required fields are missing:";
			msg += missing;
		}
		if(invNum.length !=0) {
			msg += "\n\nYou entered incorrect numeric data in these fields:";
			msg += invNum;
		}
		if(outOfRange.length !=0) {
			msg += "\n\nYou entered out-of-range data in these fields:";
			msg += outOfRange;
		}
		if(invZIP.length !=0) {
			msg += "\n\nYou entered an incorrect zip code";
			msg += invZIP;
		}
		if(invPhone.length !=0) {
			msg += "\n\nYou entered an incorrect phone number";
			msg += invPhone;
		}
		if(invState.length !=0) {
			msg += "\n\nYou entered an incorrect state abbreviation";
			msg += invState;
		}
		if(invDate.length !=0) {
			msg += "\n\nYou entered an incorrect date";
			msg += invDate;
		}
		if(invEmail.length !=0) {
			msg += "\n\nYou entered an incorrect email address";
			msg += invEmail;
		}
	      
		errMsg(msg);           // call the output function
		    
		// reset all our variables
		msg = "";
		missing = "";
		invNum = "";
		invZIP = "";
		invPhone = "";
		invState = "";
		invDate = "";
		invEmail = "";
		outOfRange = ""
	    
		return false;
	} else {
		return true;
	}
}

/* *************************************************************
* Sub-functions follow from here to end of file                *
* All sub-functions return true if field is of invalid         *
* format and false if they are valid entries                   *
***************************************************************/
function isEmpty(field) {
	str = field.value;
	if(str == "") {  // make sure not to put a space between those quotes
		return true;
	} else {
		for(j=0; j<str.length; j++) {
			if(str.charAt(j) != " ") { // make sure to put a space between those quotes!
				return false;
			}
		}
	}
	return true;
}

function notNumeric(field) {
	var errCount = 0;
	var numdecs = 0;                    // number of decimal points
	
	for(j=0;j<field.value.length;j++) {
		c = field.value.charAt(j);        // short hand notation for character at position j
		if((c >= 0 && c <= 9) || c=="." || (j==0 && c == "-")) {
			if(c==".") {
				numdecs++;          // count the number of decimal points
			}
		} else {
			errCount++;                    // if it's none of those, increment error counter
			break;                         // no need to continue looping, it's not a number
		}
	}
	// error if count is non-zero or there are more than one decimal point
	if(errCount > 0 || numdecs > 1) {
		return true;
	}
	return false;
}

function stripNonDigits(str) {
	newStr = "";
	for(j=0; j<str.length; j++) {
		c = str.charAt(j);
		if(c >= "0" && c <= "9") {
			newStr += c;
		}
	}
	return newStr;
}

function invalidZIP(field) {
	var zipcode = field;
	
	if(zipcode.length == 5 || zipcode.length == 9) {
		var subZip = stripNonDigits(zipcode);
		if(subZip.length == zipcode.length) {
			return false;
		} else {
			return true;
		}
	} else if(zipcode.length == 10 && (zipcode.charAt(5) == "-" || zipcode.charAt(5) == " ")) {
		subZip = zipcode.substring(0,5) + zipcode.substring(6,10);
		subZip = stripNonDigits(subZip);
		if(subZip.length == 9) {
			return false;
		} else {
			return true;
		}
	} else if(zipcode.length == 6 || zipcode.length == 7) {
		if(zipcode.length == 6) {
			zipcode = zipcode.substr(0,3) + " " + zipcode.substr(3,3)
		}
		if (zipcode.search(/^([A-Z]\d[A-Z]\s\d[A-Z]\d)|([a-z]\d[a-z]\s\d[a-z]\d)$/) != -1)
		    return false;
		else
		    return true;
	}
	return true;
}

function invalidPhone(field) {
	newStr = stripNonDigits(field.value);
	if(newStr.length == 10) {
		return false;
	}
	return true;
}

function invalidState(field) {
	var STATES = "AL/AK/AZ/AR/CA/CO/CT/DE/DC/FL/GA/HI/ID/IL/IN/IA/KS/KY/LA/ME/MD/MA/MI/MN/MS/MO/MT/NV/NE/NH/NJ/NM/NY/NC/ND/OH/OK/OR/PA/PR/RI/SC/SD/TN/TX/UT/VT/VA/WA/WV/WI/WY/AB/BC/MB/NB/NF/NS/NU/NW/ON/PE/QC/SK/YK";
	var newStr = field.value.toUpperCase();
	if(STATES.indexOf(newStr) == -1 || newStr.indexOf("/") != -1 || newStr.length != 2) {
		return true;
	}
	return false;
}

function invalidEmail(str) {
    if (str.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
        return false;
    else
        return true;
}

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 ValidateDate(dtStr){
	var returnMsg = "";
	var today = new Date();
	var dtCh= "/";
	var minYear=2001;
	var maxYear= today.getYear() + 1;
	
	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 (pos1==-1 || pos2==-1){
		returnMsg = ": date format should be : mm/dd/yyyy";
	}
	if (strMonth.length<1 || month<1 || month>12){
		returnMsg = " is not a valid month";
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		returnMsg = " is not a valid day"
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		returnMsg = " does not have a valid 4 digit year between " + minYear + " and " + maxYear
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		returnMsg = " is not a valid date"
	}
	return returnMsg;
}

function ValidateForm(){
	var dt=document.frmSample.txtDate
	if (isDate(dt.value)==false){
		dt.focus()
		return false
	}
    return true
}

function errMsg(msg) {
	var theMsg = "You entered some incorrect values into the form. ";
	theMsg += "Please correct your entries then re-submit the form.\n";
	theMsg += "____________________________________________________________________";
	theMsg += msg;
	theMsg += "\n____________________________________________________________________\n";
	alert(theMsg);
}
//-->
