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) {
		var urlString = "";
		if(document.getElementById("location_zipCode").value != "") {
			urlString = urlString + encodeURIComponent(document.getElementById("location_zipCode").value) + "+";
		}
		if(document.getElementById("cityName").value != "") {
			urlString = encodeURIComponent(document.getElementById("cityName").value) + "+";
		}
		if(document.getElementById("stateName").value != "") {
			urlString = urlString + encodeURIComponent(document.getElementById("stateName").value) + "+";
		}
		
		urlString = urlString.substring(0,urlString.length-1);
		
		if(document.getElementById("stateName").value != "" && document.getElementById("location_zipCode").value == "" && document.getElementById("cityName").value == "") {
			document.getElementById("stateOnlySearch").value = "Y";
		}
		
		document.getElementById("location").value = urlString;
		
		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=="location_zipCode" || fieldName=="cityName" || fieldName=="stateName" ||  fieldName=="checkForm") {
		if(document.getElementById("stateName").value == "" && document.getElementById("location_zipCode").value == "" && document.getElementById("cityName").value == "") {
			checkElement("location_zipCode", "Please enter a zip code, city, or state.  If you would like to see all of Williams Scotsman's locations in the US and Canada please <a href=\"/All-Locations\">click here</a>.", "");
		}
	}
}

/////////////////////////////////////////////////////////////////////////////////////////////
function checkCountryFlag(objValue) {
	if(objValue == "Canada") {
		document.getElementById("state-fields").style.display = "none";
	}
	else {
		document.getElementById("state-fields").style.display = "";
	}
}

/////////////////////////////////////////////////////////////////////////////////////////////
$(function() {	
		/*$("#birds").autocomplete({
			source: "/cgi-bin/act_location-service.cgi?queryStr=Bal",
			minLength: 2,
			select: function(event, ui) {
				log(this.country);
				//log(ui.item ? ("Selected: " + ui.item.value + " aka " + ui.item.id) : "Nothing selected, input was " + this.value);
			}
		});*/
		var cache = {};
		$('#cityName').autocomplete({
			minLength: 2,
			//source: "/cgi-bin/act_location-service.cgi",
			source: function(request, response) {
				if ( request.term in cache ) {
					response( cache[ request.term ] );
					return;
				}
				
				$.ajax({
					url: "/cgi-bin/act_location-service.cgi",
					dataType: "json",
					data: request,
					success: function( data ) {
						cache[ request.term ] = data;
						response( data );
					}
				});
			}
			,
			focus: function(event, ui) {
				$('#cityName').val(ui.item.city + ", " + ui.item.state + ", " + ui.item.country);
				return false;
			},
			select: function(event, ui) {
				$('#cityName').val(ui.item.city + ", " + ui.item.state + ", " + ui.item.country);
				$('#location-country').val(ui.item.country);
				$('#location-city').val(ui.item.city);
				$('#location-state').val(ui.item.state);
				$('#location-zip').val(ui.item.zip);
				
				if(document.getElementById("formSubmitBtnFlag").value == "N") {
					document.getElementById("requestQuoteBtn").disabled = true;
					document.getElementById("formSubmitBtnFlag").value = "Y";
					document.getElementById("webForm").submit();
				}
				
				return false;
			}
		})
		.data( "autocomplete" )._renderItem = function( ul, item ) {
			return $( "<li></li>" )
				.data( "item.autocomplete", item )
				.append( "<a>" + item.city + ", " + item.state + ", " + item.country + "</a>" )
				.appendTo( ul );
		};

	});
