/*****************************************************************************************/

function getWindowHeight() 
	{
		var windowHeight = 0;
		if(typeof(window.innerHeight) == 'number') 
		{
			windowHeight = window.innerHeight;
		}
		else 
		{
			if(document.documentElement && document.documentElement.clientHeight) 
			{
				windowHeight = document.documentElement.clientHeight;
			}
			else 
			{
				if(document.body && document.body.clientHeight) 
				{
					windowHeight = document.body.clientHeight;
				}
			}
		}
	
		return windowHeight;
	}
	
	function setFooter() 
	{
		if(document.getElementById) 
		{
			var windowHeight = getWindowHeight();

			if(windowHeight > 0) 
			{
				var contentHeight = document.getElementById('content').offsetHeight;
				var footerElement = document.getElementById('footer');
				var footerHeight  = footerElement.offsetHeight;
				
				if(windowHeight - (contentHeight + footerHeight) >= 0) 
				{
					footerElement.style.position = 'relative';
					footerElement.style.top = (windowHeight - (contentHeight + footerHeight)) + 'px';
				}
				else 
				{
					footerElement.style.position = 'static';
				}
			}
		}
	}

/*****************************************************************************************/

function press_enter_to_login(e) 
{
	var pK = e? e.which: window.event.keyCode;
	if (pK == 13 )
	{
		document.frmLogin.submit();
	}    
}

/***********************************************************************************/

// pour utiliser cette section il faut declarer cette variable array(); => div_id

function toggle_visibility(id) 
{
	var e = document.getElementById(id);
	if(e.style.display == 'block')
	{
		e.style.display = 'none';
	}
	else
	{
		e.style.display = 'block';
	}
}


// changer d'id 
function toggle_id(id,value)
{	
	//hide_all();
	//show(id);
	var div = document.getElementById(id);
	
	if(value == 'oui') {
		div.style.display = 'block';
	} 
	
	if(value == 'non') 
	{
		div.style.display = 'none';
	}
}

//toutes les cacher
function hide_all()
{
	for (var i=0;i<div_id.length;i++)
	{
		hide(div_id[i]);
	}		  
}

function hide(id) 
{
	if(document.getElementById) 
	{ // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}
	else 
	{
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'none';
		}
	}
}

function show(id) {
  
	if (document.getElementById) 
	{ // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';
	}
	else 
	{
		if (document.layers) 
		{ // Netscape 4
			document.id.display = 'block';
		}
		else 
		{ // IE 4
			document.all.id.style.display = 'block';
		}
	}
}

/***********************************************************************************/

function rediriger(url)
{
	window.location=url;
}

// generate password 

function generatepass(plength,id)
{
	var keylist="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@%&*()-_=+;:";
	var temp = '';
	
	for(i=0; i<plength; i++)
	{
		temp += keylist.charAt(Math.floor(Math.random() * keylist.length));
	}
	
	document.getElementById(id).value = temp;
}

/*****************************************************************************************/

noSequence = 1;
noMedia = 1;
nbreElementsGalerie = 0;
bulleOuverte='';
infoGalerie = new Array();

var OS,browser,version,thestring;
detectBrowser();

/*****************************************************************************************/
/*
function confirmer(message){
	return confirm(message);			  
}
*/
/*****************************************************************************************/

function confirmer(message,url){
	message = confirm(message);			  
	if(message){
		rediriger(url);
	}
}
     
/*****************************************************************************************/

function updateProduitsForCategorie(idElement,idCategorie){
	
	var idSelectProduit = 'id_produit_'+keepNumbers(idElement);
	var oSelectBox = document.getElementById(idSelectProduit);
	
	//On flush la liste des produits
	deleteAllOptions(oSelectBox);
	
	if(idCategorie != "" && listeProduits != null && listeProduits[idCategorie] != null){
		
		//On ajoute tous les produits liés à cette catégorie
		for(i=0;i<listeProduits[idCategorie].length;i++){
			addOption(oSelectBox,listeProduits[idCategorie][i][1],listeProduits[idCategorie][i][0]);
		}
		
	}
}

/*****************************************************************************************/

function detectBrowser(){
	
	var detect = navigator.userAgent.toLowerCase();
	
	//BROWSER
	if(inString(detect,'konqueror')){
		browser = "konqueror";
		OS = "linux";
	}else if(inString(detect,'safari')){
		browser = "safari";
	}else if(inString(detect,'omniweb')){
		browser = "omniweb";
	}else if(inString(detect,'opera')){
		browser = "opera";
	}else if (inString(detect,'webtv')){
		browser = "webtv";
	}else if (inString(detect,'icab')){
		browser = "icab"
	}else if (inString(detect,'msie')){
		browser = "ie"
	}else if (!inString(detect,'compatible')){
		browser = "mozilla";
		version = detect.charAt(8);
	}else{
		browser = "";
	}
	
	//VERSION
	if(!version){
		version = detect.charAt(place + thestring.length);
	}
	
	//PLATE-FORME
	if(!OS)
	{
		if(inString(detect,'linux')){
			OS = "linux";
		}else if(inString(detect,'x11')){
			OS = "unix";
		}else if(inString(detect,'mac')){
			OS = "mac";
		}else if(inString(detect,'win')){
			OS = "windows";
		}else{
			OS = "";
		}
	}
	
	/*
	alert(OS);
	alert(browser);
	alert(version);
	*/
}

/*****************************************************************************************/

function inString(src,needle)
{
	place = src.indexOf(needle) + 1;
	thestring = needle;
	return place;
}

/*****************************************************************************************/

function videoYoutube(idBloc,codeVideo){

	//var idBloc = creerBlocFlash();
	
	var fo = new FlashObject("http://www.youtube.com/v/"+codeVideo, "flash_youtube", "425", "344", "8");
	
	fo.addVariable("allowScriptAccess", "sameDomain");
	fo.addParam("wmode", "transparent");
	fo.addVariable("quality", "high");
	
	//fo.write(idBloc);
	document.getElementById(idBloc).innerHTML += fo.getFlashHTML();	
}

/*****************************************************************************************/

function loadFLV(idBloc,nomFichier,autoplay){
	
	var pathFile = pathLibrairie+nomFichier;
	
	//var idBloc = creerBlocFlash();
	
	var fo = new FlashObject("00-videoplayer.swf", "mymovie", "480", "398", "8");
	fo.addParam("wmode", "transparent");
	fo.addVariable("allowScriptAccess", "sameDomain");
	fo.addVariable("quality", "high");
	fo.addVariable("MEDIA_FILENAME", pathFile);
	fo.addVariable("AUTO_PLAY", autoplay);
	
	//fo.write(idBloc);
	document.getElementById(idBloc).innerHTML += fo.getFlashHTML();	
}

/*
//Fonction d'affichage du player
function player(nomfichier,autoplay){
	
	var strFichier = new String(nomfichier);
	var extension = strFichier.substr(strFichier.length-3, 4);
	
	afficher('<div style="text-align:center;">');
	
	if(extension=='flv'){
		loadFLV(pathLibrairie+nomfichier,autoplay);
	}
	
	afficher('</div>');
}
*/

/*****************************************************************************************/

function setCookie(name, value)
{
	//nouvel objet date
	var aujourdhui = new Date() ;
	
	//nouvel objet date
	var expdate = new Date() ;
	 
	//expiration
	expdate.setTime( aujourdhui.getTime() + ( 7*24*60*60*1000 ) )
	
	//creation du cookie
	document.cookie = name + "=" + value + ";expires=" + expdate.toGMTString() ;
}

/*
function afficherDiapo(listeUrl,listeImages,listeElementsGaleries,listeTitres,listeDesc) {
	
	var so = new FlashObject("flash/diapo/diapo.swf", "flash_diapo", "220", "400", "8");
	
	so.addVariable("allowScriptAccess", "sameDomain");
	so.addParam("wmode", "transparent");
	so.addVariable("quality", "high");

	so.addVariable("ITEM_TO_LOAD", "flash/diapo/diapo.swf");
	so.addVariable("PATH_MEDIAS_TITLES", pathTitles);
	so.addVariable("PATH_MEDIAS_GALLERIES", pathGalleries);
	
	so.addVariable("DIAPO_LISTE_URL", listeUrl);
	so.addVariable("DIAPO_LISTE_IMAGES", listeImages);
	so.addVariable("DIAPO_LISTE_GALERIES", listeElementsGaleries);
	so.addVariable("DIAPO_LISTE_TITRES", listeTitres);
	so.addVariable("DIAPO_LISTE_DESC", listeDesc);
	
	so.write("homepage_diapo");
}
*/

/*****************************************************************************************/

function afficherBannieres(listeImages,listeURL,tempsAffichage,tempsTransition) {
	
	//var so = new FlashObject("flash/header/header_preload.swf", "flash_header", "814", "140", "8");
	var so = new FlashObject("flash/header/header.swf", "flash_header", "814", "140", "8");
	
	so.addVariable("allowScriptAccess", "sameDomain");
	so.addParam("wmode", "transparent");
	so.addVariable("quality", "high");
	
	so.addVariable("PATH_MEDIAS", "http://dev.mbiance.com/files/banniere/big/");
	so.addVariable("DIAPO_LISTE_IMAGES", listeImages);
	so.addVariable("DIAPO_LISTE_URL", listeURL);
	so.addVariable("TEMPS_AFFICHAGE", tempsAffichage);
	so.addVariable("TEMPS_TRANSITION", tempsTransition);
	
	so.write("header_bannieres");
}

/*****************************************************************************************/

function gotoSelect(idElement){
	for (i=0; i < document.getElementById(idElement).options.length; i++) {
		if (document.getElementById(idElement).options[i].selected == true) {
			rediriger(document.getElementById(idElement).options[i].value);
		}
	}
	return false;
}

/*****************************************************************************************/

function addOption(oSelectBox,text,value){
	var optionExistante = false;
	
	for(var i=0;i<oSelectBox.length;i++){
		if(oSelectBox.options[i].value==value){
			optionExistante = true;
		}
	}
	
	//Si elle n'existe pas déjà, on l'ajoute
	if(!optionExistante){
		var optn = new Option(text,value);

		if(browser == "safari"){
			//Safari
			oSelectBox.appendChild(optn);
		}else{
			//Autres
			oSelectBox.options.add(optn);
		}
	}
}

/*****************************************************************************************/

function deleteOption(oSelectBox,position){
	labelText = oSelectBox.options[position].text;
	oSelectBox.options[position] = null;
	return labelText;
}

/*****************************************************************************************/

function deleteAllOptions(oSelectBox,debut){
	if(debut==null){
		debut=1;
	}
	var nbreElements = oSelectBox.length;
	for(i=debut;i<nbreElements;i++){
		label = deleteOption(oSelectBox,debut);
	}
}

/*****************************************************************************************/

function selectOption(oSelectBox,valeurASelectionner){
	for(var i=0;i<oSelectBox.length;i++){

		if(oSelectBox.options[i].value==valeurASelectionner){
			oSelectBox.selectedIndex=i;
		}
	}
}

/*****************************************************************************************/

function rediriger(urlPage,nouvellePage){
	
	if(urlPage != ''){
		
		if(nouvellePage != null){
			openPopup(urlPage);
		}else{
			window.location.href = urlPage;
		}
	}
}

/*****************************************************************************************/

function openPopup(urlPage){
	
	oWindow = window.open(urlPage,"");
}

/*****************************************************************************************/

function addInput(nom, valeur, typeInput, nomForm){
	
	if(nomForm==null){
		//On utilise le premier form existant
		var mainNode = document.getElementsByTagName("form");
		var leForm = mainNode[0];
	}else{
		var leForm = document.forms[nomForm];
	}
	
	element = document.createElement("input");
	element.setAttribute("name", nom);
	element.setAttribute("value", valeur);
	element.setAttribute("type", typeInput);
	leForm.appendChild(element);
}

/*****************************************************************************************/

function soumettreForm(oForm,nomBoutonSubmit,nomForm){

	var soumettreForm=true;
	
	//Soumission du formulaire
	if(oForm.onsubmit){
	     if(oForm.onsubmit()){
	          //OK	
		}else{
			soumettreForm=false;	
		}
	}
	
	if(soumettreForm){
		
		//Si on veut poster le nom du bouton submit
		if(nomBoutonSubmit != null){
			//On crée un input hidden pour qu'on poste le nom du bouton submit
			addInput(nomBoutonSubmit, 1, "hidden", nomForm);
		}
		//document.forms[nomForm].submit();
		oForm.submit();
	}
}

/*****************************************************************************************/

function checkEnter(e){
	
	var codeTouche=document.layers?e.which:document.all?event.keyCode:e.keyCode;
	if(codeTouche==13){	
		return false;
	};
	return true;
}

/*****************************************************************************************/

function bloquerEnter(){
	
	//document.forms[0].onkeypress=bloquerSubmit;
	
	var arrayInput = document.forms[0].getElementsByTagName("input");
	for (var i=0; i<arrayInput.length; i++ ) {
		
		oInput = arrayInput[i];
		
		if (oInput.tagName == "INPUT" && oInput.getAttribute("type") == "text") {
			oInput.onkeypress=bloquerSubmit;
			//oInput.onkeypress=function(e){	return bloquerSubmit(e); }
		}
	}
}

/*****************************************************************************************/

function bloquerSubmit(e){
	
	if(window.event){
		if(window.event.keyCode == 13){
			//IE
			return false;
		}
	}else if(e.which){
		//FIREFOX
		if(e.which == 13){
			return false;
		}
	}
	return true;
}

/*****************************************************************************************/

function getLargeurFenetre()
{
	if(window.innerWidth)
		return window.innerWidth;
	else if(document.body && document.body.offsetWidth)
		return document.body.offsetWidth;
	else
		return 0;
}

/*****************************************************************************************/

function getHauteurFenetre()
{
	if(window.innerHeight)
		return window.innerHeight;
	else if(document.body && document.body.offsetHeight)
		return document.body.offsetHeight;
	else
		return 0;
}

/*****************************************************************************************/

function getCentre(largeur,hauteur){
	
	//largeurTotale = screen.availWidth;
	//hauteurTotale = screen.availHeight;
	
	//OK
	//alert(largeur);
	var pointx=(screen.availWidth-largeur)/2;
	
	//NON
	//alert(hauteur);
	var pointy=(screen.availHeight-hauteur)/2;
	
	var points = new Array(pointx,pointy);
	return points;
}

/*****************************************************************************************/

function findPosition(oElement){
	
	var posX = 0;
	var poxY = 0;
	
	
	if(typeof(oElement.offsetParent) != 'undefined'){
		for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent ){
			posX += oElement.offsetLeft;
			posY += oElement.offsetTop;
		}
	}else{
		posX = oElement.x;
		poxY = oElement.y;
	}
	
	if(document.all){
		//alert("IE");
		posY += 10;
	}else{
		//alert("MOZILLA");
		posY -= 50;
	}
	
	return [posX,posY];
}

/*****************************************************************************************/

function creerNouveauDiv(){

	var id='media'+noMedia;
	
	afficher('<div id="'+id+'" name="'+id+'"></div>');
	noMedia++;
	
	return id;
}

/*****************************************************************************************/

function creerNouveauSpan(){

	var id='media'+noMedia;
	
	afficher('<span id="'+id+'" name="'+id+'"></span>');
	noMedia++;
	
	return id;
}

/*****************************************************************************************/

function afficher(message){
	document.write(message);
	//document.getElementById('contenupage').innerHTML = document.getElementById('contenupage').innerHTML+message;
}

/*****************************************************************************************/

function creerBlocFlash(){
	
	var idBloc = "emplacementFlash"+noSequence;

	document.write('<div id="'+idBloc+'">');
	document.write('<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><div style="color: #000000;"><strong>Pour visualiser ce contenu, vous devez mettre à jour votre Flash Player</strong></div><div style="color: #000000;"><a style="color: #000000;" href="http://www.macromedia.com/go/getflashplayer" target="_blank">Cliquez ici pour télécharger la version la plus récente</a>.</div><br />');
	document.write('</div>');
	
	noSequence++;
	
	return idBloc;
}

/*****************************************************************************************/

function decode(tChaine){
  while (true) {
    var i = tChaine.indexOf('+');
    if (i < 0) break;
    tChaine = tChaine.substring(0,i) + '%20' + tChaine.substring(i + 1, tChaine.length);
  }
  return unescape(tChaine)
}

/*****************************************************************************************/
var lastRow;

function cloneInit(idTable){
	//On clone le premier row pour l'ajouter lors du clic sur le bouton
	cloneRow(idTable);
}

/*****************************************************************************************/

function cloneRow(idTable){
	
	//On pointe sur le tr
	var oTR = document.getElementById(idTable).getElementsByTagName('tr');
	var nbreTR = oTR.length;
	
	lastRow = oTR[nbreTR-1].cloneNode(true);
	
	var listeSelects = lastRow.getElementsByTagName('select');
	var listeInputs = lastRow.getElementsByTagName('input');
	var listeElements = new Array();
	
	for(i=0;i<listeSelects.length;i++){
		listeElements.push(listeSelects[i]);
	}
	for(i=0;i<listeInputs.length;i++){
		listeElements.push(listeInputs[i]);
	}
	
	//On vide les champs
	for(i=0;i<listeElements.length;i++){
		//listeElements[i].name.replace(/\d/g,'')+(nbreTR+1);
		listeElements[i].id = listeElements[i].id.replace(/\d/g,'')+nbreTR;
		listeElements[i].value = '';
	}
}

/*****************************************************************************************/

function addRow(idTable){
	var oTBody = document.getElementById(idTable).getElementsByTagName('tbody')[0];
	oTBody.appendChild(lastRow);
	
	//On clone déjà le row pour être prêt à l'ajouter lors du clic sur le bouton
	cloneRow(idTable);
}

/*****************************************************************************************/

function deleteRow(idElement,idTable){
	
	var noLigne = keepNumbers(idElement);
	
	if(noLigne>0){
		var oTR = document.getElementById(idTable).getElementsByTagName('tr');
		var theRow = oTR[noLigne];
		
		//On cache le row
		theRow.style.display = 'none';
		
		var listeSelects = theRow.getElementsByTagName('select');
		var listeInputs = theRow.getElementsByTagName('input');
		var listeElements = new Array();
		
		for(i=0;i<listeSelects.length;i++){
			listeElements.push(listeSelects[i]);
		}
		for(i=0;i<listeInputs.length;i++){
			listeElements.push(listeInputs[i]);
		}
		
		//On vide les champs contenus dans le row
		for(i=0;i<listeElements.length;i++){
			//listeElements[i].name.replace(/\d/g,'')+(nbreTR+1);
			//listeElements[i].id = listeElements[i].id.replace(/\d/g,'')+nbreTR;
			listeElements[i].value = '';
		}
	}
}

/*****************************************************************************************/

function keepNumbers(str){
	return str.match(/[\d\.]+/g);
}

/*****************************************************************************************/
