
//******************************************************************
//*** BOTTONI ONMOUSEOVER ******************************************
//******************************************************************
function highlightButton(s) {
    if ("INPUT"==event.srcElement.tagName)
      event.srcElement.className=s
}

//******************************************************************
//*** CALENDARIO        ********************************************
//******************************************************************

//Funzione che apre la finestra per inserire la data
function openCalendar(iForm, sField)
{
	var queryString;
	queryString  = '?Date=' + escape(sField.value);
	queryString += '&FormName=' + escape(iForm);
	queryString += '&DateFld=' + escape(sField.name);
	try
	{
		var newWin = window.open("calendar.asp" + queryString, "calwin", "dependent=yes,directories=no,height=194,width=218,location=no,menubar=no,personalbar=no,resizable=no,scrollbars=no,status=no,toolbar=no,left=" + eval(event.screenX-8) + ",top=" + eval(event.screenY-12) + "");
		newWin.focus();
	}
	catch (ex)
	{
		window.open("calendar.asp" + queryString, "calwin", "dependent=yes,directories=no,height=194,width=218,location=no,menubar=no,personalbar=no,resizable=no,scrollbars=no,status=no,toolbar=no,left=300,top=200");
	}
}

/******************************************************************************************************
'** Nome					:	InsertImage
'******************************************************************************************************
'** Tipo			    :	function
'** Descrizione   :	Apre la finestra per l'inserimento delle immagini (seleziona_immagini.asp)
'**
'** Par. In       :	sField 			-> Il campo su cui si vuole inserire l'immagine
'**
'** Autore        :	TWT S.p.A. 
'** Data		      :	04/04/2005
'** Modifica      :	
'** Versione      :	1.0.0
'** Da implemen.  :	
'** NOTE          :	
'** Overload 	   	:	
'******************************************************************************************************/
function InsertImage(sField, imgUrl)
{
	var ie4 = (document.all && !document.getElementById);
	var ie5 = (document.all && document.getElementById);
	var ns6 = (!document.all && document.getElementById);

	var strLink = "seleziona_immagini.asp?fld=" + sField + "&imgUrl=" + imgUrl ;

	if(ie4 || ie5)
		window.open(strLink , "SelectImage","scrollbars=NO,status=1,width=534,height=360");
	else
		window.open(strLink , "SelectImage","scrollbars=NO,status=1,width=534,height=390");
		
	return false;
}

//******************************************************************
//*** PDF        ***************************************************
//******************************************************************

//Funzione che apre la finestra per l'inserimento dei pdf seleziona_pdf.asp)
//psso come parametro il nome del campo
function InsertPDF()
{
	var sField = document.form1.nomePFD.value
	var ie4 = (document.all && !document.getElementById);
	var ie5 = (document.all && document.getElementById);
	var ns6 = (!document.all && document.getElementById);

	var strLink = "seleziona_PDF.asp?NomePDF=" + sField;

	if(ie4 || ie5)
	{
		var newwin = window.open(strLink , "InserisciPDF","scrollbars=NO,width=542,height=464");
		newwin.focus();
	}		
	else
	{
		var newwin = window.open(strLink , "InserisciPDF","scrollbars=NO,width=542,height=484");
		newwin.focus();
	}
	return false;
}

//******************************************************************
//*** EDITOR HTML       ********************************************
//******************************************************************

var viewMode = 1; // WYSIWYG

  function Init()
  {
    iView.document.designMode = 'On';
  }
  
  function selOn(ctrl)
  {
	ctrl.style.borderColor = '#0A1741';
	ctrl.style.backgroundColor = '#FFCCCC';
	ctrl.style.cursor = 'hand';	
  }
  
  function selOff(ctrl)
  {
	ctrl.style.borderColor = '#D6DEEF';  
	ctrl.style.backgroundColor = '#D6DEEF';
  }
  
  function selDown(ctrl)
  {
	ctrl.style.backgroundColor = '#8492B5';
  }
  
  function selUp(ctrl)
  {
    ctrl.style.backgroundColor = '#E6E6E6';
  }
    
  function doBold()
  {
	iView.document.execCommand('bold', false, null);
  }


  function doItalic()
  {
	iView.document.execCommand('italic', false, null);
  }

  function doBulList()
  {
    iView.document.execCommand('insertunorderedlist', true, null);
  }

  function doOrdList()
  {
    iView.document.execCommand('insertorderedlist', false, null);
  }

  function doIndent()
  {
    iView.document.execCommand('Indent', false, null);
  }

  function doOutdent()
  {
    iView.document.execCommand('Outdent', false, null);
  }
  
  function doLink()
  {
    iView.document.execCommand('createlink');
  }
  
  function doToggleView()
  {  
    if(viewMode == 1)
    {
      iHTML = iView.document.body.innerHTML;
      iView.document.body.innerText = iHTML;
      tblCtrls.style.display = 'none';
      iView.focus();
      viewMode = 2; // Code
    }
    else
    {
      iText = iView.document.body.innerText;
      iView.document.body.innerHTML = iText;
      tblCtrls.style.display = 'inline';
      iView.focus();
      viewMode = 1; // WYSIWYG
    }
  }
  
//******************************************************************
//*** FINESTRE POPUP    ********************************************
//******************************************************************

//Funzione che apre finestre popup
function apriPopup( cosa, titolo, larghezza, altezza )
{        		
	var newWin = window.open(cosa, titolo, 'toolbar=no,location=no,directories=no,status=yes,menu=no,scrollbars=yes,resizable=no,left=100,top=100,width='+larghezza+',height='+altezza,true);
	newWin.focus();
	return;
}

//***********************************************************
//*** Funzione che apre finestre popup   ********************
//***********************************************************
function openPopup( pagina, larghezza, altezza )
{
	var newWin = window.open(pagina, "Popup", "dependent=yes,directories=no,height=" + altezza + ",width=" + larghezza + ",location=no,menubar=no,personalbar=no,resizable=no,scrollbars=yes,status=no,toolbar=no,left=168,top=161");
	newWin.focus();
}


//***********************************************************
//*** Funzione che apre finestre popup   ********************
//***********************************************************
function openPopupPos( pagina, titolo, larghezza, altezza, top, left )
{
	var newWin = window.open(pagina, titolo, "dependent=yes,directories=no,height=" + altezza + ",width=" + larghezza + ",location=no,menubar=no,personalbar=no,resizable=no,scrollbars=yes,status=no,toolbar=no,left="+left+",top=" +top);
	newWin.focus();
}

//Funzione che apre finestre popup
function OpenCenterPopup( pagina, titolo, larghezza, altezza )
{        		
	var w = 800, h = 600;	
	if (document.all) 
	{
		 w = document.body.clientWidth;
		 h = document.body.clientHeight;
	}
	else //if (document.layers) 
	{
		 w = window.innerWidth;
		 h = window.innerHeight;
	}	
	var popW = larghezza, popH = altezza;	
	var left = (w-popW)/2
	var top = (h-popH)/2;
	var newWin = window.open(pagina, titolo, "dependent=yes,directories=no,height=" + altezza + ",width=" + larghezza + ",location=no,menubar=no,personalbar=no,resizable=no,scrollbars=yes,status=no,toolbar=no,left="+left+",top=" +top);
	newWin.focus();
	return;
}

//***********************************************************
//*** STAMPA     ********************************************
//***********************************************************

function printpage() 
{
	if (window.print) 
	{
		window.print();
	} 
	else 
	{
		alert('Browser non compatibile, usa il menu per la stampa');
	}
}

//***********************************************************
//*** RIGHELLO SULLE TABELLE  *******************************
//***********************************************************
function Righello(IDtabella)
{
	try
	{
		trs=document.getElementById(IDtabella).tBodies[0].rows;
		for(i=0;i<trs.length;i++)
		{
			if (trs[i].className != "Header" && trs[i].className != "Footer")
			{
				tds=trs[i].cells;
				trs[i].onmouseover = function(){
											 if (this.className!="Acceso")
												this.className="Corrente"
												};
				trs[i].onmouseout = function(){
											 if (this.className!="Acceso")
												this.className="Data"
												};
				//trs[i].onclick = function(){this.className="Acceso";};
				//trs[i].ondblclick = function(){this.className="Data";};
			}
		}
	}
	catch(er){}
}


//Questa funzione controlla la validità del codice fiscale immesso
function controllaCF(cf)
{
        var validi, i, s, set1, set2, setpari, setdisp;
        if( cf == '' )  return "Inserire il codice fiscale";
        cf = cf.toUpperCase();
        if( cf.length != 16 )
                return "La lunghezza del codice fiscale non è\n"
                +"corretta: il codice fiscale dovrebbe essere lungo\n"
                +"esattamente 16 caratteri.\n";
        validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        for( i = 0; i < 16; i++ ){
                if( validi.indexOf( cf.charAt(i) ) == -1 )
                        return "Il codice fiscale contiene un carattere non valido `" +
                                cf.charAt(i) +
                                "'.\nI caratteri validi sono le lettere e le cifre.\n";
        }
        set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
        setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
        s = 0;
        for( i = 1; i <= 13; i += 2 )
                s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
        for( i = 0; i <= 14; i += 2 )
                s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
        if( s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0) )
                return "Il codice fiscale non è corretto:\n"+
                        "il codice di controllo non corrisponde.\n";
        return "";
}

//Questa funzione controlla la validità della Partita IVA immessa
function controllaPIVA(pi)
{
        if( pi == '' )  return 'Inserire partita IVA';
        if( pi.length != 11 )
                return "La lunghezza della partita IVA non è\n" +
                        "corretta: la partita IVA dovrebbe essere lunga\n" +
                        "esattamente 11 caratteri.\n";
        validi = "0123456789";
        for( i = 0; i < 11; i++ ){
                if( validi.indexOf( pi.charAt(i) ) == -1 )
                        return "La partita IVA contiene un carattere non valido `" +
                                pi.charAt(i) + "'.\nI caratteri validi sono le cifre.\n";
        }
        s = 0;
        for( i = 0; i <= 9; i += 2 )
                s += pi.charCodeAt(i) - '0'.charCodeAt(0);
        for( i = 1; i <= 9; i += 2 ){
                c = 2*( pi.charCodeAt(i) - '0'.charCodeAt(0) );
                if( c > 9 )  c = c - 9;
                s += c;
        }
        if( ( 10 - s%10 )%10 != pi.charCodeAt(10) - '0'.charCodeAt(0) )
                return "La partita IVA non è valida:\n" +
                        "il codice di controllo non corrisponde.\n";
        return '';

}

//Questa funzione verifica che il numero di telefono immesso sia valido.
//Attenzione: fa uso della funzione numberOK() sotto riportata
function TelNumberOK(data)
{
   var size        = data.length;
   var ReturnValue = true;
   var italiano    = false;
   var index  = 0;
   var prefix = 0;
   var space  = 0;
   var num;

   var CellPrefix = "/333|334|335|336|337|338|339|330" +
      "|360|368|340|347|348|349|320|328|329|380|388|389|392/g";

   if (data == "")
   {
      return true;
   }

   // controlla che il primo carattere sia un + e che
   // ci sia almeno uno spazio
   if ((data.indexOf('+', 0) != 0) || (data.indexOf(' ', 0) == -1))
   {
      ReturnValue = false;
   }
   else
   {
      // aggiunge uno spazio in fondo alla stringa
      data = data + ' ';
      size++;

      // cerca il primo spazio
      space = data.indexOf(' ', index);

      // fintanto che ci sono degli spazi e non c'è un errore
      while ((space != -1) && (space != '') && (ReturnValue == true))
      {
         // legge il numero
         num = data.substring(index + 1, space);

         if ((num == '') || !numberOK(num))
         {
            ReturnValue = false;
         }
         else
         {
            // controlla se il primo prefisso è italiano
            if ((prefix == 0) && (num == 39))
               italiano = true;

            // se è un numero per l'italia il secondo prefisso
            // deve iniziare con uno zero solo se non è un numero di cellulare
            if ((prefix == 1) && (italiano == true) && (num.charAt(0) == 0))
            {
               // controlla che non sia un numero di cellulare
               if (num.match(CellPrefix))
               {
                  ReturnValue = false;
               }
            }

            index = space;
            space = data.indexOf(' ', index + 1);
            prefix ++;
         }
      }
   }

   if ((ReturnValue == true) && (prefix == 3))
   {
      return true;
   }
   else
   {
      //alert ("Numero non valido!\n (Inserisci un numero del tipo +39 06 315000)");
      return false;
   }
}


function numberOK(num)
{
   var i = 0;
   var letter;

   while (i < num.length)
   {
      letter = num.charAt(i);

      if ((letter == '0') || (letter == '1') || (letter == '2') ||
          (letter == '3') || (letter == '4') || (letter == '5') ||
          (letter == '6') || (letter == '7') || (letter == '8') ||
          (letter == '9'))
      {
         i++;
      }
      else
      {
         return false;
      }
   }

   return true;
}
