
/*

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

jQuery(function($) {
	var dimensionsUnit = $(".RadioGroup.DimensionsUnit input[type='radio']:checked").val();
	if (dimensionsUnit) {
		$(".DataTable.Dimensions td").append('<span class="FldNote">' + dimensionsUnit + '</span>');	
	}
	
	$(".RadioGroup.DimensionsUnit input[type='radio']").click(function(evt) {
		var dimensionsUnit = $(this).val();
		$(".DataTable.Dimensions .FldNote").text(dimensionsUnit);
	});
	
	var rateQuoteForm = $("#RateQuoteForm");
	if (rateQuoteForm.length) {
		
		// parse error message; adds click event handlers
		ParseErrorMsg();
		
		$("#ClearBtn").click(function(evt) {
			// prevent form from submitting
			evt.preventDefault();
			// clear button focus
			$(this).blur();
			// reset the form
			ClearForm(rateQuoteForm);
			// error message clean-up
			ClearErrors();
			// scroll to top of page
			ScrollToElm("#PageTop");
			// set defaults
			$(".RadioGroup.DimensionsUnit input[type='radio']:first").trigger('click');
			$("#PackageWeightUnitFld").get(0).selectedIndex = 0;
			$("#ShipmentWeightUnitFld").get(0).selectedIndex = 0;
		});
		
		rateQuoteForm.submit(function(evt) {

			var errorMsg = "";
	
			var errorPrefix = '<span class="ErrorPrefix">Error: </span>';
			
			var customerName = jQuery.trim($("#CustomerNameFld").val());
			var customerCompany = jQuery.trim($("#CustomerCompanyFld").val());
			var customerAddress = jQuery.trim($("#CustomerAddressFld").val());
			var customerCity = jQuery.trim($("#CustomerCityFld").val());
			var customerState = jQuery.trim($("#CustomerStateFld").val());
			var customerZip = jQuery.trim($("#CustomerZipFld").val());
			var customerCountry = jQuery.trim($("#CustomerCountryFld").val());
			var customerPhone = jQuery.trim($("#CustomerPhoneFld").val());
			var customerFax = jQuery.trim($("#CustomerFaxFld").val());
			var customerEmail = jQuery.trim($("#CustomerEmailFld").val());
			var originCompany = jQuery.trim($("#OriginCompanyFld").val());
			var originAddress = jQuery.trim($("#OriginAddressFld").val());
			var originCity = jQuery.trim($("#OriginCityFld").val());
			var originState = jQuery.trim($("#OriginStateFld").val());
			var originCountry = jQuery.trim($("#OriginCountryFld").val());
			var destinationCompany = jQuery.trim($("#DestinationCompanyFld").val());
			var destinationAddress = jQuery.trim($("#DestinationAddressFld").val());
			var destinationCity = jQuery.trim($("#DestinationCityFld").val());
			var destinationState = jQuery.trim($("#DestinationStateFld").val());
			var destinationCountry = jQuery.trim($("#DestinationCountryFld").val());
			var shipmentCommodity = jQuery.trim($("#ShipmentCommodityFld").val());
			var totalPackages = jQuery.trim($("#TotalPackagesFld").val());
			var packageType = jQuery.trim($("#PackageTypeFld").val());
			var shipmentWeight = jQuery.trim($("#ShipmentWeightFld").val());
			
			var cost = new Array;
			for (var i = 1; i <= 5; i++) {
				cost[i] = $(".Cost" + i + " input[type='radio']:checked").val();
			}
			
			var hazardousCargo = $(".RadioGroup.HazardousCargo input[type='radio']:checked").val();
			
			// error message clean-up
			ClearErrors();
			
			if (customerName == "") {
				$("label.CustomerName").addClass("Error").prepend(errorPrefix);
				errorMsg += '<li><a href="#CustomerNameFld">Your Name</a>: Your name is required.</li>\n';
			}
			
			if (customerCompany == "") {
				$("label.CustomerCompany").addClass("Error").prepend(errorPrefix);
				errorMsg += '<li><a href="#CustomerCompanyFld">Company Name</a>: Your company name is required.</li>\n';
			}
			
			if (customerAddress == "") {
				$("label.CustomerAddress").addClass("Error").prepend(errorPrefix);
				errorMsg += '<li><a href="#CustomerAddressFld">Address</a>: Your address is required.</li>\n';
			}
			
			if (customerCity == "") {
				$("label.CustomerCity").addClass("Error").prepend(errorPrefix);
				errorMsg += '<li><a href="#CustomerCityFld">City</a>: Your city is required.</li>\n';
			}
			
			if (customerState == "") {
				$("label.CustomerState").addClass("Error").prepend(errorPrefix);
				errorMsg += '<li><a href="#CustomerStateFld">State</a>: Your state is required.</li>\n';
			}
			
			if (customerZip == "") {
				$("label.CustomerZip").addClass("Error").prepend(errorPrefix);
				errorMsg += '<li><a href="#CustomerZipFld">Postal Code</a>: Your postal code is required.</li>\n';
			}
			
			if (customerCountry == "") {
				$("label.CustomerCountry").addClass("Error").prepend(errorPrefix);
				errorMsg += '<li><a href="#CustomerCountryFld">Country</a>: Your country is required.</li>\n';
			}
			
			if (customerPhone == "") {
				$("label.CustomerPhone").addClass("Error").prepend(errorPrefix);
				errorMsg += '<li><a href="#CustomerPhoneFld">Telephone Number</a>: Your telephone is required.</li>\n';
			}
			
			if (customerFax == "") {
				$("label.CustomerFax").addClass("Error").prepend(errorPrefix);
				errorMsg += '<li><a href="#CustomerFaxFld">Fax Number</a>: Your fax number is required.</li>\n';
			}
			
			if (customerEmail == "") {
				$("label.CustomerEmail").addClass("Error").prepend(errorPrefix);
				errorMsg += '<li><a href="#CustomerEmailFld">Email Address</a>: Your email address is required.</li>\n';
			}
			else if (isBadEmail(customerEmail)) {
				$("label.CustomerEmail").addClass("Error").prepend(errorPrefix);
				errorMsg += '<li><a href="#CustomerEmailFld">Email Address</a>: Your email address is not a valid format.</li>\n';
			}
			
			
			if (originCompany == "") {
				$("label.OriginCompany").addClass("Error").prepend(errorPrefix);
				errorMsg += '<li><a href="#OriginCompanyFld">Company Name</a>: The origin location\'s company name is required.</li>\n';
			}
			
			if (originAddress == "") {
				$("label.OriginAddress").addClass("Error").prepend(errorPrefix);
				errorMsg += '<li><a href="#OriginAddressFld">Address</a>: The origin location\'s address is required.</li>\n';
			}
			
			if (originCity == "") {
				$("label.OriginCity").addClass("Error").prepend(errorPrefix);
				errorMsg += '<li><a href="#OriginCityFld">City</a>: The origin location\'s city is required.</li>\n';
			}
			
			if (originState == "") {
				$("label.OriginState").addClass("Error").prepend(errorPrefix);
				errorMsg += '<li><a href="#OriginStateFld">State</a>: The origin location\'s state is required.</li>\n';
			}
			
			if (originCountry == "") {
				$("label.OriginCountry").addClass("Error").prepend(errorPrefix);
				errorMsg += '<li><a href="#OriginCountryFld">Country</a>: The origin location\'s country is required.</li>\n';
			}
			
			
			if (destinationCompany == "") {
				$("label.DestinationCompany").addClass("Error").prepend(errorPrefix);
				errorMsg += '<li><a href="#DestinationCompanyFld">Company Name</a>: The destination location\'s company name is required.</li>\n';
			}
			
			if (destinationAddress == "") {
				$("label.DestinationAddress").addClass("Error").prepend(errorPrefix);
				errorMsg += '<li><a href="#DestinationAddressFld">Address</a>: The destination location\'s address is required.</li>\n';
			}
			
			if (destinationCity == "") {
				$("label.DestinationCity").addClass("Error").prepend(errorPrefix);
				errorMsg += '<li><a href="#DestinationCityFld">City</a>: The destination location\'s city is required.</li>\n';
			}
			
			if (destinationState == "") {
				$("label.DestinationState").addClass("Error").prepend(errorPrefix);
				errorMsg += '<li><a href="#DestinationStateFld">State</a>: The destination location\'s state is required.</li>\n';
			}
			
			if (destinationCountry == "") {
				$("label.DestinationCountry").addClass("Error").prepend(errorPrefix);
				errorMsg += '<li><a href="#DestinationCountryFld">Country</a>: The destination location\'s country is required.</li>\n';
			}
			
			
			if (shipmentCommodity == "") {
				$("label.ShipmentCommodity").addClass("Error").prepend(errorPrefix);
				errorMsg += '<li><a href="#ShipmentCommodityFld">Commodity</a>: The shipment commodity is required.</li>\n';
			}
			
			if (!hazardousCargo) {
				$("label.HazardousCargo").addClass("Error").prepend(errorPrefix);
				errorMsg += '<li><a href="#ShipmentDetailsFldSet">Is this cargo hazardous/dangerous?</a>: ' +
					'Please check "Yes" if the cargo is hazardous/dangerous or "No" if the cargo is not hazardous/dangerous.</li>\n';
			}
			
			if (totalPackages == "") {
				$("label.TotalPackages").addClass("Error").prepend(errorPrefix);
				errorMsg += '<li><a href="#TotalPackagesFld">Total Number of Packages</a>: The total number of packages is required.</li>\n';
			}
			
			if (packageType == "") {
				$("label.PackageType").addClass("Error").prepend(errorPrefix);
				errorMsg += '<li><a href="#PackageTypeFld">Package Type</a>: The package type is required.</li>\n';
			}
			
			if (shipmentWeight == "") {
				$("label.ShipmentWeight").addClass("Error").prepend(errorPrefix);
				errorMsg += '<li><a href="#ShipmentWeightFld">Total Shipment Weight</a>: The total shipment weight is required.</li>\n';
			}
			
			for (var i = 1; i <= 5; i++) {
				if (!cost[i]) {
					var costDesc = $(".Cost" + i + " .FirstCol");
					var id = ($(".Cost" + i + " input[type=radio]:first").attr("id"));
					errorMsg += '<li><a href="#' + id + '">Terms of Shipment</a>: Please select the party responsible for ' + 
						jQuery.trim(costDesc.text().toLowerCase()) + '.</li>\n';
					costDesc.addClass("Error").prepend(errorPrefix);
				}
			}
			
			if (errorMsg != "") {
				// prevent the form from submitting
				evt.preventDefault();
				// 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");
			}
		});
	}
});
