

var selectedCell = null;
var selectedCellClass = null;
var selectedMonth = null;
var existingSelectedCell = null;


function Calendar(type,period,srcurl,targetdiv,uid)
{
	this.type = type;
	this.period = period;
	this.uid = uid;
	
	this.rebuild = function(type,period,seldate)
	{
		var obajax = new ajax_XmlHttpPost(srcurl+'?period='+period+'&type='+type+'&seldate='+seldate+'&uid='+this.uid+'&nocache=1',targetdiv);
	}
	
}


function calendar_addstyleclass(id,styleclass)
{
	var elem = document.getElementById(id);
	if(elem)
		elem.className = elem.className + ' ' + styleclass;
}


function calendar_removestyleclass(id,styleclass)
{
	var elem = document.getElementById(id);
	if(elem)
		elem.className = elem.className.replace(styleclass,'');
}


function selDate(month,date,times,stock,formid,dayclasses,day)
{
	var n = times.length;
	formobj = document.getElementById(formid);
	// check to see if the current month is the same as previously
	if (selectedMonth !== null && selectedMonth !== month)
	{
		// if it isn't...
		// remove the hidden input for the previously selected date
		var existingInput = document.getElementById('seldate');
		if (existingInput != null)
		{
			var existingInputParent = existingInput.parentNode;
			existingInputParent.removeChild(existingInput);	
		}
		// reset selectedCell to null
		selectedCell = null;
		// reset selectedMonth to null
		selectedMonth = null;
	}
	
	// Check on whether time slots have been passed to the calendar.
	// If they have, do all the time slot stuff...
	if (times.length > 0)
	{
		// if there is an existing hidden stime element, remove it
		var existingInput = document.getElementById('stime');
		if (existingInput != null)
		{
			var existingInputParent = existingInput.parentNode;
			existingInputParent.removeChild(existingInput);	
		}	
	
		// If there is an existing time slots dropdown, remove it
		var timeSlotsDiv = document.getElementById('timeslots');
		if (timeSlotsDiv.innerHTML.length > 0)
		{
			timeSlotsDiv.innerHTML = "";
		}
		
		// Create the timeslots dropdown for the date selected
		var theNewSelect = document.createElement('select');
		theNewSelect.setAttribute('name','seltime');
		theNewSelect.setAttribute('id','seltime');
		theNewSelect.onchange = function()
		{
			selTime(formid);
		};
		
		var theNewOption = document.createElement('option');
		theNewOption.setAttribute('value',0);
		var theNewText = document.createTextNode('Select Time');
		theNewOption.appendChild(theNewText);
		theNewSelect.appendChild(theNewOption);
		
		for (i=0;i<n;i++)
		{
			var theNewOption = document.createElement('option');
			theNewOption.setAttribute('value',times[i]+':00');
			var theNewText = document.createTextNode(times[i]);
			theNewOption.appendChild(theNewText);
			theNewSelect.appendChild(theNewOption);
		}
		// formobj.appendChild(theNewSelect);
	
		timeSlotsDiv.appendChild(theNewSelect);
		
		// timeSlotsDiv.appendChild(theNewSelect);
		// formobj.appendChild(timeSlotsDiv);
	}
	
	// Check to see if there is an existing hidden date input
	var existingInput = document.getElementById('seldate');
	if (existingInput != null)
	{
		// If there is, update the value
		existingInput.setAttribute('value',date);
	}
	else
	{
		// If there isn't, create a hidden input for the selected date
		var theNewInput = document.createElement('input');
		theNewInput.setAttribute('type','hidden');
		theNewInput.setAttribute('id','seldate');
		theNewInput.setAttribute('name','seldate');
		theNewInput.setAttribute('value',date);
		formobj.appendChild(theNewInput);
	}
	
	// check to see if there is an existing selected cell, remove the selected class if so 

	if (selectedCell != null || day.length > 0)
	{
		if (day.length > 0 & existingSelectedCell == null)
		{
			selectedCell = 'daycell'+day;
			selectedCellClass = day;
			existingSelectedCell = 1;
		}
	
		var previousSelectedCellClass = dayclasses[parseInt(selectedCellClass,10)-1];
		if (previousSelectedCellClass == 1)
		{
			document.getElementById(selectedCell).className = 'daylink';
		}
		else
		{
			document.getElementById(selectedCell).className = 'daynone';
		}
	}
	
	var selectedDay = date.substring(8,10);
	
	// highlight the selected cell
	document.getElementById('daycell'+selectedDay).className += ' selected';
	
	// set the selectedCell variable to be the chosen cell
	selectedCell = 'daycell'+selectedDay;
	
	// set the selectedCellClass to the selected day (using a base of 10 to stop javascript bug which changes a string of 08 to 0 and string of 09 to 0)
	selectedCellClass = parseInt(selectedDay,10);
	
	// set the selectedMonth variable to the current month
	selectedMonth = month;
	
	// alert('selectedCell is '+selectedCell+' and selectedMonth is '+selectedMonth);
	
}

function selTime(formid)
{
	var stime = document.getElementById('seltime').value;

	// Check to see if there is an existing hidden time input
	formobj = document.getElementById(formid);
	var existingInput = document.getElementById('stime');
	if (existingInput != null)
	{
		// If there is, update the value
		existingInput.setAttribute('value',stime);
	}
	else
	{
		// If there isn't, create a hidden input for the selected time
		var theNewInput = document.createElement('input');
		theNewInput.setAttribute('type','hidden');
		theNewInput.setAttribute('id','stime');
		theNewInput.setAttribute('name','stime');
		theNewInput.setAttribute('value',stime);
		formobj.appendChild(theNewInput);
	}
}