// Alias de document.getElementById()
function $(e){
	return document.getElementById(e);
}

// Crer un noeud de type lment HTML
function $ce(str){
	return document.createElement(str);
}

// Crer un noeud de type texte
function $ctn(str){
	return document.createTextNode(str);
}

// définir les propriétés d'un élément HTML
function createQuote(paramQuote, att){
	var quote = document.createElement(paramQuote);
	for( var i=0; i<att.length; i++){
		quote.setAttribute(att[i].name,att[i].value);
		
	}
	if( arguments.length > 2 ){
		quote.appendChild(document.createTextNode(arguments[2]));
	}
	return quote;
}

// Cration d'une balise simple (<BALISE>txt</BALISE>)
function $cse(str, txt){
	var elem = $ce(str);
	elem.appendChild($ctn(txt));
	return elem;
}
function alertSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  window.alert( 'Width = ' + myWidth );
  window.alert( 'Height = ' + myHeight );
}

function showLoader(){
	$('loader').style.position = 'absolute';
	$('loader').style.top = '0px';
	$('loader').style.height = document.body.scrollHeight;
	$("loader").style.display="block";
}

function hideLoader(){
	$("loader").style.display="none";
}



/** Affichage d'un écran d'avertissement contextuel
 * @param 	title		String		Titre de la fenêtre d'avertissement
 * @param	str			String		Contenu texte de la fenêtre
 * @param	[close]		Boolean		Affichage du bouton fermer
 * @param	[skin]		String		Style de la fenêtre
 *				- locked : 		Style verrou, pour avertir d'un accès restreint
 *				- valid : 		Message positif/validation
 *				- error : 		Message negatif/erreur
 *				- question : 	Poser une question
 *				- html : 		Contenu customisé
 *				- contact : 	Utilisé pour afficher les alertes contact
 *				- commentaire :	???
 * 				- load : 		Chargement en cours
**/
function showAlert(title,str){
	showLoader();
	$("alertBox").style.display="block";
	if( arguments.length >= 4 ){
		
		switch( arguments[3] ){
			case "locked" :
				$("alertBox").className='lockedBox';
				break;
			case "valid" :
				$("alertBox").className='validBox';
				break;
			case "error" :
				$("alertBox").className='errorBox';
				break;
			case "question" :
				$("alertBox").className='questionBox';
				break;
			case "html" :
				$("alertBox").className='htmlBox';
				break;
			case "contact" :
				$("alertBox").className='contactBox';
				
				break;
			case "commentaire" :
				$("alertBox").className='commentBox';
				break;
			case "facture" :
				$("alertBox").className='factureBox';
				break;
			case "adherents" :
				$("alertBox").className='adherentsBox';
				break;
			case "load" :
				$("alertBox").className='loadBox';
				if(IDSociete == 'CHR'){
					str = '<table><tr><td><img src="/img/reload_chr.png"/></td><td valign="middle">' +str+'</td></tr></table>';} else {
					str = '<table><tr><td><img src="/img/loading_animation.gif"/></td><td valign="middle">' +str+'</td></tr></table>';	
				}
				break;	
			default :
				$("alertBox").className='warningBox';
			break;
		}
		/****/
	}/****/
	if(title == ''){
		$("alertBox").innerHTML = str;
	} else {
		$("alertBox").innerHTML = "<h1>"+title+"</h1>" + str;
	}
	if(arguments.length > 2){
		//Pour rediriger vers une URL sur le clic du bouton
		if( arguments[2] )  {
			var loc = " onClick=\"hideAlert();\"";
			if( arguments.length == 5 ){
				var loc = " onClick=\"document.location.replace('" +arguments[4] +"');hideAlert();\"";
			}
			$("alertBox").innerHTML += "<hr /><p align=\"center\"><input id=\"alertBoutFermer\" type=\"button\" class=\"alertBout\" value=\"FERMER\" "+loc+"/></p>"; 
		} 
	}

	if(jck_navdetect=="ms"){
		$("alertBox").style.top = document.body.scrollTop + 200;
		$("loader").style.top = document.body.scrollTop;

		//pour ie avant 7, on cache les SELECT et les IFRAMES qui restent au 1er plan (pb de windowless)
		//Pour + d'infos : http://download.microsoft.com/download/f/e/a/fea64d9a-7658-4939-84e9-186b8d16d52a/IEPlansAffichage.doc
		//On identifie les select qui étaient affichés de manière à ne réafficher que ceux-là lorsqu'on cachera l'alerte
		//_$("select[display=block]").addClass("alertBoxSelectBlock");
	} else {
		_$("#alertBox").css('top',window.pageYOffset + 200);
		_$("#loader").css('top',window.pageYOffset);
	}

	//Focus sur le bouton fermer
	if($("alertBoutFermer")){
		$('alertBoutFermer').focus();
	}
}

function hideAlert(){
	$("loader").style.display="none";
	$("alertBox").style.display="none";

	if(jck_navdetect=="ms"){
		//pour ie avant 7, on cache les SELECT et les IFRAMES qui restent au 1er plan (pb de windowless)
		//Pour + d'infos : http://download.microsoft.com/download/f/e/a/fea64d9a-7658-4939-84e9-186b8d16d52a/IEPlansAffichage.doc
		//_$("select.alertBoxSelectBlock").removeClass("alertBoxSelectBlock");
	}
}

//On cache la fenêtre lorsqu'on appuie sur la touche Esc
document.onkeydown = function(e){
	if (e == null) { // ie
		keycode = event.keyCode;
	} else { // mozilla
		keycode = e.which;
	}
	if(keycode == 27){ // escape, close box
		hideAlert()
	}
};

function pop_upload(the_form,action,id) {
	//document.getElementById('fileUpload').value='';
	window.open("mail.html", "popup", "width=1000,height=1000");
	$('form_upload').target = "popup";
	$('form_upload').submit();
	setTimeout("hideAlertUpload();showAlert('Analyse de votre document Excel','Veuillez patienter...',false,'load');updateMercu('"+action+"',"+id+")",600);
}

/** Fonction similaire au showAlert, mais destinée à afficher un input type=file dans l'alerte
 * @param 	arguments[0]	String		(obligatoire) (title) Titre de la fenêtre d'avertissement
 * @param	arguments[1]	string		(obligatoire) (str) Message à afficher dans la fenêtre
 * @param	arguments[2]	string		(obligatoire) affichage ou non du bouton valider
 * @param	arguments[3]	string		(obligatoire) concaténation de l'action (del ou add) et de l'id de mercu
**/
function showAlertUpload(title,str){
	showLoader();
	$("alertBox").style.display="block";
	$("alertBox").className='validBox';
	str += "<br /><br /><form action='profils/all/uploadMercuUpdate.html' name='form_upload' id='form_upload' method='POST' enctype='multipart/form-data'>";
	str += "<input type='file' id='fileUpload' name='fileUpload' /></form>";
	if(title == ''){
		$("alertBox").innerHTML = str;
	} else {
		$("alertBox").innerHTML = "<h1>"+title+"</h1>" + str;
	}

	//On splitte le 3e parametre pour récupérer l'action et l'id de mercu
	var tabAction = arguments[3].split("µ");

	//Upload sur le clic du bouton (l'URL est passée en paramètre)
	var action = " onClick=\"pop_upload('form_upload','"+tabAction[0]+"',"+tabAction[1]+");\"";

	$("alertBox").innerHTML += "<hr />";
	$("alertBox").innerHTML += "<p align=\"center\">";
	$("alertBox").innerHTML += "<input id=\"alertBoutFermer\" type=\"button\" class=\"alertBout\" value=\"Valider\" "+action+"/>";
	$("alertBox").innerHTML += "</p>"; 
	$("alertBox").innerHTML += "</form>"; 

	if(jck_navdetect=="ms"){
		$("alertBox").style.top = document.body.scrollTop + 200;
		$("loader").style.top = document.body.scrollTop;
		var tabSelect = document.getElementsByTagName("select");
		//pour ie avant 7, on cache les SELECT et les IFRAMES qui restent au 1er plan (pb de windowless)
		//Pour + d'infos : http://download.microsoft.com/download/f/e/a/fea64d9a-7658-4939-84e9-186b8d16d52a/IEPlansAffichage.doc
		for (var indexSelect = 0;indexSelect<tabSelect.length;indexSelect++){
			tabSelect[indexSelect].style.display="none";
		}
	} else {
		$("alertBox").style.top = window.pageYOffset + 200;
		$("loader").style.top = window.pageYOffset;
	}

	//Focus sur le bouton fermer
	if($("alertBoutFermer")){
		$('alertBoutFermer').focus();
	}
}

function hideAlertUpload(){
	$("loader").style.display="none";
	$("alertBox").style.display="none";
	
	if(jck_navdetect=="ms"){
		var tabSelect = document.getElementsByTagName("select");
		
		//pour ie avant 7, on cache les SELECT et les IFRAMES qui restent au 1er plan (pb de windowless)
		//Pour + d'infos : http://download.microsoft.com/download/f/e/a/fea64d9a-7658-4939-84e9-186b8d16d52a/IEPlansAffichage.doc
		for (var indexSelect = 0;indexSelect<tabSelect.length;indexSelect++){
			tabSelect[indexSelect].style.display="block";
		}
	}
}

function stripDate(nomFichier){
	//Pour retirer la date du nom des fichiers télécharger lors de l'affichage
	var extension = nomFichier.substring(nomFichier.lastIndexOf("."));
	var dbt = parseInt(nomFichier.length-(20+extension.length));
	var fin = parseInt(nomFichier.length-(extension.length));
	var nomFichierSansExtension =  nomFichier.substring(dbt,fin);
	//Seulement si le nom contient la date
	var monExpression = /^_\d{2}-\d{2}-\d{4}_\d{2}:\d{2}:\d{2}$/;

	if(nomFichierSansExtension.match(monExpression)!=null){
		var chemin = nomFichier.substr(0,nomFichier.length-(20+extension.length));
		return chemin+extension;
	}else{
		return nomFichier;
	}
}

function afficheSousMenu(menu){
	if(jck_navdetect == "ns"){
		if($("sub"+menu).style.display == "block"){
			$("sub"+menu).setAttribute("style","display:none;");
		}else{
			$("sub"+menu).setAttribute("style","display:block;");
		}
	}else{
		if($("sub"+menu).style.display == "block"){
			$("sub"+menu).style.setAttribute("cssText","display:none;");
		}else{
			$("sub"+menu).style.setAttribute("cssText","display:block;");
		}
	}
}

function uncheckBoxes(nomDiv){
	//Fonction qui décoche les cases à cocher dans une div précise
	var tabChk = $(nomDiv).getElementsByTagName("input");
	var j = 0;
	for(var i=0;i<tabChk.length;i++){
		if(tabChk[i].type == "checkbox"){
			tabChk[i].checked = false;
		}
	}
}

if(typeof(window.console) != 'undefined')
{
	logDebug = function(content, level)
  {
    if (level === undefined)
    {
      level = 'warn';
    }
    switch (level)
    {
      case 'info': console.info(content); break;                               
      case 'warn': console.warn(content); break;                               
      case 'error': console.error(content); break;                     
      case 'log': default: console.log(content); break;
    }
    return true;
  };
}
else

{
	logDebug = function()
  {
   //alert('Unable to log in console because Firebug is disable.');
  }
}


function testColspan(){
	
}



//Fonction qui remplace les accent par des caractères non accentués et met tout en bas de casse
//Utilisé notamment pour la comparaison de chaînes (activité) lors de l'édition d'une fiche adhérent
function chaineComparable(chaine){
	badChars = new Array("é","è","ê","ë","à","â","ä","î","ï","ô","ö","ù");
	goodChars = new Array("e","e","e","e","a","a","a","i","i","o","o","u");
	for(var i=0,regex; i<badChars.length; i++) {
		regex = new RegExp(badChars[i],"g");
		chaine = chaine.replace(regex,goodChars[i]);
	}
	chaine = chaine.toLowerCase();
	return chaine;
}