﻿var gbl_nCookieExpiry = 365;
var gbl_sPath = window.location.pathname;
var gbl_sPage = gbl_sPath.substring(gbl_sPath.lastIndexOf('/') + 1);
var gbl_nFontSizeVariation = 3;

// ** START Font Size Functions ** //

// This Function expects...
// sElementID: A string ID of the element requiring a font size change.
// nSizeChange: A numeric value of the font size increment. May be positive or negative.
function changeFontSize(sElementID, nSizeChange) {
	matchColumns(); //Clear column heights (pass nothing). They are reset at bottom of this function.
    var objElement;
    objElement = document.getElementById(sElementID);
    if (objElement) {
        //var sCookieName = gbl_sPage + '|' + objElement.id;
		var sCookieName = 'NewsmediaArticle';
        var sElementFS = objElement.style.fontSize;
        var sBodyFS = document.body.style.fontSize;
        var sNewFS;
        if (isNaN(parseFloat(sElementFS))) { //IF NotaNumber(onlythenumericchars(sElementFS))
            // Use document.body.fontSize as the initial value. If this is incorrect for the object having
            // it's fontsize changed here, then the object's default fontsize needs to be set with
            // javascript at the bottom of this master page.
            sNewFS = (parseFloat(sBodyFS) + nSizeChange) + sBodyFS.substring(parseFloat(sBodyFS).toString().length);
            objElement.style.fontSize = sNewFS; 
            //alert('Creating cookie: ' + sCookieName + ', ' + sNewFS);
            createCookie(sCookieName, parseFloat(sNewFS) + '|' + parseFloat(sBodyFS), gbl_nCookieExpiry);
        }
        else { 
            sNewFS = (parseFloat(sElementFS) + nSizeChange) + sElementFS.substring(parseFloat(sElementFS).toString().length);
            //Check in case the new fontsize deviates too far from the original size.
            var sCookieValue = readCookie(sCookieName);
            var bSizeOK = true;
            if (sCookieValue) {
                var arrCookieValue = sCookieValue.split("|");
                //alert(parseFloat(sNewFS));
                //alert('parseFloat(sNewFS): ' + parseFloat(sNewFS)); 
                //alert('parseInt(arrCookieValue[1]): ' + parseInt(arrCookieValue[1]));
                //alert('gbl_nFontSizeVariation: ' + gbl_nFontSizeVariation);
                if (parseFloat(sNewFS) >= parseInt(arrCookieValue[1])) {
                    //alert('here 1');
                    //alert('(parseFloat(sNewFS) - parseInt(arrCookieValue[1])): ' + (parseFloat(sNewFS) - parseInt(arrCookieValue[1])));
                    //alert('parseInt(gbl_nFontSizeVariation): ' + parseInt(gbl_nFontSizeVariation));
                    if ((parseFloat(sNewFS) - parseInt(arrCookieValue[1])) > parseInt(gbl_nFontSizeVariation)) {
                        bSizeOK = false;
                    }
                }
                else {
                    //alert('here 2');
                    //alert('(parseInt(arrCookieValue[1]) - parseFloat(sNewFS)): ' + (parseInt(arrCookieValue[1]) - parseFloat(sNewFS)));
                    //alert('parseInt(gbl_nFontSizeVariation): ' + parseInt(gbl_nFontSizeVariation));
                    if ((parseInt(arrCookieValue[1]) - parseFloat(sNewFS)) > parseInt(gbl_nFontSizeVariation)) {
                        bSizeOK = false;
                    }
                }
                //alert(bSizeOK);
                if (bSizeOK == true){
                    objElement.style.fontSize = sNewFS;
                    //alert('Creating cookie: ' + sCookieName + ', ' + sNewFS);
                    createCookie(sCookieName, parseFloat(sNewFS) + '|' + arrCookieValue[1], gbl_nCookieExpiry);
                }
            }
        }
    }
	//Set column heights (pass 'set'). They will take into account height of resized text.
	//This prevents upsized-text running over Sitemap footer in FF.
	matchColumns('set'); 
}

function createCookie(name,value,days) {
    if (days) {
	    var date = new Date();
	    date.setTime(date.getTime()+(days*24*60*60*1000));
	    var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
	    var c = ca[i];
	    while (c.charAt(0)==' ') c = c.substring(1,c.length);
	    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}


function readFontCookie(sElementID) {
    var objElement = document.getElementById(sElementID);
    if (objElement) {
        //var objElementParentID = objElement.parentNode.id;
        //if (objElementParentID) {
            //var sCookieName = gbl_sPage + '|' + objElementParentID + '|' + sElementID;

			//alert(gbl_sPage + '|' + sElementID);
            //var sCookieName = gbl_sPage + '|' + sElementID;
			var sCookieName = 'NewsmediaArticle';
            var sCookieValue = readCookie(sCookieName);
            if (sCookieValue) {
                //alert('sCookieValue: ' + sCookieValue);
                var arrCookieValue = sCookieValue.split("|");
                objElement.style.fontSize = arrCookieValue[0] + 'px';
            }
        //}
    }
}
// ** END Font Size Functions ** //


// This function is used to write out Quicktime embedded movie tags, as a way to get around Microsoft's "EOLAS" patch, in 
// which ActiveX objects like Quicktime or Flash are prevented from playing seamlessly by a prompt asking the user to "click to activate" them.
function InsertQuicktimeMovie(movie)
{
	if (document.all) {
	    //IE6 likes it this way. If done the other way (below) it says ActiveX is being used in a way it's not meant to be, and blocks it entirely
	    //citing a risk.	    
	    divObject = document.getElementById('divPlayer');	    
	    divObject.style.display = 'none';
	    document.write('<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" width="320" height="285" codebase="http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0" style="position:absolute; left:0px; top:0px;">\n');
	    document.write('<param name="src" value="' + movie + '"/>\n');
	    document.write('<param name="autoplay" value="true" />');
	    document.write('<param name="controller" value="true" />');
	    document.write('<embed src="' + movie + '" width="320" height="285" pluginspage=http://www.apple.com/quicktime/download/  autoplay="true" bgcolor="black" ></embed>');
	    document.write('</object>\n');
	}
	else {
	    //Mozilla likes it this way. If done the other way (above), the video never appears and it just keeps flicking
	    //back to the page button overlay (which should disappear).
	    divObject = document.getElementById('divPlayer');
	    divObject.innerHTML = '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" width="320" height="285" codebase="http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0" style="position:absolute; left:0px; top:0px;">';
	    divObject.innerHTML = divObject.innerHTML + '<param name="src" value="' + movie + '"/>';
	    divObject.innerHTML = divObject.innerHTML + '<param name="autoplay" value="true" />';
	    divObject.innerHTML = divObject.innerHTML + '<param name="controller" value="true" />';
	    divObject.innerHTML = divObject.innerHTML + '<embed src="' + movie + '" width="320" height="285" pluginspage=http://www.apple.com/quicktime/download/  autoplay="true" bgcolor="black" ></embed>';
	    divObject.innerHTML = divObject.innerHTML + '</object>';
	}	
}

// This function is used to write out embedded movie tags, as a way to get around Microsoft's "EOLAS" patch, in 
// which ActiveX objects like Quicktime or Flash are prevented from playing seamlessly by a prompt asking the user to "click to activate" them.
function InsertMediaPlayer(file, playerheight) {
    if (document.all) {
        //IE6 likes it this way. If done the other way (below) it says ActiveX is being used in a way it's not meant to be, and blocks it entirely
        //citing a risk.
        divObject = document.getElementById('divPlayer');
        divObject.style.display = 'none';
        document.write('<object type="video/x-ms-wmv" data="' + file + '" width="320" height="' + playerheight + '" id="MMPlayer_Player_object" style="position:absolute; left:0px; top:0px;">\n');
        document.write('<param name="src" value="' + file + '"/>\n');
        document.write('<param name="autostart" value="1" />');
        document.write('<param name="controller" value="true" />');
        document.write('<param name="qtsrcdontusebrowser" value="true" />');
        document.write('<param name="enablejavascript" value="true" />');
        document.write('</object>\n');
    }
    else {
        //Mozilla likes it this way. If done the other way (above), the video never appears and it just keeps flicking
        //back to the page button overlay (which should disappear).
        divObject = document.getElementById('divPlayer');
        divObject.innerHTML = '<object type="video/x-ms-wmv" data="' + file + '" width="320" height="' + playerheight + '" id="MMPlayer_Player_object" style="position:absolute; left:0px; top:0px;">';
        divObject.innerHTML = divObject.innerHTML + '<param name="src" value="' + file + '"/>';
        divObject.innerHTML = divObject.innerHTML + '<param name="autostart" value="1"/>';
        divObject.innerHTML = divObject.innerHTML + '<param name="controller" value="true"/>';
        divObject.innerHTML = divObject.innerHTML + '<param name="qtsrcdontusebrowser" value="true"/>';
        divObject.innerHTML = divObject.innerHTML + '<param name="enablejavascript" value="true"/>';
        divObject.innerHTML = divObject.innerHTML + '</object>';
    }	
}

// Used to open Multimedia Player popup window.
function openmultimediawindow(id, sHeight) {
	var LeftPos = (screen.width / 2) - 305;
	var TopPos = (screen.height / 2) - (sHeight / 2);
	var windowstring;
	windowstring = "void window.open('/multimediaplayer.aspx?id=" + id + "', '', 'status=0, toolbar=0, resizable=0, width=610,height=" + sHeight + ", scrollbars=1, left=" + LeftPos + ", top=" + TopPos + "');";
	eval(windowstring);
}

// Used to open Multimedia Player popup window.
function openslideshowwindow(id, sHeight) {
	var LeftPos = (screen.width / 2) - 425;
	var TopPos = (screen.height / 2) - 305;
	var windowstring2;
	windowstring2 = "void window.open('/slideshowplayer.aspx?id=" + id + "', '', 'status=0, toolbar=0, resizable=0, width=850,height=610, scrollbars=1, left=" + LeftPos + ", top=" + TopPos + "');";
	eval(windowstring2);
}

// This function is triggered by the 'onerror' property of the front cover image
// tag. When no cover image file is available, this function replaces the
// image with a blank white image of the same size.	
//
// ### Commented out 10/5/08 due to the fact that calling this was causing the drop down nav to stop
// working in IE6 (only). Instead, the onerror javascript is now handled within the image tag. ###
/*		
function validateimage(strDivName) {
	var MyDivObject;
	MyDivObject = document.getElementById(strDivName);
	if (MyDivObject) {
		MyDivObject.innerHTML = '';
		MyDivObject.innerHTML = "<img src='/images/white.gif' width='160' height='230' style='border:1px solid #999999;'/>";
	}
}
*/

//Handy script for trimming leading and trailing spaces from a string, in js.
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}



//START AC Nielsen PDF Tracking
function _nnClick(_nnClickDest)
{
	_nnCD = _nnClickDest;
	_nnECD = escape(_nnClickDest);
	_nnDelay = 500; /* click delay */
	var _nnRD=0;
	if (parseInt(navigator.appVersion)>=4)
	{
	_nnRD=(new Date()).getTime();
	}
	var _nnCI="ruralpressltd";
	var _nnCG="link-click";
	var _nnCC="au";
	var _nnLP=location.protocol.indexOf('https')>-1?'https:':'http:';
	var _nnRP=escape(window.location);
	var _nnND=_nnLP+'//secure-' + _nnCC + '.imrworldwide.com/';
	var _nnParams = "rnd=" + _nnRD + "&ci=" + _nnCI + "&cg=" + _nnCG;
	var _nnRef = "&rp=" + _nnRP;
	var _nnSI = "&si=" + _nnCI + "-ctgw-" + _nnECD;
	var _nnPixel = new Image(1,1);
	_nnPixel.src = _nnND + "cgi-bin/m?rnd=" + _nnParams + _nnSI + _nnRef;
	
	
	// Original version - opens PDF in the same window.
	setTimeout("window.location = _nnCD", _nnDelay);

	//Modified version - opens PDF in new window.
	//setTimeout("window.open('" + _nnCD + "', 'newwindow')", _nnDelay);
}
//END AC Nielsen PDF Tracking


function addBookmark(title,url) {
	if( document.all ) { //IE
		window.external.AddFavorite( url, title);
	} 
	else 
	{ 
		//Everybody else gets this. Even Firefox cannot add a bookmark properly, because once you add
		//it, it only opens in the 300px wide "sidebar" of the browser. Which is madness.
		sKeyName = 'CTRL';
		if (is_mac){
			sKeyName = 'the Apple key';
		}
		alert('In order to bookmark this page you need to do so manually through your browser. Try holding down ' + sKeyName + ' and pressing D.');				
	}
}

/*
Shows popup.
Parameters...
objThis: The object which, if hovered over, calls the popup to appear.
sPopupMenuId: The ID of the popup menu. Probably located at bottom of the master page, outside all the other DIVs.
nHeightOffset (OPTIONAL): The amount by which to offset the TOP position of the popup, when it appears. If left
blank, the function will use the height of objThis.
*/
function ShowHoverPop(objThis, sPopupMenuId, nHeightOffset){
	var objPopupMenu = document.getElementById(sPopupMenuId);
	var nXY;
	var nTop;
	var nLeft;
	if (objPopupMenu) {
		nXY = findPos(objThis);
		nLeft = nXY[0];
		nTop = nXY[1];
		if (nHeightOffset > 0){
			nTop = nTop + nHeightOffset;
		}
		else {
			nTop = nTop + objThis.offsetHeight;
		}
		objPopupMenu.style.left = nLeft + 'px';
		objPopupMenu.style.top = nTop + 'px';
		objPopupMenu.style.visibility = 'visible';
		objPopupMenu.style.display = 'block';
		//Note: display = 'block' required to show it, because gbl_sharesavesend_pop has display:none in css.
		//This is because it was causing space to appear below template in IE, unless it was
		//completely hidden initially in the DOM.
		
	}
}

/*Hides popup.*/
function HideHoverPop(sPopupMenuId){
	var objPopupMenu = document.getElementById(sPopupMenuId);
	if (objPopupMenu) {
	    objPopupMenu.style.visibility = 'hidden';
	    //Note: display = 'none' is NOT required here, as for some reason
	    //it makes the popup disappear too quickly in IE.
	}
}

var mytimeout; //Needs to be accessible globally.
//Delayed hiding of popup.
//Separated into 2 functions, because of the fiddliness of setTimeout syntax. Easier to call a function from it.
function HideHoverPopDelay(sPopupMenuId){
	mytimeout = setTimeout("HideHoverPopDelay2('" + sPopupMenuId + "')",500);//Half second delay, before hiding object.
}
function HideHoverPopDelay2(sPopupMenuId){
	var objPopupMenu = document.getElementById(sPopupMenuId);
	if (objPopupMenu) {
	    objPopupMenu.style.visibility = 'hidden';
	    //Note: display = 'none' is NOT required here, as for some reason
	    //it makes the popup disappear too quickly in IE.
	}	
}

/* Gets X,Y position of an HTML element */
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}

function populateIframe() {
	//Cycle through all iframes?
	if (document.all) //Z-Index issues appear to be restricted to IE. Firefox is fine.
	{
		// IE browsers.
		//var aSelectCollection = document.all.tags("select");
		var aiFrameCollection = document.all.tags("iframe");
	}
	else
	{
		//Mozilla browsers.
		var aiFrameCollection = document.getElementsByTagName("iframe");
	}
	//Find all iframe tags on the page.
	if (aiFrameCollection!=null) {
		for (i=0; i<aiFrameCollection.length; i++) 
		{
			/*alert(aiFrameCollection[i].id);*/
			/*if (aSelectCollection[i].id) {
				if (document.getElementById(aSelectCollection[i].id)) {
					MySelect = document.getElementById(aSelectCollection[i].id);
					MySelect.style.visibility = status;
				}
			}*/
		}
	}

/*
	var ifrm = document.getElementById('ffxadiframe6');
	ifrm = (ifrm.contentWindow) ? ifrm.contentWindow : (ifrm.contentDocument.document) ? ifrm.contentDocument.document : ifrm.contentDocument;
	ifrm.document.open();
	ifrm.document.write('Hello World!');
	ifrm.document.close();
*/
}





function resizeMediaIframe(objiFrame){	
    matchColumns();
    try{	
        frame = objiFrame;		
        innerDoc = (frame.contentDocument) ? frame.contentDocument : frame.contentWindow.document;			
	    if (innerDoc.body.scrollHeight == 0){
		    frame.style.display='none';
	    }
	    else {
            myheight = innerDoc.body.scrollHeight + 10;	
		    mywidth = innerDoc.body.scrollWidth + 20;
		    //alert('myheight: ' + myheight + ', mywidth: ' + mywidth);
		    //alert('innerDoc.body.scrollHeight: ' + innerDoc.body.scrollHeight);
            frame.style.height = myheight + 'px';
		    frame.style.width = mywidth + 'px';
	    }
    }
    catch(err){			
        window.status = err.message;
    }
    matchColumns('set');
}


//Shows/Hides the groupings of links on links.aspx, that are found within the div called "LinkSection"
//When nothing is passed, show all link groups.
function ShowLinks(sLinkSectionID) {    
    var objLinkSection = document.getElementById("LinkSection");
    if (objLinkSection) {
        var divTags = objLinkSection.getElementsByTagName("div");
        if (sLinkSectionID != '') {
            for (var i = 0; i < divTags.length; i++) {
                if (divTags[i].id != '') {
                    if (divTags[i].id == sLinkSectionID) {
                        //alert('hello');
                        divTags[i].style.display = 'block';
                    }
                    else {
                        divTags[i].style.display = 'none';
                    }
                }
            }
        }
        else {
            for (var i = 0; i < divTags.length; i++) {
                if (divTags[i].id != '') {                    
                    divTags[i].style.display = 'block';
                }
            }        
        }
    }
}


function textCounter(objTextBox, sDisplayDiv, maxlimit) {
    
    var objDisplayDiv = document.getElementById(sDisplayDiv);
    if (objDisplayDiv) {
               
        if (objTextBox.value.length > maxlimit) {
            objTextBox.value = objTextBox.value.substring(0, maxlimit);
        }
        else {
            //alert('test');
            //alert(maxlimit - objTextBox.value.length);
            //alert(objDisplayDiv.innerHTML);
            objDisplayDiv.innerHTML = (maxlimit - objTextBox.value.length) + ' characters remaining';
        }
    }
}