///////////////////////////////////////////////////////////////////////////////////////
// Function:    CallParentResize()
//
// Description: The webpage is potentially displayed within an IFrame of a parent page.
//              In order to dynamically resize the height so that there is no verticle
//              scroll bars within the IFrame, this function sends a frame resize message
//				to the calling page via the location.hash property using another iFrame via 
//				http://transmission.bchydro.com/messenger.htm.  The parent iFrame id in the 
//				templates that use the iframe must be named "htmlIFrame".
//
// Input:       n/a
// Output:      n/a
//
// Notes:       If the webpage does not sit in an Iframe this function simply does 
//              nothing. It should be harmless in this scenario.
//
//              This method works across domains because the messenger.htm web page takes
//				the location.hash property and sets the height of the appropriate page,
//				which is on the same domain.
//		
//		The try..catch block around the entire method should guarantee that 
//		users will not see any JS errors due to permission or JS version 
//		issues.
//
// History:     Date          Who              Description
//              -----------------------------------------------------------------------
//              Apr 28, 2005  Habanero         Created
//				July 19, 2010 Habanero			Updated for 2010 Rebranding (prev method doesn't work cross domain)
//				July 26, 2010 Habanero			Updated to support text files as source of data (previous method didn't apply to *.txt files in an iframe)
//
///////////////////////////////////////////////////////////////////////////////////////

// some function I found to get doc height across different browser platforms
function getDocHeight(doc, isTextResize)
{
    var docHt = 0, sh, oh;
    if (doc.height)
    {
        docHt = doc.height;
    } else if (doc.body)
    {
        if (doc.body.scrollHeight) docHt = sh = doc.body.scrollHeight;
    }
    return docHt;
}

// This function creates a hidden iframe to the messenger page
// in the parent page's domain and passes a msg via the hash value.
// We can call this on window load and should also call it
// whenever there might be something that alters the page height.
function sendResizeHashMsg(isTextResize)
{
    // Append a div that will clear any floats to ensure that the page
    // will have layout.
    var clearing_div = document.createElement('div');
    clearing_div.setAttribute('id', 'clearingDiv');
    clearing_div.style.clear = 'both';
    document.body.appendChild(clearing_div);

    // Append an iframe that will serve as our messaging conduit
    var iframe = document.getElementById('msgIframe');
    if (iframe)
    {
        document.body.removeChild(iframe);
    }
    // Ensure that it has an id, and give it our page with location hash being
    // the height of our document
    iframe = document.createElement('iframe');
    iframe.setAttribute('id', 'msgIframe');
    iframe.setAttribute('src', "http://transmission.bchydro.com/messenger.htm#" + getDocHeight(document, isTextResize));
    iframe.style.display = 'none';
    document.body.appendChild(iframe);

}

function extractTextFileToHtml(destDiv)
{
    // ensure that we have a destination DIV before we begin
    if (null !== destDiv)
    {
        // Retrieve the text filename from the query string
        var query_str = window.location.search;
        if (null !== query_str)
        {
            var query_str_wo_amp = query_str.substring(1);
            var arr = query_str_wo_amp.split('&');

            // Just support one file, so we're just going to look at the first argument
            var index = arr[0].indexOf('=');
            var key = arr[0].substring(0, index);
            var value = arr[0].substring(index + 1);

            var all_text = '';
            var lines = '';
            var txt_file = null;
            var new_html = '';

            // Retrieve an XML Http request (note there are differences in support between IE6 and IE 7 and above)
            if (window.XMLHttpRequest)
            {
                // If IE7, Mozilla, Safari, and so on: Use native object.
                txt_file = new XMLHttpRequest();
            }
            else
            {
                if (window.ActiveXObject)
                {
                    // ...otherwise, use the ActiveX control for IE5.x and IE6.
                    txt_file = new ActiveXObject('MSXML2.XMLHTTP.3.0');
                }
            }

            if (null !== txt_file)
            {
                txt_file.open("GET", value, true);
                txt_file.onreadystatechange = function()
                {
                    if (txt_file.readyState === 4)
                    {  // Makes sure the document is ready to parse.
                        if (txt_file.status === 200)
                        {  // Makes sure it's found the file.
                            all_text = txt_file.responseText;
                            lines = txt_file.responseText.split("\n"); // Will separate each line into an array							 

                            // Go through each line in the file and wrap it in a <p></p> tag
                            if (null !== destDiv)
                            {
                                for (var i = 0; i < lines.length; i++)
                                {
                                    new_html += '<p>' + lines[i] + '</p>';
                                }

                                // Set the HTML for our destination DIV and resize the iFrame for this new content
                                destDiv.innerHTML = new_html;
                                sendResizeHashMsg(true);
                            }
                        }
                    }
                };
                txt_file.send(null);
            }
        }
    }
}

// This is a dummy function that will call the hash resize function (this is to ensure that source pages can
// stay the same.
function CallParentResize(iframeName)
{
    // First determine if our base html file has an element called "TextFileContent", this would signal that
    //		we're wrapping a text file in an HTML file
    var dest_div = document.getElementById('TextFileContent');
    if (null !== dest_div)
    {
        extractTextFileToHtml(dest_div);
    }
    else
    {
        sendResizeHashMsg(false);
    }
}
