// ---------------------------------- JQUERY inizializzazione & ready function --------------------------------
// Funzioni JQuery comuni a tutti i programmi
// Funzioni specifiche del programma chiamante stanno in start_interfaccia  (alla fine del prog. chiamante)

//alert('1 - Parte pubblica di jq-ready-02.js');

/*   ***  "titolino": feedback visivo inizio e fine chiamata ajax  ***   */
 var titolino = null;// L'elemento titolino_h1 potrebbe non esistere
 var n_href = 0; // -- var usata nel "catch(myError) di jq_ready-xx"
 var ready_num = 0;// contatore per le chiamate jqready

// --- vars per la funzione update_more()   ??????
 var more_value =  new Array();
 var more_var_out = new Array();
 var more_var_in = new Array();
 var more_s_value = '';

$(document).ready(function() { try {
// --------------------------------------------- vedi commenti in DOCS.txt ed in apt_details
  if (typeof(document.getElementById('titolino_h1')) != undefined) {
    titolino = document.getElementById('titolino_h1');
  }
  ready_num ++;
  //alert('4 - funzione jqready - ready_num: '+ready_num);
  if (titolino) {titolino.style.background = 'green';}
  //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ AGGIUSTARE DATE !!!!!!!!
  // NON SI FA NIENTE !!!

if (titolino) titolino.style.background = '#F0F0F0';

  // esecuzione funzioni da attivare all'inizio del programma chiamante'
  //alert('start interfaccia: '+typeof(start_interfaccia))
  if (typeof(start_interfaccia) === 'function') {
    //alert('4bis (jq ready function) - START interfaccia - ready_num: '+ready_num)  
    start_interfaccia();
  }
  return true; 
 }catch(myError) {
   vars_state = '| Error : ' + myError.name + ' | Message:' + myError.message + ' | n_href: ' + n_href;
   var j_ID_S=document.getElementById("ID_S");
   if(typeof(j_ID_S)==undefined || j_ID_S==null){
     alert("Jquery ready function 3 - NO ID_S - " + vars_state);
     return false;
   }else{
    alert("Jquery ready function 3: " + vars_state);
    return false;
}}});

// --- nome_var       nome della variabile in un eventuale GET
// --- more_var_in    nome del campo INPUT che ospita la variabile nome_var e variato dall'utente
// --- more_var_out   nome del campo INPUT che ospita la variabile nome_var nel form di uscita
function update_more(nome_var){ try{ // ---------------- le variabili generiche sono raggruppate in MORE
  return true; // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ NON IMPLEMENTATA !!!
  var old_var_value = more_value[nome_var];                             // @@@@@@@@@@@@@@@@@@@@ NON SERVE!
//  alert('more - var old: ' + nome_var + '\nvalue: ' + old_var_value);         // @@@@@@@@@@@@@@@@@@@@ NON SERVE!

  var var_value = document.getElementById(more_var_in[nome_var]).value; // --- aggiorna il deposito "more"
//  alert('more - in: ' + more_var_in[nome_var] + '\nvalue: ' + var_value);
//  alert('more - out ID: ' + more_var_out[nome_var]);
//  document.getElementById(more_var_out[nome_var]).value = var_value;
  more_value[nome_var] = var_value;
  more_s_value = '';                                                    // --- nuovo valore "more"
  for (var i in more_value) {
	  more_s_value += '|' + i + '=' + more_value[i];
  }
  alert ('more - valore S__more: ' + more_s_value);
  
  jquery_ajax('more', more_s_value);

 }catch(myError) {
   vars_state = '| var_update error: ' + myError.name + ' | Message:' + myError.message;
   var j_ID_S=document.getElementById("ID_S");
   if(typeof(j_ID_S)==undefined || j_ID_S==null){
     alert("update more - NO ID_S - " + vars_state);
     return false;
   }else{
    alert("update_more error: " + vars_state);
    return false;
}}};

function var_update(nome_var){ try{ // ---------------------------------======---------- update variabile nome_var ---
   //alert('Aggiornamento variabile ' + nome_var);
   var valore_var = document.getElementById(nome_var).value;
   return  jquery_ajax(nome_var, valore_var);

 }catch(myError) {
   vars_state = '| var_update error: ' + myError.name + ' | Message:' + myError.message;
   var j_ID_S=document.getElementById("ID_S");
   if(typeof(j_ID_S)==undefined || j_ID_S==null){
     alert("var_update - NO ID_S - " + vars_state);
     return false;
   }else{
    alert("var_update: " + vars_state);
    return false;
}}};

function var2db(nome_var, valore_var, referer,href_id){ try{ // --------------- update variabile nome_var ---
   if (titolino) titolino.style.background = 'red'; // -------------------------(accende titolino)---
   var data = '- Error: No Data -';
   ajax_ok = false;
   //alert('ajax2db: '+nome_var+'\nvalore: '+valore_var);
   $.post("db-save-var"+".php", {ID_prog: ID_prog, progName: progName, ID_S: ID_S, referer: referer,
           href_id: href_id, evento: "Aggiorna", 
           var_name: nome_var, var_value: valore_var},
       function(data){
       //alert("Aggiornamento db: " + nome_var + ' value: ' + valore_var + '\nRisposta: ' + data);
       ajax_ok = true;
   });
   if (titolino) titolino.style.background = c_bg; // -------------------------(spenge titolino)---
   return ajax_ok;

 }catch(myError) {
   vars_state = '| var_update error: ' + myError.name + ' | Message:' + myError.message;
   var j_ID_S=document.getElementById("ID_S");
   if(typeof(j_ID_S)==undefined || j_ID_S==null){
    alert("var2db - NO ID_S - " + vars_state);
    return false;
   }else{
    alert("var2db: " + vars_state);
    return false;
}}};

// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@  DA ELIMINARE dopo aggiustato box-upper-left  @@@@@@@@@@@
function var2db_old(nome_var){ try{ // ---------------------------------======---------- update variabile nome_var ---
   //alert('Aggiornamento variabile ' + nome_var);
   var valore_var = document.getElementById(nome_var).value;
   return  ajax2db(nome_var, valore_var);

 }catch(myError) {
   vars_state = '| var_update error: ' + myError.name + ' | Message:' + myError.message;
   var j_ID_S=document.getElementById("ID_S");
   if(typeof(j_ID_S)==undefined || j_ID_S==null){
    alert("var2db_old - NO ID_S - " + vars_state);
    return false;
   }else{
    alert("var_update: " + vars_state);
    return false;
}}};
// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@  FINE ELIMINAZIONE  @@@@@@@@@@@

function ajax2db(nome_var, valore_var){ try{ // -------------------------------------------- chiamata jq-AJAX ---
   if (titolino) titolino.style.background = 'red'; // -------------------------(accende titolino)---
   var data = '- Error: No Data -';
   ajax_ok = false;
   //alert('ajax2db: '+nome_var+'\nvalore: '+valore_var);
   $.post("db-save-var"+".php", {ID_prog: ID_prog, progName: progName, ID_S: ID_S, referer: referer,
           href_id: "9999", evento: "Aggiorna", 
           var_name: nome_var, var_value: valore_var},
       function(data){
       //alert("Aggiornamento db: " + nome_var + ' value: ' + valore_var + '\nRisposta: ' + data);
       ajax_ok = true;
   });
   if (titolino) titolino.style.background = c_bg; // -------------------------(spenge titolino)---
   return ajax_ok;

 }catch(myError) {
   vars_state = '| var_update error: ' + myError.name + ' | Message:' + myError.message;
   var j_ID_S=document.getElementById("ID_S");
   if(typeof(j_ID_S)==undefined || j_ID_S==null){
    alert("ajax2db - NO ID_S - " + vars_state);
    return false;
   }else{
    alert("ajax2db: " + vars_state);
    return false;
}}};

function jquery_ajax(nome_var, valore_var){ try{ // -------------------------------------------- chiamata jq-AJAX ---
   if (titolino) titolino.style.background = 'red'; // -------------------------(accende titolino)---
   var data = '- Error: No Data -';
   ajax_ok = false;
   //alert('ajax - var: '+nome_var+'\nvalore: '+valore_var);
   $.post("session_mem"+".php", {ID_prog: ID_prog, progName: progName, ID_S: ID_S, referer: referer,
           href_id: "9999", evento: "Aggiorna", 
           var_name: nome_var, var_value: valore_var},
       function(data){
       //alert("Aggiornamento: " + nome_var + ' value: ' + valore_var + '\nRisposta: ' + data);
       ajax_ok = true;
   });
   if (titolino) titolino.style.background = c_bg; // -------------------------(spenge titolino)---
   return ajax_ok;

 }catch(myError) {
   vars_state = '| var_update error: ' + myError.name + ' | Message:' + myError.message;
   var j_ID_S=document.getElementById("ID_S");
   if(typeof(j_ID_S)==undefined || j_ID_S==null){
     //alert("var_update - NO ID_S - " + vars_state);
     return false;
   }else{
    //alert("var_update: " + vars_state);
    return false;
}}};

function makerequest(serverPage) { try{ // -------------------------- chiamata jq-AJAX per aggiornamento database ---
   //document.getElementById("titolino_h1").style.background = 'red'; // -------------------------(accende titolino)---
   if (titolino) titolino.style.background = 'red'; // -------------------------(accende titolino)---
   var data = '- Error: No Data -';
   var ajax_ok='ko';
   $.post(serverPage, function(data){
      if( data.substr(0,4).search('ok') >= 0 ) {
        var ajax_ok='ok';
        //ShowAndHide('foot_end1','foot_end2');
      }else{
        var ajax_ok='ko';
        //ShowAndHide('foot_end1','foot_end3');
      }
   });
   //alert('Risposta: ' + data);
   //document.getElementById("titolino_h1").style.background = c_bg;
   if (titolino) titolino.style.background = c_bg; // -------------------------(spenge titolino)---
   return ajax_ok;
 }catch(myError) {
   vars_state = '| var_update error: ' + myError.name + ' | Message:' + myError.message;
   var j_ID_S=document.getElementById("ID_S");
   if(typeof(j_ID_S)==undefined || j_ID_S==null){
     //alert("var_update - NO ID_S - " + vars_state);
     return false;
   }else{
    //alert("var_update: " + vars_state);
    return false;
}}};

