
function pageWidth() {
	return window.innerWidth != null ? window.innerWidth 
		: document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth
			: document.body != null ? document.body.clientWidth
				: null;
}

function pageHeight() {
	return window.innerHeight != null ? window.innerHeight
		: document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight
			: document.body != null ? document.body.clientHeight
				: null;
}

function posLeft() {
	return typeof window.pageXOffset != 'undefined' ? window.pageXOffset
		: document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft
			: document.body.scrollLeft ? document.body.scrollLeft 
				: 0;
}

function posTop() {
	return typeof window.pageYOffset != 'undefined' ? window.pageYOffset
		: document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop
			: document.body.scrollTop ? document.body.scrollTop
				: 0;
}

function _gel(x){
	return document.getElementById(x);
}

function scrollFix() {
	var modalDivContainerObj = _gel('modaldivcontainer');
	if (!modalDivContainerObj) { return; }
    if (modalDivContainerObj.style.display == 'block') {
	    var semiTransparentLayerObj=_gel('semi_transparent_layer');
	    semiTransparentLayerObj.style.top=(posTop()) + 'px';
	    semiTransparentLayerObj.style.left=(posLeft()) + 'px';
	    //positionFix(parseInt(modalDivContainerObj.style.width), parseInt(modalDivContainerObj.style.height));
	}
}

function sizeFix() {
	var modalDivContainerObj = _gel('modaldivcontainer');
	if (!modalDivContainerObj) { return; }
    if (modalDivContainerObj.style.display == 'block') {
	    var semiTransparentLayerObj=_gel('semi_transparent_layer');
	    semiTransparentLayerObj.style.height=(pageHeight()) + 'px';
	    semiTransparentLayerObj.style.width=(pageWidth()) + 'px';
  	    positionFix(parseInt(modalDivContainerObj.style.width), parseInt(modalDivContainerObj.style.height));
	}
}

function positionFix(widthOfDialog, heightOfDialog) {
    var modalDivContainerObj = _gel('modaldivcontainer');
    modaldivcontainer.ID =  	"modaldivcontainer";
    if (!modalDivContainerObj) { return; }
	var topOffset = posTop() + ((pageHeight() - heightOfDialog)/2) - 12;
	var leftOffset = posLeft() + ((pageWidth() - widthOfDialog)/2) - 12;
	modalDivContainerObj.style.top = (topOffset < 0 ? 0 : topOffset) + 'px';
	modalDivContainerObj.style.left= (leftOffset < 0 ? 0 : leftOffset) + 'px';
	modalDivContainerObj.style.width = widthOfDialog + 'px';
	modalDivContainerObj.style.height = heightOfDialog + 'px';
}	
	
function showmodaldialog(contentUrl,widthOfDialog, heightOfDialog)
{
    return showmodaldialog(contentUrl, widthOfDialog, heightOfDialog, true);
}
var selectedText;
function showmodaldialog(contentUrl, widthOfDialog, heightOfDialog, async) 
{ 
	initializemodalsetup();
	if (_gel('semi_transparent_layer')) 
	{
		//alert('we good');
	} 
	else
	{
		initializemodalsetup();
	}
	var date = new Date();
	
	hidedropdownlists();

	contentUrl += (contentUrl.indexOf('?') >= 0 ? '&' : '?') + "date=" + date; // make sure URL isn't cached	
	
	contentUrl += '&ID=' + selectedText;
	createXHR();
	var contentObj = _gel('modaldialogcontent');
	if (!contentObj) { alert('Script error.'); return; }

	contentObj.innerHTML = '<div align=center><b>Loading...</b><br/>';

	var semiTransparentLayerObj = _gel('semi_transparent_layer');
	semiTransparentLayerObj.style.display = 'block';	

	var modalDivContainerObj = _gel('modaldivcontainer');
	
	modalDivContainerObj.style.display = 'block';

	positionFix(widthOfDialog, heightOfDialog);
	scrollFix();
	sizeFix();	
	xmlHttpModalDialog.onreadystatechange = handleModalUrlLookupCallback;
	xmlHttpModalDialog.open("GET", contentUrl, true);
	xmlHttpModalDialog.send(null);
	return false;
}
function createXHR()
{
	try
	{
		xmlHttpModalDialog= new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			xmlHttpModalDialog= new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(ex)
		{
			xmlHttpModalDialog=null;
		}	
	}
	if(!xmlHttpModalDialog && typeof XMLHttpRequest != "Undefined")
	{
		xmlHttpModalDialog= new XMLHttpRequest();

	}
}

function hidemodaldialog() {
	_gel('semi_transparent_layer').style.display = 'none';
	_gel('modaldivcontainer').style.display = 'none';
}
function hidedropdownlists() 
{
	var body = document.getElementsByTagName('body')[0];
	var selects = body.getElementsByTagName("select");
	var a ;
	for (var i=0;i<selects.length;i++)
	{
		a = selects[i];
		if(selects[i].name.indexOf('ddlContactUniqueIdentifier') != -1)
		{
			a = selects[i];
			selectedText=a.options[a.selectedIndex].text;
		}	
		selects[i].style.display = 'none';
	}
}
function showdropdownlists() 
{
	var body = document.getElementsByTagName('body')[0];
	var selects = body.getElementsByTagName("select");
	for (var i=0;i<selects.length;i++)
	{	
		selects[i].style.display = 'block';
	}
}

function initializemodalsetup() { 
	var bodyObj = document.getElementsByTagName('body')[0];
	
	var modalFragmentObj = document.createDocumentFragment();
	
	var transparentLayerObj = document.createElement('div');	
	
	transparentLayerObj.setAttribute('id','semi_transparent_layer');
	transparentLayerObj.className = 'semi_transparent_layer';
	transparentLayerObj.style.display = 'none';
	transparentLayerObj.style.position = 'absolute';
	transparentLayerObj.style.top = 0;
	transparentLayerObj.style.left = 0;
	transparentLayerObj.style.zIndex = 998;
	transparentLayerObj.style.width='100%';
	
	modalFragmentObj.appendChild(transparentLayerObj);
	
	var modalDivContainerObj = document.createElement('div');
	modalDivContainerObj.className = 'modaldivcontainer';
	modalDivContainerObj.setAttribute('id','modaldivcontainer');
	modalDivContainerObj.style.display = 'none';
	modalDivContainerObj.style.position = 'absolute';
	modalDivContainerObj.style.backgroundColor = "#FFFFFF";
	modalDivContainerObj.style.noWrap ="false";
	modalDivContainerObj.style.borderWidth ="1px";
	modalDivContainerObj.style.zIndex=999;
	
	/*
	var titleBarObj = document.createElement('div');
	titleBarObj.className = 'modaltitlebar';
	//cellpadding="3" cellspacing="1"	
	titleBarObj.innerHTML = '<table border="0" width="100%"><tr><td class="modaltitle" align="left">Hobsons-Emtng	</td><td valign="middle" align="right"><a href="#" onclick="hidemodaldialog();showdropdownlists(); return false;">Close</a>&nbsp;</td></tr></table>';
	modalDivContainerObj.appendChild(titleBarObj);	*/
    
	var modalDivContainerSpanObj = document.createElement('span');
	modalDivContainerObj.appendChild(modalDivContainerSpanObj);
	
	var modalDialogContentObj = document.createElement('div');
	modalDialogContentObj.setAttribute('id','modaldialogcontent');		
	modalDivContainerSpanObj.appendChild(modalDialogContentObj);
	
	modalFragmentObj.insertBefore(modalDivContainerObj, transparentLayerObj.nextSibling);
	bodyObj.insertBefore(modalFragmentObj, bodyObj.firstChild);
		
	window.onscroll = scrollFix;
	window.onresize = sizeFix;
}




function handleModalUrlLookupCallback()
 {
	if(xmlHttpModalDialog.readyState == 4) {
		if(xmlHttpModalDialog.status == 200) {
			var contentObj = _gel('modaldialogcontent');
			if (!contentObj) { alert('Script error.'); return; }
		
			contentObj.innerHTML = xmlHttpModalDialog.responseText;
						
			scrollFix();
			sizeFix();	

			return false;												
		} else {
			alert('Error downloading data!');
		}
	}	
}

var xmlHttpModalDialog;

