var sess_timer
var url = "ajax_login.php?";

function timeoutshow() {
	remove_errorbox();
	show_errorbox('<table style="width: 100%;"><tr><td><img src="images/exclam.gif"></td><td style="color: #FFF;"><h2 style="color: white;">Warning: Your login will expire in 5 minutes</h2><p>This is because you have been viewing/using this page for an extended period of time.</p><p>If you wish to remain on this page, and stay logged in, please click "Extend Login".</p></td></tr><tr><td colspan="2" style="text-align: right"><input type="button" class="redbtn" value="Extend Login" style="width: 120px;" onclick="extend_login_send();"><input type="button" class="redbtn" value="Dismiss Warning" style="margin-left: 10px; width: 120px;" onclick="remove_errorbox()"></td></tr></table>');
	sess_timer=setTimeout('expiredshow()', 300000);
}

function expiredshow(mode) {
	remove_errorbox();

	string='<table style="width: 100%;"><tr><td><img src="images/exclam.gif"></td><td style="color: #FFF;"><h2 style="color: white;">Warning: Your login has expired</h2><p>This is because you have been viewing/using this page for an extended period of time.</p><p>You may renew your login using the form below if you wish to continue, for example, with an application or job listing.</p></td></tr><tr><td colspan="2" style="text-align: left;">';

	switch (mode) {

		case 'wrongpw':
		string=string+'<div style="text-align: center; font-weight: bold; color: white; background: #C00; padding: 15px; border: #000 1px solid; margin: 5px 0;">The password you entered was invalid - please try again</div>';
		break;

		case 'extendfail':
		string=string+'<div style="text-align: center; font-weight: bold; color: white; background: #C00; padding: 15px; border: #000 1px solid; margin: 5px 0;">Login could not be extended as it has expired. Please enter your password below to renew.</div>';
		break;

		case 'nopw':
		string=string+'<div style="text-align: center; font-weight: bold; color: white; background: #C00; padding: 15px; border: #000 1px solid; margin: 5px 0;">Please enter a password.</div>';
		break;

	}

	string=string+'<table style="margin: 0 auto;"><tr><td style="color: #FFF; font-weight: bold; width: 250px; text-align: right;">User Name:</td><td style="width: 100px;"><input type="text" id="AJAX_USERNAME" value="'+ username +'" disabled></td></tr><tr><td style="color: #FFF; text-align: right; font-weight: bold;">Password:</td><td><input type="password" id="AJAX_PASSWORD"></td><td style="width: 250px;">&nbsp;</td><tr><td></td><td><input type="button" class="redbtn" value="Renew Login" style="width: 100%;" onclick="relogin_send()"></td></tr></table></td></tr><tr><td colspan="2" style="text-align: right"><input type="button" class="redbtn" value="Dismiss" style="margin-left: 10px; width: 120px;" onclick="remove_errorbox()"></td></tr></table>';
	show_errorbox(string);

}

function extendedshow(mode) {
	remove_errorbox();
	string='<table style="width: 100%;"><tr><td><img src="images/exclam.gif"></td><td style="color: #FFF;"><h2 style="color: white;">Login extended</h2>';

	switch (mode) {

		case 'renewtried':
		string=string+'<p>You tried to renew your login, but your login had not expired.</p>';
		break;

	}

	string=string+'<p>Your login was successfully extended.</p></td></tr><tr><td colspan="2" style="text-align: right"><input type="button" class="redbtn" value="Dismiss" style="margin-left: 10px; width: 120px;" onclick="remove_errorbox()"></td></tr></table>';
	show_errorbox(string);
}

function loggedinshow() {
	remove_errorbox();
	show_errorbox('<table style="width: 100%;"><tr><td><img src="images/exclam.gif"></td><td style="color: #FFF;"><h2 style="color: white;">Login renewed.</h2><p>Your login was successfully renewed.</p></td></tr><tr><td colspan="2" style="text-align: right"><input type="button" class="redbtn" value="Dismiss" style="margin-left: 10px; width: 120px;" onclick="remove_errorbox()"></td></tr></table>');
}

function processingboxshow(msg) {
	remove_errorbox();
	show_errorbox('<h2 style="color: white; text-align: center;">'+msg+'</h2><div style="width: 100%; text-align: center;"><img src="images/chasingwhite.gif"></div>');
}

function problemboxshow(mode) {
	string='<table style="width: 100%;"><tr><td><img src="images/exclam.gif"></td><td style="color: #FFF;"><h2 style="color: white;">An error has occurred.</h2><p>We were not able to ' + mode + ' your login.</p>';
	if (mode=='renew') {
		string=string+'<p>You may log in again using the form at the top right of the page.</p><p>As your login has expired, you will lose any unsaved form data when you move away from this page.</p>';
	} else if (mode=='extend') {
		string=string+'<p>If you are working on a form, and do not complete it before the page expires, you will lose any changes when you move away from this page.</p>';
	}
	string=string+'</td></tr><tr><td colspan="2" style="text-align: right"><input type="button" class="redbtn" value="Dismiss" style="margin-left: 10px; width: 120px;" onclick="remove_errorbox()"></td></tr></table>';
	show_errorbox(string);

}

function extend_login_send() {
	clearTimeout(sess_timer);
	processingboxshow('Please Wait - Extending Login');
	ajax_isworking=true;
	http.open("GET", url + 'action=extend', true);
	http.onreadystatechange = extend_login_do;
	http.send(null);
}

function extend_login_do() {
	if (http.readyState == 4) {
		xmlreturn = http.responseXML;
		var status = xmlreturn.getElementsByTagName('status').item(0).firstChild.data;
		if (status == 'success') {
			sess_timer=setTimeout('timeoutshow()', 1440000);
			extendedshow();
		} else if (status == 'expired') {
			expiredshow('extendfail');
		} else {
			problemboxshow('extend');
		}
		ajax_isworking=false;
	}
}

function relogin_send() {
	if (!document.getElementById('AJAX_PASSWORD').value) {
		expiredshow('nopw');
		return false;
	}
	ajax_isworking=true;
	http.open("GET", url + 'action=login&username='+ username +'&password='+ document.getElementById('AJAX_PASSWORD').value, true);
	processingboxshow('Please Wait - Logging In');
	clearTimeout(sess_timer);
	http.onreadystatechange = relogin_do;
	http.send(null);
}

function relogin_do() {
	if (http.readyState == 4) {
		xmlreturn = http.responseXML;
		var status = xmlreturn.getElementsByTagName('status').item(0).firstChild.data;
		switch (status) {

			case 'success':
			sess_timer=setTimeout('timeoutshow()', 1440000);
			loggedinshow();
			break;

			case 'wrongpw':
			expiredshow('wrongpw');
			break;

			case 'extended':
			sess_timer=setTimeout('timeoutshow()', 1440000);
			extendedshow('renewtried');
			break;

			default:
			alert(status);
			problemboxshow('renew');
		}
		ajax_isworking=false;
	}
}

function show_errorbox(html) {
	if (self.innerHeight)
	// all except Explorer
	{
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	// Explorer 6 Strict Mode
	{
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}   else if (document.body)
	// other Explorers
	{
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}

	x=x+windowState.getScrollX();
	y=y+windowState.getScrollY();

	var errorbox=document.createElement('div');
	var theBody = document.getElementsByTagName('body')[0];
	errorbox.setAttribute('id','errorbox');
	errorbox.style.position='absolute';
	errorbox.style.background='#036';
	errorbox.style.border='#000 2px solid';
	errorbox.style.width='600px';
	errorbox.style.visibility='hidden';
	errorbox.style.zIndex=101;

	errorbox.style.padding='5px';
	errorbox.style.color='#FFF';

	errorbox.innerHTML = html;
	theBody.appendChild(errorbox);
	height=getRenderedHeight(errorbox);
	xoff=(x-600)/2;
	yoff=(y-height)/2;

	var specialiframe=document.createElement('iframe');
	specialiframe.setAttribute('id','specialiframe');
	specialiframe.setAttribute('src', '');
	specialiframe.style.position='absolute';
	specialiframe.style.width='600px';
	specialiframe.style.height=height+'px';
	specialiframe.style.zIndex=100;
	specialiframe.style.top=yoff+'px';
	specialiframe.style.left=xoff+'px';
	specialiframe.style.border='none';

	
	theBody.appendChild(specialiframe);
		
	errorbox.style.top=yoff+'px';
	errorbox.style.left=xoff + 'px';
	errorbox.style.visibility='visible';

}

function remove_errorbox() {
	if (document.getElementById('errorbox')) {
		var theBody = document.getElementsByTagName('body')[0];
		var errorbox = document.getElementById('errorbox');
		var specialiframe = document.getElementById('specialiframe');
		theBody.removeChild(errorbox);
		theBody.removeChild(specialiframe);
	}
}

function getRenderedHeight(el) {
	//CS = document.defaultView.getComputedStyle(el,'');
	//height=CS.getPropertyValue('height');
	//return height.substr(0,height.length-2);
	return el.offsetHeight;
}


// function to get various page attributes - used here for gettting the scroll offsets to position div verically central in the viewport.
var windowState = (function(){
var readScroll = {scrollLeft:0,scrollTop:0};
var readSize = {clientWidth:0,clientHeight:0};
var readScrollX = 'scrollLeft';
var readScrollY = 'scrollTop';
var readWidth = 'clientWidth';
var readHeight = 'clientHeight';
function otherWindowTest(obj){
if((document.compatMode)&&
(document.compatMode == 'CSS1Compat')&&
(document.documentElement)){
return document.documentElement;
}else if(document.body){
return document.body;
}else{
return obj;
}
};
if((typeof this.innerHeight == 'number')&&
(typeof this.innerWidth == 'number')){
readSize = this;
readWidth = 'innerWidth';
readHeight = 'innerHeight';
}else{
readSize = otherWindowTest(readSize);
}
if((typeof this.pageYOffset == 'number')&&
(typeof this.pageXOffset == 'number')){
readScroll = this;
readScrollY = 'pageYOffset';
readScrollX = 'pageXOffset';
}else{
readScroll = otherWindowTest(readScroll);
}
return {
getScrollX:function(){
return (readScroll[readScrollX]||0);
},
getScrollY:function(){
return (readScroll[readScrollY]||0);
},
getWidth:function(){
return (readSize[readWidth]||0);
},
getHeight:function(){
return (readSize[readHeight]||0);
}
};
})();