// *******************************
// ** Calendar Booking system   **
// ** written by Stephen Braggs **
// ** stephen_empire@yahoo.com  **
// ** April 2002                **
// *******************************

var theLocation='bookings.dat';
var fileData;
dateA43_selected = 0;
for (i=1;i<43;i++) {
  eval("dateA"+i+"_selected = 0;");
}

//calendar format
jan2004 = 3;
feb2004 = 6;
mar2004 = 0;
apr2004 = 3;
may2004 = 5;
jun2004 = 1;
jul2004 = 3;
aug2004 = 6;
sep2004 = 2;
oct2004 = 4;
nov2004 = 0;
dec2004 = 2;
jan2005 = 5;
feb2005 = 1;
mar2005 = 1;
apr2005 = 4;
may2005 = 6;
jun2005 = 2;
jul2005 = 4;
aug2005 = 0;
sep2005 = 3;
oct2005 = 5;
nov2005 = 1;
dec2005 = 3;
jan2006 = 6;
feb2006 = 2;
mar2006 = 2;
apr2006 = 5;
may2006 = 0;
jun2006 = 3;
jul2006 = 5;
aug2006 = 1;
sep2006 = 4;
oct2006 = 6;
nov2006 = 2;
dec2006 = 4;
jan2007 = 0;
feb2007 = 3;
mar2007 = 3;
apr2007 = 6;
may2007 = 1;
jun2007 = 4;
jul2007 = 6;
aug2007 = 2;
sep2007 = 5;
oct2007 = 0;
nov2007 = 3;
dec2007 = 5;
jan2008 = 1;
feb2008 = 4;
mar2008 = 5;
apr2008 = 1;
may2008 = 3;
jun2008 = 6;
jul2008 = 1;
aug2008 = 4;
sep2008 = 0;
oct2008 = 2;
nov2008 = 5;
dec2008 = 0;
jan2009 = 3;

feb2009 = 6;

mar2009 = 6;

apr2009 = 2;

may2009 = 4;

jun2009 = 0;

jul2009 = 2;

aug2009 = 5;

sep2009 = 1;

oct2009 = 3;

nov2009 = 6;

dec2009 = 1;

jan2010 = 4;

feb2010 = 0;

mar2010 = 0;

apr2010 = 3;

may2010 = 5;

jun2010 = 1;

jul2010 = 3;

aug2010 = 6;

sep2010 = 2;

oct2010 = 4;

nov2010 = 0;

dec2010 = 2;
jan2011 = 5;

feb2011 = 1;

mar2011 = 1;

apr2011 = 4;

may2011 = 6;

jun2011 = 2;

jul2011 = 4;

aug2011 = 0;

sep2011 = 3;

oct2011 = 5;

nov2011 = 1;

dec2011 = 3;

jan2012 = 6;

feb2012 = 2;

mar2012 = 3;

apr2012 = 6;

may2012 = 1;

jun2012 = 4;

jul2012 = 6;

aug2012 = 2;

sep2012 = 5;

oct2012 = 0;

nov2012 = 3;

dec2012 = 5;

sel = '#186394';
nsel = '#ffffff';
used = '#c0c0c0';
blnk = '';
var now = new Date();
today_day = now.getDate();
today_month = now.getMonth() + 1;
today_year = now.getYear();

browserName = navigator.appName;
browserVer = parseInt(navigator.appVersion);
        if ((browserName == "Netscape" && browserVer >= 3) || (browserName ==
             "Microsoft Internet Explorer" && browserVer >= 4)) version = "n3";
        else version = "n2";

var dateBooked='';
theDate=0;

function isDateBooked() {
 dateBooked=fileData.substring(theDate,theDate+1);
 if (dateBooked == "1") {
   //DATE BOOKED
 } else if (dateBooked == "0") {
   //DATE NOT BOOKED
 } else {
   //ERROR IN FILE
 }
}

function text_on(navid) {
  document.all [navid].style.color = '#ccccff';
}
function text_out(navid) {
  document.all [navid].style.color = '#ffffff';
}

function startup() {
//  fileData = somestring;
  switch(today_month) {
    case 1: mm = 'Jan'; break;
    case 2: mm = 'Feb'; break;
    case 3: mm = 'Mar'; break;
    case 4: mm = 'Apr'; break;
    case 5: mm = 'May'; break;
    case 6: mm = 'Jun'; break;
    case 7: mm = 'Jul'; break;
    case 8: mm = 'Aug'; break;
    case 9: mm = 'Sep'; break;
    case 10: mm = 'Oct'; break;
    case 11: mm = 'Nov'; break;
    case 12: mm = 'Dec'; break;
    default: alert("Please contact administrator");
  }
  document.all.month.value = mm;
  document.all.year.value = today_year;
  change_date();
}

function change_date() {
  for (i=1;i<43;i++) {
    theday = 'A'+i;
    eval("dateA"+i+"_selected = 0;");
    document.all [theday].style.background = nsel;
  }
  days_in_month = 0;
  theyear = document.all.year.value;
  switch (document.all.month.value) {
    case 'Jan': eval("themonth = jan"+theyear+";"); days_in_month = 31; location_in_file=0; break;
    case 'Feb': eval("themonth = feb"+theyear+";"); days_in_month = 28; location_in_file=33; break;
    case 'Mar': eval("themonth = mar"+theyear+";"); days_in_month = 31; location_in_file=63; break;
    case 'Apr': eval("themonth = apr"+theyear+";"); days_in_month = 30; location_in_file=96; break;
    case 'May': eval("themonth = may"+theyear+";"); days_in_month = 31; location_in_file=128; break;
    case 'Jun': eval("themonth = jun"+theyear+";"); days_in_month = 30; location_in_file=161; break;
    case 'Jul': eval("themonth = jul"+theyear+";"); days_in_month = 31; location_in_file=193; break;
    case 'Aug': eval("themonth = aug"+theyear+";"); days_in_month = 31; location_in_file=226; break;
    case 'Sep': eval("themonth = sep"+theyear+";"); days_in_month = 30; location_in_file=259; break;
    case 'Oct': eval("themonth = oct"+theyear+";"); days_in_month = 31; location_in_file=291; break;
    case 'Nov': eval("themonth = nov"+theyear+";"); days_in_month = 30; location_in_file=324; break;
    case 'Dec': eval("themonth = dec"+theyear+";"); days_in_month = 31; location_in_file=356; break;
    case 'none': for (i=1;i<43;i++) {
                   theday = 'A'+i;
                   document.all [theday].style.background = blnk;
                 }
    default: alert("error: please contact administrator");
  }

  // Leap Year
  if (theyear == '2008' || theyear == '2012') {
    if (days_in_month == 28) days_in_month = 29;
    if (location_in_file > 60) location_in_file++;
  }

  // boxes which are booked
  if (theyear == '2009') location_in_file+=391;
  else if (theyear == '2010') location_in_file+=782;
  else if (theyear == '2011') location_in_file+=1174;
  else if (theyear == '2012') location_in_file+=1565;
  else if (theyear == '2013') location_in_file+=1956;

  // boxes which aren't days
  for (i=1;i<themonth+1;i++) {
    theday = 'A'+i;
    document.all [theday].style.background = blnk;
  }
  for (i=days_in_month+themonth+1;i<43;i++) {
    theday = 'A'+i;
    document.all [theday].style.background = blnk;
  }

  j = 1;
  for (i=1;i<43;i++) {
    theday = 'A'+i;
    if (document.all [theday].style.background == nsel || document.all [theday].style.background == used) {
      document.all [theday].value = j;
      j++;
    } else {
      document.all [theday].value = "";
    }
  }
}

function selected(theday) {
  if (document.all [theday].style.background == sel) {
    document.all [theday].style.background = nsel;
    eval("date"+theday+"_selected = 0;");
  }
  else if (document.all [theday].style.background == used) {
    alert("This day is already booked, please select another day.");
  }
  else if (document.all [theday].style.background == nsel) {
    document.all [theday].style.background = sel;
    eval("date"+theday+"_selected = 1;");
  }
}

function clearall() {
  for (i=1;i<43;i++) {
    theday = 'A'+i;
    eval("dateA"+i+"_selected = 0;");
    if (document.all [theday].style.background == sel) {
      document.all [theday].style.background = nsel;
    }
  }
}

function book_dates() {
  dates_selected = "";
  for(i=1;i<43;i++) {
    if (eval("dateA"+i+"_selected") == 1) {
      theday = 'A'+i;
      dd = document.all [theday].value;
      mm = document.all.month.value
      yy = document.all.year.value;
      dates_selected = dates_selected +dd+"-"+mm+"-"+yy+" ";
    }
  }
  document.all.Dates.value = document.all.Dates.value + dates_selected;
  clearall();
  location.href = '#submit'
}

function submitDetails() {
  isDates = false;
  isName = false;
  isContact = false;
  isGuest = false;
  isAlert = false;
  str = "The following fields require details:";
  if (document.form_details.Dates.value == "") {
    isAlert = true;
    document.form_details.DateLabel.style.color = '#FF0000';
  } else {
    isDates = true;
    document.form_details.DateLabel.style.color = '#FFFFFF';
  }
  if (document.form_details.Name.value == "") {
    isAlert = true;
    document.form_details.NameLabel.style.color = '#FF0000';
  } else {
    isName = true;
    document.form_details.NameLabel.style.color = '#FFFFFF';
  }
  if (document.form_details.Phone1.value == "" && document.form_details.Email.value == "") {
    isAlert = true;
    document.form_details.Phone1Label.style.color = '#FF0000';
    document.form_details.EmailLabel.style.color = '#FF0000';
  } else {
    isContact = true;
    document.form_details.Phone1Label.style.color = '#FFFFFF';
    document.form_details.EmailLabel.style.color = '#FFFFFF';
  }
  if (document.form_details.NoOfGuest.value == "") {
    isAlert = true;
    document.form_details.NoOfGuestLabel.style.color = '#FF0000';
  } else {
    isGuest = true;
    document.form_details.NoOfGuestLabel.style.color = '#FFFFFF';
  }
  if (isDates == false) {
    str = str + "\n    Dates Selected";
  }
  if (isName == false) {
    str = str + "\n    Name";
  }
  if (isContact == false) {
    str = str + "\n    Phone Number or Email Address";
  }
  if (isGuest == false) {
    str = str + "\n    Number Of Guests";
  }
  if (isAlert) {
    alert(str);
  } else {
    document.form_details.submit();
  }
}

function resetLabel() {
    document.form_details.DateLabel.value = "Dates Selected";
    document.form_details.NameLabel.value = "Name";
    document.form_details.AddressLabel.value = "Address";
    document.form_details.Phone1Label.value = "Phone";
    document.form_details.Phone2Label.value = "Alternate Phone";
    document.form_details.EmailLabel.value = "Email Address";
    document.form_details.NoOfGuet.value = "Number Of Guests";
    document.form_details.CommentsLabel.value = "Comments";
}