$(function() {

$(function() {
     $('.help').click(function() {
        var dialogId = "#" + $(this).attr('rel');
        $(dialogId).dialog({
           width: 650,
           resizable: false,
           close: function() { $(this).dialog( 'destroy' );}
	    });
		return false;
	});

});

    function getShipping()
    {
        $("#shipping_div").slideUp("slow");

        $.ajax({
            url : 'index.php',
            data : ({ 
               page : 'modules/quickcheckout/ajax_checkout_shipping',
               command : 'start',
               shipping_country : $("select[name=shipping[country]]").val(),
               shipping_zip : $("input[name=shipping[zip]]").val()
            }),
            success : function(data) {
               $("#shipping_div").replaceWith(data);
               $("#shipping_div").slideDown("slow");
            }
        });
    }

    function getCreditCardType()
    {
        $.ajax({
            url : 'index.php',
            data : ({ 
               page : 'modules/quickcheckout/ajax_checkout_payment',
               command : 'initCreditCard',
               cc_type : $("select[name=cc_type]").val()
            }),
            success : function(data) {
               $("#credit-card").replaceWith(data);
               $("#credit-card").slideDown("slow");
               $("select[name=cc_type]").bind('change',function() { 
                   getCreditCardType();
               });
            }
        });
    }
    function calculateTotals()
    {
        $.ajax({
            url : 'index.php',
            data : ({ 
               page : 'modules/quickcheckout/ajax_checkout_order_summary',
               command : 'start',
               order_ref_id : $("input[name=order_ref_id]").val(),
               shipping_method : $("input[name=shipping_method]:checked").val(),
               payment_method : $("input[name=payment_method]:checked").val(),
               billing_country : $("select[name=billing[country]]").val(),
               billing_state : $("select[name=billing[state]]").val(),
               billing_zip : $("input[name=billing[zip]]").val(),
               shipping_country : $("select[name=shipping[country]]").val()
            }),
            success : function(data) {
               $("#order_summary").replaceWith(data);
            }
        });
    }

    $("[name^=billing]:input,[name^=shipping]:input").keypress(function() {
        $("input[name=sameshipping]").attr("checked",false);
    });

    $("input[name=sameshipping]").click(function() {
       if($(this).attr("checked") == true) {
           $("[name^=billing]:input").each(function(i,billingField) {
               if($(billingField).attr("name") == "billing[country]" && $(billingField).val() != $("[name=shipping[country]]:input").val())
               {
                   $("[name^=shipping]:input").slice(i,i+1).val($(billingField).val());
                   getShipping();
                   calculateTotals();
               }
               else
               {
                   $("[name^=shipping]:input").slice(i,i+1).val($(billingField).val());
               }
           });
       }
    });

    $("select[name=billing[country]]").change(function() {
        $("input[name=sameshipping]").attr("checked",false);
        calculateTotals();
    });

    $("select[name=shipping[country]]").change(function() {
        $("input[name=sameshipping]").attr("checked",false);
        getShipping();
        calculateTotals();
    });

    $("input[name=shipping_method]").live('click',function() {
        calculateTotals();
    });

    $("input[name=payment_method]").click(function() {
        calculateTotals();
    })

    $("select[name=cc_type]").bind('change',function() { 
        getCreditCardType();
    });

});