var gValidForm = true;
var gShowErrorHeader = false;
var mEmailFlag = false;
var gFocusElement = "";
var aFutureInvoice = "";
var acontactType = "";
var aunitType = "";

$(document).ready(function() {
	$("#webForm").find(".required").each(function() {
		$(this).change(function() {
			fieldCheck(this.id);
			
		});
	});
});

/////////////////////////////////////////////////////////////////////////////////////////////
function checkElement(fieldName, message, fieldCondition) {
	//if(gValidForm == true) {
		if(document.getElementById(fieldName).value == fieldCondition) {
			//alert(message);
			highlightError(fieldName, message, fieldCondition);
		}
		else {
			removeError(fieldName);
		}
	//}
}

/////////////////////////////////////////////////////////////////////////////////////////////
function checkScriptValue(mVariable, mValue, fieldName, message, fieldCondition, level) {
	if(mVariable == mValue) {
		highlightError(fieldName, message, fieldCondition, level);
	}
	else {
		removeError(fieldName);
	}
}

/////////////////////////////////////////////////////////////////////////////////////////////
function showErrorHeader() {
	$("#formIntro").after("<div class=\"errorheader\"><h2>There was an error with your form</h2><p>Please correct the highlighted fields and submit this form again.</p></div>");
}

/////////////////////////////////////////////////////////////////////////////////////////////
function highlightError(fieldName, message, fieldCondition,level) {
	var findError = false;
	$("#"+fieldName).parent().parent().addClass("error");
	$("#"+fieldName).parent().parent().find("p").each(function() {
		findError = true;
	});
	if(findError == false) {
		if(level == 1) {
			$("#"+fieldName).parent().next().after("<p class=\"error\">" + message + "</p>");
		}
		else if(level == 2) {
			$("#"+fieldName).parent().next().next().after("<p class=\"error\">" + message + "</p>");
		}
		else {
			$("#"+fieldName).parent().after("<p class=\"error\">" + message + "</p>");
		}
	}
	if(gFocusElement == "") {
		gFocusElement = "document.getElementById(\"" + fieldName + "\").focus()";
	}
	gValidForm = false;
}

/////////////////////////////////////////////////////////////////////////////////////////////
function removeError(fieldName) {
	$("#"+fieldName).parent().parent().removeClass("error");
	$("#"+fieldName).parent().parent().find("p").each(function() {
		$(this).remove();
	});
}


/////////////////////////////////////////////////////////////////////////////////////////////
function checkForm() {
	gValidForm = true;
	gFocusElement = "";
	
	fieldCheck("checkForm");
	//checkElement("quoteCountry", "Please select your country.", "");
	
	if(gValidForm == true) {
		document.getElementById("theunitType").value = aunitType;
		document.getElementById("thecontactType").value = acontactType;
		document.getElementById("theFutureInvoice").value = aFutureInvoice;
		if(document.getElementById("optIn").checked) {
			document.getElementById("OptInCheck").value = "Y";
		}
		if(document.getElementById("formSubmitBtnFlag").value == "N") {
			document.getElementById("requestQuoteBtn").disabled = true;
			document.getElementById("formSubmitBtnFlag").value = "Y";
			document.getElementById("webForm").submit();
		}
	}
	else {
		if(gShowErrorHeader == false) {
			showErrorHeader();
		}
		gShowErrorHeader = true;
		eval(gFocusElement);
		return false;
	}
}

/////////////////////////////////////////////////////////////////////////////////////////////
function fieldCheck(fieldName) {
	if(fieldName.indexOf("_")!=-1) {
		fieldName = fieldName.substring(0,fieldName.indexOf("_"));
	}
	
	if(fieldName=="firstName" || fieldName=="checkForm") {
		checkElement("firstName", "Please enter your first name.", "");
	}
	if(fieldName=="lastName" || fieldName=="checkForm") {
		checkElement("lastName", "Please enter your last name.", "");
	}
	if(fieldName=="companyName" || fieldName=="checkForm") {
		checkElement("companyName", "Please enter your company name.", "");
	}
	if(fieldName=="emailAddress" || fieldName=="checkForm") {
		checkElement("emailAddress", "Please enter your email address.", "");
	}
	if(fieldName=="phoneNumber" || fieldName=="checkForm") {
		checkElement("phoneNumber", "Please enter your phone number.", "");
	}
	if(fieldName=="quoteAddress1" || fieldName=="checkForm") {
		checkElement("quoteAddress1", "Please enter your company address.", "");
	}
	if(fieldName=="quoteCity" || fieldName=="checkForm") {
		checkElement("quoteCity", "Please enter your city.", "");
	}
	if(fieldName=="quoteState" || fieldName=="checkForm") {
		checkElement("quoteState", "Please select your state.", "");
	}
	if(fieldName=="quoteZip" || fieldName=="checkForm") {
		checkElement("quoteZip", "Please enter your zip code.", "");
	}
	if(fieldName=="unitType" || fieldName=="checkForm") {
		aunitType = "";
		for(var i=0;i<document.getElementById("webForm").unitType.length;i++) {
			if(document.getElementById("webForm").unitType[i].checked == true) {
				aunitType = document.getElementById("webForm").unitType[i].value;
			}
		}
		
		checkScriptValue(aunitType,"","unitType_1","Please select the type of unit you have.","");
	}
	if(fieldName=="contactType" || fieldName=="checkForm") {
		acontactType = "";
		for(var i=1;i<=3;i++) {
			if($("#contactType_"+i)[0].checked == true) {
				if(acontactType == "") {
					acontactType += $("#contactType_"+i)[0].value;
				}
				else {
					acontactType += ", " + $("#contactType_"+i)[0].value;
				}
			}
		}
		
		checkScriptValue(acontactType,"","contactType_1","Please select how you would you like to receive your invoice.","");
	}
	if(fieldName=="futureInvoice" || fieldName=="checkForm") {
		aFutureInvoice = "";
		for(var i=1;i<=3;i++) {
			if($("#futureInvoice_"+i)[0].checked == true) {
				if(aFutureInvoice == "") {
					aFutureInvoice += $("#futureInvoice_"+i)[0].value;
				}
				else {
					aFutureInvoice += ", " + $("#futureInvoice_"+i)[0].value;
				}
				if($("#futureInvoice_"+i)[0].value == "Email") {
					mEmailFlag = true;
				}
			}
		}
		
		checkScriptValue(aFutureInvoice,"","futureInvoice_1","Please select how would you like to receive all future invoices.","");
	}
	if((fieldName=="futureEmailAddress" || fieldName=="checkForm") && mEmailFlag == true) {
		document.getElementById("futureEmailField").style.display = "";
		document.getElementById("theFutureInvoiceFlag").value = "Yes";
		checkElement("futureEmailAddress", "Please enter the email address future invoices should be sent.", "");
	}
}

/////////////////////////////////////////////////////////////////////////////////////////////
function showFutureInvoiceField(objField) {
	if(objField.checked == true) {
		document.getElementById("futureEmailField").style.display = "";
	}
	else {
		document.getElementById("futureEmailField").style.display = "none";
	}
}