
/*

	air freight rate quote javascript functions
	
	dependencies: 
		jQuery
		global.js > ParseErrorMsg()
		global.js > ScrollToElm()
	
*/

jQuery(function($) {
		
	// parse error message; adds click event handlers
	ParseErrorMsg();

	$("#NextBtn").click(function(evt) {
		if (!ValidateRateQuote()) {
			// prevent the form from submitting
			evt.preventDefault();	
		}
	});

	$("#ProgressBar .ProgressBtn").click(function(evt) {
		var step = $("#RateQuoteForm input[name='step']").val();
		// validate the form if the button's step is greater than this step or the next step is already active
		if (parseInt($(this).val()) > step || $("#RateQuoteForm input[name='step" + (Number(step) + 1) + "_active']").val() == "Y") {
			if (!ValidateRateQuote()) {
				// prevent the form from submitting
				evt.preventDefault();
			}
		}
	});
	
	$(".TerritoryUnitDividerCol").hide();
	jQuery.each(["Origin","Destination"], function() {
		var prefix = this;
		$("#" + prefix + "CountryFld").change(toggleState);
	});
	
	toggleState();
});

function toggleState() {
	jQuery.each(["Origin","Destination"], function() {
		var prefix = this;
		var country = $("#" + prefix + "CountryFld").val();
		if (country == "US") {
			$("#" + prefix + "Provinces").hide();
			$("#" + prefix + "ProvinceFld").attr("disabled","disabled").val("");
			$("#" + prefix + "States").show();
			$("#" + prefix + "StateFld").removeAttr("disabled");
		}
		else if (country == "CA") {
			$("#" + prefix + "States").hide();
			$("#" + prefix + "StateFld").attr("disabled","disabled").val("");
			$("#" + prefix + "Provinces").show();
			$("#" + prefix + "ProvinceFld").removeAttr("disabled");
		}
		else {
			$("#" + prefix + "Provinces").hide();
			$("#" + prefix + "ProvinceFld").attr("disabled","disabled").val("");
			$("#" + prefix + "States").hide();
			$("#" + prefix + "StateFld").attr("disabled","disabled").val("");
		}
	});
}

function ValidateRateQuote() {

	var errorMsg = "";
	var errorPrefix = '<span class="ErrorPrefix">Error: </span>';
			
	// error message clean-up
	ClearErrors();

	jQuery.each(["Origin","Destination"], function() {
		var prefix = this;
		
		var country = jQuery.trim($("#" + prefix + "CountryFld").val());
		var company = jQuery.trim($("#" + prefix + "CompanyFld").val());
		var city = jQuery.trim($("#" + prefix + "CityFld").val());
		var state = jQuery.trim($("#" + prefix + "StateFld").val());
		var province = jQuery.trim($("#" + prefix + "ProvinceFld").val());
		var zip = jQuery.trim($("#" + prefix + "ZipFld").val());
		
		if (country == "") {
			$("label." + prefix + "Country").addClass("Error").prepend(errorPrefix);
			errorMsg += '<li><a href="#' + prefix + 'CountryFld">Country</a>: The ' + prefix.toLowerCase() + ' location\'s country is required.</li>\n';
		}
		
		if (company == "") {
			$("label." + prefix + "Company").addClass("Error").prepend(errorPrefix);
			errorMsg += '<li><a href="#' + prefix + 'CompanyFld">Company Name</a>: The ' + prefix.toLowerCase() + ' location\'s company name is required.</li>\n';
		}
		
		if (city == "") {
			$("label." + prefix + "City").addClass("Error").prepend(errorPrefix);
			errorMsg += '<li><a href="#' + prefix + 'CityFld">City</a>: The ' + prefix.toLowerCase() + ' location\'s city is required.</li>\n';
		}
		
		if (country == "US" && state == "") {
			$("label." + prefix + "State").addClass("Error").prepend(errorPrefix);
			errorMsg += '<li><a href="#' + prefix + 'StateFld">State</a>: The ' + prefix.toLowerCase() + ' location\'s state is required.</li>\n';
		}
		else if (country == "CA" && province == "") {
			$("label." + prefix + "Province").addClass("Error").prepend(errorPrefix);
			errorMsg += '<li><a href="#' + prefix + 'ProvinceFld">Province</a>: The ' + prefix.toLowerCase() + ' location\'s province is required.</li>\n';
		}
		
		
		if (zip == "") {
			$("label." + prefix + "Zip").addClass("Error").prepend(errorPrefix);
			errorMsg += '<li><a href="#' + prefix + 'ZipFld">Zip/Postal Code</a>: The ' + prefix.toLowerCase() + ' location\'s zip/postal code is required.\n' +
				'If the country you are shipping ' + (prefix.toLowerCase() == "origin" ? "from" : "to") + 
				' does NOT have zip/postal codes, please enter \"N/A\" for the Zip/Postal Code.</li>\n';
		}
		else if (zip.toUpperCase() != "N/A") {
			if (isBadZip(zip)) {
				$("label." + prefix + "Zip").addClass("Error").prepend(errorPrefix);
				errorMsg += '<li><a href="#' + prefix + 'ZipFld">Zip/Postal Code</a>: The ' + prefix.toLowerCase() + ' location\'s zip/postal code is an invalid format. ' +
					'Please enter a zip/postal code using a combination of numbers, letters, hyphens and spaces.</li>\n';
			}
		}
	});
			
	if (errorMsg != "") {
		// insert error message
		errorMsg = '<h2 class="Error">Please check the following fields for missing or invalid input:</h2>\n' +
			'<ul class="ErrorMsg">' + errorMsg + '</ul>\n';
		$("#ErrorMsg").append(errorMsg);
		// parse error message; adds click event handlers
		ParseErrorMsg();
		// scroll to top of page
		ScrollToElm("#RateQuoteForm");
		// validation failed
		return false;
	}
	
	return true;
}
