function isNumeric(sText){
  var ValidChars = "0123456789. ";
  var IsNumber=true;
  var Char;
  for (i = 0; i < sText.length && IsNumber == true; i++) {
    Char = sText.charAt(i);
    if (ValidChars.indexOf(Char) == -1) {
      IsNumber = false;
    }
  }
  return IsNumber;
}

function parentHideBlock(divName){
  tempDiv = parent.document.getElementById(divName);
  if (!tempDiv) {
    return;
  }
  if (tempDiv.style.display=="block"){
       tempDiv.style.display="none";
  }
}

function parentShowBlock(divName){
  tempDiv = parent.document.getElementById(divName);
  if (!tempDiv) {
    return;
  }
  if (tempDiv.style.display=="none"){
    tempDiv.style.display="block";
   }
}

function hideBlock(divName){
  tempDiv = document.getElementById(divName);
  if (!tempDiv) {
    return;
  }
  if (tempDiv.style.display=="block"){
       tempDiv.style.display="none";
  }
}

function showBlock(divName){
  tempDiv = document.getElementById(divName);
  if (!tempDiv) {
    return;
  }
  if (tempDiv.style.display=="none"){
    tempDiv.style.display="block";
   }
}

function switchBlock(divName){
  tempDiv = document.getElementById(divName);

  if (!tempDiv) {
    return;
  }
  if (tempDiv.style.display=="none"){
    tempDiv.style.display="block";
  }
  else {
    tempDiv.style.display="none";
  }
}

function hideInline(spanName){
  tempSpan = document.getElementById(spanName);
  if (!tempSpan) {
    return;
  }
  if (tempSpan.style.display=="inline"){
       tempSpan.style.display="none";
  }
}

function showInline(spanName){
  tempSpan = document.getElementById(spanName);
  if (!tempSpan) {
    return;
  }
  if (tempSpan.style.display=="none"){
    tempSpan.style.display="inline";
   }
}

function controllo_partita_iva(partita_iva){
  var valore = partita_iva.split("");

  if(valore.length == 11) {

    var tabella_cifra_contributo_dispari = new Array()
    tabella_cifra_contributo_dispari[0] = 0;
    tabella_cifra_contributo_dispari[1] = 2;
    tabella_cifra_contributo_dispari[2] = 4;
    tabella_cifra_contributo_dispari[3] = 6;
    tabella_cifra_contributo_dispari[4] = 8;
    tabella_cifra_contributo_dispari[5] = 1;
    tabella_cifra_contributo_dispari[6] = 3;
    tabella_cifra_contributo_dispari[7] = 5;
    tabella_cifra_contributo_dispari[8] = 7;
    tabella_cifra_contributo_dispari[9] = 9;

    var contributo_cifre_pari = 0;
    contributo_cifre_pari = parseInt(valore[0]);
    contributo_cifre_pari += parseInt(valore[2]);
    contributo_cifre_pari += parseInt(valore[4]);
    contributo_cifre_pari += parseInt(valore[6]);
    contributo_cifre_pari += parseInt(valore[8]);
    contributo_cifre_pari += parseInt(valore[10]);

    var contributo_cifre_dispari = 0;
    contributo_cifre_dispari = parseInt(tabella_cifra_contributo_dispari[valore[1]]);
    contributo_cifre_dispari += parseInt(tabella_cifra_contributo_dispari[valore[3]]);
    contributo_cifre_dispari += parseInt(tabella_cifra_contributo_dispari[valore[5]]);
    contributo_cifre_dispari +=  parseInt(tabella_cifra_contributo_dispari[valore[7]]);
    contributo_cifre_dispari +=  parseInt(tabella_cifra_contributo_dispari[valore[9]]);

    var contributo_totale = 0;
    contributo_totale = contributo_cifre_pari + contributo_cifre_dispari;
    if ((contributo_totale % 10) == 0) {
      return 1;
    }
    else {
      return 0;
    }
  }
  else {
    return 0;
  }
}

function controllo_codice_fiscale(codice_fiscale){
  codice_fiscale = codice_fiscale.toUpperCase();
  var valore = codice_fiscale.split("");
  if(valore.length == 16) {
    var alfabeto = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    var codice     = new Array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25);
    var contributo = new Array(1,0,5,7,9,13,15,17,19,21,2,4,18,20,11,3,6,8,12,14,16,10,22,25,24,23);
    var numero = 0;
    var somma = 0;
    for(i = 0; i < 15; i++) {
      if( isNaN(parseInt(valore[i])) != true ) {
        numero = valore[i];
      }
      else {
        numero = alfabeto.indexOf(valore[i]);
      }
      if((i + 1) % 2 == 0) {
        somma += codice[numero];
      }
      else {
        somma += contributo[numero];
      }
    }
    if (alfabeto.charAt((somma % 26)) == valore[15])
      return 1;
    else {
      return 0;
    }
  }
  else {
    return 0;
  }

}

function ordina(campo, ordinamento){
  location.href='?ordina_per_campo='+campo+'&ordina_ordinamento='+ordinamento;
}

function checkMail(address){
  Filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
  if (Filter.test(address)){
    return false;
  }
  else{
    return true;
  }
}

function checkChars(string){
  Filter = /^([a-zA-Z0-9_])+$/;
  if (Filter.test(string)){
    return false;
  }
  else{
    return true;
  }
}

function checkDecimal(string){
  Filter = /^([0-9])+\.([0-9])+$/;
  if (Filter.test(string)){
    return false;
  }
  else{
    return true;
  }
}

// Need Prototype
// Need ajaxErrorTip (need wz_tooltip and tip_balloon)
// Default element_id2="", min_length=5, max_length=8
function formErrorCheck(element_id, error_type, element_id2, min_length, max_length){
  var error_color = '#FFF2F1';
  var error = false;

  switch (error_type) {
    case 'empty':
      if($F(element_id)=='') error=true;
    break;

    case 'wrong_mail':
      if(checkMail($F(element_id))) error=true;
    break;

    case 'wrong_chars':
      if(checkChars($F(element_id))) error=true;
    break;

    case 'equal':
      if($F(element_id)!=$F(element_id2)) error=true;
    break;

    case 'min_chars':
      if($F(element_id).length < min_length) error=true;
    break;

    case 'max_chars':
      if($F(element_id).length > max_length) error=true;
    break;

    case 'decimal':
      if(checkDecimal($F(element_id))) error=true;
    break;

    default:
      error=false;
  }

  if (error){
    $(element_id).style.backgroundColor=error_color;
    $(element_id).onmouseover=function() {ajaxErrorTip(error_type)};
    return 1;
  }
  else{
    $(element_id).style.backgroundColor='';
    $(element_id).onmouseover=function() {};
    return 0;
  }
}

