var isIE = false;
var req;
var curComboObj;// setto  la  variabile  che  mi  crea  la  combo  obj
var numeroFormati;//  variabile  che  conta il numero  dei  formati
function cercaProdotto() {
  Id=checkCombo('smiwrapofferta.IdProdotto')
  if(Id=='')return

  if(document.getElementById){
  var select = document.getElementById(curComboObj);
       url=serverPath+"mod/ajax.php?Id="+Id+"&lang="+lang
  }
  
  if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqTxt;
        req.open("GET", url, true);
        req.send(null);
  } else if (window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
	       req.onreadystatechange = processReqTxt;
           req.open("GET",url, true);
           req.send();
        }
   }
    else {
     location.href="modulo.php?IdPage=Offerta&Id="+Id+"&lang="+lang
   }
  
}
function clearTopicList(select) {
    while (select.length > 0) {
        select.remove(0);
    }
}


function appendToSelect(select, value, content) {
    var opt;
    opt = document.createElement("option");
    opt.value = value;
    opt.appendChild(content);
    select.appendChild(opt);
}



function processReqTxt() {
   numeroFormati=getValue('smiwrapoffertaformati.Numero')
   IdColore=document.modulo.elements['smiwrapofferta.IdColore'];
   IdGrammatura=document.modulo.elements['smiwrapofferta.IdGrammatura'];
   clearTopicList(IdColore); 
   if(!document.all)appendToSelect(IdColore,'1',document.createTextNode("Please wait"));
   clearTopicList(IdGrammatura);
   if(!document.all) appendToSelect(IdGrammatura,'2',document.createTextNode("Please wait"));
   for (i=1;i<=numeroFormati;i++){
	   curIdFormato=document.modulo.elements['smiwrapoffertaformati.IdFormato:'+i];
       clearTopicList(curIdFormato); 
    }
   if (req.readyState == 4) {
       if (req.status == 200) {verificaObj();}
	   else {
              alert("There was a problem retrieving the TxT data:\n" + req.statusText);
       }
   }
}



function verificaObj(){
   var risposta= req.responseText;
   IdColore=document.modulo.elements['smiwrapofferta.IdColore'];
   IdGrammatura=document.modulo.elements['smiwrapofferta.IdGrammatura'];
      
   if(risposta!='') {
       clearTopicList(IdColore);
	   dati=risposta.split('@');
	   colori=dati[0].split(',');
	   grammatura=dati[1].split(',');
	   formati=dati[2].split('*');
	   luce=dati[3]
	   
	   for (i=0;i<colori.length;i++){
	      colore=colori[i].split('|')
		  appendToSelect(IdColore,colore[1],document.createTextNode(colore[0]));
	   } 
	   clearTopicList(IdGrammatura);
	    for (i=0;i<grammatura.length;i++){
		   gra= grammatura[i].split('|')
		   appendToSelect(IdGrammatura,gra[1],document.createTextNode(gra[0]));
	   } 
	   numeroFormati=getValue('smiwrapoffertaformati.Numero')
	   for (i=1;i<=numeroFormati;i++){
	   	  scrivi('fascia'+i,luce)
	   }
	  
	   for (i=1;i<=numeroFormati;i++){
	   	 if(formati==''){
		 	nBlocco('formato'+i)
		 	
			document.modulo.elements['smiwrapoffertaformati.IdQuantitaFormato:'+i].value='';
		 }
		 else {
		  curIdFormato=document.modulo.elements['smiwrapoffertaformati.IdFormato:'+i];
		  appendToSelect(curIdFormato,'',document.createTextNode('Please select'));
		  for (a=0;a<formati.length;a++){
		    format= formati[a].split('|')
		    appendToSelect(curIdFormato,format[1],document.createTextNode(format[0]));
		   
	      } 
		  hBlocco('formato'+i)
    	 }	
	   }
   }
}

