/* The following function creates an XMLHttpRequest object... */

function createRequestObject(){
	var request_o; //declare the variable to hold the object.
	var browser = navigator.appName; //find the browser name
	if(browser == "Microsoft Internet Explorer"){
		/* Create the object using MSIE's method */
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		/* Create the object using other browser's method */
		request_o = new XMLHttpRequest();
	}
	return request_o; //return the object
}

/* You can get more specific with version information by using 
	parseInt(navigator.appVersion)
	Which will extract an integer value containing the version 
	of the browser being used.
*/
/* The variable http will hold our new XMLHttpRequest object. */
var http = createRequestObject(); 

/* Function called to get the product categories list */
function getRegions(id){
	/* Create the request. The first argument to the open function is the method (POST/GET),
		and the second argument is the url... 
		document contains references to all items on the page
		We can reference document.form_category_select.select_category_select and we will 		
		be referencing the dropdown list. The selectedIndex property will give us the 
		index of the selected item. 
	*/
	http.open('get', 'internal_request.php?action=get_regions&id=' 
	//		+ document.getElementById('pays').selectedIndex);
			+ id);
	/* Define a function to call once a response has been received. This will be our
		handleProductCategories function that we define below. */
	http.onreadystatechange = handleRegions; 
	/* Send the data. We use something other than null when we are sending using the POST
		method. */
	http.send(null);
}
function getAssocCategory(id){
		http.open('get', 'internal_request.php?action=getCategoryAssoc&id=' + id);
		http.onreadystatechange = handleAssocCategory;
		http.send(null);
}
function getAssocCategory2(id){
		http.open('get', 'internal_request.php?action=getCategoryAssoc2&id=' + id);
		http.onreadystatechange = handleAssocCategory2;
		http.send(null);
}
function getThemeProf(){
		http.open('get', 'internal_request.php?action=getThemeProf');
		http.onreadystatechange = handleThemeProf;
		http.send(null);
}
function getCatProf(id){	
		http.open('get', 'internal_request.php?action=getCatProf&id=' + id);
		http.onreadystatechange = handleCatProf;
		http.send(null);
}
function getCatProfAdmin(id){
		http.open('get', 'internal_request.php?action=getThemeProfAdmin');
		http.onreadystatechange = handleCatProf;
		http.send(null);
}
function getSousCatProf(id){
		http.open('get', 'internal_request.php?action=getSousCatProf&id=' + id);
		http.onreadystatechange = handleSousCatProf;
		http.send(null);
}
function getCatProf2(id){	
		http.open('get', 'internal_request.php?action=getCatProf2&id=' + id);
		http.onreadystatechange = handleCatProf2;
		http.send(null);
}
function getSousCatProf2(id){
		http.open('get', 'internal_request.php?action=getSousCatProf2&id=' + id);
		http.onreadystatechange = handleSousCatProf2;
		http.send(null);
}
function handleAssocCategory(){
		if(http.readyState== 4 && http.status == 200){
			var response =http.responseText;
			document.getElementById('categoryAssoc1').innerHTML = response;
		}
}
function handleAssocCategory2(){
		if(http.readyState== 4 && http.status == 200){
			var response =http.responseText;
			document.getElementById('categoryAssoc2').innerHTML = response;
		}
}
function handleThemeProf(){
		if(http.readyState== 4 && http.status == 200){
			var response =http.responseText;
			document.getElementById('theme').innerHTML = response;
			document.getElementById('category').innerHTML = '';
			document.getElementById('2selectbutton').style.display = 'none';
		}
}
function handleCatProf(id){
		if(http.readyState== 4 && http.status == 200){	
			var response = http.responseText;
			document.getElementById('category').innerHTML = response;
			document.getElementById('souscategory').innerHTML = '';
			document.getElementById('2selectbutton').style.display = 'none';
		}
}
function handleSousCatProf(id){
		if(http.readyState== 4 && http.status == 200) {
			var response = http.responseText;
			document.getElementById('souscategory').innerHTML = response;
			document.getElementById('2selectbutton').style.display = 'none';			
		}
}
function handleCatProf2(id){
		if(http.readyState== 4 && http.status == 200){	
			var response = http.responseText;
			document.getElementById('category2').innerHTML = response;
		}
}
function handleSousCatProf2(id){
		if(http.readyState== 4 && http.status == 200) {
			var response = http.responseText;
			document.getElementById('souscategory2').innerHTML = response;
		}
}	

/* Function called to handle the list that was returned from the internal_request.php file.. */
function handleRegions(){
	/* Make sure that the transaction has finished. The XMLHttpRequest object 
		has a property called readyState with several states:
		0: Uninitialized
		1: Loading
		2: Loaded
		3: Interactive
		4: Finished */
	if(http.readyState== 4 && http.status == 200){ //Finished loading the response
		/* We have got the response from the server-side script,
			let's see just what it was. using the responseText property of 
			the XMLHttpRequest object. */
		var response = http.responseText;
		/* And now we want to change the product_categories <div> content.
			we do this using an ability to get/change the content of a page element 
			that we can find: innerHTML. */
		document.getElementById('regions').innerHTML = response;
		
	}
}
function generate2SelectsAssoc()
{
	var mother = document.getElementById('themes_select');
	var child = document.getElementById('themes_select2');
	for(var i=0; i<child.options.length; i++)
	{
		if(child.options.item(i).value == 	mother.options[mother.options.selectedIndex].value)
		{
			child.options.item(i).selected = true;
		}
	}
	getAssocCategory2(mother.options[mother.options.selectedIndex].value);
	document.getElementById('2selectdiv').style.display ='none';
	if(navigator.appName != "Microsoft Internet Explorer") 
	{
		document.getElementById('2select').style.display='table-row-group';
	}
	else
	{
		document.getElementById('2select').style.display='';
	}
	document.getElementById('themes_select2').disabled = false;
	
}
function madeForIe()
{
	if(navigator.appName != "Microsoft Internet Explorer") 
	{
		document.getElementById('2selectdiv').style.display='table-row-group';
	}
	else 
	{
		document.getElementById('2selectdiv').style.display='';
	}
	document.getElementById('2select').style.display='none';	
}

function generate2Selects(){
document.getElementById('2selectbutton').style.display ='none';
return document.getElementById('2select').style.display='';

}

function generate2Selects1()
{
	var mother = document.getElementById('select_theme');
	var child = document.getElementById('select_theme2');
	for(var i=0; i<child.options.length; i++)
	{
		if(child.options.item(i).value == 	mother.options[mother.options.selectedIndex].value)
		{
			child.options.item(i).selected = true;
		}
	}
	getCatProf2(mother.options[mother.options.selectedIndex].value);
	document.getElementById('2selectbutton').style.display ='none';
	document.getElementById('2select').style.display='';
	
}

function checkuseravailable()
{	
	var name = document.getElementById("user_login").value;
	http.open('get', 'internal_request.php?action=checkuseravailable&name=' + name);
		http.onreadystatechange = handleuseravailable;
		http.send(null);
}
function handleuseravailable()
{
	if(http.readyState == 4 && http.status == 200) 
	{
		var response = http.responseText;
		if(response == 'exists')
		{
			document.getElementById('user_login').focus();
			document.getElementById('user_login').select();
			alert("L'utilisateur existe déjà");
		}
		else 
		{
			document.getElementById('error').style.display = 'none';
		}
	}
}
	
	
