function validateEmpty(fld,errortxt) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = errortxt;
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateRadio(fld,errortxt) {
    var error = "";
    var cnt = -1;
	
    for (var i=fld.length-1; i > -1; i--) {
        if (fld[i].checked) {cnt = i; i = -1;}
    }
    if (cnt == -1) {
        error = errortxt;
	}
    return error;
}

function validateList(fld,errortxt) {
    var error = "";

    if (fld.selectedIndex == 0 ){
        fld.style.background = 'Yellow'; 
        error = errortxt;
	} else {
        fld.style.background = 'White';
    }
    return error;
}

function validateCheckbox(fld,errortxt) {
    var error = "";
    var cnt = 0;
	
	if (!fld.length && fld.checked) {
		cnt++
	}
	for (var i = 0; i<fld.length; i++) {
		if (fld[i].checked) {cnt++}   
	}
    if (cnt == 0) {
        error = errortxt;
	}
	return error;
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
	var error="";
	var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
	var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
	var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
	if (fld.value == "") {
		fld.style.background = 'Yellow';
		error = "Please enter an email address.\n";
	} else if (!emailFilter.test(tfld)) {              //test email for illegal characters
		fld.style.background = 'Yellow';
		error = "Please enter a valid email address.\n";
	} else if (fld.value.match(illegalChars)) {
		fld.style.background = 'Yellow';
		error = "The email address contains illegal characters.\n";
	} else {
		fld.style.background = 'White';
	}
	return error;
}

function validateVerify(fld1,fld2,errortxt) {
    var error = "";
	
	if (fld1.value != "") {
		if (fld1.value != fld2.value) {
			fld2.style.background = 'Yellow'; 
			error = errortxt;
		} else {
			fld2.style.background = 'White'; 
		}
	}
    return error;
}

function validatePassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "Please enter a password.\n";
    } else if ((fld.value.length < 6) || (fld.value.length > 10)) {
        error = "The password is the wrong length. \n";
        fld.style.background = 'Yellow';
    } else if (illegalChars.test(fld.value)) {
        error = "The password contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!((fld.value.search(/[a-z]+/) > -1) && (fld.value.search(/[0-9]+/) > -1))) {
        error = "The password must contain at least one letter and one numeral.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    }
   return error;
}

function validatePassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "Please enter a password.\n";
    } else if ((fld.value.length < 6) || (fld.value.length > 10)) {
        error = "The password is the wrong length. \n";
        fld.style.background = 'Yellow';
    } else if (illegalChars.test(fld.value)) {
        error = "The password contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!((fld.value.search(/[a-z]+/) > -1) && (fld.value.search(/[0-9]+/) > -1))) {
        error = "The password must contain at least one letter and one numeral.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    }
   return error;
}

function validateZip(fld) {
	var error="";
	var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
	var zipFilter = /[0-9]{5}(-[0-9]{4})?/;
   
	if (fld.value == "") {
		fld.style.background = 'Yellow';
		error = "Please enter a zip code.\n";
	} else if (!zipFilter.test(tfld)) {
		fld.style.background = 'Yellow';
		error = "Please enter a 5 digit zip code.\n";
	} else {
		fld.style.background = 'White';
	}
	return error;
}

function notZero(fld,errortxt){
    var error = "";
	
	if(fld.value < 1){
		fld.style.background = 'Yellow';
		error = errortxt;
    } else {
        fld.style.background = 'White';
    }
	return error;
}

function clearText(thefield){
	if (thefield.defaultValue==thefield.value)
	thefield.value = ""
} 

// Begin Date Functions

// Begin Date Functions

function validateYear(fld) {
    var error = "";
    var illegalChars = /^[a-zA-Z]+$/; // allow only numbers 
 
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "Please enter a year.\n";
    } else if ((fld.value.length < 4) || (fld.value.length > 4)) {
        error = "Please enter a four number year. \n";
        fld.style.background = 'Yellow';
    } else if (illegalChars.test(fld.value)) {
        error = "The year must be numbers only.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    }
   return error;
}

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 error = ""
	var dtCh= "/"                            // Declaring valid date character
	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){
		error = "The date format should be : mm/dd/yyyy.\n";
		return error;
	} 
	if (strMonth.length<1 || month<1 || month>12){
		error = "Please enter a valid month.\n";
		return error;
	} 
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		error = "Please enter a valid day.\n";
		return error;
	} 
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		error = "Please enter a valid date.\n";
		return error;
    }
   return error;
}

function validateDate(fld){
	var error = isDate(fld.value);
		if (error != "") {
        	fld.style.background = 'Yellow';
		} else {
        	fld.style.background = 'White';		
		}
    return error;
}

function validateFutureDate(fld){
	var error = ""
	var today = new Date()
	var futureDate = new Date(fld.value)
	
	error = isDate(fld.value);
	if (error != "") {
		fld.style.background = 'Yellow';
		return error;
	} else {
		fld.style.background = 'White';		
	}
	if (futureDate<today) {
		error = "Please enter a future date.\n";
		fld.style.background = 'Yellow';
		return error;
	} else {
		fld.style.background = 'White';		
	}
    return error;
}

// End Date Functions