document.writeln('<style type="text/css">');
document.writeln('div#weekly_options { display:none; }');
document.writeln('div#monthly_options { display:none; }');
document.writeln('div#yearly_options { display:none; }');
document.writeln('div#repeat_options { display:none; }');
document.writeln('</style>');

function set_display(el_id, display)
{
	display = display || 'block';
	if (el = document.getElementById(el_id)) {
		el.style.display = display;
		return true;
	}
	return false;
}

function set_enabled(el_id)
{
	if (el = document.getElementById(el_id)) {
		el.disabled = false;
	}
}

function set_disabled(el_id)
{
	if (el = document.getElementById(el_id)) {
		el.disabled = true;
	}
}

function disable_repeat()
{
	set_display('repeat_options', 'none');
	set_disabled('interval');
	set_disabled('frequency');
}

function enable_repeat()
{
	set_enabled('interval');
	set_enabled('frequency');
	set_display('repeat_options', 'block');
}

function change_repeat_type(repeat_sel)
{
	if (repeat_sel.value == 'DAILY') {
		set_display('weekly_options', 'none');
		set_display('monthly_options', 'none');
		set_display('yearly_options', 'none');
	}

	if (repeat_sel.value == 'WEEKLY') {
		set_display('weekly_options', 'block');
		set_display('monthly_options', 'none');
		set_display('yearly_options', 'none');
	}

	if (repeat_sel.value == 'MONTHLY') {
		set_display('weekly_options', 'none');
		set_display('monthly_options', 'block');
		set_display('yearly_options', 'none');
	}

	if (repeat_sel.value == 'YEARLY') {
		set_display('weekly_options', 'none');
		set_display('monthly_options', 'none');
		set_display('yearly_options', 'block');
	}
}

function change_monthly_option(monthly_radio)
{
	if (monthly_radio.value == 'bymonthday') {
		set_enabled('monthly_bymonthday');
		set_disabled('monthly_bydayord');
		set_disabled('monthly_byday');
	}
	if (monthly_radio.value == 'byday') {
		set_enabled('monthly_byday');
		set_enabled('monthly_bydayord');
		set_disabled('monthly_bymonthday');
	}
}

function change_yearly_byday(byday_checkbox)
{
	if (byday_checkbox.checked) {
		set_enabled('yearly_bydayord');
		set_enabled('yearly_byday');
	} else {
		set_disabled('yearly_bydayord');
		set_disabled('yearly_byday');
	}
}

function change_repeat_end(end_radio)
{
	if (end_radio.value == 'never') {
		set_disabled('repeat_until');
		set_disabled('repeat_count');
	}
	if (end_radio.value == 'until') {
		set_enabled('repeat_until');
		set_disabled('repeat_count');
	}
	if (end_radio.value == 'count') {
		set_disabled('repeat_until');
		set_enabled('repeat_count');
	}
}

function setup_repeat_elements()
{
	var freq;
	var el;

	// if we have any repeat fields filled out...
	if ((el = document.getElementById('repeat_yes')) && el.checked) {
		// unhide repeat_options div
		enable_repeat();

		// unhide the options div for the selected frequency
		freq = document.getElementById('frequency');
		change_repeat_type(freq);

	} else {
		disable_repeat();
	}

	// nothing to disable in daily and weekly options

	// disable inputs in monthly options
	if ((el = document.getElementById('monthly_type_bymonthday')) && !el.checked) {
		set_disabled('monthly_bymonthday');
	}
	if ((el = document.getElementById('monthly_type_byday')) && !el.checked) {
		set_disabled('monthly_bydayord');
		set_disabled('monthly_byday');
	}

	// disable inputs in yearly options
	if ((el = document.getElementById('yearly_byday_check')) && !el.checked) {
		set_disabled('yearly_bydayord');
		set_disabled('yearly_byday');
	}

	// disable inputs in ending options
	if ((el = document.getElementById('repeat_end_type_never')) && el.checked) {
		set_disabled('repeat_until');
		set_disabled('repeat_count');
	}
	if ((el = document.getElementById('repeat_end_type_until')) && el.checked) {
		set_disabled('repeat_count');
	}
	if ((el = document.getElementById('repeat_end_type_count')) && el.checked) {
		set_disabled('repeat_until');
	}

}

function set_address(location)
{
	var xmlhttp=false;

	// Native XMLHttpRequest
	if (window.XMLHttpRequest) {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch(e) {
			xmlhttp = false;
		}

	// IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		try {
			xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
		} catch (e) {
			try {
				xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
			} catch (E) {
				xmlhttp = false;
			}
		}
	}

	if (xmlhttp) {
		xmlhttp.onreadystatechange = function() {
			// If stuff has returned, and it was an OK response
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				var el;
				if (el = document.getElementById('X-OSU-ADDRESS')) {
					new_addr = getElementTextNS('', 'address', xmlhttp.responseXML, 0);
					if(new_addr) {
						el.value = new_addr;
					}
				}
				if (el = document.getElementById('X-OSU-CITY')) {
					new_city = getElementTextNS('', 'city', xmlhttp.responseXML, 0);
					if(new_city) {
						el.value = new_city;
					}
				}
				if (el = document.getElementById('X-OSU-STATE')) {
					new_state = getElementTextNS('', 'state', xmlhttp.responseXML, 0);
					if(new_state) {
						el.value = new_state;
					}
				}
			}
		}
		// @todo: find a way to pull this url from config?
		// or simply change it to production install at some point
		xmlhttp.open('GET', 'http://calendar.oregonstate.edu/mapapi.php?action=getLocation&name='+escape(location), true);
		xmlhttp.send('');
	}
}
/* vim:set noexpandtab tabstop=4 sw=4: */
