function setVisible(){
	var cells = document.getElementsByTagName("div");
	var i;
	for (i=0;i<cells.length;i++) {
		var eId = cells[i].getAttribute("id");
		if((eId!=null)&&(eId!='')) {
			if (eId.indexOf("DivEv")!=-1) {
				cells[i].style.display="none";
			}
		}
	} 
}

function setVisibleOk(who){
	var element = document.getElementById(who);
	element.style.display="";
}

function setVisibleKo(who){
	var element = document.getElementById(who);
	element.style.display="none";
}

function openClose(who){
	if (document.getElementById){
		var element = null;
		
		tmp = document.getElementsByTagName('div');
		for (i=0;i<tmp.length;i++){
			if (tmp[i].className == "panelCit") {
				tmp[i].style.display = "none";
			}
		}
		
		tmp = document.getElementsByTagName('div');
		for (i=0;i<tmp.length;i++){
			if (tmp[i].className == "panelImp") {
				tmp[i].style.display = "none";
			}
		}
		element = document.getElementById (who);
		if (element.style.display == "none"){
			element.style.display = "";
		} else {
			element.style.display = "none";
		}
	} else {
		alert("Dom Object non supportato!");
	}
}

function openCloseSez(who){
	if (document.getElementById){
		var element = null;
		
		tmp = document.getElementsByTagName('div');
		for (i=0;i<tmp.length;i++){
			if (tmp[i].id.substr(0,3) == "sez") {
				tmp[i].style.display = "none";
			}
		}
		
		element = document.getElementById (who);
		if (element.style.display == "none"){
			element.style.display = "";
		} else {
			element.style.display = "none";
		}
	} else {
		alert("Dom Object non supportato!");
	}
}


function gotoWhere(){
	if (document.getElementById){
		var theform = null;
		if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {
			theform = document.forms["Form1"];
		} else {
			theform = document.Form1;
		}
		window.location.hash = theform["where"].value
		//document.url= document.url+"#"+theform["where"].value
	} else {
		alert("Dom Object non supportato!");
	}
}


// -------------------------------------------------------
// apertura pagina di edit delle schede
// -------------------------------------------------------
function EditSk(page)
{
  OpenWindow(page, 'EditSk', 700, 500);
}


// -------------------------------------------------------
// messaggio di conferma
// -------------------------------------------------------
function ConfirmDelete(msg)
{
  return confirm(msg);
}

// -------------------------------------------------------
// permette di digitare solo numeri
// -------------------------------------------------------
function numbersonly(myfield, e, dec)
{
  var key;
  var keychar;
  
  if (window.event)
     key = window.event.keyCode;
  else if (e)
     key = e.which;
  else
     return true;
  keychar = String.fromCharCode(key);
  
  // control keys
  if ((key==null) || (key==0) || (key==8) || 
      (key==9) || (key==13) || (key==27) )
     return true;
  
  // numbers
  else if ((("0123456789").indexOf(keychar) > -1))
     return true;
  
  // decimal point jump
  else if (dec && (keychar == "."))
     {
     myfield.form.elements[dec].focus();
     return false;
     }
  else
     return false;
}

// -------------------------------------------------------
// permette di digitare solo numeri e lettere
// -------------------------------------------------------
function letternumber(e)
{
  var key;
  var keychar;
  
  if (window.event)
     key = window.event.keyCode;
  else if (e)
     key = e.which;
  else
     return true;
  keychar = String.fromCharCode(key);
  keychar = keychar.toLowerCase();
  
  // control keys
  if ((key==null) || (key==0) || (key==8) || 
      (key==9) || (key==13) || (key==27) )
     return true;
  
  // alphas and numbers
  else if (((" '.-abcdefghijklmnopqrstuvwxyz0123456789àèìòù?_,éç&âäèêëöüçß&()").indexOf(keychar) > -1))
     return true;
  else
     return false;
}

// -------------------------------------------------------
// classica openWindow
// -------------------------------------------------------
function OpenWindow(url, windowName, w, h)
{
   var wf = "";
   wf = wf + "width=" + w;
   wf = wf + ",height=" + h;
   wf = wf + ",resizable=yes";
   wf = wf + ",scrollbars=yes";
   wf = wf + ",menubar=no";
   wf = wf + ",toolbar=no";
   wf = wf + ",directories=no";
   wf = wf + ",location=no";
   wf = wf + ",status=yes";
   var wnd = window.open(url,windowName,wf);
   wnd.focus();
}


// -------------------------------------------------------
// si muove tra le pagine del navigatore
// -------------------------------------------------------
function ChangeOffset(NewOffset) {
  document.mainForm.last_record.value = NewOffset;
  document.mainForm.submit();
}

//-------------------------------------------------------
// check mandatory registrazione
//-------------------------------------------------------
function checkMandatory(id) {
  return ((document.getElementById(id).value).length == 0);
}

function checkCF(id) {
  return (!((document.getElementById(id).value).length == 16));
}

function checkCAP(id) {
  if (!((document.getElementById(id).value).length == 5)) {
    return true;
  }
  else {
    var stringa = document.getElementById(id).value;
    var numbers = "0123456789";
    var isNumber = true;
    for(j=0;j<5;j++) {
      isNumber = isNumber && (numbers.indexOf(stringa.substring(j, j+1)) >= 0)
    }
    return (!isNumber);
    
  }
}

function checkData(id) {
  if (!((document.getElementById(id).value).length == 10)) {
    return true;
  }
  else {
    var stringa = document.getElementById(id).value;
    var numbers = "0123456789";
    var isNumber = true;
    for(j=0;j<10;j++) {
      if ((j == 2) || (j == 5)) {
        isNumber = isNumber && (stringa.substring(j, j+1) == "/");
      } else {
        //isNumber = isNumber && (numbers.indexOf(stringa.substring(j, j+1)) >= 0);
      }
    }
    return (!isNumber);
  }
}


function checkRegistrazione() {
  var id_errato = "";
  var messaggio_errore = "";
  var formOK = true;  
  
  
  if (checkMandatory("cognome")) {
    id_errato = "cognome";
    messaggio_errore = "Il campo cognome è obbligatorio!"
    formOK = false;
  }
  
  if (checkMandatory("nome")) {
    id_errato = "nome";
    messaggio_errore = "Il campo nome è obbligatorio!"
    formOK = false;
  }

  if (checkCF("codiceFiscale")) {
    id_errato = "codiceFiscale";
    messaggio_errore = "Il campo codice fiscale è obbligatorio!"
    formOK = false;
  }

  if (checkData("dataNascita")) {
    id_errato = "dataNascita";
    messaggio_errore = "Il campo data di nascita è obbligatorio e nella forma gg/mm/aaaa (es. 12/05/1958)!"
    formOK = false;
  }

  if (checkMandatory("comuneNascita")) {
    id_errato = "comuneNascita";
    messaggio_errore = "Il campo comune di nascita è obbligatorio!"
    formOK = false;
  }

  if (checkMandatory("numeroDocumento")) {
    id_errato = "numeroDocumento";
    messaggio_errore = "Il campo numero documento è obbligatorio!"
    formOK = false;
  }

  if (checkData("dataRilDocumento")) {
    id_errato = "dataRilDocumento";
    messaggio_errore = "Il campo data di rilascio del documento di identità è obbligatorio e nella forma gg/mm/aaaa (es. 12/05/1958)!"
    formOK = false;
  }

  if (checkData("dataScadDocumento")) {
    id_errato = "dataScadDocumento";
    messaggio_errore = "Il campo data di scadenza del documento di identità  è obbligatorio e nella forma gg/mm/aaaa (es. 12/05/1958)!"
    formOK = false;
  }

  if (checkMandatory("rilascioDocumento")) {
    id_errato = "rilascioDocumento";
    messaggio_errore = "Il campo autorità che rilascia il documento è obbligatorio!"
    formOK = false;
  }

  if (checkMandatory("indirizzoResidenza")) {
    id_errato = "indirizzoResidenza";
    messaggio_errore = "Il campo indirizzo di residenza è obbligatorio!"
    formOK = false;
  }

  if (checkMandatory("civicoResidenza")) {
    id_errato = "civicoResidenza";
    messaggio_errore = "Il campo numero civico di residenza è obbligatorio!"
    formOK = false;
  }

  if (checkMandatory("cittaResidenza")) {
    id_errato = "cittaResidenza";
    messaggio_errore = "Il campo città di residenza è obbligatorio!"
    formOK = false;
  }
  
  if (checkCAP("capResidenza")) {
    id_errato = "capResidenza";
    messaggio_errore = "Il campo CAP è obbligatorio e deve essere composto da 5 numeri!"
    formOK = false;
  }
  
  if (!(checkMandatory("indirizzoDomicilio") && 
        checkMandatory("civicoDomicilio") &&
        checkMandatory("internoDomicilio") &&
        checkMandatory("scalaDomicilio") &&
        checkMandatory("edificioDomicilio") &&
        checkMandatory("lottoDomicilio") &&
        checkMandatory("cittaDomicilio") &&
        checkMandatory("capDomicilio") )) {
    
    if (checkMandatory("indirizzoDomicilio")) {
      id_errato = "indirizzoDomicilio";
      messaggio_errore = "Il campo indirizzo di domicilio è obbligatorio!"
      formOK = false;
    }
  
    if (checkMandatory("civicoDomicilio")) {
      id_errato = "civicoDomicilio";
      messaggio_errore = "Il campo numero civico di domicilio è obbligatorio!"
      formOK = false;
    }
  
    if (checkMandatory("cittaDomicilio")) {
      id_errato = "cittaDomicilio";
      messaggio_errore = "Il campo città di domicilio è obbligatorio!"
      formOK = false;
    }
    
    if (checkCAP("capDomicilio")) {
      id_errato = "capDomicilio";
      messaggio_errore = "Il campo CAP è obbligatorio e deve essere composto da 5 numeri!"
      formOK = false;
    }
  }
  
  if (checkMandatory("email")) {
    id_errato = "email";
    messaggio_errore = "Il campo email è obbligatorio!"
    formOK = false;
  }
  if (checkMandatory("professione")) {
    id_errato = "professione";
    messaggio_errore = "Il campo professione è obbligatorio!"
    formOK = false;
  }
  
  if (!formOK){
    alert(messaggio_errore);
    document.getElementById(id_errato).focus();
    
  }

  return formOK;
}

function checkVarIndirizzo() {
  var id_errato = "";
  var messaggio_errore = "";
  var formOK = true;  
  var messaggio = "";

  if (checkMandatory("telefono")) {
    id_errato = "telefono";
    messaggio_errore = "Il campo telefono è obbligatorio!"
    formOK = false;
  }

  if (checkMandatory("indirizzo")) {
    id_errato = "indirizzo";
    messaggio_errore = "Il campo indirizzo è obbligatorio!"
    formOK = false;
  }

  if (checkMandatory("numero")) {
    id_errato = "numero";
    messaggio_errore = "Il campo numero è obbligatorio!"
    formOK = false;
  }

  if (!formOK){
    alert(messaggio_errore);
    document.getElementById(id_errato).focus();
    
  }
  
  
  if (formOK){
  	messaggio = 
  		"tipo: " + document.getElementById("tipo").value +
  		" indirizzo: " + document.getElementById("indirizzo").value +
  		" n. " + document.getElementById("numero").value +
  		" scala: " + document.getElementById("scala").value +
  		" piano: " + document.getElementById("piano").value +
  		" interno: " + document.getElementById("interno").value +
  		" frazione: " + document.getElementById("frazione").value +
  		" telefono: " + document.getElementById("telefono").value +
  		" motivo: " + document.getElementById("motivo").value ;
  		document.getElementById("messaggio").value = messaggio; 
  }

  return formOK;
	
	
}

function mostraFamiglia(selezione) {
		element = document.getElementById (elenco_membri);
		
		if (selezione == 0){
			element.style.display = "none";
		} else {
			element.style.display = "";
		}
}