// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

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++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function validEmail(email) {
	invalidChars = " /:,;"
	
	if (email == "") {// cannot be empty
				return false
	}
	for (i=0; i<invalidChars.length; i++) {	// does it contain any invalid characters?
	badChar = invalidChars.charAt(i)
		if (email.indexOf(badChar,0) > -1) {
			return false
		}
	}
	atPos = email.indexOf("@",1)			// there must be one "@" symbol
		if (atPos == -1) {
			return false
		}
		if (email.indexOf("@",atPos+1) != -1) {	// and only one "@" symbol
			return false
		}
	periodPos = email.indexOf(".",atPos)
		if (periodPos == -1) {			// and at least one "." after the "@"
			return false
		}
		if (periodPos+3 > email.length)	{	// must be at least 2 characters after the "."
			return false
		}
	return true
}


//get values from querystring
	var args = new Object();
	var query = location.search.substring(1); 		// get query string.
	//alert("querystring=" + query);
	var pairs = query.split(",");					//break at comma
	//alert("Commas removed(pairs)=" + pairs);
	var hairs = query.split("&");					//break at &
	//alert("& removed(hairs)=" + hairs);
	//var firstpart = hairs[0].split("=");
	//alert("Equal signs removed(firstpart) -> " + firstpart);
	var checker = "";
	
	var styleclass = "";
	var styleclass2 = "";
	var styleclass3 = "";
	var styleclass4 = "";
	var styleclass5 = "";
	var styleclass6 = "";
	var styleclass7 = "";
	var styleclass8 = "";
	
	var firstname = "";
	var lastname = "";
	var email = "";
	var address = "";
	var city = "";
	var state = "";
	var zip = "";
	var phone = "";
	
	var iggy1;
	var iggy2;
	var iggy3;
	var iggy4;
	var iggy5;
	var iggy6;
	var iggy7;
	var iggy8;
	
	if (query == "") {
		//alert("no query");
		checker == "";
	}
	if (query != "") {
	//alert("there's a querystring");
	checker="1";
	firstname = hairs[0].split("=");
	lastname = hairs[1].split("=");
	email = hairs[2].split("=");
	address = hairs[3].split("=");
	city = hairs[4].split("=");
	state = hairs[5].split("=");
	zip = hairs[6].split("=");
	phone = hairs[7].split("=");
	
	/*alert("First name = " + firstname + "\n" +
		  "Last name = " + lastname + "\n" +
		  "Email = " + email + "\n" +
		  "Address = " + address + "\n" +
		  "City = " + city + "\n" +
		  "State = " + state + "\n" +
		  "Zip = " + zip + "\n" +
		  "Phone = " + phone); */
	}
	
function quoteform() {
	if (checker == "1") {
		//alert("Oops - ERROR");
		//styleclass = "error";
			iggy1 = firstname[1];
			if (iggy1 == "") {
				styleclass = "error";
			}
			else {styleclass = "noerror"}
			////////////////////////////
			iggy2 = lastname[1];
			if (iggy2 == "") {
				styleclass2 = "error";
			}
			else {styleclass2 = "noerror"}
			////////////////////////////
			
			
			/*
			if (!validEmail(email.value)) {
				alert("Please enter a valid e-mail address")
				email.focus()
				email.select()
				return false;
			}
			*/
			
			/////////////////////////
			iggy3 = email[1];
			//alert(iggy3.value);
			if (iggy3 == "" || !validEmail(iggy3)) {
				styleclass3 = "error";
			}
			else {styleclass3 = "noerror"}
			//////////////////////////////
			iggy4 = address[1];
			if (iggy4 == "") {
				styleclass4 = "error";
			}
			else {styleclass4 = "noerror"}
			////////////////////////////////
			iggy5 = city[1];
			if (iggy5 == "") {
				styleclass5 = "error";
			}
			else {styleclass5 = "noerror"}
			///////////////////////////////
			iggy6 = state[1];
			if (iggy6 == "") {
				styleclass6 = "error";
			}
			else {styleclass6 = "noerror"}
			///////////////////////////////
			iggy7 = zip[1];
			if (iggy7 == "" || isNaN(iggy7)) {
				styleclass7 = "error";
			}
			else {styleclass7 = "noerror"}
			/////////////////////////////////
			
			//add phone code here
			
			
			/////////////////////////////////
			iggy8 = phone[1].replace(/%20/g, " ");
			if (iggy8 == "" || checkInternationalPhone(iggy8) == false) {
				styleclass8 = "error";
			}
			else {styleclass8 = "noerror"}
		}
		else if (checker == "") {
			//alert("A-OK")
			styleclass = "noerror";
			if (firstname[1] == undefined) {
				//alert("yyyyy");
				iggy1 = "";
			}
			else {iggy1 = firstname[1]}
			//////////////
			styleclass2 = "noerror";
			if (lastname[1] == undefined) {
				iggy2 = "";
			}
			else {iggy2 = lastname[1]}
			/////////////////////////
			styleclass3 = "noerror";
			if (email[1] == undefined) {
				iggy3 = "";
			}
			else {iggy3 = email[1]}
			/////////////////////////
			styleclass4 = "noerror";
			if (address[1] == undefined) {
				iggy4 = "";
			}
			else {iggy4 = address[1]}
			//////////////////////////
			styleclass5 = "noerror";
			if (city[1] == undefined) {
				iggy5 = "";
			}
			else {iggy5 = city[1]}
			////////////////////////////
			styleclass6 = "noerror";
			if (state[1] == undefined) {
				iggy6 = "";
			}
			else {iggy6 = state[1]}
			/////////////////////////////
			styleclass7 = "noerror"
			if (zip[1] == undefined) {
				iggy7 = "";
			}
			else {iggy7 = zip[1]}
			////////////////////////////
			styleclass8 = "noerror"
			if (phone[1] == undefined) {
				iggy8 = "";
			}
			else {iggy8 = phone[1]}
	}
	
	document.writeln("<table width=\"420\" border=\"0\" cellspacing=\"3\" id=\"frmtbl\">")
	
	document.writeln("<tr>")        
	document.writeln("<td width=\"113\"><span class=\"error\">*</span>&nbsp;<span class=\"" + styleclass + "\">First Name</span></td>")
	document.writeln("<td colspan=\"2\"><input name=\"txtFirstName\" type=\"text\" id=\"txtFirstName\" size=\"30\" maxlength=\"20\" value=\"" + iggy1 + "\" /></td>")
	document.writeln("</tr>")
	
	document.writeln("<tr>")
	document.writeln("<td><span class=\"error\">*</span>&nbsp;<span class=\"" + styleclass2 + "\">Last Name</span></td>")
	document.writeln("<td colspan=\"2\"><input name=\"txtLastName\" type=\"text\" id=\"txtLastName\" size=\"30\" maxlength=\"20\" value=\"" +  iggy2 + "\" />")
    document.writeln("</td>")
	document.writeln("</tr>")
	
	document.writeln("<tr>")
	document.writeln("<td><span class=\"error\">*</span>&nbsp;<span class=\"" + styleclass3 + "\">E-mail</span></td>")
	document.writeln("<td colspan=\"2\"><input name=\"txtEmail\" type=\"text\" id=\"txtEmail\" size=\"30\" maxlength=\"50\" value=\"" + iggy3 + "\" />")
	document.writeln("</td>")
	document.writeln("</tr>")
	
	document.writeln("<tr>")
	document.writeln("<td><span class=\"error\">*</span>&nbsp;<span class=\"" + styleclass4 + "\">Address</span></td>")
	document.writeln("<td colspan=\"2\"><input name=\"txtAddress\" type=\"text\" id=\"txtAddress\" size=\"30\" maxlength=\"50\" value=\"" + iggy4 + "\" /></td>")
	document.writeln("</tr>")
	
	document.writeln("<tr>")
	document.writeln("<td><span class=\"error\">*</span>&nbsp;<span class=\"" + styleclass5 + "\">City</span></td>")
	document.writeln("<td colspan=\"2\"><input name=\"txtCity\" type=\"text\" id=\"txtCity\" size=\"30\" maxlength=\"30\" value=\"" + iggy5 + "\" /></td>")
	document.writeln("</tr>")
	
	document.writeln("<tr>")
	document.writeln("<td><span class=\"error\">*</span>&nbsp;<span class=\"" + styleclass6 + "\">State</span></td>")
	document.writeln("<td colspan=\"2\"><input name=\"txtState\" type=\"text\" id=\"txtState\" size=\"4\" maxlength=\"2\" value=\"" + iggy6 + "\" /></td>")
	document.writeln("</tr>")
	
	document.writeln("<tr>")
    document.writeln("<td><span class=\"error\">*</span>&nbsp;<span class=\"" + styleclass7 + "\">Zip Code</span></td>")
	document.writeln("<td colspan=\"2\"><input name=\"txtZip\" type=\"text\" id=\"txtZip\" size=\"5\" maxlength=\"5\" value=\"" + iggy7 + "\" /></td>")
    document.writeln("</tr>")
	
	document.writeln("<tr>")
    document.writeln("<td><span class=\"error\">*</span>&nbsp;<span class=\"" + styleclass8 + "\">Telephone</span></td>")
    document.writeln("<td colspan=\"2\"><input name=\"txtPhone\" type=\"text\" id=\"txtPhone\" size=\"30\" maxlength=\"30\" value=\"" + iggy8 + "\" /></td>")
    document.writeln("</tr>")
}