// JavaScript Library for common utility functions

function getURLParameter( name )
{
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var tmpURL = window.location.href;
  var results = regex.exec( tmpURL );
  if( results == null )
    return "";
  else
    return results[1];
}

function replace(string,text,by) {
    // Replaces text with by in string
    var i = string.indexOf(text), newstr = '';
    if ((!i) || (i == -1))
        return string;
    newstr += string.substring(0,i) + by;
    if (i+text.length < string.length)
        newstr += replace(string.substring(i+text.length,string.length),text,by);
    return newstr;
}


function stringUnescape(string)
{
  var unescaped = unescape(string);
  return replace(unescaped, '+', ' '); 
}

/**
 * Function:	trim(string)
 * Purpose:	Remove leading and trailing whitespace from a string
 * Params:	strText 
 * Returns:	string 
 **/
 
function trim(strText)
{
	while (strText.substring(0,1) == ' ') 
		strText = strText.substring(1, strText.length);
	while (strText.substring(strText.length-1,strText.length) == ' ')
		strText = strText.substring(0, strText.length - 1);

	return strText;
}

/**
 * Function:	isBlank(string)
 * Purpose:	Check whether the input value is all blank
 * Params:	strText 
 * Returns:	boolean
 **/

function isBlank(strText)
{
	for (var i = 0; i < strText.length; i++)
	{
		var c = strText.charAt(i);
		if (c != ' ' && c != '\n' && c != '\t') return false;
	}

	return true;
}

function disableControls()
{
	disableAllControls();
	disableAllLinks();
	disableAllInputImages();
}

/**
 * Function:	disableAllControls()
 **/

function disableAllControls()
{

	if (typeof document.forms[0] != 'undefined')
	{
		var elements = document.forms[0].elements;
	
	//    var elementslist;
	
		for (var i in elements) 
		{
			if (elements[i])
			{
	//            elementslist = elementslist + elements[i].name + " " + elements[i].type + " " + elements[i].id + "\n";
				if (elements[i].type == "text"     ||
					elements[i].type == "textarea" ||
					elements[i].type == "hidden"   ||
					elements[i].type == "password" ||
					elements[i].type == "fileUpload")
				{
					elements[i].onfocus = onTextDisabled;
				}
	
				if (elements[i].type == "button"   ||
					elements[i].type == "reset"    ||
					elements[i].type == "image"    ||				
					elements[i].type == "submit")
				{
					elements[i].onclick = onButtonDisabled;
				}
			}
		}
	}
//	alert(elementslist);
}

/**
 * Function: disableAllInputImages()
 * Purpose:  disable images since they are not available
 *           through disableAllControls method
 * Params:	 none
 * Returns:	 nothing
 **/
function disableAllInputImages()
{
	var elements = document.all;
	for (var i in elements)	{
		if (elements[i])		{
			if (   (elements[i].type) 
			    && (elements[i].type == "image")){
              elements[i].disabled = true;
			}
		}
	}
}

/**
 * Function:	onTextDisabled()
 * Purpose:	Forces focus to this field to be discarded immediately
 *		by calling the blur() function on the current field.
 * Params:	none
 * Returns:	nothing
 **/

function onTextDisabled()
{
	this.blur();
}

/**
 * Function:	onButtonEnabled()
 * Function:	onButtonDisabled()
 * Function:	buttonReturn()
 * Purpose:	Enable/Disable an HTML button control 
 **/

function onButtonEnabled()
{
	return buttonReturn();
}

function onButtonDisabled()
{
	return !buttonReturn();
}

function buttonReturn()
{
	var bAppName = window.navigator.appName;
	var bAgent = window.navigator.userAgent;

	if (bAppName.indexOf("Explorer") >= 0 &&
		bAgent.indexOf("Mozilla/3") >= 0)
	{
		return false; 
	}
	else
	{
		return true; 
	}
}

/**
 * Function:	disableAllLinks()
 * Purpose:	Disables all links in the DOM.
 * Params:	none
 * Returns:	nothing
 **/

function disableAllLinks()
{
	for (var i in document.links)
	{
		if (document.links[i])
		{
			if (i != "length")
			{

				document.links[i].onclick = onLinkDisabled;
			}
		}
	}
}

/**
 * Function:	onLinkEnabled()
 * Function:	onLinkDisabled()
 * Function:	linkReturn()
 * Purpose:	Enable/Disable an HTML hyper-link control 
 * Params:	none
 * Returns:	nothing
 **/

function onLinkEnabled()
{
	return !linkReturn();
}

function onLinkDisabled()
{
	return linkReturn();
}

function linkReturn()
{
	var bAppName = window.navigator.appName;
	var bAgent = window.navigator.userAgent;

	if (bAppName.indexOf("Explorer") >= 0 &&
		bAgent.indexOf("Mozilla/3") >= 0)
	{
		return true; 
	}
	else
	{
		return false; 
	}
}
