function select(a, changeTo)
{
   a.style.backgroundColor = changeTo;
}


var lampI = 0; 
var lampAColor = new initArray("", "");
var lampTimerId; 
var lampSpeed = 500; 

var g_Oggi;
var g_Giorno;
var g_Mese;
var g_Anno;

g_Oggi = new Date();

g_Giorno = String(g_Oggi.getDate ()); if(g_Giorno.length == 1) { g_Giorno = '0' + g_Giorno; }
g_Mese   = String(g_Oggi.getMonth() + 1); if(g_Mese.length   == 1) { g_Mese   = '0' + g_Mese;   }
g_Anno   = String(g_Oggi.getFullYear());

g_Oggi = g_Giorno + '/' + g_Mese + '/' + g_Anno;

function EvidenziaRiga()
{
	var mybody = document.getElementById("xbrowser_tbody");
	if(mybody == null) return;
	
	var riga = mybody.getElementsByTagName("tr");
	for (var i=0; i<riga.length; i++) {
		riga[i].onmouseover=function()
		{ 
		   eval("select(this, 'CADBE4');");
		};
		
		riga[i].onmouseout=function()
		{ 
		   eval("select(this, '');");
		};
		
	}
}

function trim(stringa)
{
   while (stringa.substring(0,1) == ' ')
   {
      stringa = stringa.substring(1, stringa.length);
   }
   while (stringa.substring(stringa.length-1, stringa.length) == ' ')
   {
      stringa = stringa.substring(0,stringa.length-1);
   }
   return stringa;
}

function TextToFloat(txt_num)
{
   txt_num = ReplaceGlobal(txt_num, ".", "");
   txt_num = ReplaceGlobal(txt_num, ",", ".");
   
   return parseFloat(txt_num);
}

function chiudi() {
   top.self.opener = self;
   window.top.close();
}

function chcolor(oggetto, colore, sfondo)
{
   lampAColor[0] = colore;
   lampAColor[1] = sfondo;
   obj = document.getElementById(oggetto);
   obj.style.color = lampAColor[lampI]; 
   (lampI < lampAColor.length-1) ? (lampI++) : (lampI=0); 
} 

function initArray()
{ 
  this.length = initArray.arguments.length; 
  for (var i = 0; i < this.length; i++) { 
          this[i] = initArray.arguments[i]; 
   } 

} 

function checkData(dataString)
{
   if (/^(\d\d?)\/(\d\d?)\/(\d\d){1,2}$/.test(dataString)) {
      gg = parseInt(RegExp.$1, 10)
      mm = parseInt(RegExp.$2, 10)
      aa = parseInt(RegExp.$3, 10)
      data = new Date(aa,mm-1,gg)
      
      if(data.getDate()==gg && data.getMonth()+1==mm && (data.getYear()==aa||data.getYear()==1900+aa)) {
         return true;
      }
      else {
         return false;
      }
   }
   else {
      return false;
   }
} 

function modifica_div(valore)
{  
   var div = document.getElementById("div1");
   var width_div = div.style.width; 
   var obj = document.getElementById("div_tab");
       
   div.style.overflowY = valore;
   div.style.width = width_div;
   
   if(valore == "visible")
   {
      obj.style.display = "none";
   }
   else
   {
      obj.style.display = "block";
   }
   
   var TopFrame = top.main;
   if(TopFrame != undefined) 
   {
      var url_pag = TopFrame.window.location.href.substr(TopFrame.window.location.href.indexOf("MovimentiArtx"));
      //alert(url_pag);
      if(url_pag > 0)
      {
         var divB = document.getElementById("div2");
         divB.style.overflowY = valore;
         divB.style.width = width_div;
      }
   }
   
}

function Lampeggia()
{
   obj = document.getElementById("temp");
   
   if(obj.style.visibility == "hidden")
   {
      obj.style.visibility = "visible";
   }
   else
   {
      obj.style.visibility = "hidden";
   }
   
   t = setTimeout("Lampeggia()", 500);
}

function StopLamp()
{
   clearTimeout(t);
}

function ControllaData( NewValue , ErrMsg , OldValue , DataNullaAmmessa )
{
   if( ErrMsg           == undefined ) { ErrMsg           = "";           }
   if( OldValue         == undefined ) { OldValue         = "01/01/9999"; }
   if( DataNullaAmmessa == undefined ) { DataNullaAmmessa = true;         }
   
   ok = checkData( NewValue );
   
   if(ok) {
      //formatto i campi in modo da avere 0+num giorno se è minore di 10
      vNewValue = NewValue.split("/");
      if(vNewValue[0].length == 1) { NewValue = "0" + NewValue; }
      if(vNewValue[1].length == 1) { NewValue = NewValue.substr(0, 3) + "0" + NewValue.substr(3); }
      if(vNewValue[2].length == 2) { NewValue = NewValue.substr(0, 6) + "20" + NewValue.substr(6); }
      if( NewValue  == "01/01/9999" ) {
         ok = DataNullaAmmessa;
      }
   }
   
   if (!ok) {
      if(ErrMsg != "") {
         alert( ErrMsg );
      }
      NewValue = OldValue;
   }
   
   return NewValue;
}

function ControllaEConferma(valore, errore)
{
   // SOSTITUIRE CON LA NUOVA E PIU' COMPLETA   ControllaData
   
   ok = checkData(valore);
   
   if(ok) {
      datanulla = "01/01/9999";
      if (valore  == datanulla) {
         ok = false;
      }
      else {
         if(valore.length == 8) {
            valore = valore.substr(0,6) + '20' + valore.substr(6,2);
         }
      }
   }
   
   if (!ok) {
      alert(errore)
      valore = "01/01/9999";
   }
   
   return valore;
}

function ControllaEConferma2(valore, dataoggi)
{
   // SOSTITUIRE CON LA NUOVA E PIU' COMPLETA   ControllaData
   ok = checkData(valore);
   
   if(ok) {
      datanulla = "01/01/9999";
      if (valore  == datanulla) {
         ok = false;
      }
      else {
         if(valore.length == 8) {
            valore = valore.substr(0,6) + '20' + valore.substr(6,2);
         }
      }
   }
   
   if (!ok) {
      valore = dataoggi;
   }
   
   return valore;
}

function ReplaceGlobal(stringa, oldvalue, newvalue)
{
   if(oldvalue == "." || oldvalue == "*" || oldvalue == "?" || oldvalue == "+" || oldvalue == "(" || oldvalue == ")" || oldvalue == "{" || oldvalue == "}" || oldvalue == "[" || oldvalue == "]" || oldvalue == "^" || oldvalue == "$")
   {
      soldvalue = "\\" + oldvalue;
   }
   else
   {
      soldvalue = oldvalue;
   }
   //document.write(soldvalue);
   return stringa.replace(new RegExp(soldvalue, "g"), newvalue);
}


function ApriFinestra(MyUrl) {
   var x = navigator;
   w = screen.width;
   h = screen.height;
   
   if(x.appName == 'Microsoft Internet Explorer')
   {
      if(x.appVersion.indexOf('MSIE 6.0') != -1)
      {
         // Internet Explorer 6.0
         h = h - 100;
         w = w - 10;
      }
      else if(x.appVersion.indexOf('MSIE 7.0') != -1)
      {
         // Internet Explorer 7.0
         h = h - 100;
      }
   }
   else
   {
      // Non Internet Explorer
   }
   
   strpos = 'top=0px, left=0px,';
   strsiz = 'height=' + h + 'px, width=' + w + 'px';
   window.open(MyUrl, '', 'location=1,' + strpos + strsiz);
}

function LeggiSession(session_name)
{
   /////////////////////////////////////////////////
   //legge una variabile di sessione da javascript//
   /////////////////////////////////////////////////
   
   ret = ajaxpage("/common/funzioni_ajax.asp?Tipo=LeggiSession&SessionName="+session_name, undefined, false);
   return ret;
}

function SettaSession(nome_session, valore_session)
{
   ///////////////////////////////////////////////////////////////
   //Setta una variabile di sessione tramite chiamata javascript//
   ///////////////////////////////////////////////////////////////
   
   ret = ajaxpage("/common/funzioni_ajax.asp?Tipo=SettaSession&Nome="+nome_session+"&Valore="+valore_session, undefined, false);
   //alert(ret);
}

function FormatNumber(num)
{
   ret = ajaxpage("/common/funzioni_ajax.asp?Tipo=FormatNumber&Number="+num, undefined, false);
   return ret;
}

function FormattaNumero(num, decimals)
{
   //uguale alla FormatNumber, ma si possono scegliere i decimali
   
   if(decimals == null)                        { decimals = 2; }
   if(ReplaceGlobal(""+num+"", " ", "") == "") { num      = 0; }
   
   return ajaxpage("/common/funzioni_ajax.asp?Tipo=FormattaNumero&Num="+num+"&Decimals="+decimals, undefined, false);
}

function CountTextareaChrs(txtarea, chrlimit, block_length)
{
   /////////////////////////////////////////////////////////////////////////////////
   //Conta i caratteri di una textarea, eventualmente bloccando futuri inserimenti//
   //di caratteri. Restituisce una stringa in questo formato:                     //
   //'<caratteri_inseriti>|<limite_caratteri>'                                    //
   /////////////////////////////////////////////////////////////////////////////////
   
   if(block_length == null) { block_length = false; }
   
   if(chrlimit == null)
   {
      //non ho posto limiti nella textarea, conto solo i caratteri
      return "" + txtarea.value.length + "|";
   }
   else
   {
      //la textarea è limitata a N caratteri
      if(block_length)
      {
         //la textarea ha un limite, gestisco qui il blocco della textarea
         if(txtarea.value.length > chrlimit)
         {
            txtarea.value = txtarea.value.substr(0, chrlimit);
         }
      }
      return "" + txtarea.value.length + "|" + chrlimit;
   }
}

function XBrowser_Events(EventName, ColumnName, PrimaryKey)
{
   retclk = false;
   
   try
   {
      switch(EventName)
      {
         case "OnClick":
            retclk = XBrowser_OnClick(ColumnName, PrimaryKey);
            break;
         case "OnBlur":
            retclk = XBrowser_OnBlur(ColumnName, PrimaryKey);
            break;
         case "OnKeyDown":
            retclk = XBrowser_OnKeyDown(ColumnName, PrimaryKey);
            break;
         default:
            alert("ERRORE!\n\nDESCRIZIONE:\nEvento non gestito.");
      }
   }
   catch(Errore)
   {
      /*testo = "ERRORE!\n\n"
            + "DESCRIZIONE:\nFallita la chiamata alla function "
            + "XBrowser_"+EventName+"(\""+ColumnName+"\", \""+PrimaryKey+"\").\n\n"
            + "DETTAGLI:\n"+Errore.description+".";
      alert(testo);*/
   }
   
   if(!retclk)
   {
      SelezionaCodice(PrimaryKey);
   }
}

/*function CreaCodiceJs(Code)
{
   ret = ajaxpage("/common/funzioni_ajax.asp?Tipo=CreaCodiceJs&Code="+encodeURIComponent(Code), undefined, false);
   return ret;
}*/

function DateDiff(d_ini, d_fine)
{  
   chkdate = ajaxpage("/common/funzioni_ajax.asp?Tipo=DateDiff&d_ini="+d_ini+"&d_fine="+d_fine, undefined, false);
   
   return chkdate;
}

function CallXBrowser(sql, view, elesrc, parametri, div, testo, testototale, eval_func)
{
   if(testo == null) { testo = ''; }
   if(testototale == null) 
   { 
      testototale = ''; 
      parametri   = parametri + '&NoThTotale=True';
   }
   if(eval_func == null) { eval_func = ''; }
   
   prima = "/common/XBrowser.asp?Sql=";
   sql   = encodeURIComponent( sql );
   dopo  = "&EleSrc=" + elesrc + "&View=" + view + parametri;
   memo  = "&div=" + div + "&testo=" + testo;
   s     = prima + sql + dopo + memo;
   //document.write(s); return;
   
   if(div != "")
   {
      document.getElementById(div).innerHTML = testo;
      ajaxpage(s, div, true, 'XBrowser_SetTotale("' + testototale + '");EvidenziaRiga();'+eval_func);
   }
   else
   {
      return ajaxpage(s, undefined, false);
   }
   //ret = ajaxpage(s, undefined, false);
   //wdw = window.open();
   //wdw.document.write(ret);
}

function CallXBrowserPost(sql, view, elesrc, parametri, div, testo, testototale, eval_func)
{
   ////////////////////////////////////////////////////////////////////
   //Versione della CallXBrowser che passa i parametri tramite post. //
   //I parametri vanno passati nel seguente formato:                 //
   //- "Parametro1=Valore1&Parametro2=Valore2&...&ParametroN=ValoreN"//
   ////////////////////////////////////////////////////////////////////
   
   if(testo == null) { testo = ''; }
   if(parametri == null || trim(parametri) == "")
   {
      parametri = "";
   }
   else
   {
      parametri = "&" + parametri;
   }
   if(testototale == null) 
   { 
      testototale = ''; 
      parametri   = '&NoThTotale=True' + parametri;
   }
   if(eval_func == null) { eval_func = ""; }
   
   addr  = "/common/XBrowser.asp?";
   sql   = encodeURIComponent( sql );
   params = "Sql=" + sql + "&View=" + view + "&EleSrc=" + elesrc + parametri ;
   //document.write(s); return;

   if(div != "")
   {
      document.getElementById(div).innerHTML = testo;
      ajaxpost(addr, params, div, true, 'XBrowser_SetTotale("' + testototale + '");EvidenziaRiga();'+eval_func);
   }
   else
   {
      return ajaxpost(addr, params, undefined, false);
   }
   //ret = ajaxpage(s, undefined, false);
   //alert(ret);
}

function XBrowser_SetTotale(testototale)
{
   elem = document.getElementById('thtotale');
   if(elem == undefined) return;
   
   if(testototale == '') 
   {
      elem.style.display = "none";
      return;
   }
   
   RC = document.getElementById('hdntotale').value;
   testototale = ReplaceGlobal(testototale, "@RC", RC);
   elem.innerHTML = testototale;
}

function SelectAll()
{
	var vettore = document.getElementsByTagName("input");
	for (i = 0 ; i < vettore.length ; i++ )
	{
	   if(vettore[i].type == 'checkbox')
	   {
	      vettore[i].checked = true;
	   }
   }
}

function UnSelectAll()
{
	var vettore = document.getElementsByTagName("input");
	for (i = 0 ; i < vettore.length ; i++ )
	{
	   if(vettore[i].type == 'checkbox')
	   {
	      vettore[i].checked = false;
	   }
   }
}

function SezioneArrotondata_Ini(inBColor, inSfondoOut, inSfondoIn, inBodyBColor, inBodySfondo, inDivId)
{
   //se non ho selezionato un colore di bordo, non ha senso
   //continuare l'esecuzione
   //if(inBColor == null || inBColor == "") { return; }
   
   
   //preparazione del bordo
   if(inBColor == null || inBColor == "") 
   { 
      BColor = " background: transparent; ";
   }
   else
   {
      BColor = " background: " + inBColor + "; ";
   }
   //controllo sullo sfondo esterno
   if(inSfondoOut == null || inSfondoOut == "") 
   { 
      SfondoOut = " background: transparent; "; 
   }
   else
   {
      SfondoOut = " background: " + inSfondoOut + "; ";
   }
   //controllo sullo sfondo interno
   if(inSfondoIn == null || inSfondoIn == "") 
   { 
      SfondoIn = " background: transparent; "; 
   }
   else
   {
      SfondoIn = " background: " + inSfondoIn + "; ";
   }
   //controllo sullo sfondo del corpo
   if(inBodySfondo == null || inBodySfondo == "") 
   { 
      BodySfondo = " background: transparent; "; 
   }
   else
   {
      BodySfondo = " background: " + inBodySfondo + "; ";
   }
   //preparazione del bordo del corpo
   if(inBodyBColor == null || inBodyBColor == "") 
   { 
      BodyBColor = "transparent";
   }
   else
   {
      BodyBColor = inBodyBColor;
   }
   //preparo l'id del div, se l'ho passato
   if(inDivId != null && inDivId != "") 
   {
      //controllo che non esistano altri elementi con lo stesso id
      if(document.getElementById(inDivId) != null)
      {
         DivId = "";
         alert("Esiste gia' un elemento con id uguale a [" + inDivId + "]!\nId non assegnato.");
      }
      else
      {
         DivId = " id='" + inDivId + "' ";
      }
   }
   else
   {
      DivId = "";
   }
   
   
   ///////////////////////////
   //riproduzione del codice//
   ///////////////////////////
   
   //stile di rtop e rbottom
   Stile1 = " display:block; " + SfondoOut;
   //stili comuni di r1, r2 e r3
   Stile2 = "display: block; height: 1px; overflow: hidden; " + BColor;
   //stile di r1
   StileR1 = Stile1 + Stile2 + " margin: 0 5px;";
   //stile di r2
   StileR2 = Stile1 + Stile2 + " margin: 0 3px;";
   //stile di r3
   StileR3 = Stile1 + Stile2 + " margin: 0 2px;";
   //stile di r4
   StileR4 = Stile1 + Stile2 + " margin: 0 1px; height: 1px; ";
   //stile b inferiori
   StileB = "display:block; overfow:hidden; height:1px; " + SfondoIn;
   //stile recupero 2
   StileRec2 = StileB + " margin: 0 2px; ";
   //stile recupero 3
   StileRec3 = StileB + " margin: 0 1px; ";
   //stile recupero 4
   StileRec4 = StileB + " margin: 0 1px; ";
   
   
   //scrittura
   document.write("<b style='" + Stile1 + "'>");
   document.write("<b style='" + StileR1 + "'></b>");
   document.write("<b style='" + StileR2 + "'><b style='" + StileRec2 + "'></b></b>");
   document.write("<b style='" + StileR3 + "'><b style='" + StileRec3 + "'></b></b>");
   document.write("<b style='" + StileR4 + "'><b style='" + StileRec4 + "'></b></b>");
   document.write("</b>");
   document.write("<div " + DivId + " style='" + BodySfondo + " border-left:1px solid " + BodyBColor + "; border-right:1px solid " + BodyBColor + "; padding-left:5px; padding-right:5px;'>");
}

function SezioneArrotondata_End(inBColor, inSfondoOut, inSfondoIn)
{
   //se non ho selezionato un colore di bordo, non ha senso
   //continuare l'esecuzione
   if(inBColor == null || inBColor == "") { return; }
   
   
   //controllo sullo sfondo esterno
   if(inSfondoOut == null || inSfondoOut == "") 
   { 
      SfondoOut = " background: transparent; "; 
   }
   else
   {
      SfondoOut = " background: " + inSfondoOut + "; ";
   }
   //controllo sullo sfondo interno
   if(inSfondoIn == null || inSfondoIn == "") 
   { 
      SfondoIn = " background: transparent; "; 
   }
   else
   {
      SfondoIn = " background: " + inSfondoIn + "; ";
   }
   //preparazione del colore del bordo
   BColor = " background: " + inBColor + "; "
   
   
   ///////////////////////////
   //riproduzione del codice//
   ///////////////////////////
   
   //stile di rtop e rbottom
   Stile1 = " display:block; " + SfondoOut;
   //stili comuni di r1, r2 e r3
   Stile2 = "display: block; height: 1px; overflow: hidden; " + BColor;
   //stile di r1
   StileR1 = Stile1 + Stile2 + " margin: 0 5px;";
   //stile di r2
   StileR2 = Stile1 + Stile2 + " margin: 0 3px;";
   //stile di r3
   StileR3 = Stile1 + Stile2 + " margin: 0 2px;";
   //stile di r4
   StileR4 = Stile1 + Stile2 + " margin: 0 1px; height: 1px; ";
   //stile b inferiori
   StileB = "display:block; overfow:hidden; height:1px; " + SfondoIn;
   //stile recupero 2
   StileRec2 = StileB + " margin: 0 2px; ";
   //stile recupero 3
   StileRec3 = StileB + " margin: 0 1px; ";
   //stile recupero 4
   StileRec4 = StileB + " margin: 0 1px; ";
   
   
   //scrittura
   document.write("</div>");
   document.write("<b style='" + Stile1 + "'>");
   document.write("<b style='" + StileR4 + "'><b style='" + StileRec4 + "'></b></b>");
   document.write("<b style='" + StileR3 + "'><b style='" + StileRec3 + "'></b></b>");
   document.write("<b style='" + StileR2 + "'><b style='" + StileRec2 + "'></b></b>");
   document.write("<b style='" + StileR1 + "'></b>");
   document.write("</b>");
}

function LeggiApplication(application_name)
{
   /////////////////////////////////////////////////
   //legge una variabile di sessione da javascript//
   /////////////////////////////////////////////////
   
   ret = ajaxpage("/common/funzioni_ajax.asp?Tipo=LeggiApplication&ApplicationName="+application_name, undefined, false);
   return ret;
}

function VbCrLf()
{
   return String.fromCharCode("13") + String.fromCharCode("10");
}

function SessionAbandon()
{
   /////////////////////////////////////////////////////////////
   //Tenta di forzare un Session.Abandon tramite chiamata ajax//
   /////////////////////////////////////////////////////////////
   
   ret = ajaxpage("/common/funzioni_ajax.asp?Tipo=AbbandonaSession", undefined, false);
   //return ret;
}

function CreaPopup(nome, view, modo, sigla, cod, siglacont, table, retv, condsupp, formatting, samewindow)
{
   if(sigla != ""){ sigla = "&sigla=" + sigla; }
   if(cod != ""){ cod = "&cod=" + cod; }
   if(siglacont != ""){ siglacont = "&siglacont=" + siglacont; }
   if(condsupp == null || condsupp == ""){ condsupp = ""; } else { condsupp = "&csup=" + condsupp; }
   if(formatting == null){ formatting = ""; } else { formatting = "&f=" + encodeURIComponent(formatting); }
   if(samewindow == null) { samewindow = false; }
   
   s = nome + "?View="+view+"&modo="+modo+sigla+cod+siglacont+"&table="+table+"&retv="+retv+condsupp+formatting;
   //alert(s);
   
   if(document.getElementById("db_log") != null) { document.getElementById("db_log").innerHTML = s; }
   if(!samewindow)
   {
      ret = window.showModalDialog(s, '','dialogHeight:600px;dialogWidth:700px;status:yes;');
      return ret;
   }
   else
   {
      location.href = s;
   }
}
