//Determines if image size falls within an acceptable range i.e. less than the client's screen size.
function correctDimension(thumbnail)
{
    if(thumbnail)
    {
        if(thumbnail.width > 0 && thumbnail.width < screen.availWidth)
            if(thumbnail.height > 0 && thumbnail.height < screen.availHeight)
                return true;
    }
    return false;
}//correctDimension()

//Opens image in a new window at the correct size
function thumbnailViewer(thumbnail)
{
    if(thumbnail)
    {
        var thumbnailWindow;
        var fullImage;
        thumbnailWindow = window.open(thumbnail.src , "", "scrollbars=yes, toolbar=no, resizable=yes, left=0, top=0, width=" + thumbnail.width + ", height=" + thumbnail.height);
        thumbnailWindow.document.write('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>' + thumbnail.alt + '</title></head>');
        thumbnailWindow.document.write('<body><img id="fullImage" onclick="self.close()" src="' + thumbnail.src + '" alt="' + thumbnail.alt + '" /></body>');
        
        fullImage = thumbnailWindow.document.getElementById("fullImage")
        if(fullImage)
        {
            if(correctDimension(fullImage))
            {
        	    if(navigator.appName == "Netscape")
        	    {
                    thumbnailWindow.resizeTo(fullImage.width + 16, fullImage.height + 58);
                }
                else if (navigator.appName == "Microsoft Internet Explorer")
                {
            	    thumbnailWindow.resizeTo(fullImage.width + 33, fullImage.height + 88);
                }
                else if (navigator.appName == "Opera")
                {
            	    thumbnailWindow.resizeTo(fullImage.width + 18, fullImage.height + 51);
                }
                else
                {
            	    thumbnailWindow.resizeTo(fullImage.width + 18, fullImage.height + 58);
                }
            }//if(correctDimension(fullImage))
            else
            {
                thumbnailWindow.resizeTo(screen.availWidth - 64, screen.availHeight - 64);
            }
        
            thumbnailWindow.document.body.style.margin = "0px";
            thumbnailWindow.document.body.style.padding = "0px";
            thumbnailWindow.document.body.style.borderStyle = "none";
            thumbnailWindow.document.close();
            thumbnailWindow.focus();
        }
    }//if(thumbnail)
}//thumbnailViewer()

//<!--

//Converts feature page to a layout suiitable for printing.
function PrintFeature()
{ 
	var windowSpecifications;
	var printLayoutWindow;
	var featureHTML;
	 
	windowSpecifications = "toolbar=yes,location=no,directories=yes,menubar=yes,"; 
    windowSpecifications += "scrollbars=yes,width=750,height=600,left=100,top=25"
    
    featureHTML = '<html xmlns="http://www.w3.org/1999/xhtml">\n<head>\n<title>' + document.title + '</title>\n<link rel="stylesheet" type="text/css" href="/portal/page/portal/BIG_Portal_Home_Page/Page%20Portlets/DocumentLibraryPortlets/BIGPortal.css" />\n</head>\n<body>'
	featureHTML += document.getElementById('mainFeatureImage').innerHTML; 
	featureHTML += document.getElementById('featureBody').innerHTML;
	featureHTML += '</body>\n</html>';

	printLayoutWindow = window.open("", "", windowSpecifications);
	printLayoutWindow.document.write(featureHTML );
	printLayoutWindow.document.close(); 
	printLayoutWindow.focus();
	printLayoutWindow.print();
	history.go(-1);			//modified by K.alleyne 2009-06-29; 4:14 pm
}


//Converts FAQ answer page to a layout suitable for printing.
function PrintFAQ()
{ 
	var windowSpecifications;
	var printLayoutWindow;
	var faqHTML;
	 
	windowSpecifications = "toolbar=yes,location=no,directories=yes,menubar=yes,"; 
    windowSpecifications += "scrollbars=yes,width=750,height=600,left=100,top=25"
    
    faqHTML = '<html xmlns="http://www.w3.org/1999/xhtml">\n<head>\n<title>' + 'Printing FAQ' + '</title>\n<link rel="stylesheet" type="text/css" href="/portal/page/portal/BIG_Portal_Home_Page/Page%20Portlets/DocumentLibraryPortlets/BIGPortal.css" />\n</head>\n<body>'
	faqHTML += document.getElementById('highlightSectionTitle').innerHTML; 
	faqHTML += document.getElementById('featureBody').innerHTML;
	faqHTML += '</body>\n</html>';

	printLayoutWindow = window.open("", "", windowSpecifications);
	printLayoutWindow.document.write(faqHTML);
	printLayoutWindow.document.close(); 
	printLayoutWindow.focus();
	printLayoutWindow.print();
	history.go(-1);
}



//Converts a feature to a PDF document
function ConvertFeaturetoPDF() 
{
    var sWinHTML = '<html xmlns="http://www.w3.org/1999/xhtml">\n<head>\n<title>' + document.title + '</title>\n<link rel="stylesheet" type="text/css" href="/portal/page/portal/BIG_Portal_Home_Page/Page%20Portlets/DocumentLibraryPortlets/BIGPortal.css" />\n</head>\n<body>\n';
    
    sWinHTML = sWinHTML + document.getElementById('mainFeatureImage').innerHTML; 
    sWinHTML = sWinHTML + document.getElementById('featureBody').innerHTML;
    sWinHTML = sWinHTML + '</body>\n</html>';
    
    document.params.htmlCode.value = sWinHTML;
    document.params.submit();

}//-->

//Function finds and returns the anchor element which triggered the event
function GetAnchorTarget(e)
{
	var target;
	
	if(!e)
	{
		e = window.event;
	}

	if (e.currentTarget)
	{
		target = e.currentTarget;
	}
	else if(e.srcElement)
	{
		target = e.srcElement;
	}
	
	if(target.nodeType == 3)
	{
		target = target.parentNode;
	}
	
	if(target && target.tagName)
	{
		while(target != null && target.tagName.toLowerCase() != "a")
    	{
			target = target.parentNode;
    	}
    }
	return target;
}//GetAnchorTarget

//Function determines when a link is not under barbados.gov.bb and displays a disclaimer page.
function DisplayExternalWebsiteDisclaimer(event)
{	
	var e = event;
    var url = "";
    var anchorTarget;
    var urlRegularExpression = /^(http[s]?:\/\/)?((([a-zA-Z]+[a-zA-Z0-9]*\.)?barbados)|www)\.gov\.bb/i;
    var mailtoOrJavascriptRegularExpression = /^(mailto)|(javascript):/i;
    var disclaimerWindow;
    var pageHTML;
    var nextPage;
    var windowSpecifications;
    
    //Get the event regardless of the browser.
    if(!e)
	{
		e = window.event;
	}
	
	//Cancel the default event
	if(e.preventDefault)	//W3C DOM
	{
		e.preventDefault();
	}
	else if(e.returnValue  || navigator.appName == "Microsoft Internet Explorer")	//IE DOM
	{
		e.returnValue = false;
	}

    anchorTarget = GetAnchorTarget(event);
    externalLink = anchorTarget.href;

    if(externalLink)
    {
        url = "" + externalLink;
        if(url.match(urlRegularExpression) || url.match(mailtoOrJavascriptRegularExpression))
        {
            window.open(url, "_self");
        }
        else
        {
        	windowSpecifications = "width=440, height=240, toolbar=no, scrollbars=no, resizable=no, location=no, status=no, menubar=no, directories=no" 
        	
        	if((screen.availWidth / screen.availHeight) <= 1.4)	//Screeen Ratio - 4:3
        	{
            	windowSpecifications += ", left=" + (screen.availWidth / 6);
            	windowSpecifications += ", top=" + (screen.availHeight / 5);
        	}
        	else if((screen.availWidth / screen.availHeight) <= 1.6)	//Screeen Ratio - 8:5
        	{
            	windowSpecifications += ", left=" + (screen.availWidth / 8);
            	windowSpecifications += ", top=" + (screen.availHeight / 5);
        	}
        	
            disclaimerWindow = window.open("", "_blank", windowSpecifications);
            pageHTML = '<html xmlns="http://www.w3.org/1999/xhtml">' + "\n"
			pageHTML += '<head>' + "\n"
			pageHTML += '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' + "\n"
			pageHTML += '<title>Disclaimer</title>' + "\n"
			pageHTML += '<link rel="stylesheet" type="text/css" href="/portal/page/portal/BIG_Portal_Home_Page/Page%20Portlets/DocumentLibraryPortlets/BIGPortal.css" />' + "\n"
			pageHTML += '</head>' + "\n"
			pageHTML += '<body style="background-color:#EFF1F2">' + "\n"
			pageHTML += '<div class="Disclaimer">' + "\n"
			pageHTML += '<h1 class="DisclaimerTitle">Disclaimer</h1>' + "\n"
			pageHTML += '<p class="DisclaimerText">The following link will take you from the Barbados Integrated Government Portal (<a href="http://www.gov.bb/">http://www.gov.bb</a>).  The Barbados Integrated Government Portal does not control the content on this website.  Click <a class="DisclaimerText" href="javascript:window.close();" onclick="opener.open(\'' + url + '\', \'_self\');"><strong>Continue</strong></a> or <a class="DisclaimerText" href="javascript:window.close();"><strong>Go Back</strong></a>.</p>' + "\n"
			pageHTML += '<p class="DisclaimerContinuePosition"><a class="DisclaimerContinuePosition" id="DisclaimerContinueLink" href="javascript:window.close();" onclick="opener.open(\'' + url + '\', \'_self\');">Continue</a></p>' + "\n"
			pageHTML += '<p class="DisclaimerGoBackPosition"><a class="DisclaimerGoBackPosition" id="DisclaimerGoBackLink" href="javascript:window.close();">Go Back</a></p>' + "\n"
			pageHTML += '</div>' + "\n"
			pageHTML += '</body>' + "\n"
			pageHTML += '</html>' + "\n"
			disclaimerWindow.document.write(pageHTML);
            nextPage = disclaimerWindow.document.getElementById("DisclaimerContinueLink");
            disclaimerWindow.document.close();
        }
    }
}//externalWebsiteDisclaimer()

//Function adds the DisplayExternalWebsiteDisclaimer to click/onlick in all anchors.
function AddDisclaimerToAllAnchorsInCurrentPage()
{
	var anchor;
	var anchorSet = document.getElementsByTagName("a");
	
	if(anchorSet)
	{
		if(anchorSet[0].addEventListener)	//W3C DOM
    	{
        	for (anchor = 0; anchor < anchorSet.length; anchor++)
        		anchorSet[anchor].addEventListener('click', DisplayExternalWebsiteDisclaimer, false);
        }
    	else if(anchorSet[0].attachEvent)	//IE DOM
    	{
    		for (anchor = 0; anchor < anchorSet.length; anchor++)
        		anchorSet[anchor].attachEvent('onclick', DisplayExternalWebsiteDisclaimer);
    	}
    }//if(anchorSet)
}//AddDisclaimerToAnyAnchorInCurrentPage


//function hides and collapses image placeholders without any images
function HideImagePlaceHoldersWithNoImage()
{
	var image;
	var imageCounter;
	var imageSet = document.getElementsByTagName("img");
	var imageClassName;
	
	if(imageSet)
	{
		for(imageCounter = 0; imageCounter < imageSet.length; imageCounter++)
		{
			image = imageSet[imageCounter];
			if(!image.complete)	//IE, Opera
			{
				image.style.display = 'none';
			}
		}
	}
}//HideImagePlaceHoldersWithNoImage

function RefreshIFramesInIE()
{
    var listOfIFrames = document.getElementsByName("navigationFrame");
    var iFrameCounter = 0;
    var singleIFrame;
    
    if(navigator.appName == "Microsoft Internet Explorer")
    {
        if(listOfIFrames)
        {
            for(iFrameCounter = 0; iFrameCounter < listOfIFrames.length; iFrameCounter++)
            {
                singleIFrame = listOfIFrames[iFrameCounter];
                if(singleIFrame)
                {
                    //alert(singleIFrame);
                    //singleIFrame.reload();
                }
            }
        }
    }
}

//function changes the body background from blue to gray and makes the center and footer visible.
function RenderPage()
{
	var bodyCanvas = document.getElementById("bodyCanvas");
	var footerCanvas = document.getElementById("footerCanvas");
	
	document.body.style.backgroundColor = '#EFF1F2';
	if(bodyCanvas )
		bodyCanvas.style.visibility = 'visible';
	if(footerCanvas)
		footerCanvas.style.visibility = 'visible';
}//RenderPage

//function calls any functions to be run after the page is loaded.
function SetupPage()
{
	AddDisclaimerToAllAnchorsInCurrentPage();
	HideImagePlaceHoldersWithNoImage();
	RenderPage();
	RefreshIFramesInIE();
}//setupPage