function PCSLoader(ControlId,DefaultMessage,FitToWidth,VerticalAlignment,OnTop,Square,OverlayOnly){
	
	this.Controls = new PCSLoaderControls(this);
	this.DefaultMessage='Refreshing...'
	this.FitToWidth=false;
	this.VerticalAlignment='middle';
	this.OnTop=false;
	this.ControlId = ControlId;
	this.Square=false;
	this.Enabled=true;
	this.OverlayOnly=false;
	
	this.Constructor = function(DefaultMessage,FitToWidth,VerticalAlignment,OnTop,Square,OverlayOnly){
		this.DefaultMessage = (!DefaultMessage ? 'Refreshing...' : DefaultMessage);
		this.FitToWidth = (!FitToWidth ? false : FitToWidth);
		this.VerticalAlignment = (!VerticalAlignment ? 'middle' : VerticalAlignment);
		this.OnTop = (!OnTop ? false : OnTop);
		this.Square = (!Square ? false : Square);
		this.OverlayOnly = (!OverlayOnly ? false : OverlayOnly);
		return this;
	}
	
	this.ShowControl = function(ControlId,strMessage){this.Controls.Show(ControlId,strMessage)};
	this.Show = function(strMessage){this.Controls.ShowAll(strMessage)};
	this.HideControl = function(ControlId){this.Controls.Hide(ControlId)};
	this.Hide = function(){this.Controls.HideAll()};
	
	this.Constructor(DefaultMessage,FitToWidth,VerticalAlignment,OnTop,Square,OverlayOnly);
	
	if(document.getElementById(ControlId) || ControlId.toLowerCase()=='body'){
		this.Controls.Add(ControlId).Constructor(this.DefaultMessage,this.FitToWidth,this.VerticalAlignment,this.OnTop,this.Square,this.OverlayOnly);
	}
	
}
function PCSLoadersVisible(){
	var divs = document.getElementsByTagName('div');
	for(var i=0; i<divs.length; i++){  
		if(divs[i].id.indexOf('_Loader') > -1){
			if(divs[i].style.display != 'none') return true;
		}
	}
}
PCSLoader.prototype.Reposition=function(){
	this.Controls.Reposition();
}
function PCSLoaderControls(Loader){
	this.Loader = Loader;
	this.Controls = new Array();
}
PCSLoaderControls.prototype.Add=function(){
	for(var i=0;i<arguments.length;i++){
		this.Controls[this.Controls.length] = new PCSLoaderControl(this.Loader,arguments[i]);
		this.Controls[i].Constructor(this.Loader.DefaultMessage,this.Loader.FitToWidth,
			this.Loader.VerticalAlignment,this.Loader.OnTop,this.Loader.Square,this.Loader.OverlayOnly);
	}
	if(arguments.length > 1) return null;
	return this.Controls[this.Controls.length-1];	
}
function PCSLoaderControl(Loader,ControlId){
	this.Loader = Loader;
	this.ControlId = ControlId;
	
	if(ControlId.toLowerCase()=='body')
		this.Parent = document.body
	else
		this.Parent=document.getElementById(ControlId);
		
	this.LoaderDIV=null;
	this.MessageDIV=null;
	this.MessageContainer=null;
	this.DefaultMessage='';
	this.VerticalAlignment=false;
	this.FitToWidth=false;
	this.OnTop=false;
	this.XTop=null;
	this.XBottom=null;
	this.Square=false;
	this.OverlayOnly=false;
	this.Constructor = function(DefaultMessage,FitToWidth,VerticalAlignment,OnTop,Square,OverlayOnly){
		this.DefaultMessage = (!DefaultMessage ? 'Refreshing...' : DefaultMessage);
		this.FitToWidth = (!FitToWidth ? false : FitToWidth);
		this.VerticalAlignment = (!VerticalAlignment ? 'middle' : VerticalAlignment);
		this.OnTop = (!OnTop ? false : OnTop);
		this.Square = (!Square ? false : Square);
		this.OverlayOnly = (!OverlayOnly ? false : OverlayOnly);
		return this;
	}	
}
PCSLoaderControls.prototype.Reposition=function(strMessage){
	for(var i=0;i<this.Controls.length;i++){
		this.Controls[i].Reposition();
	}
}
PCSLoaderControls.prototype.ShowAll=function(strMessage){
	if(!this.Loader.Enabled) return;
	for(var i=0;i<this.Controls.length;i++){
		this.Controls[i].Show(strMessage);
	}
}
PCSLoaderControls.prototype.Show=function(ControlId,strMessage){
	if(!this.Loader.Enabled) return;
	var ctrl = this.FindById(ControlId);
	if(!ctrl) return;
	ctrl.Show(strMessage);
}
PCSLoaderControls.prototype.HideAll=function(){
	for(var i=0;i<this.Controls.length;i++){
		this.Controls[i].Hide();
	}
}
PCSLoaderControls.prototype.Hide=function(cid){
	var ctrl = this.FindById(cid);
	if(!ctrl) return;
	ctrl.Hide();
}
PCSLoaderControls.prototype.FindById=function(ControlId){
	for(var i=0;i<this.Controls.length;i++){
		if(this.Controls[i].ControlId==ControlId) return this.Controls[i];
	}
	return null;
}

PCSLoaderControl.prototype.Show=function(message){
	//this.LoaderDIV = document.getElementById(this.ControlId+'_loader');
	if(!this.LoaderDIV) this.BuildLoader();
	if(this.LoaderDIV.style.display!='none') return;
	//this.MessageDIV = document.getElementById(this.ControlId+'_message');
	//this.MessageContainer = document.getElementById(this.ControlId+'_container');
	
	if(this.ControlId.toLowerCase()=='body')
		this.Parent = document.body
	else
		this.Parent =document.getElementById(this.ControlId);
		
	var message = (!message ? this.DefaultMessage : message);
	this.Message(message);
	this.Reposition();
	this.LoaderDIV.style.display='block';
	if(!this.OverlayOnly){
		this.MessageContainer.style.display='block';
	}
	this.XTop.style.width = (this.MessageDIV.clientWidth+2) + 'px';
	this.XBottom.style.width = (this.MessageDIV.clientWidth+2) + 'px';
	this.Reposition();
}
PCSLoaderControl.prototype.Hide=function(){
	//this.LoaderDIV = document.getElementById(this.ControlId+'_loader');
	//this.MessageDIV = document.getElementById(this.ControlId+'_message');
	//this.MessageContainer = document.getElementById(this.ControlId+'_container');
	//if(!this.LoaderDIV) return;
	if(!this.LoaderDIV) this.BuildLoader();
	this.LoaderDIV.style.display='none';
	this.MessageContainer.style.display='none';
}

PCSLoaderControl.prototype.BuildLoader=function(){

	this.LoaderDIV = document.createElement('DIV');
	this.LoaderDIV.id = this.ControlId + '_Loader';
	this.LoaderDIV.style.display='none';
	this.LoaderDIV.className= (this.OverlayOnly ? 'PCSLoaderOverlayOnly' : 'PCSLoader');
	this.LoaderDIV.style.left='0px';
	this.LoaderDIV.style.top='0px';
	this.LoaderDIV.style.position='absolute';
	
	this.MessageContainer = document.createElement('DIV');
	this.MessageContainer.id = this.ControlId + '_container';
	this.MessageContainer.style.display='none';
	this.MessageContainer.style.position='absolute';
	this.MessageContainer.className = 'curvedborders';
	
	this.MessageDIV = document.createElement('DIV');
	this.MessageDIV.id = this.ControlId + '_Message';
	this.MessageDIV.className = 'xboxcontent' + (this.Square ? 'square' : 'round');
	
	this.XTop = document.createElement('B');
	this.XTop.className = 'xtop';
		
	if(!this.Square){
		
		var x = document.createElement('B');
		x.className = 'xb1';
		this.XTop.appendChild(x);
		
		var x = document.createElement('B');
		x.className = 'xb2';
		this.XTop.appendChild(x);
		
		var x = document.createElement('B');
		x.className = 'xb3';
		this.XTop.appendChild(x);
		
		var x = document.createElement('B');
		x.className = 'xb4';
		this.XTop.appendChild(x);
		
	}
	
	this.MessageContainer.appendChild(this.XTop);
	
	var nobr = document.createElement('NOBR');
	nobr.appendChild(this.MessageDIV);
	this.MessageContainer.appendChild(nobr);

	this.XBottom = document.createElement('B');
	this.XBottom.className = 'xbottom';
		
	if(!this.Square){
	
		var x = document.createElement('B');
		x.className = 'xb4';
		this.XBottom.appendChild(x);
		
		var x = document.createElement('B');
		x.className = 'xb3';
		this.XBottom.appendChild(x);
		
		var x = document.createElement('B');
		x.className = 'xb2';
		this.XBottom.appendChild(x);
		
		var x = document.createElement('B');
		x.className = 'xb1';
		this.XBottom.appendChild(x);
		
	}
	
	this.MessageContainer.appendChild(this.XBottom);

	document.body.insertBefore(this.LoaderDIV, document.body.firstChild);
	document.body.insertBefore(this.MessageContainer, document.body.firstChild);
	
	this.XTop.style.width = this.MessageDIV.clientWidth + 'px';
	this.XBottom.style.width = this.MessageDIV.clientWidth + 'px';
	
	var l = this;
	window.attachEvent('onresize', function(){setTimeout(function(){l.Reposition()},50)});
	window.attachEvent('onscroll', function(){setTimeout(function(){l.Reposition()},50)});
}
PCSLoaderControl.prototype.Move=function(x,y,w,h,s){
	
	if(isNaN(x)) return;
	if(!this.LoaderDIV) return;
	
	this.LoaderDIV.style.left=x+'px';
	this.LoaderDIV.style.top=y+1+'px';
	this.LoaderDIV.style.width=w+'px';
	this.LoaderDIV.style.height=h+'px';
	if(x<=0 && y<=0 && this.Parent.tagName.toLowerCase() != 'body'){
		//This fixes a bug where loads from invisible tabs are shown
		//Because the control is invisible, the loader shows at 0 or less and makes things look weird
		this.LoaderDIV.style.left=-32000;
		this.LoaderDIV.style.top=-32000;
		this.MessageContainer.style.left=-32000;
		this.MessageContainer.style.top=-32000;
		return;
	}
	
	if(this.FitToWidth) this.MessageContainer.style.width=parseInt(this.Parent.clientWidth)+'px';
	
	this.MessageContainer.style.left = x + (w / 2) - (this.MessageContainer.clientWidth / 2) - 1;	
	this.MessageContainer.style.top = y + 1 + (h / 2) - (this.MessageContainer.clientHeight / 2);
	
	if(this.OnTop){
		this.LoaderDIV.style.zIndex = 32000;
		this.MessageContainer.style.zIndex = 32001;
	}else{
		this.LoaderDIV.style.zIndex = parseInt(this.Parent.style.zIndex) + 2;
		this.MessageContainer.style.zIndex = parseInt(this.Parent.style.zIndex) + 3;
	}
}
PCSLoaderControl.prototype.Reposition=function(){
	var xy = this.Loader.GetCoordinates(this.Parent);
	var w = parseInt(this.Parent.clientWidth);
	var h = parseInt(this.Parent.clientHeight);
	this.Move(xy[0],xy[1],w,h);
}
PCSLoaderControl.prototype.Message=function(msg){
	this.MessageDIV.innerHTML = msg + '<img src="../images/loading.gif" border=0 align="absmiddle">';
}
PCSLoader.prototype.GetCoordinates= function (el){
    var parent=null;
    var coords=[];
    var box=new Array();
    if (el.getBoundingClientRect){
	    box=el.getBoundingClientRect();
        var scrollTop=document.body.scrollTop;
        var scrollLeft=document.body.scrollLeft;
        var x=box.left+scrollLeft;
        var y=box.top+scrollTop-1;
        return [x,y];
    } else if (document.getBoxObjectFor){
        box=document.getBoxObjectFor(el); 
        coords=[box.x-2,box.y-2];
    } else {
        coords=[el.offsetLeft,el.offsetTop];
        parent=el.offsetParent;
        if (parent!=el){
            while (parent){
                coords[0]+=parent.offsetLeft;
                coords[1]+=parent.offsetTop;
                parent=parent.offsetParent;
            }
        }
    }
    if (window.opera){
        parent=el.offsetParent;
        while (parent && parent.tagName!="BODY" && parent.tagName!="HTML"){
            coords[0]-=parent.scrollLeft;
            coords[1]-=parent.scrollTop;
            parent=parent.offsetParent;
        }
    }else {
        parent=el.parentNode;
        while (parent && parent.tagName!="BODY" && parent.tagName!="HTML"){
            coords[0]-=parent.scrollLeft;
            coords[1]-=parent.scrollTop;
            parent=parent.parentNode;
        }   
    }
    return coords;
}