function clearField(field) {
    if (field.value.substring(0, 3) == "***")
        field.value = ""
}

function ToggleCalendar() {
    $('.slotCalendar').toggle();
}

if (typeof equalHeight == 'function') {
    equalHeight();
}

var postback = false;

function refreshCompare() {

    var _pnl = document.getElementById('ctl00__mainpageContent__productFamilyListingView__comparisonBar__compareBar');

    if (_pnl != null) {
        var service = new Buy4Now.Shop4NowGM.UserControls.Product.Comparison.Comparison;
        service.showItems(successCompare);
    }
}

function addCompare(productid, categoryid, parentcategoryid, checkboxid, checked, image, title, checked) {

    var ret;
    ret = document.getElementById("ImbAddRemoveClientId");
    var hiddenBox;
    var actualBox;
    var hiddenCatid;
    var hiddenParentId;

    hiddenBox = document.getElementById("CompareProductIdClientId");
    hiddenCatid = document.getElementById("CompareCategoryIdClientId");
    hiddenParentId = document.getElementById("CompareParentCategoryIdClientId");
    if (hiddenBox != null) {
        actualBox = document.getElementById(hiddenBox.value);
        if (actualBox) {
            actualBox.value = productid;
        }
    }
    if (hiddenCatid != null) {
        actualBox = document.getElementById(hiddenCatid.value);
        if (actualBox) {
            actualBox.value = categoryid;
        }
    }
    if (hiddenParentId != null) {
        actualBox = document.getElementById(hiddenParentId.value);
        if (actualBox) {
            actualBox.value = parentcategoryid;
        }
    }

    var chkbox = document.getElementById(checkboxid);

    if (chkbox) {
        if (checked == '') {
            if (chkbox.checked == true) {

                chkbox.checked = false;
            }
            else {

                chkbox.checked = true;
            }
        }

    }

    var compareErrorClientId = document.getElementById("compareErrorClientId");
    if (compareErrorClientId) {
        compareErrorClientId.value = checkboxid
    }


    var service = new Buy4Now.Shop4NowGM.UserControls.Product.Comparison.Comparison;

    service.AddProduct(productid, categoryid, parentcategoryid, checkboxid, checked, image, title, successCompare);
    // __doPostBack('ctl00__mainpageContent__productFamilyListingView__listingUpdatePanel', '');


}
function successCompare(result, context) {

    var table = document.getElementById('CompareBarDiv');



    table.innerHTML = result;
    if (result.toString().indexOf("compareerror") > 0) {
        var checkboxid;
        var compareErrorClientId = document.getElementById("compareErrorClientId");
        if (compareErrorClientId) {
            checkboxid = compareErrorClientId.value
        }


        var chkbox = document.getElementById(checkboxid);
        if (chkbox) {
            chkbox.checked = false;
        }
    }
}
function onFailure(result) {
    alert(result);
}
function numCharLeft(input, counter, maxChars) {
    var numChars = input.value.length;

    if (numChars > maxChars) {
        input.value = input.value.substring(0, maxChars);
        counter.value = '0 left';
    }
    else {

        counter.value = (maxChars - numChars) + ' left';
    }
}

function hideModal(modalId) {
    $find(modalId).hide();
}
function showModal(modalId) {
    $find(modalId).show();
}
function hideElement(e) {
    var element = document.getElementById(e);
    element.style.display = "none";
}

function openfgp() {
    $find("_forgottenPasswordPopup").show();
}
function closefgp() {
    $find("_forgottenPasswordPopup").hide();
}

function openrp() {
    $find("_reviewPopupExtender").show();
}
function closerp() {
    $find("ctl00__mainpageContent_ProductFamilyDetailsView1__reviewProduct__reviewPopupExtender").hide();
}
function openspp() {

    if ($find("_addServicePlanModalPopup") != null) {
        $find("_addServicePlanModalPopup").show();
    }
}
function closespp() {
    $find("_addServicePlanModalPopup").hide();
}
function openespp() {
    $find("_editWarrantyPopupExtender").show();
}
function closeespp() {
    $find("_editWarrantyPopupExtender").hide();
}

function openwp() {

    var ret = document.getElementById("extralargeImageClientId");

    var largeImage = document.getElementById(ret.value);

    GenericSetExtraLargeImageName();


    if (largeImage.src.indexOf('spinner.gif') < 0) {

        var ret2 = document.getElementById("enlargeImagePopupClientId");
        var image = document.getElementById(ret2.value);

        image.src = largeImage.src;
    }

    $find("_enlargeImagePopupExtender").show();

}
function closewp() {
    $find("_enlargeImagePopupExtender").hide();
}
function openwf() {
    $find("_TellAFriendModalPopup").show();
}
function closewf() {
    $find("_TellAFriendModalPopup").hide();
}
//show hide select - fix for ie6 (maybe else browsers)
function ComboBoxShow(v) {
    var e = document.getElementsByTagName('select');
    if (e != null) {
        for (var i = 0; i < e.length; i++) e[i].style.visibility = v;
    }
}

function MM_swapImgRestore() { //v3.0
    var i, x, a = document.MM_sr; for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) x.src = x.oSrc;
}

function MM_preloadImages() { //v3.0
    var d = document; if (d.images) {
        if (!d.MM_p) d.MM_p = new Array();
        var i, j = d.MM_p.length, a = MM_preloadImages.arguments; for (i = 0; i < a.length; i++)
            if (a[i].indexOf("#") != 0) { d.MM_p[j] = new Image; d.MM_p[j++].src = a[i]; }
    }
}

function MM_findObj(n, d) { //v4.01
    var p, i, x; if (!d) d = document; if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
    for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
    if (!x && d.getElementById) x = d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0

    var i, j = 0, x, a = MM_swapImage.arguments; document.MM_sr = new Array; for (i = 0; i < (a.length - 2); i += 3)
        if ((x = MM_findObj(a[i])) != null) { document.MM_sr[j++] = x; if (!x.oSrc) x.oSrc = x.src; x.src = a[i + 2]; }
}

function clearField(field) {
    var fieldContent = field.value;

    if (fieldContent.substring(0, 5) == "Enter") {
        field.value = ""
    }
}

function hookEvent(element, eventName, callback) {
    if (typeof (element) == "string") element = document.getElementById(element);
    if (element == null) return;

    if (element.addEventListener) {
        if (eventName == 'mousewheel')
            element.addEventListener('DOMMouseScroll', callback, false);

        element.addEventListener(eventName, callback, false);
    }
    else if (element.attachEvent) element.attachEvent("on" + eventName, callback);
}

function unhookEvent(element, eventName, callback) {
    if (typeof (element) == "string") element = document.getElementById(element);
    if (element == null) return;

    if (element.removeEventListener) {
        if (eventName == 'mousewheel')
            element.removeEventListener('DOMMouseScroll', callback, false);

        element.removeEventListener(eventName, callback, false);
    }
    else if (element.detachEvent) element.detachEvent("on" + eventName, callback);
}



function cancelEvent(e) {
    e = e ? e : window.event;

    if (e.stopPropagation) e.stopPropagation();
    if (e.preventDefault) e.preventDefault();

    e.cancelBubble = true;
    e.cancel = true;
    e.returnValue = false;

    return false;
}

function disableMouseWheel(e) {
    e = e ? e : window.event;
    var wheelData = e.detail ? e.detail * -1 : e.wheelDelta / 40;

    return cancelEvent(e);
}

function jumpToElement(id) {
    var elem = document.getElementById(id);
    if (elem == null) return;

    var posX = 0;
    var posY = 0;

    if (elem.offsetParent) {
        while (elem != null) {
            posX += elem.offsetLeft;
            posY += elem.offsetTop;
            elem = elem.offsetParent;
        }
    }
    else if (elem.y) {
        posX += elem.x;
        posY += elem.y;
    }

    window.scrollTo(0, posY);
}

function sortBy(dropdownId, hiddenId) {
    var dropdown = document.getElementById(dropdownId);

    document.getElementById(hiddenId).value = dropdown.options[dropdown.selectedIndex].value;

}


var bannerList = new Array();
var bannerListIDs = new Array();

function bannerDisplay(pos, currId) {
    currId++;
    if (bannerList[pos][currId]) {
        document.getElementById(bannerListIDs[pos][0]).innerHTML = bannerList[pos][currId][1];
        setTimeout("bannerDisplay('" + pos + "', " + currId + ")", bannerList[pos][currId][0]);
    }
    else if (bannerList[pos][0]) {
        document.getElementById(bannerListIDs[pos][0]).innerHTML = bannerList[pos][0][1];
        if (bannerList[pos][1]) {
            setTimeout("bannerDisplay('" + pos + "', -1)", bannerList[pos][0][0]);
        }
    }
}

function bannerDisplayStart() {
    for (var pos in bannerListIDs) {
        if (bannerListIDs[pos][1] != 1 && bannerListIDs[pos].length > 1) {
            setTimeout("bannerDisplay('" + pos + "', 0)", bannerList[pos][0][0]);
        }
    }
}

function HTMLPopup(path, width, height) {
    var numberForms = document.forms.length, formIndex = 0, cformIndex = 0;
    if (numberForms > 1) {
        for (formIndex = 0; formIndex < numberForms; formIndex++) {
            if (!(document && document.forms[formIndex] && document.forms[formIndex].b4nfindform)) {
            }

            else {
                cformIndex = formIndex;
            }
        }
    }

    window.open(path, "", 'dependent=1,height=' + height + ',width=' + width
                    + ',titlebar=no,toolbar=0,resizable=0,left=100,top=150,scrollbars=1');
}

function HTMLContentPopup(path) {
    var width, height;
    width = 550;
    height = 400;
    var numberForms = document.forms.length, formIndex = 0, cformIndex = 0;
    if (numberForms > 1) {
        for (formIndex = 0; formIndex < numberForms; formIndex++) {
            if (!(document && document.forms[formIndex] && document.forms[formIndex].b4nfindform)) {
            }

            else {
                cformIndex = formIndex;
            }
        }
    }

    window.open(path, "", 'dependent=1,height=' + height + ',width=' + width
                    + ',titlebar=no,toolbar=0,resizable=0,left=100,top=100,scrollbars=1');
}

function showsecurityhelp() { window.open("contentpopup.aspx?loc=CHECKOUT&subloc=CCV", "", "dependent=1, titlebar=0, toolbar=0, resizable=0, width=500, height=550"); }

function CheckForEnterKey(inputbutton, evt) {

    var key; var newkey; var obj; var nkey; var tempkeyword; var rExp; var netscapekey;

    if (navigator.appName == 'Netscape') { netscapekey = evt.which; key = netscapekey; }
    else { key = window.event.keyCode; }

    if (key == 13) {
        if (navigator.appName == 'Netscape')
        { evt.cancelBubble = true; evt.returnValue = false; }
        else
        { event.cancelBubble = true; event.returnValue = false; }

        var ret = null; ret = document.getElementById(inputbutton);

        if (ret) {
            ///ret.focus();
            ret.click();
        }
    }
}

function CheckTextAreaLength(textBox, maxlength) {
    var textarea = document.getElementById(textBox);

    if (textarea.value.length > maxlength) {
        textarea.value = textarea.value.substring(0, maxlength);
    }
}


function ResetValidation(hiddenTextBox) {
    var hiddenText = document.getElementById(hiddenTextBox);
    hiddenText.value = "0";
}

function BeginRequestHandler(sender, args) {
    funcChange('hidden');
}

function EndRequestHandler(sender, args) {
    funcChange('visible');

}

function funcChange(strdisplay) {
    var node_list = document.getElementsByTagName('select');
    for (var i = 0; i < node_list.length; i++) {
        var node = node_list[i];
        node.style.visibility = strdisplay;
    }
}

function EnableButtons(strdisplay) {
    var node_list = document.getElementsByName(strdisplay);
    for (var i = 0; i < node_list.length; i++) {
        node_list[i].style.visibility = 'visible';
    }
}

function StartScript() {
    bannerDisplayStart();
}

/*
function UpdateMiniBasket(quantity, basketcost) {    
var items;
items = document.getElementById('ctl00__miniCartControl__itemCountLabel');
if (items == null) {
items = document.getElementById('ctl00:miniCartControl:itemCountLabel');
}
if (items != null) {
items.innerHTML = "Items : " +  quantity;
}
var cost;
cost = document.getElementById('ctl00__miniCartControl__basketTotalLabel');
if (cost == null) {
cost = document.getElementById('ctl00:miniCartControl:basketTotalLabel');
}
if(cost != null)
{
cost.innerHTML = "Total : " + basketcost;
}
}
*/
//This function requires that the css classes have the word 'On' to signify the on button so it's not great.
function toggleTabs(name, id) {
    var control = document.getElementsByName(name);


    for (var index = 0; index < control.length; index++) {
        if (control[index].getAttribute("id") == id) {
            if (control[index].className.indexOf("On") < 0) {
                control[index].className = control[index].className + "On";
            }
        }
        else {
            if (control[index].className.indexOf("On") > 0) {
                control[index].className = control[index].className.substr(0, control[index].className.indexOf("On"));
            }
        }
    }
}

function showpanel(id) {

    var ret;
    var panel1id = document.getElementById("panel1clientid");
    var panel2id = document.getElementById("panel2clientid");
    var panel3id = document.getElementById("panel3clientid");
    var panel4id = document.getElementById("panel4clientid");
    var panel5id = document.getElementById("panel5clientid");
    var panel6id = document.getElementById("panel6clientid");
    var panel7id = document.getElementById("panel7clientid");
    var panel8id = document.getElementById("panel8clientid");
    var panel9id = document.getElementById("panel9clientid");

    var panel1 = document.getElementById(panel1id.value);
    var panel2 = document.getElementById(panel2id.value);
    var panel3 = document.getElementById(panel3id.value);
    var panel4 = document.getElementById(panel4id.value);
    var panel5 = document.getElementById(panel5id.value);
    var panel6 = document.getElementById(panel6id.value);
    var panel7 = document.getElementById(panel7id.value);
    var panel8 = document.getElementById(panel8id.value);
    var panel9 = document.getElementById(panel9id.value);

    if (id > 0 && id < 7) {


        if (panel1 != null) {
            panel1.style.display = 'none';
        }
        if (panel2 != null) {
            panel2.style.display = 'none';
        }
        if (panel3 != null) {
            panel3.style.display = 'none';
        }
        if (panel4 != null) {
            panel4.style.display = 'none';
        }
        if (panel5 != null) {
            panel5.style.display = 'none';
        }
        if (panel6 != null) {
            panel6.style.display = 'none';
        }
    }
    if (id > 6 && id < 10) {


        if (panel7 != null) {
            panel7.style.display = 'none';
        }

        if (panel8 != null) {
            panel8.style.display = 'none';
        }

        if (panel9 != null) {
            panel9.style.display = 'none';
        }
    }

    if (id == "1" && panel1 != null) {
        panel1.style.display = 'block';
    }
    else if (id == "2" && panel2 != null) {
        panel2.style.display = 'block';
    }
    else if (id == "3" && panel3 != null) {
        panel3.style.display = 'block';
    }
    else if (id == "4" && panel4 != null) {
        panel4.style.display = 'block';
    }
    else if (id == "5" && panel5 != null) {
        panel5.style.display = 'block';
    }
    else if (id == "6" && panel6 != null) {
        panel6.style.display = 'block';
    }

    else if (id == "7" && panel7 != null) {
        panel7.style.display = 'block';
    }

    else if (id == "8" && panel8 != null) {
        panel8.style.display = 'block';
    }

    else if (id == "9" && panel9 != null) {
        panel9.style.display = 'block';
    }
}


function hideModal(modalId) {
    $find(modalId).hide();
}

function hideElement(e) {
    var element = document.getElementById(e);
    element.style.display = "none";
}

function HTMLContentPopup(path) {
    var width, height;
    width = 602;
    height = 500;
    var numberForms = document.forms.length, formIndex = 0, cformIndex = 0;
    if (numberForms > 1) {
        for (formIndex = 0; formIndex < numberForms; formIndex++) {
            if (!(document && document.forms[formIndex] && document.forms[formIndex].b4nfindform)) {
            }

            else { cformIndex = formIndex; }
        }
    }

    window.open(path, null, 'dependent=1,height=' + height + ',width=' + width
                    + ',titlebar=no,toolbar=0,status=yes,menubar=no,location=no,resizable=0,left=100,top=150,scrollbars=1');
}
