var plusImage = 'icoPlus.gif';
var minusImage = 'icoMinus.gif';

// If "Expand/Contract All", then all items must be done in sync
// Otherwise just toggle individual item

function clickOpenClose(openCloseItem)
{
    // openCloseItem is the ID of the row to be shown or hidden
    // Of the form OpenCloseItemAll, OpenCloseItem0, OpenCloseItem1...

    // Cancel href
    window.event.returnValue = false;

    // Handle errors generically
    try
    {
        // Get top-level table object
        var openCloseTable = document.body.all.item('OpenCloseTable');

        // actionImage is the plus/minus image to base the action on
        // ID is of the form OpenCloseImageAll, OpenCloseImage0, OpenCloseImage1...
        var actionImage = openCloseTable.all.item(openCloseItem.replace('Item', 'Image'));

        // Determine new image-src and style-display
        if (actionImage.src.indexOf(plusImage) != -1)
        {
            // Plus to Minus
            var newSrc = actionImage.src.replace(plusImage, minusImage);
            var newDisplay = 'block';
            
            // actionImage always changes
            actionImage.src = newSrc;
        }
        else
        {
            // Minus to Plus
            var newSrc = actionImage.src.replace(minusImage, plusImage);
            var newDisplay = 'none';

            // actionImage always changes
            actionImage.src = newSrc;
        }

        // Perform the open/close(s)
        // Rows to be opened/closed have class='OpenCloseChild' and ID=openCloseItem
        if (openCloseItem == 'OpenCloseItemAll')
        {
            // Do all OpenCloseChild rows in sync
            var childImageID, childImage;
            var oRows = openCloseTable.rows;
            for (var i = 0; i < oRows.length; i++)
            {
                if (oRows(i).className.toString() == 'OpenCloseChild')
                {
                    // Get image associated with this OpenCloseChild row
                    childImageID = oRows(i).id.replace('Item', 'Image');
                    childImage = openCloseTable.all.item(childImageID);

                    // Do this row and this image
                    childImage.src = newSrc;
                    oRows(i).style.display = newDisplay;
                }
            }
            
        }
        else
        {
            // Do this OpenCloseChild row
            var oRow = openCloseTable.all.item(openCloseItem);
            
            // Do this row; this image is the action image
            oRow.style.display = newDisplay;
        }

    }
    
    catch(e)
    {
        // Prevent popup jscript errors
        // TODO: Maybe log these at some future date
    }
}

// This function prepares a "mailto:" link to send details about current page by email
// using default email software installed on client
function MailThisPage()
{
    var i;
    var subject = document.location.href;
    var email = "";
    var body = subject;
    var pageTitle = "Goowen";
    subject = "Information from Goowen.com";
    try
    {
        // try to use content of meta-description as body
        var metaElements = document.getElementsByTagName("meta");
        if(metaElements.length > 0)
        {
            if(document.title == null || document.title == "unknown")
            {
                pageTitle = "Goowen";
            }
            else
            {
                pageTitle = document.title;
            }
            if(pageTitle == "")
            {
                pageTitle = "Goowen";
            }
            for(i = 0; i < metaElements.length; i++)
            {
                if (metaElements[i].name == "description")
                {
                   body = pageTitle + "\n" + "\n" + metaElements[i].content + "\n" + "\n" + body;
                }
                if (metaElements[i].name == "PType")
                {
                   if(metaElements[i].content == "av")
                   {
                       subject = "Article from Goowen.com";
                   }
                }
            }
        }
    }
    catch(e)
    {
    }
    
    document.location.href="mailto:" + email + "?subject=" + escape(subject) + "&body=" + escape(body);
}

// Show printer friendly view for this page. Which is pretty much adding pf=true
// in query string.
function PrinterFriendlyView()
{
    var url = document.location.href.split("#");
    var pfUrl;
    if (url[0].indexOf('?') > 0)
    {
        pfUrl = url[0] + '&pf=true';
    }
    else
    {
        pfUrl = url[0] + '?pf=true';
    }
    
    if (url.length > 1)
    {
        pfUrl += '#' + url[1];
    }
    
    var pfWindow = window.open(pfUrl, 'Goowen_PrinterFriendlyView', '');
    try
    {
        // if window with this name exists, make sure it comes to focus
        pfWindow.focus();
    }
    catch(e)
    {
    }
}

// In case a script error occured due to page not beeing properly loaded
// handle it....
function HandleUnexpectedScriptError(e)
{
    // TODO: add proper handling for unexpected script errors.
    // Possibly direct to some error.aspxl
    document.body.innerHTML = '<h1 align="center">Unable to load the page properly. Please reload.</h1>';
}

