var BW_theme;
var BW_formation;
var BW_List_Formation;
var domaine_id = -1;
var BW_domaine;


function onBwLoaded()
{
    BW_theme= BwGetById('theme');
    BW_formation = BwGetById('formation');
    BW_List_Formation = BwGetById('listFormation');

    if (isFillField==true && id_domaine!=null && id_theme!=null && id_formation!=null && posDomaine!=null)
    {  
	var link = "link_domaine_"+posDomaine;
	selectedDom(link,posDomaine);
        setDomaine(posDomaine,id_domaine);
        document.forms[0].theme.value = id_theme;
        setTheme(id_theme);
        //drawFormations(id_formation); 
        //alert('ok');BW_List_Formation.selectRowByValue(id_formation);alert('ok');
        setFormation(id_formation);
    }
    else
    {
        BW_theme.disable();
        BW_formation.disable();
    }
}


function setDomaine(numb,id)
{

 /*   if (eval('document.forms[0].radio_domaine_'+id+'.checked')==false)
        eval('document.forms[0].radio_domaine_'+id+'.checked=true;');*/

    if (eval('document.forms[0].radio_domaine['+numb+'].checked')==false)
        eval('document.forms[0].radio_domaine['+numb+'].checked=true;');

    var q = new BwQuery();
    q.get ('/dev/planeteformation/planeteformation/getThemes?location.id:='+locationId+'&id_domaine='+id);
    src = q.getMessage();

    // empty list
    BW_theme.options.length = 0;
    if (src.count(BW_theme.record)==-1)
    {
  		var elt = document.createElement ("option");
		elt.setAttribute ("value", "-1");
		var txt = document.createTextNode ("Aucun thème pour ce domaine");
		elt.appendChild (txt);
		BW_theme.appendChild (elt);
    }
    else
    {
        var elt = document.createElement ("option");
		elt.setAttribute ("value", "-1");
		var txt = document.createTextNode ("Sélectionnez votre thème");
		elt.appendChild (txt);
		BW_theme.appendChild (elt);
        BW_theme.drawRows(src);
        var elt = document.createElement ("option");
		elt.setAttribute ("value", "0");
		var txt = document.createTextNode ("Tous les thèmes");
		elt.appendChild (txt);
		BW_theme.appendChild (elt);

        closeExpander();
    }
    BW_theme.enable();
    
    BW_formation.options.length = 0;
    BW_formation.disable();
	BW_List_Formation.clear();
    domaine_id = id;
}

function setTheme(id)
{

    var q = new BwQuery();
    if (id=='0')
    {
        // Affiche toutes les formations
        q.get ('/dev/planeteformation/planeteformation/getFormationsName?location.id:='+locationId+'&id_domaine='+domaine_id);
    }
    else
        q.get ('/dev/planeteformation/planeteformation/getFormationsName?location.id:='+locationId+'&id_theme='+id+'&domaine_id='+domaine_id);

    src = q.getMessage();

    // empty list
    BW_formation.options.length = 0;
    if (src.count(BW_formation.record)==-1)
    {
  		var elt = document.createElement ("option");
		elt.setAttribute ("value", "-1");
		var txt = document.createTextNode ("Aucune formation pour ce domaine");
		elt.appendChild (txt);
		BW_formation.appendChild (elt);
    }
    else
    {
        var elt = document.createElement ("option");
		elt.setAttribute ("value", "-1");
		var txt = document.createTextNode ("Sélectionnez votre formation");
		elt.appendChild (txt);
		BW_formation.appendChild (elt);
        BW_formation.drawRows(src);

        var elt = document.createElement ("option");
		elt.setAttribute ("value", "0");
		var txt = document.createTextNode ("Toutes les formations");
		elt.appendChild (txt);
		BW_formation.appendChild (elt);
    }
    BW_formation.enable();

	BW_List_Formation.clear();
    if (id=='0')
    {
        // Affiche toutes les formations
    	BW_List_Formation.appendXML ('/dev/planeteformation/planeteformation/getFormations?location.id:='+locationId+'&id_domaine='+domaine_id+'&id_theme=0');
    }
    else
    	BW_List_Formation.appendXML ('/dev/planeteformation/planeteformation/getFormations?location.id:='+locationId+'&id_theme='+id);

    closeExpander();
}

function drawFormations(id)
{

    if (id==-1)
        return;

    BW_List_Formation.clear();
	
    if (id==0)
    {
        if (BW_theme.value==0)
        {
            // Toutes les formations de tous les themes du domaine
            BW_List_Formation.appendXML ('/dev/planeteformation/planeteformation/getFormations?location.id:='+locationId+'&id_domaine='+domaine_id+'&id_theme=0');
        }
        else
        {
            // Toutes les formations du theme du domaine
            BW_List_Formation.appendXML ('/dev/planeteformation/planeteformation/getFormations?location.id:='+locationId+'&id_theme='+BW_theme.value);
        }
    }
    else
	{
	    BW_List_Formation.appendXML ('/dev/planeteformation/planeteformation/getFormations?location.id:='+locationId+'&id_domaine='+domaine_id+'&id_theme='+BW_theme.value+'&id_formation='+id);
	}
    closeExpander();
}

function show()
{
    if (BW_List_Formation.getSelection().length == 0)
        return;
    setFormation(BW_List_Formation.getValue());
}

function setFormation(id)
{
	if (id == null || id == 0)
		return;

    var detail = BwGetById ("ExpDetail");
    if (!detail.view.visible())
        openExpander();

	var lbl = BwGetById ("introductionText");
	    lbl.hide();
    
    var qu = new BwQuery();
	qu.get ('/dev/planeteformation/planeteformation/getFormation?location.id:='+locationId+'&id_formation='+id);
	var msg = qu.getMessage();
		
    document.getElementById("detail").style.display = "block";

	BwGetById ("nom").setValue(msg.get("formations.formation.nom"));
	BwGetById ("lieu").setValue(msg.get("formations.formation.lieu"));
    
    
    var duree = msg.get("formations.formation.duree");
    var isDay = msg.get("formations.formation.duree_isday");
    var unit = "";
    if (isDay=="1")
        unit = (duree=="1")?"jour":"jours";
    else if (isDay=="0")
        unit = (duree=="1")?"heure":"heures";
    
    
	BwGetById ("duree").setValue(duree+" "+unit);
    var msg_date_detail = msg.get("formations.formation.date_detail"); 
    if (msg_date_detail)
    {
    	BwGetById ("date_detail").setValue(msg_date_detail);
    }
    else
    	BwGetById ("date_detail").setValue("");
    

    BwGetById ("nom_organisme").setValue(msg.get("formations.formation.nom_organisme"));
    //BwGetById ("nom_organisme").setUrl("javascript:openOrganisme("+msg.get("formations.formation.id_organisme")+")");
	BwGetById ("nom_organisme").setUrl("/srt/planeteformation/detail_organisme_popup?location.id:="+organisme_moduleid+"&id="+msg.get("formations.formation.id_organisme"));
    BwGetById ("nom_organisme").setTarget("_blank");

    var msg_logo = msg.get("formations.formation.logo");
	if (msg_logo)
    {
        BwGetById ("logo").setImage(msg_logo);
        //BwGetById ("logo").setUrl("javascript:openOrganisme("+msg.get("formations.formation.id_organisme")+")");
	BwGetById ("logo").setUrl("/srt/planeteformation/detail_organisme_popup?location.id:="+organisme_moduleid+"&id="+msg.get("formations.formation.id_organisme"));
        BwGetById ("logo").setTarget("_blank");
    }
	else
    {
        BwGetById ("logo").setUrl("");
		BwGetById ("logo").setImage("/extra/img/00.gif");
    }

    BwGetById ("prix_reduit").setValue(msg.get("formations.formation.prix_reduit"));
    BwGetById ("prix_normal").setValue(msg.get("formations.formation.prix_normal"));
	BwGetById ("reduction").setValue(msg.get("formations.formation.reduction"));
    var msg_reference = msg.get("formations.formation.reference"); 
    if (msg_reference)
    {
    	BwGetById ("reference").setValue(msg_reference);
    }
    else
    	BwGetById ("reference").setValue("");
    
    var msg_programme = msg.get("formations.formation.programme");
    if (msg_programme)
    {
    	BwGetById ("programme").show();
    	BwGetById ("programme").setUrl(msg_programme);
	    BwGetById ("programme").setTarget("_blank");
    }
    else
    	BwGetById ("programme").hide();
        
//	BwGetById ("subscribe").setUrl("javascript:openInscription("+id+")");
	BwGetById ("subscribe").setUrl("/srt/planeteformation/form/inscription_popup?location.id:="+inscrire_moduleid+"&id_formation="+id);
	BwGetById ("subscribe").setTarget("_blank");

    // SCROLL DOWN 
    window.scrollBy(0,500);
}

function openExpander()
{
    var detail = BwGetById ("ExpDetail");
	window.location="#ExpDetail" ;
    detail.open();
}

function closeExpander()
{
	var detail = BwGetById ("ExpDetail");
	detail.close();
}

function openInscription(id)
{

ligne="/srt/planeteformation/form/inscription_popup?location.id:="+inscrire_moduleid+"&id_formation="+id;
    window.open(ligne, 'inscription', 'top=20, left=120, width=800, height=600, resizable=yes, toolbar=no, scrollbars=yes, status=yes');
}

function openOrganisme(id)
{

    window.open("/srt/planeteformation/detail_organisme_popup?location.id:="+organisme_moduleid+"&id="+id,'organisme', "top=100, left=100, width=800, height=350, resizable=yes, toolbar=no, scrollbars=yes, status=yes");
}