var $=function(){var a=[];for(var i=0,b;b=arguments[i];i++){if(typeof(b)=='string'){b=document.getElementById(b);}if(arguments.length===1){return b;}a.push(b);}return a;};

function checkForm(inputToCheck,alertme){

	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]);
		}
	}
	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 getURL(link){
	window.location.href = link;
}

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 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>";
	}
}

//
// 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]);
		if( tmpSearchValue ){
			tmpSearchValue = tmpSearchValue.value;
			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.style.display = "";
					count++;
				}
				
				if( tmpSearchValue!="" && !tabRegExp[s].test(o.innerHTML) ){
					tabTrToHide[tabTrToHide.length] = o.parentNode;
				}
			}
		}
	}
	// 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;
}
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];
}
