var xhr_object;

function ClearOptions(OptionList) 
{
   // Always clear an option list from the last entry to the first
   for (x = OptionList.length; x >= 0; x = x - 1) 
   {
      OptionList[x] = null;
   }
}

function AddToOptionList(OptionList, OptionValue, OptionText) 
{
   // Add option to the bottom of the list
   OptionList[OptionList.length] = new Option(OptionText, OptionValue);
}

function GetXmlHttpObject(handler)
{
	try 
	{
		var xhr = new XMLHttpRequest();
	}
	catch(a) 
	{
		try	
		{
			var xhr = new ActiveXObject('Microsoft.XMLHTTP');
		}
		catch(b) 
		{
			var xhr = null;
		}
	}
	if(xhr) 
	{
		xhr.onreadystatechange = handler;
	}
	return xhr;
}


function SendGetRequest(xhr_url,CallBack) 
{
	xhr_object = GetXmlHttpObject(CallBack);
	// On ouvre la requete vers la page désirée
	xhr_object.open( 'GET', xhr_url, true );
	// On envoi la requete
	xhr_object.send( null );
}

function CallBackLocalite() 
{
var selecttopopulate = document.getElementsByName('localite')[0];
var response;
var localites = new Array();
var ispopulated = false;

	// Sur le retour de la requete, on teste son état
	if ( xhr_object.readyState == 4 || xhr_object.readyState=="complete") 
	{
	response = xhr_object.responseText;
	localites = response.split(',');
		
		for(var i=0;i<localites.length;i++)
		{
			AddToOptionList(selecttopopulate,localites[i],localites[i]);
			ispopulated = true;
		}
		if(ispopulated == false)
		AddToOptionList(selecttopopulate,"Error","Error");			
	}
}
function CallBackLocaliteLivraison() 
{
var selecttopopulate = document.getElementsByName('localite_livraison')[0];
var response;
var localites = new Array();
var ispopulated = false;

	// Sur le retour de la requete, on teste son état
	if ( xhr_object.readyState == 4 || xhr_object.readyState=="complete") 
	{
	response = xhr_object.responseText;
	localites = response.split(',');
		
		for(var i=0;i<localites.length;i++)
		{
			AddToOptionList(selecttopopulate,localites[i],localites[i]);
			ispopulated = true;
		}
		if(ispopulated == false)
		AddToOptionList(selecttopopulate,"Error","Error");			
	}
}

function populate_localite()
{
var pays = document.getElementsByName('pays')[0];
var code_postal = document.getElementsByName('code_postal')[0];
var localite = document.getElementsByName('localite')[0];

	if(pays.value != "NL")
	{
	//ClearOptions(localite);
		document.getElementById('div_localite').innerHTML = "<select name='localite'></select>";
	
		if(code_postal.value.length > 3)
		{	
		SendGetRequest('/getlocalite.php?code_pays='+pays.value+'&code_postal='+code_postal.value,CallBackLocalite);	
		}
	}
	else
	{
		document.getElementById('div_localite').innerHTML = "<input type='text' name='localite' />";
	}
}

function populate_localite_livraison()
{
var pays = document.getElementsByName('pays_livraison')[0];
var code_postal = document.getElementsByName('code_postal_livraison')[0];
var localite = document.getElementsByName('localite_livraison')[0];


	if(pays.value != "NL")
	{
	//ClearOptions(localite);
		document.getElementById('div_localite_livraison').innerHTML = "<select name='localite_livraison'></select>";
	
		if(code_postal.value.length > 3)
		{	
		SendGetRequest('/getlocalite.php?code_pays='+pays.value+'&code_postal='+code_postal.value,CallBackLocaliteLivraison);	
		}
	}
	else
	{
		document.getElementById('div_localite_livraison').innerHTML = "<input type='text' name='localite_livraison' />";
	}
}

