
function GlobalOnLoad()
{
	// if preload exists as a function, call it. 
	// this avoids having force an empty preload() function for pages that don't need it. 
	if (typeof preload=="function")
	{
		preload();		
	}
}


/*
* Changes the value of pageAction hidden field if it exists to the given value
* param string action
*/
function setPageAction(action)
{
	if(document.getElementById('pageAction'))
	{
		document.getElementById('pageAction').value = action;
		
		return true; 
	}
	return false; 
}

/*
* Duplicate of the above but with second parameter to immediatly submit the global form. 
* @param string newVal
* @param bool submitFlag
*/
function changePageActionVal(newVal, submitFlag)
{
	if(document.getElementById('pageAction'))
	{
		document.getElementById('pageAction').value = newVal;	
		if (submitFlag)
		{
			submitForm('global');
		}
	}
	return;
}

/*
* Changes the action of the global form so that it submits to another page
* param string $action
*/
function setFormAction(page, submitFlag)
{
	if(document.getElementById('global'))
	{
		document.getElementById('global').action = page;
		if (submitFlag)
		{
			submitForm('global');
		}
	}
	return; 
}

function changePageActionVal(newVal, submitFlag)
{
	if(document.getElementById('pageAction'))
	{
		document.getElementById('pageAction').value = newVal;	
		if (submitFlag)
		{
			submitForm('global');
		}
	}
	return;
}

/*
* Submits a form by ID
* param string formID
*/
function submitForm(formID)
{
	if(document.getElementById(formID))
	{
		document.getElementById(formID).submit();
		return true; 
	}
	return false; 
}



/******* TOGGLE DISPLAY OF AN ITEM **********/

/*
* Toggles an element's display value from block to none by its id
* param string id
*/
function toggleDisplayElement(id)
{
	if(document.getElementById(id))
	{
		elem = document.getElementById(id); 
		
		if(elem.style.display == "block")
		{
			elem.style.display = "none";
			return true; 
		}
		else
		{
			elem.style.display = "block";
			return true; 
		}
	}
	return false; 
}


/*************** VALIDATION AND DATE FUNCTIONS *************/

function validateZipCode(id)
{
	if(document.getElementById(id))
	{
		zip = document.getElementById(id).value; 
		
		reZip = new RegExp(/(^\d{5}$)/);

		if (reZip.test(zip)) 
		{
			return true;
        }
        alert("Zip Code Is Not Valid!");
	}
	return false; 

}

/**************** CHECK MARK ALL checkboxes ***************/

function checkAll(field, checkFlag)
{
	if(!checkFlag)
	{
		for (i = 0; i < field.length; i++)
		field[i].checked = false ;
		checkFlag = false; 	
	}
	else
	{
		for (i = 0; i < field.length; i++)
		field[i].checked = true ;
		checkFlag = true; 	
	}

}


function setSort(orderBy, sortDir, submitFlag)
{
	if(document.getElementById('orderByField') && document.getElementById('sortDirField'))
	{	
		document.getElementById('orderByField').value = orderBy;
		document.getElementById('sortDirField').value = sortDir;
		
		if(submitFlag)
		{
			submitForm('global');
		}
	}
	else
	{
		alert("Error: Sort Failed!");
	}

}


/** 
* Used to hide mail addresses
*/
function protect(name, address, display)
{
	var link = name + "@" + address
	if(!display) { display = link; }
	document.write("<a href='mailto:" + link + "'>" + display + "</a>");
}