var arrayfat=new Array();
var nrecordfat=0;
var ultimafat="";
var primafat="";
var paginafat=0;
var npagine=0;
var quantefat=20;
function InviaRichiestaFatture(ancora,qualepag)
{
	var rispostafat=new Array();
	var rispostona=new Array();	
		
	mandoquesto=CreaRichiestaFatture(ancora,qualepag);
	rispostona=SynchPOSTRequestA("PHPSER.php",mandoquesto);
//	alert("risposta"+rispostona);
	rispostafat=PHP_Unserialize(rispostona);
	if (rispostafat["errore"])
	{
		info="<table  align=center width=\"400px\" height=\"250px\" border=\"0\" cellspacing=0 cellpadding=4 >\
					<tr align=center ><td align=center width=\"50%\"><font  color="+coloresfondo+" size=\"3\"><b>Attenzione</b></font><br></td></tr><tr align=center ><td align=center  style=\" background-color:"+coloresfondo+";\"><b><font color=\"#FFFFFF\" size=\"3\">";
		info+= rispostafat["errore"];
		info+="</font></b></td></tr>";
		info+="<tr align=center ><td align=center width=\"50%\" style=\"background-color:"+coloresfondo+"; height:20px;\"><a href=\"javascript:Chiudi()\"><font   size=\"2\" color=\"#FFFFFF\"><img  src=\"images/home.png\" border=0></font></a></font></center></td></tr></table>";
		dok2=document.getElementById('display_list');
		dok2.innerHTML=(info);
		Mostra("display_list");
//		alert(info);
		paginafat=0;
//		alert(info);
	}
	else
	{
		rispostafat=rispostafat["siv"];
		npagine=parseInt(rispostafat["npag"]);
		paginafat=parseInt(rispostafat["pag"]);
		arrayfat=rispostafat["fatture"];
		DisplayFatture();
		CaricaBarraPagine(CaricaArrayFunzioni(npagine),paginafat,"barretta");
		CaricaBarraPagine(CaricaArrayFunzioni(npagine),paginafat,"barrettafondo");
	}
}
function DisplayFatture()
{
	doc=document.getElementById("cli_sel");
	if (doc)
		clientesel=doc.value;
	document.getElementById('display_large').innerHTML=("");
//alert(printArray(arrayfat));
	info="<br><table width=\"100%\" border=\"0\"  cellspacing=0 cellpadding=0 align=center style=\" border: 1px solid #eeeeee; padding:1px\">\
			<tr>";
	info+="<td class='consfondod' width=\"100px\">&nbsp;</td>";
//	alert("pagina: "+paginafat);
	info+="<td class='consfondod' colspan=\"2\" align=\"center\"><font  size=\"3\" color="+coloresfondo+"><b>FATTURE</b><br><br><font  size=\"1\" color="+coloresfondo+"><b>Se presente l'icona nella colonna PDF cliccarla per visualizzare il documento.</b></font><br><br><br><br></font></td>";
	info+="<td class='consfondod' width=\"100px\" align=right valign=\"top\">&nbsp;\
			<a href=\"javascript:Chiudi()\"><font  size=\"2\"><img  src=\"images/home.png\" border=0></font></a>";	
	info+="<input type=\"hidden\" name=\"boh\" id=\"n_recfat\" size = \"6\" MAXLENGTH=\"40\" value=\""+nrecordfat+"\"></td></tr>";
	
	if (npagine>1)
	{
		info+="<tr>";
	//INIZIO riga con scorrimento pagine:	
		
		info+="<td class=\"consfondos\" colspan=\"4\" style=\"border-top: 1px solid #eeeeee; border-left: 0px solid #eeeeee; border-right:0px solid #eeeeee; border-bottom: 1px solid #eeeeee; height:25px;\"><div ID=\"barretta\" align=\"center\" style=\"height:25px;\"></div></td>";
		info+="</tr>";
		//FINE riga con scorrimento pagine:	
	}
	
	
	info+="<tr><td align=center style=\" border-top: 1px solid #eeeeee; border-left: 1px solid #eeeeee;height:25px; \" class='chiarissimo'><font  color='#8cacbb' size='1'><b>NUMERO</b></font></td>\
		   <td style=\" border-top: 1px solid #eeeeee; height:25px; \" align=center class='chiarissimo'><font  color='#8cacbb'  size='1'><b>DATA</b></font></td>\
		  <td align=center style=\" border-top: 1px solid #eeeeee; height:25px; \" class='chiarissimo'><font color='#8cacbb'  size='1'><b>IMPORTO &nbsp;&euro;</b></font></td>\
		  <td align=center style=\" border-top: 1px solid #eeeeee;border-right: 1px solid #eeeeee; height:25px; \" class='chiarissimo'><font color='#8cacbb'  size='1'><b>PDF</b></font></td></tr>";
	arrayfat=arrayfat["fattura"];
	primo=true;
	ultimafat="";
	primafat="";
	coloreriga="transparent";
	if (arrayfat[0]!=undefined)
	{
		for (i in arrayfat)
		{	
			if (primo)
			{
				primafat=arrayfat[i]["id-rec"];
				primo=false;
			}	
			info+=CaricaListaFatture(arrayfat[i],coloreriga);
			
			//alert("Bottoni:  "+printArray(buttons));
		}
	}
	else
	{
		primafat=arrayfat["id-rec"];
		info+=CaricaListaFatture(arrayfat);
	}
	info+="<tr><td colspan=\"4\" style=\"height:25px; border-top: 1px solid #eeeeee;\">&nbsp;<input type=\"hidden\" name=\"id\" id=\"first_fat\" size = \"6\" MAXLENGTH=\"40\" value="+primafat+">\
	<input type=\"hidden\" name=\"id\" id=\"last_fat\" size = \"6\" MAXLENGTH=\"40\" value="+ultimafat+"></td></tr>";
	if (npagine>1)
	{
		info+="<tr>";
	//INIZIO riga  in fondo con scorrimento pagine:	
		
		info+="<td class=\"consfondos\" colspan=\"4\" style=\"border-top: 1px solid #eeeeee; border-left: 0px solid #eeeeee; border-right:0px solid #eeeeee; border-bottom: 1px solid #eeeeee; height:25px;\"><div ID=\"barrettafondo\" align=\"center\" style=\"height:25px;\">$nbsp;</div></td>";
	//	alert("pagina fat: "+paginafat+"numpagine: "+npagine+"");
	//	if (paginafat<npagine) alert(paginafat+" risulta minore di "+npagine);
	//	else alert(paginafat+" risulta maggiore o uguale a "+npagine);
		
		info+="</tr>";
		//FINE riga in fondo con scorrimento pagine:	
	}
	info+="</table><br><br><br>";
	dok=document.getElementById("display_list");
	dok.innerHTML=(info);
	Mostra("display_list");
}
function CreaRichiestaFatture(avanti,pagina)
{
	var chiedofat = new Array();
	bravo=true;
	chiedofat["azione"]="fatture";
	chiedofat["ditta"]=ditta;
	docID=document.getElementById("ID_SIV");
	chiedofat["id"]=docID.value;
	doc=document.getElementById("ID_UTE");
	if (doc)
		chiedofat["id-ute"]=doc.value;
	docclisel=document.getElementById("cli_sel");
	if (docclisel)
		chiedofat["cliente"]=docclisel.value;
	doc=document.getElementById("des_sel");
	if (doc)
		chiedofat["destin"]=doc.value;
	chiedofat["n-elem"]=quantefat+"";
	if (npagine>1)
	{
	  doclast=document.getElementById("last_fat");
	  if (doclast)
	  {
	//	  chiedofat["last-rec"]=doclast.value;
  //		alert("last_fat = "+chiedofat["last_fat"]);
	  }
	  docfi=document.getElementById("first_fat");
	  if (docfi)
	  {
	//	  chiedofat["first-rec"]=docfi.value;
  //		alert("first_fat = "+chiedofat["first_fat"]);
	  }
	}
	if (avanti==1)
		chiedofat["go"]="next";
	if (avanti==-1)
		chiedofat["go"]="prev";
	if (pagina!=0 && pagina!=undefined) 
		chiedofat["pag"]=pagina;	
	return chiedofat;
}

function CaricaListaFatture(lefatture,coloreriga)
{
	var colore="#002299";
	var importo=lefatture["importo"];    
	var pos=importo.indexOf("-");
	if (pos>=0)  
	    colore="#992200";
	
	sputa="<tr ><td class=\"consfondod\" id=\"numfat\" style=\" border-top: 1px solid #eeeeee;border-right: 1px dotted #eeeeee;border-left: 1px solid #eeeeee;\" align=center ><font  size=\"3\" color='#002299'><b> "+lefatture["numero"]+"</b></font></td><td class=\"consfondod\" style=\" border-top: 1px solid #eeeeee;border-right: 1px solid #eeeeee; \" align=\"center\"><font  size=\"3\" color='#002266'><i><b>"+lefatture["data"]+"</b></i></font></td>\
		<td class=\"consfondod\" align=right style=\" border-top: 1px solid #eeeeee;border-right: 1px dotted #eeeeee;\"><font  size=\"3\" color='"+colore+"'><b>"+lefatture["importo"]+"&nbsp;&nbsp;</b></font></td>";
	
	sputa+="<td class=\"consfondod\"  class=\"norepeat\"  align=center style=\" height:30px; border-top: 1px solid #eeeeee; border-right: 1px solid #eeeeee;\">&nbsp;";
	if (lefatture["cepdf-ft"]=="1")
	{
		sputa+="<a href\ =\"pdf.php?id="+document.getElementById("ID_SIV").value+"&id-rec="+lefatture["id-pdf-ft"]+"&ditta="+ditta+"&sigla="+document.getElementById("siglahidden").value+"&filiale="+document.getElementById("filialehidden").value+"&tipopdf="+lefatture["tipopdf-ft"]+"\"  TARGET=\"_blank\"  ><font size='1'>&nbsp;</font><img src=\"images/document20px.png\"></img></a>";
	}
	if (lefatture["cepdf-bl"]=="1")
	{
		sputa+="<a href\ =\"pdf.php?id="+document.getElementById("ID_SIV").value+"&id-rec="+lefatture["id-pdf-bl"]+"&ditta="+ditta+"&sigla="+document.getElementById("siglahidden").value+"&filiale="+document.getElementById("filialehidden").value+"&tipopdf="+lefatture["tipopdf-bl"]+"\"  TARGET=\"_blank\"  ><font size='1'>&nbsp;</font><img src=\"images/document20px.png\"></img></a>";
	}
	sputa+="</td></tr>";
	ultimafat=lefatture["id-rec"];
	return sputa;
	//alert("Bottoni:  "+printArray(buttons));
}
function CaricaArrayFunzioni(numpag)
{
	var funzione=new Array();
	for(var i=0;i<numpag;i++)
	{
		funzione[i]=new Array();
		funzione[i]["npag"]=i+1+"";
		funzione[i]["funzione"]="javascript:InviaRichiestaFatture("+0+","+(i+1)+")";
	}
	return funzione;
}
