//Globals
var d = document;
var b = document.body;
var w = window;
var br = d.createElement("br");

function clearError(oField){
	var oPar = oField.parentNode;
	if (oField.nextSibling.nextSibling.nextSibling.nodeName.toLowerCase() == "div")
		for (var i = 0; i < 3; i++) oPar.removeChild(oField.nextSibling);	
	return;
}

function printError(msg, oField){
	var oPar = oField.parentNode;
	var err = d.createElement("div");
	var errMsg = d.createTextNode(msg);
	var errLbl = d.createElement("label");
	
	oField.className = "quote-error";
	
	err.className = "quote-error";
	err.appendChild(errMsg);
	
	oPar.insertBefore(err, oField.nextSibling);
	oPar.insertBefore(errLbl,err);
	oPar.insertBefore(br.cloneNode(false),errLbl);
	
	return "";
}

function checkText(oField,nums){
	clearError(oField);
	var charRE = /^\.+$/;
	var errMsg = "";
	
	var val = oField.value.replace(/^\s*|\s*$/g,"");
	if (nums){
		charRE = /^([a-zA-Z0-9]|\.|\-|\s|\,)+$/;
		errMsg = "The text entered in this field may only contain letters, numbers, spaces, periods(.), commas(,), and hyphens(-).";
	}else{
		charRE = /^([a-zA-Z]|\.|\-|\s|\,)+$/;
		errMsg = "The text entered in this field may only contain letters, spaces, periods(.), commas(,), and hyphens(-).";
	}
	
	if (val.length > 0){
		if (!charRE.test(val))
			return printError(errMsg, oField);
		else oField.className = "quote-acceptable";
	}else return val = "";
	
	return val;
}


function formatzip(zip){
	if (zip.length == 6 && zip.charAt(5) != "-")
		return zip.substring(0,5) + "-" + zip.substring(5,zip.length);
	else return zip;
}

function validatezip(oZip){
	clearError(oZip);
	var zip = oZip.value.replace(/^\s*|\s*$/g,"");
	var zipre = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
	if (zip.length > 0){
		if(!zipre.test(zip)){
			if(zipre.test(zip.substring(0,5)))
				zip = zip.substring(0,5);
			return printError("Zip code must exactly 5 or 9 digits long and cannot contain any characters other than a hyphen(\"-\").",oZip);
		}else oZip.className = "quote-acceptable";
	}
	return zip;
}

function ValidateEmail(oEmail){
	clearError(oEmail);
	var email = oEmail.value.replace(/^\s*|\s*$/g,"");	//trim begining and trailing whitespace;
	var emailre = /^([a-zA-Z0-9_\-\.\&]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/; 	//Regular Expression from http://www.webassist.com/Products/Help/33/wa_fv_60.htm
	
	if (email.length > 0){
		if (!emailre.test(email))
			return printError ("Invalid email address.", oEmail);
		else oEmail.className = "quote-acceptable";
	}

	return email;
}

function FormatPhone(oPN){
	var FmtStr = "";
	var pn = oPN.value.replace(/^\s*|\s*$/g,"");
	var pnre = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;

	if (!pnre.test(pn)){
		for (var i = 0; i < pn.length; i++) if (!isNaN(parseInt(pn.charAt(i)))) FmtStr = FmtStr + pn.charAt(i); 
		if (FmtStr.length == 10) FmtStr = "(" + FmtStr.substring(0,3) + ") " + FmtStr.substring(3,6) + "-" + FmtStr.substring(6,10);
		else if (pn.length != 0) return printError("Phone numbers must have exactly ten digits.", oPN);
	}else FmtStr = pn;
	
	oPN.className = "quote-acceptable";
	return FmtStr;
}
		