﻿
function LoadCSS(hdnordrNo, hdnPrimaryId) {
    var ordrNo = document.getElementById(hdnordrNo).value;
    var primaryId = document.getElementById(hdnPrimaryId).value;

    if (document.getElementById("ctl00_hdnCategory" + ordrNo) == null) {
        return true;
    }
    var subCatArray = document.getElementById("ctl00_hdnCategory" + ordrNo).value.split(";");

    var uiTag = document.createElement("ul");
    var x;
    for (x in subCatArray) {
        var liTag = document.createElement("li");
        uiTag.appendChild(liTag);
        var aTag = document.createElement("a");
        aTag.innerHTML = subCatArray[x].substring(0, subCatArray[x].indexOf(":"));

        var applicationPath = document.getElementById("ctl00_hdnApplicationPath").value;
        var subId = subCatArray[x].substring(subCatArray[x].indexOf(":") + 1);
        aTag.href = applicationPath + "/Category.aspx?id=" + subId + "&pID=" + primaryId;

        var queryString = location.search;

        if (queryString == "") {
            //William 25/02/2011
            // Null value here cause error on the page (Internet Explorer only)
            //var selectedSubId = document.getElementById("ctl00_ContentPlaceHolder1_hdnSessionId").value;
            if (document.getElementById("ctl00_ContentPlaceHolder1_hdnSessionId") != null) 
            {
                var selectedSubId = document.getElementById("ctl00_ContentPlaceHolder1_hdnSessionId").value;
            }
            else 
            {
                var selectedSubId = "";
            }
        }
        else
        {
            var selectedSubId = queryString.substring(queryString.indexOf("id") + 3, queryString.indexOf("&"));
        }
        
        
        if (subId == selectedSubId) {
            if (navigator.appName == "Microsoft Internet Explorer") {
                aTag.setAttribute("className", "selected")
            }
            else {
                aTag.setAttribute("class", "selected")
            }
        }

        liTag.appendChild(aTag);
    }

    var placeHoldr = document.getElementById("ctl00_pnlSubCat");
    placeHoldr.innerHTML = "";
    placeHoldr.appendChild(uiTag);
      
    if (navigator.appName == "Microsoft Internet Explorer") {
        document.getElementById("ctl00_hyprCategory" + ordrNo).parentNode.className = 'current';
        //document.getElementById("ctl00_hyprCategory" + ordrNo).parentNode.setAttribute('className', 'current');
    }
    else {
        document.getElementById("ctl00_hyprCategory" + ordrNo).parentNode.setAttribute('class', 'current');
    }
}

function ShowSubCategory(subCatValues, parentLink, primaryId, lnkColor) {
   
    var underLineColor = lnkColor;
    lnkColor = '#000000';

    var orderNo = document.getElementById(primaryId).value;

    var uiTag = document.createElement("ul");

    var subCatArray = document.getElementById(subCatValues).value.split(";");
    var x;
    for (x in subCatArray) {
        var liTag = document.createElement("li");
        uiTag.appendChild(liTag);
        var aTag = document.createElement("a");
        aTag.innerHTML = subCatArray[x].substring(0, subCatArray[x].indexOf(":"));
        var applicationPath = document.getElementById("ctl00_hdnApplicationPath").value;
        aTag.href = applicationPath + "/Category.aspx?id=" + subCatArray[x].substring(subCatArray[x].indexOf(":") + 1) + "&pID=" + orderNo;

        if (navigator.appName == "Microsoft Internet Explorer") {
            aTag.style.color = lnkColor;

            if (x == subCatArray.length - 1) {
                liTag.style.borderRight = "none";
            }
            else {
                liTag.style.borderRight = "1px solid " + lnkColor;
            }
            aTag.setAttribute("onmouseover", "this.style.borderBottom='1px solid " + underLineColor + "';", false)
            aTag.setAttribute("onmouseout", "this.style.borderBottom='none';", false)
        }
        else {
            aTag.setAttribute("style", "color:" + lnkColor)
            if (x == subCatArray.length - 1) {
                liTag.setAttribute("style", "border-right:none")
            }
            else {
                liTag.setAttribute("style", "border-right:1px solid " + lnkColor)
            }
            aTag.setAttribute("onmouseover", "this.style.borderBottom='1px solid " + underLineColor + "';")
            aTag.setAttribute("onmouseout", "this.style.borderBottom='none';")
        }


        liTag.appendChild(aTag);
    }

    var placeHoldr = document.getElementById("ctl00_pnlSubCat");
    placeHoldr.innerHTML = "";
    placeHoldr.appendChild(uiTag);

    for (x = 1; x < 10; x++) {
        if (navigator.appName == "Microsoft Internet Explorer") {
            document.getElementById("ctl00_hyprCategory" + x).parentElement.setAttribute('className', 'none');
        }
        else {
            document.getElementById("ctl00_hyprCategory" + x).parentNode.setAttribute('class', 'none');
        }
    }

    if (navigator.appName == "Microsoft Internet Explorer") {
        document.getElementById(parentLink).parentNode.setAttribute('className', 'current');
    }
    else {
        document.getElementById(parentLink).parentNode.setAttribute('class', 'current');
    }

}

function loadpage(hdnFieldId) {
    browver = parseInt(navigator.appVersion);
    browtype = navigator.appName;

    browsertype = "old";
    if (browtype == "Netscape" && !(browver < 3)) {
        browsertype = "new";
    }
    if (browtype == "Microsoft Internet Explorer" && !(browver < 4)) {
        browsertype = "new";
    }
    if (browsertype == "new") {

        thetimer = setTimeout("changeimage('" + hdnFieldId + "')", 3000);
        banneradcode = 0;
    }
}

function changeimage(hdnFieldId) {

    if (document.getElementById(hdnFieldId) == null) {
        return true;
    }
    listofBanner = document.getElementById(hdnFieldId).value.split(";");
    if (browsertype == "new") {
        banneradcode = banneradcode + 1

        if (banneradcode == (listofBanner.length)) {
            banneradcode = 0
        }

        imagesource = listofBanner[banneradcode]

        window.document.bannerad.src = imagesource
        thetimer = setTimeout("changeimage('" + hdnFieldId + "')", 5000);
    }
    else if (browsertype == "old") {
    }
}

function changepage() {
    if (browsertype == "new") {
        if (banneradcode == 0) {
            newlocation = "http://www.babylon6.demon.co.uk/graphics.html"
        }
        else if (banneradcode == 1) {
            newlocation = "http://www.babylon6.demon.co.uk/get_animgifs.html"
        }
        else if (banneradcode == 2) {
            newlocation = "http://www.bmc.org.uk/"
        }
        else if (banneradcode == 3) {
            newlocation = "http://www.babylon6.demon.co.uk/"
        }
        location = newlocation
    }
    else if (browsertype == "old") {
        location = "http://www.babylon6.demon.co.uk/graphics.html"
    }
}

function ShowMe(imgUrl) {
    document.getElementById("ctl00_ContentPlaceHolder1_imgBig").src = imgUrl;
}

function OpenSearchPage(source, eventArgs) {
    var applicationPath = document.getElementById("ctl00_hdnApplicationPath").value;
    
    //window.location = applicationPath + "/SearchLanding.aspx?qry=" + eventArgs.get_text();
    
    //William 25/02/2011
    //Seting the new advsearch page as default search page
    window.location = applicationPath + "/AdvSearch.aspx?qry=" + eventArgs.get_text();
}

function OpenWindow(url) {
    window.open(url, null, '');
}

function PrintProduct(productId) {
    var applicationPath = document.getElementById("ctl00_hdnApplicationPath").value;
    window.open(applicationPath + '/PrintProduct.aspx?id=' + productId);


}

function SetProductID(source, eventArgs) {
    var hdn = document.getElementById("ctl00_ContentPlaceHolder1_tbcntAccounts_tbpnlFright_hdnProductId");
    if (hdn != null) {
        hdn.value = eventArgs.get_value();
    }
}


function OnTreeClick(evt) {
    var src = window.event != window.undefined ? window.event.srcElement : evt.target;
    var isChkBoxClick = (src.tagName.toLowerCase() == "input" && src.type == "checkbox");
    if (isChkBoxClick) {
        var parentTable = GetParentByTagName("table", src);
        var nxtSibling = parentTable.nextSibling;
        if (nxtSibling && nxtSibling.nodeType == 1)//check if nxt sibling is not null & is an element node
        {
            if (nxtSibling.tagName.toLowerCase() == "div") //if node has children
            {
                //check or uncheck children at all levels
                CheckUncheckChildren(parentTable.nextSibling, src.checked);
            }
        }
        //check or uncheck parents at all levels
        if (src.checked == true)
            CheckParents(src, src.checked);
    }
}

function CheckUncheckChildren(childContainer, check) {
    var childChkBoxes = childContainer.getElementsByTagName("input");
    var childChkBoxCount = childChkBoxes.length;
    for (var i = 0; i < childChkBoxCount; i++) {
        childChkBoxes[i].checked = check;
    }
}

function CheckParents(srcChild, check) {
    var checkUncheckSwitch = true;
    var parentDiv = GetParentByTagName("div", srcChild);
    var parentNodeTable = parentDiv.previousSibling;
    if (parentNodeTable) {
        var isAllSiblingsChecked = AreAllSiblingsChecked(srcChild);
        if (isAllSiblingsChecked)
            checkUncheckSwitch = true;

        var inpElemsInParentTable = parentNodeTable.getElementsByTagName("input");
        if (inpElemsInParentTable.length > 0) {
            var parentNodeChkBox = inpElemsInParentTable[0];
            parentNodeChkBox.checked = checkUncheckSwitch;
            //do the same recursively
            CheckParents(parentNodeChkBox, checkUncheckSwitch);
        }
    }
}



function AreAllSiblingsChecked(chkBox) {
    var parentDiv = GetParentByTagName("div", chkBox);
    var childCount = parentDiv.childNodes.length;
    for (var i = 0; i < childCount; i++) {
        if (parentDiv.childNodes[i].nodeType == 1) //check if the child node is an element node
        {
            if (parentDiv.childNodes[i].tagName.toLowerCase() == "table") {
                var prevChkBox = parentDiv.childNodes[i].getElementsByTagName("input")[0];
                //if any of sibling nodes are not checked, return false
                if (!prevChkBox.checked) {
                    return false;
                }
            }
        }
    }
    return true;
}

//utility function to get the container of an element by tagname
function GetParentByTagName(parentTagName, childElementObj) {
    var parent = childElementObj.parentNode;
    while (parent.tagName.toLowerCase() != parentTagName.toLowerCase()) {
        parent = parent.parentNode;
    }
    return parent;
}




function ShowBigImage(e, image) {

    var bigImage = document.getElementById("bigImage");
    if (bigImage != null) {

        if (navigator.appName == "Microsoft Internet Explorer") {
            //                    bigImage.style.left = e.x + "px";      
            //                    bigImage.style.bottom = e.y + "px";     
            bigImage.style.left = "160px";
            bigImage.style.top = "0px";
        }
        else {
            //                    bigImage.style.left = e.clientX  + "px";   
            //                    bigImage.style.bottom = e.clientY  + "px";   
            bigImage.style.left = "160px";
            bigImage.style.top = "0px";
        }

        bigImage.style.display = "";
        var applicationPath = document.getElementById("ctl00_hdnApplicationPath").value;

        document.getElementById("ctl00_ContentPlaceHolder1_Img2").src = applicationPath + "/ProductImages/" + image;
    }
}


function HideBigImage() {

    document.getElementById("bigImage").style.display = "none";
}


function AddCheckToList(hidnFieldId, productId, checkBoxClientId) 
{
    var hidnFlied = document.getElementById(hidnFieldId);

    if (hidnFlied != null) {
        var checkBox = document.getElementById(checkBoxClientId);
        if (checkBox != null) {
            if (checkBox.checked) {
                hidnFlied.value += productId + ",";
            }
            else {
                if (hidnFlied.value.indexOf(productId) > -1) {
                    hidnFlied.value = hidnFlied.value.replace((productId + ","), "");
                }
            }
        }
    }
}

function SelectAllList(lstBoxId, checkBoxId) {
    var lstBox = document.getElementById(lstBoxId);
    var checkBox = document.getElementById(checkBoxId);

    if (lstBox != null && checkBox.checked == true) {
        for (var i = 0; i < lstBox.options.length; i++) {
            lstBox.options[i].selected = true;
        }
    }
    else if (lstBox != null && checkBox.checked == false) {
        for (var i = 0; i < lstBox.options.length; i++) {
            lstBox.options[i].selected = false;
        }
    }
}


function ShowPriceListBox(checkBoxId, divId) {
    var checkBox = document.getElementById(checkBoxId);
    if (checkBox != null) {

        var div = document.getElementById(divId);
        if (div != null) {
            if (checkBox.checked) {
                div.style.display = "block";
            }
            else {
                div.style.display = "none";
            }
        }
    }
}


function ShowBodyText(lblBodyTextId) {
    var lblBodyText = document.getElementById(lblBodyTextId);
    if (lblBodyText != null) {
        lblBodyText.style.display = "block";
    }
}

   function ValidateStringLength(source, arguments)  
   {  
        var slen = arguments.Value.length;  
        // alert(arguments.Value + '\n' + slen);  
        if (slen >= 0 && slen <= 200){  
            arguments.IsValid = true;  
        } else {  
            arguments.IsValid = false;  
        }  
   }  
