// JavaScript Document

	/**
	 * Object Form
	 * Example:
	 * var frm = new Form();
	 * var frm = new Form('frmName'); 
	 */
	function FormHelper()
	{
		var _window;
		var _formName;
		var _childWindow;
		
		this._window = self;
		
		if(arguments.length == 0)
		{
			this._formName = "";
		}
		else
		{
			this._formName = arguments[0];
		}
		
		
		this.getForm = getForm;
		
		this.findControl = findControl;
		
		this.findControls = findControls;
		
		this.getControlByName = getControlByName;
		
		this.getTextboxValue = getTextboxValue;
		
		this.setTextboxValue = setTextboxValue;
		
		this.getDropDownValue = getDropDownValue;
		
		this.setDropDownValue = setDropDownValue;
		
		this.getCheckboxValue = getCheckboxValue;
		
		this.setCheckboxValue = setCheckboxValue;
		
		this.getRadioValue = getRadioValue;
		
		this.setRadioValue = setRadioValue;
		
		this.getRadioGroupValue = getRadioGroupValue;
		
		this.setRadioGroupValue = setRadioGroupValue;
		
		this.addOption = addOption;
		
		this.removeOption = removeOption;
		
		this.removeOptionByIndex = removeOptionByIndex;
		
		
		this.GetWindow = GetWindow;
		
		this.SetWindow = SetWindow;		
		
		this.OpenWindow = OpenWindow;
		
		this.CloseWindow = CloseWindow;
		
		this.GetControlValue = GetControlValue;
		
		this.SetControlValue = SetControlValue;
		
		this.GetCheckboxGroupValue = GetCheckboxGroupValue;

		this.SetCheckboxGroupValue = SetCheckboxGroupValue;

		this.GetDropDownListValue = GetDropDownListValue;
		
		this.SetDropDownListValue = SetDropDownListValue;
		
		this.IsChecked = IsChecked;
		
		this.SetCheckStatus = SetCheckStatus;
		
		this.Check = Check;
		
		this.Uncheck = Uncheck;
		
		this.AddOption = AddOption;
		
		this.RemoveOption = RemoveOption;
		
		this.RemoveSelectedOption = RemoveSelectedOption;
		
		this.IsOptionExists = IsOptionExists;
		
		this.RemoveOptionByIndex = RemoveOptionByIndex;
		
		this.GetSelectedIndex = GetSelectedIndex;
		
		this.SelectAllOptions = SelectAllOptions;
		
		this.PutFocus = PutFocus;
		
		this.EnableControl = EnableControl;
		
		this.DisableControl = DisableControl;
		
		this.SubmitForm = SubmitForm;
		
		this.GetLayer = GetLayer;
		
		this.SetLayerHtml = SetLayerHtml;
		
		this.clearList = __clearList;
	}
	
	/**
	 * START OF PRIVATE METHODS
	 */
	function getForm()
	{
		if(this._formName == "")
		{
			return (this._window).document.forms[0];
		}
		else
		{
			return eval("(this._window).document." + this._formName);
		}
	}
	 
	function getControlByName (controlName)
	{
		var ctl = eval("(this.getForm())." + controlName);
		return ctl;
	}
	
	
	function findControl (controlName)
	{
		var elements = (this.getForm()).elements;
		
		var totalElements = elements.length;
		
		for(var i = 0; i < totalElements; i++)
		{
			var ctl = elements[i];
			
			if(ctl.name)
			{
				if(ctl.name.indexOf(controlName) != -1)
				{
					return ctl;
				}
			}
		}
	}
	
	function findControls(controlNamePart)
	{
		var controls = new Array();
		
		var elements = (this.getForm()).elements;
		var totalElements = elements.length;
		
		for(var i = 0; i < totalElements; i++)
		{
			var ctl = elements[i];
			
			if(ctl.name)
			{
				if(ctl.name.indexOf(controlNamePart) != -1)
				{
					controls[controls.length] = ctl;
				}
			}
		}
		
		return controls;
	}
	
	function getTextboxValue(control)
	{
		return control.value;
	}
	
	function setTextboxValue(control, value)
	{
		control.value = value;
	}
	
	function getDropDownValue(control)
	{
		return control.options[control.options.selectedIndex].value;
	}
	
	function setDropDownValue(control, value)
	{
		var totalOptions = control.options.length;
		
		for(var i = 0; i < totalOptions; i++)
		{
			var opt = control.options[i];
			
			if(opt.value == value)
			{
				opt.selected = true;
				
				return;
			}
		}
	}
	
	function getCheckboxValue(control)
	{
		if(control.checked)
		{
			return control.value;
		}
		return "";
	}
	
	function setCheckboxValue(control, value)
	{
		if(control.value == value)
		{
			control.checked = true;
		}
	}
	
	function getRadioValue(control)
	{
		if(control.checked)
		{
			return control.value;
		}
		
		return "";
	}
	
	function setRadioValue(control, value)
	{
		if(control.value == value)
		{
			control.checked = true;
		}
	}
	
	function getRadioGroupValue(control)
	{
		var totalRadios = control.length;
		
		for(var i = 0; i < totalRadios; i++)
		{
			var rd = control[i];
			
			if(rd.checked)
			{
				return this.getRadioValue(rd);
			}
		}
		
		return "";
	}
	
	function setRadioGroupValue(control, value)
	{
		var totalRadios = control.length;
		
		for(var i = 0; i < totalRadios; i++)
		{
			var rd = control[i];
			
			if(rd.value == value)
			{
				return rd.checked = true;
			}
		}
	}
	
	function addOption(control, optName, optValue)
	{
		var opt = document.createElement("option");
		
		opt.text = optName;
		
		opt.value = optValue;//new Option(optName, optValue);
		
		if(control.options.add)
		{
			control.options.add(opt);
		}
		else
		{
			control.options[control.options.length] = opt;
		}
		
		return opt;
	}
	
	function removeOption(control, optValue)
	{
		var totalOptions = control.options.length;
		
		for(var i = 0; i < totalOptions; i++)
		{
			if(control.options[i].value == optValue)
			{
				this.removeOptionByIndex(control, i)
				return;
			}
		}
	}
	
	function removeOptionByIndex(control, optIndex)
	{
		control.options[parseInt(optIndex)] = null;
	}
	
	// END OF PRIVATE METHODS
	
	
	/**
	 * START OF PUBLIC METHODS
	 */
	
	function GetWindow()
	{
		return this._window;
	}
	 
	function SetWindow(objWin)
	{
		this._window = objWin;
	} 
	
	function OpenWindow(fileName, attributes)
	{
		if(this._childWindow)
		{
			this._childWindow.close();
		}
		this._childWindow = window.open(fileName, '',attributes);
	}
	
	function CloseWindow()
	{
		if(this._childWindow)
		{
			this._childWindow.close();
		}
	}
	
	function GetDropDownListValue(controlName)
	{
		var retValue = new Array();
		
		var control = this.findControl(controlName);
		
		var totalOptions = control.options.length;
		
		for(var i = 0; i < totalOptions; i++)
		{
			var opt = control.options[i];
			
			if(opt.selected)
			{
				retValue[retValue.length] = opt.value;
			}
		}
		
		return retValue;
	}
	
	function SetDropDownListValue(controlName, values)
	{
		var ctl = this.findControl(controlName);

		var totalOptions = ctl.options.length;
		
		for(var i = 0; i < totalOptions; i++)
		{
			var opt = ctl.options[i];
			
			for(var j = 0; j < values.length; j++)
			{
				if(opt.value == values[j])
				{
					opt.selected = true;
				}
			}
		}
	}	
	
	function GetCheckboxGroupValue(groupName)
	{
		var retValue = new Array();
		
		var controls = this.findControls(groupName);
		
		var totalControls = controls.length;
		
		for(var i = 0; i < totalControls; i++)
		{
			var ctl = controls[i];
			
			if(ctl.checked)
			{
				retValue[retValue.length] = this.getCheckboxValue(ctl);
			}
		}
		
		return retValue;
	}
	
	function SetCheckboxGroupValue(controlName, values)
	{
		var controls = this.findControls(controlName);
		
		var totalControls = controls.length;
		
		for(var i = 0; i < totalControls; i++)
		{
			ctl = controls[i];
			
			for(var j = 0; j < values.length; j++)
			{
				if(ctl.value == values[j])
				{
					ctl.checked = true;
				}
			}
		}
	}
	
	function GetControlValue(controlName)
	{
		var ctl = this.getControlByName(controlName);
		
		if(ctl.type)
		{
			if(ctl.type == "text" || ctl.type == "hidden" || ctl.type == "password" || ctl.type == "textarea")
			{
				return this.getTextboxValue(ctl);
			}
			else if(ctl.type == "select-one")
			{
				return this.getDropDownValue(ctl);
			}
			else if(ctl.type == "checkbox")
			{
				return this.getCheckboxValue(ctl);
			}
			else if(ctl.type == 'radio')
			{
				return this.getRadioValue(ctl);
			}
			else
			{
				alert("Not supported control type");
				return "";
			}
		}
		
		if(ctl.length)
		{
			if(ctl[0].type == "radio")
			{
				return this.getRadioGroupValue(ctl);
			}
		}
		
		alert("Not supported type control");
		return "";
	}
	
	function SetControlValue(controlName, value)
	{
		var ctl = this.getControlByName(controlName);
		
		if(ctl.type)
		{
			if(ctl.type == "text" || ctl.type == "hidden" || ctl.type == "password" || ctl.type == "textarea" || ctl.type == "submit" || ctl.type == "button" )
			{
				return this.setTextboxValue(ctl, value);
			}
			else if(ctl.type == "select-one")
			{
				return this.setDropDownValue(ctl, value);
			}
			else if(ctl.type == "checkbox")
			{
				return this.setCheckboxValue(ctl, value);
			}
			else if(ctl.type == 'radio')
			{
				return this.setRadioValue(ctl);
			}
			else
			{
				//alert("Not supported control type");
				return "";
			}
		}
		
		if(ctl.length)
		{
			if(ctl[0].type == "radio")
			{
				return this.setRadioGroupValue(ctl, value);
			}
		}
		
		alert("Not supported type control");
		return "";
	}
	
	function IsChecked(controlName)
	{
		var ctl = this.getControlByName(controlName);
		
		return ctl.checked;
	}
	
	function SetCheckStatus(controlName, checked)
	{
		var ctl = this.getControlByName(controlName);
		
		ctl.checked = checked;
	}
	
	function Check(controlName)
	{
		this.SetCheckStatus(controlName, true);
	}
	
	function Uncheck(controlName)
	{
		this.SetCheckStatus(controlName, false);
	}
	
	function IsOptionExists(controlName, optValue)
	{
		var ctl = this.findControl(controlName);
		
		var total = ctl.options.length;
		
		for(var i = total-1; i >= 0; i--)
		{
			var currentValue = ctl.options[i].value;
			
			if(currentValue == optValue)
			{
				return true;
			}
		}
		
		return false;
	}
	
	function AddOption(controlName, optName, optValue)
	{
		var ctl = this.findControl(controlName);
		
		return this.addOption(ctl, optName, optValue);
	}
	
	function RemoveOption(controlName, optValue)
	{
		var ctl = this.findControl(controlName);
		this.removeOption(ctl, optValue);
	}
	
	function RemoveSelectedOption(controlName)
	{
		var ctl = this.findControl(controlName);
		var index = ctl.options.selectedIndex;
		this.removeOptionByIndex(ctl, index);
	}
	
	function RemoveOptionByIndex(controlName, optIndex)
	{
		var ctl = this.findControl(controlName);
		
		this.removeOptionByIndex(ctl, optIndex);
	}
	
	function GetSelectedIndex(controlName)
	{
		var ctl = this.findControl(controlName);
		return ctl.options.selectedIndex;
	}
	
	function SelectAllOptions(controlName)
	{
		var ctl = this.findControl(controlName);
		var i = 0;
		for(i = 0; i < ctl.options.length; i++)
		{
			ctl.options[i].selected = true;	
		}
	}
	
	function PutFocus(controlName)
	{
		var ctl = this.findControl(controlName);
		if(ctl)
		{
			if(ctl.focus)
			{
				ctl.focus();
			}
			if(ctl.select)
			{
				ctl.select();
			}
		}
		else
		{
			alert( controlName + " not found");
		}
	}
	
	function EnableControl(controlName, defaultBackground)
	{
		var ctl = this.findControl(controlName);
		
		if(ctl)
		{
			ctl.style.backgroundColor = defaultBackground;
			ctl.disabled = false;
		}
		else
		{
			alert( controlName + " not found");
		}
	}
	
	function DisableControl(controlName, disableColor)
	{
		var ctl = this.findControl(controlName);
		
		if(ctl)
		{
			ctl.style.backgroundColor = disableColor;
			ctl.disabled = true;
		}
		else
		{
			alert( controlName + " not found");
		}
	}
	
	function SubmitForm()
	{
		var frm = this.getForm();
		frm.submit();
	}
	
	function GetLayer(lrName)
	{
		var frm = this.getForm();
		if(document.all)
		{
			return document.all[lrName];
		}
		else if(document.getElementById)
		{
			return document.getElementById(lrName);
		}
		else
		{
			return null;
		}
	}
	
	function SetLayerHtml(lrName, contents)
	{
		var lr = this.GetLayer(lrName);
		lr.innerHTML = contents;
	}
	
	
	function __clearList(ctlName)
	{
		ctl = this.findControl(ctlName);
		
		totalOptions = ctl.options.length;
		
		for(var i = totalOptions-1; i >= 0; i--)
		{
			if(ctl.options.remove)
			{
				ctl.options.remove(0);
			}
			else
			{
				ctl.options[0] = null;
			}
		}
	}
		
	// END OF PUBLIC METHODS