/* 
 * 	Show an element by name 
 *	thisName = name of the element to show
 *  tableNum = the number of the table which needs to be shown
 */
function showById(thisName, tableNum){
    allTables = document.getElementsByTagName('table');
        if(allTables[tableNum].getAttribute('name') == thisName) allTables[tableNum].style.display='block';
}
/*
 *  this function does the same as the function above, only for table cells
 */
function showCellByName(thisName, tableNum){
    allCells = document.getElementsByTagName('td');
        if(allCells[tableNum].getAttribute('name') == thisName) allCells[tableNum].style.display='block';
}
/* 
 *	Hide tables by name 
 * 	thisName = name of the element to hide	
 *  tableNum = defines which table on the page to hide
 */
function hideTableById(thisName, tableNum){
    allTables = document.getElementsByTagName('table');
        if(allTables[tableNum].getAttribute('name') == thisName) allTables[tableNum].style.display='none';
}

/*
 * Hide a child node of an element based on the element name
 * thisName - the name to search for in the child elements
 * Element - the element whose children should be searched
 */
function hideChildElementByName(thisName, Element){
    if (Element.childNodes){
        var cN = Element.childNodes;
        for (var x=0, len=cN.length;x<len;x++){
            try{
            if(cN[x].getAttribute('name') == thisName) cN[x].style.display = 'none'; 
            }catch(err){}
        }
    }
}

/*
 *  This will check the specified table rows for a name property.
 *  If the name is found, it will be copied to the printPreview table.
 *  The logo and copyright info are copied to the printPreview table.
 *  tableName - tables to hide
 *  ElementLeftName - name property to search for
 *  ElementRightName - name property to search for
 *  logo_id - id of the eaton logo div
 *  copyright_id - id of the copyright div
 */
function printView(tableName, elementLeftName, elementRightName, logo_id, copyright_id){
    var num = 0; //counter for left column
    var num2 = 0; // counter for right column
    var tab, clone, row, cell;
    var allTables = document.getElementsByTagName("table");
    
    // copy logo 
    if (document.getElementById(logo_id)){      
        clone = document.getElementById(logo_id).cloneNode(true); //item to copy
        document.getElementById("printPreviewMain").getElementsByTagName('tr')[0].getElementsByTagName('td')[0].appendChild(clone);//appends the clone 
    }
    // search all tables to hide    
    for (var i=0, allLength=allTables.length;i<allLength;i++){     
        //if the name should be hidden, hide it    
        if (allTables[i].getAttribute('name') == tableName) hideTableById(tableName,i); 
        tab = allTables[i];  // current table
        //search for all rows to copy 
        for (var x=0, len=tab.rows.length;x<len;x++){
            if (tab.getElementsByTagName('tr')[x].getAttribute('name') == elementLeftName){
                createPreviewCell("printPreviewLeft", tab, tableName, num, x);
                num++;       
            }else if (elementRightName.length > 0 && tab.getElementsByTagName('tr')[x].getAttribute('name') == elementRightName){
                createPreviewCell("printPreviewRight", tab, tableName, num2, x);
                num2++;      
            }                
        }
    }
    
    //copy copyright info
    if (document.getElementById(copyright_id)){
        row = document.getElementById("printPreviewMain").insertRow(2); //create a new row for data        
        cell = row.insertCell(0); //create a new cell for data
        
        cell.style.paddingTop = '8px';
        clone = document.getElementById(copyright_id).cloneNode(true); //item to copy
        cell.appendChild(clone);//appends the clone 
        num++;
    }
}

/*
 * This function creates the row in the specified table for print viewing
 * printPreviewTableName - defines which table to put the content into
 * tableElement - this is the table element
 * tableName - the name of the table/tr that will be hidden
 * printRowCounter - the counter to know which row to add to the table
 * rowCount - current row of the loop when this function was called
 */
function createPreviewCell(printPreviewTableName, tableElement, tableName, printRowCounter, rowCount){
    var clone, row, cell;
    tableElement.getElementsByTagName('tr')[rowCount].setAttribute('name',''); //keeps table rows from repeating
    row = document.getElementById(printPreviewTableName).insertRow(printRowCounter); //create a new row for data
    cell = tableElement.getElementsByTagName('tr')[rowCount].childNodes; 
    //find child nodes to hide
    hideChildElementByName(tableName, tableElement.getElementsByTagName('tr')[rowCount]);
    // get all the cells from the row to append                                    
    for (var y=0,cellCount=cell.length;y<cellCount;y++){
        try{
            clone = cell[y].cloneNode(true);
            row.appendChild(clone);          
        }catch(err){
            //alert(err.message);
        }
    }
}
/* 
 * calls functions to make the page normal again
 * tableName - the table name that was hidden when printview was accessed
 */
function normalView(tableName){
    removePreviewRows();
    displayNormalView(tableName);
}

/* 
 *  displays the tables which were previously hidden for print preview
 *  tableName - the table name that was hidden when printview was accessed
 */
function displayNormalView(tableName){
    var allTables = document.getElementsByTagName("table");
    for (var i=0, allLength=allTables.length;i<allLength;i++){        
        if (allTables[i].getAttribute('name') == tableName) showById(tableName,i);                        
    }
    var allCells = document.getElementsByTagName("td");
    for (var i=0, allLength=allCells.length;i<allLength;i++){        
        if (allCells[i].getAttribute('name') == tableName) showCellByName(tableName,i);                        
    }
}
 
/*
 *  removes the rows that were used for the print preview functionality
 *  from the printPreview table.
 */
function removePreviewRows(){
    var at = document.getElementById("printPreviewLeft");
    var at2 = document.getElementById("printPreviewRight");
    var nodeToRemove;
    
    // remove table rows
    for (i=at.rows.length-1;i>0;i--){    
        at.deleteRow(i);  
    }
    // remove table rows
    for (i=at2.rows.length-1;i>0;i--){    
        at2.deleteRow(i);  
    }
    //remove table divs - removes the eaton logo and copyright from preview page
    for (x=at.getElementsByTagName('div').length-1;x>=0;x--){
        nodeToRemove = at.getElementsByTagName('div')[x];
        nodeToRemove.parentNode.removeChild(nodeToRemove);
    }
     //remove table divs - removes the eaton logo and copyright from preview page
    for (x=at2.getElementsByTagName('div').length-1;x>=0;x--){
        nodeToRemove = at2.getElementsByTagName('div')[x];
        nodeToRemove.parentNode.removeChild(nodeToRemove);
    }
}
function hidePrintPreviewTable(){
    document.getElementById('printPreviewMain').style.display='none';
}
