//-------------------------------------------------------------------------------
//                               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;
}

/////////////////////////////////////////////////////////////////////////////////
// Check number value
function isNum(strValue)
{
	return !isNaN(strValue * 1);
}

/////////////////////////////////////////////////////////////////////////////////
// Check integer (0-9) value
function isInt(strValue)
{
	strValue = trim(strValue);
	for (var i=0; i < strValue.length; i++)
	{
		var ch = strValue.substring(i,i+1);
		if ("0123456789".indexOf (ch) < 0)
			return false;
	}
	return true;
}

/////////////////////////////////////////////////////////////////////////////////
function isEmail(email)
{
	var posAtSymbol = email.indexOf('@');
	var posLastDot = email.lastIndexOf('.');

	if (posAtSymbol < 1) return false;
	if (posLastDot < posAtSymbol) return false;
	if (email.length - posLastDot <= 2) return false;
	if (email.indexOf(' ') != -1)  return false;

	return true;
}

/////////////////////////////////////////////////////////////////////////////////
function isEmpty(theString)
{
	for (var i=0; i < theString.length; i++)
	{
		var ch = theString.substring(i,i+1);
		if (ch != " " && ch != "\t" && ch != "\n" && ch != "\r")
			return false;
	}
	return true;
}

/////////////////////////////////////////////////////////////////////////////////
// ltrim: removes blanks from head of string
function ltrim(theString)
{
	var ch;
	while (theString.length > 0) {
		ch = theString.substring(0,1);
		if (ch != " " && ch != "\t" && ch != "\n" && ch != "\r") break;
		theString = theString.substring(1, theString.length);
	}
	return theString;
}

/////////////////////////////////////////////////////////////////////////////////
// rtrim: removes blanks from tail of string
function rtrim(theString)
{
	var ch;
	while (theString.length > 0) {
		ch = theString.substring(theString.length-1,theString.length);
		if (ch != " " && ch != "\t" && ch != "\n" && ch != "\r") break;
		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: left pad a string with a character
function lpad(theString, theChar, Max)
{
	while (theString.length < Max)
		theString = theChar + theString;
	return theString;
}

/////////////////////////////////////////////////////////////////////////////////
// Display an error and set focus to formfield in error
function errorField (objElem, txtMsg)
{
	alert (txtMsg);
	if (objElem != null)
		objElem.focus();
}

/////////////////////////////////////////////////////////////////////////////////
// Check mandatory field
function checkMandatory(field, errmsg)
{
	if (isEmpty(field.value))
	{
		errorField(field, errmsg);
		return false;
	}
	return true;
}

/////////////////////////////////////////////////////////////////////////////////
// Popup a window
function popup(theURL,winName,features) 
{
	var hWin = window.open(theURL,winName,features);
	hWin.focus();
}

/////////////////////////////////////////////////////////////////////////////////
// Select option if value match 
function selOption (objSelect, strMatch)
{
	for (var i=0; i < objSelect.options.length; i++)
	{
		var elem = objSelect.options[i];
		if ( elem.value ==  strMatch)
			elem.selected = true;
	}
}

/////////////////////////////////////////////////////////////////////////////////
// Get value of a particular checked radiobutton
function getRadioChecked(objRadioGroup)
{
	if (objRadioGroup.length == undefined) { // check if only 1 radiobutton in group
		if (objRadioGroup.checked)
			return objRadioGroup.value;
		else
			return "";
	}

	for (var i=0; i < objRadioGroup.length; i++)
	{
		var elem = objRadioGroup[i];
		if ( elem.checked )
			return elem.value;
	}
	return "";
}

/////////////////////////////////////////////////////////////////////////////////
// Get/Set Cookies

function setCookie(name,value) {
        setCookieExp(name, value, 31536000000); // 365 dagen in milliseconds
}

function setCookieExp(name,value,Expires) {
        var now = new Date();
        var then = new Date(now.getTime()+Expires);
        document.cookie = name+"="+escape(value) +"; expires="+then.toGMTString()+"; path=/";
}

function getCookie(name) {
	var cookie = " "+document.cookie;
	var search = " "+name+"=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset);
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset,end));
		}
	}
	return(setStr);
}
