﻿/***
jgg - 10/3/06 - added code to handle adding multiple silos for a composite sample via javascript to save postbacks
*/

var _imagesFolder = new String(); // must be set in website code

function clearGridBoxes(gridId) {
    var dataGrid = _getObject(gridId);
    if (isValid(dataGrid)) {
        var boxes = getChildrenOfType(dataGrid, 'text');
        for (var i = 0; i < boxes.length; i++) {
            if (isValid(boxes[i].type))
                boxes[i].value = '';
        }
    }
}

var siloNames = new Array(); // only need a single array for silo names since only one sample will be edited at a time
function addSilos(dropdownID, hiddenID, cellID) {
    var ddl = _getObject(dropdownID);
    var selected = ddl.options[ddl.selectedIndex];
    if (!isValid(siloNames[selected.value]) && (parseInt(selected.value) > 0)) {
        var hdnField = _getObject(hiddenID); var silos = new String(hdnField.value);
        if (hdnField.value.length > 0) { hdnField.value = hdnField.value + ','; }
        hdnField.value = hdnField.value + selected.value;
        siloNames[selected.value] = selected.text;
        showSelectedSilos(hiddenID, cellID);
    }
}
function showSelectedSilos(hiddenID, cellID) {
    var hdnField = _getObject(hiddenID); var cell = _getObject(cellID);
    var silos = new String(hdnField.value); var htext = new String('<span class=normalb>Selected Silos:<br />None</span>');
    if (silos.length > 0) {
        htext = '<table cellpadding=0 cellspacing=0 border=1 class=\'dataGridTable\' style=\'border-color:#dddddd;bgcolor:#eeeeee;\' align=right><tr><td colspan=2 class=normalb><nobr>Selected Silos:</nobr></td></tr>';
        var ids = silos.split(',');
        for (i = 0; i < ids.length; i++) {
            htext = htext + '<tr><td align=left><nobr>' + siloNames[ids[i]] + '</nobr></td>';
            htext = htext + '<td><img src=\'' + _imagesFolder + 'delete.png\' alt=\'Remove ' + siloNames[ids[i]] + '\' title=\'Remove ' + siloNames[ids[i]] + '\' onclick=\'removeSilo("' + hiddenID + '", "' + cellID + '", "' + ids[i] + '");\' style=\'cursor:pointer;\' /></td></tr>';
        }
        htext = htext + '</table>';
    }
    cell.innerHTML = htext;
}
function removeSilo(hiddenID, cellID, siloID) {
    var hdnField = _getObject(hiddenID); var silos = new String(hdnField.value);
    var ids = silos.split(','); var newIds = ''; var newNames = new Array();
    for (i = 0; i < ids.length; i++) {
        if (ids[i] != siloID) {
            if (newIds.length > 0) { newIds = newIds + ','; }
            newIds = newIds + ids[i];
            newNames[ids[i]] = siloNames[ids[i]];
        }
    }
    hdnField.value = newIds;
    siloNames = newNames;
    showSelectedSilos(hiddenID, cellID);
}
/*** ***/

/*
var def_width = 600;
var def_height = 305;
var qc_heightAdjusted = false;
var qc_lastWidth = 0;
var qc_lastHeight = 0;

function resizeTable() //adjustHeight)
{
if ((window.document.documentElement.clientWidth == qc_lastWidth) && 
(window.document.documentElement.clientHeight == qc_lastHeight))
{
window.clearTimeout(resizeTimeoutId);
return;
}
else
{
qc_lastWidth = window.document.documentElement.clientWidth;
qc_lastHeight = window.document.documentElement.clientHeight;
}
    
//alert('resizeTable: ' + window.event);
var tbl = _getObject('dgQCData');
// note: "tbl" should actually be the div that contains the dataGrid table
//alert('dgQCData? ' + isValid(tbl));
//alert('dgQCData = ' + tbl.id + '\ndgQCData.parentNode.parentNode = ' + tbl.parentNode.parentNode.id);
if (isValid(tbl) && isValid(tbl.parentNode))
{
if ((tbl.parentNode.tagName == 'DIV') && (tbl.parentNode.id.indexOf('dgQCData') > -1))
tbl = tbl.parentNode;
else
{
var tmp = tbl.parentNode.parentNode;
if (isValid(tmp) && (tmp.tagName == 'DIV'))
tbl = tmp;
else
tbl = tbl.parentNode;
}
}
        
if(isValid(tbl))
{
try
{
var hScrollVis = false;
var d = window.document;
		    
// first get the width
var w = isValid(window.innerWidth) ? window.innerWidth : ( 
(isValid(d.documentElement) && isValid(d.documentElement.clientWidth)) ? 
d.documentElement.clientWidth : 
(
(isValid(d.body) && isValid(d.body.clientWidth)) ? 
d.body.clientWidth : screen.width
)
);
var subw = isValid(tbl.clip) ? tbl.clip.width : tbl.offsetWidth;
if(tbl.scrollWidth)
subw = (subw < tbl.scrollWidth) ? tbl.scrollWidth : subw;
//alert('subw = ' + subw + ', scrollWidth = ' + tbl.scrollWidth + ', window width = ' + w);
hScrollVis = (parseInt(subw) > parseInt(w));
//alert('sub width = ' + subw + ', window width = ' + w + ', hScrollVis? ' + hScrollVis);
w -= 12;
w = (hScrollVis) ? w : subw;
	        
w -= 40;
	        
//alert('resize, hScrollVis: ' + hScrollVis);
	        
//if((w > 0) && hScrollVis)
if(w > 0)
{
// now get the height
var h = isValid(tbl.clip) ? tbl.clip.height : tbl.offsetHeight;
if (qc_heightAdjusted == false)
{
h += 15;
qc_heightAdjusted = true;
//alert('resize, hScrollVis: ' + hScrollVis);
}
                
//h = (h > def_height) ? def_height : h;
                
//                var h = isValid(tbl.scrollHeight) ?
//	                tbl.scrollHeight :
//	                isValid(tbl.height) ? tbl.height : def_height;
	            
//h = def_height;
//alert('h = ' + h + ', scrollHeight = ' + tbl.scrollHeight + ', Height adjusted? ' + qc_heightAdjusted);
                
// resize the table to correctly fit the contents and window size
tbl.style.width = parseInt(w).toString() + 'px';
		        
//if (isValid(adjustHeight) && (adjustHeight == true))
tbl.style.height = parseInt(h).toString() + 'px';
//tbl.style.border = 'solid 1px #eeeeee';
}
else
{
tbl.style.border = '';
//alert('sub width = ' + subw + ', window width = ' + w + ', hScrollVis? ' + hScrollVis);
}
} catch(ex) {
//alert('exception caught in resizing data table:\n' + ex.message);
}
}
//	else
//	{
//	    alert('could not find data panel to resize!');
//	}
	
return true;
}
*/

var def_width = 600;
var def_height = 305;
var qc_heightAdjusted = false;
var qc_lastWidth = 0;
var qc_lastHeight = 0;
var qc_resizeGridWidth = true; // only false when we want to freeze the header vs. freezing the first few columns
function resizeTable() {
    if ((window.document.documentElement.clientWidth == qc_lastWidth) && (window.document.documentElement.clientHeight == qc_lastHeight)) {
        window.clearTimeout(resizeTimeoutId);
        return;
    }
    else {
        qc_lastWidth = window.document.documentElement.clientWidth;
        qc_lastHeight = window.document.documentElement.clientHeight;
    }
    var tbl = _getObject('dgQCData'); // note: "tbl" should actually be the div that contains the dataGrid table
    if (isValid(tbl) && isValid(tbl.parentNode)) {
        if ((tbl.parentNode.tagName == 'DIV') && (tbl.parentNode.id.indexOf('dgQCData') > -1))
            tbl = tbl.parentNode;
        else {
            var tmp = tbl.parentNode.parentNode;
            if (isValid(tmp) && (tmp.tagName == 'DIV'))
                tbl = tmp;
            else
                tbl = tbl.parentNode;
        }
    }

    if (isValid(tbl)) {
        try {
            var hScrollVis = false; var d = window.document;
            var w = isValid(window.innerWidth) ? window.innerWidth : (
	            (isValid(d.documentElement) && isValid(d.documentElement.clientWidth)) ?
	                d.documentElement.clientWidth :
	                (
	                    (isValid(d.body) && isValid(d.body.clientWidth)) ?
	                        d.body.clientWidth : screen.width
	                )
	        );
            var subw = isValid(tbl.clip) ? tbl.clip.width : tbl.offsetWidth;
            if (tbl.scrollWidth) { subw = (subw < tbl.scrollWidth) ? tbl.scrollWidth : subw; }
            hScrollVis = (parseInt(subw) > parseInt(w));

            //alert('w = ' + w + ', subw = ' + subw + ', resize width? ' + qc_resizeGridWidth + ', hScrollVis? ' + hScrollVis);

            w -= 12;
            if (!hScrollVis || !qc_resizeGridWidth)
                w = subw;
            else
                w -= 40;

            if (w > 0) {
                /*
                var h = isValid(window.innerHeight) ? window.innerHeight : (
                (isValid(d.documentElement) && isValid(d.documentElement.clientHeight)) ?
                d.documentElement.clientHeight :
                (
                (isValid(d.body) && isValid(d.body.clientHeight)) ?
                d.body.clientHeight : screen.height
                )
                );
                var subh = isValid(tbl.clip) ? tbl.clip.height : tbl.offsetHeight;
                alert('h = ' + h + ', subh = ' + subh + ', vScrollVis? ' + (parseInt(subw) > parseInt(w)));
                */

                var h = isValid(tbl.clip) ? tbl.clip.height : tbl.offsetHeight;

                //if (qc_heightAdjusted == false)
                if ((qc_heightAdjusted == false) && hScrollVis) {
                    h += 15;
                    tbl.style.height = parseInt(h).toString() + 'px';
                    qc_heightAdjusted = true;
                }

                tbl.style.width = parseInt(w).toString() + 'px';
                //alert(tbl.style.width + ', ' + tbl.style.display);
            }
            else {
                tbl.style.border = '';
            }
        } catch (ex) { }
    }
    return true;
}




var resizeTimeoutId;
function resizeTableWait() {
    window.clearTimeout(resizeTimeoutId);
    resizeTimeoutId = window.setTimeout('resizeTable();', 10);
}
//window.onresize = doWindowResize;
//window.onresize = resizeTable;

// jgg - 4/10/09 - this is now output in code based on whether or not we're freezing columns vs. the header row (see masterQcData.master.vb)
//addEvent(window, "resize", resizeTableWait);
