var _isMozilla = window.Event ? 1 : 0;						// Mozilla ou IE
var _d = document;
var _otooltip = '';											// id de la derniere tooltip ouverte
var _rnode = '';											// noeud racine (pour IE)

var divs_loaded = new Array();								// Contient le nom des Divs déjà chargés par xmlconnect
var sh ;											
var DefaultHeight = new Array();
var timer = null;
var height;
var div;
var move = 'show';
var showed_blok ;

// Ouvre / referme un div.
function OpenDiv(div)
{
	d = document.getElementById(div);
	// Si le div est déjà ouvert, fermons-le
	if (divs_loaded[div] && divs_loaded[div] == 'open')
	{
		divs_loaded[div] = 'close';
	}
	
	// Si le div n'est pas ouvert, ouverture
	else
	{
		divs_loaded[div] = 'open';
	}
	
	
}

// Afficher calque ( une autre )
function look(id)
{
	var show = document.getElementById(id).style ;
	show.display = show.display == 'inline' ? "none" : "inline" ;
}



// Ouvrir popup
function ImgPopup(imageFile, width, height, title){

	var html = '<html><head><title>' + title + ' - Cliquez pour fermer <\/title></head><body leftmargin=0 topmargin=0 marginwidth=0 marginheight=0 onclick="javascript:window.close()"><img src="' + imageFile + '" alt="Cliquez pour fermer" /><\/body></html>';
	var popup = window.open(imageFile, '_blank', 'width=' + width+ ', height=' + height + ', status=no');
	popup.document.write(html);
	popup.focus();
}





// Connexion "ajax" à une page "page", envoie des données "content" en POST, et affiche les infos dans l'élément "div".
function XmlConnect(page, content, div, append, loader)
{
	// Pour ne charger le contenu qu'une seule fois
	if (divs_loaded[div] && divs_loaded[div] =='open')
	{
		divs_loaded[div] = 'close';
		return false;
	}
	else
		divs_loaded[div] = 'open';
		
	
	
	var xhr_object = null; 			// Déclaration de la var objet..
	 
	// Instance de l'objet : pour FF ou IE
	if(window.XMLHttpRequest) // Firefox 
	   xhr_object = new XMLHttpRequest(); 
	else if(window.ActiveXObject) // Internet Explorer 
	   xhr_object = new ActiveXObject("Microsoft.XMLHTTP"); 
	else { // XMLHttpRequest non supporté par le navigateur 
	   alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
	   return; 
	} 
	 
	
	xhr_object.open("POST", page, true); 				// Préparation de la requête : mode de transmission, url, mode synchrone / asynchrone
	
	var divcontent = document.getElementById(div).innerHTML;	// On sauvegarde le contenu originel du div
	
	xhr_object.onreadystatechange = function() 
	{ 
		
		if(xhr_object.readyState == 4) 
		{
			if ( append == 'after')			// Contenu placé après
				document.getElementById(div).innerHTML = divcontent + xhr_object.responseText;
			else if(append == 'before')		// Contenu placé avant
				document.getElementById(div).innerHTML = xhr_object.responseText + divcontent ;
			else							// Contenu remplacé	
				document.getElementById(div).innerHTML = xhr_object.responseText ;
		}
			
		else
		{
			// On affiche le loader ?
			if (loader == 'true' )
			{
				
				if(xhr_object.readyState == 1 || xhr_object.readyState == 2 || xhr_object.readyState == 3) 
					document.getElementById(div).innerHTML = "<br /><div align='center'><img src='netbuzz_public/skins/default/images/ajax-loader.gif' alt='Chargement' /></div><br />";
			}
		}
	} 

	if ( typeof(content) == 'undefined')
		xhr_object.send(null); 
	else
	{
		xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
		xhr_object.send(content); 
	}
	 
 }
 
 
function XmlConnect2(page, content)
{

	var xhr_object = null; 			// Déclaration de la var objet..
	 
	// Instance de l'objet : pour FF ou IE
	if(window.XMLHttpRequest) // Firefox 
	   xhr_object = new XMLHttpRequest(); 
	else if(window.ActiveXObject) // Internet Explorer 
	   xhr_object = new ActiveXObject("Microsoft.XMLHTTP"); 
	else { // XMLHttpRequest non supporté par le navigateur 
	   alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
	   return; 
	} 
	 
	xhr_object.open("POST", page, true); // Préparation de la requête : mode de transmission, url, mode synchrone / asynchrone
	
	xhr_object.onreadystatechange = function() 
	{ 
		if(xhr_object.readyState == 4) 
		{
			//document.getElementById('debug').innerHTML = xhr_object.responseText;
			eval(xhr_object.responseText) ;
		}
			
	} 
	if ( typeof(content) == 'undefined')
		xhr_object.send(null); 
	else
	{
		xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
		xhr_object.send(content); 
	}
 
 }
 
 
 function Init_Var(h, d, m)
{
	div = document.getElementById(d);
	
	var length = (div.style.height).length ;
	len = length - 2 ;
	sh = div.style.height.substr(0, len);

	sh = parseInt(sh);
	
	if (sh == 0) {
		move = 'show'; }
	else
	{
		move = 'hide';
		div.style.height = h + 'px' ; //200px';
	}

	height = h;

	if (!DefaultHeight[d])
	{
		regimpaire = new RegExp("[3579]{1}$", "g");
	
		if (regimpaire.test(h))
			h = h + 1;
	
		DefaultHeight[d] = h;
	}

}

function Chgdim(height, div)
{
	
	div.style.height = height + 'px' ;
	
	
}


// Ouvre / referme un div progressivement
function testouille()
{
	latence = 1;

	var length = (div.style.height).length ;
	len = length - 2 ;
	sh = div.style.height.substr(0, len);

	sh = parseInt(sh);

	if (move == 'hide')
	{
		if (sh > 0)
		{
			if (sh == 1)
				sh = sh + 1;
				
			sh = (sh - 2);
			
			Chgdim(sh, div)										// Mise à jour des nouvelles dimensions
			timer = setTimeout('testouille()', latence );		// Récursivité, avec le timer
		}
		else
			clearTimeout(timer);
	}
	else if (move == 'show')
	{
		if (sh < DefaultHeight[div.id])
		{
			sh = (sh + 2);
			
			Chgdim(sh, div)										// Mise à jour des nouvelles dimensions
			timer = setTimeout('testouille()', latence );		// Récursivité, avec le timer
		}
		else
			clearTimeout(timer);
	}
}

// Redirige vers la page "page" au bout de "temps" secondes 
function redirect(page, temps) 
{
	setTimeout('window.location=page', temps) 
}




/*
 * Credit:
 *   If you're nice, you'll leave this bit:
 *  
 *   Class by Stickman -- http://www.the-stickman.com
 *      with thanks to:
 *      [for Safari fixes]
 *         Luis Torrefranca -- http://www.law.pitt.edu
 *         and
 *         Shawn Parker & John Pennypacker -- http://www.fuzzycoconut.com
 *      [for duplicate name bug]
 *         'neal'
 */
function MultiSelector( list_target, max, button ){

	// Where to write the list
	this.list_target = list_target;
	// How many elements?
	this.count = 0;
	// How many elements?
	this.id = 0;
	// Is there a maximum?
	if( max ){
		this.max = max;
	} else {
		this.max = -1;
	};
	
	/**
	 * Add a new  input element
	 */
	this.addElement = function( element ){

		// Make sure it's a file input element
		if( element.tagName == 'INPUT' && element.type == 'hidden' )
		{
			// Element name -- what number am I?
			element.name = 'newdlrlz_' + this.id++;

			// Add reference to this object
			element.multi_selector = this;
			
			// On attribue la bonne valeur au champ caché
			button.onclick = function(){ 
			
			element.value=document.getElementById('type_dl').value + '||' + document.getElementById('rlz_dl_nom').value  + '||' + document.getElementById('rlz_dl_url').value + '||' + document.getElementById('type_dl').options[document.getElementById('type_dl').selectedIndex].innerHTML;
			}
			
			// On rajoute le dl
			button.onblur = function(){
			
				// On prépare un nouveau champ masqué
				var new_element = document.createElement( 'input' );
				new_element.type = 'hidden';

				// Affichage du nouvel élément
				list_target.appendChild( new_element, element );
				
				// Apply 'update' to element
				element.multi_selector.addElement( new_element );

				// Update list
				element.multi_selector.addListRow( element );

				// Hide this: we can't use display:none because Safari doesn't like it
				//element.style.position = 'absolute';
				//element.style.left = '-1000px';
				
			};
			// If we've reached maximum number, disable input element
			if( this.max != -1 && this.count >= this.max ){
				element.disabled = true;
			};

			// File element counter
			this.count++;
			// Most recent element
			this.current_element = element;
			
			
		} else {
			// This can only be applied to file input elements!
			alert( 'Error: not a file input element' );
		};

	};

	/**
	 * Add a new row to the list of files
	 */
	this.addListRow = function( element ){

		// Row div
		var new_row = document.createElement( 'li' );

		// Delete button
		var new_row_button = document.createElement( 'input' );
		new_row_button.type = 'button';
		new_row_button.value = 'Supprimer';

		// References
		new_row.element = element;

		// Delete function
		new_row_button.onclick= function(){

			// Remove element from form
			this.parentNode.element.parentNode.removeChild( this.parentNode.element );

			// Remove this row from the list
			this.parentNode.parentNode.removeChild( this.parentNode );

			// Decrement counter
			this.parentNode.element.multi_selector.count--;

			// Re-enable input element (if it's disabled)
			this.parentNode.element.multi_selector.current_element.disabled = false;

			// Appease Safari
			//    without it Safari wants to reload the browser window
			//    which nixes your already queued uploads
			return false;
		};

		// Affichage du lien tout beau tout propre
		link = element.value.split('||');
		new_row.innerHTML = link[3] + '<br /><a href="' + link[2] + '" target="_blank">' + link[1] + '</a> - ';
		document.getElementById('com1').value = document.getElementById('com1').value + '&' + element.name + '=' + element.value;

		// Ajout d'un bouton
		new_row.appendChild( new_row_button );

		// On l'ajoute à la liste
		this.list_target.appendChild( new_row );
		
	};

};


// Fonction de creation de la tooltip
function tooltip( e )
{
	// recuperation du noeud racine du document HTML ( pour IE )
	if ( _rnode == '' )
		_rnode = ( !_d.documentElement.clientWidth ? _d.body : _d.documentElement );	
	
	// infos de position
	var sx = ( _isMozilla ? pageXOffset : _rnode.scrollLeft );
	var sy = ( _isMozilla ? pageYOffset : _rnode.scrollTop );
	var x = ( _isMozilla ? e.pageX : event.clientX + sx );
	var y  = ( _isMozilla ? e.pageY : event.clientY + sy );
	
	// Objet sous la souris
	el = ( _isMozilla ? e.target : event.srcElement );
	
	if (  !el.tagName )										// noeud #text	
		el = el.parentNode;
		
		if (el.tagName == 'IMG')							// Pour que ça marche aussi avec les images
			el = el.parentNode;
			
			
	if ( el.className == 'tooltip' && el.id )			// cas ou l'on est dans un lien permettant d'afficher une tooltip
	{
		
		// tooltip en cours
		var tooltip = _d.getElementById( el.id.substring( el.id.lastIndexOf('#') + 1 ) ).style;
		
		if ( tooltip != _otooltip )							// cas ou l'ancienne tooltip est pas la meme que la nouvelle
		{
			
			_otooltip.display = 'none';
			_otooltip = tooltip;
			// el.onclick = function() { return ( false ); }	// desactivation du clic sur le lien
		}
		
		// affichage de la tooltip
		with ( tooltip )
		{
			
			display = 'block';
			left = x + 'px';
			top = y + 20 + 'px';
		}
	}
	else
	{
		// fermeture automatique de la tooltip
		_otooltip.display = 'none';
		_otooltip = '';
	
	}
}

// Pour creation du tooltip a chaque mouvement de la souris.
_d.onmousemove = tooltip;



/**********************************
Copyright (c): Yves Dahan, 08/2003
e-mail: ygda.free.fr

Utilisation libre pour tout usage
sous réserve du maintien de la
présente mention de copyright.
***********************************/
function ScrollDiv(id){
if(this==window)
	return new ScrollDiv(id); //ScrollDiv() called as a Function
	
  var elt=document.getElementById(id);
  var w=elt.offsetWidth;
  var s=elt.parentNode.offsetWidth;
  
	if(!w || !s || s>w){
		this.scroll=Function(""); return;}
		
	var divStyle=elt.style;
	var offset=0;
	var maxOffset=s-w;
	var timer=null;
	var self=this;
 
	this.scroll= function(dir,speed)
	{
		var coef=1, S=speed;
		while(S--)coef*=2;
		if (timer)window.clearTimeout(timer);
		switch (dir)
		{
			case 's': //stop
				break;
			case 'l': //left
				if(offset>maxOffset)
				{
					offset-=coef;
					divStyle.left=Math.min(offset,0)+"px";
					timer=window.setTimeout(function(){self.scroll('l',speed)},25)
				}
				break;
			case 'r': //right
				if(0>offset)
				{
					offset+=coef;
					divStyle.left=Math.max(offset,maxOffset)+"px";
					timer=window.setTimeout(function(){self.scroll('r',speed)},25)
				}
		}
	}
}


function SwitchImg(img1, img2, ObjImg)
{
//	alert(document.getElementById(ObjImg).src);
//	alert(img1);
	
	if (document.getElementById(ObjImg).src.indexOf(img1) == -1) 
		document.getElementById(ObjImg).src = img1
	else
		document.getElementById(ObjImg).src = img2
}

function InscriptionQualite()
{
	if (document.forms.insc_form.qualite.selectedIndex == 1)
	{
	   setTimeout('document.getElementById(\'commercant_infos\').style.height = \'auto\'', 1);
	   setTimeout('new Effect.Appear(document.getElementById(\'commercant_infos\'),{duration: 0.5})', 100);
	   setTimeout('Nifty(\'div#commercant_infos\', \'big transparent\')', 200);	}
	else
	{
	   setTimeout('new Effect.Fade(document.getElementById(\'commercant_infos\'),{duration: 0.5})', 100);
	}
}



function FormInscValidate()
{
	
	document.forms.insc_form.submit();
	
	
}


// Ouvrir une popup
function Popup(url, titre, largeur, hauteur, scroll)
{
	
	window.open(url,titre,"status=0,height="+hauteur+",width="+largeur+",resizable=0,scrollbars="+scroll);
}
  


function GetElementsWithClassName(elementName,className) 
{
	var allElements = document.getElementsByTagName(elementName);
	var elemColl = new Array();
	for (var i = 0; i< allElements.length; i++) 
	{
		if (hasClass(allElements[i], className)) 
		{
			elemColl[elemColl.length] = allElements[i];
		}
	}
	
	return elemColl;
}



// Activer / désactiver une textbox en fonction d'un checkbox
function ActiveTxtBox(activer, txtbox)
{
	if (activer.checked==false)
	{
		txtbox.disabled="disabled";
	}
	else
	{
		txtbox.disabled="";
	}
}
	

function ActiveTxtBoxWithRadio(activer, tbxtoactive, tbxtounactive )
{
	// Si le bouton radio est coché
	if (activer.checked==true)
	{
		// On active tous les éléments à activer
		for (i=0; i < tbxtoactive.length; i++) 
		{
			tbxtoactive[i].disabled="";
		}

		// On désactive tous les éléments à désactiver
		for (i=0; i < tbxtounactive.length; i++) 
		{
			tbxtounactive[i].disabled="disabled";
		}

	}
	else
	{
		txtbox.disabled="";
	}
}
	
/* Calcul le total dans la page d'inscription */
function CalcTotal(form)
{
	IsInt = new RegExp("^[0-9]+", "g");
	f = form;
	var Total =  0;

	// Vérif du bail
	if (f.bail.value.length == 0)
	{

		return false;
	}
/*	if (IsInt.test(f.bail.value) == false)
	{
		alert('Le Bail doit être un nombre !');
		return false;
	}*/
	// On ajoute le bail
	Total = Total + parseInt(f.bail.value);
	
	// Vérif du loyer
	if (f.loyer.value.length == 0)
	{
		alert('Veuillez renseigner le loyer !');
		return false;
	}
	// Si promo, on ajoute le loyer promo
	if (f.loyer_promo.value.length > 0)
		Total = Total + (parseInt(f.loyer_promo.value)*3);
	// Sinon,
	else
		Total = Total + (parseInt(f.loyer.value)*3);
		
	// Les packs
	if ((f.Chk_blogshop[0].checked==true) && (f.Txt_blogshop.value.length >0))
		Total = Total + parseInt(f.Txt_blogshop.value);
	if ((f.Chk_blogshop[1].checked==true) && (f.Txt_blog.value.length >0))
		Total = Total + parseInt(f.Txt_blog.value);
/*	if (f.Chk_facade3d.checked==true && f.Txt_facade3d.value.length >0)
		Total = Total + parseInt(f.Txt_facade3d.value);
*/	if (f.Chk_animation.checked==true && f.Txt_animation.value.length >0)
		Total = Total + parseInt(f.Txt_animation.value);
	if (f.Chk_buzz.checked==true && f.Txt_buzz.value.length >0)
		Total = Total + parseInt(f.Txt_buzz.value);
/*	if (f.Chk_visibilite.checked==true && f.Txt_visibilite.value.length >0)
		Total = Total + parseInt(f.Txt_visibilite.value);
	if (f.Chk_design.checked==true && f.Txt_design.value.length >0)
		Total = Total + parseInt(f.Txt_design.value);
	if (f.Chk_multimedia.checked==true && f.Txt_multimedia.value.length >0)
		Total = Total + parseInt(f.Txt_multimedia.value);
*/
	f.total.value = Total;
	f.totalttc.value = FloatPrecision(parseFloat(Total*1.196), 2);

}

/* Calcul le total dans la page d'inscription */
function CalcTotal2(form)
{
	IsInt = new RegExp("^[0-9]+", "g");
	f = form;
	var Total =  0;


	// Vérif du bail
	if (f.bail.value.length == 0)
	{

		return false;
	}
	// On ajoute le bail
	Total = Total + parseInt(f.bail.value);
	
	// Vérif du loyer
	if (f.loyer.value.length == 0)
	{
		alert('Veuillez renseigner le loyer !');
		return false;
	}
	// Si promo, on ajoute le loyer promo
	if (f.loyer_promo.value.length > 0)
		Total = Total + (parseInt(f.loyer_promo.value)*3);
	// Sinon,
	else
		Total = Total + (parseInt(f.loyer.value)*3);
		
	// Les packs
	if ((f.Chk_blogshop[1].checked==true) && (f.Txt_blogshop.value.length >0))
		Total = Total + parseInt(f.Txt_blogshop.value);
	if ((f.Chk_blogshop[2].checked==true) && (f.Txt_blog.value.length >0))
		Total = Total + parseInt(f.Txt_blog.value);
	if (f.Chk_facade3d.checked==true && f.Txt_facade3d.value.length >0)
		Total = Total + parseInt(f.Txt_facade3d.value);
	if (f.Chk_animation.checked==true && f.Txt_animation.value.length >0)
		Total = Total + parseInt(f.Txt_animation.value);
	if (f.Chk_buzz.checked==true && f.Txt_buzz.value.length >0)
		Total = Total + parseInt(f.Txt_buzz.value);
/*	if (f.Chk_visibilite.checked==true && f.Txt_visibilite.value.length >0)
		Total = Total + parseInt(f.Txt_visibilite.value);
	if (f.Chk_design.checked==true && f.Txt_design.value.length >0)
		Total = Total + parseInt(f.Txt_design.value);
	if (f.Chk_multimedia.checked==true && f.Txt_multimedia.value.length >0)
		Total = Total + parseInt(f.Txt_multimedia.value);
*/
	f.total.value = Total;
	f.totalttc.value = FloatPrecision(parseFloat(Total*1.196), 2);

}


// Choisir la précision d'un flottant
function FloatPrecision (number, precision)
{
	nb = number.toString();
	// On calcule la position du point ( virgule )
	dotpos = nb.indexOf('.', 0);

// S'il y a bien une virgule
	if (dotpos > 0)
	{
		// Ce qu'on garde du nombre
		newnb = nb.substr(0, (dotpos+precision+1));
		
		return parseFloat(newnb);
	}
	// Sinon, on retourne le nombre qui est good
	return number;
}

