
// Cooky functions:

var iDays = 365 ;
var dateExp = new Date( );

dateExp.setTime( dateExp.getTime( ) + ( iDays * 24 * 60 * 60 * 1000 ) );

// Retrieve a form field.
function getFormCookie ( sForm, sField ) {
  var s = sForm + "." + sField ;
  var sValue = getCookie( s );

  if ( sValue != null ) {
    document.forms[ sForm ].elements[ sField ].value = sValue ;
  }
}

// Retrieve a cookie.
function getCookie ( sName ) {
  var sValue = null ;
  var sKey = sName + "=" ;
  var iMax = document.cookie.length ;
  var i = 0 ;

  // Find the cookie.
  while ( i < iMax ) {
    var j = i + sKey.length ;

    if ( document.cookie.substring( i, j ) == sKey ) {
      var k = document.cookie.indexOf( ";", j );

      if ( k == -1 ) {
        k = iMax ;
      }

      sValue = unescape( document.cookie.substring( j, k ) );
      break ;
    }

    i = document.cookie.indexOf( " ", i ) + 1 ;
    if ( i == 0 ) {
      break ;
    }
  }

  return sValue ;
}

// Save a form field.
function setFormCookie ( sForm, sField ) {
  var s = sForm + "." + sField ;

  setCookie( s, document.forms[ sForm ].elements[ sField ].value, dateExp );
}

// Set a cookie to a new value.
// Format: setCookie( sName, sValue
//                    <, dataExpiry <, sPath
//                    <, sDomain <, bSecure >>>> )
function setCookie ( sName, sValue ) {
  var argv = setCookie.arguments ;
  var argc = argv.length ;
  var dateExpires = ( argc > 2 ) ? argv[ 2 ] : null ;
  var sPath = ( argc > 3 ) ? argv[ 3 ] : null ;
  var sDomain = ( argc > 4 ) ? argv[ 4 ] : null ;
  var bSecure = ( argc > 5 ) ? argv[ 5 ] : null ;

  document.cookie = sName + "=" + escape( sValue ) +
    ( (dateExpires == null) ?
      "" : ( "; expires=" + dateExpires.toGMTString( ) ) ) +
    ( (sPath == null) ? "" : ( "; path=" + sPath ) ) +
    ( (sDomain == null) ? "" : ( "; domain=" + sDomain ) ) +
    ( (bSecure == true) ? "; secure" : "" );
}

// Delete a form cookie.
function deleteFormCookie ( sForm, sField ) {
  var s = sForm + "." + sField ;

  deleteCookie( s );
}

// Delete a cookie by making it expire.
function deleteCookie ( sName ) {
  var sValue = getCookie( sName );
  var date = new Date( );

  date.setTime( date.getTime( ) - 1 );
  setCookie( sName, sValue, date );
}

// Calendar functions:

function chooseDate ( iYear, sMonth, iDay, iField ) {

  // Set the date in the appropriate field.
  opener.document.Booking.elements[ iField ].value =
    iDay + " " + sMonth + " " + iYear ;

  // Dismiss the calendar window.
  close( );
}

function leapYear ( iYear ) {
  var bLeapYear = false ;

  if ( ( iYear % 4 ) == 0 ) {
    bLeapYear = true ;
  }

  return bLeapYear ;
}

function displayCalendar1 ( iYear, iMonth, iField ) {
  displayCalendar( document.w, iYear, iMonth, iField );
}

function displayCalendar2 ( iYear, iMonth, iField ) {
  displayCalendar( this, iYear, iMonth, iField );
}

function displayCalendar ( w, iYear, iMonth, iField ) {
  var sDay = new Array( );   // Day names.
  var iDays = new Array( );  // Days in each month.
  var sMonth = new Array( ); // Month names.

  // Determine the previous and next months.
  var iNextYear = iYear ;
  var iPreviousYear = iYear ;
  var iNextMonth = iMonth + 1 ;
  var iPreviousMonth = iMonth - 1 ;

  if ( iPreviousMonth < 0 ) {
    iPreviousMonth = 11 ;
    iPreviousYear -- ;
  }

  if ( iNextMonth > 11 ) {
    iNextMonth = 0 ;
    iNextYear ++ ;
  }

  // Initialise data (language-independent).
  iDays[ 0 ] = 31 ;  // January
  iDays[ 1 ] = 28 ;  // February
  iDays[ 2 ] = 31 ;  // March
  iDays[ 3 ] = 30 ;  // April
  iDays[ 4 ] = 31 ;  // May
  iDays[ 5 ] = 30 ;  // June
  iDays[ 6 ] = 31 ;  // July
  iDays[ 7 ] = 31 ;  // August
  iDays[ 8 ] = 30 ;  // September
  iDays[ 9 ] = 31 ;  // October
  iDays[ 10 ] = 30 ; // November
  iDays[ 11 ] = 31 ; // December

  if ( leapYear( iYear ) ) {
    iDays[ 1 ] = 29 ;
  }

  // Initialise data (English).
  sDay[ 0 ] = "Sunday" ;
  sDay[ 1 ] = "Monday" ;
  sDay[ 2 ] = "Tuesday" ;
  sDay[ 3 ] = "Wednesday" ;
  sDay[ 4 ] = "Thursday" ;
  sDay[ 5 ] = "Friday" ;
  sDay[ 6 ] = "Saturday" ;

  sMonth[ 0 ] = "January" ;
  sMonth[ 1 ] = "February" ;
  sMonth[ 2 ] = "March" ;
  sMonth[ 3 ] = "April" ;
  sMonth[ 4 ] = "May" ;
  sMonth[ 5 ] = "June" ;
  sMonth[ 6 ] = "July" ;
  sMonth[ 7 ] = "August" ;
  sMonth[ 8 ] = "September" ;
  sMonth[ 9 ] = "October" ;
  sMonth[ 10 ] = "November" ;
  sMonth[ 11 ] = "December" ;

  // Draw the calendar for the month.
  w.document.open( );
  w.document.write( "<html><head><title>Calendar</title>" );
  w.document.write( "<script language='JavaScript' src='form2.js'>" );
  w.document.write( "</script>" );
  w.document.write( "</head><body bgcolor='#FFDBAA'><form>" );

  w.document.write( "<center><table border=\"0\">" );
  w.document.write( "<tr>" );
  w.document.write( "<th></th>" );
  w.document.write( "<th><input type=\"button\" value=\"<--\" onClick=\"displayCalendar2(" +
                    iPreviousYear + "," + iPreviousMonth + "," + iField +
                    ");\"></th>" );
  w.document.write( "<th colspan=3>" + sMonth[ iMonth ] + " " + iYear + "</th>" );
  w.document.write( "<th><input type=\"button\" value=\"-->\" onClick=\"displayCalendar2(" +
                    iNextYear + "," + iNextMonth + "," + iField +
                    ");\"></th>" );
  w.document.write( "<th></th>" );
  w.document.write( "</tr>" );

  w.document.write( "<tr>" );
  for ( iColumn = 0 ;
        iColumn < sDay.length ;
        iColumn ++ ) {
    w.document.write( "<td><small><center>" + sDay[ iColumn ] + "</center></small></td>" );
  }
  w.document.write( "</tr>" );

  var date1 = new Date( iYear, iMonth, 1, 0, 0, 0, 0 );
  var iDay1 = date1.getDay( );
  var iDay = 0 ;
  var iRow, iColumn ;
  for ( iRow = 0 ;
        iRow < 6 ;
        iRow ++ ) {

    w.document.write( "<tr>" );
    for ( iColumn = 0 ;
          iColumn < sDay.length ;
          iColumn ++ ) {

      if ( iDay == 0 ) {
        if ( iDay1 == iColumn ) {
          iDay = 1 ;
        }
      }

      if ( iDay == 0 ) {
        w.document.write( "<td></td>" );
      } else {
        if ( iDay <= iDays[ iMonth ] ) {
          if ( document.all ) {
            w.document.write( "<td bgcolor='#EECA99'><center>" );
            w.document.write( '<a onClick="chooseDate(' + iYear + ',\'' +
                              sMonth[ iMonth ] + '\',' + iDay + ',' +
                              iField + ');">' );
            w.document.write( iDay );
            w.document.write( "</a>" );
          } else {
            var iValue ;

            if ( iDay < 10 ) {
              iValue = '0' + iDay ;
            } else {
              iValue = iDay ;
            }

            w.document.write( "<td><center>" );
            w.document.write( '<input type="button" onClick="chooseDate(' +
                              iYear + ',\'' +
                              sMonth[ iMonth ] + '\',' + iDay + ',' +
                              iField + ');" value="' + iValue + '">' );
          }
          w.document.write( "</center></td>" );
        }
        iDay ++ ;
      }
    }
    w.document.write( "</tr>" );
  }

  w.document.write( "</table></center>" );

  w.document.write( "</form></body></html>" );

  w.document.close( );
}

function openCalendar ( iField ) {
  var dateToday = new Date( );
  var iYear = dateToday.getYear( );
  var iMonth = dateToday.getMonth( );

  // Fix the millennium bug in Navigator.
  if ( iYear < 2000 ) {
    iYear = iYear + 1900 ;
  }

  document.w = open( "", "displayWindow", "width=400,height=230" );
  displayCalendar1( iYear, iMonth, iField );
}

// Local functions:

// Remove unwanted input data so that it does not appear in the e-mail.
function filterInput ( ) {
}

function firstTime ( ) {
  var bFirst ;
  var sDate = new String( );

  if ( document.Booking.Date.value == sDate ) {
    bFirst = true ;
  } else {
    bFirst = false ;
  }

  return bFirst ;
}

// Retrieve all cookies.
function getCookies ( ) {
  getFormCookie( 'Booking', 'Address' );
  getFormCookie( 'Booking', 'Email' );
  getFormCookie( 'Booking', 'Name' );
  getFormCookie( 'Booking', 'Payment' );
  getFormCookie( 'Booking', 'Phone' );
}

function initialise ( ) {
  if ( firstTime( ) ) {
    setDefaults( );
    getCookies( );
    document.Booking.Name.focus( );
  }
}

// Save all cookies.
function setCookies ( ) {
  setFormCookie( 'Booking', 'Address' );
  setFormCookie( 'Booking', 'Email' );
  setFormCookie( 'Booking', 'Name' );
  setFormCookie( 'Booking', 'Payment' );
  setFormCookie( 'Booking', 'Phone' );
}

function setDefaults ( ) {
  setDefaultTravelTimes( );
  setDefaultMethodOfPayment( );
}

function setDefaultMethodOfPayment ( ) {
  document.Booking.Payment.value = "Cash or cheque" ;
}

function setDefaultTravelTimes ( ) {
  var dateToday = new Date( );
  var iDay = dateToday.getDate( );
  var iMonth = dateToday.getMonth( );
  var iYear = dateToday.getYear( );
  var sDate = new String( );
  var sMonth = new Array( );

  // Fix the millennium bug in Navigator.
  if ( iYear < 2000 ) {
    iYear = iYear + 1900 ;
  }

  // Initialise data.
  sMonth[ 0 ] = "January" ;
  sMonth[ 1 ] = "February" ;
  sMonth[ 2 ] = "March" ;
  sMonth[ 3 ] = "April" ;
  sMonth[ 4 ] = "May" ;
  sMonth[ 5 ] = "June" ;
  sMonth[ 6 ] = "July" ;
  sMonth[ 7 ] = "August" ;
  sMonth[ 8 ] = "September" ;
  sMonth[ 9 ] = "October" ;
  sMonth[ 10 ] = "November" ;
  sMonth[ 11 ] = "December" ;

  // Set the journey date to one week from today.
  iDay = iDay + 7 ;
  if ( iDay > 28 ) {
    iDay = 1 ;
    if ( ++ iMonth > 12 ) {
      iMonth = 0 ;
      iYear ++ ;
    }
  }
  sDate = iDay.toString( ) + " " + sMonth[ iMonth ] + " " + iYear.toString( );
  document.Booking.Date.value = sDate ;
}

function validate ( ) {
  var bValid = true ;
  var sEmpty = new String( );

  // Check for a name.
  if ( document.Booking.Name.value == sEmpty ) {
    bValid = false ;
    alert( "Please enter your name." );
  }

  // Check for either an e-mail address or a telephone number.
  if ( ( document.Booking.Email.value == sEmpty ) &&
       ( document.Booking.Phone.value == sEmpty ) ) {
    bValid = false ;
    alert( "Please enter either an e-mail address or a telephone number." );
  }

  // Check for an address.
  if ( document.Booking.Address.value == sEmpty ) {
    bValid = false ;
    alert( "Please enter where you wish to travel from." );
  }

  // Check for a travel date.
  if ( document.Booking.Date.value == sEmpty ) {
    bValid = false ;
    alert( "Please enter the date of your trip." );
  }

  // Check that at least an outward journey is required.
  if ( document.Booking.PickupTime.value == sEmpty ) {
    bValid = false ;
    alert( "Please enter a pick-up time." );
  }

  // Check for a destination.
  if ( document.Booking.To.value == sEmpty ) {
    bValid = false ;
    alert( "Please enter where you wish to travel to." );
  }

  if ( bValid ) {
    setCookies( );
    filterInput( );
  }

  return bValid ;
}

