$(document).ready(function() {

	$("#shipping-address-list, #us_ship_states").hide();

	$("#address-list, .shipping-state, .update").hide();

	$(".alt_del").show();

	$(".hidden_input").html('<input type="hidden" name="use_ship_add_check" value="true" />');

	

	$("#bill-state, #shipping-state").hide();

	

	if ($("#shipping-address").is(":checked")){

		$("#ship-address").hide();
        
        

	}else{

		$("#ship-address").show();

	}


	$("#shipping-address").click(function(){

		if ($("#shipping-address").is(":checked")){

			$("#ship-address").slideUp("fast");
            
            if($('#billing-country :selected').val()!=$('#ship-country :selected').val())
            {
                //alert('billing doesnt match, need to submit');
                var selectVal = $('#billing-country :selected').val();
                $('#billing-country-change').val(selectVal);
	           $('#ship-country-change').val('');
	
	           $('#submit_continue').trigger('click');
            }

		}

		else{     

			$("#ship-address").slideDown("fast");

		}       

	});

	

	$("#postcode-searchbtn").click(function(){

		$("#shipping-address-list").show();

	});

	

	

	if($('#billing-country').val()=='US')

	{

		$('#bill-state').show();

	}

	

	$('#billing-country').change(function()

	{

		if($('#billing-country').val()=='US')

		{

			$('#bill-state').slideDown();

		}

		if(($('#billing-country').val()!='US')&&($('#bill-state').is(':visible')))

		{

			$('#bill-state').slideUp();

		}

	});

	

	if($('#ship-country').val()=='US')

	{

		$('#shipping-state').show();

	}     

	

	$('#ship-country').change(function()

	{

		if($('#ship-country').val()=='US')

		{

			$('#shipping-state').slideDown();

		}

		

		if(($('#ship-country').val()!='US')&&($('#shipping-state').is(':visible')))

		{

			$('#shipping-state').slideUp();

		}

	});

	

	

	

	

	

	

	

	$(".country select").change( function() {

		if($(this).attr('name')=='billing_country'){

			if($(this).val()=="GB"){

				$(".billing-state").slideUp();

				//$(".billing-county").slideDown();

			}

			else if($(this).val()=="US"){

				//$(".billing-county").slideUp();

				$(".billing-state").slideDown();

			}

			else {

				$(".billing-state").slideUp();

			}

		}else if($(this).attr('name')=='shipping_country'){

			if($(this).val()=="GB"){

				$(".shipping-state").slideUp();

				//$(".billing-county").slideDown();

			}

			else if($(this).val()=="US"){

				//$(".billing-county").slideUp();

				$(".shipping-state").slideDown();

			}

			else {

				$(".shipping-state").slideUp();

			}

		}

	});

	

	

	$("#delivery-time").change( function() {

		$("#checkout_form").submit();

	});	

	

	

	$(".alt_del span").click(function () {

		var $checkbox = $(this).find('checkbox');

        $checkbox.attr('checked', !$checkbox.is(':checked'));

	});


	/*var warning = true;

	window.onbeforeunload = function() { 

	  if ($("#checkout_form").submit()==false) {

	    return "You have made changes on this page that you have not yet confirmed. If you navigate away from this page you will loose your unsaved changes";

	  }

	}*/

	

  $(".item-quantity").change(function() {
    $("#checkout_form").submit();
  });

	

	

	

	

	//$('#hideselectaddress').hide();

    //$('#billing-address-list').hide();

    

    // All the basket postcode ajax stuff

    // when billing postcode submit is clicked

    $('#billing-postcode-submit').click(function (event) {

        event.preventDefault();

        if($("#billing-postcode-search").val()==""){

			$("#billing-postcode-search").addClass("error");

		}else{

			postcode_lookup('billing');

		}

    });

    // when shipping postcode submit is clicked

    $('#shipping-postcode-submit').click(function (event) {

        event.preventDefault();

        if($("#shipping-postcode-search").val()==""){

			$("#shipping-postcode-search").addClass("error");

		}else{

			postcode_lookup('shipping');

		}

    });

    

    // Do the AJAX postcode lookup

    function postcode_lookup(type) {

        // set variables depending on if its billing/shipping.

        // $pcid = postcode input box id

        // $pcselect = "select address" select box id

        // $unhide = hide address wrap div id

        if(type === 'billing') {

            $pcid = $('#billing-postcode-search');

            $pcselect = $('#billing-paf');

            $pcselected = $('#billing-paf option:selected');

            $unhide = $('#billing-address-list');

        }else if(type === 'shipping') {

            $pcid = $('#shipping-postcode-search');

            $pcselect = $('#shipping-paf');

            $pcselected = $('#shipping-paf option:selected');

            $unhide = $('#shipping-address-list');

        }

        

        // get postcode value from input

        $postcode = $pcid.val();

        // strip spaces

        $postcode = $postcode.replace(' ', '');

        // set up ajaxurl with the postcode

        $ajaxurl = "../../shop/checkout/includes/ajax_postcode.php?postcode=" + $postcode;

        

        // use load to put the output inside the select box

        $pcselect.load($ajaxurl);

        

        // show the select box

        $unhide.css('display', 'inline');

        

    }

    

    // when address list is changed

    $('#billing-paf').change(function () {

        // get the value of the newly selected option

        $value = $('#billing-paf option:selected').val();

        

        // if the value is not null (i.e. is an address)

        if ($value != '') {

            // change the address

            change_address('billing');               

        }

    });

    // when address list is changed

    $('#shipping-paf').change(function () {

        // get the value of the newly selected option

        $value = $('#shipping-paf option:selected').val();

        

        // if the value is not null (i.e. is an address)

        if ($value != '') {

            // change the address

            change_address('shipping');               

        }

    });

    

    // change address function

    function change_address($type) {

        // set up variables depending on billing/shipping

        // $selected = selected option in address list

        // $address1 = address line 1 id

        // $address2 = address line 2 id

        // $city = city id

        // $county = UK county id (if used) - if not comment out.

        if ($type == 'billing') {

            $selected = $('#billing-paf option:selected');

            $address1 = $('#billing-address-1');

            $address2 = $('#billing-address-2');

            $city = $('#billing-city');

            //$county = $('#billing-county option');

        } else if ($type == 'shipping') {

            $selected = $('#shipping-paf option:selected');

            $address1 = $('#shipping-address-1');

            $address2 = $('#shipping-address-2');

            $city = $('#shipping-city');

            //$county = $('#shipping-county option');

        }

        

        // get selected values inner text

        $seltext = $selected.val();

        

        // split the selected text into an array of parts we can use
        var address_lines = $seltext.split(", ");
        sizeof = address_lines.length;
        secondline = '';
        if (sizeof == 4) {
            secondline = address_lines[1]+', '+address_lines[2];
        } else if (sizeof == 3) {
            secondline = address_lines[1];
        }
        address_lines[0] = address_lines[0].replace('|',',');
        secondline = secondline.replace('|',',');
        
        // set input boxes values to the values of the array
        $address1.val(address_lines[0]);
    	$address2.val(secondline);
    	$city.val(address_lines[sizeof-1]);

        

        // this deals with counties. Comment out if not needed.

        //$searchcounty = address_lines[3].toLowerCase();

        //$newcounty = $county.each(function () {

        //    if ($(this).text().toLowerCase().indexOf($searchcounty) > -1) {

        //        $(this).attr('selected',true);

        //    }

        //});

    }

    

    // for basket postcode on pressing enter.

    var  TextBox = $('#billing-postcode-search');

    var code =null;

    // on keypress do this

    TextBox.keypress(function(e)

    {

        // get keycode

        code= (e.keyCode ? e.keyCode : e.which);

        // if keycode is 13 (enter)

        if (code == 13) {

            postcode_lookup('billing');

            e.preventDefault();

        }

    });

    

    var  TextBox = $('#shipping-postcode-search');

    var code =null;

    // on keypress do this

    TextBox.keypress(function(e)

    {

        // get keycode

        code= (e.keyCode ? e.keyCode : e.which);

        // if keycode is 13 (enter)

        if (code == 13) {

            postcode_lookup('shipping');

            e.preventDefault();

        }

    });

    

    //reveal password

    $(".reveal").show();

    

    $("#reveal_password").click(function(){

    	var password = $("#password").val();

    		

		if(password !="" && $("#reveal_password").is(":checked")){

			//$('<input type="text" name="password_text" id="password_text" value="'+ password +'"/>').insertAfter('#password');

			

    		$("#password").hide();

    		$("#password_text").show();

    		$("#password_text").val(password);

		} else {

			$("#password_text").hide();

			var password = $("#password_text").val();

			$("#password").val(password);

    		$("#password").show();

    	}

    });

    

    

	$('#password_text').live('keydown', function() {

		var password = $("#password_text").val();

		$("#password").val(password);

	});



});
