document.onkeydown = ajax_txt_onkeydown;
var numerotasto;
var xmlHttp;
var btndefa="btnREFRESH"; 	// tasto premuto di default
var btnvalo="1"; 		// valore del tasto premuto di default
function setdefault(defa, valo){
	// permette di impostare il tasto ed il suo valore quando viene premuto enter
	btndefa=defa;
	btnvalo=valo;
}

function ApreRinomina(Tipo){
	var LinkX;
	LinkX = "Rinomina.aspx?campo=" + Tipo + "&valore=" + document.FormGO("Inp_"+Tipo).value;
	window.open(LinkX,"TargetRinomina","width=600 height=300 top=0 left=0 scrollbars=yes resizable=yes");
}

function ApreRicerca(Campo, NomeCampo, sesID, Test, TestNomeCampo){
	var Valore;
	var LinkX;
	var Opzioni;
	if (Test!="") {
		Valore=RicercaValoreCampo("txt_"+TestNomeCampo);
		if (Valore=="") Valore=RicercaValoreCampo("txt_"+TestNomeCampo+"_hidden");
		Test=Test.replace("--123321--",Valore);
	}
	LinkX = "ricerca.php?campo="+Campo+"&ncampo="+NomeCampo+"&test="+Test+"&ATID="+sesID;
	//Opzioni="width=450, height=600, top=0, left=0, scrollbars=yes, resizable=yes";
	Opzioni="top=0, left=0, scrollbars=yes, resizable=yes";
	window.open(LinkX, "TargetRicerca", Opzioni);
}

function AT_onfocus(NomeCampo, bgselezione){
	ajax_azzera();
	document.getElementById(NomeCampo).style.background=bgselezione;
	document.getElementById("FormGO_focus").value=NomeCampo;
}

function AT_onscroll(){
	document.getElementById("FormGO_focus_Y").value=document.body.scrollTop;
}

function screen_res(){
	document.getElementById("screenwidth").value=screen.width;
	document.getElementById("screenheight").value=screen.height;
}

function fuoco(NomeCampo){
	if (window.navigator.appName.toLowerCase().indexOf("microsoft")>-1){
		campo=document.FormGO(NomeCampo);
	}else{
		campo=document.forms["FormGO"][NomeCampo];
	}
	campo.focus();

	if (window.navigator.appName.toLowerCase().indexOf("microsoft")>-1){
		theform = document.FormGO;
	}else{
		theform = document.forms["FormGO"];
	}

	var Y;
	Y=theform.FormGO_focus_Y.value;
	self.scrollTo(0,Y);
}

function mostra(id, idda, ida){
	// VISUALIZZA IL TAG <DIV ID="m1">nnnn</DIV> nascosto
	if (id==-1) primo=document.getElementById("m"+idda).style.display;
	for (indice=idda;indice<=ida;indice++){
		try 
		{
			if (id==-1) {
				if (primo=="none") {
					document.getElementById("m"+indice).style.display="";
				}else{
					document.getElementById("m"+indice).style.display="none";
				}
			}else{
				if (indice==id) {
					if (document.getElementById("m"+indice).style.display=="none") {
						document.getElementById("m"+indice).style.display="";
					}else{
						document.getElementById("m"+indice).style.display="none";
					}
				}else{
					document.getElementById("m"+indice).style.display="none";
				}
			}
		} 
		catch(e) 
		{
			indice=indice;
		} 
	}
}

function mostratr(id){
	// VISUALIZZA LE RIGHE CHE INIZIANO CON id
	primo=document.getElementById(id+"_1").style.display;
	for (indice=1;indice<=500;indice++){
		try 
		{
			if (primo=="none") {
				document.getElementById(id+"_"+indice).style.display="";
			}else{
				document.getElementById(id+"_"+indice).style.display="none";
			}
		} 
		catch(e) 
		{
			break;
			indice=indice;
		} 
	}
}

function RicercaValoreCampo(NomeCampo){
	var theform;
	if (window.navigator.appName.toLowerCase().indexOf("microsoft")>-1){
		theform = document.FormGO;
	}else{
		theform = document.forms["FormGO"];
	}
	b=String(theform[NomeCampo].value);
	if (b=="undefined") b="";
	return b;
}

function ImpostaValoreCampo(NomeCampo, Valore){
	var theform;
	if (window.navigator.appName.toLowerCase().indexOf("microsoft")>-1){
		theform = document.FormGO;
	}else{
		theform = document.forms["FormGO"];
	}
	b=String(theform[NomeCampo].value);
	if (b!="undefined") theform[NomeCampo].value=Valore;
}

function __DoPostBack(NomeButton, ValoreButton, Conferma){
	// permette l'uso di BUTTON per MSIE
	if (Conferma!="") {
		Risposta=confirm(Conferma);
		if (Risposta==false) return;
	}
	var theform;
	if (window.navigator.appName.toLowerCase().indexOf("microsoft")>-1){
		theform = document.FormGO;
	}else{
		theform = document.forms["FormGO"];
	}
	theform.btnPOSTnome.value = NomeButton;
	theform.btnPOSTvalore.value = ValoreButton;
	theform.submit();
}

function RitornaDaRicerca(NomeCampo,Valore){
	try 
	{
		document.FormGO[NomeCampo].value = Valore;
	} 
	catch(e) 
	{
		alert ("Campo "+NomeCampo+" non trovato nella pagina principale");
	} 
}

function RitornaAlPrincipale(NCampo,Valore) {
	Valore=unescape(Valore);
	if (window.opener){
		try{
			window.opener.RitornaDaRicerca(NCampo,Valore);
		}
		catch(er)
		{}
	}else{
		alert("La finestra principale e' stata chiusa");
	}
	self.close();
	document.close;
}

function showtooltip(NomeCampo, Visibile){
	tipo=document.getElementById(NomeCampo).tipo;
	if (""+tipo+""=="undefined") tipo="0";
	if (Visibile==0 && tipo!="2") document.getElementById(NomeCampo).style.display="none";
	if (Visibile==1) document.getElementById(NomeCampo).style.display="";
	if (Visibile==2) document.getElementById(NomeCampo).style.display="";
	if (Visibile==3) document.getElementById(NomeCampo).style.display="none";
	document.getElementById(NomeCampo).tipo=Visibile;
}

// **************************************************************************
// ** POSIZIONE DEL MOUSE   funzione non usata - non funziona con explorer **
// **************************************************************************
// document.onmousemove = mouse_pos(event);
// var asse_x;
// var asse_y;
// function mouse_pos(e){
// 	asse_x=e.clientX;
// 	asse_y=e.clientY;
// }

// **************************************************************************
// **                               ARTICOLI                               **
// **************************************************************************
function art_dispon(ATSESSION_id, CampoArt, CampoMgz){
	// disponibilta' per articolo
	// ATSESSION_id-> sessione
	// CampoArt    -> Nome del campo codice articolo
	// CampoTmgz   -> Nome del campo magazzino
	if (CampoArt=="" || CampoArt==null) return;
	if (CampoMgz=="" || CampoMgz==null) return;
	var Art="";
	var Mgz="";
	if (window.navigator.appName.toLowerCase().indexOf("microsoft")>-1){
		Art=document.FormGO(CampoArt).value;
		Mgz=document.FormGO(CampoMgz).value;
	}else{
		Art=document.forms["FormGO"][CampoArt].value;
		Mgz=document.forms["FormGO"][CampoMgz].value;
	}
	if (Art=="") return;
	if (Mgz=="") Mgz="01";
	LinkX = "start.php?p=MAGA0014.php&art="+Art+"&mgz="+Mgz+"&ATID="+ATSESSION_id;
	//Opzioni="width=450, height=600, top=0, left=0, scrollbars=yes, resizable=yes";
	Opzioni="top=0, left=0, scrollbars=yes, resizable=yes";
	window.open(LinkX, "TargetDispon", Opzioni);
}

function art_prezzi(ATSESSION_id, CampoArt, ClFo){
	// prezzi per articolo cliente-fornitore
	// ATSESSION_id-> sessione
	// CampoArt    -> Nome del campo codice articolo
	// ClFo        -> codice cliente - fornitore
	if (CampoArt=="" || CampoArt==null) return;
	if (ClFo=="" || ClFo==null) return;
	var Art="";
	if (window.navigator.appName.toLowerCase().indexOf("microsoft")>-1){
		Art=document.FormGO(CampoArt).value;
	}else{
		Art=document.forms["FormGO"][CampoArt].value;
	}
	if (Art=="") return;
	LinkX = "start.php?p=MAGA0015.php&art="+Art+"&clfo="+ClFo+"&ATID="+ATSESSION_id;
	//Opzioni="width=450, height=600, top=0, left=0, scrollbars=yes, resizable=yes";
	Opzioni="top=0, left=0, scrollbars=yes, resizable=yes";
	window.open(LinkX, "TargetPrezzi", Opzioni);
}

// **************************************************************************
// ** AJAX: DEFINIZIONI BASE                                               **
// **************************************************************************
function getHttpRequest() {
    var xmlHttp;
    try {
        // Firefox, Opera e Safari, Internet Explorer > 6
        xmlHttp=new XMLHttpRequest();
    } catch (exc) {
        // Internet Explorer vecchie versioni
        try {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        } catch (exc) {
            try {
                xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
            } catch (exc) {
                 // il browser non supporta le richieste XMLHttpRequest
                return false;
            }
        }
    }
    return xmlHttp;
}

function stateChanged(){
	if (xmlHttp.readyState==4){
		var b=xmlHttp.responseText;
		xmlHttp.abort();
		if (b=="") return;
		//alert(b);
		NomeCampo_Tipo=b.substr(0, b.search(";"));
		b=b.substr(b.search(";")+1);
		switch(NomeCampo_Tipo){
		case "3":
			// onblur
			do {
				v=b.substr(0, b.search("%x%"));
				c=v.substr(0, v.search("="));
				v=v.substr(v.search("=")+1);
				v=v.replace("$$","\r\n");
				try {
					if (window.navigator.appName.toLowerCase().indexOf("microsoft")>-1){
						document.FormGO("txt_"+c).value=v;
					}else{
						document.forms["FormGO"]["txt_"+c].value=v;
					}
				} catch (exc) {
					b=b;
				}
				b=b.substr(b.search("%x%")+3);
			}while (b!="");
			break;
		case "4":
			// menu'
			var nomediv=b.substr(0, b.search("#"));
			var menudiv=b.substr(b.search("#")+1);
			document.getElementById(nomediv).innerHTML=menudiv;
			break;
		case "5":
			// listino articoli
			document.getElementById('dialogListin').innerHTML=b;
			break;
		case "100":
			// greybox
			var titolo=b.substr(0, b.search("#"));
			var corpo=b.substr(b.search("#")+1);
			GB_show(titolo,'',400,600,corpo);
			break;
		default:
			ajax_imposta_div(NomeCampo_Tipo, b);
		}
	}
}

function ajax_txt(ATSESSION_id, NomeCampo, NomeCampoDB, Tipo, Valore, Lega){
	// ATSESSION_id	-> sessione
	// NomeCampo	-> Nome del campo dove visualizzare il risultato
	// NomeCampoDB	-> Nome del campo del data base
	// Tipo		-> 1 = ricerca	per articoli;	2=insert anagrafe;	3=onblur	100=greybox	101=menu'
	// Valore	-> valore del campo
	// Lega        -> tabella detail: selezione
	//alert("ajax_txt");
	if (Tipo!="3" && Tipo<"100") ajax_imposta_div(NomeCampo, "");
	xmlHttp=getHttpRequest();
	if (xmlHttp==false) return;
	var url="start.php?p=ajax.php";
	url=url+"&ATID="+ATSESSION_id;
	url=url+"&i="+NomeCampo;
	url=url+"&v="+Valore;
	url=url+"&n="+NomeCampoDB;
	url=url+"&t="+Tipo;
	url=url+"&l="+Lega;
	//alert("ajax_txt "+url);
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

// **************************************************************************
// ** AJAX: FUNZIONE PER LA DEFINIZIONE DEI VALORI DI DEFAULT (onblur)     **
// **************************************************************************
function ajax_txt_onblur(ATSESSION_id, NomeCampo){
	// quando si lascia un campo
	// ATSESSION_id-> sessione
	// NomeCampo   -> Nome del campo dove visualizzare il risultato
	if (NomeCampo==null) return;
	var Valore="";
	try {
		if (window.navigator.appName.toLowerCase().indexOf("microsoft")>-1){
			Valore=document.FormGO(NomeCampo).value;
		}else{
			Valore=document.forms["FormGO"][NomeCampo].value;
		}
	} catch(e) {
		Valore=" ";
	}
	var OldValore="";
	try {
		if (window.navigator.appName.toLowerCase().indexOf("microsoft")>-1){
			OldValore=document.FormGO("h_"+NomeCampo).value;
		}else{
			OldValore=document.forms["FormGO"]["h_"+NomeCampo].value;
		}
	} catch(e){
		OldValore=" ";
	} 
	if (""+OldValore+""=="undefined") return;
	if (Valore==OldValore) return;
	// solo se il valore cambia viene lanciata la richiesta di impostazione dei valori dipendenti dal valore del campo lasciato
	var TuttiValori=tutticampi();
	ajax_txt(ATSESSION_id, NomeCampo, "", "3", escape(TuttiValori), "");
	try {
		if (window.navigator.appName.toLowerCase().indexOf("microsoft")>-1){
			document.FormGO("h_"+NomeCampo).value=Valore;
		}else{
			document.forms["FormGO"]["h_"+NomeCampo].value=Valore;
		}
	} catch(e){
		OldValore=" ";
	}
}

function tutticampi(){
	var tot=0;
	var b="";
	var elemento;
	if (window.navigator.appName.toLowerCase().indexOf("microsoft")>-1){
		tot=document.FormGO.elements.length;
	}else{
		tot=document.forms["FormGO"].elements.length;
	}
	for(cont=0;cont<tot;cont++){
		if (window.navigator.appName.toLowerCase().indexOf("microsoft")>-1){
			elemento=document.FormGO.elements[cont];
		}else{
			elemento=document.forms["FormGO"].elements[cont];
		}
		if(elemento.name.substr(0, 4)=="txt_"){
			b=b+"<campoGO>"+elemento.name+"=";
			if (elemento.type=="checkbox"){
				b=b+elemento.checked;
			}else{
				b=b+elemento.value;
			}
			b=b+"</campoGO>";
		}
	}
	return b;
}
// **************************************************************************
// ** AJAX: FUNZIONI PER LA RICERCA DEI CAMPI CHIAVE                       **
// **************************************************************************
function ajax_txt_onkeydown(e){
	try {
		tipocampo=""+e.target.type+"";
	} catch(e) {
		tipocampo="textarea";
	}
	if (tipocampo=="textarea") return; // nei campi textarea non deve fare il refresh della form
	numerotasto=e.which;
	if (numerotasto==13 || numerotasto==38 || numerotasto==40){
		// numerotasto=13 enter se e' attivo una maschera ajax deve prendere la prima selezione per descrizione
		// numerotasto=38 freccia in su
		// numerotasto=40 freccia in giu'
		if (window.navigator.appName.toLowerCase().indexOf("microsoft")>-1){
			theform = document.FormGO;
		}else{
			theform = document.forms["FormGO"];
		}
		for (indice=1;indice<=1000;indice++){
			campo=theform.elements[indice];
			try {
				b=campo.innerHTML;
				if (b.substr(0, 16)=="Premi enter-&gt;") {
					chiave=b.substr(16);
					if (numerotasto==13) {
						campoajaxnca=theform["campoajaxnca"].value;
						theform[campoajaxnca].value=chiave;
						break;
					}
					campoajaxsel=theform["campoajaxsel"].value;
					campoajaxmax=theform["campoajaxmax"].value;
					if (numerotasto==38 && campoajaxsel>1)                    campoajaxsel--;
					if (numerotasto==40 && campoajaxsel<Number(campoajaxmax)) campoajaxsel++;
					theform["campoajaxsel"].value=campoajaxsel;
					chiave=theform["campoajax"+campoajaxsel].value;
					campo.innerHTML=b.substr(0,16)+chiave;
					// visualizza la riga
					for (i1=1;i1<=Number(campoajaxmax);i1++){
						if (Math.floor(Number(i1)/2)*2!=Number(i1)){
							campoajaxcol=theform["campoajaxcol1"].value;
						}else{
							campoajaxcol=theform["campoajaxcol2"].value;
						}
						if (i1==Number(campoajaxsel)) campoajaxcol=theform["campoajaxcolA"].value;
						theform["btnajax"+i1].style.background=campoajaxcol;
					}
					break;
				}
			} catch(e) {
			}
		}
	}
	//alert(btndefa+" "+btnvalo);
	if (numerotasto==116) __DoPostBack("btnREFRESH", "1", "");	// 116 = F5 refresh form
	if (numerotasto==13) __DoPostBack(btndefa, btnvalo, "");	// 13 enter
	if (numerotasto==13 || numerotasto==16 || numerotasto==17 || numerotasto==18 || numerotasto==116) numerotasto=0; //enter shift ctrl alt F5
	if (numerotasto==20 || numerotasto==27) numerotasto=0; // block-shift esc 
	if (numerotasto==9) numerotasto=0; // tab
	if (numerotasto>=33 && numerotasto<=40) numerotasto=0; // frecce pag+e- home e fine
	//alert(numerotasto);
}

function ajax_txt_ricerca(ATSESSION_id, NomeCampo, NomeCampoDB, Tipo, CampoLega, Lega){
	// varie ricerche
	// ATSESSION_id-> sessione
	// NomeCampo   -> Nome del campo dove visualizzare il risultato
	// NomeCampoDB -> Nome del campo del data base
	// Tipo        -> 1 = ricerca	per articoli: 2= insert anagrafe	3 = onblur
	// CampoLega   -> tabella detail: nome del campo per la ricerca del campo principale
	// Lega        -> tabella detail: selezione
	if (numerotasto==0) return;
	if (NomeCampo=="") return;
	if (NomeCampo==null) return;
	if (NomeCampoDB=="") return;
	if (NomeCampoDB==null) return;
	var Valore="";
	var Valore2="";
	Valore=RicercaValoreCampo(NomeCampo);
	if (Lega!=""){
		Valore2=RicercaValoreCampo("txt_"+CampoLega);
		if (Valore2=="") Valore2=RicercaValoreCampo("txt_"+CampoLega+"_hidden");
		Lega=Lega.replace("--123321--","\""+Valore2+"\"");
	}
	if (Valore=="" && Valore2==""){
		ajax_azzera();
		return;
	}
	//alert("*ajax_txt_ricerca*"+Lega+"*"+CampoLega);
	ajax_txt(ATSESSION_id, NomeCampo, NomeCampoDB, Tipo, Valore, Lega);
}

function ajax_txt_seleziona(NomeCampo, Valore){
	ajax_imposta_div(NomeCampo, "")
	if (window.navigator.appName.toLowerCase().indexOf("microsoft")>-1){
		document.FormGO(NomeCampo).value=Valore;
		document.FormGO(NomeCampo).select();
		document.FormGO(NomeCampo).focus();
	}else{
		document.forms["FormGO"][NomeCampo].value=Valore;
		document.forms["FormGO"][NomeCampo].select();
		document.forms["FormGO"][NomeCampo].focus();
	}
}

function ajax_imposta_div(NomeCampo, Valore){
	var div_ajax="ajax_"+NomeCampo;
	document.getElementById(div_ajax).innerHTML=Valore;
	if (Valore==""){
		document.getElementById(div_ajax).style.border="0px";
		document.getElementById(div_ajax).style.display="none";
	}else{
		document.getElementById(div_ajax).style.border="1px solid #A5ACB2";
		document.getElementById(div_ajax).style.display="";
	}
}

function ajax_azzera(){
	var campo;
	var div_ajax;
	for (indice=1;indice<=1000;indice++){
		if (window.navigator.appName.toLowerCase().indexOf("microsoft")>-1){
			campo=document.FormGO.elements[indice];
		}else{
			campo=document.forms["FormGO"].elements[indice];
		}
		try {
			if (campo.id.substr(0, 3)=="txt"){
				div_ajax="ajax_"+campo.id;
				document.getElementById(div_ajax).style.border="0px";
				document.getElementById(div_ajax).style.display="none";
			}
		} catch(e) {
		}
	}
}

