function resetZipCode()
{
	$('#zip_location').hide();
	$('#zip_label').html("Zipcode:");
	$('#form_zip').show();
	$('#form_zip').val('');
	$('#form_zip').focus();
}

function zipCodeLookup(e, zipCode)
{
	var key = (!e) ? window.event : e.which;	 
	 
	if( jQuery.trim(zipCode.value).length == 5 )
	{	 	
		$.getJSON("/zipcode/lookup/"+jQuery.trim(zipCode.value), function(result)
    	{
			if (result.success) 
			{
				$.post('registration/update', {field:'zip', value:jQuery.trim($('#form_zip').val())});
				$('#form_zip').hide();
				$('#zip_label').html("Location:");
				$('#zip_location').show();
				$('#zip_location').html(result.zip.CityMixedCase + ", " + result.zip.State);
				$('#form_city').val(result.zip.CityMixedCase);
				$('#form_state').val(result.zip.State);
				$('#form_phone_areacode').focus();
			}
			else
			{
				modalAlert('Please verify that the zipcode you entered is valid.', 0, $('#form_zip'));					
			}
		});
	 }
}

function validateRegistration()
{    
    if( !alphaNumericPlusCheck($('#form_first_name')) )
    {
         modalAlert('You must provide an accurate <b>First Name</b> in order for us to ship your free Gold Recycling Kit!', 0, $('#first_name'));
         return false;
    }

    if( !alphaNumericPlusCheck($('#form_last_name')) )
    {
        modalAlert('You must provide an accurate <b>Last Name</b> in order for us to ship your free Gold Recycling Kit!', 0, $('#last_name'));
        return false;
    }
        

    if( !alphaNumericPlusCheck($('#form_address1')) )
    {
        modalAlert('You must provide an accurate <b>Address</b> in order for us to ship your free Gold Recycling Kit!', 0, $('#address1'));
        return false;
    }

	/*
    if( !alphaNumericPlusCheck($('#city')) )
    {
        modalAlert('You must provide an accurate <b>City</b> in order for us to ship your free Gold Recycling Kit!', 0, $('#city'));
        return false;
    }

    if( $('#state').val() == "" )
    {
        modalAlert('You must select a <b>State</b> in order for us to ship your free Gold Recycling Kit!', 0, $('#state'));
        return false;
    }
    */

    if( !numericCheck($('#form_zip')) )
    {
        modalAlert('You must provide an accurate <b>Zip Code</b> in order for us to ship your free Gold Recycling Kit!', 0, $('#zip'));
        return false;
    }

    if( !emailCheck($('#form_email')) )
    {
        modalAlert('You must provide an accurate <b>Email Address</b> in order for us to ship your free Gold Recycling Kit!', 0, $('#email'));
        return false;
    }

    if( !numericCheck($('#form_phone_areacode')) || $('#form_phone_areacode').val().length != 3 )
    {
        modalAlert('You must provide an accurate <b>Phone Number</b> in order for us to ship your free Gold Recycling Kit!', 0, $('#phone_areacode'));
        return false;
    }

    if( !numericCheck($('#form_phone_prefix')) || $('#form_phone_prefix').val().length != 3 )
    {
        modalAlert('You must provide an accurate <b>Phone Number</b> in order for us to ship your free Gold Recycling Kit!', 0, $('#phone_prefix'));
        return false;
    }

    if( !numericCheck($('#form_phone_suffix')) || $('#form_phone_suffix').val().length != 4 )
    {
        modalAlert('You must provide an accurate <b>Phone Number</b> in order for us to ship your free Gold Recycling Kit!', 0, $('#phone_suffix'));
        return false;
    }

    if( !$('#form_terms').attr('checked') )
    {
        modalAlert('You must certify that you are at least age 21 and agree to our <a href="./terms_and_conditions">Terms and Conditions</a> by checking the box on the form.', 0, $('#TermsAndConditions'));
        return false;
    }

    //$('#kitRequestForm').submit(); 
    return true;
}
 
$(document).ready(function() {
    var updateFields = ['first_name', 'last_name', 'email', 'address1', 'address2', 'zip', 'phone_areacode', 'phone_prefix', 'phone_suffix'];
    for(var i=0;i<updateFields.length;i++) {
        $('#form_'+updateFields[i]).blur(function() {
            if(!$(this).val() || $(this).val().length > 0) {
                $.post('registration/update', {field:$(this).attr('name'), value:$(this).val()});
            }          
        });
    }
	zipCodeLookup(window.event, document.getElementById('form_zip'));
});
