function isdefined(variable){
    return (typeof(window[variable]) == "undefined")?  false: true;
}

String.prototype.numerique = function() {
  if (isNaN(this)) {
     var mot = this.replace(",", ".");
     if (isNaN(mot)) {
        var mot = parseFloat(mot);
        if (isNaN(mot)) return false;
        else return mot;
     }
     else return mot;
  }
  else return this;
}


function email_valide(adresse) {
return (adresse.search("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+([\.][a-z0-9-]+)+$") != -1);
}


function color_and_focus(ao_field){
  	ao_field.style.backgroundColor= "#FFDDDD";
  	ao_field.focus();
}

function fields_background_reset(ao_form){
	ln = ao_form.elements.length;
	for ( j=0; j<ln; j++){
  			ao_form.elements[j].style.backgroundColor = "#FFFFFF";
  	}
}

function is_empty(ao_field){
	if (ao_field.value == ""){	
  		alert("Veuillez remplir le champ '"+ao_field.name+"'");
  		color_and_focus(ao_field);
  		return true;
 	}
 	return false;
}


function is_valid_month(ao_field){
	if ( (!ao_field.value.numerique()) || (ao_field.value <1) || (ao_field.value >12) ){
		alert("Le champ '"+ao_field.name+"' doit etre un nombre de 1 à 12");
  		color_and_focus(ao_field);
  		return false;
 	}
	return true;
}

function is_valid_year(ao_field){
	if ( (!ao_field.value.numerique()) || (ao_field.value <1990) || (ao_field.value >2100) ){
		alert("Le champ '"+ao_field.name+"' doit etre un nombre de 1990 à 2100");
  		color_and_focus(ao_field);
  		return false;
 	}
	return true;
}


function is_valid_email(ao_field) {
	if (ao_field.value == ""){	
  		alert("Veuillez remplir le champ '"+ao_field.name+"'");
  		color_and_focus(ao_field);
  		return false;
 	}
	if ( !email_valide(ao_field.value) ){		
  		alert("Champ '"+ao_field.name+"' non valide");
  		color_and_focus(ao_field);
  		return false;
 	}
 	return true;
}

function is_checked_radio(ao_field) {
	var lb_vide= true;
	var max = ao_field.length;
	for (i=0;i<max;i++){
		rempli = eval("ao_field["+i+"].checked");
		if (rempli){
			lb_vide = false;
		}
	}
	if (lb_vide){	
		alert("Veuillez remplir le champ 'Qualité'");
		return false;
	}
	return true;
}

function is_checked_checkbox(ao_field) {
	if (ao_field.checked == false){
    	alert("Veuillez cocher la case prévue à cet effet");
  		return false;
 	}
	return true;
}


function verif_form(ao_form) {
	fields_background_reset(ao_form);
	if (is_empty(ao_form.titre)) return;
	ao_form.submit();
}