var browser_dom = (document.getElementById) ? true : false

if (browser_dom) { window.onload = init; }

function init()
{
	if (findElement('print_page')) { attachEvent('print_page','onclick',print_page); }
	if (findElement('sitesrchquery')) { attachEvent('sitesrchquery','onfocus',clearSearchKeywords); }
	if (findElement('sitesrchquery_lg')) { attachEvent('sitesrchquery_lg','onfocus',clearSearchKeywords); }
	if (findElement('frm_calculator')) { initCalculator() }
	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 == 'calculator') { a_tags[loop].onclick = launch_calculator; }
		}
	}
}

function findElement(obj)
{
	return (document.getElementById(obj)) ? document.getElementById(obj) : findFormElement(obj);
}

function findFormElement(obj)
{
	return (document.forms[0][obj]) ? document.forms[0][obj] : null;
}

function findEvtObj(evt)
{
	evt = (evt) ? evt : event;
	return (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
}

function findEvtKey(evt)
{
	evt = (evt) ? evt : event;
	return (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
}

function attachEvent(obj,event_name,event_function)
{
	if (findElement(obj))
	{
		findElement(obj)[event_name] = event_function;
	}
	return true;
}

function formGetValue(obj)
{
	if (!(obj = findFormElement(obj))) { return null; }
	switch(obj.type)
	{
		case 'select-one':
			value = obj.options[obj.selectedIndex].value;
		break;

		default:
			value = obj.value;
		break;
	}
	return value;
}

function formSetValue(obj, value)
{
	if (!(obj = findFormElement(obj))) { return null; }
	switch(obj.type)
	{
		default:
			obj.value = value;
		break;
	}
}

function valNum(value)
{
	return (!isNaN(parseFloat(value))) ? parseFloat(value) : 0;
}

function valStr(value)
{
	return (typeof(value) == 'string') ? value : '';
}

function numberKeyOnly(evt)
{
	chc = findEvtKey(evt);
	return (chc > 31 && (chc < 48 || chc > 57) && chc != 46 && chc != 45) ? false : true;
}

function send_form(evt)
{
	obj = findEvtObj(evt);
	obj.form.submit();
	return false;
}

function print_page(obj)
{
	try { window.print(); }
	catch (e) { alert("Your browser does not support automatic printing - please use the 'print' command from your browser menu"); }
	return false;
}

function clearSearchKeywords(evt)
{
	obj = findEvtObj(evt);
	if (obj.value == 'Search site for...') { obj.value = ''; }
	return obj.old_onfocus;
}
