// COMMON PURPOSE Javascript FUNCTIONS

// String trimming
function ltrim(str){return typeof(str)=='string' ?str.replace(/^\s+/,'') :str;}
function rtrim(str){return typeof(str)=='string' ?str.replace(/\s+$/,'') :str;}
function trim(str) {return typeof(str)=='string' ?str.replace(/^\s+/,'').replace(/\s+$/,'') :str;}

/**
 * Verifies the existence of an object, function parameter, etc.
 * o	The Object to be verified
 */
function IsValid (o) {return o!=null && typeof(o)!='undefined';}

/**
 * Sets the visibility of a control
 * ctrl				Control instance
 * bIsVisible		true to make the control visible, false otherwise
 * [bPreserveSpace]	true to preserve the space occupied by the control,
 *					false otherwise. Default value is false
 */
function CtrlVisible(ctrl,bIsVisible,bPreserveSpace){
if (!IsValid(ctrl)) return;
if (bPreserveSpace==null) bPreserveSpace=false;
ctrl.style.display=bIsVisible || bPreserveSpace ?'inline':'none';
ctrl.style.visibility=bIsVisible || !bPreserveSpace ?'visible':'hidden';
}

/**
 * Sets the visibility of a control
 * strCtrlID		String containing Control's id
 * bIsVisible		true to make the control visible, false otherwise
 * [bPreserveSpace]	true to preserve the space occupied by the control,
 *					false otherwise. Default value is false
 */
function CtrlVisibleByID(strCtrlID,bIsVisible,bPreserveSpace){
CtrlVisible(document.getElementById(strCtrlID),bIsVisible,bPreserveSpace);
}

/**
 * Gets the control's visibility.
 *
 * ctrl	Control instance
 *
 * Returns true if the control is visible, false otherwise.
 */
function IsCtrlVisible(ctrl){
return typeof(ctrl)!='undefined' && !(ctrl.style.display=='none' || ctrl.style.visibility=='hidden');
}

/**
 * Gets the control's visibility.
 *
 * strCtrlID	String containing Control's id
 *
 * Returns true if the control is visible, false otherwise.
 */
function IsCtrlVisibleByID(strCtrlID){
return IsCtrlVisible(document.getElementById(strCtrlID));
}

/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure)
{
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

// Remove all items from a SELECT control
function SelectClear(oSelect){
	while(oSelect.options.length>0)
		oSelect.options.remove(0);
}

// Add a SELECT control item
function SelectAddOption(oSelect, strVal, strText, bSelected){
	var oOption=document.createElement('OPTION');
	oSelect.options.add(oOption);
	oOption.innerText=strText;
	oOption.value=strVal;
	oOption.selected=bSelected;
}

// Finds and sets selected item of a SELECT control by its value.
// If match doesn't exist returns -1, otherwise returns the selected index.
function SelectSetOptionByValue(oSelect, strVal){
	for(i=0; i < oSelect.options.length; i++)
		if(oSelect.options[i].value==strVal){
			oSelect.options[i].selected=true;
			return i;
		}
	return -1;
}

// Finds and sets selected item of a SELECT control by its text.
// If match doesn't exist returns -1, otherwise returns the selected index.
function SelectSetOptionByText(oSelect, strVal){
	for(i=0; i < oSelect.options.length; i++)
		if(oSelect.options[i].text==strVal){
			oSelect.options[i].selected=true;
			return i;
		}
		return -1;
}

function IsNumeric()
{
	var code = event.keyCode
	//alert(code);
	//return false;
	switch (code)
	{
		//0-9 = 96-105	and also 48-57
		case 48:
		case 49:	
		case 50:
		case 51:
		case 52:
		case 53:
		case 54:
		case 55:
		case 56:
		case 57:
		case 96:
		case 97:
		case 98:
		case 99:
		case 100:
		case 101:
		case 102:
		case 103:
		case 104:
		case 105:
		case 110:	//.
		case 190:	//.
		case 8: //backspace
		case 46: //delete
		case 37: //arrowleft
		case 39: //arrowright
		case 9:	//tab
			return true;
		default:
			return false;
	}
}

function IsRBChecked(rb, iTotal)
{
	for(var i=0; i < iTotal; i++)
	{
		if(rb[i].checked)
			return true;
	}
	return false;
}


function roundNumber(txt, rlength) {
	var newnumber = Math.round(txt.value*Math.pow(10,rlength))/Math.pow(10,rlength);
	txt.value = newnumber;
}



