﻿//compare functionality
var compareCodes = new Array();

function UpdateCompareCode(elem, value) {
    if (elem.checked == true) {
        if (compareCodes.length < 4) {
            compareCodes.push(value);
        }
        else {
            elem.checked = false;
        }
    }
    else {
        compareCodes = removeElem(compareCodes, value);
    }
}

function removeElem(array, elem) {
    var result = new Array();
    for (var i = 0; i < array.length; i++) {
        if (array[i] != elem) {
            result.push(array[i]);
        }
    }

    return result;
}

function compareProducts() {
    if (compareCodes.length > 1) {
        var ext = compareCodes.join("|")
        window.location = 'compareproducts?codes=' + ext;
    }
}


//secondary attribute panel
var secondaryModal = '';

function ExpandFilter(label) {
    var expandDivName = 'Expanded' + label;
    var enclosedDivName = 'Enclosed' + label;
    
    $get(expandDivName).style.display = 'inline-block';
    $get(enclosedDivName).style.display = 'none';
}

function EncloseFilter(label) {
    var expandDivName = 'Expanded' + label;
    var enclosedDivName = 'Enclosed' + label;
    
    $get(expandDivName).style.display = 'none';
    $get(enclosedDivName).style.display = 'inline-block';
}

function OpenDiv(divName) {
    if (secondaryModal != '') {
        CloseDiv(secondaryModal);
    }
    secondaryModal = divName;
    $get(secondaryModal).style.display = 'inline';
}

function CloseDiv(divName) {
    //refresh the filter
    CurrentPage = '1';
    ManageFilterExecute();
    //close the modal popup
    $get(secondaryModal).style.display = 'none';
    secondaryModal = '';
}

function CloseDivNoApply(divName) {
    //close the modal popup
    $get(secondaryModal).style.display = 'none';
    secondaryModal = '';
}

  //function to fix radio button issues in the customised repeater
    function UncheckBoxes(){   
		if (!$get("ProductFilter")) return;
		
		var FilterOn = "";
		
		if (typeof(URLFilterSelection) != "undefined")
            FilterOn = URLFilterSelection;
		
        var CheckBoxes = $get("ProductFilter").getElementsByTagName("input");
        for (i = 0; i < CheckBoxes.length; i++)
        {
            if (CheckBoxes[i].type === "checkbox")
            {    
                if (CheckBoxes[i].parentNode.getAttribute("AttributeValue") != FilterOn)
	                CheckBoxes[i].checked = false ;
            }
        }

    }
    
    function BuildAttributes()
    {
        var Attributes = new Array();
        var AdditionalFilters = new Array();
        var Found = false;
        if ($get("ProductFilter") != null) {  
        
              
        var CheckBoxes = $get("ProductFilter").getElementsByTagName("input");
            for (i = 0; i < CheckBoxes.length; i++) {
                if (CheckBoxes[i].type === "checkbox") {
                    if (CheckBoxes[i].checked) {
                        var Name = CheckBoxes[i].parentNode.getAttribute("AttributeName");
                        var Value = CheckBoxes[i].parentNode.getAttribute("AttributeValue");
                        var SecondaryName = CheckBoxes[i].parentNode.getAttribute("SecondaryName");
                        var SecondaryCount = CheckBoxes[i].parentNode.getAttribute("SecondaryCount");
                        var SecondaryPrefix = CheckBoxes[i].parentNode.getAttribute("SecondaryPrefix");
                        if (Name != "Ignore") {
                            if (CheckBoxes[i].parentNode.getAttribute("IsAdditionalFilter") == "false") {
                                Found = false;
                                for (i2 = 0; i2 < Attributes.length; i2++) {
                                    if (Attributes[i2].AttributeName === Name) {
                                        Found = true;
                                        Attributes[i2].AttributeValues[Attributes[i2].AttributeValues.length] = new Object();
                                        Attributes[i2].AttributeValues[Attributes[i2].AttributeValues.length - 1].Value = Value;
                                        Attributes[i2].AttributeValues[Attributes[i2].AttributeValues.length - 1].SecondaryAttribute = new Object();
                                        Attributes[i2].AttributeValues[Attributes[i2].AttributeValues.length - 1].SecondaryAttribute.AttributeValues = new Array();
                                        if (SecondaryName != "") {
                                            Attributes[i2].AttributeValues[Attributes[i2].AttributeValues.length - 1].SecondaryAttribute.AttributeName = SecondaryName;
                                            //add secondary values
                                            for (i3 = 0; i3 < SecondaryCount; i3++) {
                                                var cbID = "SecAtt" + SecondaryPrefix + i3;
                                                var cb = $get(cbID);
                                                if (cb.checked) {
                                                    var SecValue = cb.parentNode.getAttribute("AttributeValue");
                                                    Attributes[i2].AttributeValues[Attributes[i2].AttributeValues.length - 1].SecondaryAttribute.AttributeValues[Attributes[i2].AttributeValues[Attributes[i2].AttributeValues.length - 1].SecondaryAttribute.AttributeValues.length] = new Object();
                                                    Attributes[i2].AttributeValues[Attributes[i2].AttributeValues.length - 1].SecondaryAttribute.AttributeValues[Attributes[i2].AttributeValues[Attributes[i2].AttributeValues.length - 1].SecondaryAttribute.AttributeValues.length - 1].Value = SecValue;
                                                }
                                            }
                                        }


                                        break;



                                    }
                                }

                                if (!Found) {
                                    Attributes[Attributes.length] = new Object();
                                    Attributes[Attributes.length - 1].AttributeName = Name;
                                    Attributes[Attributes.length - 1].AttributeValues = new Array();
                                    Attributes[Attributes.length - 1].AttributeValues[0] = new Object();
                                    Attributes[Attributes.length - 1].AttributeValues[0].Value = Value;
                                    Attributes[Attributes.length - 1].AttributeValues[0].SecondaryAttribute = new Object();
                                    Attributes[Attributes.length - 1].AttributeValues[0].SecondaryAttribute.AttributeValues = new Array();
                                    if (SecondaryName != "") {
                                        Attributes[Attributes.length - 1].AttributeValues[0].SecondaryAttribute.AttributeName = SecondaryName;
                                        //add secondary values
                                        for (i3 = 0; i3 < SecondaryCount; i3++) {
                                            var cbID = "SecAtt" + SecondaryPrefix + i3;
                                            var cb = $get(cbID);
                                            if (cb.checked) {
                                                var SecValue = cb.parentNode.getAttribute("AttributeValue");
                                                Attributes[Attributes.length - 1].AttributeValues[0].SecondaryAttribute.AttributeValues[Attributes[Attributes.length - 1].AttributeValues[0].SecondaryAttribute.AttributeValues.length] = new Object();
                                                Attributes[Attributes.length - 1].AttributeValues[0].SecondaryAttribute.AttributeValues[Attributes[Attributes.length - 1].AttributeValues[0].SecondaryAttribute.AttributeValues.length - 1].Value = SecValue;
                                            }
                                        }
                                    }

                                }
                            }
                            else {

                                Found = false;
                                for (i2 = 0; i2 < AdditionalFilters.length; i2++) {
                                    if (AdditionalFilters[i2].AttributeName === Name) {
                                        AdditionalFilters[i2].AttributeValues[AdditionalFilters[i2].AttributeValues.length].Value = Value;
                                        Found = true;
                                        break;
                                    }
                                }

                                if (!Found) {
                                    AdditionalFilters[AdditionalFilters.length] = new Object();
                                    AdditionalFilters[AdditionalFilters.length - 1].AttributeName = Name;
                                    AdditionalFilters[AdditionalFilters.length - 1].AttributeValues = new Array();

                                    AdditionalFilters[AdditionalFilters.length - 1].AttributeValues[0] = new Object();
                                    AdditionalFilters[AdditionalFilters.length - 1].AttributeValues[0].Value = Value;
                                    AdditionalFilters[AdditionalFilters.length - 1].AttributeValues[0].SecondaryAttributes = new Array();
                                }

                            }
                        }
                    }
                }
            }
            
        }
        
        // Update the filtermanager
        FilterMGR.Attributes = Attributes;
        FilterMGR.AdditionalFilters = AdditionalFilters;
      
    }


    function OnClientBeforeValueChange(sender, args)
    {
        var NewValue = args.get_newValue();
        var isFirstHandleActive = GetActiveDragHandle(sender);
        if (isFirstHandleActive)
        {
            $get("PriceFilterStartValue").innerHTML = "£" + NewValue;
            FilterMGR.MinPrice = NewValue;
        }
        else
        {
            $get("PriceFilterEndValue").innerHTML = "£" + NewValue;
            FilterMGR.MaxPrice = NewValue;
        }

    }

    function OnClientBeforeValueChangeABV(sender, args) {
        var NewValue = args.get_newValue();
        var isFirstHandleActive = GetActiveDragHandle(sender);
        if (isFirstHandleActive) {
            $get("ABVPriceFilterStartValue").innerHTML = NewValue;
            FilterMGR.MinABV = NewValue;
        }
        else {
            $get("ABVPriceFilterEndValue").innerHTML = NewValue;
            FilterMGR.MaxABV = NewValue;
        }

    }
    
    function GetActiveDragHandle(slider)
    {
        var dragHandles = slider.get_dragHandles();
        var activeDragHandle = slider.get_activeHandle();

        // The first drag handle is active.
        if(activeDragHandle === dragHandles[0]) return true;
        // The second drag handle is active.
        return false;
    }
    
    
    function OnClientSlideEnd(sender, args)
    {
        ManageFilterExecute();
    }

    function CheckBoxClicked(name, value) {
        CurrentPage = '1';
        ManageFilterExecute();
    }

    function CheckBoxClickedSecondary(elem,divname, name, value) {
        if (elem.checked) {
            $get(divname).style.display = 'inline';
        }
        else {
            $get(divname).style.display = 'none';
        }
        CheckBoxClicked(name, value);
    }
    
    var FilterTimeOut;
    var d;
    function ManageFilterExecute()
    {
        d = new Date().format("yyyyMMddhhmmssfff");
        
        //changeOpac(15,"ProductList");
        $get("ProductList").style.display = "none";
        $get("ProductListAjaxLoader").style.display = "block";
        
        if (FilterTimeOut != null)
            clearTimeout(FilterTimeOut);
        FilterTimeOut = setTimeout("GetProducts()",500);
    }
    
    var ProcuessingProducts = false;
    
    function GetProducts()
    {
        d = new Date().format("yyyyMMddhhmmssfff");
        BuildAttributes();
        MainWebService.GetFilter(PageID, FilterMGR, SortBySelection, d, CurrentPorductsPerPage, CurrentPage, SearchTerm,BrandID, GetFilter_Response)
    }
    
    
    function GetFilter_Response(ResultSet)
    {   
        if (ResultSet[0] === d)
        {
            if(ResultSet[1] != "[CANCELUPDATE]")
            {
                $get("ProdListHolder").innerHTML = ResultSet[1];
            }   
            $get("ProductListAjaxLoader").style.display = "none";
            $get("ProductList").style.display = "block";

            SetIconImages();
            ScrollToTop();
        }
    }

    function ScrollToTop() {

        scroll(0, 0);
    
    }
    function changeSortBy(dropdown) {
        SortBySelection = dropdown.options[dropdown.selectedIndex].value;
        CurrentPage = '1';
        ManageFilterExecute();
    }

    function changeProductsPerPage(dropdown) {
        CurrentPorductsPerPage = dropdown.options[dropdown.selectedIndex].value;
        CurrentPage = '1';
        ManageFilterExecute();
    }

    function viewAllProducts() {
        CurrentPorductsPerPage = '0';
        CurrentPage = '1';
        ManageFilterExecute();
    }

    function changePage(pageNo) {
        CurrentPage = pageNo;
        ManageFilterExecute();
    }
    
    
    //change the opacity for different browsers 
    function changeOpac(opacity, id) { 
        var object = document.getElementById(id).style; 
        object.opacity = (opacity / 100); 
        object.MozOpacity = (opacity / 100); 
        object.KhtmlOpacity = (opacity / 100); 
        object.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + opacity + ")";
    }



    var ListType = "GRID";

    function ChangeListType(type) {
        if (type != ListType) {
            if (ListType == "GRID") {
                ListType = "LIST";
            } else {
                ListType = "GRID";
            }
            SetIconImages();
        }
    }

    function SetIconImages() {
        if (ListType == "LIST") {
            document.getElementById('ProdListHolder').className = "ProdListStandard";
        } else {
            document.getElementById('ProdListHolder').className = "ProdListGrid";
        }

    }
