/*
* Filename	: form.js
* Function	:
* Comment	:
* History		: 2003/05/09, jerry, setup
*									2006/06/02, litlhope, modify
*
* Version	: 1.0
* Author		: Copyright (c) 2005 by JcomTech Corp. All Rights Reserved.
*/

/***************************************************
*	element
****************************************************/
 
/* is element */
function isElement(form) {
	var args = isElement.arguments;
	var elementCount = form.elements.length;
	var elementType = "checkbox";
	
	if (args.length == 2) {elementType = args[1];}
	
	for (inx = 0; inx < elementCount; inx++) {
		if (form.elements[inx].type == elementType) {return true;}
	}

	return false;
}

/* is checked */
function isChecked(obj) {
	var flag = false;
		
	if (obj.tagName == "FORM") {flag = isCheckedForm(obj);}
	else {flag = isCheckedElement(obj);}
	
	return flag;
}

function isCheckedForm(form) {
	var args = isChecked.arguments;
	var elementCount = form.elements.length;
	var elementType = "checkbox";
	
	if (args.length == 2) {elementType = args[1];}

	for (inx = 0; inx < elementCount; inx++) {
		if (form.elements[inx].type == elementType && form.elements[inx].checked) {return true;}
	}

	return false;
}

function isCheckedElement(obj) {
	if (obj.length > 0) {
		for (inx = 0; inx < obj.length; inx++) {
			if (obj[inx].checked) {return true;}
		}
	} else {
		if (obj.checked) {return true;}
	}

	return false;
}

/***************************************************
*	checkbox
****************************************************/

/* checked all checkbox */
function checkedAll(form, nm) {
	var elementCount = form.elements.length;

	for (inx = 0; inx < elementCount; inx++) {
		if (form.elements[inx].type == "checkbox" && form.elements[inx].name == nm) {form.elements[inx].checked = true;}
	}
}

/* unchecked all checkbox */
function uncheckedAll(form, nm) {
	var elementCount = form.elements.length;

	for (inx = 0; inx < elementCount; inx++) {
		if (form.elements[inx].type == "checkbox" && form.elements[inx].name == nm) {form.elements[inx].checked = false;}
	}
}

/* is checkbox element */
function isCheckbox(obj) {return isElement(obj, "checkbox");}

/* get checked values */
function getCheckedValues(form, elementName, div) {
	var elementCount = form.elements.length;
	var result = "";

	for (inx = 0; inx < elementCount; inx++) {
		if (form.elements[inx].name == elementName && form.elements[inx].checked) {
			result += form.elements[inx].value + div;
		}
	}
	
//	if (result.length > 0) {result = result.substring(0, result.length - 1);}
	
	return result;
}

/***************************************************
*	radio
****************************************************/

function isRadio(form) {return isElement(form, "radio");}

function isCheckedRadio(form) {return isChecked(form, "radio");}

function enabledRadio(radio) {
	for (i = 0; i < radio.length; i++) {radio[i].disabled = false;}	
}

function disabledRadio(radio) {
	for (i = 0; i < radio.length; i++) {radio[i].disabled = true;}	
}

/***************************************************
*	select(combo-box)
****************************************************/

/* select option */
function selectOption(combo, optVal) {
	for (i = 0; i < combo.length; i++) {
		if (combo.options[i].value == optVal)	{
			combo.options[i].selected = true;
			break;
		}
	}
}

/* selected all option(for multi) */
function selectedAll(combo) {
	for (i = 0; i < combo.length; i++) {combo.options[i].selected = true;}
}

/* unselected all option(for multi) */
function unselectedAll(combo) {
	for (i = 0; i < combo.length; i++) {combo.options[i].selected = false;}
}

/* option flag */
function isOption(combo, optVal) {
	for (i = 0; i < combo.length; i++) {
		if (combo.options[i].value == optVal)	{return true;}
	}
	return false;
}

/* add option */
function addOption(combo, option) {
	combo.options[combo.length] = option;
}

function addOptions(combo, options) {
	for (i = 0; i < options.length; i++) {combo.options[i] = options[i];}
}

/* remove option */
function removeSelectedOptions(combo) {	
	var size = combo.length;
	for (i = size - 1; i >= 0; i--) {
		if (combo.options[i].selected) {combo.options[i] = null;}
	}
}

function removeOptions(combo) {	
	var size = combo.length;
	for (i = size - 1; i >= 0; i--) {combo.options[i] = null;}
}

/* change option */
function changeOptions(combo, options) {
	removeOptions(combo);
	addOptions(combo, options);
}

/* move option(for multi) */
function moveOptions(srcCombo, targetCombo) {
	var size = srcCombo.length;
	
	for (i = size - 1; i >= 0; i--)	{
		if (srcCombo.options[i].selected)	{
			targetCombo.options[targetCombo.length] = new Option(srcCombo.options[i].text, srcCombo.options[i].value);
			srcCombo.options[i] = null;
		}
	}
}

/* exchange option(for multi) */
function exchangeOptions(combo1, combo2) {
	moveOptions(combo1, combo2);
	moveOptions(combo2, combo1);
}

/* submit */
function submitSelectedOption(combo) {
	combo.form.rows.value = combo.options[combo.selectedIndex].value;
	combo.form.submit();
}
