$(document).ready(function() {
    $(".checkoutstage").hide();
    $(".paypalbuynow").hide();
    $(".checkoutstage:eq(0)").show();

    $(".instructionstage:eq(0)").click(function () {
        $(".checkoutstage:eq(0)").toggle("fast");
        if ($(".checkoutstage:eq(1)").is(":hidden")==false) {
         $(".checkoutstage:eq(1)").toggle("fast");
        }
        if ($(".checkoutstage:eq(2)").is(":hidden")==false) {
            $(".checkoutstage:eq(2)").toggle("fast");
        }
    })
    $(".instructionstage:eq(1)").click(function () {
        $(".checkoutstage:eq(1)").toggle("fast");
        if ($(".checkoutstage:eq(0)").is(":hidden")==false) {
         $(".checkoutstage:eq(0)").toggle("fast");
        }
        if ($(".checkoutstage:eq(2)").is(":hidden")==false) {
            $(".checkoutstage:eq(2)").toggle("fast");
            if ($("select#paymenttype").val == "Paypal") {
                $(".paypalbuynow").show();
                $(".paybycheque").hide();
            }
        }
    })
    $(".instructionstage:eq(2)").click(function () {
        $(".checkoutstage:eq(2)").toggle("fast");
        if ($(".checkoutstage:eq(0)").is(":hidden")==false) {
         $(".checkoutstage:eq(0)").toggle("fast");
        }
        if ($(".checkoutstage:eq(1)").is(":hidden")==false) {
            $(".checkoutstage:eq(1)").toggle("fast");
        }
    })
    $(".checkoutstage:eq(0) .SUBMIT").click(function () {
        var zone = $("select#zone").val();
        var orderno = $("input#orderno").val();
        var paymenttype = $("select#paytype").val();
        //alert(paymenttype);
        //alert("1");
        //alert($("input[name='shipping']").val());
        var dataString = 'zone='+ zone + '&orderno=' + orderno + '&paymenttype=' + paymenttype;
        $.ajax({
            type: "POST",
            url: "checkoutformprocess.php",
            data: dataString,
            error : function(XMLHttpRequest, textStatus, errorThrown) { alert(textStatus); } ,
            success: function(calcfields) {
                  $(".checkoutstage:eq(0)").hide("fast");
                  $(".checkoutstage:eq(2)").hide("fast");
                  $(".checkoutstage:eq(1)").show("fast");
                  var fields = calcfields.split(",");
                  $(".calculatedshipping").html(fields[0]);
                  $(".calculatedtotal").html(fields[1]);
                  $("input[name='shipping']").attr('value', fields[2]);
                  //alert(fields[2]);
                  //alert($("input[name='shipping']").val());
            }   
        });
        $(".selectedzone").html(zone);
        return false;
    })
    $(".checkoutstage:eq(1) .SUBMIT").click(function () {
        var zone = $("select#zone").val();
        var orderno = $("input#orderno").val();
        var paymenttype = $("select#paytype").val();
        //alert(paymenttype);
        //alert(zone);
        var dataString = 'zone='+ zone + '&orderno=' + orderno + '&paymenttype=' + paymenttype;
        $.ajax({
            type: "POST",
            url: "checkoutformprocess.php",
            data: dataString,
            error : function(XMLHttpRequest, textStatus, errorThrown) { alert(textStatus); } ,
            success: function(thisdata) {
                  $(".checkoutstage:eq(0)").hide("fast");
                  $(".checkoutstage:eq(1)").hide("fast");
                  $(".checkoutstage:eq(2)").show("fast");
            }   
        });
        if (paymenttype == "Paypal" || paymenttype == "PPCard") {
                $(".paypalbuynow").show();
                $(".paybycheque").hide();
        }
        if (paymenttype == "Chq") {
                $(".paypalbuynow").hide();
                $(".paybycheque").show();
        }
        return false;
    })

});



