// JavaScript Document

var isUploading = false;
var g_RunOnClose = null;
var g_OriginalResize = null;
var g_OriginalScroll = null;
var g_OriginalOnClick = null;
var g_SettingPopup = false;

function ccShowModalWindow(title, data, width, runonclose, sysmodal)
{
	var object = 'cc_modal_window';
	var area = document.getElementById(object);
	
	if (!area)
	{
		var html = new String();
		var yScroll;
		var xScroll;
		
		var pos = ccAreaSize();
		xScroll = pos[0];
		yScroll = pos[1];
		
		var x = (xScroll - width) / 2;
		
		html = ' \
			<table border="0" cellspacing="0" cellpadding="2" width="100%"> \
				<table border="0" cellspacing="0" cellpadding="2" width="100%"> \
					<tr> \
						<td style="background-color:#cfcfcf; border-bottom:1px solid #999999;"> \
							<table border="0" cellspacing="0" cellpadding="0" width="100%"> \
								<tr> \
									<td width="100%">' + title + '</td> \
									<td><div style="cursor:pointer;" onclick="ccHideModalWindow();"><img src="/images/icons/cancel_new.png" border="0" align="absmiddle" /></div></td> \
								</tr> \
							</table> \
						</td> \
					</tr> \
					<tr> \
						<td style="padding:5px 5px 5px 5px;">' + data + '</td> \
					</tr> \
				</table> \
			</table>';
		
		area = document.createElement('div');
		area.id = object;
		area.style.position = 'absolute';
		area.style.zIndex = '501';
		area.style.width = width + 'px';
		area.style.backgroundColor = '#ffffff';
		area.style.border = '2px solid black';
		area.innerHTML = html;
		document.body.appendChild(area);
		
		var npoDimmer = document.createElement('DIV');
		npoDimmer.id = 'cc_ModalDimmer';
		npoDimmer.style.position = 'absolute';
		npoDimmer.style.zIndex = '500';
		npoDimmer.style.top = '0px';
		npoDimmer.style.left = '0px';
		npoDimmer.style.backgroundColor = '#000000';
		npoDimmer.style.filter = 'alpha(opacity = 35)';
		npoDimmer.style.MozOpacity = '0.35';
		npoDimmer.style.opacity = '0.35';
		if (!sysmodal)
			npoDimmer.onclick = function() { ccHideModalWindow(); }
		document.body.appendChild(npoDimmer);
		
		g_RunOnClose = runonclose;
		
		var elems = document.getElementsByTagName('EMBED');
		var i = 0;
		if (elems.length > 0)
		{
			for (x in elems)
			{
				if (elems[x].style)
					elems[x].style.visibility = 'hidden';
		
				i++;
			}
		}
		
		g_OriginalResize = window.onresize;
		g_OriginalScroll = window.onscroll;
		window.onresize = function() { ccResizeOverlay('cc_ModalDimmer'); ccCenterDiv(object); }
		window.onscroll = function() { ccCenterDiv(object); }
		ccResizeOverlay('cc_ModalDimmer');
		ccCenterDiv(object);
	}
	else
	{
		ccHideModalWindow();
	}
}

function ccHideModalWindow()
{
	if (!isUploading)
	{
		if (g_RunOnClose && g_RunOnClose != '')
		{
			eval(g_RunOnClose);
		}
		
		g_RunOnClose = null;
			
		var object = 'cc_modal_window';
		var area = document.getElementById(object);
		
		var modal = document.getElementById('cc_ModalDimmer');
		if (modal)
		{
			document.body.removeChild(modal);
			window.onresize = g_OriginalResize;
			window.onscroll = g_OriginalScroll;
			g_OriginalResize = null;
			g_OriginalScroll = null;
		}
		if (area) document.body.removeChild(area);
		
		var elems = document.getElementsByTagName('EMBED');
		var i = 0;
		if (elems.length > 0)
		{
			for (x in elems)
			{
				if (elems[x].style)
					elems[x].style.visibility = 'visible';
		
				i++;
			}
		}
	}
}

function ccShowWaitWindow(title)
{
	var object_name = 'cc_processing_window';
	var area = document.getElementById(object_name);
	
	var html = new String();
	var nWidth = 400;
	var yScroll;
	var xScroll;
	
	var pos = ccAreaSize();
	xScroll = pos[0];
	yScroll = pos[1];
	
	var x = (xScroll - nWidth) / 2;
	
	if (!title) title = 'Processing';

	html = title + '<br /><img src="/images/processing.gif" border="0" alt="" title="" /><br />Please wait...';
	
	area = document.createElement('div');
	area.id = object_name;
	area.style.position = 'absolute';
	area.style.zIndex = '601';
	area.style.width = nWidth + 'px';
	area.style.textAlign = 'center';
	area.style.fontSize = '24px';
	area.style.fontWeight = 'bold';
	area.style.color = 'white';
	area.innerHTML = html;
	document.body.appendChild(area);
	
	var npoDimmer = document.createElement('DIV');
	npoDimmer.id = 'cc_ModalDimmerx';
	npoDimmer.style.position = 'absolute';
	npoDimmer.style.zIndex = '600';
	npoDimmer.style.top = '0px';
	npoDimmer.style.left = '0px';
	npoDimmer.style.backgroundColor = '#000000';
	npoDimmer.style.filter = 'alpha(opacity = 45)';
	npoDimmer.style.MozOpacity = '0.45';
	npoDimmer.style.opacity = '0.45';
	document.body.appendChild(npoDimmer);
	
	g_OriginalResize = window.onresize;
	g_OriginalScroll = window.onscroll;
	window.onresize = function() { ccResizeOverlay('cc_ModalDimmerx'); ccCenterDiv(object_name); }
	window.onscroll = function() { ccCenterDiv(object_name); }
	ccResizeOverlay('cc_ModalDimmerx');
	ccCenterDiv(object_name);
}

function ccHideWaitWindow()
{
	var object_name = 'cc_processing_window';
	var area = document.getElementById(object_name);
	var modal = document.getElementById('cc_ModalDimmerx');
	if (modal)
	{
		document.body.removeChild(modal);
		window.onresize = g_OriginalResize;
		window.onscroll = g_OriginalScroll;
		g_OriginalResize = null;
		g_OriginalScroll = null;
	}
	if (area)
		document.body.removeChild(area);
}

function ccResizeOverlay(itemid)
{
	var dims = ccGetDocumentSize();
	var size = ccGetViewableSize();
	var width = dims[0];
	var height = dims[1];
	
	if (Number(height) < Number(size[1]))
		height = size[1];
	
	document.getElementById(itemid).style.width = width + 'px';
	document.getElementById(itemid).style.height = height + 'px';
}

function ccCenterDiv(itemid)
{
	var divwindow = document.getElementById(itemid);
	var size = ccGetViewableSize();
	var origin = ccGetViewportOrigin();
	divwindow.style.height = 'auto';
	var x = divwindow.offsetHeight;
	var height = parseInt(Number(origin[1]) + ((Number(size[1]) - parseInt(x)) / 2));
	//alert(x);
	
	if (height < 0) height = 20;
	//alert(height);
	divwindow.style.top = height + 'px';
	divwindow.style.left = (Number(origin[0]) + ((Number(size[0]) / 2)) - (parseInt(divwindow.style.width) / 2)) + 'px';
}

function ccGetViewportOrigin()
{
	var x = 0;
	var y = 0;
	
	if (window.pageYOffset)
	{
		y = window.pageYOffset;
		x = window.pageXOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
	{
		y = document.documentElement.scrollTop;
		x = document.documentElement.scrollLeft;
	}
	else if (document.body)
	{
		y = document.body.scrollTop;
		x = document.body.scrollLeft;
	}
	
	return [x, y];
}

function ccGetViewableSize()
{
	var nWidth;
	var nheight;
	
	if (window.innerWidth)
	{
		nWidth = window.innerWidth;
		nHeight = window.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientWidth)
	{
		nWidth = document.documentElement.clientWidth;
		nHeight = document.documentElement.clientHeight;
	}
	else if (document.body)
	{
		nWidth = document.body.clientWidth;
		nHeight = document.body.clientHeight;
	}
	
	return [nWidth, nHeight];
}

function ccGetDocumentSize()
{
	var xScroll;
	var yScroll;
	
	if (window.innerHeight && window.scrollMaxY)	// Firefox
	{
		yScroll = window.innerHeight + window.scrollMaxY;
		xScroll = window.innerWidth + window.scrollMaxX;
	}
	else if (document.body.scrollHeight > document.body.offsetHeight)	// all but Explorer Mac
	{
		yScroll = document.body.scrollHeight;
		xScroll = document.body.scrollWidth;
	}
	else	// works in Explorer 6 Strict, Mozilla (not FF) and Safari
	{
		yScroll = document.body.offsetHeight;
		xScroll = document.body.offsetWidth;
	}
	
	return [xScroll, yScroll];
}

function ccGetMouseCoords(e)
{
	var posx = 0;
	var posy = 0;
	
	if (!e) var e = window.event;
	
	if (e.pageX || e.pageY)
	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
	
	return [posx, posy];
}

function ccInRect(object, e)
{
	
	var e = e ? e : window.event;
	
	if (object && e)
	{
		var pos = ccFindObject(object);
		var x = Number(pos[0]);
		var y = Number(pos[1]);
		var mx, my;
		var winw, winh;
		
		winw = object.clientWidth;
		winh = object.clientHeight;
		
		if (window.event)
		{
			mx = window.event.clientX - 2;
			my = window.event.clientY - 2;
		}
		else
		{
			mx = e.pageX;
			my = e.pageY;
		}
		
		if (mx >= x && mx <= (x + winw) && my >= y && my <= (y + winh))
			return true;
	}
	
	return false;
}

function ccFindObject(obj)
{
	if (obj)
	{
		var curleft = curtop = 0;
		if (obj.offsetParent)
		{
			curleft = obj.offsetLeft
			curtop = obj.offsetTop
	
			while (obj = obj.offsetParent)
			{
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
			}
		}
		return [curleft,curtop];
	}
}

function ccAreaSize()
{
	var xScroll;
	var yScroll;

	if (document.documentElement) {
		yScroll = document.documentElement.clientHeight;
		xScroll = document.documentElement.clientWidth;
	} else if (window.innerHeight || window.innerWidth) {
		yScroll = window.innerHeight + window.scrollMaxY;
		xScroll = window.innerWidth + window.scrollMaxX;
		var deff = document.documentElement;
		var wff = (deff&&deff.clientWidth) || document.body.clientWidth || window.innerWidth || self.innerWidth;
		var hff = (deff&&deff.clientHeight) || document.body.clientHeight || window.innerHeight || self.innerHeight;
		xScroll -= (window.innerWidth - wff);
		yScroll -= (window.innerHeight - hff);
	} else if (document.body.scrollHeight > document.body.offsetHeight || document.body.scrollWidth > document.body.offsetWidth){ // all but Explorer Mac
		yScroll = document.body.scrollHeight;
		xScroll = document.body.scrollWidth;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		yScroll = document.body.offsetHeight;
		xScroll = document.body.offsetWidth;
  	}
	
	return [xScroll, yScroll];
}

function ccShowPopup(title, pagedata, width, e, usex, usey)
{
	g_SettingPopup = true;
	var prevmenu = document.getElementById('cc_popup_window');
	if (prevmenu)
		document.body.removeChild(prevmenu);
		
	var menu = document.createElement('DIV');
	var mc;
	var x;
	var y;
	if (e)
	{
		mc = ccGetMouseCoords(e);
		x = mc[0] - 47;
		y = mc[1];
	}
	
	if (usex) x = Number(usex) - 47;
	if (usey) y = usey;
	if (width < 300) width = 300;

	menu.id = 'cc_popup_window'
	menu.style.position = 'absolute';
	//menu.style.overflow = '-moz-scrollbars-vertical';
	//menu.style.overflowX = 'hidden';
	//menu.style.overflowY = 'scroll';
	menu.style.width = width + 'px';
	menu.style.left = x + 'px';
	menu.style.top = y + 'px';
	menu.style.zIndex = '999';
	menu.innerHTML = '<div style="text-align:left;"><table cellpadding="0" cellspacing="0" border="0" width="100%"><tr><td width="12"><img src="/images/controls/bubble_cl.png" alt="" title="" border="0"></td><td width="48"><img src="/images/controls/bubble_mid.png" alt="" title="" border="0"></td><td style="background-image:url(\'/images/controls/bubble_bg.png\');">&nbsp;</td><td width="12"><img src="/images/controls/bubble_cr.png" alt="" title="" border="0"></td></tr></table></div><div style="border:1px solid #990000; background-color:white;">' + pagedata + '</div>';
	
	document.body.appendChild(menu);
	if (document.documentElement)
	{
		g_OriginalOnClick = document.documentElement.onclick;
		document.documentElement.onclick = function(e) { ccHidePopup(false, e, x, y); };
	}
	else
	{
		g_OriginalOnClick = document.body.onclick;
		document.body.onclick = function(e) { ccHidePopup(false, e, x, y); }
	}
	
	setTimeout('g_SettingPopup = false', 100);
}
function ccHidePopup(force, e, x, y)
{
	var inrect = false;
	var menu = document.getElementById('cc_popup_window')
	
	if (!force && menu)
	{
		var pos = ccFindObject(menu);
		var mx, my;
		var winw, winh;
		
		winw = menu.clientWidth;
		winh = menu.clientHeight;
		
		if (window.event)
		{
			mx = window.event.clientX - 2;
			my = window.event.clientY - 2;
		}
		else
		{
			mx = e.pageX;
			my = e.pageY;
		}
		
		if (mx >= x && mx <= (x + winw) && my >= y && my <= (y + winh))
			inrect = true;
	}
	
	
	if (!inrect && !g_SettingPopup && menu)
	{
		document.body.removeChild(menu);
		if (document.documentElement)
			document.documentElement.onclick = g_OriginalOnClick;
		else
			document.body.onclick = g_OriginalOnClick;
	}
}


