
// checks/uncheks all checkboxes for given fieldname
var checkflag = "false";

function check(field) {
        if (field.length)
        {
            if (checkflag == "false") {
                for (i = 0; i < field.length; i++) {
                field[i].checked = true;}
                checkflag = "true";
                return "Uncheck all"; }
            else {
                for (i = 0; i < field.length; i++) {
                field[i].checked = false; }
                checkflag = "false";
                return "Check all"; }
        }
        else
        {
                if (checkflag == "false")
                {
                        field.checked=true;
                checkflag = "true";
            return "Uncheck all";
                }
                else
                {
                        field.checked=false;
                checkflag = "false";
            return "Check all";
                }
        }

}

// hides/showes element identified by ID
function toggleElementId(elementId)
{
        var el = document.getElementById(elementId);
        if (!el)
                return true;
        
        if (el.style.display == 'none')
                el.style.display = '';
        else
                el.style.display = 'none';
        
        return true;
}


function rpad(val,what,amount)
{
        for (var i=0; i < amount; i++)
                val = val + what;
                
        return val;
}

function lpad(val,what,amount)
{
        for (var i=0; i < amount; i++)
                val = what + val;
        return val;
}


function roundFloat(val,decCount)
{
        if (!val)
                return rpad('0.','0',decCount);
        
        val = Math.round(val*Math.pow(10,decCount))/Math.pow(10,decCount);
        if (Math.floor(val) == val)             
                return String(val) + rpad('.','0',decCount);
        else if (Math.floor(val*Math.pow(10,decCount-1)) == val*Math.pow(10,decCount-1))                
                return String(val) + '0';                               
        
        return String(val);
}


function getIdsAsParams(chkName, paramName)
{
	if (!paramName)
		paramName = 'id';
    var all = document.getElementsByName(chkName);
    var chkIds = '';
    var cnt = 0;
    for(var i=0; i<all.length;i++)
    {
        if (all[i].checked)
        {
            chkIds += '&'+paramName+'['+cnt+']='+all[i].value;
            cnt++;
        }
    }

    return chkIds;            
}


function expandMenuBorder()
{
    var mb = document.getElementById('menu_border');

    if (mb)
    {
        mb.height = (navigator.appName == 'Netscape' ? window.innerHeight : document.body.clientHeight);
    } 
}

function exportList(chkName, paramName, dispatchURL, validationError)
{
    var chkIds = getIdsAsParams(chkName, paramName);
    if (chkIds.length == 0)
    {
        alert(validationError);
        return false;
    }
    else
    {
    	var win = window.open(dispatchURL+chkIds,'prnWin','width=600,height=400,status,menubar,scrollbars,resizable');
    	win.blur();
    	window.focus();
		window.location.reload();//form.submit();
    	return true;
    }
}

function printList(chkName, dispatchURL, validationError)
{
    var chkIds = getIdsAsParams('itemId[]');
    if (chkIds.length == 0)
    {
        alert(validationError);
        return false;
	}        
    else
    {
        jQuery(this).removeClass('nospinner').bind('click', spinTheSpinner).click();
    	window.open(dispatchURL+chkIds,'prnWin','width=600,height=400,status,menubar,scrollbars,resizable');
    }
}

function exportItem(id, dispatchURL, validationError)
{
    if (!id)
    {
        alert(validationError);
        return false;
    }
    else
    {
    	var win = window.open(dispatchURL+'&itemId='+id,'prnWin','width=600,height=400,status,menubar,scrollbars,resizable');
    	win.blur();
    	window.focus();
		window.location.reload();//form.submit();
    	return true;
    }
}

function printItem(id, dispatchURL, validationError)
{
    if (!id)
    {
        alert(validationError);
        return false;
    }
    else
    {
        jQuery(this).removeClass('nospinner').bind('click', spinTheSpinner).click();
    	window.open(dispatchURL+'&itemId='+id,'prnWin','width=600,height=400,status,menubar,scrollbars,resizable');
    	return true;
    }
}


