//Our XmlHttpRequest object to get the auto suggest
var searchReq = getXmlHttpRequestObject();

//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
}

var totalCount = 0;

// Magasin specific treatment
function refreshMagasin() {
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		var str_pays = escape(document.getElementById('pays').options[document.getElementById('pays').selectedIndex].value);		
		if(str_pays == '') {
			document.getElementById('magasindiv').style.visibility = 'hidden';
			document.getElementById('magasindiv').style.display = 'none';
		} else {
			str_search = 'pays=' + str_pays;
			searchReq.open("GET", '/brisach/fr/fr-fr/ajax/getRelatedMagasin.cfm?' + str_search, true);
			searchReq.onreadystatechange = handleSearchMagasin; 
			searchReq.send(null);			
		}
	}
}

function refreshMagasin_install() {
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		var str_pays = escape(document.getElementById('pays_inst').options[document.getElementById('pays_inst').selectedIndex].value);		
		if(str_pays == '') {
			document.getElementById('magasindiv_inst').style.visibility = 'hidden';
			document.getElementById('magasindiv_inst').style.display = 'none';
		} else {
			str_search = 'pays=' + str_pays;
			searchReq.open("GET", '/brisach/fr/fr-fr/ajax/getRelatedMagasin.cfm?' + str_search, true);
			searchReq.onreadystatechange = handleSearchMagasin_inst; 
			searchReq.send(null);			
		}
	}
}

//Called when the AJAX response is returned.
function handleSearchMagasin() {

	if (searchReq.readyState == 4) {
		var str_pays = escape(document.getElementById('pays').options[document.getElementById('pays').selectedIndex].value);
		var str_etat = searchReq.responseText.substring(0,2);
		// ok : il existe une relation magasin - code postal pour ce pays
		// ld : (link dead) il existe des magasins pour ce pays, mais pas de relation par code postal
		// no : il n'existe pas de magasin pour ce pays

		// reset magasin select box
		document.getElementById('magasin').options.length = 0;

		if (str_etat == 'ok' || str_etat == 'no') {
			// pas besoin de mettre à jour la liste des magasins
			document.getElementById('magasindiv').style.visibility = 'hidden';
			document.getElementById('magasindiv').style.display = 'none';			
		} else {		
			var str = searchReq.responseText.split("\n");
			// skip first line
			
			// add select values
			// str[1] -> magasin list
			var str_MagasinSelect = str[1].split(";");
			for(i=0; i < str_MagasinSelect.length - 1; i++) {
				var str_tmp = str_MagasinSelect[i].split(":");
				if (str_tmp[0] != '')
					document.getElementById('magasin').options[i] = new Option(str_tmp[0],str_tmp[1]);								
			}
			document.getElementById('magasindiv').style.visibility = 'visible';
			document.getElementById('magasindiv').style.display = '';
			document.getElementById('hrdiv').style.visibility = 'visible';
			document.getElementById('hrdiv').style.display = '';			
		}
	}
}

function handleSearchMagasin_inst() {

	if (searchReq.readyState == 4) {
		var str_pays = escape(document.getElementById('pays_inst').options[document.getElementById('pays_inst').selectedIndex].value);
		var str_etat = searchReq.responseText.substring(0,2);
		// ok : il existe une relation magasin - code postal pour ce pays
		// ld : (link dead) il existe des magasins pour ce pays, mais pas de relation par code postal
		// no : il n'existe pas de magasin pour ce pays

		// reset magasin select box
		document.getElementById('magasin_inst').options.length = 0;

		if (str_etat == 'ok' || str_etat == 'no') {
			// pas besoin de mettre à jour la liste des magasins
			document.getElementById('magasindiv_inst').style.visibility = 'hidden';
			document.getElementById('magasindiv_inst').style.display = 'none';			
		} else {		
			var str = searchReq.responseText.split("\n");
			// skip first line
			
			// add select values
			// str[1] -> magasin list
			var str_MagasinSelect = str[1].split(";");
			for(i=0; i < str_MagasinSelect.length - 1; i++) {
				var str_tmp = str_MagasinSelect[i].split(":");
				if (str_tmp[0] != '')
					document.getElementById('magasin_inst').options[i] = new Option(str_tmp[0],str_tmp[1]);								
			}
			document.getElementById('magasindiv_inst').style.visibility = 'visible';
			document.getElementById('magasindiv_inst').style.display = '';	
			document.getElementById('hrdiv_inst').style.visibility = 'visible';
			document.getElementById('hrdiv_inst').style.display = '';			
		}
	}
}

// Magasin specific treatment
function refreshMagasinDoc() {
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		var str_pays = escape(document.getElementById('pays').options[document.getElementById('pays').selectedIndex].value);		
		if(str_pays == '') {
			document.getElementById('magasindiv').style.visibility = 'hidden';
			document.getElementById('magasindiv').style.display = 'none';
		} else {
			str_search = 'pays=' + str_pays;
			searchReq.open("GET", '/brisach/fr/fr-fr/ajax/getRelatedMagasin.cfm?' + str_search, true);
			searchReq.onreadystatechange = handleSearchMagasinDoc; 
			searchReq.send(null);			
		}
	}
}

function refreshMagasinDoc_install() {
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		var str_pays = escape(document.getElementById('pays_inst').options[document.getElementById('pays_inst').selectedIndex].value);		
		if(str_pays == '') {
			document.getElementById('magasindiv_inst').style.visibility = 'hidden';
			document.getElementById('magasindiv_inst').style.display = 'none';
		} else {
			str_search = 'pays=' + str_pays;
			searchReq.open("GET", '/brisach/fr/fr-fr/ajax/getRelatedMagasin.cfm?' + str_search, true);
			searchReq.onreadystatechange = handleSearchMagasinDoc_inst; 
			searchReq.send(null);			
		}
	}
}

//Called when the AJAX response is returned.
function handleSearchMagasinDoc() {

	if (searchReq.readyState == 4) {
		var str_pays = escape(document.getElementById('pays').options[document.getElementById('pays').selectedIndex].value);
		var str_etat = searchReq.responseText.substring(0,2);
		// ok : il existe une relation magasin - code postal pour ce pays
		// ld : (link dead) il existe des magasins pour ce pays, mais pas de relation par code postal
		// no : il n'existe pas de magasin pour ce pays

		// reset magasin select box
		document.getElementById('magasin').options.length = 0;

		if (str_etat == 'ok' || str_etat == 'no') {
			// pas besoin de mettre à jour la liste des magasins
			document.getElementById('magasindiv').style.visibility = 'hidden';
			document.getElementById('magasindiv').style.display = 'none';
			if (str_etat == 'no') {
				// il faut afficher la list restreinte des pays pour permettre ensuite la selection du magasin
				document.getElementById('paysdiv').style.visibility = 'visible';
				document.getElementById('paysdiv').style.display = '';
				document.getElementById('hrdiv').style.visibility = 'visible';
				document.getElementById('hrdiv').style.display = '';				
				document.getElementById('pays2').selectedIndex = 0;
			} else {
				document.getElementById('paysdiv').style.visibility = 'hidden';
				document.getElementById('paysdiv').style.display = 'none';
				document.getElementById('hrdiv').style.visibility = 'hidden';
				document.getElementById('hrdiv').style.display = 'none';				
				document.getElementById('pays2').selectedIndex = 0;				
			}
		} else {		
			var str = searchReq.responseText.split("\n");
			// skip first line
			
			// add select values
			// str[1] -> magasin list
			var str_MagasinSelect = str[1].split(";");
			for(i=0; i < str_MagasinSelect.length - 1; i++) {
				var str_tmp = str_MagasinSelect[i].split(":");
				if (str_tmp[0] != '')
					document.getElementById('magasin').options[i] = new Option(str_tmp[0],str_tmp[1]);								
			}
			document.getElementById('magasindiv').style.visibility = 'visible';
			document.getElementById('magasindiv').style.display = '';
			document.getElementById('paysdiv').style.visibility = 'hidden';
			document.getElementById('paysdiv').style.display = 'none';
			document.getElementById('hrdiv').style.visibility = 'visible';
			document.getElementById('hrdiv').style.display = '';			
			document.getElementById('pays2').selectedIndex = 0;
		}
	}
}
function handleSearchMagasinDoc_inst() {

	if (searchReq.readyState == 4) {
		var str_pays = escape(document.getElementById('pays_inst').options[document.getElementById('pays_inst').selectedIndex].value);
		var str_etat = searchReq.responseText.substring(0,2);
		// ok : il existe une relation magasin - code postal pour ce pays
		// ld : (link dead) il existe des magasins pour ce pays, mais pas de relation par code postal
		// no : il n'existe pas de magasin pour ce pays

		// reset magasin select box
		document.getElementById('magasin_inst').options.length = 0;

		if (str_etat == 'ok' || str_etat == 'no') {
			// pas besoin de mettre à jour la liste des magasins
			document.getElementById('magasindiv_inst').style.visibility = 'hidden';
			document.getElementById('magasindiv_inst').style.display = 'none';
			if (str_etat == 'no') {
				// il faut afficher la list restreinte des pays pour permettre ensuite la selection du magasin
				document.getElementById('paysdiv_inst').style.visibility = 'visible';
				document.getElementById('paysdiv_inst').style.display = '';
				document.getElementById('hrdiv_inst').style.visibility = 'visible';
				document.getElementById('hrdiv_inst').style.display = '';				
				document.getElementById('pays2_inst').selectedIndex = 0;
			} else {
				document.getElementById('paysdiv_inst').style.visibility = 'hidden';
				document.getElementById('paysdiv_inst').style.display = 'none';
				document.getElementById('hrdiv_inst').style.visibility = 'hidden';
				document.getElementById('hrdiv_inst').style.display = 'none';				
				document.getElementById('pays2_inst').selectedIndex = 0;				
			}
		} else {		
			var str = searchReq.responseText.split("\n");
			// skip first line
			
			// add select values
			// str[1] -> magasin list
			var str_MagasinSelect = str[1].split(";");
			for(i=0; i < str_MagasinSelect.length - 1; i++) {
				var str_tmp = str_MagasinSelect[i].split(":");
				if (str_tmp[0] != '')
					document.getElementById('magasin_inst').options[i] = new Option(str_tmp[0],str_tmp[1]);								
			}
			document.getElementById('magasindiv_inst').style.visibility = 'visible';
			document.getElementById('magasindiv_inst').style.display = '';
			document.getElementById('paysdiv_inst').style.visibility = 'hidden';
			document.getElementById('paysdiv_inst').style.display = 'none';
			document.getElementById('hrdiv_inst').style.visibility = 'visible';
			document.getElementById('hrdiv_inst').style.display = '';			
			document.getElementById('pays2_inst').selectedIndex = 0;
		}
	}
}


// Magasin specific treatment
function refreshMagasin2() {
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		var str_pays = escape(document.getElementById('pays2').options[document.getElementById('pays2').selectedIndex].value);		
		if(str_pays == '') {
			document.getElementById('magasindiv').style.visibility = 'hidden';
			document.getElementById('magasindiv').style.display = 'none';
		} else {
			str_search = 'pays=' + str_pays;			
			searchReq.open("GET", '/brisach/fr/fr-fr/ajax/getRelatedMagasin.cfm?' + str_search, true);
			searchReq.onreadystatechange = handleSearchMagasin2; 
			searchReq.send(null);				
		}
	}
}

function refreshMagasin2_install() {
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		var str_pays = escape(document.getElementById('pays2_inst').options[document.getElementById('pays2_inst').selectedIndex].value);		
		if(str_pays == '') {
			document.getElementById('magasindiv_inst').style.visibility = 'hidden';
			document.getElementById('magasindiv_inst').style.display = 'none';
		} else {
			str_search = 'pays=' + str_pays;			
			searchReq.open("GET", '/brisach/fr/fr-fr/ajax/getRelatedMagasin.cfm?' + str_search, true);
			searchReq.onreadystatechange = handleSearchMagasin2_inst; 
			searchReq.send(null);
		}
	}
}

//Called when the AJAX response is returned.
function handleSearchMagasin2() {

	if (searchReq.readyState == 4) {
		var str_pays = escape(document.getElementById('pays2').options[document.getElementById('pays2').selectedIndex].value);
		var str_etat = searchReq.responseText.substring(0,2);
		// ok : il existe une relation magasin - code postal pour ce pays
		// ld : (link dead) il existe des magasins pour ce pays, mais pas de relation par code postal
		// no : il n'existe pas de magasin pour ce pays

		// reset magasin select box
		document.getElementById('magasin').options.length = 0;

		if (str_etat == 'no') {
			// pas besoin de mettre à jour la liste des magasins
			document.getElementById('magasindiv').style.visibility = 'hidden';
			document.getElementById('magasindiv').style.display = 'none';			
		} else {		
			var str = searchReq.responseText.split("\n");
			// skip first line
			
			// add select values
			// str[1] -> magasin list
			var str_MagasinSelect = str[1].split(";");
			for(i=0; i < str_MagasinSelect.length - 1; i++) {
				var str_tmp = str_MagasinSelect[i].split(":");
				if (str_tmp[0] != '')
					document.getElementById('magasin').options[i] = new Option(str_tmp[0],str_tmp[1]);								
			}
			document.getElementById('magasindiv').style.visibility = 'visible';
			document.getElementById('magasindiv').style.display = '';
		}
	}
}
function handleSearchMagasin2_inst() {

	if (searchReq.readyState == 4) {
		var str_pays = escape(document.getElementById('pays2_inst').options[document.getElementById('pays2_inst').selectedIndex].value);
		var str_etat = searchReq.responseText.substring(0,2);
		// ok : il existe une relation magasin - code postal pour ce pays
		// ld : (link dead) il existe des magasins pour ce pays, mais pas de relation par code postal
		// no : il n'existe pas de magasin pour ce pays

		// reset magasin select box
		document.getElementById('magasin_inst').options.length = 0;

		if (str_etat == 'no') {
			// pas besoin de mettre à jour la liste des magasins
			document.getElementById('magasindiv_inst').style.visibility = 'hidden';
			document.getElementById('magasindiv_inst').style.display = 'none';			
		} else {		
			var str = searchReq.responseText.split("\n");
			// skip first line
			
			// add select values
			// str[1] -> magasin list
			var str_MagasinSelect = str[1].split(";");
			for(i=0; i < str_MagasinSelect.length - 1; i++) {
				var str_tmp = str_MagasinSelect[i].split(":");
				if (str_tmp[0] != '')
					document.getElementById('magasin_inst').options[i] = new Option(str_tmp[0],str_tmp[1]);								
			}
			document.getElementById('magasindiv_inst').style.visibility = 'visible';
			document.getElementById('magasindiv_inst').style.display = '';
		}
	}
}


//Called when the AJAX response is returned.
function handleSearchMagasin3() {

	if (searchReq.readyState == 4) {
		var str_pays = escape(document.getElementById('pays2').options[document.getElementById('pays2').selectedIndex].value);
		var str_etat = searchReq.responseText.substring(0,2);
		// ok : il existe une relation magasin - code postal pour ce pays
		// ld : (link dead) il existe des magasins pour ce pays, mais pas de relation par code postal
		// no : il n'existe pas de magasin pour ce pays

		// reset magasin select box
		objSelect=document.getElementById('magasin');
		objSelect.options.length = 0;
		
		if (str_etat == 'no') {
			// pas besoin de mettre à jour la liste des magasins
			document.getElementById('magasindiv').style.visibility = 'hidden';
			document.getElementById('magasindiv').style.display = 'none';			
		} else {		
			var str = searchReq.responseText.split("\n");
			// skip first line
			
			// add select values
			// str[1] -> magasin list
			var str_MagasinSelect = str[1].split(";");
			for(i=0; i < str_MagasinSelect.length - 1; i++) {
				var str_tmp = str_MagasinSelect[i].split(":");
				if (str_tmp[0] != '') {
					optGroup = document.createElement('optgroup');
					optGroup.label = str_tmp[0];
					objSelect.appendChild(optGroup);
				}
				objOption=document.createElement("option");
				objOption.innerText = str_tmp[1];
				objOption.value = str_tmp[2];
				optGroup.appendChild(objOption);
				
					
					//document.getElementById('magasin').options[i] = new OptGroup(str_tmp[0],str_tmp[1]);
					//document.getElementById('magasin').options[i] = new Option(str_tmp[0],str_tmp[1]);								
				//}
			}
			document.getElementById('magasindiv').style.visibility = 'visible';
			document.getElementById('magasindiv').style.display = '';
		}
	}
}
function handleSearchMagasin3_inst() {

	if (searchReq.readyState == 4) {
		var str_pays = escape(document.getElementById('pays2_inst').options[document.getElementById('pays2_inst').selectedIndex].value);
		var str_etat = searchReq.responseText.substring(0,2);
		// ok : il existe une relation magasin - code postal pour ce pays
		// ld : (link dead) il existe des magasins pour ce pays, mais pas de relation par code postal
		// no : il n'existe pas de magasin pour ce pays

		// reset magasin select box
		objSelect=document.getElementById('magasin_inst');
		objSelect.options.length = 0;
		
		if (str_etat == 'no') {
			// pas besoin de mettre à jour la liste des magasins
			document.getElementById('magasindiv_inst').style.visibility = 'hidden';
			document.getElementById('magasindiv_inst').style.display = 'none';			
		} else {		
			var str = searchReq.responseText.split("\n");
			// skip first line
			
			// add select values
			// str[1] -> magasin list
			var str_MagasinSelect = str[1].split(";");
			for(i=0; i < str_MagasinSelect.length - 1; i++) {
				var str_tmp = str_MagasinSelect[i].split(":");
				if (str_tmp[0] != '') {
					optGroup = document.createElement('optgroup');
					optGroup.label = str_tmp[0];
					objSelect.appendChild(optGroup);
				}
				objOption=document.createElement("option");
				objOption.innerText = str_tmp[1];
				objOption.value = str_tmp[2];
				optGroup.appendChild(objOption);
				
					
					//document.getElementById('magasin_inst').options[i] = new OptGroup(str_tmp[0],str_tmp[1]);
					//document.getElementById('magasin_inst').options[i] = new Option(str_tmp[0],str_tmp[1]);								
				//}
			}
			document.getElementById('magasindiv_inst').style.visibility = 'visible';
			document.getElementById('magasindiv_inst').style.display = '';
		}
	}
}
