var _CONFIG_MATFINDER = Array();
_CONFIG_MATFINDER["type"] = -1;
_CONFIG_MATFINDER["critere_0"] = -1;
_CONFIG_MATFINDER["critere_1"] = -1;
_CONFIG_MATFINDER["critere_2"] = -1;
_CONFIG_MATFINDER["critere_3"] = -1;

function iniMatFinderCritere(){
	var type = document.getElementById('matfinder-type').value;
	if(_CONFIG_MATFINDER["type"] == -1){
		_CONFIG_MATFINDER["type"] = type;
	}
	//alert(type);
}

function selectMatFinderCritere(holder,critere){
	var container = document.getElementById('critere_'+holder);
	for(i=0;i<container.childNodes.length;i++){
		var check = container.childNodes[i];
		if(check.nodeName == "A"){
			if(check.id != 'critere_'+holder+"_"+critere){
				check.className = "";
			}else if(check.id == 'critere_'+holder+"_"+critere){
				check.className = "active";
			}
		}
	}
		if(_CONFIG_MATFINDER["critere_"+holder] != critere){
			_CONFIG_MATFINDER["critere_"+holder] = critere;
			if(exceptionMatFinderCritere(holder,critere)){
				callProducts();
			}else{
				activeMatFinderCritere(holder);
			}
		}else if(_CONFIG_MATFINDER["critere_"+holder] == critere){
			_CONFIG_MATFINDER["critere_"+holder] = critere;
		}
}
function enableMatFinderCritere(holder){
	if(_CONFIG_MATFINDER["type"]==1 && _CONFIG_MATFINDER["critere_1"]==4 && holder==2){
		make_Visible('critere_2_holder', false);
		make_Visible('critere_3_holder', true);
		make_Visible('critere_3_mask', false);
	}else {
		make_Visible('critere_2_holder', true);
		make_Visible('critere_2_mask', true);
		make_Visible('critere_3_holder', false);
		make_Visible('critere_3_mask', false);
	}
	make_Visible('critere_'+holder+'_mask', false);
}
function disableMatFinderCritere(holder){
	make_Visible('critere_'+holder+'_mask', true);
}
function deselectAllMatFinderCritere(holder){
	var container = document.getElementById('critere_'+holder);
	_CONFIG_MATFINDER['critere_'+holder] = -1;
	for(i=0;i<container.childNodes.length;i++){
		var check = container.childNodes[i];
		if(check.nodeName == "A"){
			check.className = "";
		}
	}
}
function activeMatFinderCritere(holder){
	switch(holder){
		case 0:
			enableMatFinderCritere(1);
			deselectAllMatFinderCritere(1);
			disableMatFinderCritere(2);
			deselectAllMatFinderCritere(2);
			callProducts();
			break;
		case 1:
			if(_CONFIG_MATFINDER["type"]==1 && _CONFIG_MATFINDER["critere_1"]!=4){
				_CONFIG_MATFINDER["critere_3"] = -1;
				deselectAllMatFinderCritere(3);
			}
			enableMatFinderCritere(2);
			deselectAllMatFinderCritere(2);
			callProducts();
			break;
		case 2:
			callProducts();
			break;
		case 3:
			callProducts();
			break;
		default:
			break;
	}
}
function exceptionMatFinderCritere(holder,critere){
	var _EXCEPTIONS = Array(
													Array(
																"1:3"
																),
													Array(),
													Array()
													);
	var check = holder+":"+critere;
	for(i=0;i<_EXCEPTIONS[_CONFIG_MATFINDER["type"]].length;i++){
			if(_EXCEPTIONS[_CONFIG_MATFINDER["type"]][i] == check){
				disableMatFinderCritere(holder+1);
				deselectAllMatFinderCritere(holder+1);
				return true;
			}
	}
	return false;
}

function getMatFinderType(){
	var type = "";
	if(_CONFIG_MATFINDER["type"] != -1){
		type += _CONFIG_MATFINDER["type"];
		if(_CONFIG_MATFINDER["critere_0"] != -1){
			type += ":"+_CONFIG_MATFINDER["critere_0"];
			if(_CONFIG_MATFINDER["critere_1"] != -1){
				type += ":"+_CONFIG_MATFINDER["critere_1"];
				if(_CONFIG_MATFINDER["critere_2"] != -1){
					if(_CONFIG_MATFINDER["type"]==0 && _CONFIG_MATFINDER["critere_2"]==2){
					
					}else{
						type += ":"+_CONFIG_MATFINDER["critere_2"];
						if(_CONFIG_MATFINDER["critere_3"] != -1){
							type += ":"+_CONFIG_MATFINDER["critere_3"];
						}
					}
				}else if(_CONFIG_MATFINDER["type"]==1 && _CONFIG_MATFINDER["critere_1"]==4){
					type += ":9";
					if(_CONFIG_MATFINDER["critere_3"] != -1){
						type += ":"+_CONFIG_MATFINDER["critere_3"];
					}
				}
			}
		}
		return type;
	}
	return;
}

function callMatFinder(){
	showMatLoader();
	var type = getMatFinderType();
	alert(type);
	var xhReq = createXMLHttpRequest();
 	xhReq.open("get", "php/php_get_matfinder.php?type="+type, true);
 	xhReq.onreadystatechange = function() {
   	if (xhReq.readyState != 4)  { return; }
		//alert(xhReq.responseText);
	  var serverResponse = eval(xhReq.responseText);
		var side = "right";
		alert(serverResponse.length);
		document.getElementById('matfinder-results-template-left').innerHTML = '';
		document.getElementById('matfinder-results-template-right').innerHTML = '';
		if(serverResponse.length>0){
			for(i=0;i<serverResponse.length;i++){
				alert(i);
				side = (side=="left") ? "right" : "left";
				loadMatFinder2(side,serverResponse[i]);
				if(i == (serverResponse.length-1)){
					hideMatLoader();
				}
			}
			if(serverResponse.length == 1){
				document.getElementById('matfinder-results-template-right').innerHTML = '';
			}
		}else{
			document.getElementById('matfinder-results-template-left').innerHTML = '<span class="texte">Aucune carpette de disponible</span>';
		}
		hideMatLoader();
	};
 	xhReq.send(null);
}

function callProducts(section,i,id){
	showMatLoader();
	var type = section>-1 ? section : getMatFinderType() ;
	//alert(type);
	var soustype = i>-1 ? i : "" ;
	//alert(soustype);
	var id = id>-1 ? id : "";
	var lang = document.getElementById('lang').value;
	var xhReq = createXMLHttpRequest();
	//alert("php/php_get_matfinder.php?type="+type+"&soustype="+soustype+"&id="+id);
 	xhReq.open("get", "php/php_get_matfinder.php?type="+type+"&soustype="+soustype+"&id="+id+"&lang="+lang, true);
 	xhReq.onreadystatechange = function() {
		if (xhReq.readyState != 4)  { return; }
		//alert(xhReq.responseText);
		var serverResponse = eval(xhReq.responseText);
		var side = "right";
		//alert(serverResponse.length);
		document.getElementById('matfinder-results-template-left').innerHTML = '';
		document.getElementById('matfinder-results-template-right').innerHTML = '';
		if(serverResponse.length>0){
			for(i=0;i<serverResponse.length;i++){
				side = (side=="left") ? "right" : "left";
				loadMatFinder2(side,serverResponse[i]);
				if(i == (serverResponse.length-1)){
					hideMatLoader();
				}
			}
			if(serverResponse.length == 1){
				document.getElementById('matfinder-results-template-right').innerHTML = '';
			}
		}else{
			if(lang=="fr"){
				document.getElementById('matfinder-results-template-left').innerHTML = '<span class="texte">Aucune carpette de disponible.<br>Contactez notre Service &agrave; la client&egrave;le pour assistance au 800-363-6185</span>';
			}else{
				document.getElementById('matfinder-results-template-left').innerHTML = '<span class="texte">No mat available. <br>Please contact Customer Service for assistance (800-363-6185)</span>';
			}
			hideMatLoader();
		}
	};
 	xhReq.send(null);
}

function callProductsById(obj){
	var val = obj.value;
	if(val > -1){
		callProducts("","",val);
		deselectAllMatFinderCritere(0);
		disableMatFinderCritere(1);
		deselectAllMatFinderCritere(1);
		disableMatFinderCritere(2);
		deselectAllMatFinderCritere(2);
		_CONFIG_MATFINDER["critere_0"] = -1;
		_CONFIG_MATFINDER["critere_1"] = -1;
		_CONFIG_MATFINDER["critere_2"] = -1;
		_CONFIG_MATFINDER["critere_3"] = -1;
	}
}

function loadProducts(side, produits){
	var column = document.getElementById('matfinder-results-template-'+side);
	column.innerHTML = "";
	if(produits.length > 0){
		for(i=0;i<produits.length;i++){
			column.appendChild(getMatFinderProduct(produits[i]));
		}
		hideMatLoader();
	}else if(produits.length == 0 && side == "left"){
			var lang = document.getElementById('lang').value;
			if(lang == "en"){
				document.getElementById('matfinder-results-template-left').innerHTML = '<span class="texte">No available mat</span>';
			}else{
			document.getElementById('matfinder-results-template-left').innerHTML = '<span class="texte">Aucune carpette de disponible</span>';
			}
		 
		 hideMatLoader();
	}
	if(side == "right"){hideMatLoader();}
}

function loadMatFinder(side, produits){
	var lang = document.getElementById('lang').value;
	var column = document.getElementById('matfinder-results-template-'+side);
	column.innerHTML = "";
	if(produits.length > 0){
		for(pi=0;pi<produits.length;pi++){
			//column.appendChild(getMatFinderProduct(produits[i]));
			column.innerHTML += getMatFinderProduct2(produits[pi]);
			
		}
		hideMatLoader();
	}else if(produits.length == 0 && side == "left"){
		if(lang == "en"){
		 document.getElementById('matfinder-results-template-left').innerHTML = '<span class="texte">Aucune carpette de disponible</span>';
		}else{
		
		}
		 document.getElementById('matfinder-results-template-right').innerHTML = '';
		 hideMatLoader();
	}
	if(side == "right"){hideMatLoader();}
}

function loadMatFinder2(side, produit){
	var column = document.getElementById('matfinder-results-template-'+side);
	column.innerHTML += getMatFinderProduct2(produit);
}

function getMatFinderProduct2(produit){
	var lang = document.getElementById('lang').value;
	html = '<table width="364" border="0" cellspacing="0" cellpadding="0" class="table">';
	html +='	<tr>';
	html +='		<td width="118" align="left" valign="top">';
	var detailBtn = false;
	if(produit[0].length > 0){
					if(produit[0][0] != ""){
						html +='<img src="'+produit[0][0]+'" style="margin-bottom:8px;"/>';
					}
					if(produit[0][1] != ""){
						if(produit[0][0] != "" || produit[1][5] != ""){html +='<br>';}
						if(produit[0][2] != ""){
							html +='<a href="'+produit[0][2]+'" target="_blank">';
						}
						html +='<img src="'+produit[0][1]+'" style="margin-bottom:8px;" border="0"/>';
						if(produit[0][2] != ""){
							html +='</a>';;
						}
					}
					var pdfname = produit[1][5].split("/");
					pdfname = pdfname[pdfname.length-1];
					if(produit[1][5] != ""){
						if(produit[0][0] != ""){html +='<br>';}
						html +='<a href="'+produit[1][5]+'" target="_blank" class="more_details" style="margin-bottom:8px;"  onClick="javascript: pageTracker._trackPageview(\'/pdf/'+lang+'/produits/'+pdfname+'\');">';
						if(lang == "en"){
							html +='Learn More';
						}else{
							html +='En savoir plus';
						}
						html +='</a>';
					}
	}
	/*Rating*/
	var ratingCellData = ["Grattage","Essuyage","R&eacute;sistance &agrave;<br />l'&eacute;crasement"];
	var ratingCellData11 = ["Scraping","Wiping","Crush resistance"];
	var ratingCellData2 = ["Anti-fatigue","Durabilit&eacute;","Adh&eacute;rence"];
	var ratingCellData22 = ["Anti-fatigue","Durability","Traction"];
	var rateThis = "";
	for(i=0;i<produit[2].length;i++){
		if(produit[2][i] > 0){
			rateThis += "true";
		}else{
			rateThis += "false";
		}
	}
	if(rateThis.indexOf("true") > -1){
		html +='<table width="118" border="0" cellspacing="0" cellpadding="0" id="evaluation-holder">';
						for(i=0;i<produit[2].length;i++){
							html +='<tr>';
							html +='	<td align="left" valign="top">';
							if(produit[1][4]){
								if(lang == "en"){
									html += 	ratingCellData11[i];
								}else{
									html += 	ratingCellData[i];
								}
							}else{
								if(lang == "en"){
									html += 	ratingCellData22[i];
								}else{
									html += 	ratingCellData2[i];
								}
							}
							html +='		<div class="evaluation-bars">';
							for(ii=0;ii<10;ii++){
								if(ii < produit[2][i]){
									path = "images/evaluation-point.gif";
								}else{
									path = "images/evaluation-nopoint.gif";
								}
								html +='<img src="'+path+'"/>';
							}
							html +='		</div>';
							html +='	</td>';
							html +='</tr>';
						}
		html +='</table>';
	}
	html +='		</td>';
	html +='		<td width="10" align="left" valign="top">';
	html +='		</td>';
	html +='		<td align="left" valign="top">';
	html +='		<p class="titre">'+produit[1][0]+'<br><span class="sous-titre">'+unescape(produit[1][3])+'</span></p>';
	html +='		<p class="description">'+unescape(produit[1][1])+'</p>';
	if(produit[1][2][0] != ""){
		if(lang == "en"){
									html +='		<p class="titre-caracteristiques">Characteristics</p>';
		}else{
									html +='		<p class="titre-caracteristiques">Caract&eacute;ristiques</p>';
		}
	
							for(i=0;i<produit[1][2].length;i++){
								html +='<li class="description-caracteristiques">'+produit[1][2][i]+'</li>';
							}
	}
	html +='		</td>';
	html +='	</tr>';
	html +=' </table>';
	return html;
}

function getMatFinderProduct(produit){
	
	var mainTable = document.createElement('table');
	mainTable.width = 364;
	mainTable.border = 0;
	mainTable.cellspacing = 0;
	mainTable.cellpadding = 0;
	mainTable.className = "table";
	
	var row = document.createElement('tr');
	
	var td1 = document.createElement('td');
	td1.width = 118;
	td1.align = "left";
	td1.valign = "top";
	for(i=0;i<produit[0].length;i++){//image
		if(produit[0][i] != ""){
			var img = document.createElement('img');
			img.src = produit[0][i];
			td1.appendChild(img);
		}
	}
	
	var ratingTable = document.createElement('table'); //criteres
	ratingTable.width = 118;
	ratingTable.border = 0;
	ratingTable.cellspacing = 0;
	ratingTable.cellpadding = 0;
	ratingTable.id = "evaluation-holder";
	var ratingCellData = ["Grattage","Essuyage","R&eacute;sistance &agrave;<br />l'&eacute;crasement"];
	for(i=0;i<produit[2].length;i++){
		var ratingRow = document.createElement('tr');
		var ratingCell = document.createElement('td');
		ratingCell.align = "left";
		ratingCell.valign = "top";
		var ratingDiv = document.createElement('div');
		ratingDiv.className = "evaluation-bars";
		for(ii=0;ii<10;ii++){
			var img = document.createElement('img');
			if(ii < produit[2][i]){
				path = "images/evaluation-point.gif";
			}else{
				path = "images/evaluation-nopoint.gif";
			}
			img.src = path;
			ratingDiv.appendChild(img);
		}
		ratingCell.innerHTML = ratingCellData[i];
		ratingCell.appendChild(ratingDiv);
		ratingRow.appendChild(ratingCell);
		ratingTable.appendChild(ratingRow);
	}
	
	td1.appendChild(ratingTable);
	
	var td2 = document.createElement('td');
	td2.width = 10;
	td2.align = "left";
	td2.valign = "top";
	
	var td3 = document.createElement('td');
	td3.align = "left";
	td3.valign = "top";
	
	var titreP = document.createElement('p');
	titreP.className = "titre";
	titreP.innerHTML = produit[1][0];
	
	var texteP = document.createElement('p');
	texteP.className = "description";
	texteP.innerHTML = unescape(produit[1][1]);
	
	var caracP = document.createElement('p');
	caracP.className = "titre-caracteristiques";
	caracP.innerHTML = "Caract&eacute;ristiques";
	
	td3.appendChild(titreP);
	td3.appendChild(texteP);
	td3.appendChild(caracP);
	
	for(i=0;i<produit[1][2].length;i++){
		var li = document.createElement('li');
		li.className = "description-caracteristiques";
		li.innerHTML = produit[1][2][i];
		td3.appendChild(li);
	}
	row.appendChild(td1);
	row.appendChild(td2);
	row.appendChild(td3);
	mainTable.appendChild(row);
	return mainTable;
}

function showMatLoader(){
	make_Visible('matfinder_mask',true);
}
function hideMatLoader(){
	make_Visible('matfinder_mask',false);
}