
	

popupMessage = function()
{
		init();	
		
		
		
}

function init()
{
	
	wFormsValidation = true;
	counter = -1;
	
}

function resizePopup()
{
	
	if (counter >=0)
	{
		
		heightWidth = getHeightWidth();
		var docHeight = heightWidth[0];
		var docWidth = heightWidth[1];		
		transDiv.style.display='block';		
		transDiv.style.width = docWidth+'px';
		transDiv.style.height = docHeight+'px';
	}
}



popupMessage.prototype = {
	
	

	displayMessage : function (url, str) {
		counter++;
		heightWidth = getHeightWidth();
		__createMessage(url, str);
		__createCloseCross();
		if (counter ==0)
		{
			__createTransparant();
		}
		window.onresize = resizePopup;
		//alert(messageDiv.style.zIndex+ " " +transDiv.style.zIndex);	
		
		
	},
	
	displayResponse : function(url, str){
		messageDivResponse = document.getElementById("messageDiv"+counter);
		var response = __insertContent(url, str);
		messageDivResponse.innerHTML = response;
		__createCloseCross();
		
	},
	
	closePopup : function(){
		
		messageDivClose = document.getElementById("messageDiv"+counter);
		if (counter == 0)
		{
			transDivClose = document.getElementById("transDiv");
			transDivClose.parentNode.removeChild(transDivClose);
		}else
			{
				transDiv.style.zIndex = transDiv.style.zIndex-2;
			}
		
		messageDivClose.parentNode.removeChild(messageDivClose);
		counter--;
		if (wFormsValidation)
		{
		wFORMS.behaviors['validation'].errorPlaceHolder = 'alertMessagePlaceHolder'+counter;
		}
	}
	
	
}



function __createDivs()
	{
		
		
		
	}

function __createMessage(url, str)
	{
		messageDiv = document.createElement('div');
		messageDiv.setAttribute('id', "messageDiv"+counter);
		document.body.appendChild(messageDiv);
		
		
		//displayProps
		messageDiv.style.display = "block";
		messageDiv.style.border = "1px";
		messageDiv.style.borderStyle="solid";
		messageDiv.style.borderColor = "#636563";
		messageDiv.style.backgroundColor="#FFF";
		messageDiv.style.margin = '5';
		//Format
		var messageWidth = 500;
		var leftPossition = (heightWidth[1]/2)-(messageWidth/2)+counter*30;
		var amountScrolled = f_scrollTop();
		messageDiv.style.height = 'auto';
		messageDiv.style.position = "absolute";
		messageDiv.style.width = messageWidth+'px';
		var t1 = messageDiv.style.width;
		
		
		messageDiv.style.zIndex="1000"+(counter*2);
		messageDiv.style.left = leftPossition+'px';		
		messageDiv.style.top = amountScrolled+10+counter*30+"px";
		
		var content = __insertContent(url,str);
		messageDiv.innerHTML = content;
		
				
		//For if the content is a form that will be validated by wForms
		if (wFormsValidation){
		wFORMS.helpers.activateStylesheet("wforms-jsonly.css"); 
		wFORMS.onLoadHandler();		
		var t = document.getElementById("alertMessagePlaceHolder");
		t.setAttribute('id','alertMessagePlaceHolder'+counter);
		t.setAttribute('class','alertMessagePlaceHolder');
		//alert(document.getElementById("alertMessagePlaceHolder"+counter).id);
		wFORMS.behaviors['validation'].errorPlaceHolder = 'alertMessagePlaceHolder'+counter;
		}
		if (counter > 0)
		{
			transDiv.style.zIndex=messageDiv.style.zIndex-1;
		}
		
		
	}
	
function __createCloseCross()
	{
		closer = document.createElement('div');
		closer.setAttribute('id','closer'+counter);
		messageDiv.appendChild(closer);
		closer.style.display = 'block';
		closer.style.position = "absolute";
		closer.innerHTML = '<a href="##" onclick="javascript:popupMessage.closePopup()"><img src="/graphics/button_cross.gif"></a>';
		closer.style.top = '1px';
		closer.style.right = '1px';
	}

function __createTransparant()
	{
		transDiv = document.createElement('div');		
		transDiv.setAttribute('id',"transDiv");
		document.body.appendChild(transDiv);
		var docHeight = heightWidth[0];
		var docWidth = heightWidth[1];		
		transDiv.style.display='block';
		transDiv.style.width = docWidth+'px';
		transDiv.style.zIndex=messageDiv.style.zIndex-1;
		transDiv.style.height = docHeight+'px';
		transDiv.style.left = 0;
		transDiv.style.top = 0;
		transDiv.style.position = "absolute";
		transDiv.style.filter="alpha(opacity=50)";
		transDiv.style.opacity="0.6";
		transDiv.style.backgroundColor="#AAA";
		
		
	}
	
function __insertContent(url,str)
	{
		
		http_request = false;
		if (window.XMLHttpRequest) { // Mozilla, Safari,...
			http_request = new XMLHttpRequest();
			if (http_request.overrideMimeType) {
				http_request.overrideMimeType('text/html');
			}
		} else if (window.ActiveXObject) { // IE
				try {
					http_request = new ActiveXObject("Msxml2.XMLHTTP");
					} catch (e) {
					try {
						http_request = new ActiveXObject("Microsoft.XMLHTTP");
					} catch (e) {}
				}
		}
		if (!http_request) {
			alert('Cannot create XMLHTTP instance');
			return false;
		}
		http_request.onreadystatechange = alertContents;
		http_request.open('POST',url, false);//FALSE TO MAKE IT SYNCHRONIOUS ELSE THE JAVASCRIPT WILL NOT WAIT FOR THE COMPLETION AND POST TO AVOID CACHE PROBLEMS IN IE
		http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http_request.setRequestHeader( "Cache-Control", "no-cache, must-revalidate" ); 
		//http_request.send(str) --> needed because otherwise the post method will not work in fireFox
		http_request.send(str);
		
		
		function alertContents() {
			if (http_request.readyState == 4) {
				if ((http_request.status == 200)) {
					//alert(http_request.responseText);
					result = http_request.responseText;
					totalResult = result;
										  					
			} else {
				//Error server side / abort test
				alert('There was a problem with the request.');
				}
			}
		}
		
		return totalResult;
		}


function getHeightWidth()
{
	var pageWidth = 0, pageHeight = 0;
	if( window.innerHeight && window.scrollMaxY ) // Firefox 
		{
		//pageWidth = window.innerWidth + window.scrollMaxX;
		pageHeight = window.innerHeight + window.scrollMaxY;
		}
		else if( document.body.scrollHeight > document.body.offsetHeight ) // all but Explorer Mac
		{
		//pageWidth = document.body.scrollWidth;
		pageHeight = document.body.scrollHeight;
		}
		else // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		{ 
		//pageWidth = document.body.offsetWidth + document.body.offsetLeft; 
		pageHeight = document.body.offsetHeight + 		document.body.offsetTop; 
		}
	pageWidth = document.body.clientWidth;
	
	
	return [pageHeight,pageWidth]

}

function f_scrollTop() {
 		return _f_filterResults (window.pageYOffset ? window.pageYOffset : 0, document.documentElement ? document.documentElement.scrollTop : 0, document.body ? document.body.scrollTop : 0);
	}
	
	function _f_filterResults(n_win, n_docel, n_body) {
		var n_result = n_win ? n_win : 0;
 		if (n_docel && (!n_result || (n_result > n_docel)))
 			n_result = n_docel;
 		return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
	}



popupMessage = new popupMessage();

