/**************************************************************************
** $Id: global.js,v 1.9 2007/05/10 00:55:04 helenf Exp $
**
** Copyright (c) 2000-2007 MassMedia Studios Pty Ltd.
** 68-72 Wentworth Ave, Surry Hills, NSW 2010, Australia.
** All rights reserved.
**
** This software is the confidential and proprietary information of 
** MassMedia Studios Pty Ltd. ("Confidential Information").  You shall not
** disclose such Confidential Information and shall use it only in
** accordance with the terms of the license agreement you entered into
** with MassMedia Studios Pty Ltd.
** ------------------------------------------------------------------------
** 
** Author: Helen Fu
** Release notes:
**
**************************************************************************/

/*-------------------------- BROWSER DETECTION --------------------------*/

function dom_browser() {
	// browser identification based on dom capabilities
	this.myNav = this.navigator;
	this.version = this.navigator.appVersion;
	this.name = this.navigator.appName;
	this.userAgt = this.navigator.userAgent;
	this.ns4 = (document.layers) ? true : false;
	this.ns6 = (this.navigator.userAgent.indexOf("Netscape6") != -1) ? true : false;
	this.dom = (document.getElementById) ? true : false;
	this.ie4 = (document.all) ? true : false;
	this.mac = (this.version.indexOf("Mac") != -1) ? true : false;
	this.ie = (this.version.indexOf("MSIE") != -1) ? true : false;
	this.windows = (this.version.indexOf("Windows") != -1) ? true : false;
	this.hasPlugins = (this.navigator.plugins) ? true : false;
	this.ie6 = (this.version.indexOf("MSIE") != -1 && this.version.indexOf("6") != -1) ? true : false; 
	
	this.ie55 = (this.version.indexOf("MSIE 5.5") != -1) ? true : false;
	this.ie5 = (this.version.indexOf("MSIE 5.01") != -1) ? true : false;
	this.ns = (this.userAgt.indexOf("Netscape") != -1) ? true : false;
	this.ff = (this.userAgt.indexOf("Firefox") != -1) ? true : false;
	this.safari = (this.userAgt.indexOf("Safari") != -1) ? true : false;
}

dom_browser();

/*-------------------------- TABULAR MODULE --------------------------*/

function changeTab(myLinkObj) {
	myDL = myLinkObj.parentNode.parentNode;
	// reset all tabs..
	for (var i=0; i < myDL.childNodes.length; i++) {
		if (myDL.childNodes[i].nodeType == 1) {
			myDL.childNodes[i].className = "";
		}
	}
	// set selected tab..
	myLinkObj.parentNode.className = "selected";
	if (this.ie && this.windows) {
		myLinkObj.parentNode.nextSibling.className = "selected";
	} else {
		myLinkObj.parentNode.nextSibling.nextSibling.className = "selected";
		//myLinkObj.parentNode.nextSibling.nextSibling.blur(true); // CRASHES ON MAC BROWSERS!!!!
	}
	if(myDL.className == "tabular") { // only fix height for tabs DL..
		fixHeight(myLinkObj);
	}
}

// to fix the height of the tabular divs..
function fixHeight(myLinkObj) {
	myWrapper = myLinkObj.parentNode.parentNode.parentNode;
	if (this.ie && this.windows) {
		myHeight = myLinkObj.parentNode.nextSibling.clientHeight + 28;
	} else if(mac) {
		
		myWrapper.style.height = "0px";
		myHeight = myLinkObj.parentNode.nextSibling.nextSibling.clientHeight + 28;
	} else {
		myHeight = myLinkObj.parentNode.nextSibling.nextSibling.clientHeight + 28;
	}
	myWrapper.style.height = myHeight + "px";
}

// to run the fixHeight function onload and determine the tab that is selected..
function fixTabHeight() {
	if(document.links) {
		for(var i=0; i < document.links.length; i++) {
			currentLink = document.links[i];
			parentDT = currentLink.parentNode;
			parentDL = currentLink.parentNode.parentNode;
			if(parentDT.className == "selected" && parentDL.className == "tabular") {
				fixHeight(currentLink);
			}
		}
	}
}

/*-------------------------- ALL POPUPS - open/close functionality --------------------------*/

var loginPopups = Array("Navigation_login","Navigation_forgotpwd"); // if login and forgotten password popups..
var formPopups = Array("fSendFriend","fYourSay"); // if send to friend and your say forms..

// open/close popup..
function openPopup(myObjId,myStatus) {
	myObj = document.getElementById(myObjId); // current popup object..
	if(myObj != null && typeof(myObj) != "undefined") { // if object exists..
		var myTempArray = null;
		var myClass = null;
		var myLink = null;
		if(myObj.className.indexOf("s2fForm") != -1) { // if forms..
			myTempArray = formPopups;
			myClass = "s2fForm";
		} else if (myObj.className.indexOf("pollPopup") != -1) { // if polls..
			// do not set popup array...
			myClass = "pollPopup";
		} else if (myObj.className.indexOf("popup") != -1) { // if login popups..
			myTempArray = loginPopups;
			myClass = "popup";
			myLink = document.getElementById("lnklogin");
		} else {
			// do nothing..
		}
		
		if(myStatus == "show") { // if to show..
			// for login/forgotten password popups AND send to friend and your say forms..
			if(myTempArray != null && typeof(myTempArray) != "undefined" && myClass != null && typeof(myClass) != "undefined") {
				for(var i=0; i < myTempArray.length; i++) {
					myTempObj = document.getElementById(myTempArray[i]); // current popup..
					if(myTempObj != null && typeof(myTempObj) != "undefined") {
						if(myObjId == myTempArray[i]) { // if current popup is same as one we want to show..
							myTempObj.className = myClass + " block"; // set popup to show..
						} else {
							myTempObj.className = myClass + " hide"; // set popup to hide..
						}
					}
					if(myLink != null && typeof(myLink) != "undefined") {
						myLink.className = "loginSel"; // display selected login button state..
					}
				}
			} else if(myClass != null && typeof(myClass) != "undefined") { // for poll popups..
				myObj.className = myClass + " block"; // set popup to show..
			} else {
				// do nothing..
			}
		} else if(myStatus == "hide") { // if to hide..
			// for all popups..
			if(myClass != null && typeof(myClass) != "undefined") {
				myObj.className = myClass + " hide"; // set popup to hide..
				if(myLink != null && typeof(myLink) != "undefined") {
					myLink.className = "login"; // display selected login button state..
				}
			}
		} else {
			// do nothing..
		}
	}
}

function openPerfectpopup(oW,oTitle,oContent) { 
    var x = window.open('','windowName','width=500,height=400,resizable=1'); 
    if(!x ) { return true; } 
    x.document.open(); 
    x.document.write('<html><head><title>'+oTitle+'<\/title><\/head><body>'+ 
    (document.layers?('<layer left="0" top="0" width="'+oW+'" id="myID">') 
    :('<div style="position:absolute;left:0px;top:0px;display:table;width:'+oW+'px;" '+ 
    'id="myID">'))+ 
    oContent+(document.layers?'<\/layer>':'<\/div>')+'<\/body><\/html>'); 
    x.document.close(); 
    var oH = getRefToDivMod( 'myID', x.document ); if(!oH ) { return false; } 
    var oH = oH.clip? oH.clip.height : oH.offsetHeight; if(!oH ) { return false; } 
    x.resizeTo( oW + 200, oH + 200 ); 
    var myW = 0, myH = 0, d = x.document.documentElement, b = x.document.body; 
    if( x.innerWidth ) { myW = x.innerWidth; myH = x.innerHeight; } 
    else if( d && d.clientWidth ) { myW = d.clientWidth; myH = d.clientHeight; } 
    else if( b && b.clientWidth ) { myW = b.clientWidth; myH = b.clientHeight; } 
    if( window.opera &&!document.childNodes ) { myW += 16; } 
    x.resizeTo( oW + ( ( oW + 200 ) - myW ), oH + ( (oH + 200 ) - myH ) ); 
    if( x.focus ) { x.focus(); } 
    return false; 
} 

function getRefToDivMod( divID, oDoc ) { 
    if(!oDoc ) { oDoc = document; } 
    if( document.layers ) { 
    if( oDoc.layers[divID] ) { return oDoc.layers[divID]; } else { 
    for( var x = 0, y;!y && x < oDoc.layers.length; x++ ) { 
    y = getRefToDivNest(divID,oDoc.layers[x].document); } 
    return y; } } 
    if( document.getElementById ) { return oDoc.getElementById(divID); } 
    if( document.all ) { return oDoc.all[divID]; } 
    return document[divID]; 
} 

/*-------------------------- IMAGE CAROUSEL --------------------------*/

var currentPhoto;
currentPhoto = 0; // start at first photo..

// used to go to next/previous photo..
function changePhoto(myAction) {
	myObj = document.getElementById("mmPlaceholder"); // multimedia placeholder for feature article..
	myArrows = document.getElementById("imgSlide"); // previous/next arrows..
	myPhotoNo = document.getElementById("photoNo"); // current photo number..
	myTotalPhotos = document.getElementById("totalPhotos"); // total photos in list..
	if(myObj != null && typeof(myObj) != "undefined") {
		if(myAction == "next") { // to go to next photo..
			currentPhoto += 1;
			if(currentPhoto >= featureImageList.length) { // check if at the end..
				currentPhoto = 0;
			}
		} else if (myAction == "prev") { // to go to previous photo.. 
			currentPhoto -= 1;
			if(currentPhoto < 0) { // check if at the beginning..
				currentPhoto = (featureImageList.length - 1);
			}
		} else {
			// do nothing..
		}
		myObj.innerHTML = "<img src='" + featureImageList[currentPhoto] + "' alt='' title='' />"; // write image into placeholder..
	}
	if(myArrows != null && typeof(myArrows) != "undefined" && myPhotoNo != null && typeof(myPhotoNo) != "undefined" && myTotalPhotos != null && typeof(myTotalPhotos) != "undefined") {
		if(featureImageList.length > 1) { // if more than 1 image then show arrows..
			myArrows.style.display = "block";
		}
		myPhotoNo.innerHTML = currentPhoto + 1; // populate with current image number..
		myTotalPhotos.innerHTML = featureImageList.length; // populate with total amount of images..
	}
}

/*-------------------------- MAXLENGTH FOR TEXTAREAS --------------------------*/

function checkLength(e, myFieldObj, myLength) {
	// Acts as maxLength attribute. Used with an onkeypress event.
	// Written for textarea fields which do not cater for maxLength.
	var key;
	if (window.event || !e.which) {
		key = e.keyCode;
	} else if (e) {
		key = e.which;
	}
	if ((key >=35 && key <=40) || key == 8 || key == 9 || key == 46)  // allow functional keys eg. arrows, home, end, delete... etc.
		return true;
	if (myFieldObj.value.length >= myLength) {
		// if greater than maximum length, deny key to be written out.
		return false;
	} else {
		// allow key.
		return true;
	}
}

function taCount(myObj, myDiv, myLength) { 
	// Decrements the count value if the amount of characters have not reached max length yet
	// myObj is the form element object that characters are being type into
	// myDiv is the element that the count value is written into
	// myLength is the maximum amount of characters allowed to be entered into myObj
	if(myObj != null && typeof(myObj) != "undefined") {
		if (myObj.value.length > myLength) myObj.value = myObj.value.substring(0,myLength);
		myDivObj = document.getElementById(myDiv);
		if (myDivObj != null && typeof(myDivObj) != "undefined") {
			myDivObj.innerHTML = myLength - myObj.value.length;
		}
	}
}


/*-------------------------- INCREASE/DECREASE FONT SIZE --------------------------*/


function changeFontSize(myDir) {
	myStartFont = 1;
	myArea = document.getElementById("articleWrapper");
	if(myArea != null && typeof(myArea) != "undefined") {
		myTempFont = parseFloat(myArea.style.fontSize);
		if(myDir == "initialise") {
			myArea.style.fontSize = myStartFont + "em";
		} else if(myDir == "increase") {
			myArea.style.fontSize = (myTempFont + 0.1) + "em";
		} else if(myDir == "decrease" && myTempFont > myStartFont) {
			myArea.style.fontSize = (myTempFont - 0.1) + "em";
		} else {
			// do nothing..
		}
	}
}


/*-------------------------- ARTICLE PAGE: position RHS and quote module --------------------------*/

// used on article page to position the right hand side related links and left side quote module..
// parameters: insertRL = paragraph no. to insert RHS related links before..
// parameters: insertQut = paragraph no. to insert LHS quote module before..
function placeArticleElements(insertRL,insertQut) {

	var myWrapper = document.getElementById("articleWrapper2"); // elements wrapper around article..
	var myRightSec, myQuoteMod; // RHS related links module and LHS quote module
	var insertPos = insertRL; // position to insert related links...
	var insertQPos = insertQut; // position to insert quote module..

	var paraCount = 0; // count amount of paragraphs...

	if (myWrapper != null && typeof(myWrapper) != "undefined") { // if article exists..

		if (myWrapper.hasChildNodes()) { // if wrapper has child nodes..

			if(document.getElementById("RelatedLinks") != null && typeof(document.getElementById("RelatedLinks")) != "undefined"){ // check whether right section..
			    myRightSec = document.getElementById("RelatedLinks");
            }
			if(document.getElementById("Quote") != null && typeof(document.getElementById("Quote")) != "undefined"){
			    myQuoteMod = document.getElementById("Quote");
            } 

			for (var j = 0; j < myWrapper.childNodes.length; j++) { // for each child node..
				if (myWrapper.childNodes[j].tagName == "P") { // if its a paragraph..
					paraCount = paraCount + 1; // increment counter..
					if (paraCount == insertPos) { // if paragraph is same as related links insertation point..
						if (myWrapper.childNodes[j] != null && myRightSec != null) {
							myWrapper.insertBefore(myRightSec,myWrapper.childNodes[j]); // insert related links..
							j++; // need to increment so don't look at same element again..
						}
					}
					if (paraCount == insertQPos) { // if paragraph is same as quote insertation point..
						if (myWrapper.childNodes[j] != null && myQuoteMod != null) {
							myWrapper.insertBefore(myQuoteMod,myWrapper.childNodes[j]); // insert quote..
							j++; // need to increment so don't look at same element again..
						}
					}
				}
			}
		}
	}
}

/*-------------------------- UTILITY CONTROL: search text enter --------------------------*/

function check_submit(e,myObj) {
    var myBtn;
    var key;
    if (window.event || !e.which) {
        key = e.keyCode;
    } else if (e)  {
        key = e.which;
    }
    if (key == 13) { // if enter key is pressed..
        var myBtn = document.getElementById("Utility_lnkSearch");
        if (myBtn != null && typeof(myBtn) != "undefined") {
            var myHref = unescape(myBtn.href);
            eval(myHref.substr(11) + ";");
        }
    }
}

/*-------------------------- class name manipulation functionality --------------------------*/

function trim(str) {
// removes white spaces from beginning and end of given string, str.
	return str.replace(/^\s*|\s*$/g,"");	
}

function addClassName(myElement, myClass) {
    if (myElement != null && typeof(myElement.classname) != "undefined") {
        var currentClassName = trim(myElement.className);
    } else {
        var currentClassName = "";
    }
	var myReg = new RegExp("(^" + myClass + "$)|( " + myClass + "$)|(^" + myClass + " )|( " + myClass + " )", "g");
	if (!myReg.test(currentClassName)) {
		// not currently set on element, so add class..
		if (currentClassName != "") {
			myElement.className += " " + myClass;
		} else {
			myElement.className = myClass;
		}
	}
}
function removeClassName(myElement, myClass) {
	var currentClassName = trim(myElement.className);
	var myReg = new RegExp("(^" + myClass + "$)|( " + myClass + "$)|(^" + myClass + " )|( " + myClass + " )", "g");
	myElement.className = currentClassName.replace(myReg, "");
}


function showPassword(myElementId, myLinkId) {
	var myObj = document.getElementById(myElementId);
	var myLinkObj = document.getElementById(myLinkId);
	// hide link..
	addClassName(myLinkObj, "hide");
	// show password fields..
	if (myObj != null && typeof(myObj) != "undefined") {
		removeClassName(myObj, "hide");
	}
}