
function ifNotBlankSkipTo(fmField, toField) {
	if ((fmField.type == "text") || (fmField.type == "textarea") ||
			(fmField.type == "file")){
		if ((fmField.value != null) && !isBlank(fmField.value)) {
			toField.focus();
			toField.select();
 		}
	} else if ((fmField.type == "select-one") || (fmField.type == "select-multiple")) {
		if ((fmField.selectedIndex > -1) && !isBlank(fmField.options[fmField.selectedIndex].text)) {
			toField.focus();
		}
	}
}


//function to validate an email address

function NotValidEmail(emailfield,next) {
	
	var email = emailfield.value;
	
	invalidChars = " /:,;?~`!#$%^&*()";	
	
	if (email != "") {

		for (i=0; i<invalidChars.length; i++) {
			aChar = invalidChars.charAt(i);
			if (email.indexOf(aChar, 0) > -1) {
				alert("Email address\n contains invalid characters!"); //if any invalid chars
				emailfield.focus();
				return;
			}
		}

		atPos = email.indexOf("@", 1);
		if (atPos == -1) {
			alert("Email address\n is missing the '@'!"); //if no "@"
			emailfield.focus();
			return;
		}
		if (email.indexOf("@", atPos+1) > -1) {
			alert("Email address\n cannot contain multiple '@s'!"); //if multiple "@"
			emailfield.focus();
			return;
		}

		periodPos = email.indexOf(".", atPos);
		if (periodPos == -1) {
			alert("Email address\n is missing the '.'!"); //if no period after "@"
			emailfield.focus();
			return;
		}
		if (periodPos <= atPos+1)  {
			alert("Email address\n is missing characters between '@' and '.'!"); //if not at least 1 char after "@" and before "."
			emailfield.focus();
			return;
		}
		if (periodPos+3 > email.length)  {
			alert("Email address\n must have at least 2 characters after '.'!"); //if not at least 2 chars after "."
			emailfield.focus();
			return;
		}
	}
	if((parseInt(navigator.appVersion)<5) && (navigator.appName == "Netscape"))
		next.focus();
	return; //valid as far as we can tell
}

//Returns true if str contains only whitespace characters
function isBlank(str) {
	for (var i=0; i<str.length; i++) {
		var c = str.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '\r') && (c != '\t')) {
			return false;
		}
	}
	return true;
}


var AttentionColor = "F3F0E9";
var text = "Black";

function changeColor(e,color)
{
	if (e.style){
	e.style.background=color;
	e.style.color=text;	
	}
	
}

function placeFocus() {
if (document.forms.length > 0) {
var field = document.forms[0];
for (i = 0; i < field.length; i++) {
if ((field.elements[i].type == "text") || (field.elements[i].type == "textarea") || (field.elements[i].type.toString().charAt(0) == "s")) {
document.forms[0].elements[i].focus();
break;
}
}
}
}

function changeColorBack(e,next)
{
	if (e.style){
	e.style.background="white";
	e.style.color="black"
	}
	if((arguments.length == 2) && (parseInt(navigator.appVersion)<5) && (navigator.appName == "Netscape"))
	next.focus();
}

//Validate fields in a form and DONT show the upload message
function Validate(theForm) {
	var msg;
	var empty_fields = "";
	var errors = "";
	var topErrFld = null;

	for (var i=0; i<theForm.elements.length; i++) {
		var e = theForm.elements[i];

		if (e.Required) {

			if ((e.type == "text") || (e.type == "textarea") ||
					(e.type == "file") || (e.type == "password")){
				if ((e.value == null) || isBlank(e.value)) {
					empty_fields += "\n          " + e.ExName;
					if (topErrFld == null) topErrFld = e;
					changeColor(e,AttentionColor);
					continue;
				}
			} else if ((e.type == "select-one") || (e.type == "select-multiple")) {
				if ((e.selectedIndex <= 0) || isBlank(e.options[e.selectedIndex].text)) {
					empty_fields += "\n          " + e.ExName;
					if (topErrFld == null) topErrFld = e;
					changeColor(e,AttentionColor);
					continue;
				}
			}
		}
		if ((e.email) && NotValidEmail(e.value)) {
			if (topErrFld == null) topErrFld = e;
			errors += "-  The field " + e.ExName + " must contain a properly formed email\r\n" +
				"     address (In the form 'person@location', with no spaces.)"
		}
	}

	if ((empty_fields != "") || (errors != "")) {
		msg = "__________________________________________\n\n";
		msg += "Please resolve the following issues then \n";
		msg += "click the Submit button again.\n";
		msg += "_________________________________________\n\n";

		if (empty_fields != "") {
			msg += "-  The following  field(s) are empty:\n";
			msg += empty_fields + "\n";
			if (errors != "") msg += "\n"
		}
		msg += errors;
		alert(msg);

		if (topErrFld != null) {
			if ((topErrFld.type == "text") || (topErrFld.type == "textarea") ||
					(topErrFld.type == "file")) {
				topErrFld.select();
			}
			topErrFld.focus();
		}
		return false; //cancel the submit

	} else { 
	
	//no errors found submit the form

		return true;
	}
}


//check phone number for punctuation

function NotValidPhone(phonefield,next) {
	
	var phone = phonefield.value;
	var invalidChars = "+_@\><.;/:,;?~`!#$%^&*abcdefghijklmnopqrstuvwxyz";	
	var color = "F3F0E9";
	if (phone != "") {

		for (i=0; i<invalidChars.length; i++) {
			aChar = invalidChars.charAt(i);
			if (phone.indexOf(aChar, 0) > -1) {
				alert("Phone number contains invalid characters!\n Only Numbers, Parenthesis & Hyphens are allowed.\n (555)555-5555"); //if any invalid chars
				phonefield.focus();
				return;
			}
		}
	}
	if((parseInt(navigator.appVersion)<5) && (navigator.appName == "Netscape"))
	next.focus();
	return; //valid as far as we can tell
}


//check Number
function NotValidNumber(numberfield) {
	var number = numberfield.value;
	var color = "F3F0E9";
	var invalidChars = "-)(=+_@\><;/:,;?~`!#$%^&*abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";	
	if (number != "") {
		for (i=0; i<invalidChars.length; i++) {
			aChar = invalidChars.charAt(i);
			if (number.indexOf(aChar, 0) > -1) {
				alert("Field contains invalid characters!\n Only Numbers are allowed.");
				numberfield.focus();
				return;
			}
		}

	}	
}

//check Zip

function NotValidZip(zipfield) {
	if (zipfield.value!=""){
		if ((zipfield.value.length != 5)|| isNaN(zipfield.value)) { 
			alert("Please enter a 5 digit, numeric zip code.");
			zipfield.focus();
			return;
		}
	}	
}
// check Date

function checkValidDate(dateStr) {
    // dateStr must be of format month day year with either slashes
    // or dashes separating the parts. Some minor changes would have
    // to be made to use day month year or another format.
    // This function returns True if the date is valid.
    var slash1 = dateStr.indexOf("/");
    if (slash1 == -1) { slash1 = dateStr.indexOf("-"); }
    // if no slashes or dashes, invalid date
    if (slash1 == -1) { return false; }
    var dateMonth = dateStr.substring(0, slash1)
    var dateMonthAndYear = dateStr.substring(slash1+1, dateStr.length);
    var slash2 = dateMonthAndYear.indexOf("/");
    if (slash2 == -1) { slash2 = dateMonthAndYear.indexOf("-"); }
    // if not a second slash or dash, invalid date
    if (slash2 == -1) { return false; }
    var dateDay = dateMonthAndYear.substring(0, slash2);
    var dateYear = dateMonthAndYear.substring(slash2+1, dateMonthAndYear.length);
    if ( (dateMonth == "") || (dateDay == "") || (dateYear == "") ) { return false; }
    // if any non-digits in the month, invalid date
    for (var x=0; x < dateMonth.length; x++) {
        var digit = dateMonth.substring(x, x+1);
        if ((digit < "0") || (digit > "9")) { return false; }
    }
    // convert the text month to a number
    var numMonth = 0;
    for (var x=0; x < dateMonth.length; x++) {
        digit = dateMonth.substring(x, x+1);
        numMonth *= 10;
        numMonth += parseInt(digit);
    }
    if ((numMonth <= 0) || (numMonth > 12)) { return false; }
    // if any non-digits in the day, invalid date
    for (var x=0; x < dateDay.length; x++) {
        digit = dateDay.substring(x, x+1);
        if ((digit < "0") || (digit > "9")) { return false; }
    }
    // convert the text day to a number
    var numDay = 0;
    for (var x=0; x < dateDay.length; x++) {
        digit = dateDay.substring(x, x+1);
        numDay *= 10;
        numDay += parseInt(digit);
    }
    if ((numDay <= 0) || (numDay > 31)) { return false; }
    // February can't be greater than 29 (leap year calculation comes later)
    if ((numMonth == 2) && (numDay > 29)) { return false; }
    // check for months with only 30 days
    if ((numMonth == 4) || (numMonth == 6) || (numMonth == 9) || (numMonth == 11)) { 
        if (numDay > 30) { return false; } 
    }
    // if any non-digits in the year, invalid date
    for (var x=0; x < dateYear.length; x++) {
        digit = dateYear.substring(x, x+1);
        if ((digit < "0") || (digit > "9")) { return false; }
    }
    // convert the text year to a number
    var numYear = 0;
    for (var x=0; x < dateYear.length; x++) {
        digit = dateYear.substring(x, x+1);
        numYear *= 10;
        numYear += parseInt(digit);
    }
    // Year must be a 2-digit year or a 4-digit year
    if ( (dateYear.length != 2) && (dateYear.length != 4) ) { return false; }
    // if 2-digit year, use 50 as a pivot date
    if ( (numYear < 50) && (dateYear.length == 2) ) { numYear += 2000; }
    if ( (numYear < 100) && (dateYear.length == 2) ) { numYear += 1900; }
    if ((numYear <= 0) || (numYear > 9999)) { return false; }
    // check for leap year if the month and day is Feb 29
    if ((numMonth == 2) && (numDay == 29)) {
        var div4 = numYear % 4;
        var div100 = numYear % 100;
        var div400 = numYear % 400;
        // if not divisible by 4, then not a leap year so Feb 29 is invalid
        if (div4 != 0) { return false; }
        // at this point, year is divisible by 4. So if year is divisible by
        // 100 and not 400, then it's not a leap year so Feb 29 is invalid
        if ((div100 == 0) && (div400 != 0)) { return false; }
    }
    // date is valid
    return true;
}

/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
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 (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
}



