//Javascript doc

var monthDays=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var dayNameShort=new Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
var dayOffsetDate = new Date(2004, 9, 03, 0, 0, 0, 0);
var dayOffset = dayOffsetDate.getDay();
	
function monthChanged(monthYear, dayBoxId) {
	var dayVal;
var month = eval(monthYear.substring(5, 7)); //The selected year
var year = eval(monthYear.substring(0, 4));	//The selected month
	//alert(year);alert(month);
	var numDays = monthDays[month - 1];		//The number of days in the month
	var fromDay = 1;	//The first date to show in the drop down
	var today = new Date;	//Today's date
	var i;	//counter / index
	var dd = document.getElementById(dayBoxId); //date dropdown
	var optionText;	//option text
	var optionValue; //option value
	var aDate = new Date; //date object used to loop through the dates in the drop down box
	var prevVal = eval(dd.value);  //the previous value of the date dropdown
	var didSet = false;  //identifies whether the index position was set after the box was filled

	if (getCookie('date_val') != null) {
		prevVal = eval(getCookie('date_val'));
	}
	
	if (navigator.appName == 'Netscape') {
		year = year - 1900;
	}
	
	//if current month and year, set the start day to today
	if (((month - 1) == today.getMonth()) && (year == today.getYear())) {
		fromDay = today.getDate();
	}

	//if the month is february, check to see if it's a leap year.
	if (month == 2) {
		if((year%4==0 && year%100!=0)||(year%400==0)){
			numDays = 29;
		} 
		else {
			numDays = 28;
		}	
	}
	
	//clear out the drop down
	for (i = 0; i <= 31; i++) {
		try {
			dd.remove(0);
		}
		catch (ex) {
			i = 32;
		}
	}
	
	//prepare the aDate object
	
	if (navigator.appName == 'Netscape') {
		aDate.setYear(year + 1900);
	}	
	else {
		aDate.setYear(year);
	}
	aDate.setMonth(month - 1);
	aDate.setDate(fromDay);

	if (aDate.getMonth != (month - 1)) {
		aDate.setMonth(month - 1);
	}
	
	//fill the dropdown
	for (i = 0; i <= (numDays - fromDay); i++) {
		aDate.setDate(i + fromDay);
		
		dayVal = aDate.getDay() - dayOffset;
		//alert(dayVal);
		if (dayVal < 0) {
			dayVal = 7 + dayVal;
		}
		//alert(dayVal);
		
		optionValue = i + fromDay;
		optionText = dayNameShort[dayVal] + ' ' + optionValue;
		
		dd.options[i] = new Option(optionText, optionValue);
		
		if (prevVal == (i + fromDay)) {
			dd.selectedIndex = i;
			didSet = true;
		}
	}
	
	//if the index wasn't set, then set it to 0
	if (!didSet) {
		dd.selectedIndex = 0;
	}	
	
	//set the foucs to the day drop down
	try {
		dd.focus();
	}
	catch (ex) {
		
	}
	
	return true;
	
}

function putCookie(name, value) {
	setCookie(name, value, null, '/', 'toptable.co.uk', null);
}

function setCookie (name,value,expires,path,domain,secure) {
 DeleteCookie(name, path, domain);
 document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");

  return true;
}

function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}
//
//  Function to correct for 2.x Mac date bug.  Call this function to
//  fix a date object prior to passing it to SetCookie.
//  IMPORTANT:  This function should only be called *once* for
//  any given date object!  See example at the end of this document.
//
function FixCookieDate (date) {
  var base = new Date(0);
  var skew = base.getTime(); // dawn of (Unix) time - should be 0
  if (skew > 0)  // Except on the Mac - ahead of its time
    date.setTime (date.getTime() - skew);
}

function DeleteCookie (name,path,domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

function getCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
  return null;
}

function writeSelect() {
	document.write('<select name="EventDay" onChange="return putCookie(\'date_val\', document.getElementById(\'select4\').value);" id="select4" class="formelements" style="width:70px;">' + 
    '<option value="1"' + '></option>' + 
    '<option value="2"' + '></option>' + 
    '<option value="3"' + '></option>' + 
    '<option value="4"' + '></option>' + 
    '<option value="5"' + '></option>' + 
    '<option value="6"' + '></option>' + 
    '<option value="7"' + '></option>' + 
    '<option value="8"' + '></option>' + 
    '<option value="9"' + '></option>' + 
    '<option value="10"' + '></option>' + 
    '<option value="11"' + '></option>' + 
    '<option value="12"' + '></option>' + 
    '<option value="13"' + '></option>' + 
    '<option value="14"' + '></option>' + 
    '<option value="15"' + '></option>' + 
    '<option value="16"' + '></option>' + 
    '<option value="17"' + '></option>' + 
    '<option value="18"' + '></option>' + 
    '<option value="19"' + '></option>' + 
    '<option value="20"' + '></option>' + 
    '<option value="21"' + '></option>' + 
    '<option value="22"' + '></option>' + 
    '<option value="23"' + '></option>' + 
    '<option value="24"' + '></option>' + 
    '<option value="25"' + '></option>' + 
    '<option value="26"' + '></option>' + 
    '<option value="27"' + '></option>' + 
    '<option value="28"' + '></option>' + 
    '<option value="29"' + '></option>' + 
    '<option value="30"' + '></option>' + 
    '<option value="31"' + '></option>' + 
	'</select>');
}
