/*
Copyright © 2004-2005 Pro.Net Components, www.pronetcomponents.com, U.S.A. All rights reserved.
Patent Pending
*/
function __ProNetRPS(id)
{
this.id = id;
window[this.id] = this;
this.maxTime = 10000;
this.intervalTime = 500;
this.OnLoad = function(){};
this.OnFail = function(){};
this.Request = function(url)
{
	this.startDate = new Date();
	if(window.ActiveXObject)
	{
		this.interval = window.setInterval(this.id + '.CheckState()',this.intervalTime);
		this.xmlDoc = this.GetXmlActiveX();
		this.xmlDoc.load(url);
	}
	else if(document.implementation && document.implementation.createDocument)
	{
		this.xmlDoc = document.implementation.createDocument('','doc',null);
		var obj = this;
		this.xmlDoc.onload = function()
		{
			try
			{
				obj.result = obj.xmlDoc.getElementsByTagName('result')[0].childNodes[0].nodeValue;
				obj.xmlDoc = null;
				obj.LoadedEvent();
			}
			catch(e)
			{
				obj.xmlDoc = null;
				obj.FailedEvent();
			}
		}
		this.xmlDoc.load(url);
	}
}
this.CheckState = function()
{
	if(this.xmlDoc.readyState == 4)
	{ 
		window.clearInterval(this.interval);
		this.result = this.xmlDoc.getElementsByTagName('result')[0].childNodes[0].nodeValue;
		this.xmlDoc = null;
		this.OnLoad();
	}
	else
	{
		var now = new Date();
		if(now.getTime() - this.startDate.getTime() >= this.maxTime)
		{
			this.xmlDoc = null;
			window.clearInterval(this.interval);
			this.OnFail();
		}
	}
}
this.GetXmlActiveX = function()
{	
	var ieXml = ['MSXML2','Microsoft','MSXML','MSXML3'];
	for(var i = 0; i < ieXml.length; i++)
	{
		try
		{
			var xmlDomDoc = new ActiveXObject(ieXml[i] + '.DomDocument');
			return xmlDomDoc;
		}
		catch(e){}
	}
}
}
function __ProNetSessionRenew(inputId){
this.id = inputId;
this.resize = true;
this.styleSet = false;
this.sessionTimeout = 1;
this.timeLeft = 0;
this.startTime = new Date();
this.endTime = null;
this.warnTimeout = null;
this.updateWarnTimeout = null;
this.warnSeconds = 30;
this.numBars = 30;
this.hiddenElements = new Array();
this.showWarning = true;
this.btnStyle = new Object();
this.btnOverStyle = new Object();
this.loadCellStyle = new Object();
this.loadCellOnStyle = new Object();
this.statusStyle = new Object();
this.windowStyle = new Object();
this.msgStyle = new Object();
this.statusTimeout = null;
this.statusTime = 500;
this.getEl = function(id){return document.getElementById(id);}
this.action = 'renew';
this.successResponse = '1';
this.failureResponse = '0';
this.RPS = new __ProNetRPS();
if(window.attachEvent)
{
	this.SetEvent = function(el,theEvent,functionPtr){el.attachEvent('on'+theEvent,functionPtr);};
	this.UnsetEvent = function(el,theEvent,functionPtr){el.detachEvent('on'+theEvent,functionPtr);}; 
}
else if(window.addEventListener)
{
	this.SetEvent = function(el,theEvent,functionPtr){el.addEventListener(theEvent, functionPtr, false);};
	this.UnsetEvent = function(el,theEvent,functionPtr){el.removeEventListener(theEvent, functionPtr);};
}
this.window = this.getEl(this.id + 'window');
this.msg = this.getEl(this.id + 'msg');
this.btn = this.getEl(this.id + 'btn');
this.btnYes = this.getEl(this.id + 'btnYes');
this.btnCancel = this.getEl(this.id + 'btnCancel');
this.status = this.getEl(this.id + 'status');
this.bars = new Array();
for(var i = 0; i < this.numBars; i++) { this.bars[this.bars.length]=this.getEl(this.id+'bar'+i); }
this.SetStyle = function(el, style, cssClass)
{
	if(arguments.length == 3){if(cssClass){el.className = cssClass;}}
	for(var i in style){el.style[i] = style[i];}
}
this.SetStyle(this.status, this.statusStyle, this.statusCssClass);
this.BtnOver = function(el){this.SetStyle(el, this.btnOverStyle, this.btnOverCssClass);}
this.BtnOut = function(el){this.SetStyle(el, this.btnStyle, this.btnCssClass);}
this.HideElements = function(el){this.HideEl(el, 'select');this.HideEl(el, 'textarea');this.HideEl(el, 'embed');}
this.HideEl = function(thisEl, tag)
{	
	for(var i = 0; i < document.getElementsByTagName(tag).length; i++)
	{
		var el = document.getElementsByTagName(tag)[i];
		if(this.GetTop(el) > parseInt(thisEl.style.top) + thisEl.offsetHeight){}
		else if(this.GetLeft(el) > parseInt(thisEl.style.left)+thisEl.offsetWidth){}
		else if(this.GetRight(el) < parseInt(thisEl.style.left)){}
		else if(this.GetTop(el) + el.offsetHeight < parseInt(thisEl.style.top)){}
		else{el.style.visibility = 'hidden';this.hiddenElements[this.hiddenElements.length] = el;}
	}
}
this.ShowElements = function(tag)
{
	while(this.hiddenElements.length > 0)
	{
		this.hiddenElements[this.hiddenElements.length - 1].style.visibility = '';
		this.hiddenElements.length = this.hiddenElements.length - 1;
	}
}
this.GetLeft = function(el)
{
	var xPos = el.offsetLeft;
	var tempEl = el.offsetParent;
	while(tempEl != null){xPos += tempEl.offsetLeft;tempEl = tempEl.offsetParent;}
	return xPos;
}
this.GetRight = function(el){return this.GetLeft(el) + el.offsetWidth;}
this.GetBottom = function(el){return this.GetTop(el) + el.offsetHeight;}
this.GetTop = function(el)
{
	var yPos = el.offsetTop;
	var tempEl = el.offsetParent;
	while(tempEl != null){yPos += tempEl.offsetTop;tempEl = tempEl.offsetParent;}
	return yPos; 
}
this.Init = function()
{
	if(!this.styleSet)
	{
		for(var i = 0; i < this.bars.length; i++){this.SetStyle(this.bars[i], this.loadCellStyle, this.loadCellCssClass);}
		this.SetStyle(this.window, this.windowStyle, this.windowCssClass);
		this.SetStyle(this.msg, this.msgStyle, this.msgCssClass);
		this.SetStyle(this.btnYes, this.btnStyle, this.btnCssClass);
		this.SetStyle(this.btnCancel, this.btnStyle, this.btnCssClass);
		this.styleSet = true;
	}
	if(this.sessionTimeout > 0)
	{
		this.timeLeft = 60000 * this.sessionTimeout;
		this.endTime = new Date(this.startTime.getTime() + this.timeLeft);
		this.warnTimeout = window.setTimeout(this.id + '.Warn()', this.timeLeft - (this.warnSeconds * 1000));
	}
	else
	{
		if(this.showWarning)
		{
			this.SessionEndState();
			this.Center(this.window);
			this.FadeIn(this.window);
		}
	}
}
this.Warn = function()
{
	if(this.showWarning)
	{
		this.msg.innerHTML = this.msgWarn.replace('{0}',this.warnSeconds);
		this.btn.style.display = 'block';
		this.status.style.display = 'none';
		this.Center(this.window);
		this.FadeIn(this.window);
	}
	this.updateWarnTimeout = window.setTimeout(this.id + '.UpdateWarn()', 1000);
}
this.UpdateWarn = function()
{
	var now = new Date();
	if(this.endTime.getTime() <= now.getTime() + 1000)
	{
		this.SessionEndState();
		return;
	}
	if(this.showWarning)this.msg.innerHTML = this.msgWarn.replace('{0}',parseInt((this.endTime.getTime() - now.getTime()) / 1000));
	this.updateWarnTimeout = window.setTimeout(this.id + '.UpdateWarn()', 1000);
}
this.SessionEndState = function()
{
	if(this.showWarning){this.SetToLogin(this.msgSessionEnd);}
	this.Redirect();
}
this.SetToLogin = function(msg)
{
	this.msg.innerHTML = msg;
	this.status.style.display = 'none';
	this.action = 'login';
	this.btn.style.display = 'block';
	if(!this.loginUrl)
	{
		this.btnCancel.style.display = 'none';
		this.action = 'cancel';
	}
}
this.Redirect = function()
{
	if(this.redirectUrl)
	{
		if(this.showWarning)
		{
			this.msg.innerHTML = this.msgRedirect;
			this.btn.style.display = 'none';
			this.status.style.display = 'none';
		}
		document.location = this.redirectUrl;
	}
}
this.Center = function(el)
{
	if(!el.style.width || !el.style.height)
	{
		var elWidth = parseInt(el.offsetWidth);
		var elHeight = parseInt(el.offsetHeight);
	}
	else
	{
		var elWidth = parseInt(el.style.width);
		var elHeight = parseInt(el.style.height);
	}
	if(this.resize){this.resize=false;el.style.width=elWidth;el.style.height=elHeight;}
	var docWidth = (document.all) ? document.body.clientWidth : document.body.clientWidth;
	var docHeight = (document.all) ? document.body.clientHeight : document.body.clientHeight;
	var offsetWidth = (document.all) ? document.body.scrollLeft : window.pageXOffset;
	var offsetHeight = (document.all) ? document.body.scrollTop : window.pageYOffset;
	el.style.left = parseInt((docWidth / 2) - (elWidth / 2) + offsetWidth);
	el.style.top = parseInt((docHeight / 2) - (elHeight / 2) + offsetHeight);
}
this.ToggleFade = function(el, blnFadeIn)
{
	var style1=(blnFadeIn)?'hidden':'visible';
	var style2=(blnFadeIn)?'visible':'hidden';
	el.style.visibility = style1;
	if(blnFadeIn){this.HideElements(this.window);}else{this.ShowElements();}
	if(!document.all){el.style.visibility=style2;return;}
	try{el.filters[0].apply();el.style.visibility=style2;el.filters[0].play();}
	catch(e){el.style.visibility = style2;}
}
this.FadeOut = function(el){this.ToggleFade(el,false);}
this.FadeIn = function(el){this.ToggleFade(el,true);}
this.HandleActivity = function()
{
	if(this.window.style.visibility == 'visible')
	{
		this.ShowElements();this.HideElements(this.window);
		this.Center(this.window,false);
	}
}
this.Cancel = function(el)
{
	this.SetStyle(this.btnCancel, this.btnStyle, this.btnCssClass);
	if(this.updateWarnTimeout)window.clearInterval(this.updateWarnTimeout);
	this.FadeOut(this.window);
}
this.Ok = function(el)
{
	this.SetStyle(this.btnYes, this.btnStyle, this.btnCssClass);
	switch(this.action)
	{
		case 'renew' : this.Renew(el); break;
		case 'login' : this.Login(el); break;
		case 'cancel': this.Cancel(el);break;
	}	
}
this.Renew = function(el)
{	
	this.ResetBars();
	if(this.updateWarnTimeout){window.clearTimeout(this.updateWarnTimeout);}
	this.msg.innerHTML = this.msgRenewing;
	this.btn.style.display = 'none';
	this.status.style.display = 'block';
	window.setTimeout(this.id + '.Load()', 0);
}
this.Load = function()
{	
	var pattern = /(\([a-zA-Z0-9]+\))/;
	var loc = this.renewUrl.toString();
	if(pattern.test(document.location.href))
	{
		munge = document.location.href.match(pattern)[0];
		loc = this.root + munge + '/' + this.renewUrl.substring(this.root.length);
	}
	var preChar = '?';
	if(this.renewUrl.indexOf('?') > -1)
		preChar = '&';	
	this.RPS.Request(loc + preChar + 'pronetsessionrenew=' + escape(Math.random() * 100000) +
		'&pronetsessionrenewkey=' + escape(this.sessionKey) +
		'&pronetsessionrenewvalue=' + escape(this.sessionValue)
	);
	this.statusTimeout = window.setTimeout(this.id + '.Update(0,true)',this.statusTime);
}
this.CouldNotConnect = function()
{
	this.status.style.display = 'none';
	this.msg.innerHTML = this.msgConnErr;
	this.btn.style.display = 'block';
	this.status.style.display = 'none';	
	this.action = 'renew';	
}
this.Update = function(i, callTimeout)
{
	var badEl = (i >= this.bars.length);
	if(badEl)
	{
		var now = new Date();
		if(this.endTime.getTime() <= now.getTime() + 1000)
		{
			this.SessionEndState();
		}
		else
		{
			this.CouldNotConnect();	
		}
		return;
	}
	try
	{
		if(!this.RPS.result){}
		else if(this.RPS.result == this.successResponse)
		{
			this.startTime = new Date();
			this.endTime = new Date(this.startTime.getTime() + this.timeLeft);
			if(this.showWarning)this.warnTimeout = window.setTimeout(this.id + '.Warn()', this.timeLeft - (this.warnSeconds * 1000));
			for(var j = 0; j < this.bars.length; j++){this.SetStyle(this.bars[j],this.loadCellOnStyle,this.loadCellOnCssClass);}
			window.setTimeout(this.id+'.status.style.display=\'none\'',500);
			window.setTimeout(this.id+'.msg.innerHTML='+this.id+'.msgSuccess',500);
			window.setTimeout(this.id+'.FadeOut('+this.id+'.window)',1000);
			return;
		}
		else if(this.RPS.result == this.failureResponse)
		{
			this.SetToLogin(this.msgErr);
			return;
		}
		else
		{
			this.CouldNotConnect();
			return;
		}
	}
	catch(e){}
	for(var j = 0; j < i; j++){this.SetStyle(this.bars[j],this.loadCellOnStyle,this.loadCellOnCssClass);}
	if(callTimeout) window.setTimeout(this.id + '.Update('+ (i+1) + ',true)',this.statusTime); 			
}
this.Login = function()
{
	if(this.loginUrl) document.location = this.loginUrl;
	else alert('no login url specified');
}
this.ResetBars = function()
{
	for(var j = 0; j < this.bars.length; j++){this.SetStyle(this.bars[j],this.loadCellStyle,this.loadCellCssClass);}
}}
