function domouseover(ref)
{
	ref.style.background = '#EEEEEE';
}
function domouseout(ref)
{
	ref.style.background = '#FFFFFF';
}
function do_toggle_button(fieldref, buttonref)
{
	var str;
	var fieldlength;
	var i;
	if (fieldref != null)
	{
		fieldlength = fieldref.length
		if (fieldlength != null)
		{
			for (i=0;i<fieldlength;i++)
			{
				if (fieldref[i].checked == true)
				{
					if (null != buttonref)
						buttonref.disabled = false;
					return;
				}
			}
		}
		else
		{
			if (fieldref.checked == true)
			{
				if (null != buttonref)
					buttonref.disabled = false;
				return;
			}
		}
	}
	if (null != buttonref)
		buttonref.disabled = true;
}
function do_checkall(ref, fieldref, buttonref)
{
	var cb_status = ref.checked;
	var slength;
	var i;
	if (fieldref != null)
	{
		slength = fieldref.length;
		if (slength != null)
		{
			for (i=0;i<slength;i++)
			{
				fieldref[i].checked = + cb_status;
			}
		}
		else
		{
			fieldref.checked = + cb_status;
		}
	}
	do_toggle_button(fieldref, buttonref);
}

function move_item(src, dest)
{
	var i;
	var idx;
	var newOpt;
	if (src.selectedIndex == -1)
		return;
	for (i=src.length-1;i>=0;i--)
	{
		if (src.options[i].selected == true)
		{
			if (dest != null)
			{
				newOpt = new Option(src.options[i].text, src.options[i].value)
				idx = dest.length++;
				dest.length = idx;
				dest.options[idx] = newOpt;
			}
			src.options[i] = null;
		}
	}
}

function delete_item(src)
{
	var i;
	if (src.selectedIndex == -1) return;
	for (i=src.length-1;i>=0;i--)
	{
		if (src.options[i].selected == true)
		{
			src.options[i] = null;
		}
	}
}
function copy_item(src, dest, bduplicate)
{
	var i;
	var idx;
	var newOpt;
	if (src.selectedIndex == -1)
		return;
	for (i=src.length-1;i>=0;i--)
	{
		if (src.options[i].selected == true)
		{
			if (bduplicate == true || !item_exists(dest, src.options[i].value))
			{
				newOpt = new Option(src.options[i].text, src.options[i].value)
				idx = dest.length++;
				dest.length = idx;
				dest.options[idx] = newOpt;
			}
		}
	}
}

function copy_item_asc(src, dest, bduplicate)
{
	var i;
	var idx;
	var newOpt;
	if (src.selectedIndex == -1)
		return;
	for (i=0;i<=src.length-1;i++)
	{
		if (src.options[i].selected == true)
		{
			if (bduplicate == true || !item_exists(dest, src.options[i].value))
			{
				newOpt = new Option(src.options[i].text, src.options[i].value)
				idx = dest.length++;
				dest.length = idx;
				dest.options[idx] = newOpt;
			}
		}
	}
}
function item_exists(dest, item_value)
{
	var i;
	for (i=dest.length-1;i>=0;i--)
	{
		if (dest.options[i].value.toUpperCase() == item_value.toUpperCase())
			return true;
	}
	return false;
}
function do_selectall(ref, bflag)
{
	select_all(ref, bflag);
}
function select_all(ref, bflag)
{
	var i;
	if (ref != null)
	{
		if (ref.length != null)
		{
			for (i=0;i<ref.length;i++)
			{
				ref.options[i].selected = bflag;
			}
		}
	}
}

function do_disableall(bflag)
{
	do_disable(bflag);
}

function do_disable(bflag)
{
	var i;
	var j;

	for (j=0;j<document.forms.length;j++)
	{
		for (i=0;i<document.forms[j].elements.length;i++)
		{
			document.forms[j].elements[i].disabled = bflag;
		}
	}
}

function get_count(ref, bstatus)
{
	var i;
	var cb_count=0;
	if (ref != null)
	{
		if (ref.length != null)
		{
			for (i=0;i<ref.length;i++)
			{
				if (ref[i].checked == bstatus)
					cb_count++;
			}
		}
		else
		{
			if (ref.checked == bstatus)
				cb_count++;
		}
	}
	return cb_count;
}

function fillList(listRef, itemData, keyValue)
{
	var i=0;

	//loop through the array
	for (i=0;i<itemData[0].length;i++)
	{
		if (keyValue.toLowerCase() == itemData[2][i].toLowerCase())
		{
			listRef.options[listRef.length] = new Option (itemData[0][i],itemData[1][i]);
		}
	}
}

function fillList2(listRef, itemData, keyValue, sFilter)
{
	var i=0;

	//loop through the array
	for (i=0;i<itemData[0].length;i++)
	{
		if (keyValue.toLowerCase() == itemData[2][i].toLowerCase() && (itemData[3][i] == sFilter))
		{
			listRef.options[listRef.length] = new Option (itemData[0][i],itemData[1][i]);
		}
	}
}


function clearList (listRef)
{
	listRef.length = 0;
}

function addItem(listRef, itemText, itemValue)
{
	listRef.options[listRef.length] = new Option(itemText, itemValue);
}

function isNumeric(str)
{
	var validStr = "0123456789";
	return testString(str, validStr);
}

function isNoFunnyChar(str)
{
	var validStr = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_";
	return testString(str, validStr);
}

function testString(strTested, strPattern)
{
	var i;
	for (i=0;i<strTested.length;i++)
	{
		if (-1 == strPattern.indexOf(strTested.charAt(i))) 
		{
			return false;
		}
	}
	return true;	
}
function isValidDate(nYear, nMonth, nDay)
{
	if (false == isNumeric(nDay)) return false;
	if (false == isNumeric(nMonth)) return false;
	if (false == isNumeric(nYear)) return false;
	nDay = parseInt(nDay);
	nMonth = parseInt(nMonth);
	nYear = parseInt(nYear);
	return nDay <= getDaysInMonth(nYear, nMonth);
}

function getDaysInMonth(nYear, nMonth)
{
	nYear = parseInt(nYear);
	nMonth = parseInt(nMonth);
	switch(nMonth)
	{
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			return 31;
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			return 30;
			break;
		case 2:
			if (nYear % 4 == 0)
				return 29;
			else
				return 28;
	}
}

function isEmail(email)
{
	var at="@";
	var dot=".";
	var lat=email.indexOf(at);
	var lstr=email.length;
	var ldot=email.indexOf(dot);

	// abcxyz.com or @abc.xyz.com or abc.xyz.com@
	if (lat == -1 || lat == 0 || lat == lstr)
		return false;

	// abc@xyz or .abc@xyz or abc@xyz.
	if (ldot == -1 || ldot == 0 || ldot == lstr)
		return false;

	// abc@def@xyz.com
	if (lat != email.lastIndexOf(at))
		return false;

	// abc.@xyz.com or abc@.xyz.com
	if (email.substring(lat-1, lat) == dot || email.substring(lat+1, lat+2) == dot)
		return false;

	// abc.com@xyz
	if (ldot < lat)
		return false;

	if (email.indexOf(" ") != -1)
		return false;

	return true;
}

function RemoveAsciiSpace(str)
{
	var sourceStr = new String(str);
	var finalStr = "";
	var i = 0;

	for ( i = 0; i < sourceStr.length; i++)
	{
		if ( sourceStr.charCodeAt(i) == 160 ) // 160 is the ascii code for space
			finalStr = finalStr + " ";
		else
			finalStr = finalStr + sourceStr.charAt(i);
	}	

	return finalStr;	
}

function LTrim(str)
{
	var whitespace = new String(" \t\n\r");
	
	str = RemoveAsciiSpace(str);
	var s = new String(str);

	if (whitespace.indexOf(s.charAt(0)) != -1) {
	    // We have a string with leading blank(s)...

	    var j=0, i = s.length;

	    // Iterate from the far left of string until we
	    // don't have any more whitespace...
	    while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
	        j++;


	    // Get the substring from the first non-whitespace
	    // character to the end of the string...
	    s = s.substring(j, i);
	}

	return s;
}

function RTrim(str)
{
    var whitespace = new String(" \t\n\r");
    
    str = RemoveAsciiSpace(str);
    
    var s = new String(str);

    if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
        // We have a string with trailing blank(s)...

        var i = s.length - 1;       // Get length of string

        // Iterate from the far right of string until we
        // don't have any more whitespace...
        while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
            i--;


        // Get the substring from the front of the string to
        // where the last non-whitespace character is...
        s = s.substring(0, i+1);
    }

    return s;
}

function isDecimal(str)
{
	str = LTrim(RTrim(str));

	//blank string is not decimal
	if (str.length == 0)	return false;

	//need to check if the string contains multiple dot
	if (str.indexOf(".") != str.lastIndexOf("."))
	{
		//contains multiple dot, so invalid
		return false;
	}

	//need to test if it is numeric
	var validStr = "0123456789.";
	if (testString(str, validStr) == false)
	{
		//failed the test, so return
		return false;
	}
	
	//pass all the test
	return true;
}
