function getListCommunes(cp, idPopupCom)
{
	var listVilles;
	var idcontext = document.getElementById("idcontext").value;
	var url = idcontext+"/FormWizard?ACTION=getVilles2&codePostal="+cp;
	var request = new Ajax.Request(url,{method: 'get',asynchronous: false});  			
  	listVilles = eval(request.transport.responseText);
	var listCommune = "<select id='devis_commune' onchange=\"isInseeIssueAndClosePopup('devis_commune','"+cp+"','"+idPopupCom+"');\" >";
	listCommune = listCommune + "<option value=''>- Choisissez -</option>";
	for(var i=0; i<listVilles.size(); i++) {
		listCommune = listCommune + "<option value='"+listVilles[i]+"'>"+listVilles[i]+"</option>";
	}
	listCommune = listCommune +"</select>";
	return listCommune;
}
function getCommuneLayer(cp,formulaire){
	if(cp==null||cp==''){
		alert('VEUILLEZ RENSEIGNER UN CODE POSTAL');
		return;
	}else{
		if(isNaN(cp)==true){
			alert('LE CODE POSTAL DOIT ETRE NUMERIQUE');
			return;
		}else{
			if(cp.length!=5){
				alert('LE CODE POSTAL DOIT COMPORTER 5 CHIFFRES');
				return;
			}else{
				var listResult;
				var idcontext = document.getElementById("idcontext").value;
				var url = idcontext+"/AjaxContact?ACTION=CHECKCP&codePostal="+cp;
				var request = new Ajax.Request(url,{method: 'get',asynchronous: false});
				listResult = eval(request.transport.responseText);
				var valide = listResult[0];
				var trigramme = listResult[1];
				if (valide=='KO') {
					alert("LE CODE POSTAL SAISI EST INVALIDE.");
					return;
				}else{
					if(trigramme==''){
						win = new Window({title: "Veuillez s&eacute;lectionner votre commune dans la liste :", width:410, height:60, zIndex:100, destroyOnClose:true, maximizable:false,closable:false, minimizable:false, recenterAuto:true, onClose: function() {if(formulaire=='mappy' || formulaire=='mappy2'){submitMappy(formulaire);}else{document.forms[formulaire].submit();}}});
						win.setLocation(0,0); 
						var listCommune = getListCommunes(cp, win.getId()); 
						var contenu = "<TABLE width='410' border='0' cellspacing='1' cellpadding='0' background=''>"+
										 "<TR bgcolor='#FFFFFF' style='color:BLACK;'>"+
							              		"<TD align='center' width='160' height='20'></TD>"+
							              		"<TD></TD>"+
							              "</TR>"+
							              "<TR bgcolor='#FFFFFF' style='color:BLACK;'>"+
							              		"<TD align='center' width='160' height='20' style='font-weight: bold'>Commune</TD>"+
							              		"<TD align='center' height='20'>"+listCommune+"</TD>"+
							              "</TR>"+
						                "</TABLE>";
						win.getContent().update(contenu);
						win.showCenter(true,350,300);
					}else{
						positionDepart(trigramme,null, null);
						if(formulaire=='mappy' || formulaire=='mappy2'){
							submitMappy(formulaire);
						}else{
							document.forms[formulaire].submit();
						}
					}
				}
			}
		}
	}
}
function isInseeIssueAndClosePopup(idListCommune, cpSaisi, idPopupCom){
	if(document.getElementById('trigcr')!=null){
		var i = document.getElementById(idListCommune).selectedIndex;
		var nomComChoisi = document.getElementById(idListCommune).options[i].value;
		var idcontext = document.getElementById('idcontext').value;
		var url = idcontext+"/FormWizard?ACTION=isDepart40ByComCp&nomCom="+nomComChoisi+"&codePostal="+cpSaisi;
		var request = new Ajax.Request(url,{method: 'get',asynchronous: false});  			
		var isDepart40 = eval(request.transport.responseText);
		if(isDepart40!=null && isDepart40!= '' && isDepart40.size()>0 && isDepart40[0]==true){
			getLayerSelectCaisse(idPopupCom);
		}else{
			positionDepart(isDepart40[0],null, idPopupCom);
		}
	}
}
function getLayerSelectCaisse(idPopupCom){
	win = new Window({title: "Deux de nos caisses sont pr&eacute;sentes dans votre commune, souhaitez-vous &ecirc;tre attach&eacute;(e) &agrave; :", width:610, height:80, zIndex:100, destroyOnClose:true, maximizable:false,closable:false, minimizable:false, recenterAuto:true});
	win.setLocation(0,0); 
	var contenu = "<TABLE width='610' border='0' cellspacing='1' cellpadding='0' background=''>"+
		              "<TR bgcolor='#165D50' style='color:white;'>"+
		              		"<TD align='center' width='610' height='20'></TD>"+
		              "</TR>"
	              	  +"<TR style='color:black;' onClick=\"positionDepart('GCA','"+win.getId()+"','"+idPopupCom+"');\" onMouseOver=\" this.style.cursor='hand'; this.bgColor = '#FF9300'; \" onMouseOut =\" this.bgColor = 'transparent';\">"+
	                		"<TD align='center' width='610' height='20'>Groupama Centre Atlantique</TD>"+
	                	"</TR>"
	                  +"<TR style='color:black;' onClick=\"positionDepart('GOC','"+win.getId()+"','"+idPopupCom+"');\" onMouseOver=\" this.style.cursor='hand'; this.bgColor = '#FF9300'; \" onMouseOut =\" this.bgColor = 'transparent';\">"+
	                		"<TD align='center' width='610' height='20'>Groupama d'Oc</TD>"+
	                	"</TR>"+
	                "</TABLE>";
	win.getContent().update(contenu);
	win.showCenter(true,350,200);
}
function positionDepart(trig, idPopup, idPopupCom){
	if(trig !=null && trig!=''){
		if(document.getElementById('trigcr')){
			for(i=0; i<document.getElementsByName('trigcr').length; i++) {
				document.getElementsByName('trigcr')[i].value =trig;
			}
		}
	}
	if(idPopup!=null && idPopup !=''){
		Windows.close(idPopup);	
	}
	if(idPopupCom!=null && idPopupCom !=''){
		Windows.close(idPopupCom);	
	}
}
function submitMappy(formulaire){
	var codeBusiness ="codeMetier";
	var agency = "agence";
	if(formulaire=='mappy2'){
			codeBusiness ="codeMetier2";
			agency = "agence2";
		}
	if(document.getElementById(codeBusiness).value!=1){
		var listResult;
		var idcontext = document.getElementById("idcontext").value;
		var url = idcontext+"/AjaxContact?ACTION=GETURLAGENCEPRO&codePostal="+document.getElementById(agency).value+"&codeMetier="+document.getElementById(codeBusiness).value+"&activeRegion="+document.getElementById("trigcr").value;
		var request = new Ajax.Request(url,{method: 'get',asynchronous: false});
		listResult = eval(request.transport.responseText);
		var actif = listResult[0];
		if(actif=="CP_INVALIDE"){
			alert("Le code postal saisi n'existe pas");
			return false;
		}
		
		var url = listResult[1];
		if(actif=="OUI" && url!=null && url!=""){
			document.forms[formulaire].action = url;
			document.forms[formulaire].method = "post";
		}
	}
	document.forms[formulaire].submit();
}