// === SweetReport Javascript Functions === //

var swt_check_boxes = true;
function swt_toggle_checks(theFormname, theFieldName)
{
	// static swt_check_boxes = true;
	var theForm = document.forms[theFormname];
	if (theForm)
	{
		for (i=0, n=theForm.elements.length; i<n; i++)
		{
			if (theForm.elements[i].name.indexOf(theFieldName) != -1) { theForm.elements[i].checked = swt_check_boxes; }
		}
		swt_check_boxes = !swt_check_boxes;
	}
}

function swt_hilight_row(theRow, theColor)
{
	if (typeof(theRow.style) == 'undefined' || typeof(theRow.cells) == 'undefined') { return false; }
	var row_cells_cnt = theRow.cells.length;
	OldColor = theRow.cells[0].bgColor; 	// Save the old color
	for (var i = 0; i < row_cells_cnt; i++) {	theRow.cells[i].bgColor = theColor; }
	return true;
}


function swt_submit(formId,url,varName,values,doPrompt)
{

    // Our form object
    var frm = document.forms['swt_frm' + formId];
    // we have a url just use that as the action
    if (url) { 
        frm.action = url; 

    // otherwise we need to build our url
    } else { 

        // prompt the user if specified
        if (doPrompt) { 

            // explode value string into array
            values = unescape(values);
            var arrVals = values.split(';'); 

            // ask user to change value for THIS instance
            arrVals[formId] = prompt(doPrompt, arrVals[formId] ); 

            // If they cancelled don't do anything
            if (arrVals[formId] == null) { 
                return false; 

            // otherwise put the values string back together
            } else {
                values = arrVals.join(';');
            }

        } 

        // build url and set action
        url = varName + '=' + values; 
		//if (sre_special_url) { frm.action = sre_special_url + '?' + url; }
		//else { frm.action = '?' + url; }
frm.action = '?' + url;
    }

	// alert('url = ' + url + ', action = ' + frm.action);
	//return false; 
    // submit form
    frm.submit();

}


var group_state = new Array;
function swt_gr_toggle(id,display,stoprecur,level) {

    if (!level) { level = 0; }
    level++; // increment by one

    // bullets are the +/- toggle icons
    bullet_1 = document.getElementById('swt_tgl_' + id + '_1'); 
    bullet_0 = document.getElementById('swt_tgl_' + id + '_0'); 

    // hide 'loading..' text
    //loading  = document.getElementById('swt_ldng'); 
    loading  = document.getElementById('swt_tgl_ld_' + id); 
    if (loading.style.display == 'block')  { loading.style.display = 'none'; }


    // figure out what to do based on current display state
    if (!display) {
        if (document.getElementById('swt_r_' + id + '-1').style.display == '') {
            display = 'off'; // its currently visible, so lets turn it off 
            stoprecur = 0; // when collapsing, do it to all levels 
        } else {
            display = 'on'; // its current invisible, so lets turn it on 
        }
    }

    // toggle the +/- indicator
    if (display=='on') {
        rowstyledisplay = '';
        if (bullet_0) bullet_0.style.display = '';
        if (bullet_1) bullet_1.style.display = 'none';
    } else {
        rowstyledisplay = 'none';
        if (bullet_0) bullet_0.style.display = 'none';
        if (bullet_1) bullet_1.style.display = '';
    }
    
    
    // and toggle rows
    var i = 1;
    while (row = document.getElementById('swt_r_' + id + '-' + i)) {
        row.style.display = rowstyledisplay;
        if (!stoprecur) { // recur unless we told it to stop - used to for opening up branches at one level at a time 
            // check for children branches and run recursively 
            if (document.getElementById('swt_r_' + id + '-' + i + '-1')) { 
                swt_gr_toggle(id + '-' + i,display,stoprecur,level); 
            } 
        } else { 
            if (row = document.getElementById('swt_r_' + id + '-' + i + '_sub')) { 
                row.style.display = rowstyledisplay; 
            } 
        } 
        i++; 
    } 

    // toggle subtotal row too - only when its NOT on the same level
    if (level > 1) {
        if (row = document.getElementById('swt_r_' + id + '_sub')) {
            row.style.display = rowstyledisplay;
        }
    }
    
    // hide header thingy
    if (row = document.getElementById('swt_r_' + id + '-hdr')) {
        row.style.display = rowstyledisplay;
    }
    
   
}



function swt_gr_expand_all(pos) {
    var i = 1;
    while (row = document.getElementById('swt_r_' + i)) {
        swt_gr_toggle(i++,'on',0);
    }
    if (pos == 1)  { 
        document.getElementById('swt_ld_1').style.display = 'none'; // hide 'loading..' indicator 
    } 
    if (pos == 2)  {
        document.getElementById('swt_ld_2').style.display = 'none'; // hide 'loading..' indicator 
    }
}

function swt_gr_collapse_all(pos) {
    var i = 1;
    while (row = document.getElementById('swt_r_' + i)) {
        swt_gr_toggle(i++,'off',0);
    }
    if (pos == 1)  { 
        document.getElementById('swt_ld_1').style.display = 'none'; // hide 'loading..' indicator 
    } 
    if (pos == 2)  {
        document.getElementById('swt_ld_2').style.display = 'none'; // hide 'loading..' indicator 
    }
}

