function defvar() {
  chkV = new Array();
  return null;
}

function callPagina(npagina, url) {
  window.location = url+'&pagina='+npagina;
  return null;
}

function view(url, id) {
  window.location = url+'?id='+id;
  return null;
}


function setCampi(valore, chkV) {
  var lchk = chkV.length;
  if (valore.checked) {
    chkV[lchk] = valore.value;;
  }
  else {
    if (lchk > 1) {
      var trovato = false;
      for (var i=0; i<lchk; i++) {
        if (chkV[i] == valore.value) {
          trovato = true;
          lchk--;
        }
        if (trovato) {
          chkV[i] = chkV[i+1];
        }
      }
      if (trovato)
        chkV.length--;
    }
    else {
      chkV.length = 0;
    }
  }
  return null;
}

function inPubblicazione(tabella, url, stato, pagina) {
  var ids = "";
  for (var i=0; i<chkV.length; i++) {
    ids = ids + chkV[i] + '|';
  }   
  chkV.length = 0;
  window.location = 'inpub.php?tabella='+tabella+'&id='+ids+'&url='+url+'&stato='+stato+'&pagina='+pagina;  	
  return null;
}

function pubblica(tabella, url, stato, pagina) {
  var ids = "";
  for (var i=0; i<chkV.length; i++) {
    ids = ids + chkV[i] + '|';
  }   
  chkV.length = 0;
  window.location = 'pubblica.php?tabella='+tabella+'&id='+ids+'&url='+url+'&stato='+stato+'&pagina='+pagina;  	
  return null;
}

function cancella(tabella, url, stato, pagina) {
  var ids = "";
  for (var i=0; i<chkV.length; i++) {
    ids = ids + chkV[i] + '|';
  }   
  chkV.length = 0;
  window.location = 'cancella.php?tabella='+tabella+'&id='+ids+'&url='+url+'&stato='+stato+'&pagina='+pagina;  	
  return null;
}

function cancellaFoto(url, id, pagina) {
  var ids = "";
  for (var i=0; i<chkV.length; i++) {
    ids = ids + chkV[i] + '|';
  }   
  chkV.length = 0;
  window.location = 'cancfoto.php?id='+ids+'&urlx='+url+'&pagina='+pagina+'&idtab='+id;  	
  return null;
}

function cancellaUser(tabella, url, pagina) {
  var ids = "";
  for (var i=0; i<chkV.length; i++) {
    ids = ids + chkV[i] + '|';
  }   
  chkV.length = 0;
  window.location = 'cancuser.php?tabella='+tabella+'&id='+ids+'&url='+url+'&pagina='+pagina;  	
  return null;
}

function inCostruzione(tabella, url, stato, pagina) {
  var ids = "";
  for (var i=0; i<chkV.length; i++) {
    ids = ids + chkV[i] + '|';
  }   
  chkV.length = 0;
  window.location = 'incostr.php?tabella='+tabella+'&id='+ids+'&url='+url+'&stato='+stato+'&pagina='+pagina;  	
  return null;
}

function chkData(data, messaggio, vuoto) {
// controlla se il valore contenuto in data è valido e lo formatta in gg/mm/aaaa
// se la data non è valida visualizza il valore messaggio ed evidenzia il valore errato
// se il messaggio è vuoto non visualizza la finestra di dialogo 
// i formati accettati in ingresso sono: ggmmaa; gg/mm/aa; gg-mm-aa; gg.mm.aa
// aa può essere sia a due che a quattro cifre: 
// se è di due cifre antepone 20 se l'anno risultante è minore o uguale di quello corrente altrimeni mette 19 
// il risultato è messo nell'oggetto data dato in ingresso;
// PARAMETRI
// data:      oggetto il cui valore è testato
// messaggio: stringa visualizzata nella finestra di errore. Se è vuota non viene visualizzata la finestra
// vuoto:     se true è ammessa una lunghezza 0

  var d, gg, mm, aa, a1, s, oggi, errore;
  d = data.value;
  if ((d.length != 0) || (!vuoto)) {
    errore = true;
    oggi = new Date();
    canno = oggi.getFullYear();
    if ((d.length >=6) && (d.length <= 10)) {
      errore = false;
      s = d.charAt(2);
      if ((s == '/') || (s == '.') || (s == '-'))
        d = d.substring(0,2) + d.substring(3);
      s = d.charAt(4);
      if ((s == '/') || (s == '.') || (s == '-'))
        d = d.substring(0,4) + d.substring(5);
      gg = d.substring(0, 2);
      mm = d.substring(2, 4);
      a1 = d.substring(4);
      if (a1.length == 2) {
        aa = '20'+ a1;
        if (aa > canno)
          aa = '19' +a1;
      }
      else
        aa = a1;
      var dcorr = new Date(aa, mm-1, gg);
      if (dcorr.getDate() != gg)
        errore = true;
      if (dcorr.getMonth() != (mm-1))
        errore = true;
      if (dcorr.getFullYear() != aa)
        errore = true;
      if (isNaN(dcorr))
        errore = true;
      else 
        data.value = gg+'/'+mm+'/'+aa;
    }
    if (errore) {
      data.select();
      if (messaggio != '')
        window.alert(messaggio);
    }
  }
  return !errore;
}

function chkOra(ora, messaggio, vuoto) {
// controlla se il valore contenuto in ora è valido e lo formatta in hh.mm
// se l'ora non è valida visualizza il valore messaggio ed evidenzia il valore errato
// se il messaggio è vuoto non visualizza la finestra di dialogo 
// i formati accettati in ingresso sono: hhmm; hh.mm; hh-mm; hh:mm
// il risultato è messo nell'oggetto data dato in ingresso;
// PARAMETRI
// ora:       oggetto il cui valore è testato
// messaggio: stringa visualizzata nella finestra di errore. Se è vuota non viene visualizzata la finestra
// vuoto:     se true è ammessa una lunghezza 0

  var h, hh, mm, errore, s;
  h = ora.value;
  if ((h.length != 0) || (!vuoto)) {
    errore = true;
    if ((h.length >=4) && (h.length <= 5)) {
      errore = false;
      s = h.charAt(2);
      if ((s == ':') || (s == '.') || (s == '-'))
        h = h.substring(0,2) + h.substring(3);
      hh = h.substring(0, 2);
      mm = h.substring(2, 4);
	if ((hh < '00') || (hh > '23'))
	  errore = true;
	if ((mm < '00') || (mm > '59'))
	  errore = true;
    }
    if (errore) {
      ora.select();
      if (messaggio != '')
        window.alert(messaggio);
    }
    else
      ora.value = hh+'.'+mm;
  }
  return !errore;
}

function chkCifre(obj, ammessi, messaggio) {
// controlla che il valore di obj sia solo composto di cifre e di caratteri indicati nella stringa ammessi
// se nel valore è presente un altro carattere visualizza il valore messaggio e si posiziona in obj
// se messaggio è nullo non visualizza la finestra di dialogo 
// ritorna true o false
// PARAMETRI
// obj:       oggetto il cui valore è testato
// ammessi:   stringa con i caratteri ammessi oltre alle cifre
// messaggio: stringa visualizzata nella finestra di errore. Se è vuota non viene visualizzata la finestra

  var risultato = true;
  var valore = new String(obj.value);
  var strTest = '0123456789'+ammessi;
  var i = 0;
  while ((i < valore.length) && (risultato)) {
    var ch = valore.charAt(i);
    var trovato = false;
    var k = 0;
    while ((k < strTest.length) && (!trovato)) {
      if (ch == strTest.charAt(k))
        trovato = true;
      k++;
    }
    risultato = trovato;
    i++;
  }
  if (!risultato) {
    if (messaggio != "")
      alert(messaggio);
    obj.select();
  }
  return risultato; 
}

function obbligatorio(obj, messaggio) {
// controlla se è stato inserito un valore per obj
// se obj è vuoto visualizza il valore messaggio e si posiziona in obj
// se messaggio è nullo non visualizza la finestra di dialogo 
// ritorna il valore true se esiste il valore altrimenti false
// PARAMETRI
// obj:       oggetto il cui valore è testato
// messaggio: stringa visualizzata nella finestra di errore. Se è vuota non viene visualizzata la finestra

  var risultato = true;
  var valore = obj.value;
  valore = valore.replace(' ', '');
  if (valore == "") {
    risultato = false;
    if (messaggio != "")
      alert(messaggio);
    obj.focus();
  }
  return risultato; 
}

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title || a.getAttribute("title") == "print" ) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

window.onload = function(e) {
  var cookie = readCookie("style_ic_cerrina");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style_ic_cerrina", title, 365);
}

var cookie = readCookie("style_ic_cerrina");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);
