/* Javascript March 1, 2010*////////////////////////////////////////////////
// BHPCL version 2.02
// Copyright 2003-2008 Howard Brown
/////////////////////////////////////////////
function BHPCL()
{
	bhUI.createObject('PCL',this);
	var img,div=this.div=bhUI.createElement();
	div.style.position="absolute";div.style.overflow='hidden';
	div.style.width=div.style.top=div.style.left='0px';
	div.style.visibility='hidden';
	div=bhUI.createElement();
	img=div.addElement().addElement('IMG');
	img.src='/newlook/img/loading.gif';div.className="BusyNotice";
	div.onkeydown=div.onmousedown=div.onmouseover=bhUI.eventHandle(this.Tx+".busyNotice.eventTrap");
	this.busyNotice={me:this,div:div,timeout:0,
	show:function(evt){this.div.style.display='block';},
	hide:function(){clearTimeout(this.timeout);this.div.style.display='none';},
	eventTrap:function(evt){if(evt.type!='mousemove'){
	evt.preventDefault();evt.stopPropagation();}
	var b=bhUI.getScroll();this.div.style.left=(b.left+evt.clientX)-28+'px';
	this.div.style.top=(b.top+evt.clientY)-28+'px';},
	startTimer:function(){this.timeout=setTimeout(pcl.Tx+'.busyNotice.show()',600);},
	stopTimer:function(){clearTimeout(this.timeout);}};
	this.packets=new Array();
	this.busy=false;this.action=this.setup;
	this.requestId=(new Date()).getTime();this.busyNotice.hide();
	this.useXML=true;
}

BHPCL.prototype.setup=function()
{
	this.action=__DF;
	if(document.readyState&&document.readyState!='complete'){
	setTimeout(this.Tx+'.setup()',10);return;}
	document.body.appendChild(this.div);
	var div=this.busyNotice.div,w=document.getElementById('app-wrapper');
	div.style.top=Math.floor(w.offsetTop+(w.offsetHeight/3))+'px';
	div.style.left=Math.floor(w.offsetLeft+(w.offsetWidth/3))+'px';
	document.body.appendChild(div);
	document.addEventListener("mousemove",div.onmousedown,false);
	if(this.useXML)
	{
		if(window.XMLHttpRequest){
		this.request={create:function(){return new XMLHttpRequest();}}
		}else if(window.ActiveXObject){
		this.request={create:function(){return new ActiveXObject("Msxml2.XMLHTTP");}}
		try {(new ActiveXObject("Msxml2.XMLHTTP"));}catch(e){
		this.request={create:function(){return new ActiveXObject("Msxml2.XMLHTTP");}}
		try {(new ActiveXObject("Microsoft.XMLHTTP"));}catch(e){this.useXML=false;}}}
	}
	if(this.useXML)
	{
		this.request.onreadystatechange=new Function(this.Tx+".readystatechange()");
		this.request.open=function(m,u)
		{
			this.xmlhttp=this.create();
			this.xmlhttp.onreadystatechange=this.onreadystatechange;
			this.xmlhttp.open(m,u,true);
		};
		this.request.send=function(d)
		{
			this.xmlhttp.send(d);
		};
		this.request.setHeader=function(n,d)
		{
			this.xmlhttp.setRequestHeader(n,d);
		}
	}
	else
	{
		this.request=new Object();
		this.request.open=function(m,u)
		{
			this.method=m;this.url=u;
		};
		this.request.setHeader=new Function("");
		this.request.send=function(data)
		{
			if(this.method=='GET')this.get();else{
			var doc=this.iframe.contentWindow.document;doc.open('text/html','replace');
			doc.write('<form name="form1" action="'+this.url+'" method="'+this.method+'">');
    		if(data){var i,e,d=data.split('&');for(i=0;i<d.length;i++){e=d[i].split('=');
			doc.write('<textarea name="'+e[0]+'">'+decodeURIComponent(e[1])+'</textarea>');}}
			doc.write('</form>');doc.close();this.iframe.contentWindow.document.form1.submit();}
		};
		this.request.iframe=this.div.addElement('IFRAME');
		if(bhUI.Browser.IE)this.request.div=this.div.addElement();
		this.request.get=new Function(bhUI.Browser.IE?"this.div.innerHTML='<iframe src=\"'+this.url+'\"></iframe>'":
		"this.iframe.contentWindow.location.replace(this.url)");
		if(!this.request.iframe.contentWindow){}
	}
	this.action=this.send;
	this.send();
}

BHPCL.prototype.readystatechange=function()
{
	var k=this.request.xmlhttp;
	if(k.readyState==4&&k.status==200){
		var xpcl,e;
		xpcl=k.responseXML.getElementsByTagName('xpcl').item(0);
		if((e=xpcl.getElementsByTagName('load')).length){
			(new Function(this.Tx+".load("+e.item(0).firstChild.nodeValue+")"))();
		}else
		if((e=xpcl.getElementsByTagName('message')).length){
			(new Function(this.Tx+".UI.MessageBox("+e.item(0).firstChild.nodeValue+")"))();
		}
		this.exit();
	}
}

BHPCL.prototype.send=function()
{			
	if(this.busy||this.packets.length==0){
	this.busyNotice.hide();return;}
	this.busy=true;var m=this.packets[0],s='';
	if(m.data){
		var x=s,i,e,d=m.data;
    	if(typeof(d.elements)!=__UF){
			d=d.elements;
			for(i=0;i<d.length;i++){
				e=d[i];
				if(e.disabled)continue;
				if(e.type=='checkbox' && e.checked==false)continue;
				s+=x+e.name+'='+encodeURIComponent(e.value);
				x='&';
			}
		}else{   
			for(i=0;i<d.length;i++){
				e=d[i];
				s+=x+e[0]+'='+encodeURIComponent(e[1]);
				x='&';
			}
		}
	}
	this.timeout=setTimeout(this.Tx+".report()",30000);
	this.busyNotice.startTimer();
	this.load=m.onload?m.onload:__DF;
	if(m.method=='POST'){
		this.request.open('POST',m.url);
		this.request.setHeader('Content-Type', 'application/x-www-form-urlencoded');
		this.request.send(s);
	}
	else{
		this.request.open('GET',m.url+(s?(((m.url.indexOf('?')==-1)?'?':'&')+s):''));
		this.request.send(null);
	}
}

BHPCL.prototype.open=function(p)
{
	if(p.id){
		for(var i=0;i<this.packets.length;i++){
			if(this.packets[i].id==p.id)
			   return false;
		}
	}
	var e=new Object();
	e.url=p.url.charAt(0)==':'?'/pcl/?'+p.url.substring(1):p.url;
	e.url+=((e.url.indexOf('?')==-1)?'?':'&')+'PCLID='+(this.requestId++)+(this.useXML?'&xml':'&PCLTX='+this.Tn);
	e.onload=p.onload;e.data=p.data;e.id=p.id;
	e.method=p.method?p.method.toUpperCase():'GET';
	this.packets.push(e);setTimeout(this.Tx+".action();",20);
	return true;
}

BHPCL.prototype.suspend=function()
{
	this.action=__DF;
}

BHPCL.prototype.report=function()
{
	bhUI.MessageBox('!','Communication Error','');
	pcl.exit();
}

BHPCL.prototype.exit=function()
{
	if(!this.busy)return;
	clearTimeout(this.timeout);
	this.busyNotice.stopTimer();
	var m=this.packets.shift();	
	setTimeout(this.Tx+'.busy=false;'+this.Tx+'.action();',20);
}
