﻿var theForm = document.forms['aspnetForm'];
if (!theForm) {
    theForm = document.aspnetForm;
}

// Find feature by Id from array and set visibility
function setFilter(featureId, checked) {
    var products;
    if (ProductArray) products = ProductArray;
    if (!products) return false;

    var featureIds = theForm.hFieldFeatureFilter.value;

    // Set hidden field value with feature'id
    if (checked) {
        // Add ItemNo if not already added
        if (featureIds.indexOf(featureId) == -1) {
            if (featureIds) featureIds += ',' + featureId;
            else featureIds = featureId;
        }
    }
    else {
        // Delete featureId from list
        featureIds = removeCsvVal(featureIds, featureId);
    }

    theForm.hFieldFeatureFilter.value = featureIds;

    for (var productIndex = 0; productIndex < products.length; productIndex++) {
        var removeProduct = false;
        var splitFeatures = new Array();
        splitFeatures = featureIds.split(',');

        if (featureIds.length > 0) {
            // Only process product features when any of the feature filter checkboxes are selected
            for (var featureIndex = 0; featureIndex < splitFeatures.length; featureIndex++) {
                var featureName = "f" + splitFeatures[featureIndex];
                var featureValue = products[productIndex][featureName];
                if (featureValue) {
                    if (featureValue.toUpperCase() == "N") {
                        removeProduct = true;
                    }
                }
                else {
                    removeProduct = true;
                }
            }
        }

        var elementId = "Product" + products[productIndex].id;
        if (document.getElementById(elementId)) {
            if (removeProduct == true)
                document.getElementById(elementId).style.display = "none";
            else
                document.getElementById(elementId).style.display = "block";
        }
    }

    // Count products and show message when 0
    var productCounter = 0;
    var o = document.getElementById('ctl00_ContentPlaceHolder1_divProductList').getElementsByTagName('div');
    for (var i = 0; i < o.length; i++) {
        if (o[i].className == "table-cell") {
            if (o[i].style.display != "none")
                productCounter++;
        }
    }

    if (productCounter == 0) {
        // Set translations
        document.getElementById('divNoProducts').innerHTML = document.getElementById('ctl00_ContentPlaceHolder1_featurefilter_hFieldFeatureFilterNoProducts').value;
        // Hide buttons and show message
        document.getElementById('divNoProducts').style.display = "block";
        document.getElementById('ctl00_ContentPlaceHolder1_featurefilter_compare').style.display = "none";
    }
    else {
        // Show buttons and hide message
        document.getElementById('divNoProducts').style.display = "none";
        document.getElementById('ctl00_ContentPlaceHolder1_featurefilter_compare').style.display = "block";
    }
}

// Set visibility of product by ItemNo
function toggleLayer(itemNo) {
    var elem, vis;
    if (document.getElementById) // this is the way the standards work
        elem = document.getElementById(itemNo);
    else if (document.all) // this is the way old msie versions work
        elem = document.all[itemNo];
    else if (document.layers) // this is the way nn4 works
        elem = document.layers[itemNo];
    vis = elem.style;
    // if the style.display value is blank we try to figure it out here
    if (vis.display == '' && elem.offsetWidth != undefined && elem.offsetHeight != undefined)
        vis.display = (elem.offsetWidth != 0 && elem.offsetHeight != 0) ? 'block' : 'none';
    vis.display = (vis.display == '' || vis.display == 'block') ? 'none' : 'block';
}

// Note that if the source is not a proper CSV string, the function will return a blank string ("").
function removeCsvVal(source, toRemove) {           //source is a string of comma-seperated values,
    //toRemove is the CSV to remove all instances of
    var sourceArr = source.split(",");              //Split the CSV's by commas
    var toReturn = "";                             //Declare the new string we're going to create
    for (var i = 0; i < sourceArr.length; i++)      //Check all of the elements in the array
    {
        if (sourceArr[i] != toRemove)               //If the item is not equal
            toReturn += sourceArr[i] + ",";         //add it to the return string
    }

    return toReturn.substr(0, toReturn.length - 1); //remove trailing comma
}
