var gValidForm = true;
var gShowErrorHeader = false;
var gFocusElement = "";
var aIndustry = "";
var abuildingType = "";
var aleaseType = "";
var abuildingSize = "";
var aservices = "";

$(document).ready(function() {
	$("#webForm").find(".required").each(function() {
		$(this).change(function() {
			fieldCheck(this.name);
			
		});
	});
});

/////////////////////////////////////////////////////////////////////////////////////////////
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("theBuildingType").value = abuildingType;
		document.getElementById("theLeaseType").value = aleaseType;
		document.getElementById("theBuildingSize").value = abuildingSize;
		document.getElementById("theServices").value = aservices;
		document.getElementById("theIndustry").value = aIndustry;
		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=="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=="buildingType" || fieldName=="checkForm") {
		abuildingType = "";
		for(var i=0;i<document.getElementById("webForm").buildingType.length;i++) {
			if(document.getElementById("webForm").buildingType[i].checked == true) {
				abuildingType = document.getElementById("webForm").buildingType[i].value;
			}
		}
		
		checkScriptValue(abuildingType,"","bt_temp","Please select if the building(s) will be for temporary of permanent use.","");
	}
	if(fieldName=="leaseType" || fieldName=="checkForm") {
		aleaseType = "";
		for(var i=0;i<document.getElementById("webForm").leaseType.length;i++) {
			if(document.getElementById("webForm").leaseType[i].checked == true) {
				aleaseType = document.getElementById("webForm").leaseType[i].value;
			}
		}
		
		checkScriptValue(aleaseType,"","lt_lease","Please select if the building(s) will be for sales or lease.","");
	}
	if(fieldName=="buildingSize" || fieldName=="checkForm") {
		abuildingSize = "";
		for(var i=0;i<document.getElementById("webForm").buildingSize.length;i++) {
			if(document.getElementById("webForm").buildingSize[i].checked == true) {
				abuildingSize = document.getElementById("webForm").buildingSize[i].value;
			}
		}
		
		checkScriptValue(abuildingSize,"","bs_single","Please select the size building(s) you are interested in.","");
	}
	if(fieldName=="services" || fieldName=="checkForm") {
		aservices = "";
		for(var i=0;i<document.getElementById("webForm").services.length;i++) {
			if(document.getElementById("webForm").services[i].checked == true) {
				aservices = document.getElementById("webForm").services[i].value;
			}
		}
		
		checkScriptValue(aservices,"","s_yes","Please select the size building(s) you are interested in.","");
	}
	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.","");
	}
}