  function getPageWidth() {
    var windowWidth
    if (self.innerWidth) {	// all except Explorer
      windowWidth = self.innerWidth;
    } else if (document.documentElement && document.documentElement.clientWidth) { // Explorer 6 Strict Mode
      windowWidth = document.documentElement.clientWidth + 20;
    } else if (document.body) { // other Explorers
      windowWidth = document.body.clientWidth;
    }	
    return windowWidth
  }
  
String.prototype.isInteger = function() {
	return this.match(/^[0-9]+$/);
}

String.prototype.isEmail = function() {
	return this.match(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/);
}

function checkRappelImmediat(myForm) {
	if (myForm.societe.value == "") {
		alert("Le champ Société est obligatoire !");
		myForm.societe.focus();
		return false;
	}		
	if (myForm.nom.value == "") {
		alert("Le champ Nom est obligatoire !");
		myForm.nom.focus();
		return false;
	}	
	if (myForm.t1.value == "") {
		alert("Le champ Téléphone est obligatoire !");
		myForm.t1.focus();
		return false;
	}	
	if (!myForm.t1.value.isInteger()) {
		alert( 'Le champ Téléphone ne doit contenir que des chiffres!' );
		myForm.t1.focus();
		return false;
	}
	regex = new RegExp(/^(01|02|03|04|05|06|08|09)$/gi);
    if(!regex.test(myForm.t1.value)) {
		alert("Le champ Téléphone est Invalide");
		myForm.t1.focus();
		return false;
	}
	if ((myForm.t2.value == "") || (myForm.t3.value == "") || (myForm.t4.value == "") || (myForm.t5.value == "")) {
		alert("Téléphone Invalide");
		return false;
	}
	if (!myForm.t2.value.isInteger()) {
		alert( 'Le champ Téléphone ne doit contenir que des chiffres!' );
		myForm.t2.focus();
		return false;
	}
	if (!myForm.t3.value.isInteger()) {
		alert( 'Le champ Téléphone ne doit contenir que des chiffres!' );
		myForm.t3.focus();
		return false;
	}
	if (!myForm.t4.value.isInteger()) {
		alert( 'Le champ Téléphone ne doit contenir que des chiffres!' );
		myForm.t4.focus();
		return false;
	}
	if (!myForm.t5.value.isInteger()) {
		alert( 'Le champ Téléphone ne doit contenir que des chiffres!' );
		myForm.t5.focus();
		return false;
	}	
	
	return true;
}

function rappelImmediat(myForm, myItem) {
	rez = checkRappelImmediat(myForm);
	if (rez) {
		myItem.disabled = true;
		myItem.value = 'Patientez ...';
		
		t1 = myForm.t1.value;
		t2 = myForm.t2.value;
		t3 = myForm.t3.value;
		t4 = myForm.t4.value;
		t5 = myForm.t5.value;
		$.post(url_site+'/scripts/ajax.php', { action: "rappel_immediat", civilite: myForm.civilite.value, nom: myForm.nom.value, prenom: myForm.prenom.value, societe: myForm.societe.value, telephone: t1+t2+t3+t4+t5, appel: myForm.appel.value, section: myForm.section.value },
		function(data){
			$('#contenu_rappel_immediat').html(data);
		});
	} 
}

function Verifier_Numero_Telephone(num_tel) {
    if(num_tel.length != 10) {
        alert('Votre numéro de téléphone doit comporter 10 chiffres !');
        return false;
    }
    // Definition du motif a matcher
    var regex = new RegExp(/^(01|02|03|04|05|06|08|09)[0-9]{8}$/gi);
    // Definition de la variable booleene match
    var match = false;

    // Test sur le motif
    if(regex.test(num_tel)){
        match = true;
    }
    else {
        match = false;
        alert('Votre numéro de téléphone doit comporter 10 chiffres !');
    }

    // On renvoie match
    return match;
}

function checkNumTel(num_tel) {
    if(num_tel.length != 10) {
        return 'Votre numéro de téléphone doit comporter 10 chiffres !';
    }
    var regex = new RegExp(/^(01|02|03|04|05|06|08)[0-9]{8}$/gi);
    if(!regex.test(num_tel)){
        return 'Votre numéro de téléphone est invalide !';
    }
    return '';
}

function checkZipCode(zipcode) {
	if (zipcode != '')  {
	
	    if(zipcode.length != 5) {
	        return 'Votre zipcode doit comporter 5 chiffres !';
	    }
	    var regex = new RegExp(/^[0-9]{5}$/gi);
	    if(!regex.test(zipcode)){
	        return 'Votre zipcode est invalide !';
	    }
    }
    return '';
}

function lancerTestAdsl() {
	num_tel = $('#telephone').val();
	if (Verifier_Numero_Telephone(num_tel)) {
		$.facebox(function() {
		  $.get(url_site+'/scripts/ajax.php', { action: "testAdsl", numtel: num_tel },
		   function(data) {
			$.facebox(data)
		  })
		})	
	}
}

function lancerTestSdsl() {
	num_tel  = $('#telephone').val();
	zipcode = $('#zipcode').val();
	errtel 	 = checkNumTel(num_tel);
	errzip   = checkZipCode(zipcode);
	n 		 = $("input:checked").length;
	debits = $("input[name='debit']:checked");
	debit = '';
	for (i=0; i<n; i++) {
		if (i>0) debit += '|';
		debit += debits[i].value;
	}

	if (errtel) {
		alert (errtel);
	} else if (errzip) {
		alert (errzip);
	} else if (n==0) {
		alert ('Indiquez au moins un débit !');
	} else {
		$.facebox(function() {
		  $.get(url_site+'/scripts/ajax.php', { action: "testSdsl", numtel: num_tel, zipcode: zipcode, debit: debit },
		   function(data) {
			$.facebox(data)
		  })
		})	
	}
}

function testerAutreLigne() {
	$('#telephone').val('');
	$('#telephone').focus();
	$.facebox.close();
}

function formRappelImmediat() {
	$.facebox(function() {
	  $.get(url_site+'/scripts/ajax.php', { action: "formRappelImmediat", section: section },
	   function(data) {
		$.facebox(data)
	  })
	})	
}

function check_frm(frm)	{ 
		if(frm.nom.value=="") {
			alert("Veuillez saisir Votre Nom!");
			frm.nom.select();
			return false;
		}		
		if(frm.prenom.value=="") {
			alert("Veuillez saisir Votre Prénom!");
			frm.prenom.select();
			return false;
		}
		if(frm.email.value=="") {
			alert("Veuillez saisir votre email!");
			frm.email.select();	
			return false;	
		}
		if(frm.email.value.indexOf("@")==-1 ||frm.email.value.indexOf(".")==-1){
			alert("E-mail Invalide!");
			frm.email.select();	
			return false;	
		}
		if(frm.tel.value=="") {
			alert("Veuillez saisir votre téléphone!");
			frm.tel.select();
			return false;	
		}
		
		frm.send.value = 1;
		
	}
/****************** Programme magic partner ******************/
var nbloc=0;
function attrib_contact() {
	commercial = $('#commercial').val();
	prospect = $('#id_prospect').val();
	action = $('#action').val();
	
	if(action == 'update') action = 'update_contact';
	else  action = 'attrib_contact';
	
	$.facebox(function() {
	$.get(url_site+'/partners/includes/ajax.php', { action: action, id_commercial: commercial, id_prospect: prospect },
		   function(data) {
			$.facebox(data)
		  })
		})	
	}
	
function AffichePrestations(i) {
	var champ  = "type_prestation"+i;
	type_prestation = document.getElementById(champ).value;
	if(type_prestation != '') {
		form =  document.inscription_partner.name;
		prestation = "prestation"+i;
		action = 'affiche_prestation';
		$.get(url_site+'/partners/includes/ajax.php',{ action:action , type_prestation: type_prestation, form:form,  prestation: prestation , i:i},function(data){
		document.getElementById("montant"+i).value = '';																																					
		$('#list_prestation'+i).html(data);																																											});
	}
	else {
	document.getElementById("prestation"+i).options.length = 0;
	document.getElementById("montant"+i).value = '';
	}
}


function afficheMontant(i) {
	prestation = document.getElementById("prestation"+i).value;
	if(prestation != '') {
		form = document.inscription_partner.name;
		action = 'affiche_montant';
		$.get(url_site+'/partners/includes/ajax.php',{ action:action , prestation: prestation, form:form , i:i },function(data){
		$('#list_montant'+i).html(data);																																											});
	}
	else {
	document.getElementById("montant"+i).value = '';
	}
}


function addPrestation() {
nbloc++;
document.getElementById('bloc').innerHTML += "<br clear='all' /><div id='list_type_prestation"+nbloc+"' class='fll'><label></label>:<select name='type_prestation[]' id='type_prestation"+nbloc+"' onchange='AffichePrestations("+nbloc+");'>"+document.getElementById('type_prestation0').innerHTML+"</select></div>"+"<div id='list_prestation"+nbloc+"' class='fll'>&nbsp;Prestation :  <select name='prestation[]' id='prestation"+nbloc+"'></select></div>"+"<div id='list_montant"+nbloc+"' class='fll'>&nbsp;Montant:<input type='text' name='montant[]' id='montant"+nbloc+"' class='mnt' value=''></div>";
}

function add_article() {
document.getElementById('bloc').innerHTML += "<br /><br /><input type='hidden' value='' name='id_article[]' /><label>Code article</label>:<input type='text' name='code_article[]' id='code_article'  value=''/><br /><label>Montant d'abonnement</label> :<input type='text' name='mnt_abonnement[]' id='mnt_abonnement' value='' /><br /><label>N° de facture</label>:<input type='text' name='n_facture[]' id='n_facture' value='' /><br /><label>Code d'abonnement</label>:<input type='text' name='code_abonnement[]' id='code_abonnement' value='' /><br /><label>Code client livré</label>:<input type='text'  name='code_client_livre[]' id='code_client_livre' value='' /><br /><label>Code client facturé</label>:<input type='text' name='code_client_facture[]' id='code_client_facture' value='' /><br />";
}

function test_domaine(f){
	var txt = f.domaine_name.value;
	if(txt.length == 0){
			alert('Nom de domaine incorrect');
			return false;
		}
	
	if(f.extension.value == 'fr') {
		if(txt.length < 2){
			alert('Nom de domaine incorrect');
			return false;
		}	
	
		if(txt.length == 2 ){
			var reg = /[0-9]/;
			if(reg.exec(txt) == null) {
			alert('Nom de domaine incorrect');
			return false
			}	
		}
	}	
	return true;
}

/* formulaire de commande */
function validatePhone(fld, titre) {
	
	var error = ""; var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');   
	// Definition du motif a matcher
    var regex = new RegExp(/^(01|02|03|04|05|06|08|09)[0-9]{8}$/gi);
    // Definition de la variable booleene match
    var match = false;

   if (fld.value == "") {
        error = "Le champ "+titre+" ne doit pas être vide.\n";
    }
	
	else if(!(stripped.length == 10)) {
        error ="Votre numéro de "+titre+" doit comporter 10 chiffres !";       
    }
    else if(!regex.test(stripped)){
       error = "Le numéro de "+titre+" est invalide !";
    }
    // On renvoie match
    return error;
}

function trim(s) {
	return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld, titre) {
	
    var error="";
    var tfld = trim(fld.value);                 
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
      
    if (fld.value == "") {
        error = "Le champ "+ titre +" ne doit pas être vide.\n";
    } else if (!emailFilter.test(tfld)) {
        error = "L'adresse Email n'est pas valide.\n";
    } else if (fld.value.match(illegalChars)) {
        error = "Le champ  "+ titre +" ne doit pas contenir des caractères illégals.\n";
    } 
	 
	 return error;
}

function check_commande(f)	{
	if(f.nom.value == "")		{
		alert("Le champ Nom ne doit pas être vide");
		f.nom.focus();
		return false;
	}
	
	reason = validateEmail(f.email, 'Email');
	if(reason){
		alert(reason);
		f.email.focus();
		return false;
	}
	
	reason = validatePhone(f.telephone, 'Tél');
	if(reason)		{
		alert(reason);
		f.telephone.focus();
		return false;
	}
	
	if(f.mot.value == "")		{
		alert("Le champ code sécurité ne doit pas être vide");
		f.mot.focus();
		return false;
	}
}

function valid_newsletter(f){
		if (f.nws.value == "votre@email" || f.nws.value == "")		{
				alert("E-mail Invalide!");
				f.nws.focus();
				return false;
			}
		
		reason = validateEmail(f.nws, 'Email');
		if(reason){
			alert(reason);
			f.nws.focus();
			return false;
		}
		f.send.value = 1;	
	}

function changement(){
   if (document.getElementById('messagerie2').checked)
      document.getElementById('domaine2').disabled=false;
  else
      document.getElementById('domaine2').disabled=true;
}

function sdsl(){
   if (document.getElementById('connexion_sdsl').checked)
     form.sdsl0.value = "Statistiques de consommation : OK";
   else
      form.sdsl0.value = "Statistiques de consommation : NON";
}

function heber(){
   if (document.getElementById('hebergement').checked)
     form.heberg0.value = "Codes interface PLESK - totalhosting : OK";
   else
      form.heberg0.value = "Codes interface PLESK - totalhosting : NON";
}


function voip_(){
   if (document.getElementById('voip').checked)
     form.voip0.value = "Voip : OK";
   else
      form.voip0.value = "Voip : NON";
	 }
