﻿var DebugMode = false;
//GST in percentage
var curGST = 0.125;
var newGST = 0.15;

function onTab1Click() {
    $("#tab1").attr("style", "display:block");
    $("#tab2").attr("style", "display:none");
    $("#tab3").attr("style", "display:none");
}

function onTab2Click() {
    $("#tab2").attr("style", "display:block");
    $("#tab1").attr("style", "display:none");
    $("#tab3").attr("style", "display:none");
}

function onTab3Click() {
    $("#tab3").attr("style", "display:block");
    $("#tab1").attr("style", "display:none");
    $("#tab2").attr("style", "display:none");
}

//For tab2 page, Two formulas, 1 for inc gst selected another for ex gst selected.
//display nothing if none selected
function Calculate() {
	
    if (DebugMode) {
        alert($("#tab2").find("#radio_Inclusive").is(":checked"));
    }

    var inputPrice = 0; //input
    var newPricePoint = 0; //new price point
    
    var currentPricePlusGST = 0;
    var currentPriceWithoutGST = 0;
    var currentGSTPrice = 0;
    var newPricePlusGST = 0;
    var newPriceWithoutGST = 0;
    var newGSTPrice = 0;

    var plusGSTPriceDiff = 0;
    var withoutGSTPriceDiff = 0;
    var GSTPriceDiff = 0;

    //get currentPirce from current price text box
    inputPrice = parseFloat($("#tb_currentPrice").val());
    //check if the input value is currency
    //set error msg if false
    if (isCurrency(inputPrice.toFixed(2))) {
        //clear error msg
        $("#errorMsg_currentPrice").text("");
        //if current price is 0, clear output.
        if (inputPrice == 0) {
            ClearOutput();
            return;
        }
        else {
            //Set cuurent price to 2dicimal
            $("#tb_currentPrice").val(inputPrice.toFixed(2));
        }

        if ($("#tab2").find("#radio_Inclusive").is(":checked")) {
            //if choose pass gst change to customer
            if ($("#tab2").find("#radio_PassGST").is(":checked")) {
                currentPricePlusGST = inputPrice;
                currentPriceWithoutGST = currentPricePlusGST / (1 + curGST);
                currentGSTPrice = currentPricePlusGST - currentPriceWithoutGST;

                newPriceWithoutGST = currentPriceWithoutGST;
                newGSTPrice = newPriceWithoutGST * newGST;
                newPricePlusGST = newPriceWithoutGST + newGSTPrice;

                plusGSTPriceDiff = newPricePlusGST - currentPricePlusGST;
                withoutGSTPriceDiff = newPriceWithoutGST - currentPriceWithoutGST;
                GSTPriceDiff = newGSTPrice - currentGSTPrice;

                ShowResultText("IncPassText");
            } //if choose absorb the gst change
            else if ($("#tab2").find("#radio_AbsorbGST").is(":checked")) {
                currentPricePlusGST = inputPrice;
                currentPriceWithoutGST = currentPricePlusGST / (1 + curGST);
                currentGSTPrice = currentPricePlusGST - currentPriceWithoutGST;

                newPricePlusGST = currentPricePlusGST;
                newPriceWithoutGST = newPricePlusGST / (1 + newGST);
                newGSTPrice = newPricePlusGST - newPriceWithoutGST;

                plusGSTPriceDiff = newPricePlusGST - currentPricePlusGST;
                withoutGSTPriceDiff = newPriceWithoutGST - currentPriceWithoutGST;
                GSTPriceDiff = newGSTPrice - currentGSTPrice;

                ShowResultText("IncAbsText");
            } //if choose new price point
            else {
                //get currentPirce from current price text box
                newPricePoint = parseFloat($("#tb_newPricePoint").val());
                if (isCurrency(newPricePoint.toFixed(2))) {
                    //clear error msg
                    $("#errorMsg_newPricePoint").text("");
                    //if current price is 0, clear output.
                    if (newPricePoint == 0) {
                        ClearOutput();
                        return;
                    }
                    else {
                        //Set cuurent price to 2dicimal
                        $("#tb_newPricePoint").val(newPricePoint.toFixed(2));
                    }
                    currentPricePlusGST = inputPrice;
                    currentPriceWithoutGST = currentPricePlusGST / (1 + curGST);
                    currentGSTPrice = currentPricePlusGST - currentPriceWithoutGST;

                    newPricePlusGST = newPricePoint;
                    newPriceWithoutGST = newPricePlusGST / (1 + newGST);
                    newGSTPrice = newPricePlusGST - newPriceWithoutGST;

                    plusGSTPriceDiff = newPricePlusGST - currentPricePlusGST;
                    withoutGSTPriceDiff = newPriceWithoutGST - currentPriceWithoutGST;
                    GSTPriceDiff = newGSTPrice - currentGSTPrice;

                    ShowResultText("IncNewText");
                }
                else {
                    $("#errorMsg_newPricePoint").text("");
                    HideResultText();
                    ClearOutput();
                    return;
                }
            }
        }
        else if ($("#tab2").find("#radio_Exclusive").is(":checked")) {
            //if choose pass gst change to customer
            if ($("#tab2").find("#radio_PassGST").is(":checked")) {
                currentPriceWithoutGST = inputPrice;
                currentPricePlusGST = currentPriceWithoutGST * (1 + curGST);
                currentGSTPrice = currentPricePlusGST - currentPriceWithoutGST;

                newPriceWithoutGST = currentPriceWithoutGST;
                newGSTPrice = newPriceWithoutGST * newGST;
                newPricePlusGST = newPriceWithoutGST + newGSTPrice;

                plusGSTPriceDiff = newPricePlusGST - currentPricePlusGST;
                withoutGSTPriceDiff = newPriceWithoutGST - currentPriceWithoutGST;
                GSTPriceDiff = newGSTPrice - currentGSTPrice;

                ShowResultText("ExPassText");
            } //if choose absorb the gst change
            else if ($("#tab2").find("#radio_AbsorbGST").is(":checked")) {
                currentPriceWithoutGST = inputPrice;
                currentPricePlusGST = currentPriceWithoutGST * (1 + curGST);
                currentGSTPrice = currentPricePlusGST - currentPriceWithoutGST;

                newPricePlusGST = currentPricePlusGST;
                newPriceWithoutGST = newPricePlusGST / (1 + newGST);
                newGSTPrice = newPricePlusGST - newPriceWithoutGST;

                plusGSTPriceDiff = newPricePlusGST - currentPricePlusGST;
                withoutGSTPriceDiff = newPriceWithoutGST - currentPriceWithoutGST;
                GSTPriceDiff = newGSTPrice - currentGSTPrice;

                ShowResultText("ExAbsText");
            } //if choose new price point
            else {
                //get currentPirce from current price text box
                newPricePoint = parseFloat($("#tb_newPricePoint").val());
                if (isCurrency(newPricePoint.toFixed(2))) {
                    //clear error msg
                    $("#errorMsg_newPricePoint").text("");
                    //if current price is 0, clear output.
                    if (newPricePoint == 0) {
                        ClearOutput();
                        return;
                    }
                    else {
                        //Set cuurent price to 2dicimal
                        $("#tb_newPricePoint").val(newPricePoint.toFixed(2));
                    }
                    currentPriceWithoutGST = inputPrice;
                    currentPricePlusGST = currentPriceWithoutGST * (1 + curGST);
                    currentGSTPrice = currentPricePlusGST - currentPriceWithoutGST;

                    newPriceWithoutGST = newPricePoint;
                    newPricePlusGST = newPriceWithoutGST * (1 + newGST);
                    newGSTPrice = newPricePlusGST - newPriceWithoutGST;

                    plusGSTPriceDiff = newPricePlusGST - currentPricePlusGST;
                    withoutGSTPriceDiff = newPriceWithoutGST - currentPriceWithoutGST;
                    GSTPriceDiff = newGSTPrice - currentGSTPrice;

                    ShowResultText("ExNewText");
                }
                else {
                    $("#errorMsg_newPricePoint").text("");
                    HideResultText();
                    ClearOutput();
                    return;
                }
            }
        }
        else {
        }        
    }
    else {
        $("#errorMsg_currentPrice").text("");
        ClearOutput();
        return;
    }

    $(".current_IncGST_Price").text("$" + currentPricePlusGST.toFixed(2));
    $(".new_IncGST_Price").text("$" + newPricePlusGST.toFixed(2));
    $(".current_ExGST_Price").text("$" + currentPriceWithoutGST.toFixed(2));
    $(".new_ExGST_Price").text("$" + newPriceWithoutGST.toFixed(2));
    $(".current_GST").text("$" + currentGSTPrice.toFixed(2));
    $(".new_GST").text("$" + newGSTPrice.toFixed(2));
    $(".diff_IncGSTPrice").text("$" + plusGSTPriceDiff.toFixed(2));
    $(".diff_GSTPrice").text("$" + GSTPriceDiff.toFixed(2));
    $(".diff_ExGSTPrice").text("$" + withoutGSTPriceDiff.toFixed(2));

    $(".diff_IncGSTPrice_image").empty();
    if (plusGSTPriceDiff > 0) {
        $(".diff_IncGSTPrice").attr("style", "color: green");
        //$(".diff_IncGSTPrice_image").append("<img alt=\"\" src=\"images/greenTri.png\" />");
		$(".diff_IncGSTPrice_image").append("<div class='greenTri'>&nbsp;</div>");
    }
    else if (plusGSTPriceDiff < 0) {
        $(".diff_IncGSTPrice").attr("style", "color: red");
        //$(".diff_IncGSTPrice_image").append("<img alt=\"\" src=\"images/redTri.png\" />");
		$(".diff_IncGSTPrice_image").append("<div class='redTri'>&nbsp;</div>");
    }
    else {
        $(".diff_IncGSTPrice").removeAttr("style");
    }
    
    $(".diff_GSTPrice_image").empty();
    if (GSTPriceDiff > 0) {
        $(".diff_GSTPrice").attr("style", "color: green");
        //$(".diff_GSTPrice_image").append("<img alt=\"\" src=\"images/greenTri.png\" />");
		$(".diff_GSTPrice_image").append("<div class='greenTri'>&nbsp;</div>");
    }
    else if (GSTPriceDiff < 0) {
        $(".diff_GSTPrice").attr("style", "color: red");
        //$(".diff_GSTPrice_image").append("<img alt=\"\" src=\"images/redTri.png\" />");
		$(".diff_GSTPrice_image").append("<div class='redTri'>&nbsp;</div>");
    }
    else {
        $(".diff_GSTPrice").removeAttr("style");
    }

    $(".diff_ExGSTPrice_image").empty();
    if (withoutGSTPriceDiff > 0) {
        $(".diff_ExGSTPrice").attr("style", "color: green");
        $(".diff_ExGSTPrice_image").append("<div class='greenTri'>&nbsp;</div>");
		
        $(".diff_GSTPriceText1").text("a higher");
    }
    else if (withoutGSTPriceDiff < 0) {
        $(".diff_ExGSTPrice").attr("style", "color: red");
        //$(".diff_ExGSTPrice_image").append("<img alt=\"\" src=\"images/redTri.png\" />");
		$(".diff_ExGSTPrice_image").append("<div class='redTri'>&nbsp;</div>");
        $(".diff_GSTPriceText1").text("a lower");
    }
    else {
        $(".diff_ExGSTPrice").removeAttr("style");
        $(".diff_GSTPriceText1").text("the same");
    }
}

//1 radio button group should have and only have 1 radio button checked
function ValidateRadioButtons() {
    var isValid = false;

    if ($("#tab2").find("#radio_Inclusive").is(":checked")) {
        if ($("#tab2").find("#radio_PassGST").is(":checked")) {
            $("#newPricePoint").hide();
            Calculate();
        }
        else if ($("#tab2").find("#radio_AbsorbGST").is(":checked")) {
            $("#newPricePoint").hide();
            Calculate();
        }
        else if ($("#tab2").find("#radio_ChooseNewPrice").is(":checked")) {
            $("#newPricePoint").show();
            Calculate();
        }
        else {
            return false;
        }
    }
    else if ($("#tab2").find("#radio_Exclusive").is(":checked")) {
        if ($("#tab2").find("#radio_PassGST").is(":checked")) {
            $("#newPricePoint").hide();
            Calculate();
        }
        else if ($("#tab2").find("#radio_AbsorbGST").is(":checked")) {
            $("#newPricePoint").hide();
            Calculate();
        }
        else if ($("#tab2").find("#radio_ChooseNewPrice").is(":checked")) {
            $("#newPricePoint").show();
            Calculate();
        }
        else {
            return false;
        }
    }
    else {
        return false;
    } 
    
    return true;
}

//show text result with the property ID
//result header and table will always be shown
function ShowResultText(id) {
    HideResultText();
    $(".result").show();
    $("#" + id).show();
}

//hide all result texts
function HideResultText() {
    $(".result").hide();
    $(".resultText").hide();
}

//Clear all the output, set them to "-"
//triggered when current price input is in wrong format or 0
function ClearOutput() {
    HideResultText();
    //$("#tb_currentPrice").val("-");
    $(".current_ExGST_Price").text("-");
    $(".new_ExGST_Price").text("-");
    $(".current_GST").text("-");
    $(".new_GST").text("-");
    $(".current_IncGST_Price").text("-");
    $(".new_IncGST_Price").text("-");
    $(".diff_IncGSTPrice").text("-");
    $(".diff_GSTPrice").text("-");
    $(".diff_ExGSTPrice").text("-");
    //$("#tb_newPricePoint").val("-");
    $(".diff_IncGSTPrice_image").empty();
    $(".diff_GSTPrice_image").empty();
    $(".diff_ExGSTPrice_image").empty();
    //$(".diff_IncGSTPrice").attr("style", "color: black");
    //$(".diff_GSTPrice").attr("style", "color: black");
    //$(".diff_ExGSTPrice").attr("style", "color: black");
    $(".diff_IncGSTPrice").removeAttr("style");
    $(".diff_GSTPrice").removeAttr("style");
    $(".diff_ExGSTPrice").removeAttr("style");
}

// Check if string is currency
var isCurrency_re = /^\s*(\+)?((\d+(\.\d\d)?)|(\.\d\d))\s*$/;
function isCurrency(s) {
    return String(s).search(isCurrency_re) != -1
}

/*
instead of using .tofixed(2) we can use round. 
.toFixed() is the usual Javascript 1.5 method which works in all browsers from IE5.5 and NS6 upwards.

Another (older) way is:-

1) Multiple the original number by 10^x (10 to the power of x)
2) Apply Math.round() to the result
3) Divide result by 10^x

var original = 28.4531; 
//round "original" to two decimals
var result = Math.round(original*100)/100 //returns 28.45
*/
function ParseToTwoDicimal(number) {
    var result = Math.round(number * 100) / 100;
    return result;
}

/***********************************
* (Public) onlyNumbers
***********************************/
function onlyNumbers(event) {
    if ((event.keyCode < 45 && event.keyCode != 13) || event.keyCode > 57) event.returnValue = false;
}

   $(document).ready(function() {
            $(".tip").tooltip({
                effect: 'slide',
                position: "center right",
                relative: true
            });
            HideResultText();
        });
