function ShowArea(divId) 
{
	document.getElementById(divId).style.visibility = "visible";
}

function HideArea(divId) 
{
	document.getElementById(divId).style.visibility = "hidden";
}	
function SetMainImage(targetId, mainImage)
{
    document.getElementById(targetId).src = mainImage;
}
function ShowProductsResults() 
{
    if(document.all) 
    {
        document.all['ProductsResult'].style.display = 'block';
        document.all['AccessoryResult'].style.display = 'none';
        document.all['SiteSearchResult'].style.display = 'none';
        // document.all['DocumentSearchResult'].style.display = 'block';
    } 
    else 
    {
        document.getElementById('ProductsResult').style.display = 'block';
        document.getElementById('AccessoryResult').style.display = 'none';
        document.getElementById('SiteSearchResult').style.display = 'none';
        // document.getElementById('DocumentSearchResult').style.display = 'none';
    }
}

function ShowAccessoryResults() 
{
    if(document.all) 
    {
        document.all['ProductsResult'].style.display = 'none';
        document.all['AccessoryResult'].style.display = 'block';
        document.all['SiteSearchResult'].style.display = 'none';
        // document.all['DocumentSearchResult'].style.display = 'none';
    } 
    else 
    {
        document.getElementById('ProductsResult').style.display = 'none';
        document.getElementById('AccessoryResult').style.display = 'block';
        document.getElementById('SiteSearchResult').style.display = 'none';
        // document.getElementById('DocumentSearchResult').style.display = 'none';
    } 
}

function ShowSiteSearchResults() 
{
    if(document.all) 
    {
        document.all['ProductsResult'].style.display = 'none';
        document.all['AccessoryResult'].style.display = 'none';
        document.all['SiteSearchResult'].style.display = 'block';
        // document.all['DocumentSearchResult'].style.display = 'none';
    } 
    else 
    {
        document.getElementById('ProductsResult').style.display = 'none';
        document.getElementById('AccessoryResult').style.display = 'none';
        document.getElementById('SiteSearchResult').style.display = 'block';
        // document.getElementById('DocumentSearchResult').style.display = 'none';
    } 
}
function ShowDocumentSearchResults() 
{
    if(document.all) 
    {
        document.all['ProductsResult'].style.display = 'none';
        document.all['AccessoryResult'].style.display = 'none';
        document.all['SiteSearchResult'].style.display = 'none';
        // document.all['DocumentSearchResult'].style.display = 'block';
    } 
    else 
    {
        document.getElementById('ProductsResult').style.display = 'none';
        document.getElementById('AccessoryResult').style.display = 'none';
        document.getElementById('SiteSearchResult').style.display = 'none';
        // document.getElementById('DocumentSearchResult').style.display = 'block';
    } 
}

////////////////////////////////////////////////////////////
// AJAX code
////////////////////////////////////////////////////////////


//Global XMLHTTP Request object
var XmlHttp;

//Creating and setting the instance of appropriate XMLHTTP Request object to a “XmlHttp” variable  
function CreateXmlHttp()
{
	//Creating object of XMLHTTP in IE
	try
	{
		XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttp = null;
		}
	}
	//Creating object of XMLHTTP in Mozilla and Safari 
	if(!XmlHttp && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttp = new XMLHttpRequest();
	}
}


/////////////////////////////////////////
// OnProductSearcherChange
/////////////////////////////////////////
function ProductSearcherListOnChange(woodControlId, qualityControlId, designControlId, collectionControlId, languageCode, type)
{
    // Get controls
    var woodList = document.getElementById(woodControlId);
    var qualityList = document.getElementById(qualityControlId);
    var designList = document.getElementById(designControlId);
    var collectionList = document.getElementById(collectionControlId);
    
    // Disable control while fetching data
    woodList.disabled = true;
    qualityList.disabled = true;
    designList.disabled = true;
    collectionList.disabled = true;
    
	//Getting the selected values
	var selectedWood = woodList.options[woodList.selectedIndex].value;
	var selectedQuality = qualityList.options[qualityList.selectedIndex].value;
	var selectedDesign = designList.options[designList.selectedIndex].value;
	var selectedCollection = collectionList.options[collectionList.selectedIndex].value;
	
	
	// URL to get states for a given country
	var requestUrl = "/Ajax/ProductSearchServer.aspx" + "?mode=updateCombos&LanguageCode="+languageCode+"&SelectedWood=" + encodeURIComponent(selectedWood)+"&SelectedQuality=" + encodeURIComponent(selectedQuality)+"&SelectedDesign=" + encodeURIComponent(selectedDesign)+"&SelectedCollection=" + encodeURIComponent(selectedCollection)+"&Type=" + encodeURIComponent(type);
	CreateXmlHttp();
	
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = HandleResponseProductSearcherCombos;
		
		//Initializes the request object with GET (METHOD of posting), 
		//Request URL and sets the request as asynchronous.
		XmlHttp.open("GET", requestUrl,  true);
		
		//Sends the request to server
		XmlHttp.send(null);		
	}
}
function HandleResponseProductSearcherCombos()
{
   // To make sure receiving response data from server is completed
	if(XmlHttp.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttp.status == 200)
		{		
		    ClearAndSetProductSearcherCombosItems(XmlHttp.responseText);
		}
		else
		{
			alert("There was a problem retrieving data from the server." );
		}
	}
}
function ClearAndSetProductSearcherCombosItems(reponseText)
{
    // Get controls
    var woodList = document.getElementById("ctl00_mainContent_ProductSearcher_ddlWoodDecor");
    var qualityList = document.getElementById("ctl00_mainContent_ProductSearcher_ddlQuality");
    var designList = document.getElementById("ctl00_mainContent_ProductSearcher_ddlDesign");
    var collectionList = document.getElementById("ctl00_mainContent_ProductSearcher_ddlCollection");
    //Getting the selected values
	var selectedWood = woodList.options[woodList.selectedIndex].value;
	var selectedQuality = qualityList.options[qualityList.selectedIndex].value;
	var selectedDesign = designList.options[designList.selectedIndex].value;
	var selectedCollection = collectionList.options[collectionList.selectedIndex].value;
    
    //Clears the combo box contents.
	for (var count = woodList.options.length-1; count >-1; count--)
	{
		woodList.options[count] = null;
	}
	//Clears the combo box contents.
	for (var count = qualityList.options.length-1; count >-1; count--)
	{
		qualityList.options[count] = null;
	}
	//Clears the combo box contents.
	for (var count = designList.options.length-1; count >-1; count--)
	{
		designList.options[count] = null;
	}
	//Clears the combo box contents.
	for (var count = collectionList.options.length-1; count >-1; count--)
	{
		collectionList.options[count] = null;
	}
	
	// alert("Wood: " + woodList.length + "\nQuality: " +qualityList.length + "\nDesign: " +designList.length + "\nCollection: " +collectionList.length);
	
	// Add new values to combo boxes
	var comboLists = reponseText.split("|---|");
	//alert("Number of boxes: " + comboLists.length);
	
	// Wood list
	var optionItem, woodItems, woodItem;
	woodItems = comboLists[0].split("|--|");
	optionItem = new Option( "", "",  false, false);
	woodList.options[woodList.length] = optionItem;
	for (var count = 0; count < woodItems.length-1; count++)
	{
   		woodItem = woodItems[count].split("|-|");
   		if(woodItem[0] == selectedWood)
   		{
   		    optionItem = new Option( woodItem[1], woodItem[0],  false, true);
   		}
   		else
   		{
   		    optionItem = new Option( woodItem[1], woodItem[0],  false, false);
   		}
		woodList.options[woodList.length] = optionItem;
	}
	// Quality list
	var qualityItems, qualityItem;
	qualityItems = comboLists[1].split("|--|");
	optionItem = new Option( "", "",  false, false);
	qualityList.options[qualityList.length] = optionItem;
	for (var count = 0; count < qualityItems.length-1; count++)
	{
   		qualityItem = qualityItems[count].split("|-|");
   		if(qualityItem[0] == selectedQuality)
   		{
   		    optionItem = new Option( qualityItem[1], qualityItem[0],  false, true);
   		}
   		else
   		{
   		    optionItem = new Option( qualityItem[1], qualityItem[0],  false, false);
   		}
		qualityList.options[qualityList.length] = optionItem;
	}
	// Design list
	var designItems, designItem;
	designItems = comboLists[2].split("|--|");
	optionItem = new Option( "", "",  false, false);
	designList.options[designList.length] = optionItem;
	for (var count = 0; count < designItems.length-1; count++)
	{
   		designItem = designItems[count].split("|-|");
   		if(designItem[0] == selectedDesign)
   		{
   		    optionItem = new Option( designItem[1], designItem[0],  false, true);
   		}
   		else
   		{
   		    optionItem = new Option( designItem[1], designItem[0],  false, false);
   		}
		designList.options[designList.length] = optionItem;
	}
	// Collection list
	var collectionItems, collectionItem;
	collectionItems = comboLists[3].split("|--|");
	optionItem = new Option( "", "",  false, false);
	collectionList.options[collectionList.length] = optionItem;
	for (var count = 0; count < collectionItems.length-1; count++)
	{
   		collectionItem = collectionItems[count].split("|-|");
		if(collectionItem[0] == selectedCollection)
   		{
   		    optionItem = new Option( collectionItem[1], collectionItem[0],  false, true);
   		}
   		else
   		{
   		    optionItem = new Option( collectionItem[1], collectionItem[0],  false, false);
   		}
		collectionList.options[collectionList.length] = optionItem;
	}
	// Enable control when data update is finnished
    woodList.disabled = false;
    qualityList.disabled = false;
    designList.disabled = false;
    collectionList.disabled = false;
}
/////////////////////////////////////////
// ProductSearcherReset
/////////////////////////////////////////
function ProductSearcherListReset(woodControlId, qualityControlId, designControlId, collectionControlId, languageCode, type)
{
    
    // URL to get states for a given country
	var requestUrl = "/Ajax/ProductSearchServer.aspx" + "?mode=resetCombos&LanguageCode="+languageCode+"&Type=" + encodeURIComponent(type);
	CreateXmlHttp();
	
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = HandleResponseProductSearcherResetCombos;
		
		//Initializes the request object with GET (METHOD of posting), 
		//Request URL and sets the request as asynchronous.
		XmlHttp.open("GET", requestUrl,  true);
		
		//Sends the request to server
		XmlHttp.send(null);		
	}
	return false;
}

function HandleResponseProductSearcherResetCombos()
{
   // To make sure receiving response data from server is completed
	if(XmlHttp.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttp.status == 200)
		{		
		    // Call previous declared function
		    ClearAndSetProductSearcherCombosItems(XmlHttp.responseText);
		    // Reset default values
		    // Get controls
            var woodList = document.getElementById("ctl00_mainContent_ProductSearcher_ddlWoodDecor");
            var qualityList = document.getElementById("ctl00_mainContent_ProductSearcher_ddlQuality");
            var designList = document.getElementById("ctl00_mainContent_ProductSearcher_ddlDesign");
            var collectionList = document.getElementById("ctl00_mainContent_ProductSearcher_ddlCollection");
            woodList.options[0].selected = true;
            qualityList.options[0].selected = true;
            designList.options[0].selected = true;
            collectionList.options[0].selected = true;
		}
		else
		{
			alert("There was a problem retrieving data from the server." );
		}
	}
}
