﻿$(document).ready(function() {
    var tabCount = 0;

    $("#navbars li").click(function() {


        if ($.trim($(this).attr("id")) == "live") {
            window.location = "products.aspx";
        } else if ($.trim($(this).attr("id")) == "frozen") {
            window.location = "products.aspx?tab=2";
        } else if ($.trim($(this).attr("id")) == "liveNZ") {
            window.location = "NZProducts.aspx";
        } else if ($.trim($(this).attr("id")) == "frozenNZ") {
            window.location = "NZProducts.aspx?tab=2";
        }
    });

    $("#menu li").hover(function() {
        $(this).addClass("hover");
    }, function() {
        $(this).removeClass("hover");
    });

    $("#menu2 li").hover(function() {
        $(this).addClass("hover");
    }, function() {
        $(this).removeClass("hover");
    });

    $("#tabs li").each(function() {

        if (!$(this).hasClass("no")) {
            $(this).html("<div class='tableft'></div><div class='tabtext'>" + $(this).html() + "</div><div class='tabright'></div>");
            tabCount++;
            $(this).attr("id", "tab" + tabCount);
        }
        else {
            $(this).html("<div class='tableftgray'></div><div class='tabtextgray'>" + $(this).html() + "</div><div class='tabrightgray'></div>");
            tabCount++;
            $(this).attr("id", "tab" + tabCount);
        }
    });

    $("#tabs li").hover(function() {
        if (!$(this).hasClass("no")) {
            if (!$(this).hasClass("hold")) {
                tabOver($(this));
            }
        }
    }, function() {
        if (!$(this).hasClass("no")) {
            if (!$(this).hasClass("hold")) {
                tabOut($(this));
            }
        }
    });

    $("#tabs li").click(function() {

        if (!$(this).hasClass("no")) {
            //Hide all tabPanels
            $(".tabpanel").fadeOut(300);

            var activeTab = $(this).attr("id");
            $("#" + activeTab + "panel").animate({ opacity: 1.0 }, 300).fadeIn(250);

            //Remove active tab colors from all tabs
            $("#tabs li").each(function() {
                $(this).removeClass("hold");
                tabOut($(this));
            });
            //Set active colors to selected tab.
            $(this).addClass("hold");
            tabOver($(this));
        }
    });

    if ($.trim($("#thetab").html()) == "tab2") {
        $("#tab2").click();
    }
    else {
        $("#tabs li:first").click();
    }

    $(".contactbtn").css("width", "80px");
    $(".contactbtn").click(function() {

        var isComplete = false;
        var problem = 0;

        $("#contacterror").html("<strong>Please correct the following to proceed</strong><br />");

        if ($("#ctl00_ContentPlaceHolder1_txtName").val() == "") {
            problem = 1;
            $("#contacterror").removeClass("hide");
            $("#contacterror").html($("#contacterror").html() + "Name is a required field.<br />");
        }
        if ($("#ctl00_ContentPlaceHolder1_txtEmail").val() == "") {
            problem = 1;
            $("#contacterror").removeClass("hide");
            $("#contacterror").html($("#contacterror").html() + "Email is a required field.<br />");
        }
        if ($("#ctl00_ContentPlaceHolder1_txtMsg").val() == "") {
            problem = 1;
            $("#contacterror").removeClass("hide");
            $("#contacterror").html($("#contacterror").html() + "Message is a required field.<br />");
        }
        if ($("#ctl00_ContentPlaceHolder1_txtEmail").val() != $("#ctl00_ContentPlaceHolder1_txtEmail2").val()) {
            problem = 1;
            $("#contacterror").removeClass("hide");
            $("#contacterror").html($("#contacterror").html() + "Email address fields do not match.<br />");
        }

        if (problem != 1) {
            isComplete = true;
        }

        return isComplete;
    });

    $("#btnSubmitProducts").click(function() {

        if ($.trim($("#initialtotal").html()) != "$0.00") {

            $("#tab2").removeClass("no");
            $("#tab2").each(function() {
                var text = $(this).children(".tabtextgray").html();
                $(this).html("<div class='tableft'></div><div class='tabtext'>" + text + "</div><div class='tabright'></div>");
            });

            $("#tabs li").hover(function() {
                if (!$(this).hasClass("no")) {
                    if (!$(this).hasClass("hold")) {
                        tabOver($(this));
                    }
                }
            }, function() {
                if (!$(this).hasClass("no")) {
                    if (!$(this).hasClass("hold")) {
                        tabOut($(this));
                    }
                }
            });

            $("#tab2").click(function() {

                var activeTab = $(this).attr("id");
                $("#" + activeTab + "panel").animate({ opacity: 1.0 }, 300).fadeIn(250);

                //Remove active tab colors from all tabs
                $("#tabs li").each(function() {
                    $(this).removeClass("hold");
                    tabOut($(this));
                });
                //Set active colors to selected tab.
                $(this).addClass("hold");
                tabOver($(this));
            });
            $("#tab2").click();
        }
        else {
            alert("Please select a product too proceed.");
        }
    });

    $(".submitsale_1").click(function() {

        if ($("#ctl00_ContentPlaceHolder1_cbxAgree:checked").length > 0) {

            var problem = 0;

            $("#contacterror").addClass("hide");
            $("#contacterror").html("<strong>Oops, there is a problem. Please correct the following to proceed</strong><br />");

            if ($("#ctl00_ContentPlaceHolder1_txtContactName").val() == "") {
                problem = 1;
                $("#contacterror").removeClass("hide");
                $("#contacterror").html($("#contacterror").html() + "Name is a required field.<br />");
            }
            if ($("#ctl00_ContentPlaceHolder1_txtStreetAddress").val() == "" ||
            $("#ctl00_ContentPlaceHolder1_txtTown").val() == "" ||
            $("#ctl00_ContentPlaceHolder1_txtPostCode").val() == "") {
                problem = 1;
                $("#contacterror").removeClass("hide");
                $("#contacterror").html($("#contacterror").html() + "Street Address, Town/City and Post Code are required.<br />");
            }
            if ($("#ctl00_ContentPlaceHolder1_txtEmail").val() == "") {
                problem = 1;
                $("#contacterror").removeClass("hide");
                $("#contacterror").html($("#contacterror").html() + "Email is a required field.<br />");
            }
            if ($("#ctl00_ContentPlaceHolder1_txtPhone").val() == "") {
                problem = 1;
                $("#contacterror").removeClass("hide");
                $("#contacterror").html($("#contacterror").html() + "Contact Phone is a required field.<br />");
            }
            if ($("#ctl00_ContentPlaceHolder1_txtEmail").val() != $("#ctl00_ContentPlaceHolder1_txtEmail2").val()) {
                problem = 1;
                $("#contacterror").removeClass("hide");
                $("#contacterror").html($("#contacterror").html() + "Email address fields do not match.<br />");
            }

            if (problem != 1) {

                $("#tab3").removeClass("no");
                $("#tab3").each(function() {
                    var text = $(this).children(".tabtextgray").html();
                    $(this).html("<div class='tableft'></div><div class='tabtext'>" + text + "</div><div class='tabright'></div>");
                });

                $("#tabs li").hover(function() {
                    if (!$(this).hasClass("no")) {
                        if (!$(this).hasClass("hold")) {
                            tabOver($(this));
                        }
                    }
                }, function() {
                    if (!$(this).hasClass("no")) {
                        if (!$(this).hasClass("hold")) {
                            tabOut($(this));
                        }
                    }
                });

                $("#tab3").click(function() {

                    var activeTab = $(this).attr("id");
                    $("#" + activeTab + "panel").animate({ opacity: 1.0 }, 300).fadeIn(250);

                    //Remove active tab colors from all tabs
                    $("#tabs li").each(function() {
                        $(this).removeClass("hold");
                        tabOut($(this));
                    });
                    //Set active colors to selected tab.
                    $(this).addClass("hold");
                    tabOver($(this));
                });

                $("#tab3").click();
                $(".btnSubmitSale").click();
            }
        } else {
            alert('Sorry, we cannot process your order unless you agree to the terms regarding delivery.');
        }


    });

    $(".ddlsize").change(function() {

        var price = $("option:selected", this).val();
        var row = $(this).parent().parent();
        var quantity = $(".quantity", row).val();
        var total = parseFloat(price).toFixed(2) * parseFloat(quantity).toFixed(2);

        $(".price", row).val(parseFloat(price).toFixed(2));
        $(".totalprice", row).val(parseFloat(total).toFixed(2));
        addTotal();
    });

    $(".quantity").change(function() {

        var row = $(this).parent().parent();
        var price = $(".ddlsize option:selected").val();
        var quantity = $(this).val();
        var total = parseFloat(price).toFixed(2) * parseFloat(quantity).toFixed(2);

        $(".totalprice", row).val(parseFloat(total).toFixed(2));
        addTotal();
    });


    checkoutBtn();

    $("#addrow").click(function() {
        if ($("#row6").hasClass("hide")) { $("#row6").removeClass("hide"); }
        else if ($("#row7").hasClass("hide")) $("#row7").removeClass("hide");
        else if ($("#row8").hasClass("hide")) $("#row8").removeClass("hide");
        else if ($("#row9").hasClass("hide")) {
            $("#row9").removeClass("hide");
            $("#addrow").addClass("hide");
        }
    });

    $(".datereq").datepicker({ dateFormat: 'dd/mm/yy' });

    $("#videolist li").hover(function() {
        $(this).addClass("vidhover");
    }, function() {
        $(this).removeClass("vidhover");
    });

    $("#videolist li").click(function() {

        $(this).children(".hide").click();

    });

});

function tabOver(obj) {
    obj.addClass("hover");
    obj.children(".tableft").css("background", "url(Assets/CSS/Images/fulltableft.png) no-repeat top left");
    obj.children(".tabright").css("background", "url(Assets/CSS/Images/fulltabright.png) no-repeat top left");
}

function tabOut(obj) {
    obj.removeClass("hover");
    obj.children(".tableft").css("background", "url(Assets/CSS/Images/tableft.png) no-repeat top left");
    obj.children(".tabright").css("background", "url(Assets/CSS/Images/tabright.png) no-repeat top left");
}

function getSubtotal() {
    var price = $.trim($(".ddllength option:selected").val());
    var quantity = $.trim($(".quantity option:selected").val());

    var subTotal = parseFloat(price).toFixed(2) * parseFloat(quantity).toFixed(2);
    
    if (isNaN(subTotal)) {
        $("#subtotal").html("$0.00NZD + GST + <a href='#'>Freight</a>");
    }
    else {
        $("#subtotal").html("$" + subTotal + "NZD + GST + <a href='#' id='prodfreight'>Freight</a>");
        $(".txtPrice").val(subTotal);

        $("#prodfreight").click(function () {
            window.open("NZFreight.aspx?amount=" + subTotal, "mywindow", "location=1,status=1,scrollbars=1,width=500,height=300");
        });
    }
}

function viewCart() {

    if (readCookie("cart") != null) {

        //createCookie("cart", $(".productids").val(), 7);
        tb_show("View Shopping Cart", "Cart.aspx?id=1&TB_iframe=true&height=360&width=648", 'false');
    }
    else {
        alert('The shopping cart is currently empty');
    }
}

function cc() {

    var amount = $("#ctl00_ContentPlaceHolder1_txtFinalTotal").val();
    var custID = $("#ctl00_ContentPlaceHolder1_txtCustID").val();
    tb_show("Enter Details", "PxPay/Default.aspx?amount=" + amount + "&custID=" + custID + "&TB_iframe=true&height=360&width=448", 'false');
}

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

function checkoutBtn() {

    $(".btncheckout").click(function() {
            return true;
    });

}

function addTotal() {

    var total = 0;
    var price0 = 0;
    var price1 = 0;
    var price2 = 0;
    var price3 = 0;
    var price4 = 0;
    var price5 = 0;
    var price6 = 0;
    var price7 = 0;
    var price8 = 0;
    var price9 = 0;
    
    if(parseFloat($("#ctl00_ContentPlaceHolder1_txtPrice0").val()) > 0)
        price0 = parseFloat($("#ctl00_ContentPlaceHolder1_txtPrice0").val());
    if (parseFloat($("#ctl00_ContentPlaceHolder1_txtPrice1").val()) > 0)
        price1 = parseFloat($("#ctl00_ContentPlaceHolder1_txtPrice1").val());
    if (parseFloat($("#ctl00_ContentPlaceHolder1_txtPrice2").val()) > 0)
        price2 = parseFloat($("#ctl00_ContentPlaceHolder1_txtPrice2").val());
    if (parseFloat($("#ctl00_ContentPlaceHolder1_txtPrice3").val()) > 0)
        price3 = parseFloat($("#ctl00_ContentPlaceHolder1_txtPrice3").val());
    if (parseFloat($("#ctl00_ContentPlaceHolder1_txtPrice4").val()) > 0)
        price4 = parseFloat($("#ctl00_ContentPlaceHolder1_txtPrice4").val());
    if (parseFloat($("#ctl00_ContentPlaceHolder1_txtPrice5").val()) > 0)
        price5 = parseFloat($("#ctl00_ContentPlaceHolder1_txtPrice5").val());
    if (parseFloat($("#ctl00_ContentPlaceHolder1_txtPrice6").val()) > 0)
        price6 = parseFloat($("#ctl00_ContentPlaceHolder1_txtPrice6").val());
    if (parseFloat($("#ctl00_ContentPlaceHolder1_txtPrice7").val()) > 0)
        price7 = parseFloat($("#ctl00_ContentPlaceHolder1_txtPrice7").val());
    if (parseFloat($("#ctl00_ContentPlaceHolder1_txtPrice8").val()) > 0)
        price8 = parseFloat($("#ctl00_ContentPlaceHolder1_txtPrice8").val());
    if (parseFloat($("#ctl00_ContentPlaceHolder1_txtPrice9").val()) > 0)
        price9 = parseFloat($("#ctl00_ContentPlaceHolder1_txtPrice9").val());

    total = price0 + price1 + price2 + price3 + price4 + price5 + price6 + price7 + price8 + price9
    $("#initialtotal").html("$" + parseFloat(total).toFixed(2));



}

function doTabs() {

    $("#tabs li").hover(function() {
        if (!$(this).hasClass("no")) {
            if (!$(this).hasClass("hold")) {
                tabOver($(this));
            }
        }
    }, function() {
        if (!$(this).hasClass("no")) {
            if (!$(this).hasClass("hold")) {
                tabOut($(this));
            }
        }
    });

    $("#tabs li").click(function() {

        //Hide all tabPanels
        $(".tabpanel").fadeOut(300);

        var activeTab = $(this).attr("id");
        $("#" + activeTab + "panel").animate({ opacity: 1.0 }, 300).fadeIn(250);

        //Remove active tab colors from all tabs
        $("#tabs li").each(function() {
            $(this).removeClass("hold");
            tabOut($(this));
        });
        //Set active colors to selected tab.
        $(this).addClass("hold");
        tabOver($(this));

        //alert('click');

    });
}

function loadPlayer(pic, video) {

    
    var so = new SWFObject('player.swf', 'mpl', '500', '300', '9');
    so.addParam('allowscriptaccess', 'always');
    so.addParam('allowfullscreen', 'true');
    so.addParam('flashvars', '&file=http://202.41.137.77/OB_2/Media/' + video + '&image=Assets/CSS/Images/' + pic + '&controlbar=bottom&skin=snel.swf&frontcolor=#246271&dock=false');
    so.write('player');

}

function reloadVideo() {
    $("#videolist li").hover(function() {
        $(this).addClass("vidhover");
    }, function() {
        $(this).removeClass("vidhover");
    });

    $("#videolist li").click(function() {
        $(this).children(".hide").click();
    });

    var pic = $("#ctl00_ContentPlaceHolder1_hdnImage").val();
    var vid = $("#ctl00_ContentPlaceHolder1_hdnVideo").val();

    loadPlayer(pic, vid);

}


