
var BOOKING_URL = "/ferry-services/routes/request-a-booking/" ;
var REQUEST_QUOTATION_URL = "/ferry-services/routes/request-a-quote/" ;


jQuery(document).ready(function($) 
{
	
	setFormFromVars();
	
	jQuery("select#departLocation").change(function()
	{
		doSelectFilter();
	});	
	
	setupRadios();

	jQuery('#booking-form, #quote-form').submit(function(e)
	{
		if(validateForm(this))
		{
			
			return true;
		}else
		{
			return false;	
		}
		
	});

});
/* Manually handle the validation */
function validateForm(form)
{
	var valid = true;
	var errorMsg = "<ul>";
	
	if(jQuery(form).attr('id')=="booking-form")
	{
		if(jQuery('#customer_account_no').val() =="")
		{
			errorMsg += "<li>Please enter a valid Customer account number</li>";
			valid = false;	
		}
		if(jQuery('#email_address').val() =="")
		{
			errorMsg += "<li>Please enter a valid email address</li>";
			valid = false;	
		}		
		
	}else if(jQuery(form).attr('id')=="quote-form")
	{
		if(jQuery('#txt_CoName').val() =="")
		{
			errorMsg += "<li>Please enter a Company name</li>";
			valid = false;	
		}
		if(jQuery('#txt_Contact').val() =="")
		{
			errorMsg += "<li>Please enter a Contact name</li>";
			valid = false;	
		}		
		if(jQuery('#txt_Address').val() =="")
		{
			errorMsg += "<li>Please enter an address</li>";
			valid = false;	
		}	
		if(jQuery('#txt_Phone').val() =="")
		{
			errorMsg += "<li>Please enter a Telephone number</li>";
			valid = false;	
		}									
		//console.log("Quote form validation:"+valid);			
	}
	
	errorMsg += "</ul>";
	jQuery(".error").html(errorMsg)
	return valid;
	
}

function setFormFromVars()
{
	
	if(retrieveGetVars()['departLocation'])
	{
		var theValue = retrieveGetVars()['departLocation'];
		jQuery("select#departLocation").val( theValue ).attr('selected',true);			
	}
	doSelectFilter();
	
	if(retrieveGetVars()['arriveLocation'])
	{
		var theValue = retrieveGetVars()['arriveLocation'];
		jQuery("select#arriveLocation").val( theValue ).attr('selected',true);			
	}	
}

function doSelectFilter()
{
		jQuery.getJSON("/wp-content/themes/harbourTheme/routeslookup.php",{id: jQuery("select#departLocation").val(), ajax: 'true'}, function(j)
		{
			var options = '';
			for (var i = 0; i < j.length; i++) 
			{
				options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
			}
			jQuery("select#arriveLocation").html(options);
		});
	
}

function retrieveGetVars()
{
	var $_GET = {};
	
	document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function () {
		function decode(s) {
			return decodeURIComponent(s.split("+").join(" "));
		}
	
		$_GET[decode(arguments[1])] = decode(arguments[2]);
	});
	
	return $_GET;
}

function SubmitForm(sDepart, sArrive) 
{
	document.location.href ='index.php?Depart=' + sDepart + '&Arrive=' + sArrive
} 

function ResetForm() 
{
	document.UserOpts.reset()
}

function setupRadios()
{
		jQuery("input[name='RadioBtn']").click(
			function()
			{
				if(jQuery(this).attr('id')=="Booking")
				{
					//console.log("Booking");
					jQuery('form#UserOpts').attr('action', BOOKING_URL);
				}else
				{
					jQuery('form#UserOpts').attr('action', REQUEST_QUOTATION_URL);
				}
			}
		);
}


