var is_ie = document.all;
var onloadFunctions = [];
var mouseXpos = 0;
var mouseYpos = 0;

document.onmousedown = getMousePos;

function getMousePos ( e )
{
	mouseXpos = is_ie ? event.clientX + document.body.scrollLeft : e.pageX;
	mouseYpos = is_ie ? event.clientY + document.body.scrollTop : e.pageY;
	return true;
}

function addOnLoadFunc ( onloadFunction )
{
    onloadFunctions[onloadFunctions.length] = onloadFunction;

    window.onload = function ( )
    {
        for ( var i = 0; i < onloadFunctions.length; i++ )
        {
            onloadFunctions[i]();
        }
    }
}

function getObj ( id )
{
	return document.getElementById ( id );
}

function togView ( id )
{
	var el = getObj ( id );
	if ( el ) el.style.display = el.style.display == 'none' ? '' : 'none';
}

function checkIt ( chkBox, chkd )
{
	if ( chkBox ) chkBox.checked = chkd;
}

function showIt ( id, visible )
{
	var x = getObj ( id );
	if ( x ) x.style.display = ( visible ? 'block' : 'none' );
}


function checkAllBoxes ( formObj, fieldName, chkVal )
{
	if ( !formObj ) return false;

	for ( var i = 0; i < formObj.elements.length; i++ )
	{
		if ( formObj.elements[i].type == 'checkbox' && formObj.elements[i].name == fieldName && formObj.elements[i].disabled == false )
		{
			checkIt ( formObj.elements[i], chkVal );
		}
	}
}

function countCheckedBoxes ( formObj, fieldName )
{
	if ( !formObj ) return false;
	var count = 0;
	for ( var i = 0; i < formObj.elements.length; i++ )
	{
		count += ( formObj.elements[i].type == 'checkbox' && formObj.elements[i].name == fieldName && formObj.elements[i].checked ) ? 1 : 0;
	}
	return count;
}

function adjustRatio ( event, current, target, ratio, maxVal )
{
	if ( current.value != '' ) current.value = parseInt ( current.value );
	if ( current.value == 'NaN' ) current.value = 0;
	if ( current.value > maxVal ) current.value = maxVal;
	if ( current.value < 0 ) current.value = 0;
	target.value = Math.ceil ( current.value / ratio );
}

function pInt ( fieldObj )
{
	if ( fieldObj )
	{
		fieldObj.value = parseInt ( fieldObj.value );
		if ( fieldObj.value == 'NaN' )
			fieldObj.value = 0;
	}
}

function copyTextArea ( id )
{
	var field = getObj ( id );
	if ( field )
	{
		if ( is_ie ) field.createTextRange ( ).execCommand ( 'copy' );
		else alert ( 'Sorry, this feature is only enabled for Internet Explorer at the moment. Select the text and press Ctrl+C to copy and Ctrl+V to paste.' );
	}
}

function pop ( url )
{
	return !window.open ( url );
}

function go ( url )
{
	window.location = url;
	return false;
}

function help ( str )
{
	var x = getObj ( 'message_content' );
	if ( x ) x.innerHTML = str;
	showIt ( 'message', true );
}


function getXMLHttpObject ( )
{
	var xmlhttp = false;
	try { xmlhttp = new XMLHttpRequest ( ); }
	catch ( e ) { try { xmlhttp = new ActiveXObject ( 'Microsoft.XMLHTTP' ); } catch ( e ){} }
	return xmlhttp;
}

function deleteNode ( oNode )
{
	return oNode.parentNode.removeChild ( oNode );
}

function alternateRowColor ( obj, el, color1, color2 )
{
	if ( obj )
	{
		var els = obj.getElementsByTagName ( el );
		for ( var i = 0; i < els.length; i++ ) if ( !els[i].getAttribute ( 'skip_alternate' ) ) els[i].style.backgroundColor = ( i & 1 ? color1 : color2 );
	}
}