function checkForm(inputToCheck){
	var tmp;
	var ok = true;
	var specialCheckOK;
	
	for(var i=0; i<inputToCheck.length; i++){
		specialCheckOK = true;
		tmp = document.getElementById(inputToCheck[i]);
		if(tmp){
			
			// CP
			if( inputToCheck[i]=="cp" && ( parseInt(tmp.value)!=tmp.value || tmp.value.length!=4 ) ){
				specialCheckOK = false;
			}
			// Email
			if( inputToCheck[i]=="email" && !is_valid_email(tmp.value) ){
				specialCheckOK = false;
			}
			
			// Phone/fax
			/*
			if( inputToCheck[i]=="phone" || inputToCheck[i]=="telephone" || inputToCheck[i]=="fax" ){
				if( tmp.value!="" && !is_valid_phone(tmp.value) ){
					ok = false;
				}
			}
			*/
			
			// REQUIRED
			if(tmp.value==null || tmp.value=="" || tmp.value==" " || !specialCheckOK ){
				ok = false;
				tmp.className = 'inp inpRed';
			}else{
				tmp.className = 'inp';
			}
		}else{
			alert("Id manquant: "+inputToCheck[i]);
		}
	}
	return ok;
}
function checkForm2(form, alertme){
	var ok = true;
	for(var i in form){
		var tmp = form[i];
		if( tmp && tmp.getAttribute && tmp.getAttribute("req")==1 ){
			if(tmp.value==null || tmp.value=="" || tmp.value==" "){
				ok = false;
				tmp.className = 'inp inpRed';
			}else{
				tmp.className = 'inp';
			}
		}
	}
	if( !ok && alertme==1){
		alert("Les champs marqués d'une * sont obligatoires!");
	}
	return ok;
}

function is_valid_email(email){
	return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email);
}
function is_valid_phone(phone){
	return /^ ([0-9_\.\-\_\/\+]{9,25})$/.test(phone);
}



function addToFavorites(anchor){
	if (document.all) {
        window.external.AddFavorite(anchor.href,anchor.title);
    }
    else if (window.sidebar) {
        window.sidebar.addPanel(anchor.title, anchor.href, "");
    }
    else {
        alert("Désolé! Votre navigateur ne supporte pas cette fonction.");
    }
return false;
}

function getURL(link){
	window.location.href = link;
}

function loadFlashes(){
	for(var i=0; i<flashToLoad.length; i++){
		loadFlash(flashToLoad[i][0], flashToLoad[i][1], flashToLoad[i][2], flashToLoad[i][3], flashToLoad[i][4]);
	}
}

function loadFlash(id_div, url_flash, width, height, transparent){
	var div = document.getElementById(id_div);
	if(div){
	
		var wmode = (transparent==1) ? "<param name='wmode' value='transparent'>" : "";
		
		div.innerHTML = 
			"<object id='"+id_div+"_swf' type='application/x-shockwave-flash' data='"+url_flash+"' width='"+width+"' height='"+height+"'> "+
				"<param name='movie' value='"+url_flash+"'> "+
				"<param name='allowScriptAccess' value='always'>  "+
				"<param name='allowFullScreen' value='true'> "+wmode+
			"</object>"; 
	}
}
var flashToLoad = new Array();

//
// Dimension_detect
function dimension_detect(){
	var d={
		'viewW':0, //viewPort Width
		'viewH':0, //viewPort Height
		'docH':0,  //document Height
		'docW':0,  //document Width
		'left':0,  //content Left Position according to the document flow
		'top':0    //content top position according to the document flow
	};
	if(document.body.scrollHeight>document.body.offsetHeight){
		d.docW=document.body.scrollWidth;
		d.docH=document.body.scrollHeight;
	}else{
		d.docW=document.body.offsetWidth;
		d.docH=document.body.offsetHeight;
	}
	if(self.innerWidth){
		d.viewW=self.innerWidth;
		d.viewH=self.innerHeight;
		d.left=window.pageXOffset;
		d.top=window.pageYOffset;
	}else{
		var ie=(document.compatMode&&document.compatMode!='BackCompat')?document.documentElement:document.body;
		d.viewW=ie.clientWidth;
		d.viewH=ie.clientHeight;
		d.left=ie.scrollLeft;
		d.top=ie.scrollTop;
	}
	return d;
};


// Gestion de filtre pour recherche
function initFiltre(){
	for( var s=0; s<tabFiltre.length; s++){
		document.getElementById("search_"+tabFiltre[s]).value = "";
	}
	filtre();
	return false;
}
function filtre(){
	
	var tabTrToHide = new Array();
	var tabRegExp = new Array();
	var count = 0;
	
	// Pour chaque 'search field'
	for( var s=0; s<tabFiltre.length; s++){ 
		
		// Regexp
		//var tmpSearchValue = document.getElementById("search_"+tabFiltre[s]);
		var tmpSearchValue = $("#search_"+tabFiltre[s]).val();
		
		if( tmpSearchValue ){
			tabRegExp[s] = new RegExp(tmpSearchValue, "i");
			
			// Values
			var values = document.getElementsByName("values_"+tabFiltre[s]); 
			for(var i=0, o; o=values[i]; i++){
			
				if(s==0){
					o.parentNode.parentNode.style.display = "";
					count++;
				}
				
				var valueToCheck;
				var parentToShow;
				
				if(o=="[object HTMLTableCellElement]"){
					valueToCheck = o.innerHTML;
					parentToShow = o.parentNode;
				}else if(o=="[object HTMLInputElement]"){
					valueToCheck = o.value;
					parentToShow = o.parentNode.parentNode;
				}
				
				if( tmpSearchValue!="" && !tabRegExp[s].test(valueToCheck) ){
					tabTrToHide[tabTrToHide.length] = parentToShow;
				}
			}
		}
	}
	// On cache ceux qui faut
	for(var h=0; h<tabTrToHide.length; h++){
		count--;
		tabTrToHide[h].style.display = "none";
	}
	
	// On affiche le nombre de résulats
	var nbResult = document.getElementById("nbResult");
	if( nbResult ){
		nbResult.innerHTML = count+" résultat(s)";
	}
	
	return false;
}

//
// fullinSelect: garnit un select en fonction dune reference dun autre select
function fullinSelect(select_ref_id, select_dest_id, tab_value){
	
	var id_ref = document.getElementById(select_ref_id).value;
	var select_dest = document.getElementById(select_dest_id);
	var default_value;
	
	if( this["default_"+select_dest_id] ){
		default_value = this["default_"+select_dest_id];
	}
	
	// On clean le select
	for(var i=select_dest.length-1; i>=0; i--){
		select_dest.options[i] = null;
	}
	
	// On disabled le select si la valeur de ref est nulle
	if(id_ref==0){
		select_dest.disabled = "disabled";
		return;
	}
	
	//
	// tab_value[0] = id du reccord du select_dest
	// tab_value[1] = id de FK correspondant au select_ref
	// tab_value[2] = libellé du select_dest
	if( !tab_value ){
		return;
	}
	
	var ctr = 0;
	var selectedIndex = 0;
	for(var z=0; z<tab_value.length; z++){
		if(tab_value[z][1]==id_ref){
			//var selected = () ? true : false;
			if(default_value && default_value===tab_value[z][0]){
				//alert(default_value+" <-> "+tab_value[z][0]);
				selectedIndex = ctr;
			}
			select_dest.options[ctr++] = new Option(tab_value[z][2],tab_value[z][0]);//, selected);
		}
	}
	select_dest.selectedIndex = selectedIndex;
	select_dest.disabled = "";
}

function in_array(needle, haystack){
    var key = '';
	for(key in haystack){
		if (haystack[key] == needle) {
			return true;
		}
	} 
    return false;
}

function trim(str, charlist) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: mdsjack (http://www.mdsjack.bo.it)
    // +   improved by: Alexander Ermolaev (http://snippets.dzone.com/user/AlexanderErmolaev)
    // +      input by: Erkekjetter
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: DxGx
    // +   improved by: Steven Levithan (http://blog.stevenlevithan.com)
    // +    tweaked by: Jack
    // +   bugfixed by: Onno Marsman
    // *     example 1: trim('    Kevin van Zonneveld    ');
    // *     returns 1: 'Kevin van Zonneveld'
    // *     example 2: trim('Hello World', 'Hdle');
    // *     returns 2: 'o Wor'
    // *     example 3: trim(16, 1);
    // *     returns 3: 6
 
    var whitespace, l = 0, i = 0;
    str += '';
    
    if (!charlist) {
        // default list
        whitespace = " \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000";
    } else {
        // preg_quote custom list
        charlist += '';
        whitespace = charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '$1');
    }
    
    l = str.length;
    for (i = 0; i < l; i++) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(i);
            break;
        }
    }
    
    l = str.length;
    for (i = l - 1; i >= 0; i--) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(0, i + 1);
            break;
        }
    }
    
    return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}

function str_replace(search, replace, subject, count) {
    // Replaces all occurrences of search in haystack with replace  
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'
    // *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
    // *     returns 2: 'hemmo, mars'
    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }

    for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}
        }
    }
    return sa ? s : s[0];
}

//
// checkOrtho
function checkOrtho(titre){
	var tabAllow = new Array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '-','0','1','2','3','4','5','6','7','8','9');
	var tabAcc = new Array('é', 'è', 'ê', 'ë', 'à', 'ù', 'ç', '&');
	var tabNonAcc = new Array('e', 'e', 'e', 'e', 'a', 'u', 'c', 'and');
	
	// Minuscule - Spaces
	titre = titre.toLowerCase();
	titre = str_replace(' ', '-', titre);
	titre = str_replace('_', '-', titre);
	titre = str_replace('/', '-', titre);
	titre = str_replace(tabAcc, tabNonAcc, titre);
	
	// Allowed letters
	var newTitre = "";
	for(var i=0; i<titre.length; i++){
		var letter = titre.charAt(i);
		if( i>0 && letter=='-' && titre.charAt(i-1)=='-'){
			// pas plus de un tiret a la fois
		}else{
			if( in_array( letter, tabAllow) ){
			
				if( newTitre=="" && letter=="-"){
					// pas de tiret en premiere pos
				}else{
					newTitre += letter;
				}
			}
		}
	}
	return newTitre;
}