// JavaScript Document

var defaultTextSize = 11;	//INITIAL TEXT SIZE IN PX, SHOULD BE THE SAME AS SITE CSS FILE
var minTextSize = 11;		//MINIMUM FONT SIZE IN PX
var maxTextSize = 18;		//MAXIMUM FONT SIZE IN PX


function fontSizeInit(QStextSize)
	{
	rewritelinks(QStextSize);
	writesize(QStextSize);
	}
	
function rewritelinks(textsize)
	{
		for (var i = 0; i < document.links.length; i++)
		{
			document.links[i].href = processURL(document.links[i].href, textsize)
		}
	}

function writesize (textsize)
	{
		if(textsize != "" && textsize >= minTextSize && textsize <= maxTextSize )
		{
			//IF TEXT SIZE IS SPECIFIED THEN WRITE IT
			document.write('<style> p { font-size: ' + textsize + 'px; } </style>' );
		}
	}

function increaseFontSize(textsize)
	{
		var currurl = window.location.href;
		if (textsize == "")
		{
			textsize = "11"
		}
		if(textsize != "" && textsize < maxTextSize )
		{
			var newtextsize = parseFloat(textsize) + 1;
			newtextsize = newtextsize + "";
			newurl = processURL(currurl, newtextsize);
			window.location.href = newurl;	
		}
	}

function decreaseFontSize(textsize)
	{
		var currurl = window.location.href;
		if (textsize == "")
		{
			textsize = "11"
		}
		if(textsize != "" && textsize > minTextSize )
		{
			var newtextsize = parseFloat(textsize) - 1;
			newtextsize = newtextsize + "";
			newurl = processURL(currurl, newtextsize);
			window.location.href = newurl;	
		}
	}

function resetFontSize()
	{
		var currurl = window.location.href;
		var newtextsize = 11;
		newtextsize = newtextsize + "";
		newurl = processURL(currurl, newtextsize);
		window.location.href = newurl;	
	}

function processURL (theurl, textsize)
	{
		querystringresult = theurl.indexOf('?');
		querystringtextsizeresult = theurl.indexOf('textsize=');
		jsresult = theurl.indexOf('javascript');
		anchorresult = theurl.indexOf('#');
		if(textsize == "" )
			{
				// DO NOTHING IF TEXTSIZE IS NOT TO BE CHANGED
			}	
		else if (jsresult != -1 )
			{
				// DO NOTHING IF JAVASCRIPT LINK
			}
		else if (querystringtextsizeresult != -1 )
			{
				// DO NOTHING IF TEXTSIZE IS ALREADY SPECIFIED OR THERE IS A HASH ONLY LINK
				regexp = /\btextsize=[0-9]+\b/ ;
				theurl = theurl.replace( regexp , "textsize=" + textsize);
			}
		else if (anchorresult != -1 && querystringresult != -1 )
			{
				// ADD QUERY BEFORE ANCHOR IF ANCHOR EXISTS, QUERYSTRING ALREADY EXISTS
				var url_array=theurl.split("#");
				theurl = url_array[0] + "&textsize=" + textsize + "#" + url_array[1];
			}	
		else if (anchorresult != -1  )
			{
				// ADD QUERY BEFORE ANCHOR IF ANCHOR EXISTS, QUERYSTRING DOES NOT YET EXIST
				var url_array=theurl.split("#");
				theurl = url_array[0] + "?textsize=" + textsize + "#" + url_array[1];
			}	
		else if (querystringresult != -1 )
			{
				// ADD TO QUERYSTRING IF QUERYSTRING EXISTS
				theurl = theurl + "&textsize=" + textsize ;
			}
		else
			{
				// CREATE QUERYSTRING IF QUERYSTRING DOES NOT YET EXIST
				theurl = theurl + "?textsize=" + textsize ;
			}
		
		return theurl
}