// JavaScript Document
/*
LINK srl: 31/10/2003 - util.js - procedure e funzioni di utilità 
elenco: printIt, openwin
*/
    
// funzione per la stampa
  function printIt() {  
    var NS = (navigator.appName == "Netscape");
    var VERSION = parseInt(navigator.appVersion);
    if (VERSION > 3) {  
       if (NS) window.print()
       else {           
            if (navigator.appVersion.indexOf('MSIE 5.') == -1) {
               var WebBrowser = '<object id="WebBrowser1" width=0 height=0 classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></object>';
               document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
               WebBrowser1.ExecWB(6, 2);
               WebBrowser1.outerHTML = ""  
            }
            else window.print() 
       }
    }
  }  

var finestra;
// funzione di apertura di una nuova finestra
  function openwin (theurl,tipo) {
	switch (tipo) {
	  case 1:
	       scrollpage = 'yes';
           w = 640;
	       if (window.screen.width > 800) h = 668
           else h = 480
	       break
	  case 2:
	       scrollpage = 'yes';
           w = 360;
           h = 360;
	}
    features = 'toolbar=no,location=no,resizable=no,scrollbars='+ scrollpage + ',status=0,width='+ w + ',height=' + h + ',innerWidth=' + w +',innerHeight=' + h + ',top=20,left=20,screenX=20,screenY=20'; 
    finestra=window.open(theurl,'finestra',features);
    finestra.focus()
  }


/* funzione che formatta un campo di testo togliendo gli spazi bianchi e CrLf all'inizio e alla fine
e sostituendo più spazi bianchi con un solo spazio bianco */
   function formatta (f,flag) {
     trimLeadingBlanksCrLf(f);
     blanksToblank(f);
	 if (flag && (f.value.length > 0)) f.value=f.value.charAt(0).toUpperCase()+f.value.slice(1)
   }

//funzione che toglie gli spazi bianchi e gli CrLf all'inizio e alla fine di un campo di testo
  function trimLeadingBlanksCrLf(f) {
//toglie i blank e i CrLf all'inizio
    if (f.value.length > 0) {
	   for (i=0; i < f.value.length; i++) {
		   carat=f.value.charAt(i);
		   if ((carat != ' ') && (carat != '\r') && (carat != '\n')) {
			   f.value=f.value.slice(i);
		       break
		   }
	   }
	   if (i==f.value.length) f.value='';
	}
//toglie i blank e CrLf alla fine
    if (f.value.length > 0) {
	   for (i=f.value.length-1; i > -1; i--) {
		   carat=f.value.charAt(i);
		   if ((carat != ' ') && (carat != '\r') && (carat != '\n')) {
			   f.value=f.value.slice(0,i+1);
		       break
		   }
	   }
	}
  }
  

//funzione che sostituisci più spazi bianchi con uno spazio bianco
  function blanksToblank(f){
    if (f.value.length > 0) {
       firstBlank = f.value.indexOf(' ');
       lastBlank = f.value.lastIndexOf(' ');
       if (firstBlank < lastBlank) {
          newstr = '';
     	  tmpstr = f.value;
          iBlank = firstBlank;
     	  while (iBlank > 0) { 
     	        newstr = newstr + tmpstr.substring(0,iBlank);
     	        tmpstr = LtrimBlanks(tmpstr.substring(iBlank,tmpstr.length));
     	        newstr = newstr + ' ';
     	        iBlank = tmpstr.indexOf(' ');
     	  }
          newstr = newstr + tmpstr;
          f.value = newstr
       }
    }
  }

//funzione che toglie gli spazi bianchi all'inizio di una stringa
  function LtrimBlanks(str) {
//toglie i blank
    if (str.length > 0) {
       carat=str.charAt(0);	
       while (carat == ' ' ) {
             str = str.substring(1,str.length);	
             carat = str.charAt(0)
       }
    }
    return str
  }
  
function validaEmail(f) {
  var checkOK = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzƒŠŒšœŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþ0123456789-@_.';
  var checkStr = f.value;
  var allValid = false;
  for (i = 0;  i < checkStr.length;  i++) {
       allValid = false;
       ch = checkStr.charAt(i);
       for (j = 0; j < checkOK.length; j++) {
            if (ch == checkOK.charAt(j)) {
               allValid=true;
               break
            }   
       }      
       if (!allValid) {
          alert('Attenzione! Inserire un indirizzo di e-mail valido');
          f.focus();    
          return false
       }
  }
  var badValue = 1;
  var posI = checkStr.indexOf('\@');
  var posF = checkStr.lastIndexOf('\@');
  if (posI > -1) {
     if (posI > 0) {
        if (posF < checkStr.length - 1) {
	       if (posI >= posF) badValue = 0
	       else alert('Attenzione! Inserire un indirizzo di e-mail valido')
	    }
	    else alert('Attenzione! Inserire un indirizzo di e-mail valido')
     }
     else alert('Attenzione! Inserire un indirizzo di e-mail valido')
  }
  else alert('Attenzione! Inserire un indirizzo di e-mail valido')
  if (badValue) {
     f.focus(); 
     return false
  }
  var badValue = 1;
  var pospI = checkStr.indexOf('\.');
  var pospF = checkStr.lastIndexOf('\.');
  if (pospI > -1) {
     if (pospI < posI & pospF < posI) alert('Attenzione! Inserire un indirizzo di e-mail valido')
     else {
	    if (pospF == checkStr.length-1) alert ('Attenzione! Inserire un indirizzo di e-mail valido')    	
          else badValue = 0
     }
  }
  else alert('Attenzione! Inserire un indirizzo di e-mail valido')
  if (badValue) {
     f.focus();
     return false
  }
  return true
}
