    /*
    ********************************************** FIELDS.JS *************************************************
    QUESTO FILE CONTIENE FUNZIONI COMUNI PER IL CONTROLLO SUI CAMPI DELLE FORM : FORMATTAZIONE, OBBLIGATORIETA' ETC..
    */
    /*
    ********************************** CHECK FIELDS ********************************************************
    FUNZIONE CHE CONTROLLA OBBLIGATORIETA' DEI CAMPI IN FUNZIONE DELL'ATTRIBUTO REQUIRED(VALORI TRUE O FALSE).
    SE UN CAMPO REQUIRED NON E' VALORIZZATO ALLORA VIENE EVIDENZIATO CON UN NUOVO COLORE E L'UTENTE VIENE AVVISATO CON UN MESSAGGIO.
    LA FUNZIONE RITORNA TRUE SE TUTTI I CAMPI REQUIRED SONO VALORIZZATI, FALSE IN CASO CONTRARIO.
    */

    function checkFields(obj){
        collection = obj.elements ;
        bOk = true;
        bPrimo = true;
        for (var i=0; i < collection.length ; i++) {
            var temp = collection(i);
            if (temp.required == "true" ){

                if (temp.value == "" || temp.value == " "){
                    bOk = false;
                    temp.style.backgroundColor = 'pink';
                    if (bPrimo){
                        bPrimo = false;
                        if (! temp.disabled == "true"){
                        temp.focus();
                        }
                    }
                }
                else{
                    temp.style.backgroundColor = '';
                }
            }
        }
        if (! bOk){
            alert("Alcuni campi obbligatori non sono valorizzati !");
        }
        return bOk;
        }
    /*
    ********************************** LOOKLETTERE ********************************************************
    FUNZIONE CHE CONTROLLA CHE IN UN CAMPO VENGANO INSERITI SOLAMENTE LETTERE E QUINDI NON CARATTERI NUMERICI.
    DA ASSOCIARE ALL'EVENTO ONKEYPRESS.
    */

    function lookLettere(obj) {
            var valore = 0;
            numero=true;
            valore =event.keyCode;

            if ((valore >= 65 && valore <= 90) || (valore >= 97 && valore <= 122 ) ){

                    event.returnValue=true;
            }
            else{

                    event.returnValue=false;
            }
    }
    /*
    ********************************** LOOK INTEGER ********************************************************
    FUNZIONE CHE CONTROLLA CHE IN UN CAMPO VENGANO INSERITI SOLAMENTE CARATTERI NUMERICI
    DA ASSOCIARE ALL'EVENTO ONKEYPRESS.
    */
     function lookInteger(obj) {
        var valore = 0;
        numero=true;
        valore =event.keyCode;
        if ((valore > 47 && valore < 58) || (valore == 8) ){

            event.returnValue=true;
        }
        else{

            event.returnValue=false;
        }
    }

    /*
    ********************************** LOOK DECIMAL ********************************************************
    FUNZIONE CHE CONTROLLA CHE IN UN CAMPO VENGANO INSERITI SOLAMENTE CARATTERI NUMERICI ED EVENTUALMENTE LA VIRGOLA PER I DECIMALI
    DA ASSOCIARE ALL'EVENTO ONKEYPRESS.

    /* Ro-Planet function */
    function lookDecimal(obj) {
        var valore = 0;
        numero = true;
        valore = event.keyCode;

        // 44 = ,
        // 45 = -
        if (valore > 47 && valore < 58 || valore == 44 || valore == 45){
            if (valore == 44) {
                var i, stringa;
                stringa = obj.value;
                for (i=0; i<stringa.length; i++){
                    if (stringa.charAt(i) == ","){
                        event.returnValue=false;
                        break;
                    }
                }
            } else {
                event.returnValue=true;
            }
        }
        else {
            event.returnValue=false;
        }
    }
    /* end Ro-Planet function */
    /*
    ********************************** VALIDA DATA ********************************************************
    FUNZINONE PRIVATA: UTILIZZARE LA FUNZIONE CHECKDATE
    */
    function validaData(valore)
    {
     var dateArray = Array();
     dateArray = valore.split("-");

    if (dateArray.length!=3)
    {
        dateArray = valore.split("/");
        if (dateArray.length!=3)
        {
            dateArray = valore.split(" ");
            if (dateArray.length!=3)
            {
                errorMessage = "Formatul datei nu este corect";
                alert(errorMessage);
                return false;
            }
        }
    }

    giorno = parseInt(dateArray[2],10);
    mese = parseInt(dateArray[1],10);
    anno = parseInt(dateArray[0],10);


    if (isNaN(giorno) || isNaN(mese) || isNaN(anno))
        {
        errorMessage = "Formatul datei nu este corect";
        alert(errorMessage);
        return false;
        }

    // Controllo anno
    // mill bug
     if (dateArray[0].length == 3 || dateArray[0].length > 4)
     { alert("Formatul datei nu este corect");
       return false;
     }
     if (anno<100)
      {
      if (anno<50) anno = 2000 + anno;
      else anno = 1900 + anno;
      }

    //Controllo mese
    if(dateArray[1].length > 2)
     {
      alert("Luna gresita");
     }

    if (mese <= 0 || mese > 12)
              {
              errorMessage = "Luna gresita";
              alert(errorMessage);
              return false;
              }

    if (mese==4 || mese==6 || mese==9 || mese==11) nDays = 30;
    else nDays = 31;

    // Controllo anno bisestile
    if (mese==2)
      if ((anno%4==0 && anno%100!=0) || anno%400==0) nDays = 29;
      else nDays = 28;

    // Controllo giorno
    if(dateArray[2].length > 2)
     {
      alert("Zi gresita");
     }

    if (giorno<=0 || giorno>nDays)
        {
        errorMessage = "Zi gresita";
        alert(errorMessage);
        return false;
        }

    // Se arrivo qui allora la data è valida
    strData = ""
    if (giorno<10)
      {strData = "0" + giorno + "-"}
    else
      {strData = giorno + "-"}

    if (mese<10)
      {strData = strData + "0" + mese + "-"}
    else
      {strData = strData + mese + "-"}

    return strData + anno;
    }
    
   //validazione data ok 
   function validaDataOk(valore)
    {
    if(valore.length > 0){
    
     var dateArray = Array();
     dateArray = valore.split("/");

    if (dateArray.length!=3)
    {
        errorMessage = "Formatul datei nu este corect (zz/ll/aaaa)";
        alert(errorMessage);
        return false;
    }

    giorno = parseInt(dateArray[0],10);
    mese = parseInt(dateArray[1],10);
    anno = parseInt(dateArray[2],10);
    

    if (isNaN(giorno) || isNaN(mese) || isNaN(anno))
        {
        errorMessage = "Formatul datei nu este corect";
        alert(errorMessage);
        return false;
        }

    // Controllo anno
    // mill bug
     if (dateArray[2].length == 3 || dateArray[2].length > 4)
     { alert("Formatul datei nu este corect");
       return false;
     }
     if (anno<100)
      {
      if (anno<50) anno = 2000 + anno;
      else anno = 1900 + anno;
      }

    //Controllo mese
    if(dateArray[1].length > 2)
     {
      alert("Luna gresita");
     }

    if (mese <= 0 || mese > 12)
              {
              errorMessage = "Luna gresita";
              alert(errorMessage);
              return false;
              }

    if (mese==4 || mese==6 || mese==9 || mese==11) nDays = 30;
    else nDays = 31;

    // Controllo anno bisestile
    if (mese==2)
      if ((anno%4==0 && anno%100!=0) || anno%400==0) nDays = 29;
      else nDays = 28;

    // Controllo giorno
    if(dateArray[0].length > 2)
     {
      alert("Zi gresita");
     }

    if (giorno<=0 || giorno>nDays)
        {
        errorMessage = "Zi gresita";
        alert(errorMessage);
        return false;
        }

    // Se arrivo qui allora la data è valida
    strData = ""
    if (giorno<10)
      {strData = "0" + giorno + "/"}
    else
      {strData = giorno + "/"}

    if (mese<10)
      {strData = strData + "0" + mese + "/"}
    else
      {strData = strData + mese + "/"}

    //alert(strData + anno);
    return strData + anno;
    }else{niente = "";return niente;}
    
    }

    /*
    ************************************* CHECK DATE *******************************
    FUNZIONE CHE CONTROLLA CHE IN UN CAMPO DATA I DATI SIANO IMMESSI CORRETTAMENTE
    DA ASSOCIARE ALL'EVENTO ONBLUR

    */
    function checkDate(obj){
      if (obj.value)
      {
        dataValida = validaData(obj.value);
        if (!dataValida)  // se la conversione non ha successo allora la data non è valida
        {   // in caso di validazione fallita il campo cambia colore e dà avviso

          obj.style.backgroundColor = 'pink';
          obj.focus();

          obj.status = 'wrong';
          //alert('Formato della data non valido ! ');

        }
        else  // colore normale se la data è valida
        {
          //alert(inputDate.toString());

          obj.value = dataValida;
          obj.style.backgroundColor = '';

        }
      }
      else obj.style.backgroundColor = '';
    }
    /*
    *************************************************** FUNZIONI DI CONVERSIONE EURO/LIRE************************************
    */
    function setLireToEuro (objLire, objEuro)
    {

         if (objEuro.value == null || objEuro.value == 0)
         {
           valore=objLire.value/1936.27;

             objEuro.value=(Math.round(valore*100))/100
          }
      return true;
    }

    function setEuroToLire (objEuro, objLire){
        objLire.value=Math.round(objEuro.value*1936.27);
        return true;
        }
    function setEuroToLireNoValue (objEuro){
        parseInt(objEuro);
        objLire=Math.round(objEuro*1936.27/1000);
        return objLire;
        }


    /***************************************** Ro-Planet functions *******************************************************/

    function lookDecimalPositive(obj) {
        var valore = 0;
        numero = true;
        valore = event.keyCode;

        // 44 = ,
        // 45 = -
        if (valore > 47 && valore < 58 || valore == 44 ){
            if (valore == 44) {
                var i, stringa;
                stringa = obj.value;
                for (i=0; i<stringa.length; i++){
                    if (stringa.charAt(i) == ","){
                        event.returnValue=false;
                        break;
                    }
                }
            } else {
                event.returnValue=true;
            }
        }
        else {
            event.returnValue=false;
        }
    }

    // general purpose function to see if a suspected numeric input is decimal in format percentual
    function isDecimalPercentual(obj, inputVal) {
        oneDecimal = false;
        oneSign = false;

        inputStr = inputVal.toString();
        while (inputStr.indexOf(".") > -1) {
            inputStr = inputStr.replace(".","");
        }

        // integer part of number accepts maximum 3 digits [000,00]
        var pos = inputStr.indexOf(",");
        if (pos > -1) {
            inputStr = inputStr.replace(",",".");
            if ( pos > 3) {
                alert("Numero non valido: " + inputVal);
                obj.focus();
                return false;
            }
        }

        for (var i = 0; i < inputStr.length; i++) {
            var oneChar = inputStr.charAt(i);
            if (oneChar == "."  && !oneDecimal) {
                oneDecimal = true;
                continue;
            }

            if (oneChar < "0" || oneChar > "9") {
                alert("Numero non valido: " + inputVal);
                obj.focus();
                return false;
            }
        }
        return true;
     }


    //  check if the number is valid for a percent type   ex:  100,00%  or  12,50%  (positive)
    function getBluredPercentual(obj, initial_str) {
      if (initial_str != null && initial_str.length > 0) {
            if (!isDecimal(obj, initial_str)) {
                    return initial_str;
            }
            var saveStr = "";
            str = initial_str.replace(",",".");
            nr = str.split(".");
            saveStr += nr[0];
            if ((saveStr.length > 3) || (saveStr == null) || (saveStr.length == 0)){
                     alert("Numero non valido: " + initial_str);
                     obj.focus();
                     return initial_str;
            } else {
                     if (nr[1] != null && nr[1].length > 0) {
                        if (nr[1].length > 2) {
                            rational = nr[1].substring(0,2);
                        } else {
                            if (nr[1].length == 1) {
                                rational = nr[1] + "0";
                            } else {
                                rational = nr[1];
                            }
                        }
                    } else {
                        rational = "00";
                    }

                var newStr = '';
                len = saveStr.length; // for positive numbers
                if (saveStr != null && saveStr.length > 0) { // for negative numbers
                    if (saveStr.charAt(0) == "-") {
                        len -= 1;
                    }
                }

                var revStr = reverseIt(saveStr);
                for (var i=0; i<len; i++) {
                    if (i>0 && (i%3)==0) newStr += '.';
                    newStr += revStr.charAt(i);
                }
                if (len < saveStr.length) {
                    newStr += "-";
                }
                return reverseIt(newStr) + "," + rational;

            }
      }
      return "";
    }

    /*
      check if the number is valid and insert the formating points for thousands
    */
    function getBluredDecimal(obj, initial_str) {
        if (initial_str != null && initial_str.length > 0) {
               if (!isDecimal(obj, initial_str)) {
                    //alert("1");
                    return initial_str;
                }
                var saveStr = "";
                str = initial_str.replace(",",".");
                nr = str.split(".");
                saveStr += nr[0];
                if (((saveStr.charAt(0) == "-" && saveStr.length > 10) || (saveStr.charAt(0) != "-" && saveStr.length > 9))
                    || (saveStr == null) || (saveStr.length == 0)){
                    alert("Numero non valido: " + initial_str);
                    obj.focus();
                    return initial_str;
                } else {
                    if (nr[1] != null && nr[1].length > 0) {
                        if (nr[1].length > 2) {
                            rational = nr[1].substring(0,2);
                        } else {
                            if (nr[1].length == 1) {
                                rational = nr[1] + "0";
                            } else {
                                rational = nr[1];
                            }
                        }
                    } else {
                        rational = "00";
                    }

                var newStr = '';
                len = saveStr.length; // for positive numbers
                if (saveStr != null && saveStr.length > 0) { // for negative numbers
                    if (saveStr.charAt(0) == "-") {
                        len -= 1;
                    }
                }

                var revStr = reverseIt(saveStr);
                for (var i=0; i<len; i++) {
                    if (i>0 && (i%3)==0) newStr += '.';
                    newStr += revStr.charAt(i);
                }
                if (len < saveStr.length) {
                    newStr += "-";
                }
                return reverseIt(newStr) + "," + rational;
            }
        }
        return "";
    }


  // general purpose function to see if a suspected numeric input is decimal
     function isDecimal(inputVal) {
   
        oneDecimal = false;
        oneSign = false;

        inputStr = inputVal.toString();
        while (inputStr.indexOf(".") > -1) {
            inputStr = inputStr.replace(".","");
        }

        // integer part of number accepts maximum 9 digits [0..8]
        var pos = inputStr.indexOf(",");
        if (pos > -1) {
            inputStr = inputStr.replace(",",".");
            if ((inputStr.charAt(0) == "-" && pos > 10) || (inputStr.charAt(0) != "-" && pos > 9)) {
                alert("Numarul nu este valid: " + inputVal );
                return false;
            }
        }

        for (var i = 0; i < inputStr.length; i++) {
            var oneChar = inputStr.charAt(i);
            if (oneChar == "."  && !oneDecimal) {
                oneDecimal = true;
                continue;
            }
            if (oneChar == "-"  && !oneSign  && i==0) {
                oneSign = true;
                continue;
            }

            if (oneChar < "0" || oneChar > "9") {
                alert("Numarul nu este valid: " + inputVal);
                return false;
            }
        }
        return true;
     }


     // general purpose function to see if a suspected numeric input is decimal
     function isDecimal(obj, inputVal) {
   
        oneDecimal = false;
        oneSign = false;

        inputStr = inputVal.toString();
        while (inputStr.indexOf(".") > -1) {
            inputStr = inputStr.replace(".","");
        }

        // integer part of number accepts maximum 9 digits [0..8]
        var pos = inputStr.indexOf(",");
        if (pos > -1) {
            inputStr = inputStr.replace(",",".");
            if ((inputStr.charAt(0) == "-" && pos > 10) || (inputStr.charAt(0) != "-" && pos > 9)) {
                alert("Numarul nu este valid: " + inputVal );
                obj.focus();
                return false;
            }
        }

        for (var i = 0; i < inputStr.length; i++) {
            var oneChar = inputStr.charAt(i);
            if (oneChar == "."  && !oneDecimal) {
                oneDecimal = true;
                continue;
            }
            if (oneChar == "-"  && !oneSign  && i==0) {
                oneSign = true;
                continue;
            }

            if (oneChar < "0" || oneChar > "9") {
                alert("Numarul nu este valid: " + inputVal);
                obj.focus();
                return false;
            }
        }
        return true;
     }
     
     // raluca - validari pt numere de telefon
     
      function isDecimalTel(obj, inputVal) {
   
        oneDecimal = false;
        oneSign = false;

        inputStr = inputVal.toString();
        while (inputStr.indexOf(".") > -1) {
            inputStr = inputStr.replace(".","");
        }

        // integer part of number accepts maximum 9 digits [0..8]
        var pos = inputStr.indexOf(",");
        if (pos > -1) {
            inputStr = inputStr.replace(",",".");
            if ((inputStr.charAt(0) == "-" && pos > 10) || (inputStr.charAt(0) != "-" && pos > 9)) {
                alert("Numarul de telefon sau fax nu este valid ! ");
                obj.focus();
                return false;
            }
        }

        for (var i = 0; i < inputStr.length; i++) {
            var oneChar = inputStr.charAt(i);
            if (oneChar == "."  && !oneDecimal) {
                oneDecimal = true;
                continue;
            }
            if (oneChar == "-"  && !oneSign  && i==0) {
                oneSign = true;
                continue;
            }

            if (oneChar < "0" || oneChar > "9") {
                alert("Numarul de telefon sau fax nu este valid ! ");
                obj.focus();
                return false;
            }
        }
        return true;
     }
     
     
  
     
       function validTel(obj, inputVal) {
   
        oneDecimal = false;
        oneSign = false;
        
        
        inputStr = inputVal.toString();
    
	
	var lung = inputStr.length;
	var subStr = inputStr.substring(1,lung);
	var subStr1 = inputStr.substring(0,lung);
	
	if (inputStr.charAt(0)== "+" ){
		 for (var i = 0; i < lung-1; i++) {
		    
            var oneChar = subStr.charAt(i);
            if (oneChar==" "){
            	return true;
            }
            if (oneChar < "0" || oneChar > "9") {
                alert("Numarul de telefon sau fax nu este valid  ! ");
                obj.focus();
                return false;
            }
        }
	}
	
	else {
		if (!isDecimalTel(obj,inputStr.charAt(0))){
                 obj.focus();
               return false; 
		}
		 for (var j = 0; j < lung; j++) {
		    var oneChar = subStr1.charAt(j);
		    if (oneChar!=" "){
		        if (oneChar < "0" || oneChar > "9") {
                alert("Numarul de telefon sau fax nu este valid ! ");
                obj.focus();
                return false;
            }
            }
        }
	}
	
        return true;
     }



    /*
      erase the formating points for thousands from the decimal format
    */
    function getFocusedDecimal(str) {
        while(str.indexOf(".")>-1) {
            str=str.replace(".","");
        }
        return str;
    }

    function reverseIt(str) {
        if (!str) return; // nothing to change
        var rstr = '';
        for (i=str.length-1;i>=0;i--) rstr += str.charAt(i);
        return rstr;
    }
    /**************** begin seleziona util functions ***********************************/
    /* If The source string is not empty align it on right, obtaining a string with length <finalLength> and filled with 0 at the begining. */
    function alignRight(finalLength, source) {
        fieldValue = source;
        valueLength = source.length;
        if (valueLength > 0 && valueLength < finalLength) {
            for (var i = 0; i < finalLength - valueLength; i++) {
                fieldValue = '0' + fieldValue;
            }
        }
        return fieldValue;
    }
    /**
    * If numero valido: true
    * Otherwise: false;
    * @return
    */
    function nrBlured(field) {
        var fieldValue = field.value;
        if (field.value == "" || isNumeroIntero(field.value)) {
            fieldSize = field.size;
            valueLength = fieldValue.length;
            if (valueLength == 0 && field.name == "nrAccSenzaAnno") {
                cleanAccertamento();
            }
            if (valueLength == 0 && field.name == "nrImpSenzaAnno") {
                cleanImpegno();
            }
            if (fieldValue == "0" || fieldValue == "00") {
                fieldValue == "";
            }
            fieldValue = alignRight(fieldSize, fieldValue);
            field.value = fieldValue;
            return true;
        }
        return false;
    }
    function isNumeroIntero(inputVal) {
        inputStr = inputVal.toString();
        if (inputStr.length == 0) {
            return false;
        }
        for (var i = 0; i < inputStr.length; i++) {
            var oneChar = inputStr.charAt(i);
            if (oneChar < "0" || oneChar > "9") {
                return false;
            }
        }
        return true;
    }
   /*transformInNumberToBeSummed*/
  function transformInNumberToBeSummed(inputVal) {

    	oneDecimal = false;
	oneSign = false;
	inputStr = inputVal.toString();
    	while (inputStr.indexOf(".") > -1) {
        	inputStr = inputStr.replace(".","");
	}
	// integer part of number accepts maximum 9 digits [0..8]
	var pos = inputStr.indexOf(",");
	if (pos > -1) {
	    inputStr = inputStr.replace(",",".");
	    if ((inputStr.charAt(0) == "-" && pos > 10) || (inputStr.charAt(0) != "-" && pos > 9)) {
    	         alert("Numero non valido: " + inputVal);
         	 return null;
    	    }
	}
    	for (var i = 0; i < inputStr.length; i++) {
        	var oneChar = inputStr.charAt(i);
	        if (oneChar == "."  && !oneDecimal) {
    	        oneDecimal = true;
        	    continue;
        	}
        	if (oneChar == "-"  && !oneSign  && i==0) {
            	oneSign = true;
	        continue;
    	        }
        	if (oneChar < "0" || oneChar > "9") {
                    alert("Numero non valido: " + inputVal);
	            return null;
    	        }
    	}
	return inputStr;
     }


    /*checkIfPositiveNumber*/
    function checkIfPositiveNumber(field) {
        var nr = transformInNumberToBeSummed(field.value);
        if (field.value && field.value.length > 0) {
            if (nr != null && nr < 0 ) {
                alert("Il campo " + field.name + " deve essere un numero positivo!");
                field.focus();
                return field.value;
            }  if (nr != null && nr > 0 ){
                return getBluredDecimal(field, field.value);
            }
            if (nr == null)
            {
            	field.focus();
            }            
        }
        return field.value;
    }
    
/**************** end seleziona util functions ***********************************/

function closeChildWindows(){
  if(window.parent.aWin){
    for(var i = 0; i < window.parent.aWin.length; i++){
      try{
        if(window.parent.aWin[i] && window.parent.aWin[i].open && !window.parent.aWin[i].closed){
            window.parent.aWin[i].close();
        }
      }catch(e){}
    }
    window.parent.aWin = new Array();
  }
}
function addChildWindow(win){
    if(!window.parent.aWin || !(window.parent.aWin.length)){
        window.parent.aWin = new Array();
    }
    try{
        window.parent.aWin = window.parent.aWin.concat(win);
    } catch(e){
        window.parent.aWin = new Array();
        window.parent.aWin = window.parent.aWin.concat(win);
    }
}
/**************************************************************************************/
//function is for validate fields like MMYYYY(mensile+anno)
function checkDatePeriodo(obj){
     if (obj.value)
      {
        dataValida = checkDatePeriodoRif(obj.value);
        if (!dataValida)  // se la conversione non ha successo allora la data non è valida
        {   // in caso di validazione fallita il campo cambia colore e dà avviso
          obj.style.backgroundColor = 'pink';
          obj.focus();
          obj.status = 'wrong';
        }
        else  // colore normale se la data è valida
        {
          obj.value = dataValida;
          obj.style.backgroundColor = '';
        }
      }
      else obj.style.backgroundColor = '';
    }

function checkDatePeriodoRif(date) {
     if(date.length == 5 || date.length== 6){
         if(date.length == 6){
            mese = parseInt(date.substring(0,2));
            anno = parseInt(date.substring(2,6));
         }
         if(date.length == 5){
            mese = parseInt(date.substring(0,1));
            anno = parseInt(date.substring(1,5));
         }

         if (isNaN(mese) || isNaN(anno)){
            errorMessage = "Formato data non corretto: deve essere mensilita + anno";
            alert(errorMessage);
            return false;
            }
        //Controllo mese
         if(mese.length > 2){
           alert("Mese errato!");
         }
         if (mese <= 0 || mese > 12){
             errorMessage = "Mese errato!";
             alert(errorMessage);
             return false;
         }
         // Controllo anno
         if (anno.length == 3 || anno.length > 4){ alert("Formato data non corretto: deve essere mensilita + anno");
             return false;
         }
         if (anno<100){
          if (anno<50) anno = 2000 + anno;
          else anno = 1900 + anno;
          }
          strData = ""
         // Se arrivo qui allora la data è valida
         if (mese<10){
            strData = strData + "0" + mese
         } else{
            strData = strData + mese
         }
         return strData + anno;
    } else {
         alert("Formato data non corretto: deve essere mensilita + anno");
    }
}


    function getCustomizedDecimal(obj, initial_str , realPart, decimalParts) {
      if (initial_str != null && initial_str.length > 0) {
            if (!isDecimal(obj, initial_str)) {
                    return initial_str;
            }
            var saveStr = "";
            str = initial_str.replace(",",".");
            nr = str.split(".");
            saveStr += nr[0];
            if ((saveStr.length > realPart) || (saveStr == null) || (saveStr.length == 0)){
                     alert("Numero non valido !");
                     obj.focus();
                     return initial_str;
            } else {
                     if (nr[1] != null && nr[1].length > 0) {
                        if (nr[1].length > decimalParts) {
                            rational = nr[1].substring(0,7);
                        } else {
                            if (nr[1].length == 1) {
                                rational = nr[1] + "0";
                            } else {
                                rational = nr[1];
                            }
                        }
                    } else {
                        rational = "00";
                    }

                var newStr = '';
                len = saveStr.length; // for positive numbers
                if (saveStr != null && saveStr.length > 0) { // for negative numbers
                    if (saveStr.charAt(0) == "-") {
                        len -= 1;
                    }
                }

                var revStr = reverseIt(saveStr);
                for (var i=0; i<len; i++) {
                    if (i>0 && (i%3)==0) newStr += '.';
                    newStr += revStr.charAt(i);
                }
                if (len < saveStr.length) {
                    newStr += "-";
                }
                return reverseIt(newStr) + "," + rational;

            }
      }
      return "";
    }

   //function permist 7 decimal after comma and 3 digits
  //  check if the number is valid for a percent type   ex:  100,0000000%  or  12,5000000%  (positive)
    function getPercentualSevenDecimal(obj, initial_str) {
      return getCustomizedDecimal(obj, initial_str , 3, 7);
      /*
      if (initial_str != null && initial_str.length > 0) {
            if (!isDecimal(obj, initial_str)) {
                    return initial_str;
            }
            var saveStr = "";
            str = initial_str.replace(",",".");
            nr = str.split(".");
            saveStr += nr[0];
            if ((saveStr.length > 3) || (saveStr == null) || (saveStr.length == 0)){
                     alert("Numero non valido ! Es: xxx,xxxxxxx");
                     obj.focus();
                     return initial_str;
            } else {
                     if (nr[1] != null && nr[1].length > 0) {
                        if (nr[1].length > 7) {
                            rational = nr[1].substring(0,7);
                        } else {
                            if (nr[1].length == 1) {
                                rational = nr[1] + "0";
                            } else {
                                rational = nr[1];
                            }
                        }
                    } else {
                        rational = "00";
                    }

                var newStr = '';
                len = saveStr.length; // for positive numbers
                if (saveStr != null && saveStr.length > 0) { // for negative numbers
                    if (saveStr.charAt(0) == "-") {
                        len -= 1;
                    }
                }

                var revStr = reverseIt(saveStr);
                for (var i=0; i<len; i++) {
                    if (i>0 && (i%3)==0) newStr += '.';
                    newStr += revStr.charAt(i);
                }
                if (len < saveStr.length) {
                    newStr += "-";
                }
                return reverseIt(newStr) + "," + rational;

            }
      }
      return "";
      */
    }



/********************************************************************************************/

/*
********************FUNCTIONS FOR FIELD CONVERSION FROM STRING TO NUMBER************************************
metodo aggiunto a Cluj, da Marian Aurelian (aurelian.marian@ro-planet.ro)
*/

function StrToNum(obj)
{
   inputStr = obj.toString();
   while (inputStr.indexOf(".") > -1)
   {
       inputStr = inputStr.replace(".","");
   }

   var pos = inputStr.indexOf(",");
   if (pos > -1)
   {
       inputStr = inputStr.replace(",",".");
   }

   return inputStr;
}

//METTERE VIRGOLA al posto del punto
function virgola(importo){
 indice = importo.indexOf('.');
 if(indice > 0){
   importo = importo.substring(0,indice) + ',' + importo.substring(indice+1,importo.length);
 }
 return importo;
}

//METTERE IL PUNTO al POSTO della VIRGOLA
function punto(importo){
 indice = importo.indexOf(',');
 if(indice > 0){
   importo = importo.substring(0,indice) + '.' + importo.substring(indice+1,importo.length);
 }
 return importo;
}

function checkLength(textField, maxLength) {
	if (textField.value.length > maxLength) {
        alert("La lunghezza massima per questo campo è " + maxLength + " caratteri! ");
		textField.focus();
	}
}

// raluca p, 02.03.2006
    function fromDoubleToString(str) {
	 if(str.indexOf(".")>0)
      return str.substring(0,str.indexOf("."));
	  else
	  return str;
      } 
      
      function fromDoubleToStringKeepDecimal(str)  {
      if(str.indexOf(".")>0 && ( str.substring(str.indexOf(".")+1, str.length)=="00" || str.substring(str.indexOf(".")+1, str.length)=="0" ))
       return str.substring(0,str.indexOf("."));
      else
       return str;
    } 

// transform yyyy-mm-dd to dd-mm-yyyy
function transformDate(initialDate) {
 year = initialDate.substring(0,4);
 month = initialDate.substring(5,7);
 day = initialDate.substring(8,10);
 result = day+"-"+month+"-"+year;
}

// returns an array of selected options from a list with multiple selection 	
 function returnSelectedOptions(list) {	
  selected = new Array();
  for (var i = 0; i < list.options.length; i++)
    if (list.options[ i ].selected)
      selected.push(list.options[ i ].value);
  return selected;  
}
 function getDataCorrente()
  {
   var today = new Date();
   var month = today.getMonth() + 1;
   var day = today.getDate();
   var year = today.getFullYear();
   var s = "-";
   var dataCorrente=year+s+month+s+day;
   return dataCorrente;
  }

  function getAnnoCorrente()
  {
   var today = new Date();
   var year = today.getFullYear();
   return year;
  }

//return decimal with correct format for a double
    function getDecimalInCorrectFormat(str) {
        while(str.indexOf(".")>-1) {
            str=str.replace(".","");
        }
        if (str.indexOf(",")>-1) {
        	str=str.replace(",",".");
        }
        
        return str;
    }

    
    // check email address
    
    <!-- V1.1.3: Sandeep V. Tamhankar (stamhankar@hotmail.com) -->
<!-- Original:  Sandeep V. Tamhankar (stamhankar@hotmail.com) -->
<!-- Changes:
/* 1.1.4: Fixed a bug where upper ASCII characters (i.e. accented letters
international characters) were allowed.
 
1.1.3: Added the restriction to only accept addresses ending in two
letters (interpreted to be a country code) or one of the known
TLDs (com, net, org, edu, int, mil, gov, arpa), including the
new ones (biz, aero, name, coop, info, pro, museum).  One can
easily update the list (if ICANN adds even more TLDs in the
future) by updating the knownDomsPat variable near the
top of the function.  Also, I added a variable at the top
of the function that determines whether or not TLDs should be
checked at all.  This is good if you are using this function
internally (i.e. intranet site) where hostnames don't have to 
conform to W3C standards and thus internal organization e-mail
addresses don't have to either.
Changed some of the logic so that the function will work properly
with Netscape 6.
 
1.1.2: Fixed a bug where trailing . in e-mail address was passing
(the bug is actually in the weak regexp engine of the browser; I
simplified the regexps to make it work).
 
1.1.1: Removed restriction that countries must be preceded by a domain,
so abc@host.uk is now legal.  However, there's still the 
restriction that an address must end in a two or three letter
word.
 
1.1: Rewrote most of the function to conform more closely to RFC 822.
 
1.0: Original  */
// -->
 
<!-- Begin

	function emailCheck (emailStr) {
 
		/* The following variable tells the rest of the function whether or not
		to verify that the address ends in a two-letter country or well-known
		TLD.  1 means check it, 0 means don't. */
		 
		var checkTLD=1;
		 
		/* The following is the list of known TLDs that an e-mail address must end with. */
		 
		var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
		 
		/* The following pattern is used to check if the entered e-mail address
		fits the user@domain format.  It also is used to separate the username
		from the domain. */
		 
		var emailPat=/^(.+)@(.+)$/;
		 
		/* The following string represents the pattern for matching all special
		characters.  We don't want to allow special characters in the address. 
		These characters include ( ) < > @ , ; : \ " . [ ] */
		 
		var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
		 
		/* The following string represents the range of characters allowed in a 
		username or domainname.  It really states which chars aren't allowed.*/
		 
		var validChars="\[^\\s" + specialChars + "\]";
		 
		/* The following pattern applies if the "user" is a quoted string (in
		which case, there are no rules about which characters are allowed
		and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
		is a legal e-mail address. */
		 
		var quotedUser="(\"[^\"]*\")";
		 
		/* The following pattern applies for domains that are IP addresses,
		rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
		e-mail address. NOTE: The square brackets are required. */
		 
		var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
		 
		/* The following string represents an atom (basically a series of non-special characters.) */
		 
		var atom=validChars + '+';
		 
		/* The following string represents one word in the typical username.
		For example, in john.doe@somewhere.com, john and doe are words.
		Basically, a word is either an atom or quoted string. */
		 
		var word="(" + atom + "|" + quotedUser + ")";
		 
		// The following pattern describes the structure of the user
		 
		var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
		 
		/* The following pattern describes the structure of a normal symbolic
		domain, as opposed to ipDomainPat, shown above. */
		 
		var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
		 
		/* Finally, let's start trying to figure out if the supplied address is valid. */
		 
		/* Begin with the coarse pattern to simply break up user@domain into
		different pieces that are easy to analyze. */
		 
		var matchArray=emailStr.match(emailPat);
		 
		if (matchArray==null) {
		 
		/* Too many/few @'s or something; basically, this address doesn't
		even fit the general mould of a valid e-mail address. */
		 
		alert("Adresa e-mail incorecta ");
		return false;
		}
		var user=matchArray[1];
		var domain=matchArray[2];
		 
		// Start by checking that only basic ASCII characters are in the strings (0-127).
		 
		for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
		alert("Numele utilizator al adresei de e-mail contine caractere invalide.");
		return false;
		   }
		}
		for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
		alert("Domeniul adresei de e-mail contine caractere invalide.");
		return false;
		   }
		}
		 
		// See if "user" is valid 
		 
		if (user.match(userPat)==null) {
		 
		// user is not valid
		 
		alert("Numele utilizator al adresei de e-mail nu este valid.");
		return false;
		}
		 
		/* if the e-mail address is at an IP address (as opposed to a symbolic
		host name) make sure the IP address is valid. */
		 
		var IPArray=domain.match(ipDomainPat);
		if (IPArray!=null) {
		 
		// this is an IP address
		 
		for (var i=1;i<=4;i++) {
		if (IPArray[i]>255) {
		alert("Adresa IP a adresei de e-mail este invalida!");
		return false;
		   }
		}
		return true;
		}
		 
		// Domain is symbolic name.  Check if it's valid.
		 
		var atomPat=new RegExp("^" + atom + "$");
		var domArr=domain.split(".");
		var len=domArr.length;
		for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
		alert("Numele domeniului adresei de e-mail nu este valid.");
		return false;
		   }
		}
		 
		/* domain name seems valid, but now make sure that it ends in a
		known top-level domain (like com, edu, gov) or a two-letter word,
		representing country (uk, nl), and that there's a hostname preceding 
		the domain or country. */
		 
		if (checkTLD && domArr[domArr.length-1].length!=2 && 
		domArr[domArr.length-1].search(knownDomsPat)==-1) {
		alert("Adresa de e-mail trebuie sa se termine cu un domeniu cunoscut sau in doua litere.");
		return false;
		}
		 
		// Make sure there's a host name preceding the domain.
		 
		if (len<2) {
		alert("Din aceasta adresa de e-mail lipseste hostname -ul!");
		return false;
		}
		 
		// If we've gotten this far, everything's valid!
		return true;
	}
    
    function isValidEmail(strEmail){
    	var filter=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    	return filter.test(strEmail);
    }
    
    