/* prototype de fonction Array.in_array(p_val) */
Array.prototype.in_array = function(p_val) {
	for(var i = 0, l = this.length; i < l; i++) {
		if(this[i] == p_val) {
			return i+1; // retourne l'emplacement dans le tableau +1 pour eviter le 0.
		}
	}
	return false;
}
	
Number.prototype.nombreFormate = function (decimales, signe, separateurMilliers) {
	var _sNombre = String(this), i, _sRetour = "", _sDecimales = "";
	if (decimales == undefined) decimales = 2;
	if (signe == undefined) signe = '.'; 
	if (separateurMilliers == undefined) separateurMilliers = ' ';

	function separeMilliers (sNombre) {
		var sRetour = "";
		while (sNombre.length % 3 != 0) {
			sNombre = "0"+sNombre;
		}
		for (i = 0; i < sNombre.length; i += 3) {
			if (i == sNombre.length-1) separateurMilliers = '';
			sRetour += sNombre.substr(i, 3)+separateurMilliers;
		}
		while (sRetour.substr(0, 1) == "0") {
			sRetour = sRetour.substr(1);
		}
		return sRetour.substr(0, sRetour.lastIndexOf(separateurMilliers));
	}
	if (this == "0" ){
		return "0";
	} else {

		if (_sNombre.indexOf('.') == -1) {
			for (i = 0; i < decimales; i++) {
				_sDecimales += "0";
			}
			_sRetour = separeMilliers(_sNombre);
		} else {
			var sDecimalesTmp = (_sNombre.substr(_sNombre.indexOf('.')+1));
			if (sDecimalesTmp.length > decimales) {
				var nDecimalesManquantes = sDecimalesTmp.length - decimales;
				var nDiv = 1;
				for (i = 0; i < nDecimalesManquantes; i++) {
					nDiv = 10;
				}
				_sDecimales = Math.round(Number(sDecimalesTmp) / nDiv);
			}
			_sRetour = separeMilliers(_sNombre.substr(0, _sNombre.indexOf('.')))+String(signe)+_sDecimales;
		}
		return _sRetour;
	}
}
    
function overlay(nomdiv){
	my_div = document.getElementById("overlay");
	//document.getElementById("debug").innerHTML += show_props( document.getElementById("contenu"),null );
	my_div.style.height = document.getElementsByTagName('body')[0].clientHeight - 10 + "px"; //- 270
	my_div.style.width =  document.getElementsByTagName('body')[0].clientWidth + 1 + "px"; // "955px"; //955document.body.scrollWidth +
	toogle_div("overlay");
	toogle_div(nomdiv);
}

function toogle_div(nomdiv){
	var div = document.getElementById(nomdiv);
	//var img = document.getElementById(imgname);
	if (div.style.display == "none"){
		div.style.display = "block";
		//img.src = "commun/images/btn_moins.jpg";
	} else {
		div.style.display = "none";
		//img.src = "commun/images/btn_plus.jpg";
	}
}

function get_pays(pays){
	var c = document.getElementById("cntpays");
	document.getElementById("tmp").innerHTML = "<img src='commun/images/indicator.gif'>";
	var xmlhttp = getHTTPObject(c,"html","innerHTML");
	xmlhttp.open("POST", "ajax/pays.php",true);
	xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlhttp.send("pays="+pays+"&cp="+document.getElementById("cp_cnt").value+"&ext=_cnt");
}

function get_cp(){ 
	var c = document.getElementById("cntville");
	document.getElementById("tmp").innerHTML = "<img src='commun/images/indicator.gif'>";
	var xmlhttp = getHTTPObject(c,"html","innerHTML");
	xmlhttp.open("POST", "ajax/ville.php",true);
	xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlhttp.send("cp="+document.getElementById("cp_cnt").value+"&ext=_cnt");
}
function resetchp(value,id,js,width,tindex){
	if (!value){
		document.getElementById("cnt"+id).innerHTML = "<input onfocus='"+js+"' tabindex='"+tindex+"' id='"+id+"_cnt' name='"+id+"_cnt' value=''>";
	} else {
		document.getElementById("cnt"+id).innerHTML = "<input onfocus='"+js+"' tabindex='"+tindex+"' id='"+id+"_cnt' name='"+id+"_cnt' value='"+value+"'>";
	}
}

function form_validation_rappel() {
    var checkEmail = "@.";
    var EmailValid = false;
    var EmailAt = false;
    var EmailPeriod = false;
    var err = false;
    var checkStr = document.getElementById('email_cnt').value;
    
    if (document.getElementById('nom_cnt').value == ''){
        alert('Veuillez saisir votre Nom');
        document.getElementById('nom_cnt').focus();
        err = true;
    }
    if (document.getElementById('email_cnt').value == '' && !err){
        alert('Veuillez saisir votre E-mail');
        document.getElementById('email_cnt').focus();
        err = true;
    }
    if (document.getElementById('tel_cnt').value == '' && !err){
        alert('Veuillez saisir votre téléphone');
        document.getElementById('tel_cnt').focus();
        err = true;
    }
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkEmail.length;  j++)
    {
      if (ch == checkEmail.charAt(j) && ch == "@")
        EmailAt = true;
      if (ch == checkEmail.charAt(j) && ch == ".")
        EmailPeriod = true;
	  if (EmailAt && EmailPeriod)
		break;
	  if (j == checkEmail.length)
		break;
	}
	// if both the @ and . were in the string
    if (EmailAt && EmailPeriod)
    {
		EmailValid = true
		break;
	}
  }
  
  if (!EmailValid && !err)
  {
    alert("L'adresse électronique rentrée est invalide !");
    document.getElementById('email_cnt').focus();
    err = true;
  }
  if (err == false)
      return true;
  else
      return false;

}

function form_validation_alerte() {
    var checkEmail = "@.";
    var EmailValid = false;
    var EmailAt = false;
    var EmailPeriod = false;
    var err = false;
    var checkStr = document.getElementById('email_ale').value;
    
    if (document.getElementById('nom_ale').value == ''){
        alert('Veuillez saisir votre Nom');
        document.getElementById('nom_ale').focus();
        err = true;
    }
    if (document.getElementById('email_ale').value == '' && !err){
        alert('Veuillez saisir votre E-mail');
        document.getElementById('email_ale').focus();
        err = true;
    }
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkEmail.length;  j++)
    {
      if (ch == checkEmail.charAt(j) && ch == "@")
        EmailAt = true;
      if (ch == checkEmail.charAt(j) && ch == ".")
        EmailPeriod = true;
	  if (EmailAt && EmailPeriod)
		break;
	  if (j == checkEmail.length)
		break;
	}
	// if both the @ and . were in the string
    if (EmailAt && EmailPeriod)
    {
		EmailValid = true
		break;
	}
  }
  if (!EmailValid && !err)
  {
    alert("L'adresse électronique rentrée est invalide !");
    document.getElementById('email_ale').focus();
    err = true;
  }
  if (err == false)
      return true;
  else
      return false;

}


function form_validation(param) {
    var checkEmail = "@.";
    var EmailValid = false;
    var EmailAt = false;
    var EmailPeriod = false;
    var err = false;
    var checkStr = document.getElementById('email_ct').value;
    
    if (document.getElementById('nom_ct').value == ''){
        alert('Veuillez saisir Nom');
        document.getElementById('nom_ct').focus();
        err = true;
    }
    if (document.getElementById('email_ct').value == '' && !err){
        alert('Veuillez saisir E-mail');
        document.getElementById('email_ct').focus();
        err = true;
    }
    if (param && document.getElementById('adresse_cnt').value == '' && !err){
        alert('Veuillez saisir votre N° et Rue');
        document.getElementById('adresse_cnt').focus();
        err = true;
    }
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkEmail.length;  j++)
    {
      if (ch == checkEmail.charAt(j) && ch == "@")
        EmailAt = true;
      if (ch == checkEmail.charAt(j) && ch == ".")
        EmailPeriod = true;
	  if (EmailAt && EmailPeriod)
		break;
	  if (j == checkEmail.length)
		break;
	}
	// if both the @ and . were in the string
    if (EmailAt && EmailPeriod)
    {
		EmailValid = true
		break;
	}
  }
  if (!EmailValid && !err)
  {
    alert("L'adresse électronique rentrée est invalide !");
    document.getElementById('email_c').focus();
    err = true;
  }
  if (err == false)
      return true;
  else
      return false;

}
