var dbNameLength = 90;

/* 
	Insert an Item into an array
*/
function InsertIntoArray(arr, idx, entity)
{
	if(idx < 0)
	{
		alert("Cannot insert with negative index");
		return;
	}
	else
	if(idx >= arr.length)
	{
		// if insert index > length, assume it's append
		arr[arr.length] = entity;
		return;
	}
	for(i = arr.length; i > idx; i--)
	{
		arr[i] = arr[i-1];
	}
	arr[idx] = entity;
}

/* 
	return an new array and delete the items specified with index in an array passed in.
	This function assumes the indices are specified in the ascending order.
*/
function DeleteElementsFromArray(targetArr, indexArr)
{
	if(indexArr.length > 0)
	{
		arr = new Array();
		idx = 0;
		for(i = 0; i < targetArr.length; i++)
		{
			if(i == indexArr[idx])
			{
				idx++;
			}
			else
			{
				arr[arr.length] = targetArr[i];
			}
		}
		return arr;
	}
	else
	{
		return targetArr;
	}
}

function RemoveFromArray(targetArr, index)
{
	if(index < 0 || index >= targetArr.length)
	{
		return targetArr;
	}
	else
	{
		arr = new Array();
		for(i = 0; i < targetArr.length; i++)
		{
			if(i != index)
			{
				arr[arr.length] = targetArr[i];
			}
		}
		return arr;
	}
}

/* 
	Insert an option into the list box
*/
function InsertIntoListBox(listBox, idx, option)
{
	if(idx < 0)
	{
		alert("Cannot insert with negative index");
		return;
	}
	else
	if(idx >= listBox.length)
	{
		// if insert index > length, assume it's append
		listBox[listBox.length] = option;
		return;
	}
	for(i = listBox.length; i > idx; i--)
	{
		listBox[i] = new Option(listBox[i-1].text, listBox[i-1].value, false, false);
		listBox[i-1] = new Option();
	}
	listBox[idx] = option;
}

/*
	Get selected indices
	This will return an array containing the indices of
	the selected items in the list box passed in. If there's
	no selected item, an empty array will be return;
*/
function GetSelectedIndices(listBox)
{
	arr = new Array();
	list = listBox.options;
	for(i = 0; i < list.length; i++)
	{
		if(list[i].selected)
		{
			arr[arr.length] = i;
		}
	}
	return arr;
}

/*
	Delete the selected item from the list box
*/
function DeleteSelectedFromListBox(listBox)
{
	list = listBox.options;
	for(i = list.length - 1; i >= 0; i--)
	{
		if(list[i].selected)
		{
			list[i] = null;
		}
	}
}

function Trim(strText) 
{	
        // this will get rid of leading spaces 
        while (strText.substring(0,1) == ' ') 
            strText = strText.substring(1, strText.length);

        // this will get rid of trailing spaces 
        while (strText.substring(strText.length-1,strText.length) == ' ')
            strText = strText.substring(0, strText.length-1);

        return strText;
} 
	
function ValidateNameLength(str) 
{
	return ValidateLength(str, dbNameLength);
}

function ValidateLength(str, l)
{
	if (str.length <= l) return true;
	else return false;
}

function ValidateFloat(num) 
{
    if (num > 2.0 * 1000000000) return false;
    return true;   
}

function ValidateInteger(num) 
{
    if (!IsInteger(num) || (num > 2.0 * 1000000000)) return false;
    return true;   
}

function IsInteger(aNumber)
{
	var s = new String(aNumber);
	var n = s.indexOf("e");
	if(n >= 0 && n < s.length-1)
		return false;
	n = s.indexOf("E");
	if(n >= 0 && n < s.length-1)
		return false;
	n = s.indexOf(".");
	if( n >= 0 )
		return false;
	
	// catch the trailing non-digit
	if (isNaN(aNumber))
		return false;

	// note: this function return NaN if aNumber is empty.
	var n = parseInt(aNumber);
	return !isNaN(n);
}
function IsFloat(aNumber)
{
	// catch the trailing non-digit
	if (isNaN(aNumber))
		return false;

	// note: this function return NaN if aNumber is empty.
	var n = parseFloat(aNumber);
	return !isNaN(n);
}
// encode a java script string to be HTML friendly
function encodeHTML(s)
{
	if (s == null)
		return s;
	var result = "";
	for (var i = 0; i < s.length; i ++)
	{
		var c = s.charAt(i);
		switch(c)
		{
             case '<':
                 result += "&lt;";
             break;
             case '>':
                 result += "&gt;";
             break;
             case '&':
                 result += "&amp;";
             break;
             case '"':
                 result += "&quot;";
             break;
             case '\'':
                 result += "&#39;";
             break;
             default:
                 result += c;
             break;
		}		
	}
	return result;
}



