/*----------------------------------------------------------------------
| Form.js :
----------------------------------------------------------------------*/

//----------------------------------------------------------------------
//   SelChk : selected,checked automatically.
//
// SelChk.slck ('id','value');					<input type=select|checkbox>
// SelChk.radio ('formname.name' ,'value');		<input type=radio>
//----------------------------------------------------------------------

var SelChk =
{
	objid : '',
	objval : '',

	getTagName : function ()
	{
		return document.getElementById(this.objid).tagName.toLowerCase();
	},

	getType : function ()
	{
		return document.getElementById(this.objid).getAttribute('type')
	},

	type_checkbox : function ()
	{
		if (document.getElementById(this.objid).value = this.objval)
		{
			document.getElementById(this.objid).checked = true;
		}
	},

	type_select : function ()
	{
		var selName = document.getElementById(this.objid);

		for (i=0;i<selName.length;i++)
		{
			if (selName[i].getAttribute("value") == this.objval)
			{
				selName[i].selected = true;
				return ;
			}
		}
	},

	slck : function (id,val)
	{
		if ( document.getElementById(id) && id != '' && val != '' )
		{
			this.objid = id;
			this.objval = val;

			if ( this.getTagName() == "select" )
			{
				this.type_select();
			}
			else if ( this.getTagName() == "input" )
			{
				if ( this.getType() == "checkbox" )
				{
					this.type_checkbox();
				}
			}
		}
	},

	radio : function (name, newValue)
	{
		if ( name != '' && newValue != '' )
		{
			// formname.name
			if (name.indexOf(".") != -1)
			{
				var splitname = name.split("."); 
				var formname = splitname['0'];
				var radioname = splitname['1'];
				var radioObj = document.forms[formname].elements[radioname];
			}

			if(!radioObj)
				return;
			var radioLength = radioObj.length;
			if(radioLength == undefined)
			{
				radioObj.checked = (radioObj.value == newValue.toString());
				return;
			}
			for(var i = 0; i < radioLength; i++)
			{
				radioObj[i].checked = false;
				if(radioObj[i].value == newValue.toString())
				{
					radioObj[i].checked = true;
				}
			}
		}
	}
}




//----------------------------------------------------------------------
//   ChkAll : select/unselect all checkbox in the form.
//----------------------------------------------------------------------
var ChkAll =
{
	stat : false,

	selAll : function ( form_id , chkstat )
	{
		var checkboxes = [];
		checkboxes = $(form_id).getInputs('checkbox');
		checkboxes.each(function(e){ e.checked = chkstat });
	},

	toggle : function ( form_id )
	{
		var chkstat = !this.stat;
		this.selAll ( form_id , chkstat );
		this.stat = chkstat;
	}
}




//----------------------------------------------------------------------
//   Getting Radio buttons value
//----------------------------------------------------------------------
/*
function get_radio_val ( formname , radioname )
{
	var chked = Form.getInputs(formname,'radio',radioname).find(function(radio){ return radio.checked; });
    return (chked) ? $F(chked) : null;
}
*/


function get_radio_val(formname , radioname)
{
	var radioObj = document.forms[formname].elements[radioname];

	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}


