// QT Website JavaScript files
// version 2.0
// Last update: 22/12/2008

// Set global variables
var currSize, classes;
//Function to open a new window with the parameters for the new window passed in
function MM_openBrWindow(theURL,winName,features) 
{ //v2.0
	var width, height, settings, newWindow;

	width = screen.availWidth;
	height = screen.availHeight;
	settings= "left=0,top=0,width=" + width + ",height=" + height + "," + features;
	newWindow = window.open(theURL,winName,settings);
	newWindow.focus()
}
//Appends values to the query string for the forms
function appendQueryString(formName, fieldName, valMsg) 
{
	var curForm = window.document.forms[formName];
	var curField = curForm.elements[fieldName];
	var path =  "";

	if (curField.selectedIndex == 0)
	{
		alert (valMsg);
		return false;
	}
	else
	{
		return true;
		//    path = '/?' + fieldName + '=' + curField.options[curField.selectedIndex].value
	}
	//  window.location=path;
}
//Gets the listed variable from the url string and passes back its value
function getQueryString(variable) 
{
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) 
	{
		var pair = vars[i].split("=");
		if (pair[0] == variable) 
		{
			var qsVal = replaceSubString( pair[1], "+", " ");
			// alert( pair[1].replace( "+", " ") + " | " + qsVal );
			return qsVal;
		}
	} 
	return(0);
}
//Replaces all occurences of one string in another string
function replaceSubString(argvalue, x, y) 
{
	if ((x == y) || (parseInt(y.indexOf(x)) > -1)) 
	{
		errmessage = "replace function error: \n";
		errmessage += "Second argument and third argument could be the same ";
		errmessage += "or third argument contains second argument.\n";
		errmessage += "This will create an infinite loop as it's replaced globally.";
		alert(errmessage);
		return false;
	}    
	while (argvalue.indexOf(x) != -1) 
	{
		var leading = argvalue.substring(0, argvalue.indexOf(x));
		var trailing = argvalue.substring(argvalue.indexOf(x) + x.length, 	argvalue.length);
		argvalue = leading + y + trailing;
	}
	return argvalue;
}
//Changes the print style sheet from an alternate style sheet to a current style sheet
function setPrintStyleSheet(title) 
{
	var i, a, main;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) 
	{
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) 
		{
			a.disabled = true;
			if(a.getAttribute("title") == title) a.disabled = false;
		}
	}
}
//Opens a new printable page with the pritnable style active
function openPrintablePage()
{
	var newurl, newtitle, newfeatures;
	newurl = window.location + "?&printable=yes";
	newtitle = window.name;
	newfeatures = "toolbar, location, directories, status, menubar, scrollbars, resizable";
	MM_openBrWindow(newurl,newtitle,newfeatures)
  
}
//The onload function.  Loads the fontsize class definitions, also disables the printable stylesheet just in case.
//Will activate the printable style sheet if the url contains the value printable=yes
function init() 
{	
	var printpage, i, a;
	classes = ["small", "default", "large", "larger"];
	//check for font-size preference
	currSize = ReadCookie('fontsize');
	if (!currSize){
		currSize = 1;
	}
	else
	{
		document.body.className = classes[currSize];
	}
  
	printpage = getQueryString('printable');
	if (printpage == "yes")
	{
		setPrintStyleSheet('printableStyle');
	}else{
		for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
		{
			if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") == "printableStyle")
			{
				a.disabled = true;
			}
		}
	}
}
//Page options script
function writePageOptionsScript()
{
	var fup = "<li><a class='plus' href='javascript:fontUp();'>+</a> / <a class='minus' href='javascript:fontDown();'>-</a> Text size</li>";
	var rset = "<li><a href=\"javascript:delete_cookie('fontsize')\" class='pageOptionsLinks'>Reset text size</a></li>";
	var pfri = "<li><a href='javascript:openPrintablePage()' class='print' title='Print friendly'>Print friendly</a></li>";
	        
	//document.writeln(fup);
	//document.writeln(rset);
        document.writeln(pfri);
}
function fontUp()
{
	currSize = (currSize == 3) ? 3 : ++currSize;
	document.body.className = classes[currSize];
	setCookie(currSize);
}
function fontDown()
{
	currSize = (currSize == 0) ? 0 : --currSize;
	document.body.className = classes[currSize];
	setCookie(currSize);
}
// adds the class attribute to the body tag
function setSize()
{
	var theBody = document.body;
	var theClass;
	theClass = ReadCookie('fontsize');
	// this is for the difference between IE and other browsers
	if (theBody.hasAttribute)
	{
		theBody.setAttribute("class", theClass);
	}
	else 
	{
		theBody.setAttribute("className", theClass);
	}
}
// reads the cookie
function ReadCookie(cookieName) 
{
	var theCookie=""+document.cookie;
	var ind=theCookie.indexOf(cookieName);
	if (ind==-1 || cookieName=="") return ""; 
	var ind1=theCookie.indexOf(';',ind);
	if (ind1==-1) ind1=theCookie.length; 
	return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}
// sets the cookie
function setCookie(size)
{
	expireDate = new Date;
	var cookiename = "fontsize";
	var expires = expireDate.setTime(expireDate.getTime() + 24 * 60 * 60 * 1000);
	var path = "/";
	var domain = ".qld.gov.au";
	var secure;
	var theCookie = cookiename + "=" + escape(size) +
 ((expires) ? "; expires=" + expireDate.toGMTString() : "") +
 ((path) ? "; path=" + path : "") +
 ((domain) ? "; domain=" + domain : "") + 
 ((secure) ? "; secure" : "");
	document.cookie = theCookie;
}
// Deletes the cookie dropped onto the system by the font changer
function delete_cookie ( cookie_name )
{

	if (!ReadCookie("fontsize")) 
	{
		alert("The text size has not been changed.");
	}
	else
	{
		var cookie_date = new Date ( );  // current date & time
		cookie_date.setTime ( cookie_date.getTime() - 1 );
		//  document.cookie = cookie_name + "=null;" + "expires= " + cookie_date.toGMTString() + "; " + "path=/; ";
		document.cookie = cookie_name + "=null;" + "expires= " + cookie_date.toGMTString() + "; " + "path=/; " + "domain=.qld.gov.au;";
		alert("The text size has been reset to the page defaults.");
		document.location.reload();
	}
}

// Dynamic HTML functions

function addOnClick(addToEl, fToAdd)
{
	// Adds an onClick JavaScript event to a selected element
	
	// Parameters
	// addToEl - The element to add the function to
	// fToAdd - The name of the function to add
	
	var bEl = document.getElementById(addToEl);
	var functEvent = new Function (fToAdd);

	if (bEl)
	{
		bEl.onclick = functEvent;
	}
}

function hideElement(elToHide)
{
	// Hides an element either completely or visually
	
	// Parameters
	// elToHide - The id of the element to hide

	var dEl = document.getElementById(elToHide);
	
	if (dEl)
	{
		dEl.className = "hideVisually";	
	}
}

function unHideElement(elToChange, linkId, expImgPth, colImgPth)
{
	// Toggles visibility of the section specified
	
	var elChange = document.getElementById(elToChange);
	var linkEl = document.getElementById(linkId);
	var elImg = linkEl.getElementsByTagName('img');

	if (elChange.className == "hideVisually")
	{
		elChange.className = "";
		if (elImg)
		{
			elImg[0].setAttribute('src', colImgPth);
			elImg[0].setAttribute('alt', 'Collapse this section');
		}
	}else{
		elChange.className = "hideVisually";
		if (elImg)
		{
			elImg[0].setAttribute('src', expImgPth);
			elImg[0].setAttribute('alt', 'Expand this section');
		}
	}
}

function addExpImage(addToEl, imgPth)
{
	// Adds an expand image to the expanding fields
	
	var imgEl, txtEl, linktxt;
	// var imgPth = "/qt/CorpTem.nsf/files/Expand.gif/$file/Expand.gif";
	var toEl = document.getElementById(addToEl);
	linktxt = toEl.innerHTML;
	txtEl = document.createTextNode(linktxt);
	
	imgEl = document.createElement('img');
	imgEl.setAttribute('src', imgPth);
	imgEl.setAttribute('alt', 'Expand this section');
	
	// Put it all back together
	toEl.innerHTML = "";
	toEl.appendChild(imgEl);
	toEl.appendChild(txtEl);
}

function createCollapseSection(collId, sectId, collImageUrl, expImageUrl)
{
	// Creates a collapsable section on the webpage
	// Parameters
	// collId - The id of the link to collapse or expand the section
	// sectId - The id of the object to hide or unhide on clicking the link
	// collImageUrl - the url for the image to use for the collapse image
	// expImageUrl - the url for the image to use for the expand image
	
	// Firstly do compatability checks
	if (!document.getElementById && !document.createElement && !document.createTextNode)
	{
		return;
	}
	
	var temp;
	
	var colEl = document.getElementById(collId);
	
	temp = "unHideElement('" + sectId + "', '" + collId + "', '" + expImageUrl + "', '" + collImageUrl + "')";
	addOnClick(collId, temp);
	hideElement(sectId);
	addExpImage(collId, expImageUrl);
	
	colEl.setAttribute('href', 'javascript:void(0)');
}


// End of functions


