//FORM VALIDATION///////////////////////////////////////////////////////////////////////////////////////

// "t" = TEXTBOX OR TEXTAREA
// "s" = SELECT BOX (DROP-DOWN MENU)
// "n" = NUMBER in TEXTBOX
// "d" = DATE
// "z" = ZIP
// "e" = EMAIL
// "p" = PHONE OR FAX

//TEXTBOX////////////////////////////////////////////////////////////////////////////////////////////////
function validate(s_type,b_req,s_val,s_ele,s_eletext) {
	var b_retval = false;
	switch(s_type) {
		case "t":
			b_retval = validateText(s_val,b_req);
			break;
		case "e":
			b_retval = validateEmail(s_val,b_req);
			break;
		case "d":
			b_retval = validateDate(s_val,b_req);
			break;
		case "s":
			b_retval = validateSelect(s_val);
			break;
		case "n":
			b_retval = validateNumber(s_val,b_req);
			break;
		case "p":
			b_retval = validatePhone(s_val,b_req);
			break;
		case "z":
			b_retval = validateZip(s_val,b_req);
			break;
		case "io":
			b_retval = validateIO(s_val,b_req);
			break;
	}
	o_ele = document.getElementById(s_ele);
	o_eletext = document.getElementById(s_eletext);
	if (b_retval) {
		if (o_eletext) { o_eletext.className = "valid"; }
	} else {
		if (o_eletext) { o_eletext.className = "invalid"; }
	}
	return b_retval;
}

function validateMatch(s_ele1,s_ele2,s_eletext) {
	var b_retval, s_class;
	try {
		b_retval = ($F(s_ele1) == $F(s_ele2));
	} catch(e) {
		return false;
	}
	if (b_retval) {
		s_class = "valid";
	} else {
		b_retval = false;
		s_class = "invalid";
	}
	try { $(s_eletext).className = s_class; } catch(e) {};
	return b_retval;
}

function validateText(str,isreq) {
	if(str==""){
		return false
	} else { 
		return true 
	}
}

//WEB ADDRESS////////////////////////////////////////////////////////////////////////////////////////////////

function validateWeb(myElement,isreq){
	webaddress = myElement.value.toLowerCase()
	if(isreq && webaddress=="") {
		alert("Please fill in this field as it is required.")
		myElement.focus()
		return false
	}
	else if(webaddress!="" && webaddress.substring(0,4)!="www."){
		alert("Certain characters required for a valid web address are missing in this field.")
		myElement.focus()
		return false
	}
	else {
		return true
	}
}

//PHONE//////////////////////////////////////////////////////////////////////////////////////////

function validatePhone(str,isreq){
	if (isreq || str != "") {
		s=stripCharsInBag(str,"()- ");
		return (isInteger(s) && s.length == 10);
	} 
	else { return true }
}

//VALIDATE SPECIFIC CHARACTERS///////////////////////////////////////////////////////////////////

function charCheck(myElement, swap) {
	for(var i=0;i<myElement.length;i++) {
		if(myElement.charAt(i)==swap){ return true }
	}
}

//EMAIL//////////////////////////////////////////////////////////////////////////////////////////////

function validateEmail(str,isreq){
	if (str=="" && isreq){ return false }
	else if (str!="" && !charCheck(str,"@")){ return false }
	else if (str!="" && !charCheck(str,".")){ return false }
	else { return true }
}

//ZIP//////////////////////////////////////////////////////////////////////////////////////////////////

function validateZip(str,isreq){
	if(isreq && str == "") { return false; }
	if(isNaN(str)){ return false; }
	else if(charCount(str, 5)) { return false; }
	else { return true }
}

//DATE////////////////////////////////////////////////////////////////////////////////////////////////

function validateDate(str,isreq){
	if (isreq || str != "") {
		return isDate(str);
	}
	else {
		return true;
	}
}


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 (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;
}

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
}

//NUMBER////////////////////////////////////////////////////////////////////////////////////////////

function validateNumber(str,required){
	if(str==""){
		if(required){ return false; }
		else { return true; }
	} else {
		if(isNaN(str)) { return false; } else { return true; }
	}
}

function validateIO(str,required){
	if(str==""){
		if(required){ return false; } else { return true; }
	} else {
		if (str.length == 5) {
			var pos1 = str.substr(0,3);
			var pos2 = str.substr(3,1);
			var pos3 = str.substr(4);
			return ((!isNaN(pos1))&&(pos2=="-")&&(!isNaN(pos3)));
		} else { return false; }
	}
}

//COUNT THE CHARACTERS////////////////////////////////////////////////////////////////////////////////

function charCount(myElement, swap){
if(myElement.length!=swap){
	return true
	}
}

//SOCIAL SECURITY NUMBERS//////////////////////////////////////////////////////////////////////////////

function validateSocSec(myElement,isreq){
if(isreq && myElement.value == ""){
		alert("Please enter a Social Security Number as it is required.")
		myElement.focus()
		return false
	}	
else if(myElement.value != "" && isNaN(myElement.value)){
	alert("Please re-enter this Social Security Number using ONLY numerals.")
	myElement.focus()
	myElement.value=""
	return false
	}
else if(myElement.value != "" && charCount(myElement.value, 9)){
	alert("Nine (9) numerals are required for this field.")
	myElement.focus()
	return false
	}
else {
	return true
	}
}

//SELECT / DROPDOWN MENUS///////////////////////////////////////////////////////////////////////////////

function validateSelect(str){
	if(str==""){
		return false
	} else { 
		return true 
	}
}
