
var cp="";

function getCity(input){
	getCity(input, null)
}
function getCity(input, param){
		var selectPays=$('eltPays');
		var selectVille=$('eltVille');
		
		// CONSTANTES:
		SAISIR=1;
		CHARGEMENT=2;
		ERR_CODE=3;
		
		// AFFICHAGE DES INFORMATIONS D'ETATS
		this.show=function(code){
			switch(code){
				case 1:
					selectVille.options.length = 0;
					selectVille.options[selectVille.options.length] = new Option("Saisir un code postal","");
					selectPays.options.length = 0;
					selectPays.options[selectPays.options.length] = new Option("-----------------------","");					
					break;
				case 2:
					selectVille.options.length = 0;
					selectVille.options[selectVille.options.length] = new Option("Chargement des villes...","");
					break;
				case 3:
					selectVille.options.length = 0;
					selectVille.options[selectVille.options.length] = new Option("Code postal invalide","");
					break;
			}
		}
		// CODE POSTAL SEULEMENT NUMERIQUE ?
		if (input.value.search(/^[0-9]*$/) == -1){
			show(ERR_CODE);
			return 0;
		}
		else{
			// CODE POSTAL COMPLET ?
			if (input.value.length==5){
				// CODE POSTAL DIFFERENT DE L'ANCIENNE VALEUR
				if (cp!=input.value){
					show(CHARGEMENT);
					var url = "/particuliers/getCity.php"
					new Ajax.Request("/particuliers/getCity.php", {
						method: 'post',
						parameters:{
							cp:input.value
						},
						onSuccess: function(transport){
							cp=input.value;
							//alert(transport.responseText);
							// SI UN CODE POSTAL CORRESPOND A UNE VILLE
							if(transport.responseText!=0){
								data=transport.responseText.evalJSON();
								selectVille.options.length = 0;
								for(elt in data)
									if (typeof(data[elt])=="object"){
										var oSelect = false;
										if (param==data[elt]['ville']) {
											oSelect=true;
										}
										selectVille.options[selectVille.options.length] = new Option(data[elt]['ville'],data[elt]['ville'],oSelect, oSelect);
									}
								selectPays.options.length = 0;
								selectPays.options[selectPays.options.length] = new Option("France","France");
								selectPays.style.background='#FFFFFF';
								selectVille.style.background='#FFFFFF';
								selectVille.focus();
							}
							// CODE POSTAL INCORREXT
							else{
								show(ERR_CODE);
								return 0;
							}
						}
					});	
				}
			}
			// SAISIR UN CODE POSTAL
			else{
				show(SAISIR);
				return 0;
			}
		}
	}


function slideBar(eltImg,eltDesc,input){
	var formulaire=document.formulaire;
	var value=1;
	var eltImg=$(eltImg);
	var eltDesc=$(eltDesc);
	var data=new Array("Forfait Économique","Forfait Sécurité","Forfait Équilibre","Forfait Optimum","Forfait Maximum");	
	this.plus=function(){
		if (value<5){
			value++;
			eltImg.src='images/barren'+value+'.jpg';
			eltDesc.innerHTML=data[value-1];
			formulaire[input].value="n"+value;
		}
	}
	this.moins=function(){
		if (value>1){		
			value--;
			eltImg.src='images/barren'+value+'.jpg';
			eltDesc.innerHTML=data[value-1];
			formulaire[input].value="n"+value;
		}
	}
	
}

