// *** VARIABLES *********************************************************/
window.name='QTACMain';

// hostname variables
var textOnlyHostname = replaceSubString(location.hostname, 'www', 'text');

// compliance test variables
var width;
var height;
var bestfit_width = 1010;
var bestfit_height = 680;
// set up width and height for new window
if ((screen.availWidth < bestfit_width) || (screen.availHeight < bestfit_height))
{
	width = screen.availWidth - 5;
	height = screen.availHeight - 60;
}
else
{
	width = bestfit_width;
	height = bestfit_height;
}
var compURL = 'https://' + window.location.hostname + '/oaApps/jsp/compliance.jsp';
var compFeatures = 'width=' + width + ',height=' + height + ',resizable=0,scrollbars=1,status=1,top=0,left=0';



// cookie variables
var cookieExpiryTime = 1;  // days til cookies expire

// queryString variables
QueryString.keys = new Array();
QueryString.values = new Array();



// global mouse position variables
var yMousePos = 100;	// this is updated each time the mouse is moved over
			// a top level left_menu item. (see onload function)

var cWin = null;

function showForm(page) {
	window.open(page, '_blank', 'menubar=1,scrollbars=1,status=1,resizable=0,width=1000,height=670');
}

function openWindow(page) {
	window.open(page, '_blank');
}

// scholarship application
function launchScholarship() {
	window.open('https://' + window.location.hostname + '/scholarship-frontend/scholarship.html', '_blank',
			'menubar=0,scrollbars=0,status=1,resizable=0,width=800,height=560');
}

// *** LOAD COMPLIANCE TEST **********************************************/
function loadTest(service)
{
	cWin = window.open(compURL + '?service=' + service + '&server=' + window.location.hostname,'login', compFeatures);
	setTimeout('isWindowOpen()', 700);
}

function isWindowOpen()
{
	if ((cWin == null) || (typeof(cWin) == 'undefined'))
	{
		alert('Your computer has a pop-up blocker which is preventing you from using QTAC\'s Online Services.\n\n' +
				'Click OK to read instructions on changing your pop-up blocker ' +
				'settings so that QTAC\'s Online Services can be accessed.');
		window.open('/FAQs/FAQ_Web_Enquiries_Answers.htm#Popup', '_top');
	}
}


// *** TOOLS *************************************************************/

// function getObject(objectId)
// this function will return an object (if it exists) for any browser that uses
// document.getElementById or document.all.
// For other browsers, or if the object does not exist, it will return null.
function getObject(objectId)
{
	if (document.getElementById)	// ie 5+, netscape 6+ (and compatible)
	{
		return document.getElementById(objectId);
	}
	else if (document.all)		// ie 4 (and compatible)
	{
		return document.all[objectId];
	}
	else				// everything else
	{
		return null;
	}
}

// replaceSubString(string, oldSubString, newSubString)
// this function replaces every instance of 'oldSubstring' with 'newSubString' in 
// the given string.
function replaceSubString(string, oldSubString, newSubString)
{
	var stringArray = string.split(oldSubString);
	var newString = stringArray[0];
	for (var i = 1; i < stringArray.length; i++)
	{
		newString = newString + newSubString + stringArray[i];
	}

	return newString;
}



// *** QUERY STRING ******************************************************/
// the following functions - QueryString(key) and QueryString_Parse() - were found on
// the web at http://www.experts-exchange.com/Manager/Web/Web_Languages/Q_20628128.html

function QueryString(key)
{
	var value = null;
	for (var i=0;i<QueryString.keys.length;i++)
	{
		if (QueryString.keys[i]==key)
		{
			value = QueryString.values[i];
			break;
		}
	}
	return value;
}


function QueryString_Parse()
{
	var query = window.location.search.substring(1);
	var pairs = query.split("&");
	
	for (var i=0;i<pairs.length;i++)
	{
		var pos = pairs[i].indexOf('=');
		if (pos >= 0)
		{
			var argname = pairs[i].substring(0,pos);
			var value = pairs[i].substring(pos+1);
			QueryString.keys[QueryString.keys.length] = argname;
			QueryString.values[QueryString.values.length] = value;		
		}
	}

}



// *** COOKIES ***********************************************************/

// function to set a cookie with the given name and value.
// if the value is blank and the cookie has not previously been set, do nothing.
// all cookies will expire at the end of the session.
function setCookie(name, value)
{
	if (value != '' && value != undefined)
	{
		// cookie name and value
		strCookie = name + '=' + escape(value);
		// cookie domain
		strCookie += ';path=/';  // the cookie is available to all pages under '/'

		// set cookie
		document.cookie = strCookie;
	}
	else if (getCookie(name) != '')
	{
		// a blank value is being assigned to a live cookie
		// it should be removed instead
		removeCookie(name);
	}
}


// this function removes a cookie by setting it's expiry date to now
function removeCookie(name)
{
	var expDate = new Date();
	document.cookie = name + '=' + ';expires=' + expDate.toGMTString() + ';path=/;';
}


// Get cookie routine by Shelley Powers (found on the web somewhere)
// finds the value for the given cookie name.
function getCookie(name)
{
	var search = name + '=';
	var returnvalue = '';

	if (document.cookie.length > 0)
	{
		offset = document.cookie.indexOf(search)

		if (offset != -1)  // cookie exists
		{ 
			offset += search.length
			// set index of beginning of value
			end = document.cookie.indexOf(";", offset);
			// set index of end of cookie value
			if (end == -1) end = document.cookie.length;
			returnvalue = unescape(document.cookie.substring(offset, end))
		}
	}
	return returnvalue;
}



