﻿function CopyToClipBoard(DivName, SectorName,Type) {
    if (navigator.appName.indexOf('Microsoft') == -1) {
        alert('Warning:Your Browser type is: ' + navigator.appName + navigator.appVersion + ' \r\n\r\n It does not support this function.\r\n\r\n Please use Internet explorer.');
    }
    else {
        var div = document.getElementById(DivName)
        //store the orginal innerHTML for recovering
        var text = div.innerHTML;

        //if it is not the table for the chart
        //remove the "Trend graph" and image
        //Type=1,noraml enquiry 
        //Type=2,Chart, Trend, and State Comparison
        var table = null;
        if (document.getElementById('ctl00_cph1_GridViewReport') != null&&Type=='1') {
            table = document.getElementById('ctl00_cph1_GridViewReport');
            for (var i = 0; i < table.rows.length; i++) {
                table.rows[i].deleteCell(table.rows[i].cells.length - 1);
            }
        }
        //add footer
        div.innerHTML += "<div>Source: Australian Greenhouse Emissions Information System, Department of Climate Change and Energy Efficiency. (" + Date() + ")";
        div.innerHTML += "Footnote: These emission estimates are accounted for under the sector '" + SectorName + "'";
        div.innerHTML += "<br/>&copy; 2010 Department of Climate Change and Energy Efficiency.</div>";
        div.contentEditable = 'true';
        //copy the content to the clip board
        var controlRange;
        if (document.body.createControlRange) {
            controlRange = document.body.createControlRange();
            controlRange.addElement(div);
            controlRange.execCommand('Copy');
        }
        //Recover the div because its innerHTML was changed by updating the content
        div.contentEditable = 'false';
        div.innerHTML = text;
        alert("The grid data is copied to the clipboard.");
    }
}
