// Javascript form control functions

function clearForm(ref)
{
    var obj = getObj(ref);
    if (obj) {
        for(i=0; i<obj.length; i++) {
            var elem = obj.elements[i];

            // do not clear the value of buttons and hidden form elements
            if (elem.type == 'submit' || elem.type == 'reset' || elem.type == 'button' || elem.type == 'hidden') {
                continue;
            } else {
                elem.value = '';
            }
        }
    }
    return false;
}

function clickOnce(ref)
{
    var obj = getObj(ref);
    if (obj) {
        if (document.all || document.getElementById) {
            obj.disabled = true;
        } else {
            obj.onclick = null;
            obj.value = 'clicked';
        }
    }
    return true;
}

function confirmSubmit(msg)
{
    var msg = init(msg, 'Are you sure?');
    if (confirm(msg)) {
        return true;
    } else {
        return false;
    }
}

// TEXTAREA
function adjHeight(ref, amount, limit, op)
{
	var op = init(op, '+');

	var limit = init(limit, '100');
	limit = amount.replace(/px/i, '');
	limit = Number(limit);

	var amount = init(amount, '200');
	amount = amount.replace(/px/i, '');
	amount = Number(amount);
	
	var height = getStyle(ref, 'height');
	height = height.replace(/px/i, '');
	height = Number(height);

	if (op === '-') {	
		new_height = height - amount;
		if (new_height > limit) {
			new_height = limit;
		}
	} else {
		new_height = height + amount;
		if (new_height < limit) {
			new_height = limit;
		}
	}

	setStyle(ref, 'height', new_height +'px');
}
function upHeight(ref, amount, max)
{
	var amount = init(amount, '200');
	var max = init(max, '1000');
	adjHeight(ref,amount,max,'+');
}

function downHeight(ref, amount, min) 
{ 
	var amount = init(amount, '200');
	var min = init(min, '100');
	adjHeight(ref,amount,min,'-');
}

//  RADIO & CHECKBOX
function toggleChecked(ref)
{
    var obj = getObj(ref);
    if (obj) {
        obj.click();
    }
}
function checkRadio(a) {toggleChecked(a);}
function checkBox(a) {toggleChecked(a);}

function setChecked(ref)
{
    var obj = getObj(ref);
    if (obj) {
        if (obj.checked == undefined) {
            obj.click();
        }
    }
}

function toggleDisabled(ref)
{
    var obj = getObj(ref);
    if (obj) {
        if (document.all || document.getElementById) {
            if (obj.disabled === true) {
                obj.disabled = false;
            } else {
                obj.disabled = true;
            }
        } else {
            if (obj.onclick !== null) {
                obj.onclick = null;
            }
        }
    }
}

 
/* Puts all the selected elements from a multi-select box into CSV
 * and puts it in a form control
 * @param DOM obj form 
 * @param string NAME - name of multi-select control
function getSelected(form, NAME) {
    NAME = init(NAME, 'SELECTED');
    MULTI_SELECT = form.elements[NAME];
    if (MULTI_SELECT == undefined) {
        return false;
    } else if (MULTI_SELECT.options.length == 0) {
        return false;
    } else {
        var VALUES = '';
        for (i=0; i<MULTI_SELECT.length; i++) {
            MULTI_SELECT.options[i].selected;
            VALUES = VALUES + MULTI_SELECT.options[i].value;

            // add a comma if not at the end
            if (i != (MULTI_SELECT.length-1)) {
                VALUES = VALUES + ',';
            }
        }
        MULTI_SELECT.form.elements[NAME + '_VALUES'].value = VALUES;
    } 
}
 */

