$(document).ready
(
	function()
	{
		/**
		 * Copies phone 1 to phone 2, I didn't know where to put this code and I chose this file cause it does prepopulation
		 */
		if($('#slfPhoneDay').size() > 0 && $('#slfPhoneNight').size() > 0)
		{
			$('#slfPhoneDay').blur
			(
				function()
				{
					if($('#slfPhoneDay').val() != '' && $('#slfPhoneNight').val() == '')
					{
						$('#slfPhoneNight').val($('#slfPhoneDay').val());
					}
				}
			);
		}
		
		if($('#slfZip').size() > 0 && $('#slfCity').size() > 0 && $('#slfState').size() > 0)
		{
			var label = $('label[for=slfZip]');			
			var zipCodeLabelText = label.html();
			var zipCodeLabelColor = label.css('color');
			var zipcodeValid = true;
			
			var ajaxLoaderImage = 'images/ajax-loader-small.gif';
			
			if($.browser.mozilla)
			{
				ajaxLoaderImage = 'images/ajax-loader-small-grey.gif';
			}
			
			$('#slfZip').blur
			(
				function()
				{
					var zipCode = $(this).val();
					var zipRegexp = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);					
					
					$('#slfCity').val('');
					$('#slfState').val('');
					
					if(zipRegexp.test(zipCode))
					{
						label.html(zipCodeLabelText);
						label.css('color', zipCodeLabelColor);
						
						$.ajax
						(
							{
								url: 'ajax-helper?action=location',
								data: {'zipcode': zipCode},
								type: 'GET',
								dataType: 'json',
								cache: false,
								beforeSend: function()
								{
									$('#slfCity').attr('disabled', true).css('backgroundImage', 'url(' + ajaxLoaderImage + ')').css('backgroundRepeat', 'no-repeat').css('backgroundPosition', 'left center');
									$('#slfState').attr('disabled', true);
								},
								success: function(data)
								{									
									if(data == null)
									{
										label.html(zipCodeLabelText + ' (invalid)').css('color', '#ff0000');						
										$(this).val('').focus();
									}
									
									$('#slfCity').attr('disabled', false).css('backgroundImage', '')
									
									if(data != null)
									{
										$('#slfCity').val(data.city);
									}

									$('#slfState').attr('disabled', false);
									
									if(data != null)
									{
										$('#slfState').val(data.stateID);
									}
								},
								error: function(request, textStatus, errorThrown)
								{
									alert('Error: ' + textStatus + ", " + errorThrown);
								}
							}
						);
					}					
					else
					{
						label.html(zipCodeLabelText + ' (invalid)').css('color', '#ff0000');						
						$(this).val('').focus();
					}
				}
			);
		}
	}
);
