//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;

// Guide specific treatment
function refreshGuide() {
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		var str_typologie = escape(document.getElementById('search_typologie').options[document.getElementById('search_typologie').selectedIndex].value);
		var str_style = escape(document.getElementById('search_style').options[document.getElementById('search_style').selectedIndex].value);
		var str_energie = escape(document.getElementById('search_energie').options[document.getElementById('search_energie').selectedIndex].value);
		var str_emplacement = escape(document.getElementById('search_emplacement').options[document.getElementById('search_emplacement').selectedIndex].value);
		if(str_typologie == '' && str_style == '' && str_energie == '' && str_emplacement == '') {	
			document.getElementById('search_suggest').style.visibility = 'hidden';
			document.getElementById('search_suggest').innerHTML = '&nbsp;';
		} else {
			str_search = 'str_typologie=' + str_typologie
					  + '&str_style=' + str_style
					  + '&str_energie=' + str_energie
					  + '&str_emplacement=' + str_emplacement;					 
			searchReq.open("GET", '/brisach/fr/fr-fr/ajax/getGuideProducts.cfm?' + str_search, true);
			searchReq.onreadystatechange = handleSearchGuide; 
			searchReq.send(null);			
		}
	}
}

//Called when the AJAX response is returned.
function handleSearchGuide() {

	if (searchReq.readyState == 4) {
		var ss = document.getElementById('search_suggest');
		var str_typologie = escape(document.getElementById('search_typologie').options[document.getElementById('search_typologie').selectedIndex].value);
		var str_style = escape(document.getElementById('search_style').options[document.getElementById('search_style').selectedIndex].value);
		var str_energie = escape(document.getElementById('search_energie').options[document.getElementById('search_energie').selectedIndex].value);
		var str_emplacement = escape(document.getElementById('search_emplacement').options[document.getElementById('search_emplacement').selectedIndex].value);				
		ss.innerHTML = '';
		var str = searchReq.responseText.split("\n");		
		totalCount=str.length;
		if (totalCount != 0) {
			document.getElementById('search_suggest').style.visibility = 'visible';
			ss.innerHTML = str[0];
		}
		// repopulate each select box
		// reset
		document.getElementById('search_typologie').options.length = 0;
		document.getElementById('search_style').options.length = 0;
		document.getElementById('search_energie').options.length = 0;
		document.getElementById('search_emplacement').options.length = 0;
		
		
		// add select values
		// str[1] -> typologie
		var str_TypologieSelect = str[1].split(";");
		for(i=0; i < str_TypologieSelect.length - 1; i++) {
			var str_tmp = str_TypologieSelect[i].split(":");
			if (str_tmp[0] != '')
				document.getElementById('search_typologie').options[i] = new Option(str_tmp[0],str_tmp[1]);
			if (str_typologie == str_tmp[1])
				document.getElementById('search_typologie').selectedIndex = i;								
		}
		
		// str[2] -> style
		var str_StyleSelect = str[2].split(";");
		for(i=0; i < str_StyleSelect.length - 1; i++) {
			var str_tmp = str_StyleSelect[i].split(":");
			if (str_tmp[0] != '')
				document.getElementById('search_style').options[i] = new Option(str_tmp[0],str_tmp[1]);
			if (str_style == str_tmp[1])
				document.getElementById('search_style').selectedIndex = i;				
		}		
		// str[3] -> energie
		var str_EnergieSelect = str[3].split(";");
		for(i=0; i < str_EnergieSelect.length - 1; i++) {
			var str_tmp = str_EnergieSelect[i].split(":");
			if (str_tmp[0] != '')
				document.getElementById('search_energie').options[i] = new Option(str_tmp[0],str_tmp[1]);
			if (str_energie == str_tmp[1])
				document.getElementById('search_energie').selectedIndex = i;				
		}		
		// str[4] -> emplacement
		var str_EmplacementSelect = str[4].split(";");
		for(i=0; i < str_EmplacementSelect.length - 1; i++) {
			var str_tmp = str_EmplacementSelect[i].split(":");
			if (str_tmp[0] != '')
				document.getElementById('search_emplacement').options[i] = new Option(str_tmp[0],str_tmp[1]);
			if (str_emplacement == str_tmp[1])
				document.getElementById('search_emplacement').selectedIndex = i;				
		}
	}
}

// Documentation specific treatment	
function addToBasket(contentid) {
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
			searchReq.open("GET", '/brisach/fr/fr-fr/ajax/addProductToBasket.cfm?productId=' + contentid, true);
			searchReq.onreadystatechange = handleAddToBasket;		
			searchReq.send(null);
	}
}

function handleAddToBasket() {
	if (searchReq.readyState == 4) {
		if (searchReq.responseText.substring(0,2) == 'ok')		
			alert('Produit ajoute au catalogue');
		else if (searchReq.responseText.substring(0,2) == 'in')
			alert('Produit deja present dans le catalogue');
		else if (searchReq.responseText.substring(0,2) == 'mx')
			alert('Le catalogue contient deja le maximum de 15 elements');
		else
			alert('Erreur ...');		
	}
}
