//
// Restiruisce il browser
//
function browserWeb() {
  if (document.layers) //Netscape 4.x 
    return "NS4"
  else if (document.all)  //explorer
    return "IE";
  else if (document.getElementById) // Netscape 6.x
    return "NS6";

  return NULL;
}

//
//
//
function popup(url, name, width, height, p) {
  newwindow = window.open(url, name, 'width='+width+', height='+height+', toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes');
  newwindow.focus();
}

function openPopup(url, name, width, height) {
  newWindow = window.open(url, name, 'width=' + width + ',height=' + height + ',status=no toolbar=no, location=no, directories=no, menubar=no, scrollbars=no, resizable=yes');
  return false;
}


//
// controlla dimensioni video PC e ridimensiona per apertura popup
//
function ShowReg(url, resX, resY, delta_x, delta_y, width_adv) {
  var altezza = window.screen.height; // altezza schermo
  var larghezza = window.screen.width; //larghezza schermo

  var widthPopup = resX + delta_x + width_adv;
  var heightPopup = resY +  delta_y;
  var setRatio = 1;

  var tipoBrowser = browserWeb();

  //ingombri browser
  var hPadd = 90;
  //var hPadd = 85;
  var wPadd = 50;
  //var wPadd = 36;
  altezza -= hPadd;
  larghezza -= wPadd;


  try {
    var codticket = document.getElementById('codticket');
    url +="&codticket="+codticket.value;
  } catch (e) { }
  try {
    var user = document.getElementById('user');
    url +="&user="+user.value;
  } catch (e) { }
  try {
    var pwd = document.getElementById('pwd');
    url +="&pwd="+pwd.value;
  } catch (e) { }
  try {
    var ckdisclaimer = document.getElementById('checkDisclaimer');
    if (ckdisclaimer.checked) {
      url +="&ckdisclaimer=1";
    }
  }  catch(e) { }

  // controllo se ridimensionare popup
  if (altezza < heightPopup || larghezza < widthPopup) {
    //maxWidth = larghezza*0.85;
    maxWidth = larghezza*0.90;
    //maxHeight = altezza*0.80;
    maxHeight = altezza*0.85;
    widthRatio = (maxWidth - delta_x - width_adv) / resX;
    heightRatio = (maxHeight - delta_y) / resY;
    setRatio = Math.min(widthRatio, heightRatio);
  }
  widthPopup = Math.floor(setRatio * resX) + delta_x + width_adv;
  heightPopup = Math.floor(setRatio * resY) + delta_y;
  objX = Math.floor(resX * setRatio) +  delta_x;
  objY = Math.floor(resY  * setRatio) +  delta_y;

  if (tipoBrowser == "IE") {
    widthPopup += 20;
    heightPopup += 15;
  } else {
    widthPopup += 10;
    heightPopup += 10;
  }

  url += "&widthPopup="+widthPopup+"&heightPopup="+heightPopup+"&objX="+objX+"&objY="+objY;
  popup(url, 'registrazione', widthPopup, heightPopup);
}


//
// Mostra messaggio se java NON attivo
//
function javaNotEnabled() {
  var box = document.getElementById('alertJava');
  (navigator.javaEnabled()) ? box.style.display = 'none': box.style.display = 'block';
}

String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

//
// controlla validita codice fiscale
//
function controllaCF(CodiceFiscale) {

  if (CodiceFiscale == "" || CodiceFiscale.length != 16)
    return false;

  // Definisco un pattern per il confronto
  var pattern = /^[a-zA-Z]{6}[0-9]{2}[a-zA-Z][0-9]{2}[a-zA-Z][0-9]{3}[a-zA-Z]$/;

  // utilizzo il metodo search per verificare che il valore inserito nel campo
  // di input rispetti la stringa di verifica (pattern)
  if (CodiceFiscale.value.search(pattern) == -1) {
    return false;
  }

  return true;
}

//
// Controlla form iscrizione 
//
function checkFormIscr(formIscr) {

  var err = "";
  var alertBox = document.getElementById('alertFormField');

  var espressione = /^[_a-zA-Z0-9+-]+(\.[_a-zA-Z0-9+-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$/;
 
  var mail = document.getElementById('email').value;
  var mail2 = document.getElementById('email2').value;
  var nome = document.getElementById('name').value;
  var cognome = document.getElementById('lastname').value;
  var privacy = document.getElementById('privacy').checked;

  try {
    var telefono = document.getElementById('telefono').value;
    telefono = telefono.trim();

    if (telefono == "") {
      err += "<li>Campo Telefono obbligatorio</li>";
    }
  } catch (e) {}

  try {
    var d = document.getElementById('dd').selectedIndex;
    var m = document.getElementById('mm').selectedIndex;
    var a = document.getElementById('aa').selectedIndex;
    if (d == 0 || m == 0 || a == 0) {
      err += "<li>Inserire data di nascita valida</li>";
    }
  } catch (e) {}

  try {
    var codfisc = document.getElementById('cfisc').value;
    codfisc = codfisc.trim();

    if (controllaCF(codfisc) == false ) {
      err += "<li>Codice fiscale obbligatorio</li>";
    }
  } catch (e) {}


  if (nome == "") {
    err += "<li>Campo Nome obbligatorio</li>";
  }

    if (cognome == "") {
    err += "<li>Campo Cognome obbligatorio</li>";
  } 

  if (!espressione.test(mail)) {
    err += "<li>La mail inserita non e' valida!</li>";
  }

  if (mail != mail2) {
     err += "<li>Le mail inserite non coincidono!</li>";
  }

  if (privacy == "") {
    err += "<li>Attenzione, &egrave; necessario accettare la Privacy</li>";
  }

  if (err != "") {
    alert("Attenzione: compilare con cura tutti i campi contrassegnati con *");
    alertBox.innerHTML = '<ul class="redL">'+err+'</ul>';
    //alertBox.style.display = (alertBox.style.display == "none") ? "block" : "none";
    alertBox.style.display = "block";
    return false;
  }
  return true;
}


