var pilots         = 1;
var squadron       = new Object;
var pilotsPane     = new Object;
var requiredFields = [ 'pilot_name',    'pilot_callsign', 'pilot_email', 'pilot_city', 
                       'pilot_country', 'squadron',       'confirm'
                     ];
var hiddenFields   = [ 'newSquadRow', 'demo_sim_other', 'free_sim_other' ];

function addPilot()
{
  var firstPilot = document.getElementById("pilot1");
  var frame      = firstPilot.parentNode;
  var newPilot   = firstPilot.cloneNode(true);
  var button     = document.getElementById("newPilotButton");
  var caption    = newPilot.getElementsByTagName("LEGEND")[0];
  var fields     = [ newPilot.getElementsByTagName("INPUT"), 
                     newPilot.getElementsByTagName("SELECT")
                   ];
  var firstField = fields[0][0];

  pilots++;
  
  caption.innerHTML = caption.innerHTML.substr(0, caption.innerHTML.lastIndexOf("#") + 1) + pilots;
  
  for (var type = 0; type < fields.length; type++)
  {
    for (var idx = 0; idx < fields[type].length; idx++)
    {
      if (fields[type][idx].tagName == "INPUT")
        fields[type][idx].value = "";
      fields[type][idx].id      = fields[type][idx].id.substr(0, fields[type][idx].id.lastIndexOf("_") + 1) + pilots;
      fields[type][idx].name    = fields[type][idx].name.substr(0, fields[type][idx].name.lastIndexOf("_") + 1) + pilots;
      fields[type][idx].onfocus = function() { this.style.backgroundColor = "#606060" }
      fields[type][idx].onblur  = function() { this.style.backgroundColor = "Black"   }
    }
  }
  
  frame.insertBefore(newPilot, button);
  frame.scrollTop = frame.scrollHeight;
  firstField.focus();
}

function toggle_other_field(fieldID)
{
  var other_field_id = fieldID + "_other";
  var select_field   = document.getElementById(fieldID);
  
  if (fieldID == "squadron")
    var other_field = document.getElementById("newSquadRow");
  else
    var other_field = document.getElementById(other_field_id);

  other_field.style.visibility = (select_field.value == "other") ? "visible" : "hidden";
}
function toggleSubmitButton(obj)
{
  document.getElementById("send").disabled = (obj.checked) ? false : true;
}
function showinfo(fieldID)
{
  document.getElementById('infoContainer').innerHTML = info[fieldID];
}
function clearinfo()
{
  document.getElementById('infoContainer').innerHTML = "Hover your mouse over the fields to show information";
}
function check_form()
{
  for (var idx = 0; idx < requiredFields.length; idx++)
  {
    var field = document.getElementById(requiredFields[idx]);
    if (field.value.replace(/ /g, '').length == 0)
    {
      alert('You forgot to fill in a required field.');
      field.focus();
      field.select();
      return false;
    }
  }
  document.forms[0].submit();
}
function updatePrice()
{
  var boxes = document.getElementById('pilotsContainer').getElementsByTagName('input');
  var priceTag = document.getElementById('price');
  var buffet = { 'thu' : 0, 'fri' : 0, 'sat' : 0, 'sun' : 0 }
  var patch  = 0;
  var totalPrice = 0;

  for (var idx = 0; idx < boxes.length; idx++)
  {
    if (boxes[idx].className == 'chk')
    {
      if (boxes[idx].id.indexOf('pilot') >= 0)
      {
        if (boxes[idx].checked) patch = 10;
      } else if (boxes[idx].id.indexOf('lunch') >= 0 || boxes[idx].id.indexOf('dinner') >= 0)
      {
        if (boxes[idx].checked)
        {
          var day = boxes[idx].id.substr(boxes[idx].id.indexOf('_') + 1);
          buffet[day] = (buffet[day] > 0) ? 18 : 11;
        }
      }
    }
  }
  if (patch > 0)
  {
    totalPrice = patch;
    for (var day in buffet)
    {
      totalPrice += buffet[day];
    }
  }
  priceTag.innerHTML = "&euro; " + totalPrice;
}

window.onload = function()
{
  var hlFields = [ document.getElementsByTagName('INPUT'), 
                   document.getElementsByTagName('SELECT'),
                   document.getElementsByTagName('TEXTAREA')
                 ];
  pilotsPane = document.getElementById("pilotsContainer");
 
  var pilotsFields = pilotsPane.getElementsByTagName('INPUT');
  
  for (var idx = 0; idx < pilotsFields.length; idx++)
  {
    if (pilotsFields[idx].type == 'checkbox')
    {
      pilotsFields[idx].onclick = updatePrice;
    }
  }


  for (var id = 0; id < hiddenFields.length; id++)
  {
    var field = document.getElementById(hiddenFields[id]);
    field.style.visibility = "hidden";
  }
  for (var field = 0; field < hlFields.length; field++)
  {
    var fields = hlFields[field];

    for (var idx = 0; idx < fields.length; idx++)
    {
      fields[idx].onfocus = function() { this.style.backgroundColor = "#606060"; showinfo(this.id) }
      fields[idx].onblur  = function() { this.style.backgroundColor = "Black"; clearinfo()   }
      fields[idx].onmouseover = function() { showinfo(this.id) }
      fields[idx].onmouseout  = function() { clearinfo() }
    }
  }
  clearinfo();
}



