//********************************************************************************************************************
// The following functions implement the AJAX callback to retrieve the WebOptions and display
// them to the user in a seamless popup.
// 
// Initial Creation : Chris Lee - 2006/06/10
//********************************************************************************************************************


var req;
var mousex;
var mousey;
var inDiv=0;


<!-- Capture the mouse movement, so we can have the x,y for the popup -->
if (document.layers) document.captureEvents(Event.MOUSEMOVE);
document.onmousemove=getMouseXY;
document.onstop=HideDiv;
		

//********************************************************************************************************************
// Initialize the XMLHttp object, depending on the browser.
//********************************************************************************************************************

function Initialize()
{
	try
	{
		req=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			req=new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(oc)
		{
			req=null;
		}
	}

	if(!req&&typeof XMLHttpRequest!="undefined")
	{
		req=new XMLHttpRequest();
	}

}


//********************************************************************************************************************
// Call the ASPX page which in turn calls the Webservice and get the results, formatted by language and mode.
//********************************************************************************************************************

function GetWebOptions(SourceSystem,ResourceType,ResourceID,Language)
{

	Initialize();
	var url="/HousePublications/GetWebOptionsCallBack.aspx?SourceSystem="+SourceSystem+"&ResourceType="+ResourceType+"&ResourceID="+ResourceID+"&language="+Language;
		if(req!=null)
	{
	
		req.onreadystatechange = Process;
		req.open("GET", url, true);
        req.send(null);
        
	}
	
}



//********************************************************************************************************************
// The callback function that will process the returned results from the asyncronous call.
//********************************************************************************************************************

function Process()
{

		if (req.readyState == 4) 
        {
            // only if "OK"
			if (req.status == 200) 
			{
			    // If nothing is returned from the request then inform the user that nothing is available.
				if(req.responseText=="")

					document.getElementById('divWebOptionContent').innerHTML = "<b>No options available... / Aucune option disponible...</b>";

				else
				{
					// Set the content of the popup with the HTML returned from the asynchronous call.
					// Set the Close button to show
					document.getElementById('divWebOptionContent').innerHTML =req.responseText;								
					document.getElementById('divWebOptionPopupCloseButton').style.display="block";

					// Check if there is only one WebOption returned.   If so, redirect to that option automatically 
					try
					{
						var webOption2 = document.getElementById('WebOption2');
					}
					catch (e)
					{
					
					}

					if(webOption2 == null)
					{
						var webOption1 = document.getElementById('WebOption1')
						if(webOption1 != null)
						{
						    window.location=webOption1.getAttribute("href"); 
						    document.getElementById('divWebOptionPopupCloseButton').style.display="none";
						    document.getElementById('divWebOptionContent').innerHTML = "<b>Loading... / Chargement...</b>";
						}
					}
			
				}
				
				// Show the popup
				ShowDiv();
			}
			else 
			{
				ShowDiv();
				document.getElementById('divWebOptionContent').innerHTML = "There was a problem retrieving data: ("+req.status+") "+req.statusText;
			}
		}
}



//********************************************************************************************************************
// Show the Div Popup and reposition it to the cursor.
//********************************************************************************************************************

function ShowDiv() 
{
   if (document.layers)
   	{
   		if ((mousex + document.layers['divWebOptionPopup'].scrollWidth) > document.documentElement.clientWidth)
   			mousex = mousex - (document.layers['divWebOptionPopup'].scrollWidth);
   		
		  if ((mousey + document.layers['divWebOptionPopup'].scrollHeight) > document.documentElement.clientHeight)
   				mousey = mousey - (document.layers['divWebOptionPopup'].scrollHeight);
   		   		
   		document.layers['divWebOptionPopup'].visibility="show";
   		document.layers['divWebOptionPopup'].style.left=mousex + 10;
   		document.layers['divWebOptionPopup'].style.top=mousey + document.documentElement.scrollTop + 10;
   	}
   else
	   {
		  if ((mousex + document.getElementById('divWebOptionPopup').scrollWidth) > document.documentElement.clientWidth)
   			mousex = mousex - (document.getElementById('divWebOptionPopup').scrollWidth);
   		
		  if ((mousey + document.getElementById('divWebOptionPopup').scrollHeight) > document.documentElement.clientHeight)
  			mousey = mousey - (document.getElementById('divWebOptionPopup').scrollHeight);
   		
			document.getElementById('divWebOptionPopup').style.visibility="visible";
		  document.getElementById('divWebOptionPopup').style.left=mousex + 10 + 'px';
		  document.getElementById('divWebOptionPopup').style.top=mousey + (document.documentElement.scrollTop ?  document.documentElement.scrollTop : document.body.scrollTop) + 10 + 'px';

  	}
	
	 // Set the focus on the first weboption in this popup
		try
		{
			var webOption1 = document.getElementById('WebOption1');
		}
		catch (e)
		{
		
		}

		if(webOption1)
				webOption1.focus();
			
	 // Make sure that the popup doesn't disappear from having moved past a previous popup.	 
   inDiv=1;
	 
}




//********************************************************************************************************************
// Hide the Div popup.
//********************************************************************************************************************

function HideDiv() 
{
   if (document.layers) document.layers['divWebOptionPopup'].visibility="hide";
   else
     document.getElementById('divWebOptionPopup').style.visibility="hidden";

}





//********************************************************************************************************************
// Track the mouse movement and update the coordinates 
//********************************************************************************************************************

function getMouseXY(e) {

   if (e)
   	{
   	 mousex = e.clientX;
   	 mousey = e.clientY;
   	}
   else
   	{
   	 mousex = event.clientX;
   	 mousey = event.clientY;
   	}
   	
}


//********************************************************************************************************************
// function that is called when the user mouses out of the popup.
//********************************************************************************************************************

function TimeoutHide()
{
	if (inDiv==0)
		HideDiv();
}

