/* Modal window functions */

var fncOnLoad = null;

function showModal(u, w, h, t, _fncOnLoad, _fncOnClose) {

	var th = h + 32; // Account for title bar

	var objDivC = $('modalContainer');
	objDivC.onclick = function() {
		if (_fncOnClose) {
			_fncOnClose();
		}
		hideModal('modalPage');
	};

	var objDivI = $('modalItems');
	objDivI.style.width = w + "px";
	objDivI.style.height = th + "px";
	moveModal(objDivI, th);

	$('modalTop').style.width = w + "px";
	$('modalTitle').innerHTML = t

	$('modalClose').onclick = function() {
		if (_fncOnClose) {
			_fncOnClose();
		}
		hideModal('modalPage');
	};

	fncOnLoad = _fncOnLoad;
	var strOnLoad = (_fncOnLoad != null) ? 'onLoad="fncOnLoad();" ' : '';
	var strFrame = '<iframe id="modalFrame" name="modalFrame" src="' + u + '" style="width:' +  w + 'px;height:' + h + 'px;" ' + strOnLoad + 'frameborder="0" scrolling="auto"></iframe>';
	$('modalBody').innerHTML = strFrame;

	var objDivP = $('modalPage');
	objDivP.style.display = "block";
	objDivP.style.top = document.body.scrollTop;

	window.onscroll = function () {
		objDivP.style.top = document.body.scrollTop;
	};
	window.onresize = function() {
		moveModal(objDivI, th);
	}

	if (window.XMLHttpRequest == null) {
		replaceSelectsWithSpans();
	}
}

function moveModal(objDiv, h) {
	if (h < document.body.clientHeight) {
		objDiv.style.marginTop = (document.body.clientHeight - h) / 2;
	} else {
		objDiv.style.marginTop = 20
	}
}

function hideModal(divID) {
	objThis = (window.parent != null) ? window.parent : window;
	objThis.onscroll = null;
	objThis.document.getElementById('modalPage').style.display = "none";
	removeSelectSpans();
}

function removeSelectSpans() {
	var selects = document.getElementsByTagName('select');
	
	for (var i = 0; i < selects.length; i++) {
		var select = selects[i];
		
		if (select.clientWidth == 0 || select.clientHeight == 0 || 
			select.nextSibling == null || select.nextSibling.className != 'selectReplacement') {
			continue;
		}
		
		select.parentNode.removeChild(select.nextSibling);
		select.style.display = select.cachedDisplay;
	}
}

function replaceSelectsWithSpans() {

	var selects = document.getElementsByTagName('select');
	
	for (var i = 0; i < selects.length; i++) {
		var select = selects[i];
		
		var div = document.createElement('div');
		div.style.height = (select.clientHeight) + 'px';
		div.style.width = (select.clientWidth) + 'px';
		div.className = 'selectReplacement';
		
		div.innerHTML = '<div class="selectReplacementText">' + select.options[select.selectedIndex].innerHTML + 
			'</div><img src="/images/dropdown.gif" />';
		
		select.cachedDisplay = select.style.display;
		select.style.display = 'none';
		select.parentNode.insertBefore(div, select.nextSibling);

		if (select.clientWidth == 0 || select.clientHeight == 0 || 
			select.nextSibling == null || select.nextSibling.className == 'selectReplacement') {
			continue;
		}

	}
}

/* Utility functions */

function $(id) {
	return document.getElementById(id);
}

