/*
    This JavaScript has been extracted from reseller/navbar/search_bar.jsp
    to improve the speed performance of the navbar.
*/

function categoryList()
{	
	catlist = document.Topselector.categoryList;
	c=1;

    // SPECIAL PREPARATION IF MANUFACTUER IS CHOOSEN before going here'...
	mycat=':';

    if (manvalue!='')

    for (i = 0; i < subcats_length; i++)
	{
	    if (subcats[i].indexOf(manvalue)!=-1)
        {
            mycat=mycat+subcats[i].slice(0,2)+':';
        } 
	}

    // START OF CATEGORY CODE...
 	catlist.length = 0;

    for (i = -1; i < cats_length; i++)
	{
        if (i == -1) 
        {
          if (cattext != '' || catvalue != '') 
          {
              catlist.options[0] = new Option(cattext, catvalue);
          }
          else 
          {
              c=0;
          }
        }
		else
		{
            // case 1
			if (subcatvalue=='' & manvalue=='')
			{
                catlist.options[c] = new Option(cats[(i*2)+1],cats[(i*2)]);
                c=c+1;
			}

			// case 2
			if (subcatvalue!='' & manvalue=='')
			{
                if (subcatvalue.slice(0,2)==cats[(i*2)])
                {
                    catlist.options[c] = new Option(cats[(i*2)+1],cats[(i*2)]);
                    c=c+1;
                }
			}

			// case 3

			if (manvalue!='' & subcatvalue=='')
			{
                if (mycat.indexOf(cats[(i*2)])!=-1)
				{
                    catlist.options[c] = new Option(cats[(i*2)+1],cats[(i*2)]);
                    c=c+1;
                }
			}

			// case 4
			if (manvalue!='' & subcatvalue!='')
			{
                if (mycat.indexOf(cats[(i*2)])!=-1 & subcatvalue.slice(0,2)==cats[(i*2)])
                {
                    catlist.options[c] = new Option(cats[(i*2)+1],cats[(i*2)]);
                    c=c+1;
                }
			}
 		}
	}

    catlist.options[c] = new Option(filler,'');
}
function JcategoryList(VarValue)
{
    malist = document.Topselector.manufacturerList;
	sublist= document.Topselector.subCategoryList;
	catlist = document.Topselector.categoryList;
	catlist.options[catlist.selectedIndex].value = 0+VarValue;
	catvalue=catlist.options[catlist.selectedIndex].value;
	cattext=catlist.options[VarValue].text;
	
	if (cattext==filler)
	{
        init('---------Recherche par region--------', '---------Recherche par activite-------', '---------Recherche par departement----------', '------------ Remise a zero ---------------');
        resetSelectBox();
    }
	else
	{
	   subCategoryList ();
	   manufacturerList ();
	}   
}

function categoryList_onchange()
{
    malist = document.Topselector.manufacturerList;
	sublist= document.Topselector.subCategoryList;
	catlist = document.Topselector.categoryList;
	catvalue=catlist.options[catlist.selectedIndex].value;
	cattext=catlist.options[catlist.selectedIndex].text;
	//alert(catvalue);
    //alert(cattext);
	if (cattext==filler)
	{	
        init('---------Recherche par region--------', '---------Recherche par activite-------', '---------Recherche par departement----------', '------------ Remise a zero ---------------');
        resetSelectBox();
    }
	else
	{
	   subCategoryList ();
	   manufacturerList ();
	}
	click_Region(catvalue.substr(1,1));
}

function manufacturerList ()
{
    malist = document.Topselector.manufacturerList;
	sublist= document.Topselector.subCategoryList;
	catlist = document.Topselector.categoryList;
	c=1;

    // SPECIAL PREPARATION IF CAT IS CHOOSEN before going here...
	mycat=':';
	
    if (catvalue!='')
	{
        for (i = 0; i < subcats_length; i++)
	    {
            if (catvalue==subcats[i].slice(0,2))
	        {
                mycat=mycat+subcats[i].slice(subcats[i].indexOf(':'),subcats[i].length)+':';
            }
	    }
	}

    // SPECIAL PREPARATION IF SUBCAT IS CHOOSEN before going here...
    
	mycat2=':';
	if (subcatvalue!='')
	{	
		for (i = 0; i < subcats_length; i++)
	    {	
            if (subcatvalue==subcats[i].slice(0,4))
            {
				mycat2=mycat2+subcats[i].slice(subcats[i].indexOf(':'),subcats[i].length)+':';}
            }
            
    }
    
    malist.length = 0;
    for (i = -1; i < vendors_length; i++)
    {
        if (i == -1)
        {
            malist.options[0] = new Option(mantext,manvalue);
        }
        else
        {
            // case 1
            if (subcatvalue=='' & catvalue=='')
            {
                malist.options[c] = new Option(vendors[(i*2)+1],vendors[(i*2)]);
                sublist.selectedIndex = 0;
                catlist.selectedIndex = 0;
                c=c+1;
            }

            // case 2
	        if (subcatvalue=='' & catvalue!='')
	        {
                if (mycat.indexOf(vendors[(i*2)])!=-1)
                {
                    malist.options[c] = new Option(vendors[(i*2)+1],vendors[(i*2)]);
                    malist.selectedIndex = 0;
                    c=c+1;
                }
            }

            // case 3
            if (subcatvalue!='' & catvalue=='')
            {
                if (mycat2.indexOf(vendors[(i*2)])!=-1)
                {
                    malist.options[c] = new Option(vendors[(i*2)+1],vendors[(i*2)]);
                    c=c+1;
                }
            }

            // case 4
            if (subcatvalue!='' & catvalue!='')
            {
                if (mycat2.indexOf(vendors[(i*2)])!=-1 & mycat.indexOf(vendors[(i*2)])!=-1)
                {
                    malist.options[c] = new Option(vendors[(i*2)+1],vendors[(i*2)]);
	                malist.selectedIndex = 0;
	                malist.lenght = 0;
	                c=c+1;
                }
            }
        }
    }
    malist.options[c] = new Option(filler,'')
}

function manufacturerList_onchange() {
    malist = document.Topselector.manufacturerList;
    sublist= document.Topselector.subCategoryList;
    catlist = document.Topselector.categoryList;
    manvalue=':'+malist.options[malist.selectedIndex].value;
    mantext=malist.options[malist.selectedIndex].text;

    if (mantext==filler)
    {
      init('---------Recherche par region--------', '---------Recherche par activite-------', '---------Recherche par departement----------', '------------ Remise a zero ---------------');
      resetSelectBox();
    }
    else
    {
        categoryList ();
        subCategoryList ();
    }
}

function subCategoryList ()
{
    malist = document.Topselector.manufacturerList;
	sublist= document.Topselector.subCategoryList;
	catlist = document.Topselector.categoryList;
 	sublist.length = 0;
	c=1;
	for (i = -1; i < subcats_length; i++)
	{
					
        if (i == -1)
		{
		    sublist.options[0] = new Option(subcattext, subcatvalue);
		}
		else
		{
			// --------- option 1
			if (manvalue=='' & catvalue=='')
			{
				sublist.options[c] = new Option(subcats[i].slice(subcats[i].indexOf('-')+1,subcats[i].indexOf(':')),subcats[i].slice(0,subcats[i].indexOf('-')));
				c=c+1;
			}

			// --------- option 2
			if (manvalue=='' & catvalue!='')
			{
				if (catvalue==subcats[i].slice(0,2))
				{
					sublist.options[c] = new Option(subcats[i].slice(subcats[i].indexOf('-')+1,subcats[i].indexOf(':')),subcats[i].slice(0,subcats[i].indexOf('-')));
				 	sublist.selectedIndex = 0;
				 	c=c+1;
				}
			}
			// --------- option 3

			if (manvalue!='' & catvalue=='')
			{
				if (subcats[i].indexOf(manvalue)!=-1)
				{
					sublist.options[c] = new Option(subcats[i].slice(subcats[i].indexOf('-')+1,subcats[i].indexOf(':')),subcats[i].slice(0,subcats[i].indexOf('-')));
					c=c+1;
				}
			}
			// --------- option 4
			if (manvalue!='' & catvalue!='')
			{
				if (subcats[i].indexOf(manvalue)!=-1 & catvalue==subcats[i].slice(0,2))
				{
					sublist.options[c] = new Option(subcats[i].slice(subcats[i].indexOf('-')+1,subcats[i].indexOf(':')),subcats[i].slice(0,subcats[i].indexOf('-')));
					sublist.selectedIndex = 0;
					sublist.lenght = 0;
					c=c+1;
				}
			}
		}
	}
	sublist.options[c] = new Option(filler,'');
}
function JsubCategoryList(Var1,Var2)
{
	
    malist = document.Topselector.manufacturerList;
    sublist= document.Topselector.subCategoryList;
	catlist = document.Topselector.categoryList;
	
	//alert(malist);
	//alert(sublist);
	//alert(catlist);
	sublist.options[sublist.selectedIndex].value = Var1;
	sublist.options[sublist.selectedIndex].text = Var2;
	subcatvalue=sublist.options[sublist.selectedIndex].value;
	subcattext=sublist.options[sublist.selectedIndex].text;
	//subcatvalue=subcatvalue.substr(0,2);
	//alert(subcatvalue);
	//alert(subcattext);
	
	if (subcattext==filler)
	{
        init('---------Recherche par region--------', '---------Recherche par activite-------', '---------Recherche par departement----------', '------------ Remise a zero ---------------');
	 	resetSelectBox();
	}
    else
	{
		categoryList ();
		manufacturerList ();
	}
}
function subCategoryList_onchange()
{
	
    malist = document.Topselector.manufacturerList;
    sublist= document.Topselector.subCategoryList;
	catlist = document.Topselector.categoryList;
	
	//alert(malist);
	//alert(sublist);
	//alert(catlist);
	
	subcatvalue=sublist.options[sublist.selectedIndex].value;
	subcattext=sublist.options[sublist.selectedIndex].text;
	//subcatvalue=subcatvalue.substr(0,2);
	//alert(subcatvalue);
	//alert(subcattext);
	
	if (subcattext==filler)
	{
        init('---------Recherche par region--------', '---------Recherche par activite-------', '---------Recherche par departement----------', '------------ Remise a zero ---------------');
	 	resetSelectBox();
	}
    else
	{
		categoryList ();
		manufacturerList ();
	}
	//alert(subcatvalue.substr(1,1));
	catlist.options[1].selected = true;
	document.getElementById('carte_france').style.visibility = "hidden";
		document.getElementById('1').style.visibility = "hidden";
		document.getElementById('2').style.visibility = "hidden";
		document.getElementById('3').style.visibility = "hidden";
		document.getElementById('4').style.visibility = "hidden";
		document.getElementById('5').style.visibility = "hidden";
		document.getElementById('6').style.visibility = "hidden";
		document.getElementById('7').style.visibility = "hidden";
		document.getElementById('8').style.visibility = "hidden";
		document.getElementById(subcatvalue.substr(1,1)).style.visibility = "visible";
		document.getElementById('retour').style.visibility = "visible";
}

function init(cat, mf, subcat, reset)
{
	catvalue='';
	cattext=cat;
	manvalue='';
	mantext=mf;
	subcatvalue='';
	subcattext=subcat;
	filler=reset;
	categoryList ();
	subCategoryList ();
	manufacturerList ();
	initSelected();
}

/*
function gouk(numOfHits, alertSearchKeyword, alertSearchCriteria, compaycd)
{
	defineVars();
	keywd = strip1(prodsearch.value);
	var hits=numOfHits;
	var stock="";
	var promo="";
	var newitem="";

	if (document.SelectBox.coStock.checked)
	{
	    stock="-1";
	}

    if(document.SelectBox.coPromo.checked)
	{
		promo="1";
	}

    if (document.SelectBox.coDay.checked)
	{
		newitem="7";

	}

    if ((keywd.length == 1) && (catvalue==""))
    {
	    alert(alertSearchKeyword);
	}
    else if(((keywd=="") || (keywd=='\"\"'))
            && (catvalue=="")
            && (subcatvalue=="")
            && (manvalue.slice(1,5)=="")
            && (document.SelectBox.coStock.checked==false)
            && (document.SelectBox.coPromo.checked==false)
            && (document.SelectBox.coDay.checked==false))
	{
	    alert(alertSearchCriteria);
	}
    else
    {
	    var link;
		var manufacturer=manvalue.replace(":", "");
		link='/' + companycd + '/reseller/jsp/search/Results.jsp?keywords='+keywd+'&scope=1&cat='+catvalue+'&subcat='+subcatvalue+'&vendor='+manufacturer+'&oStock='+stock+'&oPromo='+promo+'&day='+newitem+'&hits='+hits;
		parent.main.location.href = link;
	}
}
*/
function helpWindow()
{
    hWin = window.open("/help/?section=reseller", "HelpWin", "resizable,menubar,scrollbars,top=300,left=300,width=600,height=400");
}

function strip1(inval)
{
    retval = "";
    var oneChar = "";
	var newString= "";
	var counter = 0;
	var whitespace = 0;

	if (inval.length != null)
	{
	    for (var i = 0; i < inval.length; i++)
		{
		    charOn = inval.charAt(i);
			if (charOn != " ")
			{
			    newString += charOn;
				whitespace = 1;
				if (counter>= 1)
				{
				    counter = 0;
				}
			}
            else if (charOn = " ")
			{
			    if (whitespace > 0)
				{
				    if (counter < 1)
					{
					    newString += charOn;
					    counter++;
				    }
				}
			}
		}
		retval = newString;
	}
    return retval;
}

function defineVars()
{
    //prodsearch  = window.document.advanceLayer.quicksearchProduct.productSearch;
	//prodsearch  = document.quicksearchProduct.productSearch;
}
/*
function quickSearchFormByProduct(numOfHits, alertSearchKeyword, alertSearchCriteria, compaycd)
{
    defineVars();
    var keywd;
    var cat = "";
    var scat = "";
    var vndor = "";
    var hits=numOfHits;

    if (document.quicksearchProduct.ct.value != "-1")
    {
	    cat = document.quicksearchProduct.ct.value;
    }

    if (document.quicksearchProduct.sct.value != "-1")
    {
        scat = cat + document.quicksearchProduct.sct.value;
    }

    if (document.quicksearchProduct.vn.value != "-1")
    {
        vndor = document.quicksearchProduct.vn.value;
    }

    first_part = "/"+companycd+"/reseller/jsp/search/Results.jsp?";
    keywd = strip1(prodsearch.value);

    if(((keywd=="") || (keywd=='\"\"')) && (cat=="") && (vndor==""))
    {
        alert(alertSearchCriteria);
    }
    else if ((keywd.length == 1) && (cat==""))
    {
        alert(alertSearchKeyword);
    }
    else
    {
        var winLoc;
        winLoc = first_part + "keywords=" + keywd + "&scope=1&hits=" + hits + "&cat=" + cat + "&subcat=" + scat + "&vendor=" + vndor;
        parent.main.location.href = winLoc;
    }

    return;
}
*/
function resetSelectBox()
{	
	document.Topselector.categoryList.selectedIndex=0;
	document.Topselector.subCategoryList.selectedIndex=0;
	document.Topselector.manufacturerList.selectedIndex=0;
	document.Topselector.find.value="";
	click_retour();
    //document.quicksearchProduct.productSearch.value = "";
	//document.SelectBox.coStock.checked = false;
	//document.SelectBox.coPromo.checked = false;
	//document.SelectBox.coDay.checked = false;
}

function resetAll(cat, mf, subcat, reset)
{	//alert('Reset');
    init(cat, mf, subcat, reset);
	resetSelectBox();
	document.Topselector.categoryList.selectedIndex=0;
	document.Topselector.subCategoryList.selectedIndex=0;
	document.Topselector.manufacturerList.selectedIndex=0;
	document.Topselector.find.value="";
}
/*
function goLicFrontDoor(serverName, companyCd)
{
    parent.main.location.href = "http://" + serverName + "/" + companycd + "/reseller/jsp/license/AVLFrontDoor.jspAVL";
}
*/