      
// Global XMLHTTP Request object
var XmlHttp=null;
//var SuspendRegions=true;
var CanSendRequest=true;
       
//Creating and setting the instance of appropriate XMLHTTP Request object to a “XmlHttp” variable  
function CreateXmlHttp()
{
        try
        {
            // Firefox, Opera 8.0+, Safari
            XmlHttp=new XMLHttpRequest();
        }
        catch(e)
        {
            //alert("Error Firefox");
            // Internet Explorer
            try
            {
                XmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
            }    
            catch (e)
            {
                XmlHttp=new ActiveXObject("Microsoft.XMLHTTP");      
            }
        }
    //alert(XmlHttp);
}

function DropDowns_Populate()
{
    //alert("DropDowns Populate");
    //SuspendRegions=false;
    //CreateXmlHttp();
    Regions_populate();
    //CategoriesRoot_populate();
}
////////////////////////   REGIONS  ////////////////////////////////

// Populate all regions into the regions dropdown
// Create the XmlHttp object and set the pointer to the function "PopulateRegions"
function Regions_populate() 
{
    //CanSendRequest=false;
    //alert("Regions_populate()");
	// URL to get states for a given region
	var requestUrl = AjaxServerPageName + "regions";
	CreateXmlHttp();
	
	// If browser supports XMLHTTPRequest object
	//alert(XmlHttp);
	if(XmlHttp)
	{
	    //alert("OK");
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = PopulateRegions;
		
		//Initializes the request object with GET (METHOD of posting), 
		//Request URL and sets the request as asynchronous.
		XmlHttp.open("GET", requestUrl + "&" + Date(),  true);
		//Sends the request to server
		XmlHttp.send(null);		
	}
	else
	{
	    //alert("Error");
	}
}

// Called when the server responded, check the response status and call the function that clear all results and repopulate the dropdown
function PopulateRegions() 
{
    //alert("PopulateRegions()");
	// To make sure receiving response data from server is completed
	if(XmlHttp.readyState == 4)
	{
	    //alert(XmlHttp.status);
	    // To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttp.status == 200)
		{			
			ClearAndSetRegionListItems(XmlHttp.responseXML.documentElement);
		}
		else
		{
			//alert("There was a problem retrieving data from the server." );
		}
	}
	else
	{
	   //alert(XmlHttp.readyState);
	}
}


//Clears the contents of state combo box and adds the states of currently selected region
function ClearAndSetRegionListItems(regionNode)
{
    //alert("ClearRegions");
    //SuspendRegions=true;
    var regionsList = document.getElementById("rgid");
	//Clears the region combo box contents.
	for (var count = regionsList.options.length-1; count > -1; count--)
	{
	    //alert(count);
		regionsList.options[count] = null;
	}
	//alert("OK");
	// Add the first option "All Cities"
    //regionsList.options.add(new Option('ëì äàéæåøéí', 0));
    
    //alert(regionsNode.innerText);
	var obj_RegionsNodes = regionNode.getElementsByTagName('Region');
	//alert(obj_RegionsNodes.length);
	var textValue1; 
	var idValue1;
	var optionItem1;
	//Add new states list to the state combo box.
	for (var count = 0; count < obj_RegionsNodes.length; count++)
	{
	    var text1 = GetInnerText(obj_RegionsNodes[count]).split('|');
   		textValue1 = text1[0];
   		idValue1 = text1[1];
   		//alert(textValue1 + " | " + idValue1);
		optionItem1 = new Option(textValue1, idValue1,  false, false);
		regionsList.options[regionsList.length] = optionItem1;
	}
	
	regionsList.options[0].selected = true;
	CategoriesRoot_populate();
	//SuspendRegions=false;
	//CanSendRequest=true;
}

//Gets called when regions combo box selection changes
function Regions_onchange() 
{
    var regionList = document.getElementById("rgid");

	//Getting the selected region from region combo box.
	var selectedRegion = regionList.options[regionList.selectedIndex].value;
	
	// URL to get states for a given region
	var requestUrl = AjaxServerPageName + "cities&rgid=" + encodeURIComponent(selectedRegion);
	
	CreateXmlHttp();
	
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = PopulateCities;
		
		//Initializes the request object with GET (METHOD of posting), 
		//Request URL and sets the request as asynchronous.
		XmlHttp.open("GET", requestUrl + "&" + Date(),  true);
		
		//Sends the request to server
		XmlHttp.send(null);		
	}
}

/////////////////////////////////////    CITIES   //////////////////////////////

//Called when response comes back from server
function PopulateCities()
{
	// To make sure receiving response data from server is completed
	if(XmlHttp.readyState == 4)
	{
	    //alert(XmlHttp.status);
	    // To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttp.status == 200)
		{			
			ClearAndSetCityListItems(XmlHttp.responseXML.documentElement);
		}
		else
		{
			//alert("There was a problem retrieving data from the server." );
		}
	}
}

//Clears the contents of state combo box and adds the states of currently selected region
function ClearAndSetCityListItems(regionNode)
{
    var cityList = document.getElementById("citid");
	//Clears the state combo box contents.
	for (var count = cityList.options.length-1; count >-1; count--)
	{
		cityList.options[count] = null;
	}
	
	// Add the first option "All Cities"
    //cityList.options.add(new Option('ëì äòøéí', 0));
    //alert(regionNode.toString());
	var citiesNodes = regionNode.getElementsByTagName('City');
	var textValue; 
	var idValue;
	var optionItem;
	//Add new states list to the state combo box.
	for (var count = 0; count < citiesNodes.length; count++)
	{
    	var text = GetInnerText(citiesNodes[count]).split('|');
   		textValue = text[0];
   		idValue = text[1];
		optionItem = new Option( textValue, idValue,  false, false);
		cityList.options[cityList.length] = optionItem;
	}
}

/////////////////////////////////////   CATEGORIES   ///////////////////////////

// Populate all categories into the categories dropdown
// Create the XmlHttp object and set the pointer to the function "PopulateCategories"
function CategoriesRoot_populate() 
{
	// URL to get states for a given region
	var requestUrl = AjaxServerPageName + "rootcategories";
	
	CreateXmlHttp();
	
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = PopulateRootCategories;
		
		//Initializes the request object with GET (METHOD of posting), 
		//Request URL and sets the request as asynchronous.
		XmlHttp.open("GET", requestUrl + "&" + Date(),  true);
		
		//Sends the request to server
		XmlHttp.send(null);		
	}
}

// Populate the Categories into the categories dropdown
//Called when response comes back from server
function PopulateRootCategories()
{
	// To make sure receiving response data from server is completed
	if(XmlHttp.readyState == 4)
	{
	    //alert(XmlHttp.status);
	    // To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttp.status == 200)
		{			
			ClearAndSetRootCategoryListItems(XmlHttp.responseXML.documentElement);
		}
		else
		{
			//alert("There was a problem retrieving data from the server." );
		}
	}
}

// Root categories
//Clears the contents of state combo box and adds the states of currently selected region
function ClearAndSetRootCategoryListItems(regionNode)
{
    var categoryList = document.getElementById("cid");
	//Clears the categories combo box contents.
	for (var count = categoryList.options.length-1; count >-1; count--)
	{
		categoryList.options[count] = null;
	}
	
	var categoriesNodes = regionNode.getElementsByTagName('Category');
	var textValue; 
	var idValue;
	var optionItem;
	//Add new states list to the state combo box.
	for (var count = 0; count < categoriesNodes.length; count++)
	{
    	var text = GetInnerText(categoriesNodes[count]).split('|');
   		textValue = text[0];
   		idValue = text[1];
		optionItem = new Option( textValue, idValue,  false, false);
		categoryList.options[categoryList.length] = optionItem;
	}
}

//Gets called when RootCategory combo box selection changes
function CategoryRoot_onchange() 
{
	var categoriesList = document.getElementById("cid");

	//Getting the selected category from category combo box.
	var selectedCategory = categoriesList.options[categoriesList.selectedIndex].value;
	
	// URL to get categories for a given root category
	var requestUrl = AjaxServerPageName + "categories&cid=" + encodeURIComponent(selectedCategory);
	
	CreateXmlHttp();
	
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = PopulateSubCategories;
		
		//Initializes the request object with GET (METHOD of posting), 
		//Request URL and sets the request as asynchronous.
		XmlHttp.open("GET", requestUrl + "&" + Date(),  true);
		
		//Sends the request to server
		XmlHttp.send(null);		
	}
}

// Populate the Categories into the categories dropdown
//Called when response comes back from server
function PopulateSubCategories()
{
	// To make sure receiving response data from server is completed
	if(XmlHttp.readyState == 4)
	{
	    //alert(XmlHttp.status);
	    // To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttp.status == 200)
		{			
			ClearAndSetSubCategoryListItems(XmlHttp.responseXML.documentElement);
		}
		else
		{
			//alert("There was a problem retrieving data from the server." );
		}
	}
}

// Root categories
//Clears the contents of state combo box and adds the states of currently selected region
function ClearAndSetSubCategoryListItems(categoryNode)
{   
    //alert(categoryNode.text);
    var categoryList = document.getElementById("scid");
	//Clears the categories combo box contents.
	for (var count = categoryList.options.length-1; count >-1; count--)
	{
		categoryList.options[count] = null;
	}
	var categoriesNodes = categoryNode.getElementsByTagName('Category');
	//alert(categoriesNodes.length);
	var textValue; 
	var idValue;
	var optionGroup;
	//Add new states list to the state combo box.
	for (var count = 0; count < categoriesNodes.length; count++)
	{
	    // Create the category level 2 option item
	    var catNode = categoriesNodes[count];
	    //alert(GetInnerText(catNode));
	    var text = GetInnerText(catNode).split('|');
    	//alert(text);
   		textValue = text[0];
   		idValue = text[1];
   		//optionGroup = document.createElement('option');
   		optionGroup = new Option(textValue, idValue, false, false);
   		categoryList.options[categoryList.length] = optionGroup;
   		//optionGroup.backgroundColor='#dcdcdc';
   		//optionGroup.value=idValue;
   		//optionGroup.label=textValue;
   		//optionGroup.text=textValue;
   		//categoryList.appendChild(optionGroup);
		// Create the category level 3 option item
	    var subNodes = catNode.getElementsByTagName('SubCategory');
	    if(subNodes!=null)
	    {
	        //alert("SubNodes:" + subNodes.length);
	        var textVal2;
	        var idVal2;
	        var optionItem;
	        for( var count2=0;count2<subNodes.length;count2++)
	            {
	                var subNode=subNodes[count2];
	                var text2 = GetInnerText(subNode).split('|');
	                textVal2 = text2[0];
	                idVal2 = text2[1];
	                //alert(textVal2 + " " + idVal2)
	                optionItem = new Option(textVal2, idVal2,  false, false);
	                categoryList.options[categoryList.length] = optionItem;
	                //optionItem = document.createElement('option');
                    //optionItem.value=idVal2;
                    //optionItem.text=textVal2;
                    //optionItem.innerHtml=textVal2;
                    //optionItem.label=textVal2;
                    //categoryList.appendChild(optionItem);
	            }
	    }
	}
}

function GetTextContent(node)
{
    //alert("Content=" + node.textContent);
    return node.textConent;
}

function GetText(node)
{
    //alert("Text=" + node.text);
    return text;
}



//Returns the node text value 
function GetInnerText (node)
{
     return (node.textContent || node.innerText || node.text) ;
}









/*
//Creating and setting the instance of appropriate XMLHTTP Request object to a “XmlHttp” variable  
function CreateXmlHttp2()
{
	//Creating object of XMLHTTP in IE
	try
	{
		XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
		} 
		catch(oc)
		{
			XmlHttp = null;
		}
	}
	//Creating object of XMLHTTP in Mozilla and Safari 
	if(!XmlHttp && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttp = new XMLHttpRequest();
	}
}
*/
