function changer_href(class_element, new_href){ 
	var elements = document.getElementsByTagName("*");
	for (var i=0;(element=elements[i]);i++){
		if (element.className==class_element){
			element.href = element.href+new_href;
		}
	}
}
function modifier_form(id_form, name_element, value_element){
	document.getElementById(id_form).temp.name=name_element;
	document.getElementById(id_form).temp.value=value_element;
	document.getElementById(id_form).submit();
}
function ouvrir_popup(url,nom,width,height) {
	var x = ((screen.width - width)/2)-15;
	var y = ((screen.height - height)/2)-20;
	winprops = "height="+height+",width="+width+",top="+y+",left="+x+",scrollbars=yes, resizable=yes";
	win = window.open(url, nom, winprops);
}
function autotab(current,to){
    if (current.getAttribute && 
      current.value.length==current.getAttribute("maxlength")) {
        to.focus() 
        }
}
function switch_display(id, valeur){
	if(valeur==0) document.getElementById(id).style.display="block";
	else document.getElementById(id).style.display="none";
}
function switch_checkboxes(name) {
	var inputs	= document.getElementsByTagName('input');
	var count	= inputs.length;
	for (i = 0; i<count; i++){
		_input = inputs.item(i);
		if (_input.type == 'checkbox' && _input.id.indexOf('chx-' + name) != -1)_input.checked = document.getElementById(name + '-tout').checked;
	}
}

function verifier(id, valeur, type, obligatoire){
	var mail = new RegExp('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$', 'i');
	var erreur = false;
	switch(type){
		case "double":
			if (isNaN(parseFloat(valeur)) && valeur.length!=0) erreur=true;
			else erreur=false;
		break;
		case "int":
			if (isNaN(parseInt(valeur))) erreur=true;
			else erreur=false;
		break;
		case "mail":
			if (!mail.test(valeur)) erreur=true;
			else erreur=false;
		break;
		case "day":
			if (isNaN(parseInt(valeur)) || valeur.length!=2) erreur=true;
			else erreur=false;
		break;
		case "month":
			if (isNaN(parseInt(valeur)) || valeur.length!=2) erreur=true;
			else erreur=false;
		break;
		case "year":
			if (isNaN(parseInt(valeur)) || valeur.length!=4) erreur=true;
			else erreur=false;
		break;
	}
	if (erreur) document.getElementById(id).style.borderColor="red";
	else{
		if (obligatoire && valeur.length==0) document.getElementById(id).style.borderColor="red";
		else document.getElementById(id).style.borderColor="";
	}
	if (!obligatoire && valeur.length==0) document.getElementById(id).style.borderColor="";
}

function deplacer(de, a, supprimer) {
	de=document.getElementById(de);
	a=document.getElementById(a);
	var selection = new Array();
	for (var i=0; i<a.options.length; i++)
	selection[a[i].value] = true;
	for(var i=0; i<de.options.length; i++){
		if(de.options[i].selected){ 
			if (!selection[de[i].value]) a.options[a.options.length]=new Option(de.options[i].text, de.options[i].value);
			if(supprimer) de.options[i]=null;
		}	
	}	
}


// returns true if the string is empty
function isEmpty(str){
	return (str == null) || (str.length == 0);
}
// returns true if the string is a valid email
function isEmail(str){
	if(isEmpty(str)) return false;
	var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
	return re.test(str);
}
// returns true if the string only contains characters A-Z or a-z
function isAlpha(str){
	var re = /[^a-zA-Z]/g
	if (re.test(str)) return false;
	return true;
}
// returns true if the string only contains characters 0-9
function isNumeric(str){
	var re = /[\D]/g
	if (re.test(str)) return false;
	return true;
}
// returns true if the string only contains characters A-Z, a-z or 0-9
function isAlphaNumeric(str){
	var re = /[^a-zA-Z0-9]/g
	if (re.test(str)) return false;
	return true;
}
// returns true if the string's length equals "len"
function isLength(str, len){
	return str.length == len;
}
// returns true if the string's length is between "min" and "max"
function isLengthBetween(str, min, max){
	return (str.length >= min)&&(str.length <= max);
}
// returns true if the string is a valid date formatted as...
// mm dd yyyy, mm/dd/yyyy, mm.dd.yyyy, mm-dd-yyyy
function isDate(str){
	var re = /^(\d{1,2})[\s\.\/-](\d{1,2})[\s\.\/-](\d{4})$/
	if (!re.test(str)) return false;
	var result = str.match(re);
	var m = parseInt(result[1]);
	var d = parseInt(result[2]);
	var y = parseInt(result[3]);
	if(m < 1 || m > 12 || y < 1900 || y > 2100) return false;
	if(m == 2){
		var days = ((y % 4) == 0) ? 29 : 28;
	}else if(m == 4 || m == 6 || m == 9 || m == 11){
		var days = 30;
	}else{
		var days = 31;
	}
	return (d >= 1 && d <= days);
}
// returns true if "str1" is the same as the "str2"
function isMatch(str1, str2){
	return str1 == str2;
}
// returns true if the string contains only whitespace
// cannot check a password type input for whitespace
function isWhitespace(str){ // NOT USED IN FORM VALIDATION
	var re = /[\S]/g
	if (re.test(str)) return false;
	return true;
}
// removes any whitespace from the string and returns the result
// the value of "replacement" will be used to replace the whitespace (optional)
function stripWhitespace(str, replacement){// NOT USED IN FORM VALIDATION
	if (replacement == null) replacement = '';
	var result = str;
	var re = /\s/g
	if(str.search(re) != -1){
		result = str.replace(re, replacement);
	}
	return result;
}
// validate the form
function validateForm(f){
	var errors = '';
	var i,e,t,n,v;
	for(i=0; i < f.elements.length; i++){
		e = f.elements[i];
		if(e.optional) continue;
		t = e.type;
		n = e.name;
		v = e.value;
		if(t == 'text' || t == 'password' || t == 'textarea'){
			if(isEmpty(v)){
				errors += n+' ne peux pas être vide.\n'; continue;
			}
			if(v == e.defaultValue){
				errors += n+' ne peux pas utiliser la valeur par défaut.\n'; continue;
			}
			if(e.isAlpha){
				if(!isAlpha(v)){
					errors += n+' peux seulement contenir les caractères A-Z a-z.\n'; continue;
				}
			}
			if(e.isNumeric){
				if(!isNumeric(v)){
					errors += n+' peux seulement contenir les caractères 0-9.\n'; continue;
				}
			}
			if(e.isAlphaNumeric){
				if(!isAlphaNumeric(v)){
					errors += n+' peux seulement contenir les caractères A-Z a-z 0-9.\n'; continue;
				}
			}
			if(e.isEmail){
				if(!isEmail(v)){
					errors += v+' n\'est pas une adresse valide.\n'; continue;
				}
			}
			if(e.isLength != null){
				var len = e.isLength;
				if(!isLength(v,len)){
					errors += n+' doit seulement contenir '+len+' caractères.\n'; continue;
				}
			}
			if(e.isLengthBetween != null){
				var min = e.isLengthBetween[0];
				var max = e.isLengthBetween[1];
				if(!isLengthBetween(v,min,max)){
					errors += n+' doit contenir entre '+min+' et '+max+' caractères.\n'; continue;
				}
			}
			if(e.isDate){
				if(!isDate(v)){
					errors += v+' n\'est pas une date valide.\n'; continue;
				}
			}
			if(e.isMatch != null){
				if(!isMatch(v, e.isMatch)){
					errors += 'Les deux mots de passe sont différents.\n'; continue;
				}
			}
		}
		if(t.indexOf('select') != -1){
			if(isEmpty(e.options[e.selectedIndex].value)){
				errors += n+' nécessite une option sélectionnée.\n'; continue;
			}
		}
		if(t == 'file'){
			if(isEmpty(v)){
				errors += n+' nécessite un fichier.\n'; continue;
			}
		}
	}
	if(errors != '') alert(errors);
	return errors == '';
}

function getElementsByClassName(strClass, strTag, objContElm) {
	strTag = strTag || "*";
	objContElm = objContElm || document;
	var objColl = (strTag == '*' && document.all) ? document.all : objContElm.getElementsByTagName(strTag);
	var arr = new Array();
	var delim = strClass.indexOf('|') != -1  ? '|' : ' ';
	var arrClass = strClass.split(delim);
	for (i = 0, j = objColl.length; i < j; i++) {
		var arrObjClass = objColl[i].className.split(' ');
		if (delim == ' ' && arrClass.length > arrObjClass.length) continue;
		var c = 0;
		comparisonLoop:
		for (k = 0, l = arrObjClass.length; k < l; k++) {
			for (m = 0, n = arrClass.length; m < n; m++) {
				if (arrClass[m] == arrObjClass[k]) c++;
				if (( delim == '|' && c == 1) || (delim == ' ' && c == arrClass.length)) {
					arr.push(objColl[i]);
					break comparisonLoop;
				}
			}
		}
	}
	return arr;
}
Array.prototype.push = function(value) {
  this[this.length] = value;
}

var nbClones=0;
function cloner(conteneur, original){
	var original = document.getElementById(original);
	var clone = original.cloneNode(true);
	clone.id = original.id+nbClones;
	clone.style.display="block;"
	// clone.getElementsByTagName("legend")[0].innerHTML = "Point de vente #"+(nbClones+2);
	document.getElementById(conteneur).appendChild(clone);
	nbClones++;
}

function supprimerDernierGamin(conteneur, tag) {
	var conteneur = document.getElementById(conteneur);
	var gamins = conteneur.getElementsByTagName(tag);
	var nbGamins = gamins.length;
	var dernierGamin = gamins[nbGamins-1];
	if (dernierGamin.id!="original-pdv"){
		conteneur.removeChild(dernierGamin);
		nbClones--;
	}
}

//Ajouté par Gus
function test_email(objet,texte)
{
  with (objet)
  {
    apos=value.indexOf("@");
    dotpos=value.lastIndexOf(".");
    lastpos=value.length-1;
    if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2)
    {
      if (texte) {alert(texte);}
      objet.select();
      objet.focus();
      return false;
    }
    else
    {
      return true;
    }
  }
}

function test_vide(objet,texte)
{
  with (objet)
  {
    if (value==null || value=='')
    {
      if (texte!="") {alert(texte);}
      objet.select();
      objet.focus();
      return false;
    }
    else { return true; }
  }
}

function validation(objet)
{
  with (objet)
  {
    if (test_email(email,'Votre adresse email est vide ou non valide !')==false) { email.select(); email.focus(); return false; }
    if (test_vide(commentaires,'Il manque votre commentaires !')==false) { commentaires.select(); commentaires.focus(); return false; }
  }
}
//End - Ajouté par Gus

function verifier_pdvs(id, i){
	if(id.checked) document.getElementById('pdv-'+(i+1)).style.display='block';
	else if(i<8){
		if(!document.getElementById('next-pdv-'+(i+1)).checked) document.getElementById('pdv-'+(i+1)).style.display='none';
	}
	if(i<8){
		if (document.getElementById('next-pdv-'+(i+1)).checked) id.checked='checked';
	}
}

function creerFlash(url, width, height, options){
	div = options.div || '';
	if(div!='') var remplacement = document.getElementById(div).innerHTML;
	else var remplacement = '';

	var flash = '<object type="application/x-shockwave-flash" data="'+url+'" width="'+width+'" height="'+height+'">';
	flash += '<param name="movie" value="'+url+'"/>';
	if(options.play) flash += '<param name="play" value="'+options.play+'"/>';
	if(options.loop) flash += '<param name="loop" value="'+options.loop+'"/>';
	if(options.quality) flash += '<param name="quality" value="'+options.quality+'"/>';
	if(options.bgcolor) flash += '<param name="bgcolor" value="'+options.bgcolor+'"/>';
	if(options.scalemode) flash += '<param name="scalemode" value="'+options.scalemode+'"/>';
	if(options.align) flash += '<param name="align" value="'+options.align+'"/>';
	if(options.salign) flash += '<param name="salign" value="'+options.salign+'"/>';
	if(options.menu) flash += '<param name="menu" value="'+options.menu+'"/>';
	if(options.allowscriptaccess) flash += '<param name="allowscriptaccess" value="'+options.allowscriptaccess+'"/>';
	if(options.flashvars) flash += '<param name="flashvars" value="'+options.flashvars+'"/>';
	if(options.wmode) flash += '<param name="wmode" value="'+options.wmode+'"/>';
	flash += remplacement+'</object>';
	
	if(div=='') document.write(flash);
	else document.getElementById(div).innerHTML = flash;
}

function switcher_div(elm, div, liste){
	var conteneur=elm.parentNode.parentNode;
	h2=conteneur.getElementsByTagName('h2');
	for(var i=0; i<h2.length; i++){
		if(h2[i]!=elm.parentNode) h2[i].className='ferme';
		else h2[i].className='';
	}
	for(var i=0; i<liste.length; i++){
		if(liste[i]==div) document.getElementById(liste[i]).style.display='block';
		else document.getElementById(liste[i]).style.display='none';
	}
}