var nomCBPAYS = "CB_PAYS";
var nomCBFORMULES = "CB_TITRE";
var urlPHP='http://rubis.artsetvie.com/AV_WebSVC/XMLListeDestinations.php';
var typeAccess='xml';

document.observe("dom:loaded", function() {   
  $(nomCBPAYS).options[0].selected=true; 

  $(nomCBPAYS).observe('change', function (){
    if ($(nomCBPAYS).selectedIndex <= 0) { return false;}    
    PHP_RemplirCombo(nomCBFORMULES, 'LISTE_URL',$(nomCBPAYS).getValue());
  }); 
  
  $('B2').observe('click', function (){
    if ($(nomCBFORMULES).getValue() != '' && $(nomCBFORMULES).selectedIndex>0) {
      location.href=$(nomCBFORMULES).getValue(); 
    }
  }); 

}); 

// EXE_ListeAjoute: ajoute un élément à une liste (un select en HTML)
// ------------------------------------------------------------------------------------------------
// Paramètres :
//	l : objet liste
//	v : valeur ajouté
//  ov: valeur de l'option (facultatif: indice de l'élement par défaut)
// Variables locales :
//	t : tableau des valeurs a ajouter
function ListeAjoute(l,v,ov)
{
	var t;
	// Recherche le délimiteur (RC)
	v = v.replace(/\r\n/g, "\n");
	if (v.search(/\n/) != -1)
	{
		t = v.split("\n");
		if (ov!=null) {
		  ov = ov.replace(/\r\n/g, "\n");
		  tv= ov.split("\n");
		}
		var i;
		for (i = 0; i < t.length; i++)
		{
  			// On créé une options que l'on ajoute à la fin
      if (!ov || ov=='')
      {
  			l.options[l.length]=new Option(t[i], l.length+1);
  		}
      else {
  			l.options[l.length]=new Option(t[i], tv[i]);
      }
    }
	}
	else
	{
		// On créé une options que l'on ajoute à la fin
		if (!ov) 
    {
      //alert('Ajout1 en ' + l.length +' v  = ' + v);
      l.options[l.length]=new Option(v, l.length+1);
    }
    else {
      //alert('Ajout2 en ' + l.length +' ov  = ' + ov + '; v = ' + v);
      l.options[l.length]=new Option(v, ov);
    }
	}
}

// ListeSupprimeTout
// ------------------------------------------------------------------------------------------------
// Paramètres :
//	l: liste à vider
//	Retour : aucun
function ListeSupprimeTout(l) { l.options.length=0; }

function PHP_RemplirCombo(nomCombo, pAction, pVAR)
{
  ListeSupprimeTout($(nomCombo));
  ListeAjoute($(nomCombo),'... chargement...','');
  $(nomCombo).readOnly=true;
  $(nomCombo).disabled=true;
//Appel du script sur l'hôte distant
  sARGS="?P_ACTION="+pAction;
  sARGS+="&NomCOMBO="+nomCombo;
  sARGS+="&p_typeaccess="+typeAccess;
  if (pVAR!=null) sARGS+="&P_PAYS="+pVAR;
  var newScript = new Element('script', { 'src': urlPHP + sARGS});
  document.body.appendChild(newScript);
}