/* Copyright(c) 2003-2007 Wang, Chun-Pin All rights reserved.
 *
 * Version:	$Id: misc.js,v 1.5 2008/08/14 05:13:16 alex Exp $
 *
 */
var Firefox = (document.getElementById && !document.all);
var MSIE = (-1 != navigator.userAgent.indexOf('MSIE'));

function getTrueBody() {
	return(document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}

String.prototype.trim=function(){
	return this.replace(/^\s+|\s+$/g,"");
};

function Redirect(url)
{
	parent.location=url;
}

var Submitted = 0;
function OnSubmit(form)
{
	if (Submitted) {
		return false;
	}
	Submitted = 1;
	return true;
}

function DisableMouseSelection(element)
{
	if (element.onselectstart !== undefined){
		element.onselectstart = function() { return false; };
	}
	else if (element.style.MozUserSelect !== undefined){
		element.style.MozUserSelect = "none";
	}
	else if (element.style.KhtmlUserSelect !== undefined){
		element.style.KhtmlUserSelect = "none";
	}
	if (element.ondrag !== undefined){
		element.ondrag = function() { return false; };
	}
}

function SetMoveAble(trigger_div, move_div)
{
	var DiffX, DiffY;

	DisableMouseSelection(trigger_div);

	trigger_div.onmousedown = function(e) {
		
		e||(e=window.event);

		
		DiffX =  e.clientX - parseInt(move_div.style.left);
		DiffY =  e.clientY - parseInt(move_div.style.top);

		document.onmousemove = function(e) {
			e||(e=window.event);

			var CurX = e.clientX;
			var CurY = e.clientY;

			var left = ((CurX - DiffX) >= 0) ? (CurX - DiffX): 0;
			var top = ((CurY - DiffY) >= 0) ? (CurY - DiffY): 0;


			move_div.style.left = left + 'px';
			move_div.style.top = top + 'px';
			return;
		};
		document.onmouseup = function(e) {
			document.onmousemove = null;
		};
	};
}
/*********************************************************************
 *                         Start of mask document                    *
 *********************************************************************/

function getViewportWidth()
{
	var width = self.innerWidth;
	var mode = document.compatMode;

	if (mode || MSIE) {
		width = getTrueBody().clientWidth;
	}
	return parseInt(width);
}

function getViewportHeight()
{
	var height = self.innerHeight;
	var mode = document.compatMode;

	if (mode || MSIE) {
		height = getTrueBody().clientHeight;
	}

	return parseInt(height);
}

function GetDocumentWidth()
{
	var scrollWidth = parseInt(getTrueBody().scrollWidth);
	return Math.max(scrollWidth, getViewportWidth());
}

function GetDocumentHeight()
{
	var scrollHeight = parseInt(getTrueBody().scrollHeight);
	return Math.max(scrollHeight, getViewportHeight());
}

function DocumentMaskReSize()
{
	var div = document.getElementById('document_mask');
	
	if (div) {
		div.style.width = GetDocumentWidth() + 'px';
		div.style.height = GetDocumentHeight() + 'px';
		if (MSIE) {
			var iframe = document.getElementById('document_mask_iframe');
			iframe.style.width = div.style.width;
			iframe.style.height = div.style.height;
		}
	}
}

function DocumentMask(loading, message)
{
	var div;
	var hintArea = false;

	if (loading || (typeof message == 'string')) {
		hintArea = true;
	}

	div = document.getElementById('document_mask');
	if (!div) {
		div = document.createElement('DIV');
		div.id = 'document_mask';
		document.body.appendChild(div);

		div.maskMsg = document.createElement('DIV');
		document.body.appendChild(div.maskMsg);

		if (typeof message == 'string') {
			if (loading) {
				div.maskMsg.innerHTML = '<img border="0" src="images/loading.gif" width="16" height="16" align="texttop"> '+message;
			} else {
				div.maskMsg.innerHTML = message;
			}
		} else if (loading) {
			div.maskMsg.innerHTML = '<img border="0" src="images/loading.gif" width="16" height="16" align="middle">';
		}
		

		/* This iframe is to fix the IE6 problem. In IE6, the <select> will
		 * always on top if we don't use iframe to overwrite it.
		 */
		if (MSIE) {
			iframe = document.createElement('IFRAME');
			iframe.id = 'document_mask_iframe';
			document.body.appendChild(iframe);
		}
	}
	div.style.top = '0px';
	div.style.left = '0px';
	div.style.zIndex = 10000;
	div.style.width = GetDocumentWidth() + 'px';
	div.style.height = GetDocumentHeight() + 'px';
	div.style.display = 'block';
	div.style.position = 'absolute';
	div.style.filter = "alpha(opacity:50)";
	div.style.KHTMLOpacity = 0.5;
	div.style.MozOpacity = 0.5;
	div.style.opacity = 0.5;
	div.style.background = '#ccc';

	if (hintArea) {
		div.maskMsg.style.border = 'double #c3daf9';
		div.maskMsg.style.backgroundColor = '#ffffff';
		div.maskMsg.style.position = 'absolute';
		div.maskMsg.style.display = 'block';
		div.maskMsg.style.fontSize = '12px';
		div.maskMsg.style.fontFamily = 'Arial, Helvetica, sans-serif';
		div.maskMsg.style.color = '#333333';
		div.maskMsg.style.padding = '5px 10px';
		div.maskMsg.style.top = getTrueBody().scrollTop + (getViewportHeight()/2) - (parseInt(div.maskMsg.clientHeight) / 2) + 'px';
		div.maskMsg.style.left = (getViewportWidth()/2) - (parseInt(div.maskMsg.clientWidth)/2) +  getTrueBody().scrollLeft + 'px'; 
		div.maskMsg.style.zIndex = div.style.zIndex + 1;
	} else {
		div.maskMsg.style.display = 'none';
	}

	if (MSIE) {
		iframe.style.top = '0px';
		iframe.style.left = '0px';
		iframe.style.zIndex = div.style.zIndex - 1;
		iframe.style.display = 'block';
		iframe.style.position = 'absolute';
		iframe.style.border = 0;
		iframe.style.width = div.style.width;
		iframe.style.height = div.style.height;
		iframe.style.filter = "alpha(opacity:0)";
		iframe.style.KHTMLOpacity = 0;
		iframe.style.MozOpacity = 0;
		iframe.style.opacity = 0;
	}

	window.onresize = DocumentMaskReSize;
}

function DocumentUnMask()
{
	var div = document.getElementById('document_mask');
	
	if (!div) {
		return;
	}

	if (MSIE) {
		var iframe = document.getElementById('document_mask_iframe');
		iframe.style.display = 'none';
	}

	div.maskMsg.style.display = 'none';
	div.style.display = 'none';

	window.onresize = null;
}

/*********************************************************************
 *                         End of mask document                      *
 *********************************************************************/

/* options:
 *		option.title
 *		option.msg
 *		option.buttons[], ok, yes, no, save, cancel, submit
 *		option.fn, function(button), button is string:ok|yes|no|cancel
 *		option.width
 *
 * Example:
 *		Dialog.Show({
 *			title: 'this is title',
 *			msg: 'this is message',
 *			width: 400,
 *			buttons: ['yes', 'no'],
 *			fn: alextest
 *		});
 */
Dialog = function()
{
	var opt, div, dialog;
	
	return {
		Show: function(options) {
			var div_close, div_header;
			var buttons = '';

			DocumentMask();

			dialog = this;
			opt = options;
			
			for (var i = 0; i < options.buttons.length; i++) {
				if (buttons !== '') {
					buttons = buttons + '&nbsp;&nbsp;';
				}
				buttons = buttons + '<button id="aw-dlg-button-"' + options.buttons[i] + 
									'" class="aw-dlg-button" onclick="Dialog.ButtonHandler(\''+options.buttons[i]+'\');">' + 
									STRING['button_'+options.buttons[i]]+'</button>';
			}

			if (!div) {
				div = document.createElement('DIV');
				div.id = 'message_box';
				document.body.appendChild(div);
			}
			div.className = 'aw-dlg';
			div.innerHTML = '<div class="aw-dlg-hd-left"><div class="aw-dlg-hd-right"><div id="message_box_hd" class="aw-dlg-hd">'+options.title+'</div></div></div>' + 
				'<div class="aw-dlg-dlg-body">'+options.msg+'</div>'+
				'<div class="aw-dlg-bg-left"><div class="aw-dlg-bg-right"><div class="aw-dlg-bg-center"><p align="center">'+buttons+'</p></div></div></div>'+
				'<div id="aw-dlg-close" class="aw-dlg-close"></div>';

			
			if (!options.width || options.width < 200) { options.width = 200; }
			div.style.width = options.width + 'px';

			/* Put the dialog in the top 1/4 of screen */
			div.style.top = getTrueBody().scrollTop + (getViewportHeight()/4) + 'px';
			div.style.left = (getViewportWidth()/2) - (parseInt(div.style.width)/2) + getTrueBody().scrollLeft + 'px';
			div.style.position = 'absolute';
			div.style.display = 'block';
			div.style.zIndex = 2000001;
			
			div_header = document.getElementById('message_box_hd');
			SetMoveAble(div_header, div);

			div_close = document.getElementById('aw-dlg-close');
			div_close.onmouseover = function() {
				this.className = 'aw-dlg-close aw-dlg-close-over';
			};
			div_close.onmouseout = function() {
				this.className = 'aw-dlg-close';
			};
			div_close.onclick = function() {
				dialog.Cancel();
			};

		},
		Cancel:function() {
			 DocumentUnMask();
			 div.style.display = 'none';
		},
		ButtonHandler: function(button) {
			 this.Cancel();
			 if (opt.fn) {
				 opt.fn(button);
			 }
		}
	};
}();

/* options:
 *		option.title
 *		option.msg
 *		option.buttons[], ok, yes, no, save, cancel, submit
 *		option.fn, function(button), button is string:ok|yes|no|cancel
 *		option.width
 *
 * Example:
 *		Dialog.Show({
 *			title: 'this is title',
 *			msg: 'this is message',
 *			width: 400,
 *		});
 */
Tooltip = function()
{
	var opt, div, tooltip, iframe;
	
	return {
		Show: function(options) {
			var div_close, div_header;

			tooltip = this;
			opt = options;
			
			if (!div) {
				div = document.createElement('DIV');
				div.id = 'aw-tooltip';
				document.body.appendChild(div);

				/* This iframe is to fix the IE6 problem. In IE6, the <select> will
				 * always on top if we don't use iframe to overwrite it.
				 */
				if (MSIE) {
					iframe = document.createElement('IFRAME');
					document.body.appendChild(iframe);
				}
			}
			div.style.display = 'none';

			div.className = 'aw-dlg';
			div.innerHTML = '<div class="aw-dlg-hd-left"><div class="aw-dlg-hd-right"><div class="aw-dlg-hd">'+options.title+'</div></div></div>' + 
				'<div class="aw-dlg-dlg-body"><div style="width:100%;overflow:hidden;">'+options.msg+'</div></div>'+
				'<div class="aw-dlg-bg-left"><div class="aw-dlg-bg-right"><div class="aw-dlg-bg-center"><p align="center"></p></div></div></div>';
			
			if (!options.width || options.width < 200) { options.width = 200; }
			div.style.width = options.width + 'px';
			div.style.position = 'absolute';
			div.style.zIndex = 2000002;

			if (MSIE) {
				iframe.style.zIndex = div.style.zIndex - 1;
				iframe.style.position = 'absolute';
				iframe.style.border = 0;

				// IE/Windows
				iframe.style.filter = "alpha(opacity: 40)";
				// Safari < 1.2, Konqueror
				iframe.style.KHTMLOpacity = 0.4;
				// Older Mozilla and Firefox
				iframe.style.MozOpacity = 0.4;
				// Safari 1.2, newer Firefox and Mozilla, CSS3
				iframe.style.opacity = 0.4;
			}

			document.onmousemove = function(e) {
				e||(e=window.event);

				var curX = MSIE?(e.clientX+getTrueBody().scrollLeft) : e.pageX;
				var curY = MSIE?(e.clientY+getTrueBody().scrollTop) : e.pageY;

				//Find out how close the mouse is to the corner of the window
				var winwidth = getViewportWidth();
				var winheight = getViewportHeight();
				var rightedge = MSIE? winwidth-e.clientX-12 : winwidth-e.clientX-12;
				var bottomedge = MSIE? winheight-e.clientY-10 : winheight-e.clientY-10;
				var leftedge = -1000;

				//if the horizontal distance isn't enough to accomodate the width of the context menu
				if (rightedge < div.offsetWidth) {
					//move the horizontal position of the menu to the left by it's width
					div.style.left = curX-div.offsetWidth+"px";
				} else if (curX < leftedge) {
					div.style.left = "5px";
				} else {
					//position the horizontal position of the menu where the mouse is positioned
					div.style.left = curX+12+"px";
				}
				//same concept with the vertical position
				if (bottomedge < div.offsetHeight) {
					div.style.top = curY-div.offsetHeight-10+"px";
				} else {
					div.style.top = curY+10+"px";
				}

				div.style.display = 'block';
				if (MSIE) {
					iframe.style.top = div.style.top;
					iframe.style.left = div.style.left;
					iframe.style.width = div.offsetWidth;
					iframe.style.height = div.offsetHeight;
					iframe.style.display = 'block';
				}
			};

		},
		Hide: function() {
			document.onmousemove = null;
			div.style.display = 'none';
			if (MSIE) {
				iframe.style.display = 'none';
			}
		}
	};
}();
