function getControlPrefix() {
   if (getControlPrefix.prefix)
      return getControlPrefix.prefix;
   
   var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
   var o, o2;
   for (var i = 0; i < prefixes.length; i++) {
      try {
         // try to create the objects
         o = new ActiveXObject(prefixes[i] + ".XmlHttp");
         o2 = new ActiveXObject(prefixes[i] + ".XmlDom");
         return getControlPrefix.prefix = prefixes[i];
      }
      catch (ex) {};
   }
   
   alert("Could not find an installed XML parser");
}


XmlHttpCreate = function () {
  if (window.XMLHttpRequest) {
	 var req = new XMLHttpRequest();
	 
	 // some older versions of Moz did not support the readyState property
	 // and the onreadystate event so we patch it!
	 if (req.readyState == null) {
		req.readyState = 1;
		req.addEventListener("load", function () {
		   req.readyState = 4;
		   if (typeof req.onreadystatechange == "function")
			  req.onreadystatechange();
		}, false);
	 }
	 
	 return req;
  }
  if (window.ActiveXObject) {
	 return new ActiveXObject(getControlPrefix() + ".XmlHttp");
  }

   alert("Your browser does not support XmlHttp objects");
};

function createRequestObject() {
    var ro;
    var browser = navigator.appName;
	
    if(window.ActiveXObject){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else if (window.XMLHttpRequest){
        ro = new XMLHttpRequest();
    }
	
    return ro;
}

function errorXML(fileXML){
	if(fileXML.documentElement.nodeName=='error'){
		alert(fileXML.getElementsByTagName('testo')[0].firstChild.nodeValue);
		return false;
	}else{
		return true;
	}
}

function svuotaEl(ID){
	remove_length=document.getElementById(ID).childNodes.length;

	for (i=0;i<remove_length;i++){
		document.getElementById(ID).removeChild(document.getElementById(ID).childNodes.item(0));
	}
}


function onKeyListener(e,key,handler){
	var key_pressed = window.event ? event.keyCode : e.which;
//	var keychar = String.fromCharCode(key);
//	alert(key_pressed);
	if (key_pressed==key){
		eval(handler);
	}
}


//var XmlHttp = createRequestObject();
var XmlHttp = XmlHttpCreate();

/* ####  Riempimento automatico di un Input Text  ####*/
function autoFillXml(dir,table,field,value) {
	if(XmlHttp.readyState == 0 || XmlHttp.readyState == 4){
		field=field.substring(4);
		if (value!=''){
			document.getElementById('fill_'+field).innerHTML = '<img src="'+dir+'/img/searching.gif" width="13" height="13" align="absmiddle"> Sto cercando...';
			document.getElementById('fill_'+field).style.display='block';		
	
			file='AutoFillXml';
			url=dir+'/include/'+file+'.php?table='+table+'&field='+field+'&value='+value;
			XmlHttp.open('get', url);
			XmlHttp.onreadystatechange = eval('response'+String(file));
			XmlHttp.send(null);
			
		}else{
			document.getElementById('fill_'+field).style.display='none';		
			document.getElementById('fill_'+field).innerHTML ='';
		}
	}
}


function responseAutoFillXml() {
	if(XmlHttp.readyState == 4){
		if (XmlHttp.status == 200) {
//			var respText = XmlHttp.responseText;
//			alert(respText);
			var respXML = XmlHttp.responseXML;

			field=respXML.documentElement.nodeName;
			
			val_str='';
	
			svuotaEl('fill_'+field);
			for (i=0;i<respXML.getElementsByTagName('risultato').length;i++){
//				val_str+='<div class="autoFillElements_off" onMouseOver="this.className=\'autoFillElements_on\'" onMouseOut="this.className=\'autoFillElements_off\'" onClick="document.getElementById(\'ric_'+field+'\').value=\''+respXML.getElementsByTagName('risultato')[i].firstChild.nodeValue+'\';document.getElementById(\'fill_'+field+'\').style.display=\'none\';">'+respXML.getElementsByTagName('risultato')[i].firstChild.nodeValue+'</div>';
				
				var val=respXML.getElementsByTagName('risultato')[i].firstChild.nodeValue;
				
				div_search=document.createElement('DIV');
				div_search.setAttribute('class','autoFillElements_off');
//				div_search.setAttribute('onMouseOver','this.className=\'autoFillElements_on\'');
				div_search.setAttribute('onMouseOut','this.className=\'autoFillElements_off\'');
//				div_search.setAttribute('onClick','document.getElementById(\'ric_'+field+'\').value=\''+val+'\';document.getElementById(\'fill_'+field+'\').style.display=\'none\';');
				
				div_search.id='autoFillElements_'+ i;
				div_search.className='autoFillElements_off';
				div_search.onmouseover=function(){this.className='autoFillElements_on';};				
				div_search.onmouseout=function(){this.className='autoFillElements_off';};
				div_search.onclick=function(){invia(field);};

				txt_search=document.createTextNode(respXML.getElementsByTagName('risultato')[i].firstChild.nodeValue);
			
				div_search.appendChild(txt_search);
				document.getElementById('fill_'+field).appendChild(div_search);
			}

//			document.getElementsByTagName('body')[0].setAttribute('onKeyDown','onKeyListener(event,38,\'sposta("fill_'+field+'")\')');
			document.getElementsByTagName('body')[0].onkeydown=function(event){
				onKeyListener(event,38,'sposta(\'fill_'+field+'\',key_pressed)'); 
				onKeyListener(event,40,'sposta(\'fill_'+field+'\',key_pressed)');
				onKeyListener(event,13,'invia(\''+field+'\')');
			};

//			document.getElementById('fill_'+field).innerHTML = val_str;
//			document.getElementById('fill_'+field).style.display='block';		
		}else{
			alert("There was a problem retrieving the XML data:\n" + XmlHttp.statusText);
		}
	}
} 

function sposta(elID,key){
//	alert(elID)
	riquadro=document.getElementById(elID).getElementsByTagName('div');
//	alert(riquadro.length)
	ind_sel=-1;
	for (i=0;i<riquadro.length;i++){
//		alert(riquadro.item(i).className);
		if (riquadro.item(i).className=='autoFillElements_on'){
			ind_sel=i;
			break;
		}
	}
	
	if (ind_sel==-1){
		ind_sel=0;
		ind_sp=0;
	}else if (key==38){
		ind_sp=(ind_sel==0?riquadro.length-1:ind_sel-1);
	}else if (key==40){
		ind_sp=(ind_sel==riquadro.length-1?0:ind_sel+1);
	}

	
	riquadro.item(ind_sel).className='autoFillElements_off';
	riquadro.item(ind_sp).className='autoFillElements_on';
}

function invia(field){
//	alert(elID)
	riquadro=document.getElementById('fill_'+field).getElementsByTagName('div');
//	alert(riquadro.length)
	ind_sel=-1;
	for (i=0;i<riquadro.length;i++){
//		alert(riquadro.item(i).className);
		if (riquadro.item(i).className=='autoFillElements_on'){
			ind_sel=i;
			break;
		}
	}
	
	if (ind_sel!=-1){
		document.getElementById('ric_'+field).value=riquadro.item(ind_sel).firstChild.nodeValue;
		//bongio modifica per rendere invisibile il div fill_field
		document.getElementById('fill_'+field).style.display='none';
	}
}

/* ################*/


/* ####  Aggiunge un valore ad una tabella Categoria e aggiorna la select  ####*/
function AggSelectXml(dir,table,sel,IDsel,field,value) {
	field=field.substring(4);
	if (value!=''){
		file='AggSelectXml';
		url=dir+'/include/'+file+'.php?table='+table+'&sel='+sel+'&IDsel='+IDsel+'&field='+field+'&value='+value;
		XmlHttp.open('get', url);
		XmlHttp.onreadystatechange = eval('response'+String(file));
		XmlHttp.send(null);

		document.getElementById('span_'+field).style.display='none';
		document.getElementById('att_'+field).style.display='inline';
	}else{
		alert('Devi inserire un valore');
	}
}
function responseAggSelectXml() {
	if(XmlHttp.readyState == 4){
		if (XmlHttp.status == 200) {
//			var respText = XmlHttp.responseText;
//			alert(respText);
			var respXML = XmlHttp.responseXML;
			
			field=respXML.getElementsByTagName('field')[0].firstChild.nodeValue;
			sel=respXML.getElementsByTagName('select')[0].firstChild.nodeValue;
			IDsel=respXML.getElementsByTagName('select')[0].getAttribute('ID');
			value=respXML.getElementsByTagName('value')[0].firstChild.nodeValue;
			IDvalue=respXML.getElementsByTagName('value')[0].getAttribute('ID');
/*
			alert(field);
			alert(sel);
			alert(IDsel);
			alert(value);
			alert(IDvalue);
*/

			i=document.getElementById(sel).length;
			document.getElementById(sel).options[i] = new Option(value,IDvalue,false,false); 
			pos_menu(IDsel,document.getElementById(sel));
	
			document.getElementById('att_'+field).style.display='none';
			document.getElementById('piu_'+field).style.display='inline';
		}else{
			alert("There was a problem retrieving the XML data:\n" + XmlHttp.statusText);
		}
	}
} 
/* ################*/


/* ####  Modifica un campo  ####*/
function ModCampoXml(dir,table,ID,field,value) {
	if (value!=''){
		file='ModCampoXml';
		url=dir+'/include/'+file+'.php';
/*GET*/	//value='?table='+table+'&ID='+ID+'&field='+field+'&value='+value;
/*POST*/value='table='+table+'&ID='+ID+'&field='+field+'&value='+value;
		document.getElementById('inp_'+field+ID).style.display='none';
//		document.getElementById('span_'+field+ID).style.display='inline';
//		document.getElementById('att_'+field+ID).innerHTML='<img src="'+dir+'/img/searching.gif" width="13" height="13" align="absmiddle"> Sto aggiornando...';
//		document.getElementById('att_'+field+ID).style.display='inline';

		document.getElementById('span_'+field+ID).removeChild(document.getElementById('span_'+field+ID).firstChild);

		span_att=document.createElement('SPAN');
		span_att.ID='att_';
		img_att=document.createElement('IMG');
		img_att.src=dir+'/img/searching.gif';
		img_att.align='absbottom';
		testo_att=document.createTextNode(' Sto caricando...');
		
		span_att.appendChild(img_att);
		span_att.appendChild(testo_att);
		document.getElementById('span_'+field+ID).appendChild(span_att);
//		document.getElementById('span_'+field+ID).innerHTML='';
		document.getElementById('span_'+field+ID).className='ModXml';
		document.getElementById('span_'+field+ID).style.display='inline';
		
		XmlHttp.open('POST', url);
		XmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		XmlHttp.onreadystatechange = eval('response'+String(file));
		XmlHttp.send(value);

	}else{
		alert('Devi inserire un valore');
	}
}

function responseModCampoXml() {
	if(XmlHttp.readyState == 4){
		if (XmlHttp.status == 200) {
//			var respText = XmlHttp.responseText;
//			alert(respText);
			var respXML = XmlHttp.responseXML;
			
			field=respXML.getElementsByTagName('field')[0].firstChild.nodeValue;
			ID=respXML.getElementsByTagName('field')[0].getAttribute('ID');

			if(!errorXML(respXML)){
				document.getElementById('span_'+field+ID).style.display='none';
				document.getElementById('inp_'+field+ID).style.display='inline';
				return false;
			}
			
			value=respXML.getElementsByTagName('value')[0].firstChild.nodeValue;
			span_value=document.createTextNode(value);
/*
			alert(field);
			alert(value);
			alert(ID);
*/


//			alert(document.getElementById('span_'+field+ID).firstChild.nodeValue);
//			alert(span_value.nodeValue);
			document.getElementById('span_'+field+ID).removeChild(document.getElementById('span_'+field+ID).firstChild);
			document.getElementById('span_'+field+ID).innerHTML=value;
//			alert(document.getElementById('span_'+field+ID).innerHTML);

//			document.getElementById('att_'+field+ID).style.display='none';
//			document.getElementById('span_'+field+ID).style.display='inline';
		}else{
			alert("There was a problem retrieving the XML data:\n" + XmlHttp.statusText);
		}
	}
} 
/* ################*/


/* ####  Genera la tabella delle statistiche per Soft-Hours  ####*/
function StatCommesseXml(dir,table,IDoperatore,data_da,data_a,ordine) {
	if (document.getElementById('tipo').selectedIndex>1){
/* Insericse una riga per il loading */
		svuotaEl('ris_tab');

		riga_att=document.createElement('TR');
		cella_att=document.createElement('TD');
		img_att=document.createElement('IMG');
		img_att.src=dir+'/img/searching.gif';
		img_att.align='absbottom';
		img_att.className='cellaBianca';
		testo_att=document.createTextNode(' Sto caricando...');
	
		cella_att.appendChild(img_att);
		cella_att.appendChild(testo_att);
		riga_att.appendChild(cella_att);
		document.getElementById('ris_tab').appendChild(riga_att);
		
		file='StatCommesseXml';
		url=dir+'/include/'+file+'.php';
/*GET*/	value='?table='+table+'&IDoperatore='+IDoperatore+'&data_da='+data_da+'&data_a='+data_a+'&ordine='+ordine;
/*POST*///value='table='+table+'&IDoperatore='+IDoperatore+'&data_da='+data_da+'&data_a='+data_a;
//		alert(url+value);
		XmlHttp.open('get', url+value);
		XmlHttp.onreadystatechange = eval('response'+String(file));
		XmlHttp.send(null);
	}else{
		alert('Devi scegliere un operatore e un tipo!');
	}
}

function StatCommesseXml(dir,table,IDoperatore,data_da,data_a,ordine) {
	if (document.getElementById('tipo').selectedIndex>1){
/* Insericse una riga per il loading */
		svuotaEl('ris_tab');

		riga_att=document.createElement('TR');
		cella_att=document.createElement('TD');
		img_att=document.createElement('IMG');
		img_att.src=dir+'/img/searching.gif';
		img_att.align='absbottom';
		img_att.className='cellaBianca';
		testo_att=document.createTextNode(' Sto caricando...');
	
		cella_att.appendChild(img_att);
		cella_att.appendChild(testo_att);
		riga_att.appendChild(cella_att);
		document.getElementById('ris_tab').appendChild(riga_att);
		
		file='StatCommesseXml';
		url=dir+'/include/'+file+'.php';
/*GET*/	value='?table='+table+'&IDoperatore='+IDoperatore+'&data_da='+data_da+'&data_a='+data_a+'&ordine='+ordine;
/*POST*///value='table='+table+'&IDoperatore='+IDoperatore+'&data_da='+data_da+'&data_a='+data_a;
//		alert(url+value);
		XmlHttp.open('get', url+value);
		XmlHttp.onreadystatechange = eval('response'+String(file));
		XmlHttp.send(null);
	}else{
		alert('Devi scegliere un operatore e un tipo!');
	}
}

function responseStatCommesseXml() {
	if(XmlHttp.readyState == 4){
		if (XmlHttp.status == 200) {
//			alert(respText = XmlHttp.responseText);
			var respXML = XmlHttp.responseXML;

	/* Estrae le ore totali */
			var ore_tot=respXML.documentElement.getAttribute("ore_tot");
//			ore_tot=oreXml[0].getAttribute("oreTot");

			document.getElementById('ore_tot').innerHTML=ore_tot;
			
			var x=respXML.getElementsByTagName('riga');
			var IDtab=document.getElementById('ris_tab');

	/* Rimuove la riga di loading */
			IDtab.removeChild(IDtab.firstChild);
			
			for (i=0;i<x.length;i++){
//				alert(x.item(i).childNodes[0].firstChild.nodeValue);
				riga=document.createElement('TR');
				
				cella_commessa=document.createElement('TD');
				cella_ore=document.createElement('TD');
				cella_bar=document.createElement('TD');
				cella_perc=document.createElement('TD');
				cella_budget=document.createElement('TD');
				cella_costi_ora=document.createElement('TD');
				cella_costi_dir=document.createElement('TD');
				cella_saldo=document.createElement('TD');
				cella_saldo_perc=document.createElement('TD');
				
				testo_commessa=document.createTextNode(x.item(i).childNodes[0].firstChild.nodeValue);
				testo_ore=document.createTextNode(x.item(i).childNodes[1].firstChild.nodeValue);
				
				img_bar=document.createElement('IMG');
				img_bar.src='../img/quadrato.gif';
				img_bar.width=Math.round(Number(x.item(i).childNodes[2].firstChild.nodeValue));
				img_bar.height=15;
				img_bar.alt=x.item(i).childNodes[2].firstChild.nodeValue;
				img_bar.title=x.item(i).childNodes[2].firstChild.nodeValue;
//				alert(x.item(i).childNodes[2].firstChild.nodeValue+' '+Math.round(Number(x.item(i).childNodes[2].firstChild.nodeValue)));
		
				testo_perc=document.createTextNode(x.item(i).childNodes[2].firstChild.nodeValue+' %');
				testo_budget=document.createTextNode(x.item(i).childNodes[3].firstChild.nodeValue+' €');
				testo_costi_ora=document.createTextNode(x.item(i).childNodes[4].firstChild.nodeValue+' €');
				testo_costi_dir=document.createTextNode(x.item(i).childNodes[5].firstChild.nodeValue+' €');
				testo_saldo=document.createTextNode(x.item(i).childNodes[6].firstChild.nodeValue+' €');
				if (x.item(i).childNodes[7].firstChild.nodeValue!='---')
					testo_saldo_perc=document.createTextNode(x.item(i).childNodes[7].firstChild.nodeValue+' %');
				else
					testo_saldo_perc=document.createTextNode(x.item(i).childNodes[7].firstChild.nodeValue);
				
				cls=i%2?'Bianca':'Grigia';
				
				cella_commessa.className='cella'+cls;
				cella_ore.className='cella'+cls;
				cella_perc.className='cella'+cls;
				cella_bar.className='cella'+cls;
				cella_budget.className='cella'+cls;
				cella_costi_ora.className='cella'+cls;
				cella_costi_dir.className='cella'+cls;
				cella_saldo.className='cella'+cls;
				cella_saldo_perc.className='cella'+cls;
//				alert(Number(x.item(i).childNodes[7].firstChild.nodeValue))
				if (x.item(i).childNodes[7].firstChild.nodeValue!='---'){
					if (Number(x.item(i).childNodes[7].firstChild.nodeValue)>=0){
						cella_saldo.className+='Piu';
						cella_saldo_perc.className+='Piu';
					}else{
						cella_saldo_perc.className+='Meno';
						cella_saldo.className+='Meno';
					}
				}
				
				cella_commessa.appendChild(testo_commessa);
				cella_ore.appendChild(testo_ore);
				cella_perc.appendChild(testo_perc);
				cella_bar.appendChild(img_bar);
				cella_budget.appendChild(testo_budget);
				cella_costi_ora.appendChild(testo_costi_ora);
				cella_costi_dir.appendChild(testo_costi_dir);
				cella_saldo.appendChild(testo_saldo);
				cella_saldo_perc.appendChild(testo_saldo_perc);

				riga.appendChild(cella_commessa);
				riga.appendChild(cella_ore);
				riga.appendChild(cella_perc);
				riga.appendChild(cella_bar);
				riga.appendChild(cella_budget);
				riga.appendChild(cella_costi_ora);
				riga.appendChild(cella_costi_dir);
				riga.appendChild(cella_saldo);
				riga.appendChild(cella_saldo_perc);
				
				
				IDtab.appendChild(riga);
			}

			riga=document.createElement('TR');
				
//			CeNonCe(tab);
		}else{
			alert("There was a problem retrieving the XML data:\n" + XmlHttp.statusText);
		}
	}
} 
/* ################*/