var somethingChangedVar = false;

function somethingChanged(){
  somethingChangedVar = true;
}

function addSinistroBase(edit, sinistro){
  var giorno    = document.getElementById('dataGiorno').value;
  var mese      = document.getElementById('dataMese').value;
  var anno      = document.getElementById('dataAnno').value;
  var ore       = document.getElementById('oraOra').value;
  var minuti    = document.getElementById('oraMinuti').value;
  var luoghi    = document.getElementById('luogo');
  var luogo     = luoghi.options[luoghi.selectedIndex].text;
  var luogospec = document.getElementById('luogospec').value;
  var feriti    = false;

  var d = new Date();
  var today = new Date();

  luogospec = luogospec.replace('\n', '');

  //  alert("~~~" + luogospec + "~~~");

  if(luogospec == ""){
      alert("ATTENZIONE: Il luogo non e' specificato...\nInserisci una via o comunque alcuni dettagli per specificare meglio dove e' accaduto il sinistro...");
      setFocusWithSelect('luogospec');
      return;
  }


  //return;
  luogo = luogo + " - " + luogospec;


  if(anno != currentDate.getFullYear()){
      if(!confirm("L'anno inserito non corrisponde a quello nel registro! Inserisco il dato nel registro corretto?")){
	  setFocusWithSelect('dataAnno');
	  return;
      }
  }
  

  if(anno.length != 4){
      alert("ATTENZIONE: L'anno deve essere di 4 cifre!");
      setFocusWithSelect('dataAnno');
      return;
  }


  if(isNaN(giorno) || giorno == ""){
    alert("ATTENZIONE: Il giorno inserito non e' valido!");
    setFocusWithSelect('dataGiorno');
    return;
  }else{
      giorno =giorno;
  }

  if(mese > 12 || mese < 1 || isNaN(mese) || mese==""){
      alert("ATTENZIONE: Il mese inserito non e' corretto...");
      setFocusWithSelect('dataMese');
      return;
  }else{
      mese = parseInt(mese);
  }

  if(ore > 23 || ore < 00 || isNaN(ore) || ore==""){
    alert("ATTENZIONE: L'orario inserito non e' corretto... le ore vanno da 00 a 23");
    setFocusWithSelect('oraOra');
    return;
  }else{
      ore = ore;
  }

  if(minuti > 59 || minuti < 00 || isNaN(minuti) || minuti == ""){
    alert("ATTENZIONE: L'orario inserito non e' corretto... i minuti vanno da 00 a 59");
    setFocusWithSelect('oraMinuti');
    return;
  }else{
      minuti = minuti;
  }
    
  if(luogo == ""){
    alert("ATTENZIONE: Specificare il luogo del sinistro");
    setFocusWithSelect('luogo');
    return;
  }

  //  alert(giorno);
  //alert(mese);


  switch(mese){
  case 1:
  case 3:
  case 5:
  case 7:
  case 8:
  case 10:
  case 12:
    if(giorno > 31 || giorno < 01){
      alert("ATTENZIONE: Il giorno inserito non coincide con i giorni del mese!");
      setFocusWithSelect('dataGiorno');
      return;
    }
    break;
  case 2:
    if(giorno > 29 || giorno < 01){
      alert("ATTENZIONE: Il giorno inserito non coincide con i giorni del mese!");
      setFocusWithSelect('dataGiorno');
      return;
    }
    break;
  case 4:
  case 6:
  case 9:
  case 11:
    if(giorno > 30 || giorno < 01){
      alert("ATTENZIONE: Il giorno inserito non coincide con i giorni del mese!");
      setFocusWithSelect('dataGiorno');
      return;
    }
    break;
  }

  d.setFullYear(anno, mese - 1, giorno);

  if(d > today){
    alert("ATTENZIONE: La data inserita e' nel futuro!");
    setFocusWithSelect('dataGiorno');
    return;    
  }

  if(document.getElementById('feritiSAdd').checked == true)
    feriti = true;

  if(edit == 1){
      x_editSinistroBase(sinistro, giorno, mese, anno, ore, minuti, luogo, feriti, addSinistroBaseCallback);
  }else{
      x_addSinistroBase(giorno, mese, anno, ore, minuti, luogo, feriti, addSinistroBaseCallback);
  }
}

function addSinistroBaseCallback(focus, z){
  insertAjaxCallback(focus, z);
  setFocus("identificazione");
}

var sinistroGlobale;

function addVeicoloPerSinistro(sinistro){
    var cognome         = document.getElementById('cognome').value;
    var nome            = document.getElementById('nome').value;

    sinistroGlobale = sinistro;

    x_addAnagraficaReturnIndex(cognome, nome, addVeicoloPerSinistro2);
    //  addAnagraficaReturnIndex(addVeicoloPerSinistro2);
}

function addVeicoloPerSinistro2(focus, z){
  var identificazione    = document.getElementById('identificazione').value;
  var tipi               = document.getElementById('tipo');
  var targa              = document.getElementById('targa').value;
  var modello            = document.getElementById('modello').value;
  var conducente         = z;

  sinistro = sinistroGlobale;
  sinistroGlobale = 0;

  var d = new Date();
  var today = new Date();
  var tipo = parseInt(tipi.options[tipi.selectedIndex].id);


  if(identificazione == ""){
    alert("ATTENZIONE: L'identificazione del veicolo e' obbligatoria!\nSolitamente viene specificata con le lettere maiuscole dell'alfabeto (A, B, C, ...)");
    return;
  }

  if(targa == ""){
    if(!confirm("ATTENZIONE: La targa del veicolo non e' specificata...  Continuo ugualmente?"))
      return;
  }

  if(modello == ""){
    if(!confirm("ATTENZIONE: Il modello del veicolo non e' specificato... Continuo ugualmente?"))
      return;
  }

  x_addVeicoloPerSinistro(sinistro, identificazione, tipo, targa, modello, conducente, veicoliPerSinistroCallback);
}

function veicoliPerSinistroCallback(focus, z){
    document.getElementById('veicoliInseriti').innerHTML = z;
    setFocusWithSelect(focus);
}

function addAgentePerSinistro(sinistro){
  var matricole = document.getElementById('agenti');
  var matricola = parseInt(matricole.options[matricole.selectedIndex].id);

  x_addAgentePerSinistro(sinistro, matricola, agentiPerSinistroCallback);
}

function agentiPerSinistroCallback(focus, z){
    document.getElementById('accertatoriInseriti').innerHTML = z;
    setFocusWithSelect(focus);
}

function editSinistro(sinistro){
  var nuovoSinistro = document.getElementById('sinistro' + sinistro).value;

  x_editSinistro(sinistro, nuovoSinistro, generalAjaxCallback);

}

function delSinistro(sinistro){
  if(!confirm("Sei sicuro di voler eliminare il sinistro " + sinistro + "?")){
    return;
  }
  x_delSinistro(sinistro, generalAjaxCallback);

}




function sinistri_insertAjaxCallback(focus, z){
    insertAjaxCallback(focus, z);

//     oACDS = new YAHOO.widget.DS_XHR("./cercacomuni.php", ["\n", "\t"]);
//     oACDS.responseType = YAHOO.widget.DS_XHR.TYPE_FLAT;
//     oACDS.maxCacheEntries = 30;
//     oACDS.queryMatchSubset = true;
    
//     // Instantiate first AutoComplete
//     var myInput = document.getElementById('natoa');
//     var myContainer = document.getElementById('natoacontainer');
//     oAutoComp0 = new YAHOO.widget.AutoComplete(myInput, myContainer, oACDS);
//     oAutoComp0.queryDelay = 0;
//     oAutoComp0.prehighlightClassName = "yui-ac-prehighlight";
//     oAutoComp0.formatResult = function(oResultItem, sQuery) {
// 	var sKey = oResultItem[0];
// 	var nQuantity = oResultItem[1];
// 	var sKeyQuery = sKey.substr(0, sQuery.length);
// 	var sKeyRemainder = sKey.substr(sQuery.length);
// 	var aMarkup = ["<div id='ysearchresult'><div class='ysearchquery'>",
// 		       nQuantity,
// 		       "</div><span style='font-weight:bold'>",
// 		       sKeyQuery,
// 		       "</span>",
// 		       sKeyRemainder,
// 		       "</div>"];
// 	return (aMarkup.join(""));
//     };
    
//     myInput = document.getElementById('resa');
//     myContainer = document.getElementById('resacontainer');
//     oAutoComp1 = new YAHOO.widget.AutoComplete(myInput, myContainer, oACDS);
//     oAutoComp1.queryDelay = 0;
//     oAutoComp1.prehighlightClassName = "yui-ac-prehighlight";


//     var helloWorld = function(){
// 	alert(nKeynum);
//     }
}


function addTipoVeicolo(){
  var tipo = document.getElementById('tipoAdd').value;
  var sigla = document.getElementById('siglaAdd').value;
  var descrizione = document.getElementById('descrizioneAdd').value;

  x_addTipoVeicolo(tipo, sigla, descrizione, generalAjaxCallback);
}

function delTipoVeicolo(tipo){
  if(!confirm("Sei sicuro di voler eliminare il record " + tipo + "?"))
    return;

  x_delTipoVeicolo(tipo, generalAjaxCallback);
}

function editTipoVeicolo(tipo){
  var nuovaTipo = document.getElementById('tipo' + tipo).value;
  var sigla = document.getElementById('sigla' + tipo).value;
  var descrizione = document.getElementById('descrizione' + tipo).value;

  x_editTipoVeicolo(tipo, nuovaTipo, sigla, descrizione, generalAjaxCallback);
}

