var gValidForm = true;
var gShowErrorHeader = false;
var gFocusElement = "";
var aIndustry = "";
var alengthNeeded = "";
var atimeFrame = "";
var aproductChoices = "";
var aFeithCategoryName = "";
var aSP_Flag = "N";
var aTP_Flag = "N";
var aOther_Flag = "N";

$(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("theproductChoice").value = aproductChoices;
		document.getElementById("theFeithCatName").value = aFeithCategoryName;
		document.getElementById("thetimeFrame").value = atimeFrame;
		document.getElementById("theIndustry").value = aIndustry;
		document.getElementById("theLengthNeeded").value = alengthNeeded;
		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=="productChoice" || fieldName=="checkForm") {
		aproductChoices = "";
		aFeithCategoryName = "";
		aSP_Flag = "N";
		aTP_Flag = "N";
		aOther_Flag = "N";
		for(var i=1;i<=7;i++) {
			if($("#productChoice_"+i)[0].checked == true) {
				var currProdChoice = $("#productChoice_"+i)[0].value;
				if(currProdChoice == "Mobile Offices" || currProdChoice == "Classrooms"  || currProdChoice == "Container Offices") {
					aTP_Flag = "Y";
				}
				else if(currProdChoice == "Storage Containers" || currProdChoice == "$1.99 Special Offer Storage Promotion") {
					aSP_Flag = "Y"
				}
				else {
					aOther_Flag = "Y";
				}
				
				if(aproductChoices == "") {
					aproductChoices += currProdChoice;
				}
				else {
					aproductChoices += ", " + currProdChoice;
				}
			}
		}
		
		if(aTP_Flag == "Y" && aSP_Flag == "N" && aOther_Flag == "N") {
			aFeithCategoryName = "TRADITIONAL_PRODUCT";
		}
		else if(aTP_Flag == "N" && aSP_Flag == "Y" && aOther_Flag == "N") {
			aFeithCategoryName = "STORAGE_PRODUCT";
		}
		else {
			aFeithCategoryName = "OTHER";
		}
		
		checkScriptValue(aproductChoices,"","productChoice_1","Please select your product choice.","");
	}
	/*if(fieldName=="unitLocationCity" || fieldName=="unitLocationZip" || fieldName=="checkForm") {
		if(document.getElementById("unitLocationCity").value == "" && document.getElementById("unitLocationZip").value == "") {
			locationVar = "";
		}
		else {
			locationVar = "valueEntered";
		}
		checkScriptValue(locationVar,"","unitLocationCity","Please enter the city or state the unit will be located in.","");
	}*/
	if(fieldName=="unitLocationZip" || fieldName=="checkForm") {
		checkElement("unitLocationZip","Please enter the zip code the unit will be located in.","");
	}
	if(fieldName=="timeFrame" || fieldName=="checkForm") {
		atimeFrame = "";
		for(var i=0;i<document.getElementById("webForm").timeFrame.length;i++) {
			if(document.getElementById("webForm").timeFrame[i].checked == true) {
				atimeFrame = document.getElementById("webForm").timeFrame[i].value;
			}
		}
		
		checkScriptValue(atimeFrame,"","timeFrame_1","Please select when you need your WS Product(s).","",1);
	}
	if(fieldName=="lengthNeeded" || fieldName=="checkForm") {
		alengthNeeded = "";
		for(var i=0;i<document.getElementById("webForm").lengthNeeded.length;i++) {
			if(document.getElementById("webForm").lengthNeeded[i].checked == true) {
				alengthNeeded = document.getElementById("webForm").lengthNeeded[i].value;
			}
		}
		
		checkScriptValue(alengthNeeded,"","lengthNeeded_1","Please select how long you plan on needing your WS Product(s).","",1);
	}
	if(fieldName=="industry" || fieldName=="checkForm") {
		aIndustry = "";
		for(var i=1;i<=7;i++) {
			if($("#industry_"+i)[0].checked == true) {
				if(aIndustry == "") {
					aIndustry += $("#industry_"+i)[0].value;
				}
				else {
					aIndustry += ", " + $("#industry_"+i)[0].value;
				}
			}
		}
		
		checkScriptValue(aIndustry,"","industry_1","Please select your industry.","");
	}
}

/////////////////////////////////////////////////////////////////////////////////////////////
function removeFromQuoteRequest(objCurrProd) {
	$("#productResultDiv_" + objCurrProd).remove();
}