//check the validity of a user entered email
function validEmail(email) {
      invalidChars = " /:,;"

      if (email == "") {    // cannot be empty
        return false
      }
      for (i=0; i<invalidChars.length; i++) { // does it contain any invalid characters?
        badChar = invalidChars.charAt(i)
        if (email.indexOf(badChar,0) > -1) {
          return false
        }
      }
      atPos = email.indexOf("@",1)    // there must be one "@" symbol
      if (atPos == -1) {
        return false
      }
      if (email.indexOf("@",atPos+1) != -1) { // and only one "@" symbol
        return false
      }
      periodPos = email.indexOf(".",atPos)
      if (periodPos == -1) {          // and at least one "." after the "@"
        return false
      }
      if (periodPos+3 > email.length) {   // must be at least 2 characters after the "."
        return false
      }
      return true
}




function validPhoneNum(passedVal) {     // Is this a phone number?
  if (passedVal == "") {
    return false
  }
  for (i=0; i<passedVal.length; i++) {
    if (passedVal.charAt(i) < "0" && passedVal.charAt(i) != "("
        && passedVal.charAt(i) != "-" && passedVal.charAt(i) != ")"
      && passedVal.charAt(i) != " " && passedVal.charAt(i) != "."  ) {

      return false
    }
    if (passedVal.charAt(i) > "9") {
      return false
    }
  }
  if (i<7)   {
    return false
  }
  return true
}

//Validate data in a Download Form

function validateDownloadFormEntries(form)   {

  if(form.first_name.value == "" )   {
     alert("Please enter your First Name");
     selectErrorField(form.first_name, false);
     return false
     }


  if(form.last_name.value == "" )   {
     alert("Please enter your Last Name");
     selectErrorField(form.last_name, false);
     return false
     }

  if(form.company.value == "" )   {
     alert("Please enter your Company Name");
     selectErrorField(form.company, false);
     return false
     }

  if (!validPhoneNum(form.phone.value)) {
        alert("Please enter Phone number")
        selectErrorField(form.phone, false)
        return false
      }

  // check to see if the email's valid
      if (!validEmail(form.email.value)) {
        alert("Invalid email address")
        selectErrorField(form.email, false);
        return false
      }
if(!(form.time[0].checked || form.time[1].checked))   {
     alert("Please select Local Time or GMT Time format for the flight information you receive.");
     return false
     }



       return true

  }

//Validate the data entered for tracking a flight by airline name/code and flight number

function validateAirlineFlightNumberData(form, bAirlineCodeExists)
  {
    var AirlineSelectionBox = form.alnn
  var FlightNumber = form.fn.value
  var stdMsg = "Please fill in the following information:\n\n"
    var msg = ""
  var bError = false

  FlightNumber = trim(FlightNumber)

  form.fn.value = FlightNumber

  if(IsEmpty(FlightNumber)){
    msg += "Flight Number\n\n"
    selectErrorField(form.fn, bError)
    bError = true
  }
  else{
    if(checkCharacterValidity(FlightNumber) == false) {
      msg += "Enter valid characters for Flight Number\n\n"
      selectErrorField(form.fn, bError)
      bError = true

    }

  }

  if(bAirlineCodeExists){
    var AirlineCode = trim(form.alnc.value)
    if(IsDefaultSelection(AirlineSelectionBox) && IsEmpty(AirlineCode)){
        if(!isNum(FlightNumber) && !bError)
          return true;
        msg += "Airline Name or Airline Code"
        bError = true
    }
    else{
      if(IsDefaultSelection(AirlineSelectionBox)){
        if(checkCharacterValidity(AirlineCode) == false) {
          msg += "Enter valid characters for Airline Code\n\n"
          selectErrorField(form.alnc, bError)
          bError = true
        }
        else
          form.aln.value = AirlineCode
      }
      else
        form.aln.value = AirlineSelectionBox.options[AirlineSelectionBox.selectedIndex].value
    }

  }
  else{
    if(IsDefaultSelection(AirlineSelectionBox)){
        if(!isNum(FlightNumber) && !bError)
          return true;
        msg += "Airline Name"
        AirlineSelectionBox.options[0].selected = 1;
        AirlineSelectionBox.focus()
        bError = true
    }
    else
      form.aln.value = AirlineSelectionBox.options[AirlineSelectionBox.selectedIndex].value

  }
  if(bError){
    alert(stdMsg + msg)
    return false
  }
  return true
}


function validateDeptArrivalData(form){
  var ArrSelectionBox = form.aaptn
  var DepSelectionBox = form.daptn
  var TimeSelectionBox = form.time

  var dataMissingMsg = "Please fill in the following information:\n\n"
  msg = ""

  var bError = false
  var ArrAp, DepAp


  //Check if all the three parameters (depap,arrap, time) are entered.
  //If not, pop up alert messages
  var DepAptCode = trim(form.daptc.value)
  if(IsEmpty(DepAptCode)){

    if(IsDefaultSelection(DepSelectionBox)){
      msg += "Departure Airport Name or Code\n\n"

      bError = true
    }
    else
      DepAp = DepSelectionBox.options[DepSelectionBox.selectedIndex].value
  }
  else{
    //convert the departure and arrival airports in the text boxes to Upper Case
    DepAp = DepAptCode.toUpperCase()

    if(checkCharacterValidity(DepAp) == false){
      bError = true
      msg += "Enter valid characters for Departure Airport Code\n\n"
    }
  }

  var ArrAptCode = trim(form.aaptc.value)
  if(IsEmpty(ArrAptCode)){

    if(IsDefaultSelection(ArrSelectionBox)){
      msg += "Arrival Airport Name or Code\n\n"
      bError = true
    }
    else
      ArrAp = ArrSelectionBox.options[ArrSelectionBox.selectedIndex].value
  }
  else{
    //convert the departure and arrival airports in the text boxes to Upper Case
    ArrAp = ArrAptCode.toUpperCase()

    if(checkCharacterValidity(ArrAp) == false){
      bError = true
      msg += "Enter valid characters for Arrival Airport Code\n\n"
    }
  }

  if(IsDefaultSelection(TimeSelectionBox)){
    msg += "Time\n\n"
    bError = true
  }


  if(bError){
    alert(dataMissingMsg + msg)
    return false
  }
  else{
    if(DepAp == ArrAp){
      alert("Departure and Arrival Airports must be different!")
      return false
    }
    else{
      form.dep.value = DepAp
      form.arr.value = ArrAp
    }

  }
  return true

}

function validateAirTraffic(selectionBox){

  if(IsDefaultSelection(selectionBox)){
    alert("Please select an Airport Name")
    return false
  }
  else
    return true
}


function selectErrorField(item, bError){

  if(!bError){
    item.focus()
    item.select()
  }

}

function checkCharacterValidity(airportId){

    for(var i=0; i<airportId.length; i++){
    if(IsValidChar(airportId.charAt(i)) != true){
        return false;
    }
  }
  return true;
}


function trim(strText) {

    // this will get rid of leading spaces
    while (strText.substring(0,1) == ' ')
        strText = strText.substring(1, strText.length)

    // this will get rid of trailing spaces
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1)

   return strText;
}

function isNum(inputStr)
{
  for(var i=0; i < inputStr.length; i++)
    if(IsDigit(inputStr.charAt(i)) == false)
      return false;

  return true;

}


function IsEmpty(inputStr)
{
  if((inputStr == null) || (inputStr == ""))
    return true
  else
    return false

}

function IsDefaultSelection(selectionBox)
{
  if(selectionBox.options[selectionBox.selectedIndex].value == "-1")
    return true
  else
    return false
}

function IsDigit( Ch )
{
   if( (Ch >= '0') && (Ch <= '9') )
  return true;
   else
  return false;
}

function IsAlpha( Ch )

{
   if( (Ch >= 'a') && (Ch <= 'z') ) return true;
   else if( (Ch >= 'A') && (Ch <= 'Z') ) return true;
   else return false;
}

function IsValidChar( Ch )
{
  if(IsDigit(Ch) || IsAlpha(Ch))
    return true;
  else
    return false;
}

function callAlert(airport){
  if(airport == "Departure")
    alert('Please enter only one Departure Airport');
  if(airport == "Arrival")
    alert('Please enter only one Arrival Airport');
  return false;
}

function validateTailNumberData(form, bAirlineCodeExists)
{
  var FlightNumber = form.tnn.value
  var stdMsg = "Please fill in the following information:\n\n"
    var msg = ""
  var bError = false

  FlightNumber = trim(FlightNumber)

  form.tnn.value = FlightNumber

  if (IsEmpty(FlightNumber)) {
    FlightNumber = form.tnc.value
    if (IsEmpty(FlightNumber)) {
      msg += "Flight Number\n\n"
      selectErrorField(form.tnn, bError)
      bError = true
    }
  }

  if(bError){
    alert(stdMsg + msg)
    return false
  }
  return true
}
