//on document is ready
$(document).ready(function(){
    // check for acceptance of terms and conditions
    $("#gems_pay_submit").click(function() {
        // if not accepted
        if (! $("#gems_terms_check").attr("checked")) {
            alert ("Please accept terms and conditions.")
            return false;
        }

        return true;
    });

    // gems4u index form
    $("#gems_table img").click(function () {
        var imgId = $(this).attr("id");

        var gemId = imgId.split("-")[1];
        var chk = $("#gems_table #gemstone-" + gemId);
        if ($(chk).attr("checked")) {
            $(chk).attr("checked", false);
        } else {
            $(chk).attr("checked", true);
        }
    });

    // check atleast one gem is selected in index page
    $("#gems_index_submit").click(function() {
        if ($("[name='gemstone']:checked").length <= 0) {
            alert("Please select atleast one Gem");
            return false;
        }
        return true;
    });

    // gems selection form
    var currentGem = null;
    var form = $("#gemSelectionForm");

    if (form.length > 0) {
        // clear previous selection
        $("[name='weightId']", form).each(function() {
            // get the price
            this.selectedIndex = 0;
        });
//        $("[name='ringId']", form).each(function() {
            // get the price
//            this.selectedIndex = 0;
//    ss    });
        $("[name='reffer']", form).each(function() {
            // get the price
            this.checked = false;
        });

        // register onsubmit event for form
        $("[type='submit']", form).click(validateGemsForm);

        $("[name='weightId']", form).change(function() {
            // get selected gem
            currentGem = this.id.split("-")[1];
            // get the price
            var weightPrice = this.options[this.selectedIndex].className;
            calculateWeight(weightPrice);
        });
        
//        $("[name='ringId']", form).change(function() {
            // get selected gem
//            currentGem = this.id.split("-")[1];
            // get the price
//            var ringPrice = this.options[this.selectedIndex].className;
           // calculateRing(ringPrice);
//        });


//        $("[name='quantity']", form).keyup(function() {
//            // get selected gem
//            currentGem = this.id.split("-")[1];
//            // get qty
//            var qty = $(this).val();
//            if (isNaN(qty) || qty.length==0) {
//                $(this).val("0");
//                qty = 0;
//            } else {
//                qty = parseInt(qty)
//                $(this).val(qty);
//            }
//
//            calculateQty(qty);
//        });
    }

    function validateGemsForm() {

        var grand = getTotal();
        //alert("grand"+grand);
        if (isNaN(grand) || grand <= 0.0) {
            alert ("Please select gem stone weight");
            return false;
        }
        return true;
    }
    // calculates grand total
    function getTotal() {
        // get all elements holding "total" for that particula product
        var cal = $("div .total", form);
        var grand = 0.0;

        // iterate through all totals in the form and then calculate grand total
        for (var x=0; x<$(cal).length; x++) {
            var total = $(cal).get(x);
            var p = parseFloat($(total).html());

            if (p == 0) {
                grand = -1;
                break;
            }

            grand += p;
           
        }

         //alert("P"+ grand);
        return grand;
    }

    function calculate(weight, ring, quantity) {
        var cal = $("#price-" + currentGem, form);

        if (weight == null) {
            weight = $(cal).children(".weight").html();
        } else {
            $(cal).children(".weight").html(weight);
        }

//      if (ring == null) {
//            ring = $(cal).children(".ring").html();
//        } else {
//            $(cal).children(".ring").html(ring);
//       }
//
//        if (quantity == null) {
//            quantity = $(cal).children(".qty").html();
//        } else {
//            $(cal).children(".qty").html(quantity);
//        }

        //alert("w: " + weight + ", r: " + ring + ", q: " + quantity);
        weight = parseFloat(weight);
       // ring = parseFloat(ring);
     //   quantity = parseInt(quantity);

         var total = weight;

//        var total = (weight + ring) * quantity;
        if (weight==0) {
            total = 0;
        }
         //alert("total"+total);
        $(cal).children(".total").html(parseFloat(total));
    }

    function calculateWeight(weightPrice) {
        calculate(weightPrice, null, null);
    }

//    function calculateRing(ringPrice) {
      //  calculate(null, ringPrice, null);
//    }

//    function calculateQty(qty) {
      //  calculate(null, null, qty);
//    }
  
});  
   