﻿
function BlurText(vid, showtxt) {
    var txt = document.getElementById(vid);

    if (txt.value == "" || txt.value == showtxt) {
        txt.value = showtxt;
        txt.style.color = "#9B9A9A";
    }
    else {

        txt.style.color = "Black";
    }

}


function FocusText(vid, showtxt) {

    var txt = document.getElementById(vid);

    if (txt.value == showtxt) {
        txt.value = "";
        txt.style.color = "Black";
    }

}


function ChangeSort(vid) {

    var sortby = document.getElementById(vid).value;

    window.location.href = window.location.href + "?sort=" + sortby;
}

function FoldDiv(divid, imgid) {

    if (document.getElementById(divid).style.display == "none") {
        document.getElementById(divid).style.display = "block";
        document.getElementById(imgid).src = "../../Content/images/arrow2.jpg";
    }
    else {

        document.getElementById(divid).style.display = "none";
        document.getElementById(imgid).src = "../../Content/images/arrow1.jpg";

    }

}

function CountUp(vid, minnum, maxnum) {

    var num = parseInt(document.getElementById(vid).value);

    document.getElementById(vid).value = num + 1;

    if (parseInt(document.getElementById(vid).value) > maxnum)
        document.getElementById(vid).value = maxnum;
}

function CountDown(vid, minnum, maxnum) {

    var num = parseInt(document.getElementById(vid).value);

    document.getElementById(vid).value = num - 1;
    if (parseInt(document.getElementById(vid).value) < minnum)
        document.getElementById(vid).value = minnum;
}

function DecimalOnly(vid) {

    var reg = /^\d+(\.\d{2})?$/;
    if (reg.test(document.getElementById(vid).value) == false)
        document.getElementById(vid).value = "0";
}

function NumberOnly(vid, minnum, maxnum) {

    var reg = /^[0-9]*$/;

    if (reg.test(document.getElementById(vid).value) == false) {
        document.getElementById(vid).value = "0";
        return false;
    }
    else if (parseInt(document.getElementById(vid).value) > maxnum) {
        document.getElementById(vid).value = maxnum;
        return true;
    }
    else if (parseInt(document.getElementById(vid).value) < minnum) {


        document.getElementById(vid).value = minnum;
        return true;
    }
    else
        return true;

}

function GetTotal(totalid, cuttingid, cuttingnumid, totalplusid, sqftperbox, price, passdoc) {


    var total = parseInt(passdoc.getElementById(totalid).value);
    var cutting = parseInt(passdoc.getElementById(cuttingid).value);

    var cuttingnum = Math.round((total * cutting * 0.01) * 100) / 100;

    passdoc.getElementById(cuttingnumid).innerHTML = cuttingnum;

    passdoc.getElementById(totalplusid).innerHTML = total + cuttingnum;

    var totalsqft = total + cuttingnum;

    passdoc.getElementById("resultspan").innerHTML = "";

    GetBoxesNum(totalsqft, sqftperbox, price, passdoc);
}

function GetBoxesNum(totalsqft, sqftperbox, price, passdoc) {

    if (passdoc == "undefined" || passdoc == null)
        passdoc = document;

    var unitperbox = passdoc.getElementById(sqftperbox).value;

    var unitprices = passdoc.getElementById(price).value;

    var totalbox = 0;

    if (totalsqft == 0) {

        passdoc.getElementById("boxnum").innerHTML = "0";
    }
    else {

        passdoc.getElementById("boxnum").innerHTML = Math.round((totalsqft / unitperbox) * 100) / 100;

        if ((totalsqft % unitperbox) > 0) {
            totalbox = parseInt(totalsqft / unitperbox) + 1;
        }
        else {
            totalbox = parseInt(totalsqft / unitperbox);
        }
    }

    passdoc.getElementById("adjustboxnum").value = totalbox;

    CalculatePrice(unitperbox, unitprices, passdoc);
}

//function AddRoom() {

//    var roomnum = 0;

//    if (document.getElementById("roomnum").value == "")
//        roomnum = 0;
//    else
//        roomnum = parseInt(document.getElementById("roomnum").value);

//    if (roomnum > 0) {

//        var appstr = "";

//        for (var i = 0; i < roomnum; i++) {
//            appstr += "<div id=\"div"+i.toString()+"\" style='margin-top:5px;' ><input type=\"text\" id=\"roomx" + i.toString() + "\" onchange=\"javascript:DecimalOnly(this.id); GetSqft('roomx" + i.toString() + "','roomy" + i.toString() + "','room" + i.toString() + "');\" style=\"border:solid 1px Black; width:70px; height:15px;\" /> FT * <input type=\"text\" id=\"roomy" + i.toString()
//                    + "\" onchange=\"javascript:DecimalOnly(this.id);GetSqft('roomx" + i.toString() + "','roomy" + i.toString() + "','room" + i.toString() + "');\" style=\"border:solid 1px Black; width:70px; height:15px;\" /> FT = <input type=\"text\" readonly='readonly' id=\"room" + i.toString() + "\" style=\"border:solid 1px Black; width:70px; height:15px;\" /> SQFT </div><br/>";
//        }

//        document.getElementById("rooms").innerHTML = appstr;
//    }
//}


function GetSqft(vid) {

    var totalrooms = 0;

    var room = 0;

    var feetx = 0;
    var feety = 0;

    var feet = 0;

    for (var i = 1; i < 7; i++) {

        feetx = 0;
        feety = 0;

        if (document.getElementById("feetx" + i.toString()).value != "" && document.getElementById("feety" + i.toString()).value != "") {
            feetx = parseInt(document.getElementById("feetx" + i.toString()).value);
            feety = parseInt(document.getElementById("feety" + i.toString()).value);
        }

        if (document.getElementById("inchesx" + i.toString()).value != "" && document.getElementById("inchesx" + i.toString()).value != "0") {
            feetx += parseFloat(document.getElementById("inchesx" + i.toString()).value / 12);
        }

        if (document.getElementById("inchesy" + i.toString()).value != "" && document.getElementById("inchesy" + i.toString()).value != "0") {
            feety += parseFloat(document.getElementById("inchesy" + i.toString()).value / 12);
        }


        room = Math.round(feetx * feety * 100) / 100;

        document.getElementById("room" + i.toString()).innerHTML = room;

        totalrooms += room;

    }

    document.getElementById(vid).innerHTML = totalrooms;
}


function NextStep(previd, nextid) {

    document.getElementById(nextid).style.display = "block";
    document.getElementById(previd).style.display = "none";

    document.getElementById("process1").style.width = "50%";
    document.getElementById("step2").style.color = "#BE1E2D";
}

function CalculatePrice(sqftperbox, price, passdoc) {

    if (passdoc == "undefined" || passdoc == null)
        passdoc = document;

    var totalbox = parseInt(passdoc.getElementById("adjustboxnum").value);
    var totalsqft = Math.round(totalbox * sqftperbox * 100) / 100;

    var totalprice = Math.round(totalbox * sqftperbox * price * 100) / 100;

    passdoc.getElementById("adjusttotalsqft").innerHTML = totalsqft;

    passdoc.getElementById("boxesprice").innerHTML = totalprice;

    passdoc.getElementById("productprice").innerHTML = totalprice;

    passdoc.getElementById("grandtotal").innerHTML = totalprice;

    passdoc.getElementById("total").innerHTML = totalprice;

}

function GoPrev(previd, nextid) {

    document.getElementById(previd).style.display = "block";
    document.getElementById(nextid).style.display = "none";

    document.getElementById("process1").style.width = "25%";
    document.getElementById("step2").style.color = "#72320F";
    document.getElementById("step3").style.color = "#72320F";
    document.getElementById("step4").style.color = "#72320F";
}

function Reset(vid, totalid) {
    var divContainer = document.getElementById(vid);
    var objs = divContainer.getElementsByTagName('INPUT');
    for (var i = 0; i < objs.length; i++) {
        if (objs[i].type == "text")
            objs[i].value = "0";
    }

    for (var i = 1; i <= 6; i++) {
        document.getElementById("room" + i.toString()).innerHTML = "0.0";
    }

    document.getElementById(totalid).innerHTML = "0.0";

}

function GetResult(vid, sqftperbox, price, passdoc) {

    if (passdoc == null || passdoc == "undefined")
        passdoc = document;

    var total = 0;

    if (passdoc.getElementById(vid).value != "") {

        total = parseFloat(passdoc.getElementById(vid).value);

        passdoc.getElementById("sqfts").value = total;
        //passdoc.getElementById("resultspan").innerHTML = "Calculate result is <b style='color:Red;'>" + total + " sqft.</b>";
    }
    // alert(total);

    GetTotal('sqfts', 'cutting', 'cuttingnum', 'totalsqft', 'unitsqft', 'unitprice', passdoc);
    // GetBoxesNum(total, sqftperbox, price,passdoc);
}

function ReturnResult(resultid, returnid) {

    if (document.getElementById(resultid).innerHTML != "" && document.getElementById(resultid).innerHTML != "0.0" && document.getElementById(resultid).innerHTML != "0") {

        window.opener.document.getElementById(returnid).value = document.getElementById(resultid).innerHTML;
        window.opener.document.getElementById("sqfts").value = document.getElementById(resultid).innerHTML;
        GetResult('calculateresult', 'unitsqft', 'unitprice', window.opener.document);


        //    window.opener.document.getElementById("cuttingnum").innerHTML="0";
        //    window.opener.document.getElementById("totalsqft").innerHTML="0";

        window.close();
    }
}

function Cal() {

    var destZip = $('#zipcodebox').val();

    if (destZip != "") {

        var weight = $('#weight').val();
        var quantity = $('#quantityperbox').val();
        var box = $('#adjustboxnum').val();
        var commodityclass = $('#commodityclass').val();
        var totalsqft = quantity * box;

        var totalweight = Math.round(weight * quantity * box);

        if (totalweight <= 0 || commodityclass == "") {

            $('#noweight').attr("innerHTML", "No Weight");
            return;
        }

        $('#noweight').attr("innerHTML", "");
        document.getElementById("loading").style.display = "block";
        var insidecharge = 0;
        var residential = 0;

        var skuid = $('#ctl_hid_skuid').val();
        var skutype = $('#ctl_hid_skutype').val();

        $.ajax({
            url: "/AJAXSelectData/GetInfo?zipcode=" + destZip + "&totalweight=" + totalweight + "&commodityclass=" + commodityclass + "&totalsqft=" + totalsqft + "&skuid=" + skuid + "&skutype=" + skutype,
            type: "post",
            dataType: 'json',
            success: function(data, status) {

                $.each(data, function(index, item) {

                    if (item == null) {

                        document.getElementById("loading").style.display = "none";
                        $('#noweight').attr("innerHTML", "Invalid Zipcode");
                    }
                    else {

                        if (item.charge == "CONSTRUCTIONDELIVERY") {
                            $('#method2').attr("innerHTML", item.currentvalue);
                        }
                        if (item.charge == "RESIDENTIALDELIVERY") {
                            $('#method3').attr("innerHTML", item.currentvalue);
                            residential = item.currentvalue;
                        }
                        if (item.charge == "LIFTGATEGROUNDDELIVERY") {
                            $('#method4').attr("innerHTML", item.currentvalue);
                        }
                        if (item.charge == "INSIDEDELIVERY") {
                            insidecharge = item.currentvalue;
                        }
                    }

                });

                $('#method5').attr("innerHTML", Math.round((parseFloat(insidecharge) + parseFloat(residential)) * 100) / 100);

                document.getElementById("loading").style.display = "none";
            }

        });

    }
}


function NoDelivery(vid) {

    if (document.getElementById(vid).checked == true) {
        var objs = document.getElementsByTagName('INPUT');
        for (var i = 0; i < objs.length; i++) {
            if (objs[i].type == "checkbox")
                objs[i].checked = false;
        }

        document.getElementById(vid).checked = true;

        var product = parseFloat(document.getElementById("productprice").innerHTML);
        var delivery = 0;
        document.getElementById("deliveryprice").innerHTML = delivery;
        document.getElementById("grandtotal").innerHTML = (product + delivery);
        document.getElementById("total").innerHTML = document.getElementById("grandtotal").innerHTML;
    }
    else {

        document.getElementById(vid).checked == false;

        document.getElementById("deliveryprice").innerHTML = "0";
        document.getElementById("grandtotal").innerHTML = document.getElementById("productprice").innerHTML;
        document.getElementById("total").innerHTML = document.getElementById("grandtotal").innerHTML;
    }

}

function AddDelivery(vid, optionid) {

    if (document.getElementById(vid).checked == true) {
        var objs = document.getElementsByTagName('INPUT');
        for (var i = 0; i < objs.length; i++) {
            if (objs[i].type == "checkbox")
                objs[i].checked = false;
        }

        document.getElementById("process1").style.width = "75%";
        document.getElementById("step3").style.color = "#BE1E2D";
        document.getElementById("step4").style.color = "#72320F";
        document.getElementById(vid).checked = true;

        var product = parseFloat(document.getElementById("productprice").innerHTML);
        var delivery = parseFloat(document.getElementById(optionid).innerHTML);

        document.getElementById("deliveryprice").innerHTML = delivery;
        document.getElementById("grandtotal").innerHTML = (Math.round((product + delivery) * 100) / 100);
        document.getElementById("total").innerHTML = document.getElementById("grandtotal").innerHTML;
    }
    else {

        document.getElementById(vid).checked == false;

        document.getElementById("process1").style.width = "50%";
        document.getElementById("step3").style.color = "#72320F";
        document.getElementById("step4").style.color = "#72320F";

        document.getElementById("deliveryprice").innerHTML = "0";
        document.getElementById("grandtotal").innerHTML = document.getElementById("productprice").innerHTML;
        document.getElementById("total").innerHTML = document.getElementById("grandtotal").innerHTML;

    }
}

function GoCheckOut() {

    document.getElementById("process1").style.width = "100%";
    document.getElementById("step4").style.color = "#BE1E2D";
    document.getElementById("step3").style.color = "#BE1E2D";
}


