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('frm_division')) { attachEvent('frm_division','onchange',send_form); }
	if (findElement('frm_range_purpose')) { attachEvent('frm_range_purpose','onchange',send_form); }
	if (findElement('frm_range_finish')) { attachEvent('frm_range_finish','onchange',send_form); }
	if (findElement('frm_range_product_1')) { attachEvent('frm_range_product_1','onchange',send_form); }
	if (findElement('frm_range_product_2')) { attachEvent('frm_range_product_2','onchange',send_form); }
	if (findElement('frm_range_product_3')) { attachEvent('frm_range_product_3','onchange',send_form); }
	if (findElement('frm_range_product_4')) { attachEvent('frm_range_product_4','onchange',send_form); }
	if (findElement('frm_search_query')) { obj = findElement('frm_search_query'); obj.old_onfocus = obj.onfocus; obj.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; }
			if (a_tags[loop].className == 'change') { a_tags[loop].onchange = send_form; }
		}
		select_tags = document.getElementsByTagName('SELECT');
		for (loop=0; loop<select_tags.length; loop++)
		{
			if (select_tags[loop].className == 'change') { select_tags[loop].onchange = send_form; }
		}
	}
}

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

function findFormElement(obj)
{
	return (document.forms.length && 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 == 'Enter keywords...') { obj.value = ''; }
	return obj.old_onfocus;
}

function clearFormElement(obj,element)
{
	if (obj.form[element])
	{
		switch(obj.form[element].type)
		{
			case "select":
			case "select-one":
			case "select-multiple":
				obj.form[element].selectedIndex = 0;
			break;

			case "checkbox":
				obj.form[element].checked = false;
			break;
			
			default:
				obj.form[element].value = '';
			break;
		}
	}
}

function resetFormElement(obj, element, value)
{
	if (obj.form[element])
	{
		switch(obj.form[element].type)
		{
			case "select":
			case "select-one":
			case "select-multiple":
				for (loop=0; loop<obj.form[element].options.length; loop++)
				{
					if (obj.form[element].options[loop].value == value) { obj.form[element].selectedIndex = loop; }
				}
			break;

			case "checkbox":
				obj.form[element].checked = false;
			break;
			
			default:
				obj.form[element].value = value;
			break;
		}
	}
}

function launch_calculator(obj)
{
	target = (this.target) ? this.target : '_blank';
	link = (this.href) ? this.href : '#';
	scrx = screen.width;
	scry = screen.height;
	
	scrw = 275;
	scrh = 250;
	
	winX = (scrx/2)-(scrw/2);
	winY = (scry/2)-(scrh/2);
	enlargementWin = open(link,target,'directories=no,location=no,menubar=no,status=no,titlebar=no,toolbar=no,scrollbars=no,width='+scrw+',height='+scrh+',left='+winX+',top='+winY+',screenX='+winX+',screenY='+winY+',resizeable=no');
	enlargementWin.moveTo(winX,winY);
	enlargementWin.focus();
	return false;
}
