//// form validation scripts 
// Thanks H!
var alpha = /[^A-Za-z]/ig;
var alphaNum =/[^\w \-]/ig;
var alphaNumPunc =/[^\w \- \. , \']/ig;
var num = /[^\d]/ig;
var notNull = /[^\s]/ig;
var email = /(\w{1,})@.*\.(\w{2,})/ig;
var URL = /(http:\/\/)?[\w\.-]+\.[A-Za-z\.]{2,}/ig;
var dte = /\d{1,2}\/\d{1,2}\/[20]?[\d{2}][ ]?[\d:ampm ]?/ig;
var img = /\.(gif|jp[e]?g)$/ig;
var nospace = /\s/g;

function charCheck(f,ty,m,e) {
	var e = (e || !e) ? e : true;
	if (e) {
		var f_type = f.type;
		var f_val = (f_type == "checkbox" || f_type == "select" || f_type == "radio") ? f[f.selectedIndex].value : f.value;
		if (ty == "alpha") {
			e = (f_val.search(alpha) == -1) ? true : false;
		} else if (ty == "alphaNum") {
			e = (f_val.search(alphaNum) == -1) ? true : false;
		} else if (ty == "alphaNumPunc") {
			e = (f_val.search(alphaNumPunc) == -1) ? true : false;
		} else if (ty == "num") {
			e = (f_val.search(num) == -1) ? true : false;
		} else if (ty.toLowerCase() == "notnull") {
			e = (f_val.search(notNull) == -1) ? false : true;
		} else if (ty.toLowerCase() == "url") {
			e = (f_val.match(URL) == null) ? false : true;
		} else if (ty.toLowerCase() == "email") {
			e = (f_val.match(email) == null) ? false : true;
		} else if (ty == "date") {
			e = (f_val.match(dte) == null) ? false : true;
		} else if (ty == "nospace") {
			e = (f_val.match(nospace) == null) ? true : false;
		}
		
		if (!e) {
			if (ty == "date") {
				alert("The "+m+" you've entered is invalid.  \nPlease enter dates using the following format:\n\n\tMM/DD/YYYY");
			} else if (ty == "nospace") {
				alert ("Sorry, there are no spaces allowed in "+m+".  \nPlease remove all spaces and try again.");
			} else if (ty == "alphaNum") {
				alert ("Sorry, only letters and numbers are allowed in "+m+".  \nPlease correct this and resubmit.");
			} else if (ty == "alphaNumPunc") {
				alert ("Sorry, only letters, numbers, spaces, and punctuation marks are allowed in "+m+".  Please correct this and resubmit.");
			} else if (m != "" && m != "no") {
				var msg = (ty.toLowerCase() != "notnull") ? "The value you have entered for " + m + " is invalid.  Please re-enter this information." : "The " + m + " field is required.  Please correct this and resubmit.";
				alert(msg);
			}			
			f.focus();
		}
		
	}
	return e;
}

function zipAuth(zip,country) {
	var z = zip.value;
	var c = country;
	var zFit, zCountry, zMask;
	var procZip = "";
		if (c == 1) { // USA
			zFit = /\d{5}[\s-]?(\d{4})?/i;
			zCountry = "USA";
			zMask = "\t- #####\n\t- #####-####";
		} else if (c == 32) { // canada
			zFit = /\w\d\w([\s-]?)[0-9][a-zA-Z][0-9]/i;
			zCountry = "Canada";
			zMask = "\t- !#!-#!#";
		} else if (c == 60 || c == 143 || c == 218) { // england, n.ireland, wales
			zFit = /[\d\w]{3}[\s-]?[\d\w]{3}/i;
			zCountry = "England, Northern Ireland or Wales";
			zMask = "\t- XXX-XXX";
		} else if (c == 170) { // scotland
			zFit = /[\d\w]{2}[\s-]?[\d\w]{3}/i;
			zCountry = "Scotland";
			zMask = "\t- XX-XXX";
		} else if (c == 227) { // australia
			zFit = /[\d]{4,}/i;
			zCountry = "Australia";
			zMask = "\t- ####";
		} else {
			zFit = /[\d\w]+/i;
			zMask = "";
		}
		procZip = String(z.match(zFit));
		//alert(procZip);
		if (procZip == "" || procZip == "null") {
			msg = (zMask != "") ? "The postal code you have entered is incorrectly formatted\nfor "+zCountry+"  \n\nPlease enter your postal code in the following format:\n\n"+zMask+"\n\n where " : "The Postal Code field is required.\nPlease enter your postal code";
			if (zMask != "") {
				if (zMask.indexOf("X") > -1) {
					msg += "X can be either a letter or a number.";
				} else if (zMask.indexOf("!") > -1) {
					msg += "! represents a letter and # represents a number.";
				} else if (zMask.indexOf("!") == -1 && zMask.indexOf("X") == -1) {
					msg += "# represents a number.";
				}
			}
			alert(msg);
			zip.focus();
			return false;
		} else {
			
			zip.value = z.replace(/[^\w]/ig,"");
			return true;
		}
}
