// ====================================================================
//                      FUNZIONI DI VALIDAZIONE FORM
//                  copyright Antares di Sbrana Saverio
// ====================================================================


function cambiaColore(elemento,colore)
{
	elemento.style.backgroundColor=colore;
}
function cambiaColoreResetta(elemento)
{
	elemento.style.backgroundColor='#FFFFFF';
}
function erroreMsg(messaggio,elemento)
{
         // questa funzione viene utilizzata per aprire una
         // finestra di alert ed inserire un messaggio.
         ShowTip(this, '<b>Alert</b>:<br/>'+messaggio);
         //alert(messaggio)
         //elemento.select();
         elemento.focus();
		 //alert(elemento.style.backgroundColor)
		 //cambiaColore(elemento,'#FFD7E3')
}

// ----------------------------------------------

function conSeVuoto(elemento)
{
         // funzione che controlla se un campo testo è vuoto

         if (elemento.value.length<1) return true;
         return false
}

// -------------------------------------------------

function conSeSelezionato(elemento)
{		 
         // funzione che controlla se un radiobutton è selezionato
		 
         if (elemento[0].checked) return false;
         if (elemento[1].checked) return false;
		 if (elemento[2].checked) return false;
		 return true
}
// ----------------------------------------------

function conSeVuotoTextArea(elemento)
{
         // funzione che controlla se un campo textarea è vuoto
         if (elemento.value=="") return true;
         return false
}

// ----------------------------------------------

function conSoloNumeri(elemento)
{
    // funzione che permette di controllare se in un campo testo
    // siano presenti solo numeri.

    var alfabeto="abcdefghilmnopqrstuvzwxyjk\|!£$%&/()=?'^èé[*+]çò@°à#§ù_-:.;,<>"
    var lunghezza=elemento.value.length
    var stringa=elemento.value.toLowerCase()
    var index=0
    var num=0
    for (index=0; index<lunghezza; index++)
    {
        for (num=0; num<alfabeto.length; num++)
        {
            carStringa=stringa.charAt(index)
            carAlfabeto=alfabeto.charAt(num)
            if (carStringa==carAlfabeto) return true
        }
    }
return false
}

// --------------------------------------------------------------

function conLunghezza(elemento,dimensione)
{
         // funzione che permette di validare un campo testo
         // solo se ha un tot predefinito di caratteri.

         if (elemento.value.length!=dimensione) return true;
         return false
}

// -------------------------------------------------------------

function conEmail(email)
{
         // funzione che controlla se un indirizzo email
         // è stato inserito correttamente (@ e .)
         
         if (email.value.indexOf("@") == -1)
         {
         return true;}
         if (email.value.indexOf(".") == -1)
         {
         return true;}
         return false;
}

// -----------------------------------------------

function conLungMinima(elemento,minimo)
{
         // funzione che verifica se un campo testo ha una lunghezza minore
         // di un dato numero.
         
         if (elemento.value.length<minimo) return true
         return false
}

// ---------------------------------------------

function conLista(elemento)
{
	// funzione che controlla se da una lista è stata
	// fatta una scelta (controllando che non sia valore 0)
	var sel = elemento.selectedIndex;
	if (sel == 0)return true;
	return false;
}

// ----------------------------------------------

function conListaEmail(lista)
{
         // funzione che controlla se in una lista di email
         // contenuta in una stringa e separati l'uno dall'altro
         // tramite virgola, sia presente qualche email
         // in un formato non corretto (@ e . obbligatori)
         
         var listaEmail=lista.value.split(',')
         var numeroItem=listaEmail.length
         for (cont=0; cont<numeroItem; cont++)
         {
             if (conEmail(listaEmail[cont]))
             {
                msg="L'email "+listaEmail[cont]+" è in un formato non valido"
                erroreMsg(msg,lista)
                return true
             }
         }
         return false
}
 //----------------------------------------------------
function caratteriBrutti(elemento)
{
    // funzione che permette di controllare se in un campo testo
    // non siano presenti caratteri speciali.

    var alfabeto="\|!£$%&/()=?'^èé[*+]çò@°à#§ù_:.;,<> "
    var lunghezza=elemento.value.length
    var stringa=elemento.value.toLowerCase()
    var index=0
    var num=0
    for (index=0; index<lunghezza; index++)
    {
        for (num=0; num<alfabeto.length; num++)
        {
            carStringa=stringa.charAt(index)
            carAlfabeto=alfabeto.charAt(num)
            if (carStringa==carAlfabeto) return true
        }
    }
return false
}

function apriFin(url,variabili,nome,larghezza,altezza)
{
	status=0
	toolbar=0
	opzioni="status="+status+",scrollbars=1,toolbar="+toolbar+",width="+larghezza+",height="+altezza;
	comando=nome+"=window.open('"+url+"?user="+variabili+"','"+nome+"','"+opzioni+"')";
	
    eval(comando);
}
function sessioneScaduta()
{
	window.location= "index.php?pagina=Sessione Scaduta";
}

/**
 * Displays an confirmation box beforme to submit a "DROP/DELETE/ALTER" query.
 * This function is called while clicking links
 *
 * @param   object   the link
 * @param   object   the sql query to submit
 *
 * @return  boolean  whether to run the query or not
 */
function confirmLink(theLink, theSqlQuery)
{
    // Confirmation is not required in the configuration file
    if (confirmMsg == '') {
        return true;
    }

    var is_confirmed = confirm(confirmMsg + ' :\n' + theSqlQuery);
    if (is_confirmed) {
        theLink.href += '&is_js_confirmed=1';
    }

    return is_confirmed;
} // end of the 'confirmLink()' function

/**
 * Sets/unsets the pointer and marker in browse mode
 *
 * @param   object   the table row
 * @param   string   the action calling this script (over, out or click)
 * @param   string   the default background color
 * @param   string   the color to use for mouseover
 * @param   string   the color to use for marking a row
 *
 * @return  boolean  whether pointer is set or not
 */
function setPointer(theRow, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
    var theCells = null;

    // 1. Pointer and mark feature are disabled or the browser can't get the
    //    row -> exits
    if ((thePointerColor == '' && theMarkColor == '')
        || typeof(theRow.style) == 'undefined') {
        return false;
    }

    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    // 3. Gets the current color...
    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;
    // 3.1 ... with DOM compatible browsers except Opera that does not return
    //         valid values with "getAttribute"
    if (typeof(window.opera) == 'undefined'
        && typeof(theCells[0].getAttribute) != 'undefined') {
		
        currentColor = theCells[0].getAttribute('bgcolor');
        domDetect    = true;
    }
    // 3.2 ... with other browsers
    else {
        currentColor = theCells[0].style.backgroundColor;
        domDetect    = false;
    } // end 3

    // 4. Defines the new color
    // 4.1 Current color is the default one
    if (currentColor == ''
        || currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
        if (theAction == 'over' && thePointerColor != '') {
            newColor = thePointerColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor = theMarkColor;
        }
    }
    // 4.1.2 Current color is the pointer one
    else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()) {
        if (theAction == 'out') {
            newColor = theDefaultColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor = theMarkColor;
        }
    }
    // 4.1.3 Current color is the marker one
    else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
        if (theAction == 'click') {
            newColor = (thePointerColor != '')
                     ? thePointerColor
                     : theDefaultColor;
        }
    } // end 4

    // 5. Sets the new color...
    if (newColor) {
        var c = null;
        // 5.1 ... with DOM compatible browsers except Opera
        if (domDetect) {
            for (c = 0; c < rowCellsCnt; c++) {
				stringa = theCells[c].innerHTML;
				//alert(stringa.substring(stringa.indexOf("Pagamento"),190));
				if (stringa.indexOf("%") !=  '-1')
				{
					//alert('ok');
					//theCells[c].setAttribute('FFCC99', 'FFCC99', 0);
				}
				else
				{
					//alert('NON ok');
                theCells[c].setAttribute('bgcolor', newColor, 0);
				}
				
            } // end for
        }
        // 5.2 ... with other browsers
        else {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].style.backgroundColor = newColor;
            }
        }
    } // end 5

    return true;
} // end of the 'setPointer()' function


/**
 * Checks/unchecks all tables
 *
 * @param   string   the form name
 * @param   boolean  whether to check or to uncheck the element
 *
 * @return  boolean  always true
 */
function setCheckboxes(the_form, do_check)
{
    var elts      = document.forms[the_form].elements['selected_tbl[]'];
    var elts_cnt  = (typeof(elts.length) != 'undefined')
                  ? elts.length
                  : 0;

    if (elts_cnt) {
        for (var i = 0; i < elts_cnt; i++) {
            elts[i].checked = do_check;
        } // end for
    } else {
        elts.checked        = do_check;
    } // end if... else

    return true;
} // end of the 'setCheckboxes()' function

 
/**
  * Checks/unchecks all options of a <select> element
  *
  * @param   string   the form name
  * @param   string   the element name
  * @param   boolean  whether to check or to uncheck the element
  *
  * @return  boolean  always true
  */
function setSelectOptions(the_form, the_select, do_check)
{
    var selectObject = document.forms[the_form].elements[the_select];
    var selectCount  = selectObject.length;

    for (var i = 0; i < selectCount; i++) {
        selectObject.options[i].selected = do_check;
    } // end for

    return true;
} // end of the 'setSelectOptions()' function

function WOpen(url,name,params_str,width,height,scrollbars,status,resize) {
if (!params_str) {
params_str='';
}
params_str+=width?(params_str?',':'')+'width='+width:'';
params_str+=height?(params_str?',':'')+'height='+height:'';
params_str+=scrollbars!==(void 0)?(params_str?',':'')+'scrollbars='+scrollbars:'';
params_str+=status!==(void 0)?(params_str?',':'')+'status='+status:'';
params_str+=resize!==(void 0)?(params_str?',':'')+'resizable='+resize:'';
new_w=window.open(url,name,params_str);
new_w.focus();
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; 
  document.MM_sr=new Array; 
  for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null)
   {
	   document.MM_sr[j++]=x; 
	   if(!x.oSrc) 
	   	x.oSrc=x.src; x.src=a[i+2];
	}
}


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function setFile(nome,dimensione,tipo,url)
{	
	window.opener.document.inserisciProtocollo.allegatoUrl.value 					= url; 
	window.opener.document.inserisciProtocollo.allegatoNome.value 					= nome; 
	window.opener.document.inserisciProtocollo.allegatoDimensione.value 			= dimensione; 
	window.opener.document.inserisciProtocollo.allegatoTipo.value 					= tipo; 
	window.opener.document.inserisciProtocollo.cancellaProtocollo.disabled 			= false;
	window.opener.document.inserisciProtocollo.cancellaProtocollo.style.visibility 	= 'visible';
	window.opener.document.inserisciProtocollo.allegaFile.disabled 					= true;
	window.opener.document.inserisciProtocollo.allegaFile.style.visibility 			= 'hidden';

	//alert (file);
	self.close();
}

function removeFile()
{	
	window.opener.document.inserisciProtocollo.allegatoUrl.value 					= ""; 
	window.opener.document.inserisciProtocollo.allegatoNome.value 					= ""; 
	window.opener.document.inserisciProtocollo.allegatoDimensione.value 			= ""; 
	window.opener.document.inserisciProtocollo.allegatoTipo.value 					= ""; 
	window.opener.document.inserisciProtocollo.allegaFile.disabled 					= false;
	window.opener.document.inserisciProtocollo.allegaFile.style.visibility 			= 'visible';
	window.opener.document.inserisciProtocollo.cancellaProtocollo.disabled 			= true;
	window.opener.document.inserisciProtocollo.cancellaProtocollo.style.visibility 	= 'hidden';

	//alert (file);
	self.close();
}

function setFileModifica(nome,dimensione,tipo,url)
{	
	window.opener.document.modificaProtocollo.allegatoUrl.value 					= url; 
	window.opener.document.modificaProtocollo.allegatoNome.value 					= nome; 
	window.opener.document.modificaProtocollo.allegatoDimensione.value 			= dimensione; 
	window.opener.document.modificaProtocollo.allegatoTipo.value 					= tipo; 
	window.opener.document.modificaProtocollo.cancellaProtocollo.disabled 			= false;
	window.opener.document.modificaProtocollo.cancellaProtocollo.style.visibility 	= 'visible';
	window.opener.document.modificaProtocollo.allegaFile.disabled 					= true;
	window.opener.document.modificaProtocollo.allegaFile.style.visibility 			= 'hidden';

	//alert (file);
	self.close();
}

function removeFileModifica()
{	
	window.opener.document.modificaProtocollo.allegatoUrl.value 					= ""; 
	window.opener.document.modificaProtocollo.allegatoNome.value 					= ""; 
	window.opener.document.modificaProtocollo.allegatoDimensione.value 			= ""; 
	window.opener.document.modificaProtocollo.allegatoTipo.value 					= ""; 
	window.opener.document.modificaProtocollo.allegaFile.disabled 					= false;
	window.opener.document.modificaProtocollo.allegaFile.style.visibility 			= 'visible';
	window.opener.document.modificaProtocollo.cancellaProtocollo.disabled 			= true;
	window.opener.document.modificaProtocollo.cancellaProtocollo.style.visibility 	= 'hidden';

	//alert (file);
	self.close();
}

function FocusMe()
{
       setTimeout("self.focus()",100);
}
