function getXhr() {
    var xhr = null; 
	if(window.XMLHttpRequest) // Firefox et autres
	   xhr = new XMLHttpRequest(); 
	else if(window.ActiveXObject){ // Internet Explorer 
	   try {
            xhr = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }
	} else { // XMLHttpRequest non supporte par le navigateur 
	   alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
	   xhr = false; 
	} 
    return xhr;
}

/**
* Methode qui sera appelee sur le click du bouton
*/

function information_element (id, name, func, label_fr, label_en) {
	this.id = id;
	this.name = name;
	this.func = func;
	this.label_fr = label_fr;
	this.label_en = label_en;
}

Infos = new Array(3);
Infos['type'] = new information_element ('td_type', 'type', 'goType', 'Choisissez le type du produit', 'Select product type');
Infos['product'] = new information_element ('td_product', 'product', 'goProduct', 'Choisissez le mod&egrave;le du produit', 'Select product model');
Infos['document'] = new information_element ('td_document', 'document', 'goDocument', 'Choisissez le type du document', 'Select document type');


function resetSelect(name, lang) {
	elem = Infos[name];
	if (lang == 'fr') {
		html = "<select name='"+elem.name+"' id='"+elem.name+"' onchange='"+elem.func+"()'><option value='-1'>"+elem.label_fr+"</option></select>";
	} else {
		html = "<select name='"+elem.name+"' id='"+elem.name+"' onchange='"+elem.func+"()'><option value='-1'>"+elem.label_en+"</option></select>";
	}
	document.getElementById(elem.id).innerHTML = html;
}

function resetContent() {
	document.getElementById('ajax_content').innerHTML = "";
}

function getTypes(lang) {
	var xhr = getXhr();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
			document.getElementById('td_type').innerHTML = xhr.responseText;
		}
	}
	xhr.open("POST","ajax.php",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("l="+lang+"&act=getType");
}

function goType(lang){
	resetSelect('product', lang);
	resetSelect('document', lang);
	resetContent();
	
	sel = document.getElementById('type');
	idType = sel.options[sel.selectedIndex].value;
	if (idType > 0) {
		var xhr = getXhr();
		xhr.onreadystatechange = function(){
			if(xhr.readyState == 4 && xhr.status == 200){
				document.getElementById('td_product').innerHTML = xhr.responseText;
				/* document.getElementById('product').select(); */
			}
		}
		xhr.open("POST","ajax.php",true);
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhr.send("l="+lang+"&idType="+idType);
	}
}

function goProduct(lang){
	resetSelect('document', lang);
	resetContent();
	
	sel = document.getElementById('product');
	idProduct = sel.options[sel.selectedIndex].value;
	if (idProduct > 0) {
		var xhr = getXhr();
		xhr.onreadystatechange = function(){
			if(xhr.readyState == 4 && xhr.status == 200){
				document.getElementById('td_document').innerHTML = xhr.responseText;
			}
		}
		xhr.open("POST","ajax.php",true);
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		sel = document.getElementById('type');
		idType = sel.options[sel.selectedIndex].value;
		xhr.send("l="+lang+"&idType="+idType+"&idProduct="+idProduct);
	}
}

function goDocument(lang){
	resetContent();
	
	sel = document.getElementById('document');
	idDoc = sel.options[sel.selectedIndex].value;
	if (idDoc > 0) {
		var xhr = getXhr();
		xhr.onreadystatechange = function(){
			if(xhr.readyState == 4 && xhr.status == 200){
				document.getElementById('ajax_content').innerHTML = xhr.responseText;
			}
		}
		xhr.open("POST","ajax.php",true);
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		sel = document.getElementById('type');
		idType = sel.options[sel.selectedIndex].value;
		sel = document.getElementById('product');
		idProduct = sel.options[sel.selectedIndex].value;
		xhr.send("l="+lang+"&idType="+idType+"&idProduct="+idProduct+"&idDoc="+idDoc);	
	}
}


