/* JavaScript file for all script functions in the booking page */

/* Created: 06/08/06 */
/*          Daniel Del Conte */


/* Removes spaces from the beginning and the end of a string */
function Trim(s)
{
  return s.replace(/^\s+|\s+$/g,'');
}

/* Checks, if a date is valid */
function DateValid(d,m,y)
{
  var ms=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
  if (y<1) return false;
  if (m<1||m>12) return false;
  var maxday=ms[m-1];
  if (((y%4==0&&!(y%100==0))||y%400==0)&&m==2) maxday++;
  if (d<1||d>maxday) return false;
  return true;
}

/* Changes the background of the current focused form field */
function FieldFocus(Field) { SetBackground(Field,'#FFFFFF'); }

/* Changes the background of the current form field loosing the focus */
function FieldBlur(Field) { SetBackground(Field,'#FFFF7F'); }

/* Retrieves the name of the applicant */
function GetName()
{
  var name=Trim(document.forms.BookForm.elements.na.value);
  return (name=='')?null:name;
}

/* Retrieves the email adress of the potential booker */
function GetEMail()
{
  var email=Trim(document.forms.BookForm.elements.krz.value);
  return (email.search(/^[A-Z0-9._%-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$/i)==-1)?null:email;
}

/* Retrieves the booking reference number of the potential future room occupiant */
function GetBookingRef()
{
  var number=Trim(document.forms.BookForm.elements.br.value);
  return (number=='')?null:number;
}

/* Retrieves the phone number of the maybe Chosen One */
function GetPhoneNumber()
{
  var number=Trim(document.forms.BookForm.elements.ph.value);
  if (number=='') return number;
  return (number.search(/[^\d\s]/)==-1)?number:null;
}

/* Retrieves the probable arrival date of the booker */
function GetArrivalDate()
{
  var day=document.forms.BookForm.elements.add.options[document.forms.BookForm.elements.add.selectedIndex].value;
  var month=document.forms.BookForm.elements.adm.options[document.forms.BookForm.elements.adm.selectedIndex].value;
  var year=document.forms.BookForm.elements.ady.options[document.forms.BookForm.elements.ady.selectedIndex].value;
  if (!DateValid(day,month,year)) return null;
  return new Date(year,month,day);
}

/* Retrieves the number of persons the applicant consists of */
function GetPersonCount()
{
  return Number(Trim(document.forms.BookForm.elements.pp.value));
}

/* Retrieves, if the booker wants a pick-up */
// Pick up is disabled
function GetPickup()
{
	//if (document.forms.BookForm.elements.pk[0].checked) return 1;
  //if (document.forms.BookForm.elements.pk[1].checked) return 2;
  return 0;
}

/* Retrieves the arrival point the pick-up service must show up */
function GetArrivalPoint()
{
  var index=document.forms.BookForm.elements.pf.selectedIndex;
  if (index==0) return null;
  return document.forms.BookForm.elements.pf.options[index].text;
}

/* Retrieves further information for the pick-up service to know */
function GetAirline()
{
  var index=document.forms.BookForm.elements.al.selectedIndex;
  if (index==0) return null;
  return document.forms.BookForm.elements.al.options[index].text;
}

/* Retrieves the time the approved traveller will probably appear on the selected arrival point */
function GetArrivalTime()
{
  var hour=document.forms.BookForm.elements.ath.options[document.forms.BookForm.elements.ath.selectedIndex].value;
  var minutes=document.forms.BookForm.elements.atm.options[document.forms.BookForm.elements.atm.selectedIndex].value;
  if (hour<0||hour>23) return null;
  if (minutes<0||minutes>59) return null;
  return new Date(1970,1,1,hour,minutes,0);
}

/* Sets an error message in the appropriate row next to the erroneous field */
function SetError(id,errormsg)
{
  var obj=GetObject('error'+id);
  if (!obj) return;
  var text=null;
  
  /* If there is a text node like in standards comformant browsers we just change the content of the text node */
  /* otherwise we create one */
  if (dom) {
    text=obj.firstChild;
    if (!text) {
      text=createTextNode("NewNode");
      obj.appendChild(text);
    } else text.nodeValue=errormsg;
  } else if (ie4) {
    obj.innerText=errormsg;
  } else alert(errormsg);
}

/* Clears all error messages */
function ClearErrors()
{
  var ids=new Array('na','em','br','ad','at','pf','al','pp','ph');
  var i;
  for (i=0;i<ids.length;i++) SetError(ids[i],'');
}

/* Focusses one of the form fields */
function Focus(field)
{
  var obj=eval('document.forms.BookForm.elements.'+field);
  if (obj) obj.focus();
}

/* Check Data Wrapper to disable the submit button to prevent double clicks leading to doubly submission */
function CheckDataWrapper()
{
  var obj=GetObject('sb');
  obj.disabled=true;
  var result=CheckData();
  if (!result) obj.disabled=false;
  return result;
}

/* Checks the input and displays error messages if necessary */
function CheckData()
{
  /* Any previous displayed error messages should disappear now */
  ClearErrors();

  /* Name - must be entered */
  var string=GetName();
  if (!string) {
    SetError('na',GetErrorText('na'));
    document.forms.BookForm.elements.na.focus();
    return false;
  } else document.forms.BookForm.elements.na.value=string;
  
  string=Trim(document.forms.BookForm.elements.em.value);
  if (string != "") return false;

  /* Email address - must be entered */
  string=GetEMail();
  if (!string) {
    SetError('krz',GetErrorText('krz'));
    document.forms.BookForm.elements.krz.focus();
    return false;
  } else document.forms.BookForm.elements.krz.value=string;

  /* If the user doesn't want a pickup, all pick-up fields aren't evaluated */
  var pickup=GetPickup();
  if (pickup==1) {

    /* Booking reference number - must be entered */
    string=GetBookingRef();
    if (!string) {
      SetError('br',GetErrorText('br'));
      document.forms.BookForm.elements.br.focus();
      return false;
    } else document.forms.BookForm.elements.br.value=string;

    /* Arrival date - must be selected */
    var date=GetArrivalDate();
    if (!date) {
      SetError('ad',GetErrorText('ad'));
      document.forms.BookForm.elements.add.focus();
      return false;
    }
    
    /* Arrival time - must be selected */
    date=GetArrivalTime();
    if (!date) {
      SetError('at',GetErrorText('at'));
      document.forms.BookForm.elements.ath.focus();
      return false;
    }

    /* Arrival point - must be selected */
    string=GetArrivalPoint();
    if (!string) {
      SetError('pf',GetErrorText('pf'));
      document.forms.BookForm.elements.pf.focus();
      return false;
    }

    /* Airline must only be entered if "Domestic airport" was selected above */
    if (string=="Domestic Airport") {
      var string2=GetAirline();
      if (!string2) {
        SetError('al',GetErrorText('al'));
        document.forms.BookForm.elements.al.focus();
        return false;
      }
    }

    /* Person number - must be entered */
    var number=GetPersonCount();
    if (number.toString()=='NaN'||number<=0) {
      SetError('pp',GetErrorText('pp'));
      document.forms.BookForm.elements.pp.focus();
      return false;
    } else document.forms.BookForm.elements.pp.value=number;

    /* Phone number - must be entered */
    string=GetPhoneNumber();
    if (string==null) {
      SetError('ph',GetErrorText('ph'));
      document.forms.BookForm.elements.ph.focus();
      return false;
    } else document.forms.BookForm.elements.ph.value=string;
  }

  /* The special request text won't be checked, it's free text and can be entered at will */
  
  /* All filled out correctly */
  return true;
}

/* Updates all enable states in the form fields based on the currently entered data */
function UpdateFormFields()
{
  /* Pick-up selection determines availability of all following pick-up controls */
  /*var pickup=GetPickup()==1;
  var domestic=(GetArrivalPoint()=="Domestic Airport");
  document.forms.BookForm.elements.br.disabled=!pickup;
  document.forms.BookForm.elements.add.disabled=!pickup;
  document.forms.BookForm.elements.adm.disabled=!pickup;
  document.forms.BookForm.elements.ady.disabled=!pickup;
  document.forms.BookForm.elements.ath.disabled=!pickup;
  document.forms.BookForm.elements.atm.disabled=!pickup;
  document.forms.BookForm.elements.pf.disabled=!pickup;
  document.forms.BookForm.elements.al.disabled=!(pickup&&domestic);
  document.forms.BookForm.elements.pp.disabled=!pickup;
  document.forms.BookForm.elements.ph.disabled=!pickup;
  SetColor("bookingref",pickup?'#000000':'#666666');
  SetColor("arrivaldate",pickup?'#000000':'#666666');
  SetColor("arrivaltime",pickup?'#000000':'#666666');
  SetColor("people",pickup?'#000000':'#666666');
  SetColor("from",pickup?'#000000':'#666666');
  SetColor("airline",pickup&&domestic?'#000000':'#666666');
  SetColor("phonenumber",pickup?'#000000':'#666666');*/
}

/* Shows explanation popup */
function ShowExplanation(e)
{
  var x=GetX('explink');
  var y=GetY('explink');
  var height=GetHeight('explink');
  MoveTo('explanation',x,y+height);
  Show('explanation',true);
  StopEvent(e);

  /* This makes the browser not follow the link associated with this function */
  return false;
}

/* Hides the explanation popup */
function HideExplanation()
{
  Show('explanation',false);
  HideAllMenus();			/* This is necessary for the menus to vanish, too */
}

/* Initializes the booking page */
function InitBooking()
{
  if (document.captureEvents) document.captureEvents(Event.MOUSEDOWN);
  document.onmousedown=HideExplanation;
  
  UpdateFormFields();
}
