// JavaScript Document


/*
var fond = "#000000";// Couleur de fond de la barre
var couleur = "#FFFFFF";//Couleur de fond de la page de 
var largeur = screen.width;
var haut = ((screen.height/2)+80)/30;
document.write('<div id="hidepage" style="position: absolute; left:0px; top:0px; background-color: '+ couleur +'; layer-background-color: '+couleur+'; height: 100%; width: '+largeur+'; display:none">Please wait</div>');
*/

function allDone(){
	wbo_hide();
	pupi_Close();
}

function rotTwoDivs(div1, div2){
	//alert (div1.style.display);
	if (div1.style.display == 'block'){
		div1.style.display = 'none';
		div2.style.display = 'block';
	} else {
		div1.style.display = 'block';
		div2.style.display = 'none';	
	}
}


function copyFielToField(f1,f2){
	f2.value = f1.value;	
}


function textCounter(field, countfield, maxlimit) {
	if (field.value.length > maxlimit){
		field.value = field.value.substring(0, maxlimit);
		// otherwise, update 'characters left' counter
	}
	else { 
		countfield.value = maxlimit - field.value.length;
	}
}



function doSendCC(obj) {
    
	var tabResult = obj.responseXML.getElementsByTagName('resultat');
	if (tabResult.length > 0) {
		
		//document.getElementById('loadingBar').style.display = "block";
		for (var i = 0; i < tabResult.length; i++) {
			resultat = tabResult.item(i); 
			//appendOptionLast(resultat.getAttribute('envois'), resultat.getAttribute('motif'), 'select_region');
			//alert(resultat.getAttribute('motif'));
			if (resultat.getAttribute('envois') == 1){
				alert(tradCC);
			}
			else if (resultat.getAttribute('envois') == 0 && resultat.getAttribute('motif') == 1){
				alert(tradActif+'! '+ tradOp+'!');
			}
			else if (resultat.getAttribute('envois') == 0 && resultat.getAttribute('motif') == 3){
				alert(tradMs+'! \n'+tradQuota+'!');
			}
			else {
				alert(tradCon+'!\n'+tradConOp+'!\n'+tradFAQ+'!');
				//popup('popup_connexion.php', 'Connexion', 250, 110, 'no');
				document.getElementById('zoneconnex').style.visibility = 'visible';
			}
			
			//verifie si le membre est logué (session), s'il ne l'est pas, montrer popup : window.open + envois coup de coeur
			//si membre logué, envoyer coup de coeur + alert client
			
		}
	
	}
	else {
		//document.getElementById('zoneResultats').style.visibility = 'hidden';
	}
	document.getElementById('loadingBar').style.display = "none";
}


function sendCoupCoeur(id_membre_dest){
	//id_dest
	//alert(id_membre_dest);
	document.getElementById('loadingBar').style.display = "block";
	var XHR = new XHRConnection();
	XHR.appendData("id_dest", id_membre_dest);
	XHR.sendAndLoad("xml_coup_coeur.php", "POST", doSendCC);

}



function doConnect(obj) {
    
	var tabResult = obj.responseXML.getElementsByTagName('resultat');
	if (tabResult.length > 0) {
		//document.getElementById('loadingBar').style.display = "block";
		for (var i = 0; i < tabResult.length; i++) {
			resultat = tabResult.item(i); 
			if (resultat.getAttribute('connexion') == 0){
				//vous n'etes pas connecté
				alert(tradConnexion);
			} 
			else {
				//vous etes connecté
				alert(tradConnexion2);
				document.getElementById('zoneconnex').style.visibility = 'hidden';
			}
		}
	}
	else {
		//document.getElementById('zoneResultats').style.visibility = 'hidden';
	}
	document.getElementById('loadingBar').style.display = "none";
}


function getConnected(){
	//id_dest
	//alert(id_membre_dest);
	document.getElementById('loadingBar').style.display = "block";
	var XHR = new XHRConnection();
	XHR.appendData("login", document.getElementById('login').value);
	XHR.appendData("pass", document.getElementById('pass').value);
	XHR.sendAndLoad("xml_connection.php", "POST", doConnect);

}


function doSendMess(obj) {
    
	var tabResult = obj.responseXML.getElementsByTagName('resultat');
	if (tabResult.length > 0) {
		
		//document.getElementById('loadingBar').style.display = "block";
		for (var i = 0; i < tabResult.length; i++) { 
			resultat = tabResult.item(i); 
			//appendOptionLast(resultat.getAttribute('envois'), resultat.getAttribute('motif'), 'select_region');
			//alert(resultat.getAttribute('motif'));
			if (resultat.getAttribute('envois') == 1){
				alert(tradMess);
			}
			else if (resultat.getAttribute('envois') == 0 && resultat.getAttribute('motif') == 1){
				alert(tradActif+'! '+tradOp+'!');
			}
			else if (resultat.getAttribute('envois') == 0 && resultat.getAttribute('motif') == 3){
				alert(tradMs+'!\n'+tradQuota+'!');
			}
			else {
				alert(tradCon+'!\n'+tradConOp+'!\n'+tradFAQ+'!');
				document.getElementById('zoneconnex').style.visibility = 'visible';
				//popup('popup_connexion.php', 'Connexion', 250, 110, 'no');
			}
			
			//verifie si le membre est logué (session), s'il ne l'est pas, montrer popup : window.open + envois coup de coeur
			//si membre logué, envoyer coup de coeur + alert client
		}
	}
	else {
		//document.getElementById('zoneResultats').style.visibility = 'hidden';
	}
	document.getElementById('loadingBar').style.display = "none";
}

function sendMessage(id_membre_dest, txtMess, divMess){
	//id_dest
	//alert(id_membre_dest);
	//alert(document.getElementById(txtMess).value);
	document.getElementById('loadingBar').style.display = "block";
	var XHR = new XHRConnection();
	XHR.appendData("id_dest", id_membre_dest);
	XHR.appendData("txt_message", document.getElementById(txtMess).value);
	XHR.sendAndLoad("xml_message.php", "POST", doSendMess);
	document.getElementById(divMess).style.visibility = 'hidden';

}



function afficherResultatsRegions(obj) {
     
	removeOptionLast('select_region');
	removeOptionLast('select_town');
	
	document.getElementById('divregions').style.display = 'block';
	document.getElementById('divvilles').style.display = 'none';
	
	// Construction des noeuds
	var tabResult = obj.responseXML.getElementsByTagName('resultat');
	if (tabResult.length > 0) {
		//document.getElementById('loadingBar').style.display = "block";
		for (var i = 0; i < tabResult.length; i++) {
			resultat = tabResult.item(i); 
			appendOptionLast(resultat.getAttribute('nomregion'), resultat.getAttribute('idregion'), 'select_region');
		}
		//document.getElementById('loadingBar').style.display = "none";
		if (theRegion != 0){
			loadDataVilles();
		}
		
	}
	else {
		//document.getElementById('zoneResultats').style.visibility = 'hidden';
	}
	document.getElementById('loadingBar').style.display = "none";
}

function afficherResultatsVilles(obj) {
     
	removeOptionLast('select_town');
	// Construction des noeuds
	document.getElementById('divvilles').style.display = 'block';
	var tabResult = obj.responseXML.getElementsByTagName('resultat');
	if (tabResult.length > 0) {
		//document.getElementById('loadingBar').style.display = "block";
		//alert(document.getElementById('loadingBar').style.display);
		for (var i = 0; i < tabResult.length; i++) {
			resultat = tabResult.item(i); 
			appendOptionLast(resultat.getAttribute('nomville'), resultat.getAttribute('idville'), 'select_town');
		}
		//document.getElementById('loadingBar').style.display = "none";
	}
	else {
		//document.getElementById('zoneResultats').style.visibility = 'hidden';
	}
	document.getElementById('loadingBar').style.display = "none";
}


function hideDiv(theDiv){
	document.getElementById(theDiv).style.visibility = 'hidden';	
}

function showDiv(theDiv){
	hideDivMess();
	document.getElementById(theDiv).style.visibility = 'visible';	
}


function hideDivMess(){
	var tabDiv = document.getElementsByTagName('div');
	for (var i = 0; i < tabDiv.length; i++){
		var nomDiv = tabDiv[i].id;
		if (nomDiv.substring(0,7) == "divmess" || nomDiv.substring(0,7) == "divprof" || nomDiv.substring(0,10) == "divcredits"){
			//alert('a te voila!');
			tabDiv[i].style.visibility = 'hidden';
		}
	}
}



// Déclaration de la fonction qui lance la recherche
function loadDataRegions() {
     // Création de l'objet
	 document.getElementById('loadingBar').style.display = "block";
     var XHR = new XHRConnection();
     XHR.appendData("pays", document.getElementById('pays').options[document.getElementById('pays').selectedIndex].value);
     XHR.sendAndLoad("xml_regions.php", "POST", afficherResultatsRegions);
}


function loadDataVilles() {
     // Création de l'objet
	 document.getElementById('loadingBar').style.display = "block";
     var XHR = new XHRConnection();
     XHR.appendData("select_region", document.getElementById('select_region').options[document.getElementById('select_region').selectedIndex].value);
     XHR.sendAndLoad("xml_villes.php", "POST", afficherResultatsVilles);
}



function removeOptionLast(select_name){
  var elSel = document.getElementById(select_name);
  while (elSel.length > 1){
    elSel.remove(elSel.length - 1);
  }
}


function appendOptionLast(texte, id, select_name){
	var elOptNew = document.createElement('option');
	elOptNew.text = texte;
	elOptNew.value = id;
	var elSel = document.getElementById(select_name);
	
	try {
		elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
	}
	catch(ex) {
		elSel.add(elOptNew); // IE only
	}
	if (elOptNew.value == theRegion && select_name == 'select_region'){
		elOptNew.selected = true;	
	}
	if (elOptNew.value == theTown && select_name == 'select_town'){
		elOptNew.selected = true;	
	}
  
}


function ShowHide(myDiv, myDiv2){
	document.getElementById(myDiv).style.display = 'none';
	document.getElementById(myDiv2).style.display = 'block';
}

var errors;
document.MM_returnValue = (errors == '');

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+tradContainMail+'.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' doit contenir un nombre.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' '+tradContainNumber+' '+min+' '+tradAnd+' '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' '+tradRequis+'.\n'; }
  } 
  if (!document.getElementById('check_contrat').checked){
	  errors += '- '+tradTermOfUse+'.\n';
  }
  if (parseInt(document.getElementById('from_age').options[document.getElementById('from_age').selectedIndex].value) > parseInt(document.getElementById('to_age').options[document.getElementById('to_age').selectedIndex].value)){
	  errors += '- '+tradTrancheAge+'.\n';
  }
  var chaine_pays = document.getElementById('pays').options[document.getElementById('pays').selectedIndex].value;
  //alert (chaine_pays);
  if (chaine_pays.substring(0,1) == "_" || chaine_pays == "-1"){
	  errors += '- '+tradCountry+'.\n';
  }
  
  //alert(document.getElementById('NAISSANCE_MOIS_PERSO').options[document.getElementById('NAISSANCE_MOIS_PERSO').selectedIndex].value);
  
  if (parseInt(document.getElementById('NAISSANCE_JOUR_PERSO').options[document.getElementById('NAISSANCE_JOUR_PERSO').selectedIndex].value) == 0 || parseInt(document.getElementById('NAISSANCE_MOIS_PERSO').options[document.getElementById('NAISSANCE_MOIS_PERSO').selectedIndex].value) == 0 || parseInt(document.getElementById('NAISSANCE_ANNEE_PERSO').options[document.getElementById('NAISSANCE_ANNEE_PERSO').selectedIndex].value) == 0){
	  errors += '- '+tradDateNaissance+'.\n';
  }
  
  if (errors) alert(tradError+' :\n'+errors);
  if (errors == ''){
	document.forms[0].submit();
  }
  document.MM_returnValue = (errors == '');
}



function MM_validateFormNormal() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateFormNormal.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' '+tradContainMail+'.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' doit contenir un nombre.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' '+tradContainNumber+' '+min+' '+tradAnd+' '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' '+tradRequis+'.\n'; }
  } 
  
 
  
  if (errors) alert(tradError+' :\n'+errors);
  if (errors == ''){
	document.forms[0].submit();
  }
  document.MM_returnValue = (errors == '');
}





function popup(url, titre, largeur, hauteur, scroll){
	window.open(url, titre, 'width=' + largeur + ', height=' + hauteur + ', scrollbars=' + scroll);
}



