// Popup function
function popup(url,name,params) {
 var sizeX = 0;
 var sizeY = 0;
 var winX = screen.availWidth;
 var winY = screen.availHeight;
 var tmpArray = params.split(',');
 for (var i = 0; i < tmpArray.length; i++) {
  if (tmpArray[i].indexOf('width') != -1) {
   var tmpArray2 = tmpArray[i].split('=');
   sizeX = tmpArray2[1];
  }
  if (tmpArray[i].indexOf('height') != -1) {
   var tmpArray2 = tmpArray[i].split('=');
   sizeY = tmpArray2[1];
  }
 }
 var centerX = Math.round(winX / 2 - sizeX / 2);
 var centerY = Math.round(winY / 2 - sizeY / 2);
 params += ',left=' + centerX + ',top=' + centerY;
 var pWindow = open(url,name,params).focus();
}

// Contact Form Submit
var alertMe = "";
function contactCheck() {
    var alertMe = "";
    if (document.getElementById("contactFormFirstName").value == "") {
        alertMe += "Il cognome è obbligatorio.\n";
    }
    if (document.getElementById("contactFormSurname").value == "") {
        alertMe += "Il nome è obbligatorio.\n";
    }
    alertMe += checkSubject(document.getElementById("contactFormSubject").options[document.getElementById("contactFormSubject").selectedIndex].value);
    alertMe += filterEmailField(document.getElementById("contactFormEmail").value);
    if (alertMe != "") {
        alert (alertMe);
        return false;
    } else {
        return true;
    }
}

function modificationFlightCheck() {
    var alertMe = "";
    if (document.getElementById("contactFormFirstName").value == "") {
        alertMe += "Il cognome è obbligatorio.\n";
    }
    if (document.getElementById("contactFormSurname").value == "") {
        alertMe += "Il nome è obbligatorio.\n";
    }
    if (document.getElementById("numeroDeReservation").value == "") {
        alertMe += "Il numero di prenotazione è obbligatorio.\n";
    }
    if (document.getElementById("dateDuVol").value == "") {
        alertMe += "Il data del volo è obbligatorio.\n";
    }
    if (document.getElementById("aeroportDepart").value == "") {
        alertMe += "Il aeroporto di partenza è obbligatorio.\n";
    }
    if (document.getElementById("aeroportArrivee").value == "") {
        alertMe += "Il aeroporto di arrivo è obbligatorio.\n";
    }
    if (document.getElementById("telephone").value == "") {
        alertMe += "Il telefono è obbligatorio.\n";
    }
    if (document.getElementById("contactFormEmail").value == "") {
        alertMe += "Il e-mail è obbligatorio.\n";
    }
    if (alertMe != "") {
        alert (alertMe);
        return false;
    } else {
        return true;
    }
}

function checkSubject(subject) {
    if (subject== "---") {
        alertMe = "Si prega di scegliere un tema.\n";
        return alertMe;
    } else {
        alertMe = "";
        return alertMe;
    }
}

function filterEmailField(email) {
    var alertMe = "";
    if (email == '') {
        alertMe += "L'indirizzo e-mail è obbligatorio.";
        return alertMe;
    }

    var invalid = "\:\,\;\#$\%\&\(\)\+\=\/";
    for(var i=0; i<invalid.length; i++) {
        var badChar = invalid.charAt(i);
        if (email.indexOf(badChar,0) != -1) {
            alertMe += "L'indirizzo e-mail che avete battuto contiene uno o molti caratteri non validi.";
            return alertMe;
        }
    }
 
    var atSignPos = email.indexOf('@',1);
        if (atSignPos == -1) {
            alertMe += "Il segno @ manca nell'indirizzo e-mail che avete battuto.";
            return alertMe;
        } else if (email.indexOf('@',atSignPos+1) != -1) {
            alertMe += "L'indirizzo e-mail che avete battuto contiene tanti segni @.";
            return alertMe;
        }    
    
    var dotPos = email.indexOf('.',atSignPos+2);
  
    if (dotPos == -1) {
        alertMe += "L'estensione manca nell'indirizzo e-mail che avete battuto.";
        return alertMe;
    } else if (dotPos+3 > email.length) {
        alertMe += "L'estensione del vostro indirizzo e-mail sembra essere invalida.";
        return alertMe;
    }

    return alertMe;
}

function getURLParam(strParamName) {
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 ) {
    var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ) {
      if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ) {
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return strReturn;
}
