﻿function ChangeClass(obj)
{
    if(obj.className=='size_list_text_selected')
    {
        obj.className='size_list_text';
    }
    else
    {
        obj.className='size_list_text_selected';
    }
     
}
function querySt(ji) {
hu = window.location.search.substring(1);
gy = hu.split("&");
for (i=0;i<gy.length;i++) {
ft = gy[i].split("=");
if (ft[0] == ji) {
return ft[1];
        }
    }
    return '';
}
function SubmitClick(type)
{
var params="";
var urlint=[]
var counter=0;
var currp=querySt('params');
if(currp!=null && currp!='')
{
    var stg1=currp.split(';');
    for(var i=0;i<stg1.length;i++)
    {
           var stg2=stg1[i].split('-');
           if(stg2.length>0)
           {
                var sfid=parseInt(stg2[0].replace("pcid",""));
                sfid=parseInt(sfid);
                if(sfid!=='NaN')
                {
                    var stg3=stg2[1].split(',');
                    for(var j=0;j<stg3.length;j++)
                    {
                        var sfv=stg3[j].split('|');
                        var smd=sfv[0];
                        var sfv=sfv[1];
                        
                        urlint[counter]=sfid+'-'+smd+'-'+sfv;
                        counter++;
                    }
                }
           }
    }
//params=currp;
}
var currentparam="";
var controlname=''
if(type=='sub')
{
controlname='size_guide_contant_sub';
}
else
{
controlname='size_guide_contant';
}
var divMain=document.getElementById(controlname);
var alink=document.getElementById(controlname).getElementsByTagName("a");
//var alink=divMain.getElementsByName("alink");
var catnamearr=[];
for(var i=0;i<alink.length;i++)
{
    for( var x = 0; x < alink[i].attributes.length; x++ ) 
    {
      if( alink[i].attributes[x].nodeName.toLowerCase() == 'internalid' ) {
      var str=alink[i].attributes[x].nodeValue.split("-");
      catnamearr[str[0]]=str[3]; 
        var isp=false; //selected node is already there in url
        var indx=-1;
        var j=0;
        for(j;j<urlint.length;j++)
        {
            if(urlint[j]==str[0]+'-'+str[1]+'-'+str[2])
            {
                isp=true; indx=j;
            }
        }
        if(isp==true && alink[i].className!='size_list_text_selected')//then remove it
        {
            urlint[indx]='';
        }
        if(isp==false && alink[i].className=='size_list_text_selected') //ok then add it if its selected
        {
            urlint[j]=str[0]+'-'+str[1]+'-'+str[2];
        }
      }
    }
}
for(var j=0;j<urlint.length;j++)
{
    if(urlint[j]!='')
    {
        var str=urlint[j].split('-');
        
        currentparam=getParams(str[0],str[1],str[2], currentparam);
    }
}
if(params.indexOf(";")>0)
{
    params=params.substring(0,params.length-1);
}
//alert(currentparam);
var loc=false;
    if(type=='sub')
    {
//        var currp=querySt('productcategoryid');
//            if(currp!=null && currp!='')
//            {
//                return currp;
//            }
//            else
//            {
                var pname=location.pathname;
                var startindx=pname.indexOf('/Catalogue/');
                if(startindx>=0)
                {
                loc=true;
                    //alert('1: '+pname+"?params="+currentparam);
                    if(currentparam!='') {currentparam="?params="+currentparam;}
                    //alert('1: '+pname+currentparam);
                    window.location.href=pname+currentparam;
                }
//            } 
      }
    else
    {
        var productcatid;
        if(loc==false)
        {
        productcatid=GetFilterProductCategoryID(currentparam);
        if(productcatid=='')
        {
        productcatid=925;
        }
        var catname='WoMen';
        
        
        if(catnamearr[productcatid]!=null)
        {
        catname=catnamearr[productcatid].replace("##","-");
        }
        if(currentparam!='') {currentparam="?params="+currentparam;}
        window.location.href="/Catalogue/Category_"+productcatid+"_"+catname+".aspx"+currentparam;
        //alert('2: '+"/Catalogue/Category_"+productcatid+"_"+catname+".aspx?params="+currentparam);
        }
    }
}
function getParams(pcid,md,atvid, currentparam1)
{
//alert('currentparam1: '+currentparam1);
    if(currentparam1!=='')
    {
    
    var fvQueryString='';
    //pcstart: product class start
    //pcend: product class end
        var pcstart = -1;
        
                pcstart = currentparam1.indexOf('pcid'+pcid+ '-');// +pcidLength + 1;  
                //alert('pcstart: '+pcstart);
            
            var pcend = -1;
            if (pcstart >= 0)
            {
                pcend = currentparam1.indexOf(';', pcstart);
                //alert('pcend1: '+pcend);
                if (pcend < 0) //';' not found
                {
                    pcend = currentparam1.length;
                    //alert('pcend2: '+pcend);
                }
            }
            else
            {
                pcend = currentparam1.length;
                //alert('pcend3: '+pcend);
            }
            
            if (pcstart >= 0 && pcend > pcstart)
            {
                var newGNFVForThisFilter = '';
                var oldGNFVForThisFilter = currentparam1.substring(pcstart, pcend);
                //alert('pcstart:'+pcstart);
                //alert('pcend - pcstart:'+(pcend - pcstart));
                //alert('currentparam1:'+currentparam1);
                //alert('oldGNFVForThisFilter='+oldGNFVForThisFilter);
                var pcidInt = 0;
                
                pcidInt=parseInt(pcid);
                //alert('pcidInt:'+pcidInt);
                if (pcidInt=='NaN')
                {
                    newGNFVForThisFilter = 'pcid'+ pcid+  '-'+ md+"|"+atvid; 
                }
                else
                {
                    newGNFVForThisFilter = oldGNFVForThisFilter+ ','+ md+"|"+atvid; 
                }
                //alert('newGNFVForThisFilter:'+newGNFVForThisFilter);
                fvQueryString = currentparam1.replace(oldGNFVForThisFilter, newGNFVForThisFilter);
                //alert('fvQueryString1:'+fvQueryString);
            }
            else
            {
                //this filter does not exist
                fvQueryString = currentparam1+ ';'+ 'pcid'+ pcid+ '-'+ md+"|"+atvid; 
                //alert('fvQueryString2:'+fvQueryString);
            }
        }
        else
        {
            fvQueryString = 'pcid'+pcid+ '-'+ md+"|"+atvid; 
            //alert('fvQueryString3:'+fvQueryString);
        }
        //alert('return fvQueryString:'+fvQueryString);
        return fvQueryString;
}
function GetFilterProductCategoryID(gnString)
    {
        if (gnString!=null && gnString!='' && gnString.indexOf("pcid") !=-1)
        {
            if (gnString.indexOf("pcid") != -1 && gnString.lastIndexOf("pcid") != -1 && gnString.indexOf("pcid") == gnString.lastIndexOf("pcid"))
            {
                var filters = gnString.split(";");
                for(var i=0;i<filters.length;i++)
                {
                    var filterSelections = filters[i].split("-");
                    if (filterSelections.length > 0)
                    {
                    
                        var filterIdString = filterSelections[0].replace("pcid", "");
                        var filterId = 0;
                        filterId=parseInt(filterIdString);
                        if (filterId!='NaN')
                         return filterId; 
                    }
                }
            }
        }
        return '';
    }
function ClearClick(type)
{
if(type=='sub')
{
window.location.href=window.location.pathname;
//    var params="";
//    var divMain=document.getElementById('size_guide_contant_sub');
//    var alink=document.getElementById('size_guide_contant_sub').getElementsByTagName("a");
//    //var alink=divMain.getElementsByName("alink");
//    for(var i=0;i<alink.length;i++)
//    {
//        if(alink[i].className=='size_list_text_selected')
//        {
//            alink[i].className='size_list_text';

//        }
//    }
//    SubmitClick('sub');
}
else
{
window.location.href=window.location.pathname;
}

}
