    //Global dates
    var viewDate = new Date ();
    var selectedDate = null;
    var selectedIndex = -1;
    var todayIndex    = -1;
    
    //Enable another function to be called when a date is selected.
    var selectionFunction = null;
    
    function getMonthArray () {
        
    	var month=new Array(12);
    	month[0]="January";
    	month[1]="February";
    	month[2]="March";
    	month[3]="April";
    	month[4]="May";
    	month[5]="June";
    	month[6]="July";
    	month[7]="August";
    	month[8]="September";
    	month[9]="October";
    	month[10]="November";
    	month[11]="December";
    	
    	return month;        
    }
    
    function setHeaderToMonth (date) {

    	//Sunday = 0 ... Saturday = 6
    	var day = date.getDay();
    	var month = date.getMonth ();
    	var year = date.getFullYear();

    	//Use the DIV identifier to set the HTML.
    	$('#calendar_month').html(getMonthArray()[month]+"&nbsp;"+year);
        
    }

    function getFirstDayOfMonthAsDate (date) {
        //Return 1st day of current year and month, @ midnight
        var result = new Date (date.getFullYear(), date.getMonth(), 1, 0, 0, 0, 0);
        return result;
    }

    function getNextDay (date) {
        var result = new Date (date.getFullYear(), date.getMonth(), date.getDate()+1, 0, 0, 0, 0);
        return result;		    	
    }

    function areDatesSameMonth (date1, date2) {
        if (date1.getMonth() != date2.getMonth ()) {
            return false;
        }
        return true;
    }
    
    function displayCalendarForMonth (date) {

    	setHeaderToMonth (date);

    	firstDayOfMonth =  getFirstDayOfMonthAsDate (date);
    	firstDayOfWeek  = firstDayOfMonth.getDay();

    	startDivIndex = 1 + firstDayOfWeek;  

    	thisDate = firstDayOfMonth;

		//Blank out previous month days
		i = 0;
		for (i=0; i < startDivIndex; i++) {
			id = "#day" + i;
			$(id).html("&nbsp;");
			$(id).attr ('class','day cal_no_day');
		}
				
    	//Fill in month
    	doNextDay = true;
    	while (doNextDay) {

    		id = "#day" + startDivIndex;

    		html = thisDate.getDate();
    		html = "<a href=\"javascript:selectDate('"+thisDate.getDate()+"','"+startDivIndex+"');\">"+html+"</a>";
    		$(id).html(html);
    		
			if (isDateToday (thisDate)) {
				//This is the current date
				$(id).attr('class','day cal_today');
				//Also set the div index for today, for use in selection
				todayIndex = id;
			}
			else {
				//Just another day in the month.
				$(id).attr('class','day cal_other');
			}

			//Toggle selection as well			
			if (selectedIndex > -1) {
	        	uid = "#day"+selectedIndex;
				if (thisDate.getDate() == selectedDate.getDate() &&
				    thisDate.getMonth() == selectedDate.getMonth() &&
			    	thisDate.getFullYear () == selectedDate.getFullYear()) {
		        	$(uid).attr ('class','day cal_selected');  							    	
			    }
			}
						
        	//Move onto next day      	
        	startDivIndex++;

        	thisDate = getNextDay (thisDate); 
        	
            doNextDay = areDatesSameMonth (thisDate, date)
        	
    	}

		//Blank out next month
		i1 = 0;
		for (i1=startDivIndex; i1 <= 42; i1++) {
			id = "#day" + i1;
			$(id).html("&nbsp;");
			$(id).attr ('class','day cal_no_day');
		}    	
    }

    function selectNextMonth () {
        //Change view date to next month      
        viewDate = new Date (viewDate.getFullYear(),viewDate.getMonth()+1,1,0,0,0,0);
        displayCalendarForMonth (viewDate);
    }

    function selectPreviousMonth () {
        //Change view date to previous month      
        viewDate = new Date (viewDate.getFullYear(),viewDate.getMonth()-1,1,0,0,0,0);
        displayCalendarForMonth (viewDate);        
    }

    function isDateToday (date) {
        return isSameDay (date, new Date());
    }
    
    function isSameDay (date1, date2) {
         if (date1.getFullYear() == date2.getFullYear() &&
             date1.getMonth() == date2.getMonth() &&
             date1.getDate() == date2.getDate()) {
             return true;
         }
         return false;                 
    }

    function isDateThisMonth (date) {
        var today = new Date();
        if (today.getFullYear() == date.getFullYear() &&
        	today.getMonth() == date.getMonth()) 
        	{ 
                return true;
            }
            return false;          
    }
    
    function selectDate (day, index) {
        
    	if (selectedIndex > -1) {
        	uid = "#day"+selectedIndex;
    		$(uid).attr ('class','day');
    	    if (isDateToday (selectedDate)) {
    	    	$(uid).attr ('class','day cal_today');
    	    }
    	}
        selectedDate = new Date (viewDate.getFullYear(), viewDate.getMonth(), day, 0,0,0,0); 
   			
        display = selectedDate.getDate()+"&nbsp;"+getMonthArray()[selectedDate.getMonth()]+"&nbsp;"+selectedDate.getFullYear();
        $("#selected").html(display);

        selectedIndex = index;
        id = "#day"+index;
        $(id).attr ('class','day cal_selected');
        
        //Call custom function to enable trapping of selections
        if (selectionFunction != null) {
        	selectionFunction (selectedDate);
        }
        
    }
    
    // **************************************************************************************
    
    function initialiseCalendar () {
    	//This is the date (month/year/day etc.) that the calendar will be displayed for.
    	//This can change if the user navigates to another month for example
    	displayCalendarForMonth (viewDate);
    	//$('#day2').css('background-color','#ff0000');
    }
	
