var pauseScroll;
var currentPopUpAreaId = null;

function xmlhttpPost(strURL, strQuery) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatepage(self.xmlHttpReq.responseText, self.xmlHttpReq );
        }
    }
    self.xmlHttpReq.send(strQuery);
}

function displayPageMask(){
	document.getElementById("pageMask").style.height = (document.body.offsetHeight+30)+"px";
	document.getElementById("pageMask").style.width = "100%";
	document.getElementById("pageMask").style.display = "block";
}
function hidePageMask(){
	document.getElementById("pageMask").style.display = "none";
}
function statusMessage(msg, parent){
	if ( msg.length > 0 ) {
		parent = document.getElementById(parent);
		document.getElementById("pageMask").style.height = (document.body.offsetHeight+30)+"px";
		document.getElementById("pageMask").style.width = "100%";
		document.getElementById("pageMask").style.display = "block";
		cal = document.getElementById("statusBox");
		document.getElementById("statusMsg").innerHTML = msg;
		cal.style.width = "400px";
		cal.style.position = "absolute";
		cal.style.left = (offsetLeftOfObject(parent,0)+parent.offsetWidth/2-200)+"px";
		cal.style.top = (offsetTopOfObject(parent,0)+parent.offsetHeight/2-40)+"px";
		cal.style.display = "block";
	}else{
		document.getElementById("pageMask").style.display = "none";
		document.getElementById("statusBox").style.display = "none";
	}
}
function offsetLeftOfObject(obj, pos){
	if ( obj != null ) {
		return offsetLeftOfObject(obj.offsetParent,pos+obj.offsetLeft);
	}else{
		return pos;
	}
}
function offsetTopOfObject(obj, pos ){
	if ( obj != null ) {
		return offsetTopOfObject(obj.offsetParent,pos+obj.offsetTop*1);
	}else{
		return pos;
	}
}


function displayAjaxPopUpURL(url, relativeToObjectId, width){
	if ( width == null ) width = 400;
	pauseScroll = 1;

	// Create object handles
	var source = document.getElementById(relativeToObjectId);
	currentPopUpAreaId = "popupArea";
	var popupArea = document.getElementById(currentPopUpAreaId);

	// Send the XML HTTP request
	xmlhttpPost(url);

	displayPageMask();

	// Create and position the pop up area
	popupArea.style.width = width+"px";
	popupArea.style.position = "absolute";
	popupArea.style.left = (offsetLeftOfObject(source,0) - (width/2))+"px";
	if ( popupArea.style.left.replace("px","") < 0 ) popupArea.style.left = "10px";
	popupArea.style.top = (offsetTopOfObject(source,0) - 100)+"px";
	if ( popupArea.style.top.replace("px","") < 0 ) popupArea.style.top = "10px";
	if ( (popupArea.style.left.replace("px","")*1 + width) > document.body.offsetWidth ) {
		popupArea.style.left = (document.body.offsetWidth - width)+"px";
	}
	popupArea.innerHTML = 'Please wait...';
	popupArea.style.display = "block";

	


}

// Repositions and displays the specified pop-up area object
function displayAjaxPopUpObject(popUpAreaId, relativeToObjectId, width, centred){
	if ( width == null ) width = 400;
	pauseScroll = 1;
	currentPopUpAreaId = popUpAreaId;
	var source = document.getElementById(relativeToObjectId);
	var popupArea = document.getElementById(popUpAreaId);

	// Show background shadow
	document.getElementById("pageMask").style.height = (document.body.offsetHeight+30)+"px";
	document.getElementById("pageMask").style.width = "100%";
	document.getElementById("pageMask").style.display = "block";


	popupArea.style.position = "absolute";
	popupArea.style.width = width+"px";
	popupArea.style.left = (offsetLeftOfObject(source,0) - (width/2))+"px";
	if ( popupArea.style.left.replace("px","") < 0 ) popupArea.style.left = "10px";
	popupArea.style.top = (offsetTopOfObject(source,0) - 100)+"px";
	
	if ( popupArea.style.top.replace("px","") < 0 ) popupArea.style.top = "10px";
	if ( (popupArea.style.left.replace("px","")*1 + width) > document.body.offsetWidth ) {
		popupArea.style.left = (document.body.offsetWidth - width)+"px";
	}


	// Centre the object
	if ( centred ) {
		popupArea.style.display = "block";
		var height = popupArea.offsetHeight;
		popupArea.style.display = "";
		popupArea.style.left = "50%";
		popupArea.style.top = "50%";
		popupArea.style.marginLeft = "-"+(width/2)+"px";
		marginTop = 0 - (height/2 - scrollTop() );
		popupArea.style.marginTop = marginTop+"px";

	}




	popupArea.style.display = "block";

	// Make sure not off the top of the screen
	if ( offsetTopOfObject(popupArea,0) <  0 ) 
	{
		// Oops! It's off the screen now
		var newTop = (offsetTopOfObject(source,0)-100);
		if ( newTop < 0 ) newTop = 0; 
		popupArea.style.top = newTop+"px";
		
		popupArea.style.marginTop = "0px";
	}
	pauseScroll = 1;
}
function closeAjaxPopup(){
	document.getElementById(currentPopUpAreaId).style.display = "none";
	document.getElementById("pageMask").style.display = "none";
	pauseScroll = 0;
	currentPopUpAreaId = null;
}

/* STANDARD FUNCTIONS FOR DIMENSIONS ACROSS BROWSERS */
/*window.pageXOffset, window.pageYOffset //Firefox properties for document scroll coordinates
document.body.scrollLeft, document.body.scrollTop //Firefox properties for document scroll coordinates 

window.innerWidth, window.innerHeight //Firefox properties for window dimensions
document.body.clientWidth, document.body.clientHeight ////IE properties for window dimensions*/

function scrollTop()
{
	if ( document.documentElement.scrollTop != null )
		return document.documentElement.scrollTop;
	else if ( document.body.scrollTop != null  )
		return document.body.scrollTop;
	else
		return window.pageYOffset;
}

