/***************** Variabili globali esterne *****************/
/*
 *   - PREFIX_DD
 *   - PREFIX_MM
 *   - PREFIX_YY
 *   - PREFIX_HH
 *   - PREFIX_MI
 *   - PREFIX_SS
 */

/***************** Variabili e metodi globali *****************/
var theForm;
/*
 * Init variabile su form edit.
 * Per default 'editform', se diverso aggiornare theForm nel JSP tramite la funzione
 * iniFnz().
 * Ex.:
 *    function iniFnz(){
 *      theForm = document.filtroform;
 *    }
 */
function initDates(){
  theForm = document.getElementById('editform');
}
/*
 * Verifica campi input (dates, numbers, ...)
 * Restituisce true se tutti i campi sono corretti.
 */
function verifyInputTypes(){
  if(!verifyDates()){
    return false;
  }
  
    //Verifica i times
  if(!verifyTimes()){
    return false;
  }
  
  return verifyNumbers();
}

/***************** Gestione times *****************/
var times = new Array();
function addTime(name, hh, mi, ss, required){
  times[times.length] = new Array(name, hh, mi, ss, required);
}
function verifyTimes(){
  for(var i=0; i<times.length; i++){
    if(!verifyTimeObject(times[i])){
      return false;
    }
  }
  return true;
}
function containsTime(name){
  for(var i=0; i<times.length; i++){
    if(times[i][0] == name)
      return true;
  }
  return false;
}


/*
 * Verifica l'ora.
 */
function verifyTimeObject(times){

  var name     = theForm.all[times[0]];
  var hh       = theForm.all[times[1]];
  var mi       = theForm.all[times[2]];
  var ss       = theForm.all[times[3]];
  var required = times[4];
  // Verifica se data obbligatoria
  if(required){
    if(hh.value == ''){
      emptyTime(hh);
      return false;
    }
    if(mi.value == ''){
      emptyTime(mi);
      return false;
    }
    if(ss != null && ss.value == ''){
      emptyTime(ss);
      return false;
    }
  }
  var ssVal;
  if(ss !=null){
    ssVal = ss.value;
  }
  else{
    ssVal = null;
  }  
  if(!verifyTime(hh.value, mi.value, ssVal)){
    timeError(hh);
    return false;
  }
  else{
    return true;
  }
}
// Verifica l'ora
function verifyTime(hh, mi, ss){

  var ssTmp = 0;
  if(ss !=null){
    ssTmp = ss;
  }
  /*
   * Verifica la numericità del campi time
   */
  if(isNaN(hh) || isNaN(mi) || isNaN(ssTmp) ){
    return false;
  }
  /*
   * Verifica che l'ora sia compresa tra 0 e 24
   */
   if(parseInt(hh) < 0 || parseInt(hh) > 24){
     return false;
   }
  /*
   * Verifica che i minuti siano compresi tra 0 e 59
   */
   if(parseInt(mi) < 0 || parseInt(mi) > 59){
     return false;
   }
  /*
   * Verifica che i secondi  siano compresi tra 0 e 59
   */
   if(parseInt(ss) < 0 || parseInt(ss) > 59){
     return false;
   }
  

  return true;
}
function emptyTime(fld){
  alert("Ora obbligatoria");
  fld.focus();
}
function timeError(fld){
  alert("Ora incorretta");
  fld.focus();
}

/***************** FINE Gestione times *****************/

/***************** Gestione dates *****************/
var dates = new Array();
function addDate(name, dd, mm, yy, required){
  dates[dates.length] = new Array(name, dd, mm, yy, required);
}
function verifyDates(){
  for(var i=0; i<dates.length; i++){
    if(!verifyDateObject(dates[i])){
      return false;
    }
  }
  return true;
}
function containsDate(name){
  for(var i=0; i<dates.length; i++){
    if(dates[i][0] == name)
      return true;
  }
  return false;
}
/*
 * Verifica data.
 * Nota: se l'anno è inferiore a 100 si considera come secolo XX
 * (per bug 2YK in JavaScript non possiamo usare date
 */
function verifyDateObject(date){
  var name     = theForm.all[date[0]];
  var dd       = theForm.all[date[1]];
  var mm       = theForm.all[date[2]];
  var yy       = theForm.all[date[3]];
  var required = date[4];
  // Verifica se data obbligatoria
  if(required){
    if(dd.value == ''){
      emptyDate(dd);
      return false;
    }
    if(mm.value == ''){
      emptyDate(mm);
      return false;
    }
    if(yy.value == ''){
      emptyDate(yy);
      return false;
    }
  }
  if(!verifyDate(dd.value, mm.value, yy.value)){
    dateError(dd);
    return false;
  }
  else{
    return true;
  }
}
// Verifica data
function verifyDate(dd, mm, yy){
  if((dd != '')
  || (mm != '')
  || (yy != '')){
    if(yy == ''){
      return false;
    }
    yy = y2k(yy - 0);
    var newDate = new Date(yy, mm - 1, dd);
    /*
     * Non verifichiamo l'anno per evitare problemi con 2 e 4 cifre
     * (la verifica giorno e mese e già esaustiva)
     */
    if((mm != newDate.getMonth() + 1)
    || (dd != newDate.getDate())){
      return false;
    }
  }
  return true;
}
function emptyDate(fld){
  alert("Data obbligatoria");
  fld.focus();
}
function dateError(fld){
  alert("Data incorretta");
  fld.focus();
}

/*
 * Gestione y2k: se anno di 1 ò 2 digiti e <= 40 -> aggiungere 20 davanti
 */
function y2k(number){
  //return (number < 100) ? number + 1900 : number; 
  return (number <= 40) ? number + 2000 : number; 
}

/**************** Gestione salto dates ***************/

var boxwithlastfocus = 0;
var boxwithcurrentfocus = 0;
function manageFocus(field, name, type, isTime, prefixYy, prefixMm, prefixDd){
  if(arguments.length == 4){
    prefixYy = PREFIX_YY;
    prefixDd = PREFIX_DD;
    prefixMm = PREFIX_MM;
  }
  var fieldDd  = theForm.all[name+prefixDd];
  var fieldMm  = theForm.all[name+prefixMm];
  var fieldYy  = theForm.all[name+prefixYy];
  var hiddenDd = theForm.all[name+prefixDd+'hidden'];
  var hiddenMm = theForm.all[name+prefixMm+'hidden'];
  if (type != boxwithcurrentfocus ){
    if (!(((type==1) && (fieldDd.value=="")) 
    ||    ((type==2) && (fieldMm.value=="")))){
      boxwithlastfocus = boxwithcurrentfocus;
    }
    else{
      boxwithlastfocus = 0;
    }
    boxwithcurrentfocus = type;
  }

  hiddenDd.value = fieldDd.value;
  hiddenMm.value = fieldMm.value;
  
  if ((type==1) && (boxwithlastfocus!=1)){
    fieldDd.select();
  }
  else if ((type==2) && (boxwithlastfocus!=2)){
    fieldMm.select();
  }
  else if ((type==3) && (boxwithlastfocus!=3)){
    fieldYy.select();
  }
  else if(isTime){
    var fieldHh  = theForm.all[name+PREFIX_HH];
    var fieldMi  = theForm.all[name+PREFIX_MI];
    var fieldSs  = theForm.all[name+PREFIX_SS];
    if ((fieldHh) && (type==4) && (boxwithlastfocus!=4)){
        fieldHh.select();
    }
    else if ((fieldMi) && (type==5) && (boxwithlastfocus!=5)){
        fieldMi.select();
    }
    else if ((fieldSs) && (type==6) && (boxwithlastfocus!=6)){
        fieldSs.select();
    }
  }

  if (type == boxwithcurrentfocus ){
    boxwithlastfocus = boxwithcurrentfocus;
  }
  
  paddingDates(field, name, type, isTime, prefixYy, prefixMm, prefixDd);
}

function paddingDates(field, name, type, isTime, prefixYy, prefixMm, prefixDd){
  if(arguments.length == 4){
    prefixYy = PREFIX_YY;
    prefixDd = PREFIX_DD;
    prefixMm = PREFIX_MM;
  }
  var fieldDd  = theForm.all[name+prefixDd];
  var fieldMm  = theForm.all[name+prefixMm];
  var fieldYy  = theForm.all[name+prefixYy];
  var hiddenDd = theForm.all[name+prefixDd+'hidden'];
  var hiddenMm = theForm.all[name+prefixMm+'hidden'];

  if (fieldDd.value.length == 1){
    fieldDd.value = "0" + fieldDd.value;
  }
  if (fieldMm.value.length == 1){
    fieldMm.value = "0" + fieldMm.value;
  }
  manageY2k(fieldYy);

  if(isTime){
    /*
     * Timestamp
     */
    var fieldHh  = theForm.all[name+PREFIX_HH];
    var fieldMi  = theForm.all[name+PREFIX_MI];
    var fieldSs  = theForm.all[name+PREFIX_SS];
    if ((fieldHh) && (fieldHh.value.length == 1)){
      fieldHh.value = "0" + fieldHh.value;
    }
    if ((fieldMi) && (fieldMi.value.length == 1)){
      fieldMi.value = "0" + fieldMi.value;
    }
    if ((fieldSs) && (fieldSs.value.length == 1)){
      fieldSs.value = "0" + fieldSs.value;
    }
  }
}

function manageY2k(fieldYy){
  if (!(isNaN(fieldYy.value))){ 
    if ((fieldYy.value.length == 1) || (fieldYy.value.length == 2)){
      fieldYy.value = y2k(fieldYy.value - 0);
    }
  }
}

/*
 * Gestione 'invio' altrimenti la gestione padding '0' e y2k la fa dopo il submit
 * (e dunque arrivano i dati incorretti).
 */
function keyDown(field, name, type, time, prefixYy, prefixMm, prefixDd){
  if(event.keyCode == 13){
    if(arguments.length == 4){
      manageFocus(field, name, type, time);
    }
    else{
      manageFocus(field, name, type, time, prefixYy, prefixMm, prefixDd);
    }
  }
}

function skipTextBox(field, name, type, isTime, prefixYy, prefixMm, prefixDd){
  /*
   * Salta campo successivo solo se keyCode numerico (0 e 9 tastiera + bloc num)
   */
  if((event.keyCode < 48 || event.keyCode > 57)
  && (event.keyCode < 96 || event.keyCode > 105)){
    return;
  }

  if(arguments.length == 4){
    prefixYy = PREFIX_YY;
    prefixDd = PREFIX_DD;
    prefixMm = PREFIX_MM;
  }

  var fieldDd  = theForm.all[name+prefixDd];
  var fieldMm  = theForm.all[name+prefixMm];
  var fieldYy  = theForm.all[name+prefixYy];
  var hiddenDd = theForm.all[name+prefixDd+'hidden'];
  var hiddenMm = theForm.all[name+prefixMm+'hidden'];

  if (type==1){
    /*
     * Giorno
     */
    var day = fieldDd.value;
    if ((boxwithlastfocus != 2 ) || (day == "")){   
      if ((hiddenDd.value != day)){
        if (!(isNaN(day))){ 
          if (parseInt(day) > 3 || (day.length == 2)){
            if (day.length == 1){
              fieldDd.value = "0" + day
            }
            fieldMm.focus();
            fieldMm.select();         
          }
          hiddenDd.value = fieldDd.value;
          boxwithcurrentfocus = 2;
          boxwithlastfocus = 1; 
        }
      }
    }
    else if (boxwithlastfocus == 2 ){
      if (parseInt(day) > 3 || (day.length == 2)){
        if (day.length == 1){
          fieldDd.value = "0" + day
        }
        fieldMm.focus();
        fieldMm.select();         
      }
    }
    else{
      boxwithlastfocus = 1;
    }
  }
  else if (type==2){
    /*
     * Mese
     */
    var month = fieldMm.value;
    if ((boxwithlastfocus != 3 ) || (month == "")){
      if ((month != hiddenMm.value)){
        if (!(isNaN(month))){
          if (parseInt(month) > 1 || (month.length == 2)){
            if (month.length == 1){
              fieldMm.value = "0" + month
            }
            fieldYy.focus();
            fieldYy.select();
          }
          hiddenMm.value = fieldMm.value;    
          boxwithcurrentfocus = 3;
          boxwithlastfocus = 2; 
        }
      }
    }
    else if (boxwithlastfocus == 3 ){
      if (parseInt(month) > 1 || (month.length == 2)){
        if (month.length == 1){
          fieldMm.value = "0" + month
        }
        fieldYy.focus();
        fieldYy.select();
      }
    }
    else{
      boxwithlastfocus = 2;
    } 
  }
  else if (isTime){
    /*
     * Timestamp
     */
    var fieldHh  = theForm.all[name+PREFIX_HH];
    var fieldMi  = theForm.all[name+PREFIX_MI];
    var fieldSs  = theForm.all[name+PREFIX_SS];
    if(type == 3){
      /*
       * Anno
       */
      if ((boxwithlastfocus != 4 ) || (year == "")){
        var year = fieldYy.value;
        if (!(isNaN(year))){
          if (year.length == 4){
            if(fieldHh){
              fieldHh.focus();
              fieldHh.select();
            }
          }
          boxwithcurrentfocus = 4;
          boxwithlastfocus = 3;
        }
      }
    }
    else if((fieldHh) && (type == 4)){
      /*
       * Ora
       */
      var hour = fieldHh.value;
      if ((boxwithlastfocus != 5 ) || (hour == "")){
          if (!(isNaN(hour))){ 
            if (parseInt(hour) > 2 || (hour.length == 2)){
              if (hour.length == 1){
                fieldHh.value = "0" + hour
              }
              if(fieldMi){
                fieldMi.focus();
                fieldMi.select();
              }
            }
            //hiddenDd.value = fieldDd.value;
            boxwithcurrentfocus = 5;
            boxwithlastfocus = 4;
          }
      }
    }
    else if((fieldMi) && (type == 5)){
      /*
       * Minuti
       */
      var min = fieldMi.value;
          if (!(isNaN(min))){ 
            if (parseInt(min) > 6 || (min.length == 2)){
              if (min.length == 1){
                fieldMi.value = "0" + min
              }
              if(fieldSs){
                fieldSs.focus();
                fieldSs.select();         
              }
            }
            //hiddenDd.value = fieldDd.value;
            boxwithcurrentfocus = 6;
            boxwithlastfocus = 5;
          }
    }
    else if((fieldSs) && (type == 6)){
      /*
       * Secondi
       */
      var sec = fieldSs.value;
          if (!(isNaN(sec))){ 
            if (parseInt(sec) > 6 || (sec.length == 2)){
              if (sec.length == 1){
                fieldSs.value = "0" + sec
              }
            }
          }
    }
  }
}

/***************** Gestione numbers *****************/
var numbers = new Array();
function addNumber(number, required){
  numbers[numbers.length] = new Array(number, required);
}
function verifyNumbers(){
  for(var i=0; i<numbers.length; i++){
    if(!verifyNumberObject(numbers[i])){
      return false;
    }
  }
  return true;
}

function verifyNumberObject(number){
  //TODO: padding 0's
  var numberObj = theForm.all[number[0]];
  var newNumber = normalizeNumber(numberObj.value);
  var required  = number[1];
  if(required){
    if(newNumber == ''){
      emptyNumber(numberObj);
      return false;
    }
  }
  if(!verifyNumber(newNumber)){
    numberError(numberObj);
    return false;
  }
  else{
    numberObj.value = newNumber;
    return true;
  }
}
function verifyNumber(number){
  //TODO: padding 0's (01 è considerato incorretto!)
  //TODO: togliere separatore migliaia
  if(number == "") return true;
  return (("" + parseInt(number)) == number);
}

function normalizeNumber(number) {
  return number.replace(/\./g, "");
}

function formatNumber(number){
/*
  var arr=new Array('0'), i=0; 
  while (number>0) {
    arr[i]=''+number%1000; 
    number=Math.floor(number/1000); 
    i++;
  }
  arr=arr.reverse();
  for (var i in arr) if (i>0) //padding zeros
    while (arr[i].length<3) arr[i]='0'+arr[i];

  if(arr.length < 2){
    return arr.join();
  }
  
  var res = "";
  for (var i = 0; i < arr.length - 1; i++){
    res += arr[i] + ".";
  }
  res += arr[arr.length-1];
  return res;
*/
}

function emptyNumber(fld){
  alert("Numero obbligatorio");
  fld.focus();
}
function numberError(fld){
  alert("Numero incorretto");
  fld.focus();
}

/***************** Gestione BOTTONI *****************/
function scrollBackGroundDown(obj)	{
	obj.style.backgroundPosition='0% 100%';
 	obj.style.cursor="pointer";
 	
  	
}
function scrollBackGroundUp(obj)	{
 	obj.style.backgroundPosition="0% 0%";
 	obj.style.cursor="default";
}

