/******************************************************************************
* balsanFonctions.js
*******************************************************************************

*******************************************************************************
*                                                                             *
* Copyright 2009									                          *
*                                                                             *
******************************************************************************/
// Slider has a range of xxx pixels
var range = 178;

var slider;

var minVal = 0;
var maxVal = 100;
var cf = range/(maxVal-minVal);
function convert (val) { 
	return Math.round(val/cf)+minVal;
};
function reverse_convert (val) { 
	return Math.round((val-minVal) * cf); 
};
function cal_indice_rho (indiceL) { 
/*
	var fEtape1 = 0.0+indiceL+16.0;
	var fEtape2 = fEtape1 / 116;
	var fEtape3 = fEtape2^3;
	var fEtape4 = fEtape3*100
	return Math.round ( fEtape2 );
	
	*/
	return  Math.round (Math.pow(((indiceL+16) / 116),3)*100 );
};

function afficheMessage(un_texte, txt_bouton1, txt_bouton2, link1, link2){
	
	var div_fond_message = document.getElementById("fond_message");
	var div_message = document.getElementById("message");

	div_fond_message.style.display = "block";
	div_message.style.display = "block";
	
	var p_texte_message = document.createElement("p");
	p_texte_message.appendChild(document.createTextNode(un_texte));
	div_message.appendChild(p_texte_message);
	
	var p_boutons = document.createElement("p");	
	div_message.appendChild(p_boutons);
	
	var bouton1 = document.createElement("input");	
	bouton1.type = "button";
	bouton1.id = "bouton1";
	bouton1.value = txt_bouton1;
	
	if(txt_bouton2 == ""){		
		bouton1.style.styleFloat = "none";	
		bouton1.style.cssFloat = "none";
	} else {		
		bouton1.style.styleFloat = "left";	
		bouton1.style.cssFloat = "left";
	}
	if(link1 != ""){
		bouton1.onclick = function(){redirectMessage(link1);};
	} else {
		bouton1.onclick = function(){cacheMessage();};
	}	
	p_boutons.appendChild(bouton1);
	
	if(txt_bouton2 != ""){
		var bouton2 = document.createElement("input");	
		bouton2.type = "button";
		bouton2.id = "bouton2";
		bouton2.value = txt_bouton2;

		bouton2.style.styleFloat = "right";	
		bouton2.style.cssFloat = "right";
		
		if(link2 != ""){
			bouton2.onclick = function(){redirectMessage(link2);};
		} else {
			bouton2.onclick = function(){cacheMessage();};
		}		
		p_boutons.appendChild(bouton2);		
	}
}

function redirectMessage(link){
	//alert("window.location.href = " + link);
	window.location.href = link;
}

function cacheMessage(){
	//alert("cacheMessage()");
	document.getElementById("fond_message").style.display = "none";
	document.getElementById("message").style.display = "none";		
}

function VerifLogin(){
	
	this.sURLLogin = "/hel_scripts/verifLogin.asp";
	this.sURLPass = "/hel_scripts/verifPass.asp";
	
	this.sLogin = "";
	this.sPass = "";
	this.sEncryptPass = "";
	
	this.spanLogin = null;
	this.spanPass = null;
	this.textLogin = null;
	this.textPass = null;
	this.form = null;
	
	this.postData = "";
	
	this.timeout = 5000;
	
	this.handleSuccess = function(o){
	
		this.spanLogin = YAHOO.util.Dom.get("spanVerifLogin");
		this.spanPass = YAHOO.util.Dom.get("spanVerifPass");
		
		if(o.responseText !== undefined && o.responseText == "1"){
			//alert(o.responseText);
			//this.spanLogin.innerHTML = "OK";	
			this.spanLogin.className = "ok";
		} else {		
			//this.spanLogin.innerHTML = "NOK";
			this.spanLogin.className = "nok";
		}
	};
	
	this.handleFailure = function(o){		
		//alert(o.responseText);
		this.spanLogin = YAHOO.util.Dom.get("spanVerifLogin");
		//this.spanLogin.innerHTML = "Err";
		this.spanLogin.className = "nok";
	};
	this.callback = 
	{
		success:this.handleSuccess,
		failure:this.handleFailure,
		timeout:this.timeout
	};
	
	this.makeRequest = function(){
		this.sLogin = YAHOO.util.Dom.get("login").value;
		this.sPass = YAHOO.util.Dom.get("passwordClear").value;
		this.spanLogin = YAHOO.util.Dom.get("spanVerifLogin");
		this.spanPass = YAHOO.util.Dom.get("spanVerifPass");
		this.textLogin = YAHOO.util.Dom.get("textVerifLogin");
		this.textPass = YAHOO.util.Dom.get("textVerifPass");
		/*if(this.sLogin != ""){
			this.postData = "sLogin="+this.sLogin;
			var request = YAHOO.util.Connect.asyncRequest('POST', this.sURLLogin, this.callback, this.postData);
		} else {
			this.spanLogin = YAHOO.util.Dom.get("spanVerifPass");	
			this.spanLogin.className = "";
			this.spanPass = YAHOO.util.Dom.get("spanVerifPass");	
			this.spanPass.className = "";
		}*/
		if(this.sLogin != ""){
			var regexp = /^([\w\-\.]+)@((\[([0-9]{1,3}\.){3}[0-9]{1,3}\])|(([\w\-]+\.)+)([a-zA-Z]{2,4}))$/;
			if(this.sLogin.match(regexp)){
				this.spanLogin.className = "ok";
				this.textLogin.style.display = "none";
			} else {
				this.spanLogin.className = "nok";
				//this.textLogin.style.display = "block";
			}
		} else {
			this.spanLogin.className = "";	
			this.spanPass.className = "";	
			this.textLogin.style.display = "none";	
			this.textPass.style.display = "none";	
		}
	}
	
	
	this.verifPass2 = function (e){
		this.sPass = YAHOO.util.Dom.get("passwordClear").value;
		this.spanPass = YAHOO.util.Dom.get("spanVerifPass");
		if(this.sPass.length == 4){			
			this.spanPass.className = "ok";
		} else {		
			this.spanPass.className = "nok";
		}
	}
	
	this.verifPass = function (e){
		//alert("bouh2");
		this.sLogin = YAHOO.util.Dom.get("login").value;
		this.sPass = YAHOO.util.Dom.get("passwordClear").value;
		var submitBtn = YAHOO.util.Dom.get("loginBoxSubmit");
			
		if(this.sLogin != "" && this.sPass != ""){
			this.sEncryptPass = calcSHA1(this.sPass);
			var handleSuccess = function(o){
				this.spanPass = YAHOO.util.Dom.get("spanVerifPass");
				this.textPass = YAHOO.util.Dom.get("textVerifPass");
				this.form = YAHOO.util.Dom.get("formLogin");
				if(o.responseText !== undefined && o.responseText == "1"){
					//alert(o.responseText);
					//this.spanPass.innerHTML = "OK";
					//YAHOO.util.Dom.removeClass(submitBtn, "submitDisabled");
					//submitBtn.disabled = false;
					//this.spanPass.className = "ok";
					this.textPass.style.display = "none";	
					//YAHOO.util.Dom.get("password").value = this.sEncryptPass;
					//alert(YAHOO.util.Dom.get("password").value);
					sha1Ciphering();
					this.form.submit();
				} else {		
					//this.spanPass.innerHTML = "NOK";
					//YAHOO.util.Dom.addClass(submitBtn, "submitDisabled");
					//submitBtn.disabled = true;
					//this.spanPass.className = "nok";
					this.textPass.style.display = "block";
				}
			};
			
			var handleFailure = function(o){		
				this.spanPass = YAHOO.util.Dom.get("spanVerifPass");
				this.textPass = YAHOO.util.Dom.get("textVerifPass");
				//alert(o.responseText);
				//this.spanPass.innerHTML = "Err";
				//this.spanPass.className = "nok";
				this.textPass.style.display = "block";
			};
			var callback = 
			{
				success:handleSuccess,
				failure:handleFailure,
				timeout:this.timeout
			};
			
			var postData = "sLogin="+this.sLogin+"&sPass="+this.sEncryptPass;
			var request = YAHOO.util.Connect.asyncRequest('POST',this.sURLPass,callback,postData);			
		} else {
			this.spanPass = YAHOO.util.Dom.get("spanVerifPass");	
			this.spanPass.className = "";
		}
	}
}





isoYUILoader.onReady( function() {
	YAHOO.util.Event.onAvailable("bulle",function(){	
		YAHOO.namespace("helios.container");
		
		var aLabelsBouton1 = {fr : "le site", en : "website", de : "website"};
		var aLabelsBouton2 = {fr : "les produits", en : "products", de : "Produkte"};

		var sLabel1 = aLabelsBouton1[sLangue];
		var sLabel2 = aLabelsBouton2[sLangue];
		
		var searchTypeButtonGroup = new YAHOO.widget.ButtonGroup({ 
										id:  "searchTypeButtonGroup", 
										name:  "search_type", 
										container:  "searchTypeButtonGroup" });
																	
		searchTypeButtonGroup.addButtons([
			{
				label: sLabel2,
				value: "prod",
				onclick:{fn:switchToProdSearch, scope:searchTypeButtonGroup}
			},
			{
				label: sLabel1,
				value: "site",
				checked: true,
				onclick:{fn:switchToSiteSearch, scope:searchTypeButtonGroup}
			}																	
			
		]);	
		
		YAHOO.helios.container.searchBox = 
				new YAHOO.widget.Overlay("box_moteur_recherche", 
						{ context:["innerBandes","tr","tr", ["beforeShow", "windowResize"]], 
							visible:false, 
							width:"213px",
							zindex:20000} );
		YAHOO.helios.container.searchBox.render();
		
		var YSiteButton = searchTypeButtonGroup.getButton(1);
		var YProdButton = searchTypeButtonGroup.getButton(0);
		
		var div_search_box = YAHOO.util.Dom.get("box_moteur_recherche");
				
		if(bCatalogue){
			YProdButton.set("checked",true);
			YAHOO.util.Dom.replaceClass(div_search_box, "hidden", "displayed");
			YAHOO.helios.container.searchBox.show();
			
			moveInsetDown();
			YAHOO.util.Event.addListener("advanced_search_btn","click",moveInsetDown);
	
			var inputSiteSearch = YAHOO.util.Dom.get("txtRechercheFull");
			var btnSiteSearch = YAHOO.util.Dom.get("btnSubmitFull");
			inputSiteSearch.disabled = true;
			btnSiteSearch.disabled = true;
			
			YAHOO.util.Dom.get("search_box_close_btn").style.display = "none";
			YAHOO.util.Dom.replaceClass("box_moteur_recherche", "box_moteur_rechercheDisabled", "box_moteur_rechercheEnabled");
	
			sliderSync ();
		} else {
			YAHOO.util.Event.addListener("search_box_close_btn","click",switchToSiteSearch, searchTypeButtonGroup, true);
		}
		
		YAHOO.util.Event.addListener("txtRechercheFull","focus",siteSearchFocus);
		YAHOO.util.Event.addListener("txtRechercheFull","blur",siteSearchBlur);
		
		var oVerifLogin = new VerifLogin();
		YAHOO.util.Event.addListener("login","keyup",oVerifLogin.makeRequest, oVerifLogin, true);
		YAHOO.util.Event.addListener("login","click",oVerifLogin.makeRequest, oVerifLogin, true);
		YAHOO.util.Event.addListener("login","blur",oVerifLogin.makeRequest, oVerifLogin, true);
		
		YAHOO.util.Event.addListener("passwordClear","keyup",oVerifLogin.verifPass2, oVerifLogin, true);
		YAHOO.util.Event.addListener("passwordClear","click",oVerifLogin.verifPass2, oVerifLogin, true);
		//YAHOO.util.Event.addListener("passwordClear","blur",oVerifLogin.verifPass, oVerifLogin, true);
		
		YAHOO.util.Event.addListener("loginBoxSubmit","click",oVerifLogin.verifPass, oVerifLogin, true);
		
		var div_modal = YAHOO.util.Dom.get("modal");
		YAHOO.util.Dom.setXY(div_modal, [0,0]);
		


	});
});

function sliderSync () {
	if (slider==null) {
		//----------------------------------- Slider (indice réflexion lumineuse)	-------------------------------------

		// No ticks for this example
		var tickSize = 0;

		// We'll set a minimum distance the thumbs can be from one another
		var minThumbDistance = 1;
		// Initial values for the thumbs
		var hiddenValMin = YAHOO.util.Dom.get("indiceReflMin");
		var hiddenValMax = YAHOO.util.Dom.get("indiceReflMax");
		var initValues = [reverse_convert(hiddenValMin.value),reverse_convert(hiddenValMax.value)];

		var spanValMin = YAHOO.util.Dom.get("minVal");
		var spanValMax = YAHOO.util.Dom.get("maxVal");
		
		var sldIndice = YAHOO.util.Dom.get("indiceSlider");
		slider = YAHOO.widget.Slider.getHorizDualSlider(sldIndice,
				"demo_min_thumb", "demo_max_thumb",
				range, tickSize, initValues);
		//slider.minRange = 1;
		
		var divSliderJauge = YAHOO.util.Dom.get("sliderJauge");
		var updateUI = function () {
			// Update the converted values and the slider's title.
			// Account for the thumb width offsetting the value range by
			// subtracting the thumb width from the max value.
			var min = convert(slider.minVal),
			max = convert(slider.maxVal);
			
			spanValMin.innerHTML  = "<span class=\"indiceL\">"+min+"</span><br/><span class=\"indiceRho\">"+cal_indice_rho(min)+"</span>" ;
			spanValMax.innerHTML  = "<span class=\"indiceL\">"+max+"</span><br/><span class=\"indiceRho\">"+cal_indice_rho(max)+"</span>" ;
			hiddenValMin.value  = min;
			hiddenValMax.value  = max;
			
			divSliderJauge.style.left= ""+(slider.minVal+8)+"px";
			divSliderJauge.style.width=""+(slider.maxVal - slider.minVal) + "px";
			
		};

		slider.subscribe('ready', updateUI);
		slider.subscribe('change', updateUI);
	} else {
		var hiddenValMin = YAHOO.util.Dom.get("indiceReflMin");
		var hiddenValMax = YAHOO.util.Dom.get("indiceReflMax");
		var initValues = [reverse_convert(hiddenValMin.value),reverse_convert(hiddenValMax.value)];
		slider.setValues(initValues[0],initValues[1]); 
	}
}

function moveInsetDown(){
	var div_encart_droit = YAHOO.util.Dom.get("encartDroitCatalogue");
	var coords = YAHOO.util.Dom.getRegion("box_moteur_recherche");
	var height = coords["bottom"] - coords["top"];
	//alert(height);
	//if (div_encart_droit!=null) {
		div_encart_droit.style.paddingTop = height + "px";
	//}
}

// Focus sur le champ de recherche "tout le site"
function siteSearchFocus(e,a,b) {
	var inputSiteSearch = YAHOO.util.Dom.get("txtRechercheFull");
	//txtRechercheFullNoFocus
//#DA8883 - #B7B7B7

		YAHOO.util.Dom.replaceClass("txtRechercheFull", "txtRechercheFullNoFocus", "txtRechercheFullFocus");
}

// Blur sur le champ de recherche "tout le site"
function siteSearchBlur() {
	var inputSiteSearch = YAHOO.util.Dom.get("txtRechercheFull");
//#DA8883 - #B7B7B7
		YAHOO.util.Dom.replaceClass("txtRechercheFull", "txtRechercheFullFocus", "txtRechercheFullNoFocus");
}

// Passage à la recherche produit (= activation moteur de recherche produit + désactivation recherche tout site)
function switchToProdSearch(e){
	//alert("click");	
	var div_search_box = YAHOO.util.Dom.get("box_moteur_recherche");
	var div_modal = YAHOO.util.Dom.get("modal");
	var div_search_box_overlay = YAHOO.util.Dom.get("search_box_overlay");
	
	var inputSiteSearch = YAHOO.util.Dom.get("txtRechercheFull");
	var btnSiteSearch = YAHOO.util.Dom.get("btnSubmitFull");
	inputSiteSearch.disabled = true;
	btnSiteSearch.disabled = true;																																						
	
	YAHOO.util.Dom.replaceClass(div_search_box, "hidden", "displayed");
	if(!bCatalogue){
		YAHOO.util.Dom.replaceClass(div_modal, "hidden", "displayed");
		YAHOO.helios.container.searchBox.show();
	} else {
		YAHOO.util.Dom.replaceClass(div_search_box_overlay, "displayed", "hidden");
	}
	
	YAHOO.util.Dom.replaceClass("box_moteur_recherche", "box_moteur_rechercheDisabled", "box_moteur_rechercheEnabled");
	
	sliderSync ();
}

// Passage à la recherche produit (= activation moteur de recherche produit)
function switchToSiteSearch(e){	
	var YProdButton = this.getButton(0);
	var div_modal = YAHOO.util.Dom.get("modal");
	var div_search_box_overlay = YAHOO.util.Dom.get("search_box_overlay");
	
	var inputSiteSearch = YAHOO.util.Dom.get("txtRechercheFull");
	var btnSiteSearch = YAHOO.util.Dom.get("btnSubmitFull");
	inputSiteSearch.disabled = false;
	inputSiteSearch.focus();
	btnSiteSearch.disabled = false;
	
	if(!bCatalogue){
		YAHOO.util.Dom.replaceClass(div_modal, "displayed", "hidden");
		YAHOO.helios.container.searchBox.hide();
	} else {
	
		YAHOO.util.Dom.replaceClass("box_moteur_recherche", "box_moteur_rechercheEnabled", "box_moteur_rechercheDisabled");
		YAHOO.util.Dom.replaceClass(div_search_box_overlay, "hidden", "displayed");
	}
	this.check(1);
}

// affichage boîte de login
function showLoginBox(){
	var divSearchTypeButtonGroup = YAHOO.util.Dom.get("moteur_recherche");
	var divLoginbox = YAHOO.util.Dom.get("loginBox");
	
	//alert(divLoginbox.style.display);
	if(divLoginbox.style.display == "block"){
		divLoginbox.style.display = "none";
		//YAHOO.helios.container.loginBox.hide();
	} else {
		//YAHOO.helios.container.loginBox.show();
		divLoginbox.style.display = "block";
	}
}
// affichage boîte de login
function displayLoginBox(){
	var divRef = YAHOO.util.Dom.get("rechercheFull");
		
	var divLoginbox = YAHOO.util.Dom.get("loginBox");
	var region = YAHOO.util.Dom.getRegion(divRef);
	var position = parseInt(region.right) - 218;
	position = position + "px";
	YAHOO.util.Dom.setStyle(divLoginbox,"left",position);
	divLoginbox.style.display = "block";

}
// affichage boîte de login
function hideLoginBox(){
	
	var divCentreur = YAHOO.util.Dom.getElementsByClassName("centreur")[0];
	//divCentreur.style.zIndex = 2;
	
	var divLoginbox = YAHOO.util.Dom.get("loginBox");
	//alert(divLoginbox.style.display);
	/*if(divLoginbox.style.display == "block"){
		divLoginbox.style.display = "none";
		//YAHOO.helios.container.loginBox.hide();
	} else {*/
		//YAHOO.helios.container.loginBox.show();
		divLoginbox.style.display = "none";
	//}
}

function redirLang(){
	var unSelect = document.getElementById("selectLanguage");
	var langue = unSelect.options.selectedIndex;
	switch(langue){
		case 0:
		window.location = "/index.asp";
		break;
		case 1:
		window.location = "/EN/index.asp";
		break;
		case 3:
		window.location = "/DE/index.asp";
		break;
	}
}
