var gihttprequestjson;
function sendajax(s_url, s_onresponse, s_data){

	gihttprequestjson = new Request({

		url: s_url,

		data: s_data,

		onRequest: function(){
			//alert("Sending");
		},

		onComplete: function(response){
			//alert('Response is done');
		},

		onSuccess: function(jsonObj){
			s_onresponse(eval( "(" + jsonObj + ")" ), jsonObj);
		},

		onFailure: function(){
			//alert('The request failed.');
		}
	});

	gihttprequestjson.send();
}

function getvillesofregion(field, id, lang){
	s_data = "id="+id+"&select="+field+"&lang="+lang;
	sendajax("http://"+window.location.hostname+"/include/getvillesofregion.aspx", displayvilles, s_data);
}

function displayvilles(jsonObj, text){
	//alert("complete\ntext:\n" + text+"\njson:\n"+jsonObj);

	//remove villes
	liste = document.getElementById(jsonObj.select);
	while(liste.options.length > 0){
		liste.removeChild(liste.options[0]);
	}

	id = 0;

	var villes = jsonObj.villes;
	if(villes.length > 0){
		villes.each(function(ville) {
			//add news
			option = document.createElement("option");
			option.value = (ville.id == 0)?"":ville.nom; //id			 
			option.text = ville.nom;
			liste.options[liste.options.length] = option;

			if(id == 0) id = ville.id;
		});
	}
}

function getprovinces(lang){
	$("waitprovince").style.display="inline";
	idpays = document.grpaidbooking_form.country.value;
	s_data = "idpays="+idpays+"&lang="+lang;
	sendajax("http://"+window.location.hostname+"/include/getprovinces.aspx", setprovinces, s_data);
}

function setprovinces(jsonObj, text){
	//alert(text);
	//remove provinces
	liste = document.getElementById("area");
	while(liste.options.length > 0){
		liste.removeChild(liste.options[0]);
	}

	var provinces = jsonObj.provinces;
	if(provinces.length > 0){
		provinces.each(function(province) {
			//add news
			option = document.createElement("option");
			option.value = province.reservitid; //id			 
			option.text = province.nom;
			liste.options[liste.options.length] = option;
		});
	}
	$("waitprovince").style.display="none";	
	document.grpaidbooking_form.area.onchange();
	document.grpaidbooking_form.dest.value = document.grpaidbooking_form.area.options[document.grpaidbooking_form.area.selectedIndex].text;
}

function getregions(lang){
	$("waitregion").style.display="inline";
	idprovince = document.grpaidbooking_form.area.value;
	idpays = document.grpaidbooking_form.country.value;
	s_data = "idprovince="+idprovince+"&idpays="+idpays+"&lang="+lang;
	sendajax("http://"+window.location.hostname+"/include/getregions.aspx", setregions, s_data);
	document.grpaidbooking_form.dest.value = document.grpaidbooking_form.area.options[document.grpaidbooking_form.area.selectedIndex].text;
}

function setregions(jsonObj, text){
	//alert(text);
	//remove regions
	liste = document.getElementById("zone");
	while(liste.options.length > 0){
		liste.removeChild(liste.options[0]);
	}

	//id = 0;

	var regions = jsonObj.regions;
	if(regions.length > 0){
		regions.each(function(region) {
			//add news
			option = document.createElement("option");
			option.value = region.reservitid; //id			 
			option.text = region.nom;
			liste.options[liste.options.length] = option;

			//if(id == 0) id = region.id;
		});
	}
	$("waitregion").style.display="none";	
	document.grpaidbooking_form.zone.onchange();
}

function getvilles(lang){
	$("waitville").style.display="inline";
	idregion = document.grpaidbooking_form.zone.value;
	idprovince = document.grpaidbooking_form.area.value;
	idpays = document.grpaidbooking_form.country.value;
	s_data = "idregion="+idregion+"&idprovince="+idprovince+"&idpays="+idpays+"&lang="+lang;
	sendajax("http://"+window.location.hostname+"/include/getvilles.aspx", setvilles, s_data);
}

function setvilles(jsonObj, text){
	//alert(text);
	//remove villes
	liste = document.getElementById("namecity");
	while(liste.options.length > 0){
		liste.removeChild(liste.options[0]);
	}

	var villes = jsonObj.villes;
	if(villes.length > 0){
		villes.each(function(ville) {
			//add news
			option = document.createElement("option");
			option.value = ville.nom; //ville.reservitid; //id			 
			option.text = ville.nom;
			liste.options[liste.options.length] = option;
		});
	}
	$("waitville").style.display="none";
}

function getyears()
{
	var d = new Date();
	d.getFullYear();
}
