function selectByValue(obj, value) {
    for(j=0;j<obj.length;j++)
        if(obj.options[j].value == value)
            obj.options[j].selected = true;
    return true;
}

function selectByText(obj, value) {
    for(j=0;j<obj.length;j++)
        if(obj.options[j].text == value)
            obj.options[j].selected = true;
    return true;
}

// INDEX
// -- CHECK FORM
// -- STRING MANAGEMENT
// -- TEXT AREA MANAGEMENT
// -- GENERAL PURPOSE
// -- SELECT MANAGEMENT
// -- ARRAY

//********************************** CHECK FORM ********************************//

/*
	Function:isNumeric
	Check if a string contains only numeric character
	Parameters:
		sText - String to check.
	Returns:
		True id sText is a numeric string, false otherwise
*/
function isNumeric(sText)
{
	var validChars = "0123456789.";
	var isNumber = true;
	var ch;

	for (var i = 0; i < sText.length && IsNumber == true; i++)
	{
		ch = sText.charAt(i);

		if (validChars.indexOf(ch) == -1)
		{
			isNumber = false;
		}
	}

	return isNumber;
}

/*
	Function: checkApexes
	Check if the string passed by parameter contains " character or ' charcater.
	Parameters:
		string - String to check.
	Returns:
		True if the string contains " character or ' character, false otherwise.
*/
function checkApexes(string)
{
	if ((!(string.indexOf("\"") < 0)) || (!(string.indexOf("'") < 0)))
		return true;
	else
		return false;

}

/*
	Function: clearForm
	Clear all the values (not buttons) of a form with her id passed by parameter.
	Parameters:
		idForm - Form's id to clear.
	Returns:
		Void.
*/
function clearForm(idForm)
{
	//alert(idForm);
	var form = document.all[idForm];

	//alert(form);
	for (var i = 0; i < form.elements.length; i++)
	{
		if ((form.elements[i].type != 'button') && (form.elements[i].type != 'submit') && (form.elements[i].type != 'reset'))
			form.elements[i].value = "";
	}
}

//*********************************************************************************//

//********************************** STRING MANAGEMENT ***************************//

/*
	Function: rTrim
	Remove all the spaces positoned at start of the string passed by parameter.
	Parameters:
		string - String to remove space characters
	Returns:
		String without space charachters at start.

	See Also:
		<lTrim> <trim>
*/
function rTrim(string)
{
	while(string.charAt((string.length -1)) ==" ")
	{
		string = string.substring(0, string.length-1);
	}

	return string;
}

/*
	Function: lTrim
	Remove all the spaces positoned at end of the string passed by parameter.
	Parameters:
		string - String to remove space characters
	Returns:
		String without space charachters at end.

	See Also:
		<rTrim> <trim>
*/
function lTrim(string)
{
	while(string.charAt(0)==" ")
	{
		string = string.replace(string.charAt(0),"");
	}

	return string;
}

/*
	Function: lTrim
	Remove all the spaces positoned at start and at end of the string passed by parameter.
	Parameters:
		string - String to remove space characters
	Returns:
		String without space charachters at start and at end.

	See Also:
		<rTrim> <lTrim>
*/
function trim(string)
{
	string = lTrim(string);
	return rTrim(string);
}

/*
	Function: collapseSpace
	Collapse all blank sapaces in one blank space.
	Parameters:
		string - String to collapse blank spaces
	Returns:
		String without spaces concatenated.

	See Also:

*/
function collapseSpace(string)
{
	var r = "\\s{2,}";
	var re = new RegExp(r, "g");
	string = string.replace(re," ");

	return string;
}

//******************************************************************************//

//********************************* TEXT AREA MANAGEMENT ***********************//

/*
	Function: insertAtCursor
	Insert a text value with a blank space at cursor position, appended in a field form like "text", "hidden", "text-area", etc...
	Parameters:
		val - Text value to insert in field
		idField - Field's id to insert in text value.
	Returns:
		Void.
*/
function insertAtCursor(myValue, idMyField) {

	myValue = unescape(myValue);
	myField = document.getElementById(idMyField);

	//alert(document.selection);

	//IE support
	if (document.selection)
	{
		myField.focus();
		sel = document.selection.createRange();

		sel.text = " "+myValue+" ";
		myString = collapseSpace(myField.value);
		myField.value = trim(myString);

	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || (myField.selectionStart == 0))
	{
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		//alert(startPos+" "+endPos)

		var myString = myField.value.substring(0, startPos)
			+" "+myValue+" "
			+ myField.value.substring(endPos, myField.value.length);

		myString = collapseSpace(myString);
		myField.value = trim(myString );

	}
	else
	{

		if (myField.value.length == 0)
			myField.value += myValue;
		else
			myField.value += " "+myValue;
	}
}

//******************************************************************************//

//******************************* GENERAL PURPOSE ******************************//

/*
	Function: isUndefined
	Check if parameter is undefined or not.
	Parameter:
		a - parameter to check
	Returns:
 		true if a is the undefined value. You can get the undefined value from an uninitialized variable or from an object's missing member.
 */
 function isUndefined(a)
 {
     return typeof a == 'undefined';
 }

 /*
	Function: isNull
	Check if parameter is null or not.
	Parameter:
		a - parameter to check
	Returns:
 		 true if a is the null value
 */
  function isNull(a)
 {
      return typeof a == 'object' && !a;
 }

//******************************************************************************//

//********************************** SELECT MANAGEMENT ************************//
 /*
	Function: addOption

	Add an option created with "theText" as text value and "theValue" as option value to select "theSel"

	Parameters:
      		theSel - Select module to add option.
      		theText - Option's text value to add to select.
      		theValue - Option's value to add to select.

      	Returns:
      		Void

      	See Also:
      		<insOpt>
*/
function addOption(theSel, theText, theValue)
{
	var newOpt = new Option(theText, theValue);
	var selLength = theSel.length;
	theSel.options[selLength] = newOpt;
}

/*
	Function: setSelectedOpt

	Set as selected options passed by parameter

	Parameters:
		sel - Select to serch in option to set selected
		textOpt - Text value of option to set as selected

	Returns:
		Void
*/
function setSelectedOpt(sel, textOpt)
{
	for (var i = 0; i < sel.options.length; i++)
	{
		if (sel.options[i].text == textOpt)
			sel.options[i].selected = true;
	}
}

/*
	Function: setSelectedOptByVal

	Set as selected options passed by parameter

	Parameters:
		sel - Select to serch in option to set selected
		valOpt - value of option to set as selected

	Returns:
		Void
*/
function setSelectedOptByVal(sel, value)
{
	for (var i = 0; i < sel.options.length; i++)
	{
		if (sel.options[i].value == value)
			sel.options[i].selected = true;
	}
}

/*
	Function: sortOptions
	sort the options of select passed by parameter
	Parameters:
		sel - Select sort options
	Returns:
		Void
*/
function sortOptions(sel)
{
	//alert(sel);

	var arrOptTxtVal = new Array();
	var arrTmp = "";

	for (var i=0; i < sel.options.length; i++)
	{
		arrOptTxtVal.push(sel.options[i].text+"*|*"+sel.options[i].value);
	}

	//alert(arrOptTxtVal);
	arrOptTxtVal.sort(function(x,y){
		var a = String(x).toUpperCase();
		var b = String(y).toUpperCase();

		if (a > b)
		return 1
		if (a < b)
		return -1
		return 0;
	});
	sel.options.length = 0;

	for (var i=0; i < arrOptTxtVal.length; i++)
	{
		arrTmp = arrOptTxtVal[i].split('*|*');
		//alert(arrTmp[0]+" "+arrTmp[1]);
		addOption(sel, arrTmp[0], arrTmp[1]);
	}
}

/*
	Function: removeSelectOpt
	Remove select's options selected by user
	Parameters:
		sel - select to delete option
	Returns:
		Void
*/

function removeSelectOpt(sel)
{
	var arrOpt = new Array();
	var arrTmp = "";

	for (var i = 0; i < sel.options.length; i++)
	{
		if (sel.options[i].selected == false)
			arrOpt .push(sel.options[i].text+"*|*"+sel.options[i].value);
	}

	sel.options.length = 0;

	for (var i = 0; i < arrOpt .length; i++)
	{
		arrTmp = arrOpt[i].split("*|*");
		addOption(sel, arrTmp[0], arrTmp[1]);
	}
}

/*
	Function: removeAllOptions
	Remove all select's options
	Parameters:
		sel - select to delete options
	Returns:
		Void
*/

function removeAllOptions(sel)
{
	for (var i = sel.options.length; i >= 0; i--)
	{
		sel.options[i] = null;
	}
}

/*
	Function: isInsideSelectByVal
	Returns true if there is an option inside select "sel" with the value equal to "val"
	Parameters:
		sel - select check
		val - value to check
	Returns:
		Void
*/

function isInsideSelectByVal(sel, val)
{
	for (var i = 0; i < sel.options.length; i++)
	{
		if (sel.options[i].value == val)
			return true;
	}

	return false;
}


/*
	Function: getSelectedOpt
	Returns an array where every value is an array composed like this:
		arr[0] ==> selected option value
		arr[1] ==> selected option text
	Parameters:
		sel - select to obtain selected options
	Returns:
		An array where every value is an array composed like this:
		arr[0] ==> selected option value
		arr[1] ==> selected option text
*/

function getSelectedOpt(sel)
{
	var arrVal = new Array();

	for (var i = 0; i < sel.options.length; i++)
	{
		if (sel.options[i].selected == true)
		{
			var tmp = new Array();
			tmp.push(sel.options[i].value);
			tmp.push(sel.options[i].text);

			arrVal.push(tmp);
		}
	}

	return arrVal;
}


//******************************************************************************//
//**********************************ARRAY**************************************//

/*
	Function: compareArray

	Compare two array considering order

	Parameters:
		arr1 - array to compare with arr1
		arr2 - array to compare with arr2

	Returns:
		true if arr1 is equal to arr2, false otherwise.
*/
function compareArray(arr1, arr2)
{
	var len1 = arr1.length;
	var len2 = arr2.length;

	if (len1 != len2)
		return false;

	for (var i = 0; i < len1; i++)
	{
		if (len1[i] != len2[i])
		{
			return false;
		}
	}

	return true;
}

