//	''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
//	'' DESCR:	Javascript Utilities
//	'' AUTHOR:	Herbert Govaerts
//	'' CREATION:	nov 2006			  copyright 2006 'web etcetera'
//	''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


//-------------------------------------------------------------------------------
//                               U T I L I T I E S
//-------------------------------------------------------------------------------

		
/////////////////////////////////////////////////////////////////////////////////
// Check alphanumeric (A-Z,a-z,0-9,_ or space) value
var strAlpaNum = " _abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

function isAlpaNum(strValue)
{
	for (var i=0; i < strValue.length; i++)
	{
		if (strAlpaNum.indexOf(strValue.substring(i,i+1)) < 0)
			return false;
	}
	return true;
}

/////////////////////////////////////////////////////////////////////////////////
function isEmpty(theString)
{
	for (var i=0; i < theString.length; i++)
	{
		if (theString.substring(i,i+1) != " ")
			return false;
	}
	return true;
}

/////////////////////////////////////////////////////////////////////////////////
// ltrim: removes blanks from head of string
function ltrim(theString)
{
	while (theString.length > 0 && theString.substring(0,1) == " ")
		theString = theString.substring(1, theString.length);
	return theString;
}

/////////////////////////////////////////////////////////////////////////////////
// rtrim: removes blanks from tail of string
function rtrim(theString)
{
	while (theString.length > 0 && theString.substring(theString.length-1,theString.length) == " ")
		theString = theString.substring(0, theString.length-1);
	return theString;
}

/////////////////////////////////////////////////////////////////////////////////
// trim: removes blanks from both head and tail of string
function trim(theString)
{
	return ltrim (rtrim (theString));
}

/////////////////////////////////////////////////////////////////////////////////
// lpad: fill a string at leftside with 'ch' for until 'len'
function lpad(theString, ch, len)
{
	while (theString.length < len) theString = ch + theString;
	return theString;
}

/////////////////////////////////////////////////////////////////////////////////
// Display an error and set focus to formfield in error
function errorField (objElem, txtMsg)
{
	if (objElem != null)
		objElem.focus();
	alert (txtMsg);
}

/////////////////////////////////////////////////////////////////////////////////
// Display an error and set focus to formfield in error if field is empty
function checkMandatory (objElem, txtMsg)
{
	if (isEmpty(objElem.value)) {
		errorField(objElem, txtMsg);
		return false;
	}
	return true;
}

/////////////////////////////////////////////////////////////////////////////////
// Target=_blank
function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") &&
			anchor.getAttribute("rel") == "ext")
		anchor.target = "_blank";
    }
}
