// Set date to tomorrow

var DOMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
// Leap year Month days..
var lDOMonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var months_en = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var months_es = ["Ene", "Feb", "Mar", "Abr", "May", "Jun", "Jul", "Ago", "Sep", "Oct", "Nov", "Dic"];

var _month = new Array("January","February","March","April","May","June",
		"July","August","September","October","November","December");
var _day = new Array("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31");
var _year = new Array("2009","2010","2011");

// set dates and setup pp params
function setdates_dub_gal() 
{

// For Galway: Sunday to Thursday, now till October
// Start on Sunday 26th 2009

if(document.theForm.item_name.options[2].selected == true) {

	removeAllOptions(document.theForm.selMonth);
	removeAllOptions(document.theForm.selYear);	

	var i;var j;
	
	//No tour friday or saturday (index 5 and 6 - 0 is sunday)
	for(j=7;j<=12;j++) {
		addOption(document.theForm.selMonth,_month[j-1],j)
	}
	
	//now the days for current month
	var today = new Date();
	var cur_month = today.getMonth();
	set_galway_days_for_selected_month(cur_month);

	addOption(document.theForm.selYear,"2009", 2009);	

	//change the price to galway price 5euro
	if (document.getElementById) { 
	//alert("hidden")
		//document.getElementById("button10").style.visibility="hidden";
		document.getElementById("amount").value= 5;
	}

	}
	
	else if(document.theForm.item_name.options[1].selected == true) {
		// Dublin all is possible
		removeAllOptions(document.theForm.selDay);
		removeAllOptions(document.theForm.selMonth);
		removeAllOptions(document.theForm.selYear);	
		
		for (var i=0; i < _month.length;++i){
	
		addOption(document.theForm.selMonth, _month[i], i+1);
		}
		
		for (var i=0; i < _day.length;++i){
	
		addOption(document.theForm.selDay, _day[i], i+1);
		}
		
		//year is in reverse format
		var optn = document.createElement("OPTION");
		optn.text = "2011";
		optn.value = 2011;
		document.theForm.selYear.options.add(optn);
		var optn1 = document.createElement("OPTION");
		optn1.text = "2010";
		optn1.value = 2010;
		document.theForm.selYear.options.add(optn1);
		var optn2 = document.createElement("OPTION");
		optn2.text = "2009";
		optn2.value = 2009;
		document.theForm.selYear.options.add(optn2);
		
		
		setTodayTomorrow();
	
	
	//show calender again
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById("button10").style.visibility="visible";
		document.getElementById("amount").value= 9;
	}

	} else {
			//show calender again
		if (document.getElementById) { // DOM3 = IE5, NS6
			document.getElementById("button10").style.visibility="visible";
		}
	}
	
	
}

////////////////////////////////////////////
//
//  Sets the available days for galway per month selected
//  July, Aug, Sep - Sun to Thurs
//  Oct, Nov, Dec - Fri, Sat
//
/////////////////////////////////////
function set_galway_days_for_selected_month(cur_month)
{
	removeAllOptions(document.theForm.selDay);
	var max_days;
	
	if ( cur_month == 3 || cur_month == 5 || cur_month == 8 || cur_month == 10 ) {
    	max_days = 30;
	}
	else if(cur_month == 1) {
		max_days = 28
	} else {
		max_days = 31
	}
	var i;
	for(i=1;i<=max_days;i++) {
		var cur_day = new Date()
		cur_day.setDate(i)
		cur_day.setMonth(cur_month)
		cur_day.setYear(2009)
	
		if(cur_month == 7 || cur_month == 8) {
			if(cur_day.getDay() < 5) {
				//alert("current day = " + cur_day.getDay() )
				addOption(document.theForm.selDay, i, i)
			}	
		} else if(cur_month == 9 || cur_month == 10 || cur_month == 11) {
			if(cur_day.getDay() >= 5) {
				if(cur_month == 11 && (i == 25 || i == 26))
				{
					//exclude christmas day and 26th
				} else{
					addOption(document.theForm.selDay, i, i)
				}
			}
		}
	
	}
}	


// remove all days and months
function removeAllOptions(selectbox)
{
var i;
for(i=selectbox.options.length-1;i>=0;i--)
{
selectbox.remove(i);
}
}

function addOption(selectbox,text,value )
{
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;
selectbox.options.add(optn);
}



function setTodayTomorrow() {

	tomorrow = getDayMonYeaAfterTomorrow();
	
	dayInIndex = tomorrow[0] -1;
	monInIndex = tomorrow[1];
	yearInIndex = tomorrow[2]-2007;

 //alert(dayInIndex + " "+ monInIndex + " "+ yearInIndex);

	document.theForm.selDay.options[dayInIndex].selected=true;
	document.theForm.selMonth.options[monInIndex].selected=true;
	document.theForm.selYear.options[yearInIndex].selected=true;

	aftertomorrow = getDayMonYeaInThreeDays();
	dayOutIndex = aftertomorrow[0] -1;
	monOutIndex = aftertomorrow[1];
	
	//alert(dayOutIndex + " "+ monOutIndex);
	
//	document.DatesForm.outday.options[dayOutIndex].selected=true;
//	document.DatesForm.outmonth.options[monOutIndex].selected=true;
	if(!set_pp_vars()) {
		document.theForm.on1.value = "Tour Date is " + document.theForm.selDay.options[dayInIndex].value + "/" + document.theForm.selMonth.options[monInIndex].value + "/" + document.theForm.selYear.options[yearInIndex].value;
	}
}

function validateAndCorrectDayMonthYear(daySelect, monthYearSelect) {
	// use the fact that the worst case scenario is that a user select Feb 31, to make r
	//alert('validateAndCorrectDayMonthYear');
	var day = daySelect[daySelect.selectedIndex].value;
	//alert(day);
	var monthYear = monthYearSelect[monthYearSelect.selectedIndex].value;
	var month = monthYear.substring(5,monthYear.length)-1;
	var year = monthYear.substring(0,4);
	var cDate = new Date(year, month, day , 0, 0, 0, 0);
	//alert(cDate.getDate());
	if(cDate.getDate() != day) { // dates should be the same
		daySelect[day-2].selected=true;
		validateAndCorrectDayMonthYear(daySelect, monthYearSelect);
	}

}

function setOutDayMonthYearIfBeforeInDayMonthYear(formChanged) {

	var inMonthYear = formChanged.inmonthyear[formChanged.inmonthyear.selectedIndex].value;
	var outMonthYear = formChanged.outmonthyear[formChanged.outmonthyear.selectedIndex].value;
	var inDay = formChanged.inday[formChanged.inday.selectedIndex].value;
	var outDay = formChanged.outday[formChanged.outday.selectedIndex].value;
	
	var inMonth = inMonthYear.substring(5,inMonthYear.length)-1;
	var inYear = inMonthYear.substring(0,4);
	var outMonth = outMonthYear.substring(5,outMonthYear.length)-1;
	var outYear = outMonthYear.substring(0,4);
	
	
	
	var inDate = new Date(inYear, inMonth, inDay , 0, 0, 0, 0);
	var outDate = new Date(outYear, outMonth, outDay , 0, 0, 0, 0);
	
	if(outDate.getTime() <= inDate.getTime()) {
		outDate = new Date(inDate.getTime());
		outDate.setDate(outDate.getDate() + 1);
		formChanged.outday[outDate.getDate()-1].selected=true;
		
		//alert(' ' + inDate.getDate() + ' ' + outDate.getDate() );
		if( outDate.getDate() < inDay ) {
			formChanged.outmonthyear[ formChanged.inmonthyear.selectedIndex + 1].selected=true;
		} else {
			formChanged.outmonthyear[ formChanged.inmonthyear.selectedIndex].selected=true;		
		}
	}	
}

function setOutDayMonthIfBeforeInDayMonth(formChanged) {
	var now = new Date();
	
	var inDay = formChanged.inday[formChanged.inday.selectedIndex].value;
	var outDay = formChanged.outday[formChanged.outday.selectedIndex].value;
			
	var inMonth = formChanged.inmonth[formChanged.inmonth.selectedIndex].value;
	var outMonth = formChanged.outmonth[formChanged.outmonth.selectedIndex].value;

	
	//alert('inDay ' + inDay + 'inMonth ' + inMonth + 'outDay ' + outDay + 'outMonth ' + outMonth);
	var inDate = new Date(now.getFullYear(), inMonth-1, inDay , 0, 0, 0, 0);

	if(now.getTime() >= inDate.getTime()) {
		inDate.setFullYear(now.getFullYear()+1);
	}
	
	var outDate = new Date(now.getFullYear(), outMonth-1, outDay , 0, 0, 0, 0);
	if(now.getTime() >= outDate.getTime()) {
		outDate.setFullYear(now.getFullYear()+1);
	}
	
	if(outDate.getTime() <= inDate.getTime()) {
	
		outDate = new Date(inDate.getTime());
		outDate.setDate(outDate.getDate() + 1);
		formChanged.outday[outDate.getDate()-1].selected=true;
		
		if( outDate.getDate() < inDay ) {
			formChanged.outmonth[formChanged.inmonth.selectedIndex + 1].selected=true;
		} else {
			formChanged.outmonth[formChanged.inmonth.selectedIndex].selected=true;		
		}
	}	
}

function getDayMonYeaNow() {
	var ret_arr = new Array();
	var gNow = new Date();
	var vNowDay = gNow.getDate();
	var vNowMonth = gNow.getMonth();
	var vNowYear = gNow.getFullYear();

	ret_arr[0] = vNowDay;
	ret_arr[1] = vNowMonth;
	ret_arr[2] = vNowYear;

	return ret_arr;

}

function getDayMonYeaTomorrow() {
	var ret_arr = new Array();
	var gNow = new Date();
	var vNowDay = gNow.getDate();
	var vNowMonth = gNow.getMonth();
	var vNowYear = gNow.getFullYear();
	
	var vLastDay= get_daysofmonth(vNowMonth, vNowYear);

	vNowDay = vNowDay +1;	
	if (vNowDay > vLastDay) {
		vNowDay = 1;
		moYeArr = Calendar_calc_month_year(vNowMonth, vNowYear, 1);
		vNowMonth = moYeArr[0];
		vNowYear = moYeArr[1];
	}

	ret_arr[0] = vNowDay;
	ret_arr[1] = vNowMonth;
	ret_arr[2] = vNowYear;

	return ret_arr;

}

function getDayMonYeaAfterTomorrow() {
	var ret_arr = new Array();
	var gNow = new Date();
	var vNowDay = gNow.getDate();
	var vNowMonth = gNow.getMonth();
	var vNowYear = gNow.getFullYear();
	
	var vLastDay= get_daysofmonth(vNowMonth, vNowYear);

	vNowDay = vNowDay +1;	
	if (vNowDay > vLastDay) {
		vNowDay = 1;
		moYeArr = Calendar_calc_month_year(vNowMonth, vNowYear, 1);
		vNowMonth = moYeArr[0];
		vNowYear = moYeArr[1];
	}

//	vNowDay = vNowDay +1;	
//	if (vNowDay > vLastDay) {
//		vNowDay = 1;
//		moYeArr = Calendar_calc_month_year(vNowMonth, vNowYear, 1);
//		vNowMonth = moYeArr[0];
//		vNowYear = moYeArr[1];
//	}

	ret_arr[0] = vNowDay;
	ret_arr[1] = vNowMonth;
	ret_arr[2] = vNowYear;

	return ret_arr;

}
function getDayMonYeaInThreeDays() {
	var ret_arr = new Array();
	var gNow = new Date();
	var vNowDay = gNow.getDate();
	var vNowMonth = gNow.getMonth();
	var vNowYear = gNow.getFullYear();
	
	var vLastDay= get_daysofmonth(vNowMonth, vNowYear);

	vNowDay = vNowDay +1;	
	if (vNowDay > vLastDay) {
		vNowDay = 1;
		moYeArr = Calendar_calc_month_year(vNowMonth, vNowYear, 1);
		vNowMonth = moYeArr[0];
		vNowYear = moYeArr[1];
	}

	vNowDay = vNowDay +1;	
	if (vNowDay > vLastDay) {
		vNowDay = 1;
		moYeArr = Calendar_calc_month_year(vNowMonth, vNowYear, 1);
		vNowMonth = moYeArr[0];
		vNowYear = moYeArr[1];
	}

	vNowDay = vNowDay +1;	
	if (vNowDay > vLastDay) {
		vNowDay = 1;
		moYeArr = Calendar_calc_month_year(vNowMonth, vNowYear, 1);
		vNowMonth = moYeArr[0];
		vNowYear = moYeArr[1];
	}

	ret_arr[0] = vNowDay;
	ret_arr[1] = vNowMonth;
	ret_arr[2] = vNowYear;

	return ret_arr;

}
 function get_daysofmonth(monthNo, p_year) {
	/* 
	Check for leap year ..
	1.Years evenly divisible by four are normally leap years, except for... 
	2.Years also evenly divisible by 100 are not leap years, except for... 
	3.Years also evenly divisible by 400 are leap years. 
	*/
	if ((p_year % 4) == 0) {
		if ((p_year % 100) == 0 && (p_year % 400) != 0)
			return DOMonth[monthNo];
	
		return lDOMonth[monthNo];
	} else
		return DOMonth[monthNo];
}

function Calendar_calc_month_year(p_Month, p_Year, incr) {
	/* 
	Will return an 1-D array with 1st element being the calculated month 
	and second being the calculated year 
	after applying the month increment/decrement as specified by 'incr' parameter.
	'incr' will normally have 1/-1 to navigate thru the months.
	*/
	var ret_arr = new Array();
	
	if (incr == -1) {
		// B A C K W A R D
		if (p_Month == 0) {
			ret_arr[0] = 11;
			ret_arr[1] = parseInt(p_Year) - 1;
		}
		else {
			ret_arr[0] = parseInt(p_Month) - 1;
			ret_arr[1] = parseInt(p_Year);
		}
	} else if (incr == 1) {
		// F O R W A R D
		if (p_Month == 11) {
			ret_arr[0] = 0;
			ret_arr[1] = parseInt(p_Year) + 1;
		}
		else {
			ret_arr[0] = parseInt(p_Month) + 1;
			ret_arr[1] = parseInt(p_Year);
		}
	}
	
	return ret_arr;
}

//-->

function makeValidDate(_object) {
  var year;
  var month;
  var day;
  // no fridays or saturdays in galway - so set available days if month is selected
  if(document.theForm.item_name.options[2].selected == true) {
	  var elemId = _object.getAttribute("id");
	  //alert("elem id =  " + elemId)
	  if(elemId == "selectMonth"){
		
		month = document.theForm.selMonth.options[ document.theForm.selMonth.selectedIndex ].value;
		set_galway_days_for_selected_month(month-1)
		//alert("month set" + month)
	  }
  }
  
  year = document.theForm.selYear.options[ document.theForm.selYear.selectedIndex ].value;
  month = document.theForm.selMonth.options[ document.theForm.selMonth.selectedIndex ].value;
  day = document.theForm.selDay.options[ document.theForm.selDay.selectedIndex ].value;
  maxDay = 31;
  
  
  if ( month == 4 || month == 6 || month == 9 || month == 11 ) {
    maxDay = 30;
  } else if ( month == 2 ) {
    if ( year%100 != 0 && year%4 == 0 ) {
      maxDay = 29;
    } else {
      maxDay = 28;
    }
  }
  
  //seems to be some issuw with this function
  if(document.theForm.item_name.options[2].selected == false) {
  	document.theForm.selDay.selectedIndex = Math.min(day, maxDay)-1;
  }
  set_pp_vars();
  
}

////////////////////////////////////////////////
//
//  Sets up Paypal vars
//
////////////////////////////////////////////////
function set_pp_vars()
  {
	  
	  
  //setup pp vars
  	//set the date for pp
	var ppday;
	var ppmonth;
	var ppyear;
	var ret_val = 0;
	
	for (var i=0; i < document.theForm.selMonth.length;++i){
		if(document.theForm.selMonth.options[i].selected == true) {
			ppmonth = document.theForm.selMonth.options[i].value;
			ret_val = 1;
		}
	}
	
	for (var j=0; j < document.theForm.selDay.length;++j){
	
		if(document.theForm.selDay.options[j].selected == true) {
			ppday = document.theForm.selDay.options[j].value;
			ret_val = 1;
		}
	}
	
	for (var j=0; j < document.theForm.selYear.length;++j){
	
		if(document.theForm.selYear.options[j].selected == true) {
			ppyear = document.theForm.selYear.options[j].value;
			ret_val = 1;
		}
	}
	
	document.theForm.on1.value = "Tour Date is " + ppday + "/" + ppmonth + "/" + ppyear;	
	return(ret_val);
}
var monthLength = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

function checkDate(){

  var day = parseInt(document.theForm.selDay.options[ document.theForm.selDay.selectedIndex ].value);
  var month = parseInt(document.theForm.selMonth.options[ document.theForm.selMonth.selectedIndex ].value);
  var year = parseInt(document.theForm.selYear.options[ document.theForm.selYear.selectedIndex ].value);

  if (!day || !month || !year)
    return false;

  if (year/4 == parseInt(year/4))
    monthLength[1] = 29;

  if (day > monthLength[month-1])
    return false;

  monthLength[1] = 28;

  var now = new Date();
  now = now.getTime(); //NN3

  var dateToCheck = new Date();
  dateToCheck.setYear(year);
  dateToCheck.setMonth(month-1);
  dateToCheck.setDate(day);
  var checkDate = dateToCheck.getTime();

  var futureDate = (now < checkDate);
  var pastDate = (now > checkDate);

  if(pastDate)
  {
    alert("Please Enter Dates Ahead Of Today\'S Date.  Thank You.");
  }
  else
  {
    document.theForm.Submit.value=1;
    document.theForm.submit();
  }
  
}

 function isBrowserSupp() {
// ****************************************************************
// *    Author: Geraldine Healy         Date:   25/08/1998
// *    Description: Checks if browser is Netscape 2.0 since the options 
// *                            array properties don't work with Netscape 2.0x
// ****************************************************************

    // Get the version of the browser
    version =  parseFloat( navigator.appVersion );

    if ( ( version >= 2.0 ) && ( version < 2.1 ) && ( navigator.appName.indexOf( "Netscape" ) != -1 ) ) {
        return false;
    }
    else {
        return true;
    }                  
}


function isLeapYear(yrStr)
{
// ****************************************************************
// *    Author: Geraldine Healy         Date:   25/08/1998
// *    Description:    Checks if Year selected is a leap year
// ****************************************************************
var leapYear=false;
// every fourth year is a leap year
if ((parseInt(yrStr, 10)%4) == 0)
        {
        leapYear=true;
        }
return leapYear;
}

function getDaysInMonth(mthIdx, YrStr)
// ****************************************************************
// *    Author: Geraldine Healy         Date:   25/08/1998
// *    Description:    Retrieves the number of days in a given month
// ****************************************************************
{
//Default number of days in a month is 31
var maxDays=31
// expect Feb. 
if (mthIdx==2) 
        {
        if (isLeapYear(YrStr))
                {
                maxDays=29;
                }
        else 
                {
                maxDays=28;
                }
        }
// All the rest of the months have 30 days
if (mthIdx==4 || mthIdx==6 || mthIdx==9 || mthIdx==11)
        {
        maxDays=30;
        }
return maxDays;
}


function adjustDate(mthIdx, Dt, Yr) 
// ****************************************************************
// *    Author: Geraldine Healy         Date:   25/08/1998
// *    Description:    Adjusts the format of the Date
// ****************************************************************
{
var value=0;            
var numDays=getDaysInMonth(mthIdx, Yr.options[Yr.options.selectedIndex].value);

if (mthIdx==2) 
        {
        if (Dt.options.selectedIndex < numDays)
                {
                return 0;
                }
        else 
                {
                //check for leap year
                Dt.options.selectedIndex=numDays;
                if (numDays==29)
                        {
                        return 99;
                        }
                else 
                        {
                        return 1;
                        }
                }
        }
if (Dt.options.selectedIndex < numDays)
        {
        value=0;
        }
else 
        {
        if (Dt.options.selectedIndex > numDays)
                {
                Dt.options.selectedIndex;
                value=3;
                }
        else 
                {
                //index is 31 or 30
                value=2;
                }
        }
return value;
}



function parseMonth(mth, inM)
// ****************************************************************
// *    Author: Geraldine Healy         Date:   25/08/1998
// *    Description:    Parses a string and returns a month value
// ****************************************************************
{
var i=1;
var retval =1;
for (i=1;i<=12;i++)
        {
        if (mth == inM.options[i].value)
                {
                retval=i;       
                break;
                }       
        }
        return retval;
}

function parseDay(day, inD)
// ****************************************************************
// *    Author: Geraldine Healy         Date:   25/08/1998
// *    Description:    Parses a string and returns a day value
// ****************************************************************
{
var i=1;
var retval =1;
for (i=1;i<=31;i++)
        {
        if (day == inD.options[i].value)
                {
                retval=i;       
                break;
                }       
        }
return retval;
}

function parseYear(year, inY)
// ****************************************************************
// *    Author: Geraldine Healy         Date:   25/08/1998
// *    Description:    Parses a string and returns a year value
// ****************************************************************
{
var retval=0;
var i=0;
     for (i=0; i<=5; i++)
     {
   
        if (year == inY.options[i].value)
                {
                retval=i;       
                break;
                }       
     }
return retval;
}

//Calendar Section
//calculation functions
function nextMonth(month)
{
if (month==12)
        {
        return 1;
        }
else
        {
        return (month+1);
        }
}


function prevMonth(month) 
{
var prevMonth = (month-1)
if (month==1)
        {
        prevMonth = 12;
        }
return prevMonth
}

function changeYear(direction,month,year)
{
var theYear = year
if (direction=="next")
        {
        if (month == 12)
                {
                theYear = (year+1)
                }
        }
if (direction=="prev")
        {
        if (month == 1)
                {
                theYear = (year-1)
                }
        }
return theYear
}


function createCalendar(month,year,io) 
{
if (!isBrowserSupp())
        {
        alert("Your browser is outdated and does not support this feature")
        return;
        }
if (navigator.appVersion.indexOf("Mac",0) != -1) 
        {
        calendarWindow = window.open("","Calendar","width=175,height=205,resizable=yes,toolbar=no,status=no,menubar=no,location=no,directories=no,scrollbars=no");
        } 
else 
        {
        calendarWindow = window.open("","Calendar","width=175,height=205,resizable=no,toolbar=no,status=no,menubar=no,location=no,directories=no,scrollbars=no");
        }
        var mthIdx = month.options.selectedIndex
        var mthVal = month.options[mthIdx].value
        var yearVal = year.options[year.options.selectedIndex].value
        //call the function to populate the window
        generateCalendar(calendarWindow,mthVal,yearVal,io)
}


//generates the meat of the calendar
function generateCalendar(target,month,year,io)
// ****************************************************************
// *    Author: Geraldine Healy         Date:   25/08/1998
// *    Description:    generates the contents of the calender window
// **************************************************************** 
{
if (!isBrowserSupp())
        {
        return;
        }       

//begin table for calendar
target.document.open()
calendar = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"
calendar +="<html xmlns=\"http://www.w3.org/1999/xhtml\">"
calendar +="<html><head><title>Pubcrawl Calendar</title>"
calendar +="<link rel=\"stylesheet\" href=\"../css/screen.css\" type=\"text/css\"></head>"
calendar +="<body>"
calendar +="<table border='0' cellspacing='1' cellpadding='0' width='150' id='calendar'>"
calendar +="<tr valign='top'>"

//The parseInt function parses the string argument as a signed decimal integer. 
var mthIdx = parseInt(month);
var endday = getDaysInMonth(mthIdx, year)
var monthName = new Array ("January","February","March","April","May","June","July","August","September","October","November","December")

//month header
calendar +="<td colspan='7' align='center' class='JSCalendarTitle'>"
var index = (mthIdx-1)
calendar +="<b>" + monthName[index] + " " + year + "</b></td></tr>"
calendar +="</tr>"

//writes in the day of the week labels
calendar +="<tr align='center'>"
calendar +="<td class='JSCalendarWeek'>S</td>"
calendar +="<td class='JSCalendarWeek'>M</td>"
calendar +="<td class='JSCalendarWeek'>T</td>"
calendar +="<td class='JSCalendarWeek'>W</td>"
calendar +="<td class='JSCalendarWeek'>T</td>"
calendar +="<td class='JSCalendarWeek'>F</td>"
calendar +="<td class='JSCalendarWeek'>S</td>"
calendar +="</tr>"

wholeDate = month + "/01/" + year
thedate = new Date(wholeDate)
firstDay = thedate.getDay()

selectedmonth = mthIdx;
var today = new Date();
var thisyear = today.getYear() + 1900;
selectedyear = year

var lastDay = (endday + firstDay+1)
var lastCalspace = 42

var NoDays = (lastDay - (firstDay + 1))
var TotalDaysFilled = NoDays + firstDay;

calendar +="<tr>"
for (var i = 1; i <= lastCalspace; i++)
        {
		
        if (i <= firstDay)
                {
                // 'empty' boxes prior to first day
                calendar +="<td class='JSCalendarDays'></td>"
                }
        if (i > firstDay && i <= TotalDaysFilled)
                {
                // enter date number
                calendar +="<td align='center' class='JSCalendarDays'><a href='JavaScript:self.close();opener.closeCalendar"+io+"("+(i-firstDay) + ");'> "+(i-firstDay)+"</a></td>"
				}
		if (i > TotalDaysFilled && i <= lastCalspace) 
				{
				// 'empty' boxes after TotalDaysFilled
				calendar +="<td></td>"
				}
        //must start new row after each week
        if (i % 7 == 0 &&  i != lastCalspace)
                {
                calendar +="</tr>"
				calendar +="<tr>"
                }
        }
calendar +="</tr>"

//prev month - next month controls table
calendar +="<tr><td colspan='7' align='center' class='JSCalendarTitle'>"

//next month and previous month buttons
var goPrevMonth = prevMonth(mthIdx)
var goNextMonth = nextMonth(mthIdx)
var nextYear = changeYear("next",parseInt(month),parseInt(year))
var prevYear = changeYear("prev",parseInt(month),parseInt(year))

if(navigator.userAgent.indexOf('MSIE',0) != -1)
        {
		calendar +="<table cellpadding='0' cellspacing='0' border='0' width='100%'>"
        calendar +="<tr><td align='left' class='JSCalendarTitle'><a href='javascript:opener.generateCalendar(self,"+goPrevMonth+","+prevYear+",\""+io+"\")' class='calcontrol'>Prev</a></td>"
        calendar +="<td align='right' class='JSCalendarTitle'><a href='javascript:opener.generateCalendar(self,"+goNextMonth+","+nextYear+",\""+io+"\")' class='calcontrol'>Next</a></td></tr>"
		calendar +="</table>"
		calendar +="</td></tr>"
		calendar +="</table></body></html>"
        target.document.close()
        }
else
        {
		calendar +="<table cellpadding='0' cellspacing='0' border='0' width='100%'>"
        calendar +="<tr><td align='left' class='JSCalendarTitle'><a href='javascript:opener.generateCalendar(self,"+goPrevMonth+","+prevYear+",\""+io+"\")' class='calcontrol'>Prev</a></td>"
        calendar +="<td align='right' class='JSCalendarTitle'><a href='javascript:opener.generateCalendar(self,"+goNextMonth+","+nextYear+",\""+io+"\")' class='calcontrol'>Next</a></td></tr>"
		calendar +="</table>"
		calendar +="</td></tr>"
        calendar +="</table></body></html>"
        }
target.document.write(calendar);
target.document.close() 
}

function closeCalendar(day) {
        var yrIdx = parseYear(selectedyear,document.theForm.selYear );

        // Decrement index for day and month, because code assumes 
        // that we have an extra defaultvalue at the start.
        document.theForm.selMonth.options.selectedIndex=selectedmonth-1;
        document.theForm.selYear.options.selectedIndex= yrIdx;
        document.theForm.selDay.options.selectedIndex=parseInt(day)-1;
}

function check_input()
{
			
	// PUB needs to be entered
	// Name 
	// Email address
	// location needs to be set
	//	alert("location=" + document.theForm.item_name.value)

	// first do not allow user book day in the past
	var _day; var _month; var _year;
	var today = new Date();
		
	_year = document.theForm.selYear.options[ document.theForm.selYear.selectedIndex ].value;
  	_month = document.theForm.selMonth.options[ document.theForm.selMonth.selectedIndex ].value;
  	_day = document.theForm.selDay.options[ document.theForm.selDay.selectedIndex ].value;

	if(_year < today.getYear() || _month < today.getMonth() || (_month-1 == today.getMonth() && _day < today.getDate() ) ) { 
		alert("oops, The date your requested in the past, please enter another date, thank you");
		return false;
	}
		
	if(document.theForm.item_name.value == "") {
		alert("oops, you forgot to fill in the tour location");
		return false;
	} 

	if(document.theForm.name.value == "") {
		alert("oops, you forgot to fill in your name");
		return false;
	} else {
		document.theForm.last_name.value = document.theForm.name.value;
	}

	if(document.theForm.email.value == "") {
		alert("oops, you forgot to fill in your email");
		return false;
	}
	
	if(document.theForm.spamq.value == "") {
		alert("oops, you forgot to fill in the spam checker - please type pub");
		return false;
	}
	
	set_pp_vars()
		
	
	return true;
	
}
