window.onload = init;

function init()
{
	if (document.getElementsByTagName('A'))
	{
		a_tags = document.getElementsByTagName('A');
		for (loop=0; loop<a_tags.length; loop++)
		{
			if (a_tags[loop].className == 'external') { a_tags[loop].target = '_blank'; }
			if (a_tags[loop].className == 'vr') { a_tags[loop].onclick = launchVR; }
		}
	}
}

function getElement(obj)
{
	if (document.getElementById) { return document.getElementById(obj); }
	if (document.all) { return document.all[obj]; }
	if (document.layers) { return getLayer(obj, document); }
}

function submitForm()
{
	if (typeof(tinyMCE) != 'undefined')
	{
		tinyMCE.triggerSave(false);
	}
	if (document.forms[0].onsubmit)
	{
		if (!document.forms[0].onsubmit()) { return false; }
	}
	document.forms[0].submit();
	return false;
}

function calendarCallback(date, month, year, formID)
{
	regexp = /[\[\]]+/gi;
	element = getElement('cal_val_'+formID);
	safename = formID.replace(regexp,'_');
	if (element)
	{
		getElement('calendar_'+safename).innerHTML = ''; // Be nice to IE5.x for Mac
		getElement('calendar_'+safename).innerHTML = constructDate(date,month,year,"dd mmm, yyyy");
		if (getElement('chk_'+safename).type == 'checkbox') { getElement('chk_'+safename).checked = true; }
		element.value = constructDate(date,month,year,"yyyy-mm-dd");
	}
}

function constructDate(d, m, y, format)
{
	monthName =	new Array("January","February","March","April","May","June","July","August","September","October","November","December")
	sTmp = format
	sTmp = sTmp.replace ("dd","<e>")
	sTmp = sTmp.replace ("d","<d>")
	sTmp = sTmp.replace ("<e>",padZero(d))
	sTmp = sTmp.replace ("<d>",d)
	sTmp = sTmp.replace ("mmm","<o>")
	sTmp = sTmp.replace ("mm","<n>")
	sTmp = sTmp.replace ("m","<m>")
	sTmp = sTmp.replace ("<m>",m)
	sTmp = sTmp.replace ("<n>",padZero(m))
	sTmp = sTmp.replace ("<o>",monthName[m-1])
	return sTmp.replace ("yyyy",y)
}

function padZero(num) {
	return (num	< 10)? '0' + num : num ;
}

function toggleDateChkBox(element,text) {
	if (getElement('chk_'+element) && getElement(text))
	{
		getElement('chk_'+element).checked = false;
		getElement('cal_val_'+element).value = '';
		getElement(text).innerHTML = '-';
	}
}

function toggleView(element, div, msg)
{
    flag = (getElement(element).innerHTML == 'Show '+msg);
    getElement(element).innerHTML = (flag) ? 'Hide '+msg : 'Show '+msg;
    getElement(div).style.display = (flag) ? 'block' : 'none';
    return false;
}

