////////////////////////////////// Gestion Appel Service Web ///////////////////////////////////////
function updateComboBox (idSelect,data,selectedKey,idChargement) {

	var monSelect = document.getElementById(idSelect);
	monSelect.options.length = 0;
	var selected;
	i=0;
	for (var key in data) {
		// permet de choisir le champs à definir par defaut
		if (selectedKey == key) {
			selected = i;
		}
		monSelect.options[monSelect.length] = new Option(data[key],data[key]);
		i++;
	}
	// permet de positionner la combo au bon champs
	monSelect.selectedIndex = selected;
	document.getElementById(idChargement).style.visibility = "hidden";
}



function updateCityState(idCP, idCommune, idAlerte, idChargement) {
	var zipValue = document.getElementById(idCP).value;
	if (zipValue != '') {
		if (errNum(zipValue) || zipValue.length != 5) {
			Msg = "Le code postal doit &ecirc;tre renseign&eacute; et num&eacute;rique";
			showError (idCP, Msg, idAlerte);
			document.getElementById(idCP).focus();
			return false;
		}
		else {
			document.getElementById(idChargement).style.visibility = "visible";
			var urlSWCP = "/AppelSwCommune/getCommunes.ashx?cp=";
			http.open("GET", urlSWCP + escape(zipValue), true);
			http.onreadystatechange = function handleHttpResponse() {
							if (http.readyState == 4) {
								results = http.responseText.split(",");
								if (results != '') {
									updateComboBox (idCommune, results, results[0], idChargement);
									restoreStyle (idCP, idAlerte);
								}
								else {
									Msg = "Le code postal est incorrect";
									showError (idCP, Msg, idAlerte);
									document.getElementById(idChargement).style.visibility = "hidden";
								}
							}
						  }
			http.send(null);
			
		}
	}
}

function getHTTPObject() {

  var xmlhttp;
  /*@cc_on

  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}

var http = getHTTPObject(); // We create the HTTP Object