// General function for pop-up window
var newWindow = null
	function makeNewWindow(x,y) {
		newWindow =  window.open(x,y,"status=1,menubar=0,toolbar=0,scrollbars=1,resizable=0,width=600,height=450")
		newWindow.focus()
}

// General function that returns false if "fieldname" is empty.
function checkfield(fieldname) {
	var fieldval = (fieldname.value.length == 0) ? false: true;
	return fieldval;
}

// General function that returns false if "Phone" contains "x".
function checkphonefield(fieldname) {
	var phonefieldval = (fieldname.value.indexOf("x",0) > -1) ? false: true;
	return phonefieldval;
}

// General function that returns false if "Fax" contains "x".
function checkfaxfield(fieldname) {
	var faxfieldval = (fieldname.value.indexOf("x",0) > -1) ? false: true;
	return faxfieldval;
}

// Function that checks for valid E-mail. Returns false if not valid.
function checkemail(fieldname) {
	if (!checkfield(fieldname)) {
		return false;
	}

	invalidchars = " /:,;"
	for (count = 0; count < invalidchars.length; count++) {
		badchar = invalidchars.charAt(count);
		if (fieldname.value.indexOf(badchar,0) > -1) {
			return false;
		}
	}

	atpos = fieldname.value.indexOf("@", 1);
	if (atpos == -1) {
		return false;
	}
	if (fieldname.value.indexOf("@", atpos + 1) > -1) {
		return false;
	}

	periodpos = fieldname.value.indexOf(".", atpos);
	if (periodpos == -1) {
		return false;
	}
	if (periodpos + 3 > fieldname.value.length) {
		return false;
	}
	return true;
}

// Function that checks if other billing province or state. Returns false if not valid.
function checkstateother(State, StateOther) {
	if ( State.options[State.selectedIndex].value == "Other") {
		if ( checkfield(StateOther) == false ) {
			return false;
		} else {
			return true;
		}
	} else {
		return true;
	}
	return true;
}

// Function that checks for valid billing province or state. Returns false if not valid.
function checkstate(State) {
	if ( State.options[State.selectedIndex].value == "") {
	return false;
    } else {
        return true;
    }
}

// This checks the free estimate request form. It calls the various functions to check for validity.
function checkestimateform(f) {
	var FirstNameval = checkfield(f.FirstName);
	var LastNameval = checkfield(f.LastName);
	var Address1val = checkfield(f.Address1);
	var Cityval = checkfield(f.City);
	var StateOtherval = checkbillstateother(f.State, f.StateOther);
	var Stateval = checkbillstate(f.State);
	var Zipval = checkfield(f.Zip);
	var Phoneval = checkphonefield(f.Phone);
	var Emailval = checkemail(f.Email);
	
	var formvalid = true;
	var focusfield = "";
	var errormsg = "The following errors were found when attempting to submit your request form:\n\n";
	
	if (!Emailval) {
		formvalid = false;
		errormsg = errormsg + '  - Billing E-mail Address must be filled in.\n';
		focusfield = f.Email;
	}
	
	if (!Phoneval) {
		formvalid = false;
		errormsg = errormsg + '  - Billing Phone Number must be filled in.\n';
		focusfield = f.Phone;
	}

	if (!Zipval) {
		formvalid = false;
		errormsg = errormsg + '  - Billing Postal or Zip Code must be filled in.\n';
		focusfield = f.Zip;
	}

	if (!Stateval) {
		formvalid = false;
		errormsg = errormsg + '  - Billing Province or State must be selected.\n';
		focusfield = f.State;
	}
	
	if (!StateOtherval) {
		formvalid = false;
		errormsg = errormsg + '  - Other Billing Province or State must be filled in if other.\n';
		focusfield = f.StateOther;
	}
	
	if (!Cityval) {
		formvalid = false;
		errormsg = errormsg + '  - Billing City must be filled in.\n';
		focusfield = f.City;
	}

	if (!Address1val) {
		formvalid = false;
		errormsg = errormsg + '  - Billing Street Address must be filled in.\n';
		focusfield = f.Address1;
	}

	if (!LastNameval) {
		formvalid = false;
		errormsg = errormsg + '  - Billing Last Name must be filled in.\n';
		focusfield = f.LastName;
	}

	if (!FirstNameval) {
		formvalid = false;
		errormsg = errormsg + '  - Billing First Name must be filled in.\n';
		focusfield = f.FirstName;
	}
	
	if ( formvalid ) {
		return true;
	} else {
		alert(errormsg);
		focusfield.focus();
		return false;
	}
}
