﻿/// <reference path="../jquery-vsdoc.js" />
$(document).ready(function() {
    Initialize();
});

function Initialize() {
    SetupFormFunctions();
    SetupValidation();
}

function SetupValidation() {
    $("#buyNowForm").validationEngine({
        success: false,
        failure: function() {  }
    })
}

function SetupFormFunctions() {

    /*** add onChange functions to various inputs ***/
    
    //country dropdown change
    $("#ddlCountry").change(function () {
        if ($(this).val() == "Canada") {
            //show province ddl and hide tb
            $("#ddlProvince").show().val("0");
            $("#tbProvince").hide();

            if ($("#ddlProvince").val() == "Quebec") {
                /****** Province is Quebec so we need to add GST and PST ******/
                //total before tax
                var totalWithOutGST = GetTotalBeforeTax($("#tbAmount").val(), 15, 2.5);
                //console.log("total without gst: " + totalWithOutGST);
                //GST
                var GST = (5 * totalWithOutGST) / 100;
                GST = GST;
                $("#intGst").text(GST.toFixed(2));
                $("#tbhtax1").val(GST.toFixed(2));
                //console.log("GST: " + GST);
                //Total with GST
                var totalWithGST = totalWithOutGST + GST;
                //console.log("total with gst: " + totalWithGST);
                //PST
                var PST = (9.5 * totalWithGST) / 100;
                $("#intPst").text(PST.toFixed(2));
                $("#tbhtax2").val(PST.toFixed(2));
                //console.log("PST: " + PST);
                //Total with PST (Final Total)
                var totalWithPST = totalWithGST + PST;
                totalWithPST = totalWithPST.toFixed(2);
                $("#intTotal").text(totalWithPST);
                //console.log("Final Total: " + totalWithPST);

                //Set Hidden Field
                $("#hfAmount").val(totalWithPST);
                //console.log("Set HF: $" + totalWithPST);
            }
            else {
                /****** Province NOT Quebec so we need to add ONLY GST ******/
                //total before tax
                var totalWithOutGST = GetTotalBeforeTax($("#tbAmount").val(), 15, 2.5);
                //console.log("total without gst: " + totalWithOutGST);
                //GST
                var GST = (5 * totalWithOutGST) / 100;
                GST = GST;
                $("#intGst").text(GST.toFixed(2));
                $("#tbhtax1").val(GST.toFixed(2));
                //console.log("GST: " + GST);
                //Total with GST (Final total)
                var totalWithGST = totalWithOutGST + GST;
                totalWithGST = totalWithGST.toFixed(2);
                //console.log("total with gst: " + totalWithGST);
                //PST
                $("#intPst").text("0");
                $("#tbhtax2").val("0");
                //console.log("PST: " + PST);
                //Total with PST (Final Total)
                $("#intTotal").text(totalWithGST);

                //Set Hidden Field
                $("#hfAmount").val(totalWithGST);
                //console.log("Set HF: $" + totalWithGST);
            }
        }
        else {
            $("#ddlProvince").hide();
            $("#tbProvince").show();

            //total before tax
            var totalWithOutGST = GetTotalBeforeTax($("#tbAmount").val(), 15, 4.5);
            //console.log("total without gst: " + totalWithOutGST);
            totalWithOutGST = totalWithOutGST.toFixed(2);
            $("#intTotal").text(totalWithOutGST);
            //console.log("Final Total: " + totalWithPST);
            //Set Hidden Field
            $("#hfAmount").val(totalWithOutGST);
            $("#intShipping").text("4.50");
            $("#tbhShipping").val("4.50");
            $("#intGst").text("0");
            $("#tbhtax1").val("0");
            $("#intPst").text("0");
            $("#tbhtax2").val("0");

        }

        //update shipping and taxes
        if ($(this).val() == "Canada" || $(this).val() == "USA") {
            $("#intShipping").text("2.50");
            $("#tbhShipping").val("2.50");
        }
        else {
            $("#intShipping").text("4.50");
            $("#tbhShipping").val("4.50");
        }


    });
    //province dropdown change
    $("#ddlProvince").change(function() {
        //update taxes
        if ($(this).val() == "Quebec") {
            /****** Province is Quebec so we need to add GST and PST ******/
            //total before tax
            var totalWithOutGST = GetTotalBeforeTax($("#tbAmount").val(), 15, 2.5);
            //console.log("total without gst: " + totalWithOutGST);
            //GST
            var GST = (5 * totalWithOutGST) / 100;
            GST = GST;
            $("#intGst").text(GST.toFixed(2));
            $("#tbhtax1").val(GST.toFixed(2));
            //console.log("GST: " + GST);
            //Total with GST
            var totalWithGST = totalWithOutGST + GST;
            //console.log("total with gst: " + totalWithGST);
            //PST
            var PST = (9.5 * totalWithGST) / 100;
            $("#intPst").text(PST.toFixed(2));
            $("#tbhtax2").val(PST.toFixed(2));
            //console.log("PST: " + PST);
            //Total with PST (Final Total)
            var totalWithPST = totalWithGST + PST;
            totalWithPST = totalWithPST.toFixed(2);
            $("#intTotal").text(totalWithPST);
            //console.log("Final Total: " + totalWithPST);

            //Set Hidden Field
            $("#hfAmount").val(totalWithPST);
        }
        else {
            /****** Province NOT Quebec so we need to add ONLY GST ******/
            //total before tax
            var totalWithOutGST = GetTotalBeforeTax($("#tbAmount").val(), 15, 2.5);
            //console.log("total without gst: " + totalWithOutGST);
            //GST
            var GST = (5 * totalWithOutGST) / 100;
            GST = GST;
            $("#intGst").text(GST.toFixed(2));
            $("#tbhtax1").val(GST.toFixed(2));
            //console.log("GST: " + GST);
            //Total with GST (Final total)
            var totalWithGST = totalWithOutGST + GST;
            totalWithGST = totalWithGST.toFixed(2);
            //console.log("total with gst: " + totalWithGST);
            //PST
            $("#intPst").text("0");
            $("#tbhtax2").val("0");
            //console.log("PST: " + PST);
            //Total with PST (Final Total)
            $("#intTotal").text(totalWithGST);
            //console.log("Final Total: " + totalWithGST);

            //Set Hidden Field
            $("#hfAmount").val(totalWithGST);
        }
    });
   
    /************* copies textbox change ****************/
    $("#tbAmount").change(function () {
        if ($("#ddlCountry").val() == "Canada") {
            //show province ddl and hide tb
            //$("#ddlProvince").show().val("0");
            $("#tbProvince").hide();

            if ($("#ddlProvince").val() == "Quebec") {
                /****** Province is Quebec so we need to add GST and PST ******/
                //total before tax
                var totalWithOutGST = GetTotalBeforeTax($("#tbAmount").val(), 15, 2.5);
                //console.log("total without gst: " + totalWithOutGST);
                //GST
                var GST = (5 * totalWithOutGST) / 100;
                GST = GST;
                $("#intGst").text(GST.toFixed(2));
                $("#tbhtax1").val(GST.toFixed(2));
                //console.log("GST: " + GST);
                //Total with GST
                var totalWithGST = totalWithOutGST + GST;
                //console.log("total with gst: " + totalWithGST);
                //PST
                var PST = (9.5 * totalWithGST) / 100;
                $("#intPst").text(PST.toFixed(2));
                $("#tbhtax2").val(PST.toFixed(2));
                //console.log("PST: " + PST);
                //Total with PST (Final Total)
                var totalWithPST = totalWithGST + PST;
                totalWithPST = totalWithPST.toFixed(2);
                $("#intTotal").text(totalWithPST);
                //console.log("Final Total: " + totalWithPST);

                //Set Hidden Field
                $("#hfAmount").val(totalWithPST);

            }
            else {
                /****** Province NOT Quebec so we need to add ONLY GST ******/
                //total before tax
                var totalWithOutGST = GetTotalBeforeTax($("#tbAmount").val(), 15, 2.5);
                //console.log("total without gst: " + totalWithOutGST);
                //GST
                var GST = (5 * totalWithOutGST) / 100;
                GST = GST;
                $("#intGst").text(GST.toFixed(2));
                $("#tbhtax1").val(GST.toFixed(2));
                //console.log("GST: " + GST);
                //Total with GST (Final total)
                var totalWithGST = totalWithOutGST + GST;
                totalWithGST = totalWithGST.toFixed(2);
                //console.log("total with gst: " + totalWithGST);
                //PST
                $("#intPst").text("0");
                $("#tbhtax2").val("0");
                //console.log("PST: " + PST);
                //Total with PST (Final Total)
                $("#intTotal").text(totalWithGST);
                //console.log("Final Total: " + totalWithGST);

                //Set Hidden Field
                $("#hfAmount").val(totalWithGST);
            }
        }
        else {
            $("#ddlProvince").hide();
            $("#tbProvince").show();

            //total before tax
            var totalWithOutGST = GetTotalBeforeTax($("#tbAmount").val(), 15, 4.5);
            //console.log("total without gst: " + totalWithOutGST);
            totalWithOutGST = totalWithOutGST.toFixed(2);
            $("#intTotal").text(totalWithOutGST);
            //console.log("Final Total: " + totalWithOutGST);
            //Set Hidden Field
            $("#hfAmount").val(totalWithOutGST);
            $("#intShipping").text("4.50");
            $("#tbhShipping").val("4.50");
            $("#intGst").text("0");
            $("#tbhtax1").val("0");
            $("#intPst").text("0");
            $("#tbhtax2").val("0");

        }

        //update shipping and taxes
        if ($("#ddlCountry").val() == "Canada" || $(this).val() == "USA") {
            $("#intShipping").text("2.50");
            $("#tbhShipping").val("2.50");
        }
        else {
            $("#intShipping").text("4.50");
            $("#tbhShipping").val("4.50");
        }
    });
}

//Calculate Total before tax
function GetTotalBeforeTax(qty, price, shipping) {
    var tbt = (qty * price) + shipping;
    return tbt;
}
