// Validazioni
function checkSubmit(mode, theForm, operation){
  if(mode == 'Utente')
    return validaUT(theForm, operation);
  else if(mode == 'Password')  
    return validaPWD(theForm, operation);
  else if (mode == 'Notizia')  
    return validaN(theForm, operation);
  else if (mode == 'Banner')  
    return validaB(theForm, operation);
  else if (mode == 'Succursale')  
    return validaS(theForm, operation);
  else if (mode == 'File')  
    return validaF(theForm, operation);
  else if (mode == 'TipiMutuo')  
    return validaTM(theForm, operation);
  else if (mode == 'TipoTasso')  
    return validaTT(theForm, operation);
  else if (mode == 'Mutuo')  
    return validaMU(theForm, operation);
  else if (mode == 'Condizioni')  
    return validaC(theForm, operation);
  else if (mode == 'Contenuto')  
    return validaCO(theForm, operation);
  else if (mode == 'Ammortamento')  
    return validaAmm(theForm, operation);
  else if (mode == 'AbiCab')  
    return validaAbiCab(theForm, operation);
  else if (mode == 'Contatto')  
    return validaCon(theForm, operation);
}

// Validazioni Gestione utenti
function validaUT(theForm, operation) {
  if(operation != 'Elimina') {     // le label devono essere congruenti
    if(operation != 'Modifica'){   // con quelle definite in GLobalDefinition
      if (!Is_Select(theForm.id_tipo)) {
        alert("Specificare la tipologia di utente.");
        theForm.id_tipo.focus();
        return false;
      } else if (theForm.cd_username.value=="") {
        alert("Inserire lo username.");
        theForm.cd_username.focus();
        return false;
      } else if(theForm.ds_password.value=="") {
        alert("Inserire la password!");
        theForm.ds_password.focus();
        return false;
      } else if(theForm.ds_conferma.value=="") {
        alert("Confermare la password!");
        theForm.ds_conferma.focus();
        return false;
      } else if(theForm.ds_password.value != theForm.ds_conferma.value) {
        alert("I campi password e conferma password devono coincidere!");
        theForm.ds_conferma.focus();
        return false;
      } 
      doSubmit(theForm, operation);
    } else {
      if((theForm.ds_password.value != "") &&
         (theForm.ds_conferma.value == "")) {
        alert("Confermare la password!");
        theForm.ds_conferma.focus();
        return false;
      } if(theForm.ds_password.value != theForm.ds_conferma.value) {
        alert("I campi password e conferma password devono coincidere!");
        theForm.ds_conferma.focus();
        return false;
      } else if (!Is_Select(theForm.id_tipo)) {
        alert("Specificare il profilo utente.");
        theForm.id_tipo.focus();
        return false;
      }
      doSubmit(theForm, operation);
    }
  } else {
    doSubmit(theForm, operation);
  }
}

// Validazione Change Password
function validaPWD(theForm, operation){
  if(theForm.old_password.value=="") {
    alert("Inserire la vecchia password!");
    theForm.old_password.focus();
    return false;
  } else if(theForm.ds_password.value=="") {
    alert("Inserire la nuova password!");
    theForm.ds_password.focus();
    return false;
  } else if(theForm.ds_conferma.value=="") {
    alert("Confermare la password!");
    theForm.ds_conferma.focus();
    return false;
  } else if(theForm.ds_password.value != theForm.ds_conferma.value) {
    alert("I campi password e conferma password devono coincidere!");
    theForm.ds_conferma.focus();
    return false;
  }
  doSubmit(theForm, operation);
}

// Validazioni Gestione News
function validaN(theForm, operation) {
  if(operation != 'Elimina') {
    if (theForm.dt_inizio.value=="") {
      alert("Inserire la data inizio validità.");
      theForm.dt_inizio.focus();
      return false;
    } else {
      ret = Is_Date(theForm.dt_inizio.value);
      if(ret != ""){
        alert("Il campo data inizio validità "+ret);
        theForm.dt_inizio.focus();
        return false;      
      } else {
        if (theForm.dt_fine.value=="") {
          alert("Inserire la data fine validità.");
          theForm.dt_fine.focus();
          return false;
        } else {
          ret = Is_Date(theForm.dt_fine.value);
          if(ret != "") {
            alert("Il campo data fine validità "+ret);
            theForm.dt_fine.focus();
            return false;
          }
        }  
      }    
      if (theForm.ds_titolo.value=="") {
        alert("Inserire il titolo della notizia!");
        theForm.ds_titolo.focus();
        return false;
      }else if(theForm.ds_abstract.value==""){
        alert("Inserire l\'abstract della notizia!"); 
        theForm.ds_abstract.focus();     
        return false;
      }else{
        doSubmit(theForm, operation);
        return true;
      } 
    }
  } 
  doSubmit(theForm, operation);
}

// Validazioni Gestione Banner
function validaB(theForm, operation) {
  if(operation != 'Elimina') {
    if((theForm.ds_immagine.value=="")&&(theForm.prev_img.value=="")) {
      alert("Selezionare un\'immagine per il banner!.");
      theForm.ds_immagine.focus();
      return false;
    } else if(theForm.ds_url.value=="") {
      alert("Inserire il collegamento!");
      theForm.ds_url.focus();
      return false;
    } else if(theForm.ds_info.value=="") {
      alert("Inserire la descrizione!");
      theForm.ds_info.focus();
      return false;  
    } else if(theForm.num_priorita.value!=""){
      if(!checkNumber(theForm.num_priorita)){
        alert("Il campo priorità deve essere numerico!");
        theForm.num_priorita.focus();
        return false;
      }
    }
  } 
  doSubmit(theForm, operation);
}

// Validazioni Gestione Succursali
function validaS(theForm, operation) {
  if(operation != 'Elimina') {
    if(theForm.id_comune.value=="") {
      alert("Selezionare il comune!");
      return false;
    } else if(theForm.ds_succursale.value=="") {
      alert("Inserire la succursale!");
      theForm.ds_succursale.focus();
      return false;
    } else if(theForm.ds_titolare.value=="") {
      alert("Inserire il titolare!");
      theForm.ds_titolare.focus();
      return false;  
    } else if(theForm.num_tel.value=="") {
      alert("Inserire il recapito telefonico!");
      theForm.num_tel.focus();
      return false;  
    } else if(theForm.ds_cab.value=="") {
      alert("Inserire il CAB!");
      theForm.ds_cab.focus();
      return false;  
    } else if(theForm.ds_email.value!="") {
      if(!Is_Email(theForm.ds_email.value)){
        alert("Inserire un indirizzo di mail corretto!");
        theForm.ds_email.focus();
        return false;
      }
    }  
    if(theForm.ds_coordinate.value!=""){
      if(!Is_Coord(theForm.ds_coordinate.value)){
        alert("Inserire le coordinate in formato nnn,nnn,nnn,nnn!");
        theForm.ds_coordinate.focus();
        return false;
      }
    }
  } 
  doSubmit(theForm, operation);
}

// Validazioni Gestione File
function validaF(theForm, operation) {
  if((operation == 'Inserisci')||(operation == 'Modifica')) {
    if(theForm.ds_file.value=="") {
      alert("Selezionare un file da caricare!.");
      theForm.ds_file.focus();
      return false;
    }
  } else if(operation == 'Pubblica') {
    if(theForm.prev_file.value=="") {
      alert("Selezionare un file da caricare!.");
      theForm.ds_file.focus();
      return false;
    }
    
    if(theForm.id_folder.value != ""){
	  // Controllo se il folder scelto è pubblicato
	  var ids, ret = true;
	  ids = theForm.id_folder.value.split('#'); // # deve essere lo stesso che trovo nel file di property
	  if((ids.length < 2)||(ids[1] == "")){
	    ret = false;
	  } 
	  if(!ret){
	    alert("Pubblicazione non consentita!\nAccertarsi di aver pubblicato la cartella selezionata.");
	    return ret;
	  }
	}
  }
  if(operation == 'Inserisci') {
    theForm.ds_folder.value=theForm.id_folder.options[theForm.id_folder.selectedIndex].text;
  }
  doSubmit(theForm, operation);
}

// Validazioni Tipi Mutuo
function validaTM(theForm, operation) {
  if(operation != 'Elimina') {
    if(theForm.ds_tipo.value=="") {
      alert("Inserire il tipo mutuo!");
      theForm.ds_tipo.focus();
      return false;
    }
  } 
  doSubmit(theForm, operation);
}

// Validazioni Tipi Tasso
function validaTT(theForm, operation) {
  if(operation != 'Elimina') {
    if(theForm.ds_tipo.value=="") {
      alert("Inserire il tipo tasso!");
      theForm.ds_tipo.focus();
      return false;
    }
  } 
  doSubmit(theForm, operation);
}

// Validazioni Mutui
function validaMU(theForm, operation) {
  if(operation != 'Elimina') {
    if(theForm.ds_mutuo.value=="") {
      alert("Inserire il mutuo!");
      theForm.ds_mutuo.focus();
      return false;
    }
    if(operation == 'Pubblica') {
      // Controllo se le tipologie selezionate sono pubblicate
      var ids, ret = true;
      for (i=0; i<theForm.id_tipo.options.length; i++) {
        ids = theForm.id_tipo.options[i].value.split('#'); // # deve essere lo stesso che trovo nel file di property
        if((ids.length < 2)||(ids[1] == "")){
          ret = false;
          break;
        } 
      }
      if(!ret){
        alert("Pubblicazione non consentita!\nAccertarsi di aver pubblicato i Tipi Mutuo/Tasso.");
        return ret;
      }
      // Devo controllare i tassi utilizzati
      if(theForm.id_dettaglio) {
        ret = true;
        var i = 0;
        var obj;
        tot = theForm.id_dettaglio.length;
        while(i < tot) {
          obj = document.getElementById("id_tipo_tasso_"+theForm.id_dettaglio[i].value);
          if(obj){
            ids = obj.value.split('#');  
            if((ids.length < 2)||(ids[1] == "")){
              ret = false;
              break;
            }
          }
          obj = document.getElementById("id_tipo_tasso2_"+theForm.id_dettaglio[i].value);
          if((obj)&&(obj.value != "")){
            ids = obj.value.split('#');  
            if((ids.length < 2)||(ids[1] == "")){
              ret = false;
              break;
            }
          }
          i++;
        }
      }
      if(!ret){
        alert("Pubblicazione non consentita!\nAccertarsi di aver pubblicato i Tipi Mutuo/Tasso.");
        return ret;
      }
    }
  } 
  seleziona(theForm.id_tipo);
  doSubmit(theForm, operation);
}

// Validazioni Condizioni Generali
function validaC(theForm, operation) { 
  doSubmit(theForm, operation);
}

// Validazioni Contenuti
function validaCO(theForm, operation) {
  if(operation != 'Elimina') {
    if (theForm.id.value=="") {
      alert("Inserire il codice contenuto.");
      theForm.id.focus();
      return false;
    } else {
      if(!checkStringNumber(theForm.id.value)){
        alert("Inserire un codice contenuto valido.");
        theForm.id.focus();
        return false;
      }else{
	    if (theForm.ds_titolo.value=="") {
	      alert("Inserire il titolo del contenuto!");
	      theForm.ds_titolo.focus();
	      return false;
	    }
	  }
    }
  } 
  doSubmit(theForm, operation);
}

// Validazioni Ammortamento
function validaAmm(theForm, operation) {
  if(theForm.num_importo.value=="") {
	alert("E' necessario specificare l'importo del mutuo da calcolare.");
	theForm.num_importo.focus();
	return false;
  } else if(!checkReal(theForm.num_importo)){
    alert("E' necessario specificare un valore numerico per il campo importo mutuo.");
	theForm.num_importo.focus();
	return false;
  } else if(theForm.num_anno.value=="") {
	alert("E' necessario specificare l'anno di accensione del mutuo.");
	theForm.num_anno.focus();
	return false;
  } else if(!checkNumber(theForm.num_anno)){
    alert("E' necessario specificare un valore numerico per il campo anno di accensione del mutuo.");
	theForm.num_anno.focus();
	return false;
  }
  if(theForm.mode.value == 'S'){
    if(theForm.num_maxirata){
	  if(theForm.num_maxirata.value=="") {
		alert("E' necessario specificare l'importo della maxirata.");
		theForm.num_maxirata.focus();
		return false;
	  } else if(!checkReal(theForm.num_maxirata)){
		alert("E' necessario specificare un valore numerico per il campo maxirata.");
		theForm.num_maxirata.focus();
		return false;
	  } else {
	    appo = theForm.num_maxirata.value.replace(/\,/g,".");
	    tot = theForm.num_importo.value.replace(/\,/g,".");
	    if(appo < 0){
	      alert("E' necessario specificare un valore maggiore di 0 per il campo maxirata.");
		  theForm.num_maxirata.focus();
		  return false;
	    } else if(appo > (tot/2)){
	      alert("L' importo della maxirata non può essere maggiore del 50 percento del mutuo.");
		  theForm.num_maxirata.focus();
		  return false;
	    }
	  }
    }
  }else{
    if(theForm.num_spread.value=="") {
	  alert("E' necessario specificare la percentuale del tasso.");
	  theForm.num_spread.focus();
	  return false;
	} else if(!checkReal(theForm.num_spread)){
	  alert("E' necessario specificare un valore numerico per il campo percentuale.");
	  theForm.num_spread.focus();
	  return false;
	}  
	theForm.ds_tipo_tasso.value=theForm.id_tipo_tasso.options[theForm.id_tipo_tasso.selectedIndex].text;
  }
  theForm.ds_frequenza.value=theForm.num_frequenza.options[theForm.num_frequenza.selectedIndex].text;
  theForm.ds_mese.value=theForm.num_mese.options[theForm.num_mese.selectedIndex].text;
  doSubmit(theForm, operation);
}

// Validazioni AbiCab
function validaAbiCab(theForm, operation) {
  var bolApp = true	
  var bolOk = true
  bolApp = bolApp && (theForm.cd_abi.value == "");
  bolApp = bolApp && (theForm.cd_cab.value == "");
  bolApp = bolApp && (theForm.ds_banca.value == "");
  bolApp = bolApp && (theForm.ds_filiale.value == "");
  bolApp = bolApp && (theForm.ds_indirizzo.value == "");
  bolApp = bolApp && (theForm.ds_comune.value == "");
  bolApp = bolApp && (theForm.ds_cap.value == "");
  bolApp = bolApp && (theForm.ds_prov.value == "");
  if (bolApp == true) {
	alert("Per effettuare la ricerca è necessario valorizzare almeno un campo!");
	theForm.cd_abi.focus();
	return false;
  }	
  
  if(theForm.ds_cap.value != ""){
    if(!checkNumber(theForm.ds_cap)){
      alert("Inserire un CAP corretto!");
      theForm.ds_cap.focus();
      return false;
    }
  }
  
  if(theForm.cd_abi.value != ""){
    theForm.cd_abi.value = zeriDavanti(theForm.cd_abi.value, 5);
  }
  if(theForm.cd_cab.value != ""){
    theForm.cd_cab.value = zeriDavanti(theForm.cd_cab.value, 5);
  }
  			
  doSubmit(theForm, operation);
}

// Validazioni Contatti
function validaCon(theForm, operation) {
  if (theForm.ds_identita.value=="") {
    if(theForm.idm.value == "i")
      alert("Attenzione, e' necessario completare il campo 'Ragione sociale'.");
    else
      alert("Attenzione, e' necessario completare il campo 'Cognome/Nome'.");
    theForm.ds_identita.focus();
    return false;
  } else if (theForm.ds_indirizzo.value=="") {
    alert("Attenzione, e' necessario completare il campo 'Indirizzo'.");
    theForm.ds_indirizzo.focus();
    return false;
  } else if (theForm.ds_cap.value=="") {
    alert("Attenzione, e' necessario completare il campo 'CAP'.");
    theForm.ds_cap.focus();
    return false;
  } else if(!checkNumber(theForm.ds_cap)){
    alert("Attenzione, utilizzare solo numeri nel campo 'CAP'.");
    theForm.ds_cap.focus();
    return false;
  } else if (theForm.ds_comune.value=="") {
    alert("Attenzione, e' necessario completare il campo 'Città'.");
    theForm.ds_comune.focus();
    return false;
  } else if (!Is_Select(theForm.id_provincia)) {
    alert("Attenzione, e' necessario selezionare la 'Provincia'.");
    theForm.id_provincia.focus();
    return false;
  } else if (theForm.ds_email.value=="") {
    alert("Attenzione, e' necessario completare il campo 'E-mail'.");
    theForm.ds_email.focus();
    return false;  
  } else if(!Is_Email(theForm.ds_email.value)){
    alert("Attenzione, e' necessario inserire un indirizzo corretto nel campo 'E-mail'.");
    theForm.ds_email.focus();
    return false; 
  } else if (theForm.ds_messaggio.value=="") {
    alert("Attenzione, e' necessario completare il campo 'Messaggio'.");
    theForm.ds_messaggio.focus();
    return false;
  } else if (!theForm.ds_autorizzo[0].checked){
    alert("Attenzione, e' necessario autorizzare il trattamento dei dati personali.");
    theForm.ds_autorizzo[0].focus();
    return false;
  }
  
  var bolOk = confirm ("Confermi l'invio dei dati inseriti ?");
  if(bolOk) doSubmit(theForm, operation);
}