/*
PER RICHIAMARE LE FUNZIONI DI CONTROLLO

VEDE SE CAMPO TESTO è VUOTO         ==>   CtrlEmpty([OGGETTO],[TESTO ALERT PER L'ESITO NEGATIVO]);
 "   SE IL CAMPO è NUMERICO         ==>   CtrlNumeric([OGGETTO],[TESTO ALERT PER L'ESITO NEGATIVO]);
 "   SE LA VALUTA è CORRETTA      ==> CtrlValute([OGGETTO],[TESTO ALERT PER L'ESITO NEGATIVO],[IMPORTO MASSIMO CONSENTITO]);
 "   SE LA DATA è CORRETTA         ==> CtrlDate(modulo.data,[TESTO ALERT PER L'ESITO NEGATIVO]);

 COMPARA 2 DATE, VERIFICA SE LA DATA DI INIZIO è INFERIORE ALLA FINALE RISPONDENDO TRUE O FALSE==> CtrlDateMaxMin([data iniziale],[data finale]) se TRUE LA CONDIZIONE è VERIFICATA
*/

function CtrlMail(string, req) {
   if(!req && !string.length) return true;
   if(req && !string.length) return false;
   else {
      regexpCtrl = /^(\w+(?:\.\w+)*(?:\-\w+)*)@((?:\w\.)*\w[\w-]{0,66})\.(\w{2,66}(?:\.\w{2,6})?)$/i
      if(!regexpCtrl.test(string)) return false;
   }
   return true;
}

function CtrlDateMaxMin(datai,dataf)
{
   var dd1=datai.split('/');
   var dd2=dataf.split('/');
   var data1= new Date(dd1[2],dd1[1]-1,dd1[0]);
   var dd1=data1.getTime();
   var data2= new Date(dd2[2],dd2[1]-1,dd2[0]);
   var dd2=data2.getTime();

   if (dd1>dd2)
      return false;
   else
      return true;
}

function CtrlEmpty(Obj,AlertText)
{
   if((!Obj.value)||(Obj.value==' '))
      {
      alert(AlertText);
      Obj.focus();
      return false;
      }
      else
      {return true;}
}

function CtrlNumeric(Obj,AlertText)
{
   if(isNaN(Obj.value))
      {
      alert(AlertText);
      Obj.focus();
      return false;
      }
}

function CtrlValute(Obj,AlertText,MaxValuta)
{
//il formato è: 12.000,30 e non 12,000.30(tipo inglese)

   valore=Obj.value;
   valore=valore.replace('.','');
   valore=valore.replace(/,/g,".");
   valore=valore.replace(/-/g,".");
   if (isNaN(valore))
   {
      alert(AlertText);
      Obj.focus();
      return false;
   }

   if (valore>MaxValuta)
   {
      alert('L\'importo max è di '+ MaxValuta);
      Obj.focus();
      return false;
   }
}



function CtrlDate(Obj,AlertText)
{
   var dateStr = Obj.value;
   var datePat = /^(\d{1,2})(\/)(\d{1,2})(\/)(\d{4})$/;
   var matchArray = dateStr.match(datePat); //

   if (matchArray == null)
   {
      alert(AlertText);
      Obj.focus();
      return false;
   }

   day = matchArray[1];
   month = matchArray[3];
   year = matchArray[5];

   if (month < 1 || month > 12)
   {
      alert('Il mese deve essere compreso tra  1 e 12.');
      Obj.focus();
      return false;
   }

   if (day < 1 || day > 31)
   {
      alert('Il giorno deve essere compreso tra 1 e 31.');
      Obj.focus();
      return false;
   }

   if ((month==4 || month==6 || month==9 || month==11)&& day==31)
   {
      alert('Il mese '+month+' non ha 31 giorni!')
      Obj.focus();
      return false;
   }
   if (month == 2)
   {

      var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
      if (day > 29 || (day==29 && !isleap))
      {
         alert('Febbraio ' + year + ' non ha ' + day + ' giorni!');
         Obj.focus();
         return false;
      }
   }
   return true;
}



function FormattaValuta(decimali,maxvalore,elemento){
   if (elemento.contenuto===undefined){
      elemento.contenuto='';
   }

   if (elemento.contenuto==elemento.value){
      return false;
   }

   if ((decimali==0)&& (elemento.value.indexOf(',')!=-1)){
      alert('Non sono previsti valori decimali');
      new_valore=elemento.value.split(',');
      elemento.value=punti(new_valore[0]);
   }

   var valore = new String(elemento.value);
   verifica=valore.replace(/\./g,'');
   verifica=verifica.replace(/,/g,'');
   verifica=verifica.replace(/ /g,'');

   if (isNaN(verifica)) {
      alert('inserire solo valori numerici');
      elemento.value=elemento.contenuto;
      elemento.focus();
      return false;
   }

   max=new Number(maxvalore)

   if (max!=0) {
      contrmax=valore.replace(/\./g,'');
      contrmax=contrmax.replace(/,/g,'.');
      val=new Number(contrmax);

      if (val>max) {
         alert('Il valore massimo inseribile è di '+punti(maxvalore));
         elemento.value=elemento.contenuto;
         elemento.focus();
         return false;
      }
   }

   if(valore.indexOf(',')!= -1 ){

      var new_valore =valore.split(',');

      if (new_valore[1].length > decimali){

         alert('Non è possibile inserire più di '+decimali+' cifre decimali');

         elemento.value = punti(new_valore[0]) + ',' + new_valore[1].slice(0,decimali);

         return false;

      }

      if (punti(new_valore[0])==''){

         elemento.value = '0,' + new_valore[1];

      }

      else

      {

         elemento.value = punti(new_valore[0]) + ',' + new_valore[1];

      }

   }

   else{

      elemento.value=punti(valore);

      //if (valore=='0') elemento.value = '';

   }

   elemento.contenuto=elemento.value;

   return false;

}

function punti(valore)

{

   valore = new String(valore)

   valore=valore.replace(/\./g,'')

   valore=valore.replace(/ /g,'')

   var valoren = ''

   var x = valore.length

   //alert(x)

   //alert(valore)

   if (x>3)

   {

      while ((x < 3) == false)

      {

         valoren= + valore.charAt(x-3) + valore.charAt(x-2)+ valore.charAt(x-1) + valoren

         x=x-3

         if (x!=0) valoren='.' + valoren

      }

      if (x==2)

      {





         valoren = valore.charAt(0) +valore.charAt(1) + valoren

      }

      if (x==1)

      {

         valoren = valore.charAt(0) + valoren

      }

      return valoren

   }

   else

   {

      //if (valore=='0') return '';

      return valore

   }

}
