﻿/*
This function will get the Ajax object to call the server.
*/
function GetXmlHttpObject() {
    var xmlHttp = null;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
        //Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}

/************ Script Funtions Use in Product Display ******************/

function submitForm(form) {
    form.submit();
}

/* RC Gallery - Variable to stored src of Default Img for Display Product */
var defaultImg;

/* RC Gallery - Used to set the default Image when Image is loaded for Display Product */
function setDefaultImg(defImg) {
    defaultImg = defImg;
}
/* RC Gallery - Called on mouseover, to change the image*/
function changeImg(selImg) {
    if ($('productInfoImg')) {
        $('productInfoImg').src = selImg;
    }
}
/* RC Gallery - Called to again set the default image*/
function defImg() {
    if ($('productInfoImg')) {
        $('productInfoImg').src = defaultImg;
    }
}

// Remove the leading and traling spaces from string text.
function trim(value) { var temp = value; var obj = /^(\s*)([\W\w]*)(\b\s*$)/; if (obj.test(temp)) { temp = temp.replace(obj, '$2'); } var obj = /  /g; while (temp.match(obj)) { temp = temp.replace(obj, " "); } return temp; }

/* Make Tab Style Default */
function defaultTab(tabID, left, mid, right) {

    var tab = document.getElementById(tabID).className = "tab0"; ;
    var lf = document.getElementById(left).className = "tab0L"; ;
    var md = document.getElementById(mid).className = "tab0M"; ;
    var rt = document.getElementById(right).className = "tab0R";

}
/* Make Tab Style Active */
function enableTab(tabID, left, mid, right) {

    var tab = document.getElementById(tabID).className = "tab1"; ;
    var lf = document.getElementById(left).className = "tab1L"; ;
    var md = document.getElementById(mid).className = "tab1M"; ;
    var rt = document.getElementById(right).className = "tab1R";
}

/* Default is Overview Tab, so hide it */
var lastTabID = "otab";
var lastLF = "o1";
var lastMD = "o2";
var lastRT = "o3";
var lastConDiv = "overviewData";


/* Navigate Between the Tabs */
function navigateTab(tabID, conDiv, left, mid, right) {

    //alert("tabID: " + tabID + "  conDiv: " + conDiv + "  LF: " + left + "  MD: " + mid + "  RT: " + right);   
    //alert(conDiv);

    defaultTab(lastTabID, lastLF, lastMD, lastRT);  /*Makes the previous selected TAB as INACTIVE */
    hideTab(lastConDiv); /* Hides the previous selected contents of Tab */

    viewTab(conDiv); /*Makes the previous selected TAB as ACTIVE */
    enableTab(tabID, left, mid, right); /* Enables the newly selected contents of Tab */

    /* assinging the newly select tab to variables, so that it can be cleared on next click */
    lastConDiv = conDiv;
    lastTabID = tabID;
    lastLF = left;
    lastMD = mid;
    lastRT = right;
}

function viewTab(conDiv) {
    //alert("view: " + conDiv);
    var tab = document.getElementById(conDiv);
    tab.style.display = "block";
}

function hideTab(conDiv) {
    //alert("hide: " + conDiv);
    var tab = document.getElementById(conDiv);
    tab.style.display = "none";
}

/*
function viewTab(tab){
var tab = document.getElementById(tab);
tab.style.visibility='visible';
	
document.getElementById('overviewData').style.display='none';
document.getElementById('techspecsData').style.display='none';
document.getElementById('optionsData').style.display='none';
document.getElementById('similarData').style.display='none';
	
tab.style.display='block';
}
*/

function populateTechSpecsTab(productId) {
    //Empty Div Box have 13 Character 
    if ((document.getElementById("techspecsData").innerHTML).length > 13) {
        //Data is already populated just show the Div
        viewTab('techspecsData');
    } else {
        document.getElementById("techspecsData").innerHTML = "<div style='font-size:13px;' class=\"padT10 a12BlackB\">Loading...</div>";
        viewTab('techspecsData');
        //Populate Data using Ajax Request
        xmlHttp = GetXmlHttpObject()
        if (xmlHttp == null) {
            alert("Browser does not support HTTP Request");
            return;
        }
        var url = "product.aspx?divType=techSpecs&productId=" + productId;
        url = url + "&sid=" + Math.random()
        xmlHttp.onreadystatechange = function() {
            if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
                //Set populated data in DIV
                document.getElementById("techspecsData").innerHTML = xmlHttp.responseText;
                //Data is Populated show Div Now
                viewTab('techspecsData');
            }
        }
        xmlHttp.open("GET", url, true)
        xmlHttp.send(null)
    }
}

/************ Script Funtions Use in Search Results ******************/

function clearText(theField) {
    if (theField.defaultValue == theField.value)
        theField.value = '';
}

function addText(theField) {
    if (theField.value == '')
        theField.value = theField.defaultValue;
}

function searchTextHeader(form, keyCode) {
    if (keyCode == 13) { validateSearchKeywordHeader(form); }
}

function filterKeywordSearch(form) {
    typeValue = "keywordFilter";
    form.type.value = typeValue;
}

/* This function the keyword enter in the keyword search input box */
function validateSearchKeywordHeader(form) {
    keywordValue = form.keywords.value.replace(/^(\s)*/, '');
    keywordValue = form.keywords.value.replace(/(\s)*$/, '');
    if (keywordValue == '' || keywordValue == 'Enter Keyword' || keywordValue == 'keywords') {
        // DO NOTHING
    } else {
        //escape method don't encode / so we will encode it 
        keywordValue = keywordValue.replace("/", "%2F");
        form.submit();
    }
}

/* This function populates filters list thorugh Ajax based call */
function populateFilters(url, flid) {

    if ((document.getElementById(flid).innerHTML).length > 0) {
        var list = document.getElementById(flid);
        list.style.display = "block";
    } else {
        document.getElementById(flid).innerHTML = "<div class=\"a11Blue\">Loading...</div>";
        var list = document.getElementById(flid);
        list.style.display = "block";

        //Populate Data using Ajax Request
        xmlHttp = GetXmlHttpObject()
        if (xmlHttp == null) {
            alert("Browser does not support HTTP Request")
            return
        }

        xmlHttp.onreadystatechange = function() {
            if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
                //Set populated data in DIV
                document.getElementById(flid).innerHTML = xmlHttp.responseText;
                //Data is Populated show Div Now
                var list = document.getElementById(flid);
                list.style.display = "block";
            }
        }
        xmlHttp.open("GET", url, true)
        xmlHttp.send(null)
    }
}

/*
This function passes the combo box value to the hiddel field inside Attribute Filters
*/
function changeCombo(val) {
    $('operator').value = val;
}

function changeAttrCombo(val) {
    $('attrValue').value = val;
}

/* This function provides the functionality to Expand/Collapse filters list */
function hideShowFilters(div, show, hide, fold) {
    if (fold == 'true')
        new Effect.BlindUp(div, { duration: 0.2 });
    Element.hide(hide);
    Element.show(show);
}

/* This function is used to set extra paramters pass on every page ins earch results */
function paginateSearchResults(url) {
    window.location = url; // + '&compareIds=' + $('productIds').value
}

/************ Script Funtions Use in Rich Contents Display ******************/

// RC Moving Images Code

function subarray(start, end, arr) {
    var subarray = new Array();
    if (arr.length >= end) {
        for (var i = start; i <= end; i++) {
            subarray.push(arr[i]);
        }
    }

    return subarray;
}

function setup() {
    if (rcimages.length <= numdisplay) {
        for (var i = 0; i < numdisplay; i++) {
            if ($("img" + i)) {
                $("img" + i).src = rcimages[i];
            }
        }

    } else {
        if (endindex < rcimages.length) {
            var subarr = subarray(startindex, endindex, rcimages);
            for (var i = 0; i < subarr.length; i++) {
                if ($("img" + i)) {
                    $("img" + i).src = subarr[i];
                }
            }
        }
    }
}


/* RC - Gallery - Navigation */
function moveimage(direction) {
    if (direction == 1) {
        if (endindex < rcimages.length) {
            endindex = endindex + 1;
            startindex = startindex + 1;
        }
    } else {
        if (startindex > 0) {
            startindex = startindex - 1;
            endindex = endindex - 1;
        }
    }

    if (startindex > 0) {
        $("leftArrow").hide();
        $("leftArrowActive").show();
    } else {
        $("leftArrow").show();
        $("leftArrowActive").hide();
    }

    if (endindex < (rcimages.length - 1)) {
        $("rightArrow").hide();
        $("rightArrowActive").show();
    } else {
        $("rightArrow").show();
        $("rightArrowActive").hide();
    }

    setup();
}


function showRCViewBox(boxid, e1, e2, e3) {
    if (showViewBox) {
        Effect.Fade($(boxid), { duration: 0.2 });
        showViewBox = false;
        resetStyle("blue", e1, e2, e3);
    } else {
        if (showDBox == true) {
            showDL = false;
            showRCDBox('dBox', 'd1', 'd2', 'd3');
        }
        richContent('vb');
        Effect.Appear($(boxid), { duration: 0.2 });
        showViewBox = true;
        changeStyle("blue", e1, e2, e3);
    }
}

function changeStyle(cl, e1, e2, e3) {
    if (cl == "blue") {
        $(e1).className = "btn1L";
        $(e2).className = "btn1M";
        $(e3).className = "btn1R";
    }
    if (cl == "gray") {
        $(e1).className = "btnGW1L";
        $(e2).className = "btnGW1M";
        $(e3).className = "btnGW1R";
    }
    if (cl == "blueBot") {
        $(e1).className = "btnBG1L";
        $(e2).className = "btnBG1M";
        $(e3).className = "btnBG1R";
    }

}

function resetStyle(cl, e1, e2, e3) {
    if (cl == "blue") {
        $(e1).className = "btn0L";
        $(e2).className = "btn0M";
        $(e3).className = "btn0R";
    }
    if (cl == "gray") {
        $(e1).className = "btnGW0L";
        $(e2).className = "btnGW0M";
        $(e3).className = "btnGW0R";
    }
    if (cl == "blueBot") {
        $(e1).className = "btnBG0L";
        $(e2).className = "btnBG0M";
        $(e3).className = "btnBG0R";
    }
}

function showRCDBox(boxid, e1, e2, e3) {
    if (showDBox) {
        Effect.Fade($(boxid), { duration: 0.2 });
        showDBox = false;
        resetStyle("blue", e1, e2, e3);
    } else {
        if (showViewBox == true) {
            showVB = false;
            showRCViewBox('viewBox', 'v1', 'v2', 'v3');
        }
        richContent('dl');
        Effect.Appear($(boxid), { duration: 0.2 });
        showDBox = true;
        changeStyle("blue", e1, e2, e3);
    }
}

function richContent(value) {
    if (value == "dl") {
        showDL = true;
    }
    else if (value == "vb") {
        showVB = true;
    }
}

/************ RC Gallery variables initialization here ******************/
showDBox = false;
showViewBox = false;
var rcimages = new Array();
var slideimages = new Array();
var numdisplay = 3;
var startindex = 0;
var endindex = numdisplay - 1;
var cartReg = false;
var lastId;

/************ Script Funtions Use in Product Comparison ******************/

/* This function is used when any product is remove from the selected product comparison */
function hidediv(a_id, b_id, c_id, d_id, z_id) {
    var lenght = $('len').innerHTML;
    if (lenght <= 2) {
        return false;
    }
    Effect.Fade($(a_id), { duration: 0.3 }); Effect.Fade($(b_id), { duration: 0.3 });
    Effect.Fade($(c_id), { duration: 0.3 }); Effect.Fade($(d_id), { duration: 0.3 });

    $(a_id).innerHTML = ''; $(b_id).innerHTML = '';
    $(c_id).innerHTML = ''; $(d_id).innerHTML = '';

    var zid = "";
    var count = $('rows').innerHTML;
    for (var index = 1; index <= count; index++) {
        zid = "z_" + index + "_" + z_id;
        Effect.Fade(zid, { duration: 0.3 });
        $(zid).innerHTML = '';
    }
    lenght = lenght - 1;
    $('len').innerHTML = lenght;

    if (lenght == 2) {
        for (var i = 1; i <= 4; i++) {
            aid = "a_" + i;
            if (a_id != aid) {
                var element = document.getElementById(aid);
                if (element != null)
                    document.getElementById(aid).innerHTML = '<span class="a12BlackB fltL"> Selected Product </span>';
            }
        }
    }
}

function hideRemove() {
    var lenght = $('len').innerHTML;
    if (lenght == 2) {
        for (var i = 1; i <= 2; i++) {
            aid = "a_" + i;
            var element = document.getElementById(aid);
            if (element != null)
                document.getElementById(aid).innerHTML = '<span class="a12BlackB fltL"> Selected Product </span>';
        }
    }
}

/* This function check the number of product valid for product comparison request. */
function validateComparison() {
    var msg = document.getElementById('errMsg');
    var pidstr = $('productIds').value
    var pids = pidstr.split(',');
    var len = pids.length - 1;
    if (len > 4) {
        msg.innerHTML = "<div class=\"a11BlueB\">Please restrict your choice to four products</div>";
        $('errMsgBot').innerHTML = "<div class=\"a11BlueB\">Please restrict your choice to four products</div>";
        msg.style.display = "block";
        $('errMsgBot').show();
        setTimeout("$('errMsgBot').hide()", 4500);
        setTimeout("$('errMsg').hide()", 4500);
        return false;
    } else if (len < 2) {
        msg.innerHTML = "<div class=\"a11BlueB\">Please select atleast two products for comparison</div>";
        $('errMsgBot').innerHTML = "<div class=\"a11BlueB\">Please select atleast two products for comparison</div>";
        msg.style.display = "block";
        $('errMsgBot').show();
        setTimeout("$('errMsgBot').hide()", 4500);
        setTimeout("$('errMsg').hide()", 4500);
        return false;
    } else {
        msg.style.display = "none";
        $('errMsgBot').hide();
    }
    return true;
}

/* 
This function will first validate the product ids selected and if valdated successfully send the product
to ProductComparison page and reset the selected comparison request.
*/
function processComparison() {
    if (validateComparison()) {
        setCompareIds();
        clearSelectedProducts('compareIds');
        document.getElementById('compare').submit();
        resetComparision();
    }
}

/* This function every selected comparison id in hidden feild. */
function setCompareIds() {
    var params = '';
    for (var i = 0; i < productIds.length; i++) {
        if (productIds[i] > 1) params += productIds[i] + ',';
    }
    $('productIds').value = params;
}

/* This selected ids array and hidden filed for comparison. */
function resetComparision() {
    productIds = null;
    productIds = new Array();
    $("compare").reset();
    $('productIds').value = '';
}

/* This function sets selected comparison ids in js array. */
function setForComparision(productId) {
    var index = indexInComparision(productId);

    if (index == -1)
        productIds[productIds.length] = productId;
    else {
        productIds[index] = null;
        productIds[index] = undefined;
    }
}

/* This function check whether selected id is alreay in js array. */
function indexInComparision(productId) {
    if (productId == null || productId == undefined) return -1;

    var index = -1;
    for (var i = 0; i < productIds.length; i++) {
        if (productIds[i] != null && productIds[i] == productId) {
            index = i;
            break;
        }
    }
    return index;
}

/* This function clears all selected checkboxes for products in search results page. */
function clearSelectedProducts(elementsName) {
    var elements = document.getElementsByName(elementsName);
    for (var i = 0; i < elements.length; i++) {
        elements[i].checked = false;
    }
}

/* This function is used to generate alternate color row in table */
function generateAlternateColor(elem) {

    var tbl = document.getElementById(elem).rows;
    for (i = 4; i < tbl.length; i++) {
        if ((i % 2) == 0)
            tbl[i].style.backgroundColor = "#f2f2f2"
    }



}

/* Product Ids array contians selected ids for product comparison */
var productIds = new Array();