var pointer = false;
var current_id_focus = 0;
var current_nbr = 0;
var search_id = new Array('-1');
// Ligne non selectionnee
var background_color_no_foc = '#FFFFFF';
var color_text = '#FFFFFF';
// Ligne selectionnee
var background_color_foc = '#EF7E38';
var color_no_text = '#000000';




function donner_focus(){
	document.getElementById('list0').focus();
}

function writetag(tag, content) {
     document.getElementById(tag).innerHTML = content;
}

//Permet la v�rification du manuel du produit
function check(myForm){
	ret =true;
	if (document.getElementById('choixProduit').value == '-1'){
		writetag('error_produit', '<span class="erreur" style="color:red">Vous devez s&eacute;lectionner un produit</span>');
		ret = false;
	}
return ret;
}



	

/**
  *		Fonction controlant les actions a faire en fonction de la valeur du code de l'event "KeyUp"
  *		Permet la navigation clavier dans la liste dynamique
  *		
  *		@ param : value => valeur du champ de texte principal
  *			       event => l'evenement associe au KeyUp
  */
function event_capture(value,event)
{

	switch (event.keyCode)
	{

		default :
				AJAX(value);
				document.getElementById('produit_comp').style.display='block';
				break;
	}
}

/**
  *		Permet avec l'outil ajax d'effectuer dynamiquement des requetes
  *
  *		@ param value : valeur de la recherche courante
  *
  */
function AJAX_old(value)
{
	
	var AJAX = createXMLHttpRequest();
		
	// On defini le fichier appele lors de l'envoie
	AJAX.open("POST",'?p=25&sp=3&mot='+value,true);
	
	//Fonction appelee automatiquement lors de la fin des transferts asynchrones
	// C'est ici que l'on dit ce qu'il va se passer apres le renvoie
	AJAX.onreadystatechange = function()
	{
		if (AJAX.readyState == 4 && AJAX.status == 200) 
		{
			writetag('produit_comp', AJAX.responseText);
		}
	}
	
	// On declare le type des donnees echangees => ici du texte
	// On pourait utiliser du XML mais ici cela est tr�s peu utile par le petite quantite de donnee echangee
	AJAX.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	AJAX.send('post='+value);
}

function hide_list()
{
	/*
	document.getElementById('list_produits').style.display='none';
	*/

	document.getElementById('list_produits').innerHTML='';
	document.getElementById('list0').value='';
}

function delete_product()
{
	document.getElementById('alert_motcle_som').checked= false;
	document.getElementById('id_produit').value='-1';
	document.getElementById('label_produit').value = '';
	document.getElementById('result_motcle').innerHTML = 'Aucun';
	document.getElementById('delete_product').innerHTML = '';
}


/**
  *		Permet de remettre le style normal
  *		
  *		@ param : id => id de l'input text a desactiver le style pointer
  */
function loose_focus_style(id)
{
	if ( current_id_focus != 0 )
	{
			var input_hidden = document.getElementById('list'+id);
			input_hidden.style.backgroundColor = background_color_no_foc;
			input_hidden.style.color = color_no_text;
	}
}


/**
  *		Permet de changer le style des inputs
  *		
  *		Changement de style par javascript 
  *		
  *		@ param : id => id de l'input text a changer le style
  *			       control => permet suivant les cas l'activation de certaines operations
  *
  */
  
 
 function give_focus_style(id,control)
{
	if ( id != 0 && id != current_id_focus && id != null)
	{
		
		var input_hidden = document.getElementById('list'+id);
		input_hidden.style.backgroundColor = background_color_foc;
		input_hidden.style.color = color_text;
		input_hidden.style.cursor = 'default';
	
	}
	if ( control == 1 && current_id_focus != null)
	{
		//alert("current"+id+"ancien"+current_id_focus);
		current_id_focus = id;
		loose_focus_style(current_id_focus);

	}
}


var xmlHttp;

function showHint(str) {

	if (xmlHttp != null) {
		xmlHttp.abort();
		xmlHttp = null;
	}

	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	} 
	var url= "index.php?p=25&sp=3&mot="+str;
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
} 

function stateChanged() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		//document.getElementById("wait").innerHTML='';
 		document.getElementById("list_produits").innerHTML=xmlHttp.responseText;
	} 
	//else { document.getElementById("wait").innerHTML='<div class="erreur"><img src=\"images/loading.gif\" alt=\"Analyse\" width=\"15\" />Analyse...</div>'; }
}

function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
	// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function loadCountryState25(id_country,id_etat) {

	if (id_etat!=null) {
		var req_etat = '&id_etat='+id_etat;
		} else {
		var req_etat = '';
		}

	xmlhttp_lcs = createXMLHttpRequest();
	if (xmlhttp_lcs!=null) {	
			xmlhttp_lcs.onreadystatechange = displayCountryState;
			xmlhttp_lcs.open("GET",'/expo/?p=25&sp=4&id_country='+id_country+req_etat,true);
			xmlhttp_lcs.send(null);
	}
}

function displayCountryState() {
	
	if (xmlhttp_lcs.readyState == 4) {
		if (xmlhttp_lcs.status == 200) {
			writetag('country_state_list', xmlhttp_lcs.responseText);
			if (xmlhttp_lcs.responseText!='') {
			document.getElementById('country_state_list').style.display = 'block';
			} else {
			document.getElementById('country_state_list').style.display = 'none';
			}
		} 
		else {
			/*alert("Problem retrieving XML data on ***,statut : "+xmlhttp_lcs.status);*/
		}
	}
	else {
		writetag('country_state_list', '<img src=\"images/loading.gif\" alt=\"En cours\" width=\"15\" />En cours...');
	}
}

function switchCat(elem){
	if(elem.checked==true)
		checkCat();
	else
		uncheckCat();
}

function checkCat(){
	document.getElementById('alert_cat_som').checked= true;
}

function uncheckCat(){
	if (allCatUnchecked())
		document.getElementById('alert_cat_som').checked= false;
}

function allCatUnchecked(){
	myForm = document.getElementById('formChoixNews');
	ret = true;
		for (i=0;i<myForm.elements.length;i++){
				champ = myForm.elements.item(i);
				tag = champ.tagName ;
				test =champ.name ;
				if ((test.indexOf('_cat')> 0) && test!='alert_cat_som' && (champ.checked==true)){
					ret = false;
				}
		}
		return ret;
}

function checkChoixCat(){
	myForm = document.getElementById('formChoixNews');
	var msg = '';
	var msg2 = '';
	ret = true;

	if(document.getElementById('alert_motcle_som').checked==false && document.getElementById('alert_cat_som').checked==false){

		alert('Veuillez choisir une newsletter');	
		return false;
	}
		
	if(document.getElementById('alert_motcle_som').checked==true && document.getElementById('id_produit').value=='-1'){
		alert('Vous devez sélectionner un produit');	
		return false;
	}

	if(document.getElementById('alert_cat_som').checked==true){
		for (i=0;i<myForm.elements.length;i++){
				champ = myForm.elements.item(i);
				tag = champ.tagName ;
				test = champ.name ;
				if ((test.indexOf('_cat')> 0) && test!='alert_cat_som' && (champ.checked==true)){
					ret=true;
					msg = '';
					break;
				}
				ret = false;
				msg = 'Veuillez sélectionner une catégorie \n';
		}
	}
	
	if (ret==false) {
		alert (msg);
	}
	
	return ret;
}

function choixProduit(){
	if(document.getElementById('list0').value != ''){
		document.getElementById('alert_motcle_som').checked= true;
	}
	else{
		document.getElementById('alert_motcle_som').checked= false;
	}
}

function choixMotCle(id,label){
	document.getElementById('id_produit').value= id;
	document.getElementById('result_motcle').innerHTML= '<span style=\'font-size:1.2em;color:#217B8E;\'>' + label + '</span>';
	document.getElementById('label_produit').value= label;
	document.getElementById('delete_product').innerHTML="&nbsp;&nbsp;<a class='ajouter' onclick='delete_product(this);return false;' href='#' onmouseover=\"this.style.textDecoration='underline'\" onmouseout=\"this.style.textDecoration='none'\" style=\"text-decoration:none;color:black;\" ><img src='images/table_supprimer.gif' /> Supprimer ce mot cl&eacute;</a>" ;
	hide_list();
	document.getElementById('alert_motcle_som').checked= true;

}

function check_email25() {
	if (xmlhttp_check_email.readyState==4) {
		if (xmlhttp_check_email.status==200) {
			writetag('error_email_o', xmlhttp_check_email.responseText);
		} 
		else {
			/*alert("Problem retrieving XML data on check_email(),statut : "+xmlhttp_check_email.status);*/
		}
	}
	else
		writetag('error_email_o', '<div class="erreur"><img src=\"images/loading.gif\" alt=\"En cours\" width=\"15\" />En cours...</div>');
}

var xmlhttp_check_email;

function verifEmail25(email) {
	if (email.value.length == 0) {
		return false;
	}
	writetag('error_'+email.id, '');
	xmlhttp_check_email = createXMLHttpRequest();
	if (xmlhttp_check_email!=null) {
			xmlhttp_check_email.onreadystatechange = check_email25;
			xmlhttp_check_email.open("GET",'/expo/?p=25&sp=5&email='+escape(email.value),false);
			xmlhttp_check_email.send(null);
	}
	var modele = /^[a-z0-9\-_\.]+@[a-z0-9.-]{2,}[.][a-z]{2,5}$/i;
	if (!modele.test(email.value)){
		return false;
	}
	return true;
}





/**/

function check_devis(id){
	writetag('error_general', '');
	myForm = document.getElementById(id);
	ret = true;
	var list_erreur = '';
	var nom_champ_tmp = '';
	for (i=0;i<myForm.elements.length;i++){
		champ = myForm.elements.item(i);
		tag = champ.tagName ;
		test =champ.name ;
		if (test.indexOf('_o')> 0){
			writetag('error_'+champ.id, '');
			current = champ.name;
			switch (tag){
				case "INPUT" :
					if (champ.value.length == 0){						
						if (test=="nom_o")		nom_champ_tmp = 'nom';
						if (test=="prenom_o")	nom_champ_tmp = 'pr&eacute;nom';
						if (test=="email_o")	nom_champ_tmp = 'email';
						if (test=="fonction_o")		nom_champ_tmp = 'fonction';
						if (test=="entreprise_o")	nom_champ_tmp = 'entreprise';
						if (test=="adresse_o")	nom_champ_tmp = 'adresse';
						if (test=="cp_o")		nom_champ_tmp = 'code postal';
						if (test=="ville_o")	nom_champ_tmp = 'ville';
						writetag('error_'+champ.id, '<span class="erreur"><img src="images/expo/croix.gif" alt="Veuillez remplir le champ" style="margin-top:6px;" /> Veuillez remplir le champ </span>');
						
						if (ret==false)list_erreur = list_erreur+', ';												
						list_erreur = list_erreur+nom_champ_tmp;
						ret = false;
					}
					else {
						if(test=="mail_o" || test=="email_o"){
							
							if (champ.value.length > 0 && !verifEmail25(champ)) {
							
								writetag('error_'+champ.id, '<span class="erreur"><img src="images/expo/croix.gif" style="margin-top:6px;" /> Email non valide </span>');
							
								if (ret==false)list_erreur = list_erreur+', ';												
								list_erreur = list_erreur+'email';
								ret = false;
							}
							
						}
						else {	
									
							//writetag('error_'+champ.id, '');
							
						}
						
						if (test=="cp_o"){							
							if (document.getElementById('pays_o').value == 47 ){
								if (isNaN(champ.value)){
									if (ret==false)list_erreur = list_erreur+', ';												
									list_erreur = list_erreur+'code postal';
									writetag('error_cp_o', '<span class="erreur"><img src="images/expo/croix.gif" alt="Veuillez remplir le champ" style="margin-top:6px;" /> Votre code postal est invalide</span>');
									ret = false;
								}else if(champ.value.length != '5'){
									if (ret==false)list_erreur = list_erreur+', ';												
									list_erreur = list_erreur+'code postal';
									writetag('error_cp_o', '<span class="erreur"><img src="images/expo/croix.gif" alt="Veuillez remplir le champ" style="margin-top:6px;" /> Votre code postal est invalide</span>');
									ret = false;			
								}
							}
						}
					}
					break;
				
				case  "SELECT" :
					if(champ.options[champ.selectedIndex].value =='' || champ.options[champ.selectedIndex].value =='-1'){		
								
						if (test=="civ_o")	nom_champ_tmp = 'civilit&eacute;';
						if (test=="secteur_o")	nom_champ_tmp = 'un secteur';
						if (test=="pays_o") nom_champ_tmp = 'pays';
						if (test=="fonction_o")		nom_champ_tmp = 'fonction';
						if (test=="service_o")	nom_champ_tmp = 'service';
						if (test=="etat_pays_o") nom_champ_tmp = 'etat';				
						if (ret==false)list_erreur = list_erreur+', ';												
						list_erreur = list_erreur+nom_champ_tmp;
						writetag('error_'+champ.id, '<span class="erreur"><img src="images/expo/croix.gif" alt="Veuillez choisir le champ" style="margin-top:6px;" /> Veuillez choisir </span>');
						ret = false;
					}
					break;
			}
		}
	}
	
	if (ret==false) writetag('error_general', '<span class="erreur"><img src="images/expo/exclamation-b0101f.gif" alt="" /> Veuillez correctement remplir les champs '+list_erreur+'</span>');
/*		
	if (ret && !(document.getElementById('information1').checked || document.getElementById('information2').checked)) {
		ret= false;
		alert('Veuillez choisir de recevoir ou non des offres promotionnelles sur votre adresse e-mail!') ;			
	}


	if (ret && !myForm.condition.checked){
		ret= false;
		alert('Vous devez accepter les Conditions G�n�rales de Vente pour valider votre demande !');
	}	
*/
	return ret; 
}
