/*
 * form tools
 *
 * version 1.0 
 *
 */

// check to see if input is whitespace only or empty
function isEmpty(val)
{
    if (val.match(/^s+$/) || val == "")
    {
        return true;
    }
    else
    {
        return false;
    }
} 

// check to see if input is number
function isNumber(val)
{
    if (isNaN(val))
    {
        return false;
    }
    else
    {
        return true;
    } 
}

// check to see if input is numeric
function isNumeric(val) 
{ 
    if (val.match(/^\d+$/) == null) 
        return isNumber(val); 
    else 
        return true; 
} 

function getHiddenFieldValue(id)
{
    var hf = $('#' + id);
    
    var v = hf.get(0).value
    return v;
}

function setHiddenFieldValue(id, v)
{
    var hf = $('#' + id);
    
    hf.get(0).value = v;
}

function getTextBoxValue(id)
{
    var tb = $('#' + id);
    
    var v = tb.get(0).value
    return v;
}

function setTextBoxValue(id, v)
{
    var tb = $('#' + id);
    
    tb.get(0).value = v;
}

function getListBoxSelectValues(id)
{
    var lb = $('#' + id);

	var v = [];
	lb.find("option:selected").each(
		function()
		{
			v[v.length] = this.value;
		}
	);  
	
	return v;
}

function setListBoxSelectValues(id, v)
{
    var lb = $('#' + id);

    for (var i=0; i<lb.get(0).options.length; i++)
    {
        for (var j=0; j<v.length; j++)
        {
            if (lb.get(0).options[i].value == v[j])
            {
                lb.get(0).options[i].selected = true;
            }
        }
    }
}

function getListBoxSelectValue(id)
{
	var v = getListBoxSelectValues(id);
	
	if (v.length >= 1)
	    return v[0];
	else
	    return null;
}

function setListBoxSelectValue(id, v)
{
    var lb = $('#' + id);

    for (var i=0; i<lb.get(0).options.length; i++)
    {
        if (lb.get(0).options[i].value == v)
        {
            lb.get(0).options[i].selected = true;
        }        
    }
}

function getCheckBoxesSelectValues(name)
{
	var v = [];
    $('input[@name=' + name + ']').each(function()
    {
	    if (this.checked)
	        v[v.length] = this.value;
    });
	
	return v;
}

function setCheckBoxesSelectValues(name, v)
{
    $('input[@name=' + name + ']').each(function()
    {
        var hasToBeChecked =  false;
        for (var i=0; i<v.length; i++)
        {
	        if (this.value == v[i])
	            hasToBeChecked = true;
	    }
	    this.checked = hasToBeChecked;
    });
}

function getCheckBoxSelectValue(name)
{
	var v = getCheckBoxesSelectValues(name);
	
	if (v.length >= 1)
	    return v[0];
	else
	    return null;
}

function setCheckBoxSelectValue(name, v)
{
    $('input[@name=' + name + ']').each(function()
    {
        var hasToBeChecked =  false;
        if (this.value == v)
            hasToBeChecked = true;
	    this.checked = hasToBeChecked;
    });
}

function getRadioButtonsSelectValues(name)
{
	var v = [];
    $('input[@name=' + name + ']').each(function()
    {
	    if (this.checked)
	        v[v.length] = this.value;
    });
	
	return v;
}

function setRadioButtonsSelectValues(name, v)
{
    $('input[@name=' + name + ']').each(function()
    {
        var hasToBeChecked =  false;
        for (var i=0; i<v.length; i++)
        {
	        if (this.value == v[i])
	            hasToBeChecked = true;
	    }
	    this.checked = hasToBeChecked;
    });
}

function getRadioButtonSelectValue(name)
{
	var v = getRadioButtonsSelectValues(name);
	
	if (v.length >= 1)
	    return v[0];
	else
	    return null;
}

function setRadioButtonSelectValue(name, v)
{
    $('input[@name=' + name + ']').each(function()
    {
        var hasToBeChecked =  false;
        if (this.value == v)
            hasToBeChecked = true;
	    this.checked = hasToBeChecked;
    });
}