// JavaScript Document



//<![CDATA[
function inizializza() {
a = document.getElementsByTagName('A');
for(i=0;a[i];i++) if(a[i].className.indexOf('blank') != -1) {
  a[i].title += " [new window]";
  a[i].onclick = function () {window.open(this.href, '_blank');return false;};
  a[i].onkeypress = function (e) {
    k = (e) ? e.keyCode : window.event.keyCode;
    if(k==13) {
      window.open(this.href, '_blank');
      return false;
    }
  }
}
}
window.onload = function() {inizializza();}

function checkVat(){
	if(document.getElementById('cf').value=='' && document.getElementById('iva').value==''){
		alert('Compilare il campo Codice Fiscale o Partita Iva per la fatturazione');
		document.getElementById(campo).focus();
		return false;
	}
	return true;
}

function check(campo, tipo, label) {
  switch(tipo) {
    case "txt":
      if(document.getElementById(campo).value == '') {
        alert('Manca il campo ' + label);
        document.getElementById(campo).focus();
        return false;
      }
    break;
	case "psw":
      if(document.getElementById(campo).value.length < 6) {
        alert('La password deve essere lunga almeno 6 caratteri');
        document.getElementById(campo).focus();
        return false;
      }
    break;
    case "chk":
      if(document.getElementById(campo).checked == '') {
        alert('Per potersi registrare in Shoolit è necessario il consenso al trattamento dei dati personali');
        document.getElementById(campo).focus();
        return false;
      }
    break;
	case "chk2":
      if(document.getElementById(campo).checked == '') {
        alert('Per poter usufruire del servizio è necessario accettare le condizioni di vendita, d\'uso e di recesso.');
        document.getElementById(campo).focus();
        return false;
      }
    break;
	case "select":
      if(document.getElementById(campo).value == false) {
        alert('Selezionare ' + label);
        return false;
      }
    break;
  }
  return true;
}

function checkEmail(campo) {
  var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if(!filter.test(document.getElementById(campo).value)) {
    alert('Non e stata specificata un\'email valida');
    document.getElementById(campo).focus();
    return false;
  }
  return true;
}

function checkUguali(campo1, campo2) {
  if(document.getElementById(campo1).value != document.getElementById(campo2).value) {
    alert('Le password inserite non coincidono');
    document.getElementById(campo1).focus();
    return false;
  }
  return true;
}

function checkCF(campo) {
	
	var cf = document.register.cf.value;
	var cf_length = document.register.cf.value.length;
	var alert_mex = "Il Codice Fiscale inserito non è corretto";
	
	if(cf_length != 16) {
    alert(alert_mex);
    return false;
 	}
	
	caratteri_validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		
    for(i = 0; i < 16; i++){
        if(caratteri_validi.indexOf(cf.charAt(i)) == -1 ) {
        alert(alert_mex);
		return false;
		}
    }
	
	set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
    setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
    s = 0;
	
    for( i = 1; i <= 13; i += 2 )
        s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
    for( i = 0; i <= 14; i += 2 )
        s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
    if( s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0) ) {
        alert(alert_mex);
		return false;
	}
  return true;
}

