
//<!-- =========== -->
//<!-- !Envoi de la demande initiale en fonction de la nomenclature pour obtenir le premier niveau-->
//<!-- =========== -->


function initDev(nomenclature){
	
	var url='config.php';
	
	var ajaxReq = new Ajax.Request(
		url,
		{
			method:'post',
			postBody:'nomenclature='+nomenclature,
			onComplete: setControl
		}
	);
}

//<!-- =========== -->
//<!-- !Envoi de la demande du niveau suivant et du type de controle en fonction de la nomenclature -->
//<!-- =========== -->

function getNext(nomenclature,nextlevel,pere,valeur){

	var url='config.php';
	
	var ajaxReq = new Ajax.Request(
		url,
		{
			method:'post',
			postBody:'thisNom='+nomenclature+'&niveau='+nextlevel+'&value='+valeur+'&pere='+pere,
			onComplete: setControl
		}
	);
}
//<!-- =========== -->
//<!-- ! Gestion des controles ˆ initialiser ˆ la volŽe-->
//<!-- =========== -->

function setControl(xhr){
	if (xhr.status == 200)
    {
        var data=xhr.responseText;
        
        if(data!="")
        {
        	data = data.replace(/\n/,"");
   			data =eval('('+ data +')');
   			if(data[1]!='999')
   			{	
   				var label=data[1][0]['niveau_lib'];
   				var typeControl=data[1][0]['niveau_type'];
   				var nomenclature=data[2];
   				var options=data[3];
   				var levelId=data[4];
   				createFormElement(label,typeControl,nomenclature,options,levelId);
   			}
   			else
   			{
   				createWTDForm()
   			}
   		}  
	}
}

//<!-- ================================== -->
//<!-- ! CrŽation des options de sauvegarde-->
//<!-- ================================== -->
function createWTDForm(){

	var myDiv = new Element('div', { 'class': 'formline','id':'f997' })
	$('formdeviseur').insert( {bottom: myDiv});
	
	var myLabel=new Element('label',{'for':'wtd'}).update("Envoyer le Devis");
	$(myDiv).insert( {bottom: myLabel})
	var myOptions=new Element('input',{'type':'radio','name':'wtd','value':'1'});
	$(myDiv).insert( {bottom: myOptions});
	
	var myDiv = new Element('div', { 'class': 'formline','id':'f998' })
	$('formdeviseur').insert( {bottom: myDiv});
	
	myLabel=new Element('label',{'for':'wtd'}).update("Envoyer et Sauvegarder");
	$(myDiv).insert( {bottom: myLabel})
	myOptions=new Element('input',{'type':'radio','name':'wtd','value':'2'});
	$(myDiv).insert( {bottom: myOptions});
	
var myDiv = new Element('div', { 'class': 'formline','id':'f999' })
	$('formdeviseur').insert( {bottom: myDiv});	

	myLabel=new Element('label',{'for':'wtd'}).update("Tout Annuler");
	$(myDiv).insert( {bottom: myLabel})
	myOptions=new Element('input',{'type':'radio','name':'wtd','value':'3'});
	$(myDiv).insert( {bottom: myOptions})
	
//<input type="radio" name="radiobutton" value="radiobutton1">
 
}

//<!-- ================================== -->
//<!-- ! CrŽation des controles ˆ la volŽe-->
//<!-- ================================== -->
function createFormElement(label,typecontrol,nom,options,levelId){

	try{
		
		var myDiv = new Element('div', { 'class': 'formline','id':'f'+levelId })
		$('formdeviseur').insert( {bottom: myDiv});
		
		
		var myLabel=new Element('label',{'for':'level'+levelId}).update(label);
   		$(myDiv).insert( {bottom: myLabel})
   		
		//var mySep=new Element('br');
			
		switch(typecontrol)
			
		{
		
			case("1"):
				
				var thevalue=options[0]["codeoption"];
				
				var myInput=new Element('input',{"type":"text","size":"10","name":'level'+levelId,"id":'n'+levelId,"alt":thevalue});
				$(myDiv).insert( {bottom: myInput});
			
				var thisLevel=options[0]['niveau_fils'];
				var myOkButton=new Element('input',{"type":"button","class":"butt","value":"OK","id":'b'+levelId,"onclick":'getNext("'+nom+'","'+thisLevel+'","'+levelId+'",'+'$('+'"n'+levelId+'")'+'.value'+')'});
				$(myDiv).insert( {bottom: myOkButton});
				//$(myDiv).insert( {bottom: mySep});
				break;
					
			case("2"):
				
   				var trans=options.length;
				var mySelect=new Element('select',{'id':'n'+levelId,'onchange':'getNext("'+nom+'",'+'this.value ,'+levelId+')'});
				
				$(myDiv).insert( {bottom: mySelect});
				var myOption=new Element('OPTION',{"value":"0"}).update('...');
				$(mySelect).insert( {bottom: myOption});
				
				for (i=0; i<trans;i++)
				{
					var sub = options[i];
					myOption=new Element('OPTION',{"value":sub['niveau_fils']}).update(sub['option_lib']);
					$(mySelect).insert( {bottom: myOption});
					//$(myDiv).insert( {bottom: mySep});
				}
				
			break;
			
			}
		
	}
	catch(e) 
	{
      alert(e);
	}
}
