﻿
String.prototype.trim = function () { return this.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); };

$(function () {
    $("#txtSearch").blur(function () {
        if ($("#txtSearch").val().trim().length == 0) {
            $("#txtSearch").val('Search for products');
            $("#txtSearch").css('color', '#B2B2B2');
            $("#txtSearch").css('border-color', '#CCCCCC');
        }
    });
});

$(function () {
    $("#txtSearch").focus(function () {
        if ($("#txtSearch").val().trim().toLowerCase() == "search for products") {
            $("#txtSearch").val('');
            $("#txtSearch").css('color', '#333333');
            //$("#txtSearch").css('border-color', '#000000');
        }
    });
});

$(function () {
    $("#txtSearch").mousedown(function () {
        if ($("#txtSearch").val().trim().toLowerCase() == "search for products") {
            $("#txtSearch").focus();
        }
    });
});

$(function () {
    $("#txtSearch").keyup(function (e) {
        if ($("#txtSearch").val().trim().toLowerCase() == "search for products") {
            $("#txtSearch").focus();
            e.returnValue = false;
            if (e.preventDefault) e.preventDefault();
        }
        else if (e.keyCode == 13 || e.which == 13) {
            if ($("#txtSearch").val().trim().length == 0) {
                $("#txtSearch").focus();
                e.returnValue = false;
                if (e.preventDefault) e.preventDefault();
            }
            else {
                $("#txtSearch").css('border-color', '');
                $("#btnSearch").click();
            }
        }
        else {
            if ((e.keyCode == 8 || e.which == 8) && $("#txtSearch").val().trim().length == 0) {
                $("#txtSearch").focus();
                e.returnValue = false;
                if (e.preventDefault) e.preventDefault();
            }
            else if ($("#txtSearch").val().trim().length > 2)
                checkKey(document.getElementById('txtSearch'), '', e);
        }
    });
});
function getSearchStoreValue() {
    return $("#selectAllStoresContainer").find("span.value").find("span").html();
}
function DoSearch() {
    var txt; var sn;
    ajax_options_hide();
    if ($("#txtSearch").val().toLowerCase() == "search for products") {
        $("#txtSearch").focus();
        return false;
    }
    if ($("#txtSearch").val().trim().length > 0 && getSearchStoreValue().trim().length > 0) {
        $("#txtSearch").css('border-color', '');
        txt = $("#txtSearch").val();
        sn = getSearchStoreValue();
        window.location.href = "/searchproducts.aspx?sn=" + sn + "&q=" + txt;
        return false;
    }
    else
        return false;
}

function ddlStoresChange() {
    if ($("#txtSearch").val().toLowerCase() == "search for products" || $("#txtSearch").val().trim().length == 0) {
        $("#txtSearch").focus();
        return;
    }
}

function Do_Compare() {
    var elem = document.getElementById('Viewcombtn');
    if (elem.textContent) elem.innerHTML = elem.textContent;
    else elem.innerHTML = elem.innerText;
    var comparedetails = elem.innerHTML;
    var count = comparedetails.match(/[\d\.]+/g);
    if (count.length > 0 && !isNaN(count[0])) {
        if (count[0] == 0) {
            alert('Please select atleast two products!');
            return false;
        }
        else if (count[0] == 1) {
            alert('Please select atleast two products!');
            return false;
        }
        else if (count[0] > 5) {
            alert('Please select <= 5 product!');
            return false;
        }
        else {
            if (document.getElementById('hdnCompare').value != '' && document.getElementById('hdnStorename').value != '') {
                var type = document.getElementById('hdnCompare').value.split(':');
                if (type.length > 1) {
                    var refurl;
                    if (document.URL.indexOf(".htm") != -1)
                        refurl = encodeURIComponent(document.URL.substring(document.URL.lastIndexOf("/"), document.URL.length).replace(".htm", "$"));
                    else
                        refurl = encodeURIComponent("/" + document.getElementById('hdnStorename').value.toLowerCase() + "/");
                    var requrl = "/" + document.getElementById('hdnStorename').value + "/compare.aspx?storename=" + document.getElementById('hdnStorename').value + "&Type=" + type[0] + "&Value=" + type[1] + "&refurl=" + refurl;
                    window.location.href = requrl;
                    return false;
                }
            }
        }
    }
    return false;
}

// FinalDetails page TabContain Script 

function FinalPageActiveTabChanged(sender, e) {
    var lblDescription = document.getElementById('ContentPlaceHolder1_FinalControlValuesHolder_ctl00_FDPMainSection_TabContain_Tab1_lblDescription');
    var lblReviews = document.getElementById('ContentPlaceHolder1_FinalControlValuesHolder_ctl00_FDPMainSection_TabContain_Tab2_lblReviews');
    var lblSpcification = document.getElementById('ContentPlaceHolder1_FinalControlValuesHolder_ctl00_FDPMainSection_TabContain_Tab3_lblSpcification');
    var lblFlashes = document.getElementById('ContentPlaceHolder1_FinalControlValuesHolder_ctl00_FDPMainSection_TabContain_Tab4_lblFlashes');
    var lblAccessories = document.getElementById('ContentPlaceHolder1_FinalControlValuesHolder_ctl00_FDPMainSection_TabContain_Tab5_lblAccessories');
    var lblSoftware = document.getElementById('ContentPlaceHolder1_FinalControlValuesHolder_ctl00_FDPMainSection_TabContain_Tab6_lblSoftware');
    var lblContentBax = document.getElementById('ContentPlaceHolder1_FinalControlValuesHolder_ctl00_FDPMainSection_TabContain_Tab7_lblContentBax');
    var lblFeatures = document.getElementById('ContentPlaceHolder1_FinalControlValuesHolder_ctl00_FDPMainSection_TabContain_Tab8_lblFeatures');
    var lblcrossseller = document.getElementById('ContentPlaceHolder1_FinalControlValuesHolder_ctl00_FDPMainSection_TabContain_Tab9_lblcrossseller');
    var lblVideo = document.getElementById('ContentPlaceHolder1_FinalControlValuesHolder_ctl00_FDPMainSection_TabContain_Tab10_lblVideo');
    var id = sender.get_activeTab().get_id();
    if (lblDescription != null)
        lblDescription.className = 'tabBg';
    if (lblReviews != null)
        lblReviews.className = 'tabBg';
    if (lblSpcification != null)
        lblSpcification.className = 'tabBg';
    if (lblFlashes != null)
        lblFlashes.className = 'tabBg';
    if (lblAccessories != null)
        lblAccessories.className = 'tabBg';
    if (lblSoftware != null)
        lblSoftware.className = 'tabBg';
    if (lblContentBax != null)
        lblContentBax.className = 'tabBg';
    if (lblFeatures != null)
        lblFeatures.className = 'tabBg';
    if (lblcrossseller != null)
        lblcrossseller.className = 'tabBg';
    if (lblVideo != null)
        lblVideo.className = 'tabBg';

    if (id == "ContentPlaceHolder1_FinalControlValuesHolder_ctl00_FDPMainSection_TabContain_Tab1")
        lblDescription.className = 'tabBgActive';
    else if (id == "ContentPlaceHolder1_FinalControlValuesHolder_ctl00_FDPMainSection_TabContain_Tab2")
        lblReviews.className = 'tabBgActive';
    else if (id == "ContentPlaceHolder1_FinalControlValuesHolder_ctl00_FDPMainSection_TabContain_Tab3")
        lblSpcification.className = 'tabBgActive';
    else if (id == "ContentPlaceHolder1_FinalControlValuesHolder_ctl00_FDPMainSection_TabContain_Tab4")
        lblFlashes.className = 'tabBgActive';
    else if (id == "ContentPlaceHolder1_FinalControlValuesHolder_ctl00_FDPMainSection_TabContain_Tab5")
        lblAccessories.className = 'tabBgActive';
    else if (id == "ContentPlaceHolder1_FinalControlValuesHolder_ctl00_FDPMainSection_TabContain_Tab6")
        lblSoftware.className = 'tabBgActive';
    else if (id == "ContentPlaceHolder1_FinalControlValuesHolder_ctl00_FDPMainSection_TabContain_Tab7")
        lblContentBax.className = 'tabBgActive';
    else if (id == "ContentPlaceHolder1_FinalControlValuesHolder_ctl00_FDPMainSection_TabContain_Tab8")
        lblFeatures.className = 'tabBgActive';
    else if (id == "ContentPlaceHolder1_FinalControlValuesHolder_ctl00_FDPMainSection_TabContain_Tab9")
        lblcrossseller.className = 'tabBgActive';
    else if (id == "ContentPlaceHolder1_FinalControlValuesHolder_ctl00_FDPMainSection_TabContain_Tab10")
        lblVideo.className = 'tabBgActive';
}

//// Get DeliveryTime in FinalDetailsPage
//function FinalPageGetDeliveryTime() {
//    if (document.getElementById('ContentPlaceHolder1_FinalControlValuesHolder_ctl00_hdnStoreid')) {
//        var storeid = document.getElementById('ContentPlaceHolder1_FinalControlValuesHolder_ctl00_hdnStoreid').value;
//        if (storeid != '') {
//            window.open('../common/deliverytime.aspx?storeid=' + storeid, '', 'height=260,width=450,status=no,toolbar=no,menubar=no,maximize=no,resize=no,scrollbars=no');
//        }
//    }
//}

//FaceBook 
(function (d, s, id) {

    var js, fjs = d.getElementsByTagName(s)[0];

    if (d.getElementById(id)) { return; }

    js = d.createElement(s); js.id = id;

    js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";

    fjs.parentNode.insertBefore(js, fjs);

} (document, 'script', 'facebook-jssdk'));


////Get DeliveryTime in Browse Page
//function BrowsePageGetDeliveryTime() {
//    if (document.getElementById('ContentPlaceHolder1_SpecificValuesHolder_ctl00_hdnStoreid')) {
//        var storeid = document.getElementById('ContentPlaceHolder1_SpecificValuesHolder_ctl00_hdnStoreid').value;
//        if (storeid != '') {
//            window.open('../common/deliverytime.aspx?storeid=' + storeid, '', 'height=260,width=450,status=no,toolbar=no,menubar=no,maximize=no,resize=no,scrollbars=no');
//        }
//    }
//}

// Get Glossary text in FDP
function ShowPopUp(sname, feature) {
    var url = "/StaticPageContainer1.aspx?htmlfilename=" + sname + "_Glossary#" + feature;
    window.open(url, "Glossary", "height=600,width=800,status=no,toolbar=no,menubar=no,maximize=no,resize=no,scrollbars=yes");
}


function buyNowOutofStock(sname, SkuID) {
    if ($('.addCartBut')) {
        $('.addCartBut').removeClass('addCartBut').addClass('outofstockNonBooks');
        if ($('div.outofstockNonBooks')) {
            $('div.outofstockNonBooks').html('');
            $('div.outofstockNonBooks').html("<a style='cursor:pointer'></a>");
        }
    }
}

//Video Tutorial in FDP
function LargeVideoStore(obj) {
    var gd = document.getElementById('ContentPlaceHolder1_FinalControlValuesHolder_ctl00_FDPMainSection_TabContain_Tab10_divVideo');
    var img1 = gd.getElementsByTagName('img');
    for (var i = 0; i < img1.length; i++) {
        img1[i].className = "videoBorderEmpty";
    }

    obj.className = "videoBorder";
    var LrgVideo = document.getElementById('ContentPlaceHolder1_FinalControlValuesHolder_ctl00_FDPMainSection_TabContain_Tab10_iframeVideo');
    LrgVideo.src = obj.alt;
}


//function getPosition(e) {

//    e = e || window.event;
//    var cursor = { x: 0, y: 0 };
//    if (e.pageX || e.pageY) {
//        cursor.x = e.pageX;
//        cursor.y = e.pageY;
//    }
//    else {
//        var de = document.documentElement;
//        var b = document.body;
//        cursor.x = e.clientX +
//            (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
//        cursor.y = e.clientY +
//            (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
//    }
//    return cursor;
//}

// Delivery Time PopUp

//function ImgHide(elemdiv) { document.getElementById(elemdiv).style.display = 'none'; }

////Browse Page
//$(function () {
//    $('.delDateQuest').click(function (e) {
//        e.stopPropagation();
//        $('#ContentPlaceHolder1_DeliveryPopUpDiv').css('left', $(this).offset().left); $('#ContentPlaceHolder1_DeliveryPopUpDiv').css('top', $(this).offset().top + 9);
//        $('#ContentPlaceHolder1_DeliveryPopUpDiv').show();
//        $('#ContentPlaceHolder1_DeliveryPopUpDiv p').addClass('deliveryPopupText');
//    });
//});

////FDP page
//$(function () {
//    $('.delDate').click(function (e) {
//        e.stopPropagation();
//        $('#ContentPlaceHolder1_DeliveryPopUpDiv').css('left', $(this).offset().left); $('#ContentPlaceHolder1_DeliveryPopUpDiv').css('top', $(this).offset().top + 9);
//        $('#ContentPlaceHolder1_DeliveryPopUpDiv').show();
//        $('#ContentPlaceHolder1_DeliveryPopUpDiv p').addClass('deliveryPopupText');
//    });
//});

////Books FDP Page Delivery Time PopUp
//$(function () {
//    $('.delDateQuest').click(function (e) {
//        e.stopPropagation();
//        $('#ContentPlaceHolder1_FinalPage_DeliveryPopUpDiv').css('left', $(this).offset().left); $('#ContentPlaceHolder1_FinalPage_DeliveryPopUpDiv').css('top', $(this).offset().top + 9);
//        $('#ContentPlaceHolder1_FinalPage_DeliveryPopUpDiv').show();
//        $('#ContentPlaceHolder1_FinalPage_DeliveryPopUpDiv p').addClass('deliveryPopupText');
//    });
//});

////Books Browse Page Delivery Time PopUp
//$(function () {
//    $('.delDateQuest').click(function (e) {
//        e.stopPropagation();
//        $('#ContentPlaceHolder1_BrowseControl_DeliveryPopUpDiv').css('left', $(this).offset().left); $('#ContentPlaceHolder1_BrowseControl_DeliveryPopUpDiv').css('top', $(this).offset().top + 9);
//        $('#ContentPlaceHolder1_BrowseControl_DeliveryPopUpDiv').show();
//        $('#ContentPlaceHolder1_BrowseControl_DeliveryPopUpDiv p').addClass('deliveryPopupText');
//    });
//});

//Fdp OutofStock Popup NonBooks
function OutofStockInsertMailID() {
    var regMail = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
    var mailVal = $("#txtOutStockemailid").val();
    var errMsg = '';
    $("#outStockerrmsg").html('');
    $("#outStockerrmsg").hide();
    if ($("#txtOutStockemailid").val().length == 0) {
        errMsg = "Please enter a mail-id";
        $("#txtOutStockemailid").focus();
    }
    else if (!regMail.test(mailVal)) {
        errMsg = "Please enter a valid mail-id";
        $("#txtOutStockemailid").focus();
    }

    if (errMsg.length > 0) {
        $("#outStockerrmsg").show();
        $("#outStockerrmsg").html(errMsg);
        $("#outStockerrmsg").css('display', 'block');
        return false;
    }

    var skuid = $("#ContentPlaceHolder1_FinalControlValuesHolder_ctl00_FDPMainSection_hdnSku").val();
    var storeName = $("#ContentPlaceHolder1_FinalControlValuesHolder_ctl00_FDPMainSection_hdnStorename").val();
    var productName = $("#ContentPlaceHolder1_FinalControlValuesHolder_ctl00_FDPMainSection_hdnProductName").val();

    $.ajax({
        beforeSend: function () {
            showMask();
        },
        url: "/Common/OutofStock.aspx?mail=" + mailVal + "&store=" + storeName + "&sku=" + skuid + "&name=" + productName, //
        context: document.body,
        success: function (data) {
            if (data != "success") {
                $("#outStockerrmsg").show();
                $("#outStockerrmsg").html(data);
                hideMask();
                $('#outStockerrmsg').show();
                $("#outStockerrmsg").html('Thanks for your interest.We will notify you on <br/>' + mailVal + ' when this product is available again.');
                $("#outStockerrmsg").css('color', 'green');
                $("#outStockerrmsg").css('border', '1px solid green');
            }
            else {
                hideMask();
            }
        }
    });
    return false;
}

function OutofStockPopUpClose() {
    $('#ContentPlaceHolder1_outStockDiv').hide();
}

$(function () {
    $('.outofstockNonBooks').click(function (e) {
        e.stopPropagation();
        $('#ContentPlaceHolder1_outStockDiv').css('left', $(this).offset().left); $('#ContentPlaceHolder1_outStockDiv').css('top', $(this).offset().top - 140);
        $('#ContentPlaceHolder1_outStockDiv').show();
        $("#txtOutStockemailid").attr("value", "");
        $("#outStockerrmsg").css('display', 'none'); //outStockDefault
        $('#ContentPlaceHolder1_outStockDiv p').addClass('outStockDefault');
    });
});

$(function () {
    $(".addCartBut").hover(function () {
        $(".addCartBut").find('a').css('cursor', 'pointer');
        $(".addCartBut").find('a').attr('title', 'Add to cart');
        var dropdownlist = $(".smVariantBg").find("select")
        var notSelectedFlag = false;
        var toolTipString = '';
        for (var i = 0; i < dropdownlist.length; i++) {
            if (dropdownlist[i].selectedIndex == 0) {
                notSelectedFlag = true;
                if (toolTipString.length == 0)
                    toolTipString = dropdownlist[i].value.replace('-- ', '').replace(' --', '');
                else
                    toolTipString += " and " + dropdownlist[i].value.replace('-- Select', '').replace(' --', '');
            }
        }
        if (notSelectedFlag) {
            $(".addCartBut").find('a').css('cursor', 'not-allowed');
            toolTipString = 'Before Adding to Cart, ' + toolTipString + ' from above.';
            $(".addCartBut").find('a').attr('title', toolTipString);
        }
    });
});

// Books FDP OutofStock Popup

function OutofStockPopUpCloseBook() {
    $('#ContentPlaceHolder1_FinalPage_outStockDiv').hide();
}

$(function () {
    $('.fdpBuyBut').click(function (e) {
        e.stopPropagation();
        $('#ContentPlaceHolder1_FinalPage_outStockDiv').css('left', $(this).offset().left); $('#ContentPlaceHolder1_FinalPage_outStockDiv').css('top', $(this).offset().top - 130);
        $('#ContentPlaceHolder1_FinalPage_outStockDiv').show();
        $("#txtOutStockemailid").attr("value", "");
        $("#outStockerrmsg").css('display', 'none');
        $('#ContentPlaceHolder1_FinalPage_outStockDiv p').addClass('outStockDefault');
    });
});

function OutofStockInsertMailIDBook() {
    var regMail = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
    var mailVal = $("#txtOutStockemailid").val();
    var errMsg = '';
    $("#outStockerrmsg").html('');
    $("#outStockerrmsg").hide();
    if ($("#txtOutStockemailid").val().length == 0) {
        errMsg = "Please enter a mail-id";
        $("#txtOutStockemailid").focus();
    }
    else if (!regMail.test(mailVal)) {
        errMsg = "Please enter a valid mail-id";
        $("#txtOutStockemailid").focus();
    }

    if (errMsg.length > 0) {
        $("#outStockerrmsg").show();
        $("#outStockerrmsg").html(errMsg);
        $("#outStockerrmsg").css('display', 'block');
        return false;
    }

    var skuid = $("#ContentPlaceHolder1_FinalPage_hdnSku").val();
    var storeName = $("#ContentPlaceHolder1_FinalPage_hdnStorename").val();
    var productName = $("#ContentPlaceHolder1_FinalPage_hdnProductName").val();

    $.ajax({
        beforeSend: function () {
            showMask();
        },
        url: "/Common/OutofStock.aspx?mail=" + mailVal + "&store=" + storeName + "&sku=" + skuid + "&name=" + productName, //
        context: document.body,
        success: function (data) {
            if (data != "success") {
                $("#outStockerrmsg").show();
                $("#outStockerrmsg").html(data);
                hideMask();
                $('#outStockerrmsg').show();
                $("#outStockerrmsg").html('Thanks for your interest.We will notify you on <br/>' + mailVal + ' when this product is available again.');
                $("#outStockerrmsg").css('color', 'green');
                $("#outStockerrmsg").css('border', '1px solid green');
            }
            else {
                hideMask();
            }
        }
    });
    return false;
}

function ValidateNewLetter() {
    var regMail = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
    $("#ContentPlaceHolder1_HomePageCtrl_divMessage").html('');
    $("#ContentPlaceHolder1_HomePageCtrl_divMessage").hide();
   // $("#ContentPlaceHolder1_HomePageCtrl_newsLet").html('');
    
    if ($("#ContentPlaceHolder1_HomePageCtrl_txtNewsLetter1").val().trim().length == 0) {
        $("#ContentPlaceHolder1_HomePageCtrl_divMessage").html('Please enter a valid email-id');
        $("#ContentPlaceHolder1_HomePageCtrl_divMessage").show();
        return false;
    }
    else if (!regMail.test($("#ContentPlaceHolder1_HomePageCtrl_txtNewsLetter1").val())) {
        $("#ContentPlaceHolder1_HomePageCtrl_divMessage").html('Please enter a valid email-id');
        $("#ContentPlaceHolder1_HomePageCtrl_divMessage").show();
        return false;
    }
    return true;
}

$(function () {
    $('.notShippableCss').click(function (e) {
        e.stopPropagation();
        $('#ContentPlaceHolder1_FinalControlValuesHolder_ctl00_FDPMainSection_DivNotShippable').css('left', $(this).offset().left - 200); $('#ContentPlaceHolder1_FinalControlValuesHolder_ctl00_FDPMainSection_DivNotShippable').css('top', $(this).offset().top);
        $('#ContentPlaceHolder1_FinalControlValuesHolder_ctl00_FDPMainSection_divShippablePopup').show();
    });
});

function NotShppableclose() {
    $('#ContentPlaceHolder1_FinalControlValuesHolder_ctl00_FDPMainSection_divShippablePopup').hide();
}
