//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();
	}
}

function getTypeByGenre(){
	//alert(typeof(document.getElementById("types")))
	var typeList = document.getElementById("types");
	//Getting the selected country from country combo box.
	var selectedType = typeList.options[typeList.selectedIndex].value;	
	// URL to get states for a given country
	var requestUrl = "/client/_ajax/type.asp?code=" + encodeURIComponent(selectedType);	
	CreateXmlHttp();
	
	
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = loadtype;
		
		//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 getTypeByGenreAndMarque(){		
	var genreList = document.getElementById("genres");
	var marqueList = document.getElementById("marques");
	//Getting the selected country from country combo box.
	var selectedMarque = marqueList.options[marqueList.selectedIndex].value;	
	var selectedGenre = genreList.options[genreList.selectedIndex].value;
	// URL to get states for a given country
	var requestUrl = "/client/_ajax/type_by_genreandmarque.asp?section1="+encodeURIComponent(selectedMarque)+"&section2="+encodeURIComponent(selectedGenre);	
	CreateXmlHttp();
	
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = loadtypes;
		
		//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 getMarqueByType(){		
	var typeList = document.getElementById("type");
	var genreList = document.getElementById("types");
	//Getting the selected country from country combo box.
	var selectedType = typeList.options[typeList.selectedIndex].value;	
	var selectedGenre = genreList.options[genreList.selectedIndex].value;	
	// URL to get states for a given country
	var requestUrl = "/client/_ajax/marque.asp?section1="+encodeURIComponent(selectedGenre)+"&section2="+encodeURIComponent(selectedType);	
	CreateXmlHttp();
	
	
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = loadmarques;
		
		//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);
	}
	
	GetSizeByType(encodeURIComponent(selectedGenre) + "," + encodeURIComponent(selectedType));
}


function GetSizeByTypeAndGenre()
{
	var typeList = document.getElementById("types");
	var genreList = document.getElementById("genres");

	var selectedType = typeList.options[typeList.selectedIndex].value;	
	var selectedGenre = genreList.options[genreList.selectedIndex].value;	

	GetSizeByType(encodeURIComponent(selectedGenre) + "," + encodeURIComponent(selectedType));
}

function GetSizeByType(section)
{
	InitializeXmlHttpRequestObject();
	
	var method = "get";
	var url = "/client/_ajax/GetSizeByType.asp?sect=" + section + "&rand=" + RandomNumberForNoCache();
	var async = true;
	var callbackfunction = "loadsizes";
	
	SendXmlHttpRequest(method, url, async, callbackfunction);
}

function getAllMarques(){		
	// URL to get states for a given country
	var requestUrl = "/client/_ajax/allmarque.asp";	
	CreateXmlHttp();
	
	
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = loadmarques;
		
		//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 getGenreByMarque(){
	var marqueList = document.getElementById("marques");
	//Getting the selected country from country combo box.
	var selectedMarque = marqueList.options[marqueList.selectedIndex].value;
	// URL to get states for a given country
	var requestUrl = "/client/_ajax/genre_by_marque.asp?code=" + encodeURIComponent(selectedMarque);	
	CreateXmlHttp();
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = loadgenres;
		
		//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);
	}
	
}



//Called when response comes back from server
function HandleResponse()
{
	// 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)
		{			
			ClearAndSetStateListItems(XmlHttp.responseXML.documentElement);
		}
		else
		{
			alert("There was a problem retrieving data from the server." );
		}
	}
}

//Returns the node text value 
function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}


function ReinitializeSizes()
{
	if(document.getElementById("size"))
	{
		var size = document.getElementById("size");
	
		while(size.childNodes.length > 0)
		{
			size.removeChild(size.childNodes[0]);
		}
	
		var option = document.createElement("option");
		option.value = "";
		option.text = "All sizes";
		size.options[0] = option;
	}
}
