// JavaScript Document
$(document).ready(function()
{
	//search for and strip / store zip code from url if available
	//ex: http://rose-int1/_projects/kirchner/kr118_zipsearch/index.php?zipCode=90210
	
	//Tree - please give _myZip variable the session variable instead of stripping the url
	var _myZip = $(document).getUrlParam("zipCode");
	
	var $zipField = $('#zip_code');
	var $zipSubmit = $('#zipForm .formBtn');
	
	//if zip code available pre-fill zip input field and change button text to be 'Change'
	if(_myZip != null)
	{
		$zipField.attr('value', _myZip);
		$zipSubmit.attr('value', 'Change');
	}
	
	//small feature to remove and store input contents on focus, restore input contents with variable on blur if no change was made
	$zipField.focus(function()
	{
		var zipChanged = false;
		
		var zipFieldValue = $zipField.attr('value'); //store value
		//$.log('zipFieldValue = ' + zipFieldValue);
		
		$zipField.attr('value', ''); //clear current input
		
		//store new value if changed
		$zipField.change(function()
		{
			zipFieldValue = $zipField.attr('value');
			//$.log('zipFieldValue changed to = ' + zipFieldValue);
		});
		
		$zipField.blur(function()
		{
			$zipField.attr('value', zipFieldValue);
		});
	});
	
	//if zip code not available do nothing until submit
	
	//when submit is clicked, send zip code to the url string, keep the zip code in the input field, change button text to be 'Change'
	//and perhaps do an ajax form field submit
	
	//validate on submit
	$("#zipForm form").validate(
	{
 		//event: "keyup",
 		rules:
		{
 			zip_code:
			{
 				required: true,
				number: true,
				minLength: 5,
 				maxLength: 5
 			}
 		},
  		
		messages:
		{
 			zip_code:
			{
 				required: function(element, validator)
				{
 					return "A valid zip code is required."
 				},
 				number: "Please enter numbers only", 
				minLength: "Please enter a 5 digit zip code.",
 				maxLength: "Please enter a 5 digit zip code."
 			}
 		}
	});
	
});