
function setMaxLength() {
	var x = document.getElementsByTagName('textarea');
	var counter = document.createElement('div');
	counter.className = 'counter';
	for (var i=0;i<x.length;i++) {
		var name = x[i].getAttribute('name');
		if (maxlength = document.getElementById(name+'_max').value) {
			var counterClone = counter.cloneNode(true);
			counterClone.relatedElement = x[i];
			counterClone.innerHTML = '<span>0</span>/'+maxlength;
			x[i].parentNode.insertBefore(counterClone,x[i].nextSibling);
			x[i].relatedElement = counterClone.getElementsByTagName('span')[0];

			x[i].onkeyup = x[i].onchange = checkMaxLength;
			x[i].onkeyup();
		}
	}
}

function checkMaxLength() {
	var maxLength = document.getElementById(this.name+'_max').value;
	var currentLength = this.value.length;
	if (currentLength > maxLength){
		this.relatedElement.className = 'toomuch';
		this.value = this.value.substring(0,maxLength);
		currentLength = maxLength;
		this.scrollTop = this.scrollHeight;
	}
	else
		this.relatedElement.className = '';
	this.relatedElement.firstChild.nodeValue = currentLength;
	// not innerHTML
}

//LIGHTBOX
function lightbox(img,title){
	flashLightbox(img,title);
}

//trims blank spaces
function trim(val)
{
	if (!val) return val;

		while(val.charAt(0)==' ') val=val.substring(1,val.length);
		while(val.charAt(val.length-1)==' ')  val=val.substring(0,val.length-1);
		
	 return val;
}

// Put first character to uppercase
function ucfirst(val){
	var firstC = val.substring(0,1);
	if(isNaN(firstC)){
		firstC = firstC.toUpperCase();
		val = firstC+val.substr(1);
	}
	
	return val;
}

// Find checked radio and checkbox value
function getcheckvalue(obj){
	nbObj				=		obj.length;
	
	// List of object
	listObj				=		true;
	if(nbObj)
	typeObj				=		obj[0].type
	else{
		typeObj			=		obj.type
		listObj			=		false;
	}
	
	if(typeObj=='radio' || typeObj=='checkbox'){
		
		if(typeObj=='radio')
		objValue		=		null;
		else
		objValue		=		new Array();
		
		// Search checked value
		if(listObj){
			for(x=0;x<nbObj;x++){
				if(obj[x].checked){
					val		=		obj[x].value;
					
					if(typeObj=='radio'){
						objValue=val;
						break;
					}
					else
					objValue.push(val);
				}
			}
		}
		else if(obj.checked){
			val		=		obj.value;
			
			if(typeObj=='radio')
			objValue=val;
			else
			objValue.push(val);
		}
		
		if(typeObj=='checkbox' && !objValue.length)
		return null;
		else
		return objValue;
	}
	else
	return null;
}

function validationReceiveEmail(radio,name,Submit){
	if(radio.value){
		radioValue				=		radio.value;
		validerRadio(radio.name,Submit,true);
	}
	else{
		radioValue				=		getcheckvalue(radio);
		validerRadio(radio,Submit,true);
	}
	
	email_oblig				=		false;
	if(radioValue==1)
	email_oblig				=		true;
	validerRegEx(name,Submit,email_oblig);
}

function bug(champ){
	if(nobug)
	eval('leForm.'+champ+'.focus()')
	
	nobug=false
}

//Pop div
function gelleriePhoto(id,titre){
	largeur=520;
	hauteur=500;
	
	largeurF=500;
	hauteurF=500;
	
	leleft=(screen.width-largeur)/2;
	letop=200;
	
	paramSWF = path2+'flash/galleries.swf?path='+path+'&amp;gal_id='+id;
	
	gallerie='<div align="center">';
	gallerie+='<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+largeurF+'px" height="'+hauteurF+'px" id="gallerie">';
		gallerie+='<param name="allowScriptAccess" value="sameDomain" />';
		gallerie+='<param name="movie" value="'+paramSWF+'" />';
		gallerie+='<param name="quality" value="high" />';
		gallerie+='<param name="bgcolor" value="#FFFFFF" />';
		gallerie+='<param name="menu" value="false" />';
		gallerie+='<param name="wmode" value="transparent" />';
		gallerie+='<embed menu="false" src="'+paramSWF+'" quality="high" bgcolor="#FFFFFF" width="'+largeurF+'px" height="'+hauteurF+'px" name="gallerie"  allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" wmode="transparent" />';
	gallerie+='</object>';
	gallerie+='</div>';
	
	var ajaxwin=dhtmlwindow.open('gallerie_'+id, 'inline', gallerie, ''+titre+'', 'width='+largeur+'px,height='+hauteur+'px,left='+leleft+'px,top='+letop+'px,resize=0,scrolling=1'); return false;
}
