// JavaScript Document
$(document).ready(function(){
	$('#createAccount').change(
		function()
		{
			if($(this)[0].checked)
			{
				$('#createAccountTable:hidden').slideDown('fast');
			}
			else
			{
				$('#createAccountTable:visible').slideUp('fast');
			}
		}
	);
	$('#otherDrivers').change(
		function()
		{
			if($(this)[0].checked)
			{
				$('#otherDriversTable:hidden').slideDown('fast');
			}
			else
			{
				$('#otherDriversTable:visible').slideUp('fast');
			}
		}
	);
	
	$('.phoneArea[@rel]').keyup(
		function(e)
		{
			var theValue = $(this).attr('value');
			var theRel = $(this).attr('rel');
			var key = window.event ? e.keyCode : e.which;
			var keyString = String.fromCharCode(key);
			if((key >= 48 && key <= 57) || (key >= 96 && key <= 105))
			{
				if(theValue.length == 3 && $('.phone1[@rel='+theRel+']').length > 0)
				{
					$('.phone1[@rel='+theRel+']')[0].focus();
				}
			}
			else
			{
				return false;
			}
		}
	);

	$('.phone1[@rel]').keyup(
		function(e)
		{
			var theValue = $(this).attr('value');
			var theRel = $(this).attr('rel');
			var key = window.event ? e.keyCode : e.which;
			var keyString = String.fromCharCode(key);
			if((key >= 48 && key <= 57) || (key >= 96 && key <= 105))
			{
				if(theValue.length == 3 && $('.phone2[@rel='+theRel+']').length > 0)
				{
					$('.phone2[@rel='+theRel+']')[0].focus();
				}
			}
			else
			{
				return false;
			}
		}
	);

	$('.phone2[@rel]').keyup(
		function(e)
		{
			var theValue = $(this).attr('value');
			var theRel = $(this).attr('rel');
			var key = window.event ? e.keyCode : e.which;
			var keyString = String.fromCharCode(key);
			if((key >= 48 && key <= 57) || (key >= 96 && key <= 105))
			{
				if(theValue.length == 4 && $('.phoneExt[@rel='+theRel+']').length > 0)
				{
					$('.phoneExt[@rel='+theRel+']')[0].focus();
				}
			}
			else
			{
				return false;
			}
		}
	);
	
	$('.franchise_mileage').change(
		function(){
			var params = 'franchise_id='+$(this).attr('value');
			jQuery.ajax({
				type: "GET",
				url: 'fleet_get_free_miles.php',
				data: params,
				dataType: 'json',
				success: function(json){
					jQuery('#free_miles').attr('value',json.free_miles);
				},
				error: function()
				{
					jQuery('#free_miles').attr('value','');
						alert('Error: No mileage rate defined for this franchise!');
				}
			});
		}
	);
	
	if ($('form').length > 0)
	{
		// Trap Backspace(8) and Enter(13) - 
		// Except bksp on text/textareas, enter on textarea/submit
		if (typeof window.event != 'undefined') // IE
			document.onkeydown = function() // IE
			{
				var t=event.srcElement.type;
				var kc=event.keyCode;
				return ((kc != 8 && kc != 13) || ( t == 'text' &&  kc != 13 ) || (t == 'textarea') || ( t == 'submit' &&  kc == 13))
			}
		else
		{
			//alert('e');
			document.onkeydown = function(e)  // FireFox/Others 
			{
				var t=e.target.type;
				var kc=e.keyCode;
				//alert(kc);
				if ((kc != 8 && kc != 13) || ( t == 'text' &&  kc != 13 ) ||(t == 'textarea') || ( t == 'submit' &&  kc == 13))
					return true
				else
				{
					//alert('Sorry Backspace/Enter is not allowed here'); // Demo code
					return false
				}
			}
		}
	}
});
