$(document).ready(function() {

    $.ajaxSetup({
        url:"ordersetup",
        type: "POST",
        timeout: "15000",
        cache: false
    });

    $("#update_indicator").ajaxStart(function(){
        $(this).show();
    });
    $("#update_indicator").ajaxStop(function(){
        $(this).hide();
    });

    $('#show-billing').click(function () {
        if (this.checked)
            $('#billing-addr').slideUp();
        else
            $('#billing-addr').slideDown("slow");
    });

    // TotalSumm, onLoad
    var ordersumm = $('input[name=summ]').val();
    var shippingsumm = $("input:radio[name='shippingmethod']:checked").val();
    var totalsumm = Number(shippingsumm) + Number(ordersumm);
    jQuery('#pay_all').html('<strong>$ '+totalsumm+'</strong>');

    // TotalSumm, change shipping method
    $("input:radio[name='shippingmethod']").live('click',function(){
        var shippingsumm = $("input:radio[name='shippingmethod']:checked").val();
        var totalsumm = Number(shippingsumm) + Number(ordersumm);
        jQuery('#pay_all').html('<strong>$ '+totalsumm+'</strong>');
    });


    // allinfo: send, disable
    $('#allinfo').click(function () {
        if (this.checked){
            // remove error info
            $('.check_center').removeClass('check_error');

            // send
            var firstname = $("input[name='firstname']").val();
            var lastname = $("input[name='lastname']").val();
            var city = $("input[name='city']").val();
            var address = $("input[name='address']").val();
            var zip = $("input[name='zip']").val();
            var country = $('select#country option:selected').val();
            var state = $('select#state option:selected').val();
            var phone = $("input[name='phone']").val();
            var email = $("input[name='email']").val();

            var showbilling = $("input[name='show-billing']").val();

            var billingfirstname = $("input[name='billingfirstname']").val();
            var billinglastname = $("input[name='billinglastname']").val();
            var billingcity = $("input[name='billingcity']").val();
            var billingaddress = $("input[name='billingaddress']").val();
            var billingzip = $("input[name='billingzip']").val();
            var billingcountry = $('select#billingcountry option:selected').val();
            var billingstate = $('select#billingstate option:selected').val();

            $.ajax({
                data: "action=register&firstname="+firstname+"&lastname="+lastname+"&city="+city+"&address="+address+"&zip="+zip+"&country="+country+"&state="+state+"&phone="+phone+"&email="+email+"&showbilling="+showbilling+"&billingfirstname="+billingfirstname+"&billinglastname="+billinglastname+"&billingcity="+billingcity+"&billingaddress="+billingaddress+"&billingzip="+billingzip+"&billingcountry="+billingcountry+"&billingstate="+billingstate,
                success: function(data){
                    // replace shipping type
                    var dataobject = eval('(' + data + ')');
                    $('.fieldcheckboxone').remove();

                    $.each(dataobject, function(i,item){
                        if (i == 0) $("#shippingmethods").after("<div class='fieldcheckboxone'><div class='fieldcbbody'><input type='radio' name='shippingmethod' value='"+item.price+"' class='radio' checked /></div><div class='fieldcbheader'><label>$"+item.price+"     "+item.name+"</label></div></div>");
                        else $("#shippingmethods").after("<div class='fieldcheckboxone'><div class='fieldcbbody'><input type='radio' name='shippingmethod' value='"+item.price+"' class='radio' /></div><div class='fieldcbheader'><label>$"+item.price+"     "+item.name+"</label></div></div>");
                    });

                    // totalsumm
                    var ordersumm = $('input[name=summ]').val();
                    var shippingsumm = $("input:radio[name='shippingmethod']:checked").val();
                    var totalsumm = Number(shippingsumm) + Number(ordersumm);
                    jQuery('#pay_all').html('<strong>$ '+totalsumm+'</strong>');
                },
                error: function(xhr, status){
                    alert("Error "+status);
                }
            });

            // disable if success!
            $("input[name='firstname']").attr('disabled', true);
            $("input[name='lastname']").attr('disabled', true);
            $("input[name='city']").attr('disabled', true);
            $("input[name='address']").attr('disabled', true);
            $("input[name='zip']").attr('disabled', true);
            $("#country").attr('disabled', true);
            $("#state").attr('disabled', true);
            $("input[name='phone']").attr('disabled', true);
            $("input[name='email']").attr('disabled', true);
            $("input[name='billingfirstname']").attr('disabled', true);
            $("input[name='billinglastname']").attr('disabled', true);
            $("input[name='billingcity']").attr('disabled', true);
            $("input[name='billingaddress']").attr('disabled', true);
            $("input[name='billingzip']").attr('disabled', true);
            $("#billingcountry").attr('disabled', true);
            $("#billingstate").attr('disabled', true);
            $("#show-billing").attr('disabled', true);

            // no remove from basket
            $(".cross").remove();
        }
        else {
            $("input[name='firstname']").removeAttr('disabled');
            $("input[name='lastname']").removeAttr('disabled');
            $("input[name='city']").removeAttr('disabled');
            $("input[name='address']").removeAttr('disabled');
            $("input[name='zip']").removeAttr('disabled');
            $("#country").removeAttr('disabled');
            $("#state").removeAttr('disabled');
            $("input[name='phone']").removeAttr('disabled');
            $("input[name='email']").removeAttr('disabled');
            $("input[name='billingfirstname']").removeAttr('disabled');
            $("input[name='billinglastname']").removeAttr('disabled');
            $("input[name='billingcity']").removeAttr('disabled');
            $("input[name='billingaddress']").removeAttr('disabled');
            $("input[name='billingzip']").removeAttr('disabled');
            $("#billingcountry").removeAttr('disabled');
            $("#billingstate").removeAttr('disabled');
        }
    });


    // order send
    $("#submitorder").click(function(){

        // if allinfo not checked
        if($("#allinfo").is(':checked') == false){
            $('.check_center').addClass('check_error');
        }else{
            var nameoncard = $("input[name='nameoncard']").val();
            var cardnumber = $("input[name='cardnumber']").val();
            var cvv = $("input[name='cvv']").val();
            var expiremonth = $('select#expiremonth option:selected').val();
            var expireyear = $('select#expireyear option:selected').val();
            var shippingsumm = $("input:radio[name='shippingmethod']:checked").val();

            $.ajax({
                data: "action=submitorder&nameoncard="+nameoncard+"&cardnumber="+cardnumber+"&cvv="+cvv+"&expiremonth="+expiremonth+"&expireyear="+expireyear+"&shippingsumm="+shippingsumm,
                success: function(data){
                    window.location="http://www.online-laptop.com/thank-you";
                },
                error: function(xhr, status){
                    alert("Error "+status);
                }
            });
        }
    });

    // Live validation
    $("#firstname").validate({
        expression: "if (VAL.length > 1 && VAL) return true; else return false;",
        message: "Enter First Name",
        error_class: "error"
    });
    $("#lastname").validate({
        expression: "if (VAL.length > 1 && VAL) return true; else return false;",
        message: "Enter Last Name",
        error_class: "error"
    });
    $("#address").validate({
        expression: "if (VAL.length > 5 && VAL) return true; else return false;",
        message: "Enter address",
        error_class: "error"
    });
    $("#city").validate({
        expression: "if (VAL.length > 2 && VAL) return true; else return false;",
        message: "Enter City",
        error_class: "error"
    });
    $("#zip").validate({
        expression: "if (VAL.length > 3 && VAL && !isNaN(VAL)) return true; else return false;",
        message: "Enter zip or postal code",
        error_class: "error"
    });
    $("#phone").validate({
        expression: "if (VAL.length > 5 && VAL) return true; else return false;",
        message: "Enter phone",
        error_class: "error"
    });
    $("#email").validate({
        expression: "if (VAL.length > 4 && VAL) return true; else return false;",
        message: "Enter email",
        error_class: "error"
    });
    $("#nameoncard").validate({
        expression: "if (VAL.length > 5 && VAL) return true; else return false;",
        message: "Enter name on card",
        error_class: "error"
    });
    $("#cardnumber").validate({
        expression: "if (VAL.length > 8 && VAL) return true; else return false;",
        message: "Enter card no",
        error_class: "error"
    });
    $("#cvv").validate({
        expression: "if (VAL.length > 2 && VAL && !isNaN(VAL)) return true; else return false;",
        message: "Enter CVV",
        error_class: "error"
    });
});
