var types         = new Array("text"    , "password", "checkbox"    , "radio"    , "submit"  , "reset"   , "file"    , "hidden"  , "image"  , "button"  , "textarea", "select-one", "select-multiple")
var get_functions = new Array("get_text", "get_text", "get_checkbox", "get_radio", "get_text", "get_text", "get_text", "get_text", "get_src", "get_text", "get_text", "get_select", "get_select")
var set_functions = new Array("set_text", "set_text", "set_checkbox", "set_radio", "set_text", "set_text", "set_text", "set_text", "set_src", "set_text", "set_text", "set_select", "get_select")
var controls  = new Array(3)

controls[0] = types
controls[1] = get_functions
controls[2] = set_functions


function get_value(obj)
{
	if(obj.type == "undefined" || obj.type == null)
	{
		functions_v = find_type(obj[0].type, 1);
	}
	else
	{
		functions_v = find_type(obj.type, 1);
	}
	if ( functions_v != false)
	{
		return eval(functions_v + "(obj)");
	}
	else
	{
		return false;
	}
}

function set_value(obj, value, index)
{
	if(obj.type == "undefined" || obj.type == null)
	{
		functions_v = find_type(obj[0].type, 2);
	}
	else
	{
		functions_v = find_type(obj.type, 2);
	}
	if ( functions_v != false)
	{
		eval(functions_v + "(obj, '" + value + "', " + index + ")");
		return true;
	}
	else
	{
		return false;
	}
}

function find_type(type_name, index)
{
	for( i = 0 ; i < types.length ; i++)
	{
		if(type_name == controls[0][i])
		{
			return controls[index][i];
		}
	}
	return false;
}

// -------------------- get function -------------------------------
function get_text(obj)
{
	return obj.value;
}

function get_checkbox(obj)
{
	if(obj.checked == true)
		return obj.value;
	else
		return false;
}

function get_radio(obj)
{
	if (isNaN(obj.length))
	{
		if (obj.checked)
		{
			return obj.value;
		}
	}
	for (i = 0 ; i < obj.length; i++)
	{
		if(obj[i].checked)
		{
			return obj[i].value;
		}
	}
	return false;
}

function check_radio(obj)
{
	for (i = 0 ; i < obj.length; i++)
	{
		if(obj[i].checked)
		{
			return true;
		}
	}
	return false;
}

function get_select(obj)
{
	if(obj.multiple)
	{
		t_i = 0
		for(t_j=0 ; t_j < obj.length ; t_j++)
		{
			if(obj.options[t_j].selected) t_i++;
		}
		select_v = new Array(t_i);
		t_i = 0
		for(t_j=0 ; t_j < obj.length ; t_j++)
		{
			if(obj.options[t_j].selected)
			{
				select_v[t_i] = obj.options[t_j].value;
				t_i++;
			}
		}
		return select_v;
	}
	else
	{
		return obj.options[obj.selectedIndex].value;
	}
}

function get_src(obj)
{
	return obj.src;
}


// -------------------- set function -------------------------------
function set_text(obj, value, index)
{
	obj.value = value;
}

function set_checkbox(obj, value, index)
{
	if(!(value == "" || value == null))
	{
		obj.value = value;
	}
	if ( index == true)
	{
		obj.checked = true;
	}
	else
	{
		obj.checked = false;
	}
}

function set_radio(obj, value, index)
{
	if(value == "")
	{
		obj[index].checked = true;
	}
	else
	{
		for( i = 0 ; i < obj.length ; i++)
		{
			if(obj[i].value == value)
			{
				obj[i].checked = true;
				return ;
			}
		}
	}
}

function set_select(obj, value, index)
{
	if(value == "")
	{
		obj.selectedIndex = index;
	}
	else
	{
		for( t_i = 0 ; t_i < obj.length ; t_i++)
		{
			if(obj.options[t_i].value == value)
			{
				obj.selectedIndex = t_i;
				return ;
			}
		}
	}
}

function set_select2(obj, value, index)
{
	if(value == "")
	{
		obj.options[index].selected = 1;
	}
	else
	{
		for( i = 0 ; i < obj.length ; i++)
		{
			if(obj.options[i].value == value)
			{
				obj.options[i].selected = 1;
//				return ;
			}
		}
	}
}

function set_src(obj, value, index)
{
	obj.src = value;
}
// -------------------- select control function -------------------------------
function clear_select(obj)
{
	sel_len = obj.length
	for(i = 0 ; i < sel_len; i++)
	{
		obj.options[0] = null;
	}
	return ;
}


function add_select(obj, add_value, add_text, pos)
{
	var opt = new Option(add_value, add_text);
	obj.options[pos] = opt;
	return ;
}

function del_select(obj, index)
{
	obj.options[index] = null;
}

function copy_select(src, desc)
{
	clear_select(desc);
	if (src != 0)
	{
		cnt = src.length;
		for ( i = 0 ; i < cnt ; i++)
		{
			var opt = new Option(src.options[i].text, src.options[i].value);
			desc.options[i] = opt;
		}
	}
	desc.selectedIndex = 0
}

function move_select(src, desc, src_index, desc_index)
{
	if (src != 0)
	{
		desc.options[desc_index] = src.options[src_index];
		del_select(src, src_index);
	}
}

function set_mark(str, len, mark, pos)
{
	var tmp_str = new String(str);
	slen = tmp_str.length;
	if(slen < len)
	{
		for(t_i = 1 ; t_i <= (len - slen) ; t_i++)
		{
			if(pos>0)
				tmp_str = tmp_str + "" + mark;
			else
				tmp_str = mark + "" + tmp_str;
		}
	}
	return tmp_str;
}
