// Début de fonctions javascript qui etaient dans AskInfosUser.tpl (Partie Admin)
function formValidation(formulaire) { 

	if ( $("user_lastname").value == "" ) {
		alert( "Merci d'inscrire votre nom" );
		$("user_lastname").focus();
		return false;
	}
	if ( $("user_address1").value == "" ) {
		alert( "Merci d'entrer une adresse" );
		$("user_address1").focus();
		return false;
	}
	if ( $("user_postcode").value == "" ) {
		alert( "Le code postal est obligatoire." );
		$("user_postcode").focus();
		return false;
	} else if ( !isInteger( $("user_postcode").value ) ) {
		alert( "Le code postal doit être en chiffre." );
		$("user_postcode").focus();
		return false;
	} else if ( $("user_postcode").value.length != 5 ) {
		alert( "Le code postal doit avoir 5 chiffres." );
		$("user_postcode").focus();
		return false;
	}
	if ( $("user_phone").value == "" ) {
		alert( "Le numero de téléphone est obligatoire." );
		$("user_phone").focus();
		return false;
	} else if ( !isInteger( $("user_phone").value ) ) {
		alert( "Le numero de téléphone doit être en chiffre" );
		$("user_phone").focus();
		return false;
	} else if ( $("user_phone").value.length != 10 ) {
		alert( "Le numero de téléphone doit avoir 10 chiffres" );
		$("user_phone").focus();
		return false;
	}
	if ($("user_email").value == "" ) {
		alert( "L'adresse email est obligatoire" );
		$("user_email").focus();
		return false;
	} else if ( !(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test($("user_email").value) ) ) {
		alert( "Adresse email invalide." );
		$("user_email").focus();
		return false;
	}
	return true;
}

// Activer et désactiver un user
function Activer_Desactiver (div1, div2, id, type) {
	if (document.getElementById(div1).innerHTML == 'Désactivé') { 
		document.getElementById(div1).innerHTML = 'Activé';
		document.getElementById(div2).innerHTML = '- Désactiver';
		affPageAJax('Gest'+type+'s','message','action|activer#'+type+'_id|'+id+'#');
	} else { 
		document.getElementById(div1).innerHTML = 'Désactivé';
		document.getElementById(div2).innerHTML = '- Activer';
		affPageAJax('Gest'+type+'s','message','action|desactiver#'+type+'_id|'+id+'#');
	}
}


// Gestion du div d'attribution
function Gestion_Attribution(div1, div3, id) {
	if(document.getElementById(div1).innerHTML == 'Activé' && document.getElementById(div3).innerHTML == 'Validé') {
		display_div('attribution'+id);
	} else {
		notdisplay_div('attribution'+id);
	}
}

function isInteger( s ) {
	var i;
	for ( i = 0; i < s.length; i++ ) {  
	// Check that current character is number.
		var c = s.charAt( i );
		if ( ( ( c < "0" ) || ( c > "9" ) ) ) {
			return false;
		}
	}
	// All characters are numbers.
	return true;
}
	

function initEmail(form) {
	document.getElementById("autre_email") = document.getElementById("autre_email").value;
}

function copyEmail(form) {
	if(form.other_address.checked) {
		document.getElementById("autre_email").value = document.getElementById("user_email").value;
	} else {
		document.getElementById("autre_email").value = "";
	}
}
// Fin de fonctions javascript qui etaient dans AskInfosUser.tpl (Partie Admin)


function ShowSubClass(class_id) {
	if ($('select_class'+class_id) != undefined) {
		$('ScrollSubCat').innerHTML=$('select_class'+class_id).innerHTML;
	}
}



function InitFormQuestion2() {
	pays = $("post_project[country_id]");
	CP = $("user_postcode");
	ville = $("user_city");
	ville.divSelect = "SelectUserCity";
	ville.divInput = "InputUserCity";
	ville.verify = function (n) {
		if (ville.value=='other') {
			ville.value='';
			$(ville.divSelect).style.display="none";
			$(ville.divInput).style.display="block";
		}
	}
	CP.exvalue = CP.value;
	CP.verify = function (n) {

	if (CP.value.length<0) {
			ville.disabled=true;
			ville.value='';
		} else if(CP.value.length==5) {
			ville.disabled=false;
			if (CP.exvalue != CP.value) {
				SearchVilleByCP(CP.value,ville.divSelect,"post_user[user_citySelect]2","user_city");
				$(ville.divInput).style.display='none';
				$(ville.divSelect).style.display='block';
				CP.exvalue = CP.value;
				ville.verify();
			}
		}
	}
	CP.verify();
	
	/*Fin initialisation*/
	CP.onkeyup = function (n) {
		CP.verify(); 
	}
	
	$('divVille').onclick = function (n) {
		ville.verify();
	}
}


function InitFormCompte() {
	CP = $("user_postcode");
	ville = $("user_city");
	ville.divSelect = "SelectUserCity";
	ville.divInput = "InputUserCity";
	ville.verify = function (n) {
		if (ville.value=='other') {
			ville.value='';
			$(ville.divSelect).style.display="none";
			$(ville.divInput).style.display="block";
		}
	}
	CP.exvalue = CP.value;
	CP.verify = function (n) {
		if (CP.value.length < 1) {
			ville.disabled=true;
			ville.value='';
		} else {
			ville.disabled=false;
			if (CP.exvalue != CP.value && CP.value.length == 5) {
				SearchVilleByCP(CP.value,ville.divSelect,"post_user[user_citySelect]2","user_city");
				$(ville.divInput).style.display='none';
				$(ville.divSelect).style.display='block';
				CP.exvalue = CP.value;
				ville.verify();
			}
		}
	}
//	CP.verify();
	
	/*Fin initialisation*/
	CP.onkeyup = function (n) {
		CP.verify(); 
	}
	
	$('divVille').onclick = function (n) {
		ville.verify();
	}
}

function OnChangeCity(oSelect, sType, sInputIdFinal) {
	ville = $("selectVille_"+sType);	
	ville.divSelect = "SelectUserCity_"+sType;
	ville.divInput = "InputUserCity_"+sType;
	if (ville.value == 'other') {
		$(sInputIdFinal).value = '';
		$(ville.divSelect).style.display="none";
		$(ville.divInput).style.display="block";	
	}
	//quand on select une ville dans le select creer par le fonction en dessous met cette valeur dans le VRAI input bien nomé
	$(sInputIdFinal).value = oSelect.value;
}


function verifcp() {
	pays = $("post_project[country_id]");
	if (pays.value == '74') {
		CP = $("post_project[project_postcode]");
		ville = $("post_project[project_city]");
		ville.divSelect = "SelectUserCity_project";
		ville.divInput = "InputUserCity_project";
		if (CP.value.length<0) {
				ville.disabled=true;
				ville.value='';
		} else if(CP.value.length==5) {
			ville.disabled=false;
			if (CP.exvalue != CP.value) {
				SearchVilleByCP(CP.value, ville.divSelect, 'project', 'post_project[project_city]');
				$(ville.divInput).style.display='none';
				$(ville.divSelect).style.display='block';
				CP.exvalue = CP.value;
				if (ville.value=='other') {
					ville.value='';
					$(ville.divSelect).style.display="none";
					$(ville.divInput).style.display="block";
				}
			}
		}
	}
}


function verifcpuser() {
	pays = $("post_user[country_id]");
	if (pays.value == '74') {
		CP = $("post_user[user_postcode]");
		ville = $("post_user[user_city]");
		ville.divSelect = "SelectUserCity_user";
		ville.divInput = "InputUserCity_user";
		if (CP.value.length<0) {
			ville.disabled=true;
			ville.value='';
		} else if(CP.value.length==5) {
			ville.disabled=false;
			if (CP.exvalue != CP.value) {
				SearchVilleByCP(CP.value, ville.divSelect, 'user', 'post_user[user_city]');
				$(ville.divInput).style.display='none';
				$(ville.divSelect).style.display='block';
				CP.exvalue = CP.value;
				if (ville.value=='other') {
					ville.value='';
					$(ville.divSelect).style.display="none";
					$(ville.divInput).style.display="block";
				}
			}
		}
	}
}


function VerifCpModificationProject() {
	pays = $("post_user[country_id]");
	if (pays.value == '74') {
		CP = $("post_user[project_postcode]");
		ville = $("post_user[project_city]");
		ville.divSelect = "SelectUserCity_project";
		ville.divInput = "InputUserCity_project";
		if (CP.value.length<0) {
			ville.disabled=true;
			ville.value='';
		} else if(CP.value.length==5) {
			ville.disabled=false;
			if (CP.exvalue != CP.value) {
				SearchVilleByCP(CP.value, ville.divSelect, 'project', 'post_user[project_city]');
				$(ville.divInput).style.display='none';
				$(ville.divSelect).style.display='block';
				CP.exvalue = CP.value;
				if (ville.value=='other') {
					ville.value='';
					$(ville.divSelect).style.display="none";
					$(ville.divInput).style.display="block";
				}
			}
		}
	}
}

function VerifCpCreateModifUser() {
	CP = $("user_postcode");
	ville = $("user_city");
	ville.divSelect = "SelectUserCity_user";
	ville.divInput = "InputUserCity_user";
	if (CP.value.length<0) {
		ville.disabled=true;
		ville.value='';
	} else if(CP.value.length==5) {
		ville.disabled=false;		
		if (CP.exvalue != CP.value) {
			SearchVilleByCP(CP.value, ville.divSelect, 'user', 'user_city');
			$(ville.divInput).style.display='none';
			$(ville.divSelect).style.display='inline';
			CP.exvalue = CP.value;
			if (ville.value=='other') {
				ville.value='';
				$(ville.divSelect).style.display="none";
				$(ville.divInput).style.display="inline";
			}
		}
	}
}
/*
function SearchVilleByCP(CP,div,name,id) {
	var url = 'ajax.php?page=SearchByCP&CP='+CP;
	new Ajax.Request(url, {
	  method: 'get',
	  onComplete: function(transport) {
		var nodeListe = transport.responseXML;
		var nodesGeocode = nodeListe.firstChild.getElementsByTagName('geocode');
		
		var output = '<select name="'+name+'" id="Form'+id+'" onchange="$(\''+id+'\').value = this.value"><option value="choose" selected>Choisissez</option>';
		$A(nodesGeocode).each(function (nodeGeocode) {
			var attributes = nodeGeocode.attributes;
			var id = attributes.getNamedItem('id').nodeValue;
//			var nodeLowerarticle = nodeGeocode.getElementsByTagName('geocode_lowerarticle')[0];
//			var lowerarticle = nodeLowerarticle.firstChild.nodeValue;
            var nodeLowername = nodeGeocode.getElementsByTagName('geocode_lowername')[0];
            var lowername = nodeLowername.firstChild.nodeValue;
			output += '<option value="'+lowername+'">'+lowername+'</option>';
		});
		output+= '<option value="other">Autre Choix</option></select>';
		$(div).innerHTML = output;
	  }
	});
}
*/

//C CETTE PROCEDUR QUI EST utiliser pas cellequi est au dessus a voir plus tard 
function SearchVilleByCP(CP, div, sType, sInputIdFinal, bHideDivGeocode) {
	var url = 'ajax.php?page=SearchByCP&CP='+CP;
	new Ajax.Request(url, {
	  method: 'get',
	  onComplete: function(transport) {
		var nodeListe = transport.responseXML;
		var nodesGeocode = nodeListe.firstChild;
		var nodesGeocode = nodeListe.getElementsByTagName('geocode');

		var sHideGeocode = '';
		if (bHideDivGeocode) {
			sHideGeocode = "if (this.value != 'other' && $('no_geocode') != undefined)  $('no_geocode').style.display='none';if (this.value != 'other' && this.value != 'choose' && $('refuse') != undefined)  $('refuse').disabled = false;if (this.value != 'other' && this.value != 'choose' && $('accept') != undefined)  $('accept').disabled = false;if (this.value != 'other' && this.value != 'choose' && $('not_define') != undefined)  $('not_define').disabled = false;";
		}
		var output = "<select class='select1' id='selectVille_"+sType+"' onchange=\" "+sHideGeocode+" OnChangeCity(this, '"+sType+"','"+sInputIdFinal+"');\"><option value=\"choose\" selected=\"selected\">Choisissez</option>";
		$A(nodesGeocode).each(function (nodeGeocode) {
			var attributes = nodeGeocode.attributes;
			var id = attributes.getNamedItem('id').nodeValue;
			var nodeLowername = nodeGeocode.getElementsByTagName('geocode_lowername')[0];
			var lowername = nodeLowername.firstChild.nodeValue;
			output += '<option value="'+lowername+'">'+lowername+'</option>';
		});
		output+= '<option value="other">Autre Choix</option></select>';
		$(div).innerHTML = output;
	  }
	});
}

function CheckForm(oForm) {
	var iCount = oForm.elements.length;
	for (i=0; i<iCount; i++) {
		if (oForm.elements[i].value == '') return false; 
	}
	return true;
}