

function getxmlhttp() {
   xmlhttp= null;
  if( window.XMLHttpRequest) { // Mozilla
    xmlhttp= new XMLHttpRequest();
  } else {
    if( window.ActiveXObject) { // IE
      try { xmlhttp= new ActiveXObject( "Msxml2.XMLHTTP");
      } catch (e) { xmlhttp= new ActiveXObject( "Microsoft.XMLHTTP");
      }
    } else {
      alert( "Votre navigateur ne supporte pas XMLHttpRequest");
    }
  }
  return xmlhttp;
}


/*  -------------------------------------------------------------------- */
/* 							Controles saisies							 */
/*  -------------------------------------------------------------------- */


// Supprime les espaces inutiles en début et fin de la chaîne passée en paramètre.
function trim( aString) {

	var regExpBeginning = /^\s+/;
	var regExpEnd       = /\s+$/;

    return aString.replace( regExpBeginning, "").replace( regExpEnd, "");
}

// contrôle de l'envoi des infos de bases d'un partenaire venant d'un 
// formulaire (form). Contrôle différent suivant la provenance (page)
// ex: depuis validation.php, pas de contrôle MDP 
function controlSendInfo( page) {
 
  var error= 0;
  
  // test saisie des chamsp societe, nom_resp et tel
  if ( trim( document.getElementById( "societe").value)== "") 
  {
    error++;
    document.getElementById( "societe").style.backgroundColor='#FC6D8A';
    document.getElementById( "societe").value='';
  }
  
  if ( trim( document.getElementById( "nom_resp").value)== "") 
  {
    error++;
    document.getElementById( "nom_resp").style.backgroundColor='#FC6D8A';
    document.getElementById( "nom_resp").value='';
  }
  
  if ( trim( document.getElementById( "tel").value)== "") 
  {
    error++;
    document.getElementById( "tel").style.backgroundColor='#FC6D8A';
    document.getElementById( "tel").value='';
  }
  
  if ( page== "inscription")
  {
  	// test mots de passes sont saisis et correspondent
  	if ( document.getElementById( "pwd").value.length< 5 || document.getElementById( "pwd").value!= document.getElementById( "conf_pwd").value) 
  	{
    	error++;
	    document.getElementById( "pwd").style.backgroundColor='#FC6D8A';
    	document.getElementById( "pwd").value='';
		document.getElementById( "conf_pwd").style.backgroundColor='#FC6D8A';
    	document.getElementById( "conf_pwd").value='';
  	}
  }
  
	// test saisie de l'e-mail (existence + regex)
  var reEMail= /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  if ( trim( document.getElementById( "email").value)== "" || reEMail.test( trim( document.getElementById( "email").value))== false) 
  {
    error++;
    document.getElementById( "email").style.backgroundColor='#FC6D8A';
    document.getElementById( "email").value='';
  }
  
  // test saisie du type
  if ( document.getElementById( "id_type").value== -1)
  {
  	error++;
    document.getElementById( "id_type").style.backgroundColor='#FC6D8A';
  }
  
 
  // test saisie de region
  if ( document.getElementById( "id_region").value== -1)
  {
  	error++;
  	document.getElementById( "id_region").style.backgroundColor='#FC6D8A';
  }
  
  // résultat
  if ( error>0) 
  {
        alert ( "les champs en rouge sont obligatoires ou mal renseignés");
        return false;
  }
  else 
       return true;
}

function changeColor( id) {
  
   document.getElementById( id).style.backgroundColor= 'white';
}

function confirmDelPartner( ) {
	if( confirm( "la suppression sera définitive"))
		return true;
	else
		return false;
}

/*  -------------------------------------------------------------------- */
/* 							objet getxmlhttp							 */
/*  -------------------------------------------------------------------- */

function getxmlhttp() {
   xmlhttp= null;
  if( window.XMLHttpRequest) { // Mozilla
    xmlhttp= new XMLHttpRequest();
  } else {
    if( window.ActiveXObject) { // IE
      try { xmlhttp= new ActiveXObject( "Msxml2.XMLHTTP");
      } catch (e) { xmlhttp= new ActiveXObject( "Microsoft.XMLHTTP");
      }
    } else {
      alert( "Votre navigateur ne supporte pas XMLHttpRequest");
    }
  }
  return xmlhttp;
}

/*  -------------------------------------------------------------------- */
/* 							fonctions "AJAX"							 */
/*  -------------------------------------------------------------------- */

function goUpdateInfo( ) {
  
  if( !controlSendInfo( "infos"))
  	return;

  url= "infos.goUpdateInfo.php";
  xmlhttp= getxmlhttp();
  xmlhttp.onreadystatechange= xmlhttpChangeInfo;
  xmlhttp.open( "POST",url,true);
  xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  xmlhttp.send( "id_content=" + document.getElementById( "id_content").value +
  				"&societe=" + document.getElementById( "societe").value.replace( /&/g, '%26') + 
  				"&nom_resp=" + document.getElementById( "nom_resp").value.replace( /&/g, '%26') +
				"&pnom_resp=" + document.getElementById( "pnom_resp").value.replace( /&/g, '%26') +
				"&email=" + document.getElementById( "email").value.replace( /&/g, '%26') +
				"&site=" + document.getElementById( "site").value.replace( /&/g, '%26') +
				"&adresse1=" + document.getElementById( "adresse1").value.replace( /&/g, '%26') +
				"&cp1=" + document.getElementById( "cp1").value.replace( /&/g, '%26') +
				"&ville1=" + document.getElementById( "ville1").value.replace( /&/g, '%26') +
				"&pays=" + document.getElementById( "pays").value.replace( /&/g, '%26') +
				"&tel=" + document.getElementById( "tel").value.replace( /&/g, '%26') +
				"&mobile=" + document.getElementById( "mobile").value.replace( /&/g, '%26') +
				"&fax=" + document.getElementById( "fax").value.replace( /&/g, '%26') +
				"&id_region=" + document.getElementById( "id_region").value.replace( /&/g, '%26') +
				"&id_type=" + document.getElementById( "id_type").value.replace( /&/g, '%26') );
}

function xmlhttpChangeInfo() {
  if ( xmlhttp.readyState==4) { // if xmlhttp shows "loaded"
    if ( xmlhttp.status==200) { // if "OK"
      s= xmlhttp.responseText;
	  alert( html_entity_decode( s));
    } else {
      alert( "Problem retrieving XML data" + xmlhttp.status);
    }
  }
}

function isset( variable){

if ( typeof( window[ variable] )!= "undefined" ) 
     return true;
else 
     return false;
}

function html_entity_decode(str) {
   var ta=document.createElement( "textarea");
   ta.innerHTML=str.replace( /</g, "&lt;").replace( />/g, "&gt;");
   return ta.value;
}

// envoie une alerte si une zone de texte est "zone" supérieure à "max",
// réduit la taille de "zone" à "max"
function lenghtTA( zone, max)
{
	if( zone.value.length>= max)
	{
		alert( "cette zone de saisie est limitée à " + max + " caractères");
		zone.value= zone.value.substring( 0, max);
	}
}

