if((document.all || document.layers) && !document.getElementById)
{
	document.getElementById = function(id)
	{
		if (document.all) return document.all[id]
		else return document.layers[id];
	}
}

function isLeapYear(year)
{
	return new Date(year, 1, 29).getDate() == 29;
}

function adjustDays(pref)
{
	var setDay = 1;
	var day = pref + "day";
	var month = pref + "month";
	var year = pref + "year";
	var themonth = $("select#" + month).val();	
	//var selectedMonth = document.getElementById(month).selectedIndex;
	//var themonth = document.getElementById(month).options[selectedMonth].value;
	//var selectedYear = document.getElementById(year).selectedIndex;
	//var theyear = document.getElementById(year).options[selectedYear].value;
	if(themonth == 5 || themonth == 8 || themonth == 10) themonth = 3;
	switch(parseInt(themonth))
	{
		case 1:
			if(isLeapYear($("select#" + year).val()))
			//if(isLeapYear(theyear))
			{
				removeDays();
				addDays(1);
			}
			else removeDays();
			break;
		case 3:
			removeDays();
			addDays(2);
			break;
		default:
			
			removeDays();
			addDays(3);
			
	}
	function removeDays() {
		setDay = $("#" + day).val();
		var optionSel = "#" + day + " option"				
		$(optionSel + "[@value=29]," + optionSel + "[@value=30]," + optionSel + "[@value=31]").remove();
	}
	function addDays(plusdays) {
	 switch(plusdays) {
	 	case 1 : // add 29th
		 	$("#" + day).append('<option value="29">29</option>');
			 $("#" + day).val(setDay);
			 break;
		case 2 : // add 29th and 30th
			$("#" + day).append('<option value="29">29</option>');
			$("#" + day).append('<option value="30">30</option>');
			$("#" + day).val(setDay);
			break;
		case 3 : // add 29th, 30th and 31st
			$("#" + day).append('<option value="29">29</option>');
			$("#" + day).append('<option value="30">30</option>');
			$("#" + day).append('<option value="31">31</option>');
			$("#" + day).val(setDay);
			break;
		default:
			$("#" + day).append('<option value="29">29</option>');
			$("#" + day).append('<option value="30">30</option>');
			$("#" + day).append('<option value="31">31</option>');
			$("#" + day).val(setDay);
	 }
	}
}


function EventDate(day, month, year)
{
	this.dayId = day;
	this.monthId = month;
	this.yearId = year;
	this.selectedDay = document.getElementById(this.dayId).selectedIndex;
	this.day = document.getElementById(this.dayId).options[this.selectedDay].value;
	this.selectedMonth = document.getElementById(this.monthId).selectedIndex;
	this.month = document.getElementById(this.monthId).options[this.selectedMonth].value;
	this.selectedYear = document.getElementById(this.yearId).selectedIndex;
	this.year = document.getElementById(this.yearId).options[this.selectedYear].value;
}

EventDate.prototype.getEventDate = function()
{
	var thedate =  new Date(this.year, this.month, this.day);
	return thedate;
}

EventDate.prototype.setTo = function(date)
{
	document.getElementById(this.dayId).selectedIndex = date.selectedDay;
	document.getElementById(this.monthId).selectedIndex = date.selectedMonth;
	document.getElementById(this.yearId).selectedIndex = date.selectedYear;
}

function adjustStartDate()
{
	var sdate = new EventDate("sday", "smonth", "syear");
	var edate = new EventDate("eday", "emonth", "eyear");
	if(sdate.getEventDate() > edate.getEventDate()) sdate.setTo(edate);
	adjustDays("s");
}

function adjustEndDate()
{
	var sdate = new EventDate("sday", "smonth", "syear");
	var edate = new EventDate("eday", "emonth", "eyear");
	if(sdate.getEventDate() > edate.getEventDate()) edate.setTo(sdate);
	adjustDays("e");
}

function showOnly(toshow, all)
{
	var divstoshow = (typeof toshow != "object") ? [toshow] : toshow;
	
	for (var i = 0; i < all.length; i++)
	{
		if($("#" + all[i])) $("#" + all[i]).hide();
	}
	for (var j = 0; j < divstoshow.length; j++)
	{
		if($("#" + divstoshow[j])) $("#" + divstoshow[j]).show();
	}
	
	if($('#showsingle'))
	{
		if($('#showsingle').attr('checked') == true)
		{
			$('#startDate legend').empty().append("Date");
			$('#endDate').hide();
		}
		else
		{
			$('#startDate legend').empty().append("Start Date");
			$('#endDate').show();
		}
	}
	
}


function changeSup(id)
{
	$("#" + id).next().empty();
	var value = $("#" + id).val()
	if(value && !isNaN(value))
	{
		var absvalue = Math.abs(value);
		if(isMultiple(absvalue, 1)) $("#" + id).next().append("st")
		else if(isMultiple(absvalue, 2)) $("#" + id).next().append("nd")
		else if(isMultiple(absvalue, 3)) $("#" + id).next().append("rd")
		else $("#" + id).next().append("th");
	}
	else $("#" + id).next().append("th");
		
	function isMultiple(int1, int2)
	{
		return (int1 == int2 || (int1 > int2 + 10 && (int1 - int2) % 10 == 0))
	}
}


function removeCRs()
{
	var text = $('#memLongDescription').val();
	if(text)
	{
		// Convert MS-DOS/Windows newlines to UNIX format
		text = text.replace(/\r\n/g, "\n");
		// Convert Mac OS 9- newlines to UNIX format
		text = text.replace(/\r/g, "\n");
		// Now we only have \n's.
		// Replace single \n's with spaces
		text = text.replace(/([^\n]+)\n{1}(?!\n)/g, "$1 ");
		// Replace the value in the textarea
		$('#memLongDescription').val(text);
	}
}


var eventTypes = new Array('single', 'ongoing', 'recurrent');
var recurrenceTypes = new Array('daily', 'weekly', 'monthly');


function enableVenueOption(intOption)
{
	
 	switch(intOption)
	 {
	 	case 1: 
		 	document.getElementById("txtVenueName").disabled = true;
			document.getElementById("intVenueID").disabled = false;
			break;
	
	 	case 2: 
		 	document.getElementById("txtVenueName").disabled = false;
			document.getElementById("intVenueID").disabled = true;
			break;
	}
}