// By Michael
var Utils = function(){};
Utils.$ = function(obj){ return document.getElementById(obj); };
Utils.$n = function(obj){ return document.getElementsByName(obj); };
Utils.$$ = function(tagName){ return document.getElementsByTagName(tagName); };
Utils.$t = function(obj, tagName){ return obj.getElementsByTagName(tagName); };
Utils.$e = function(eltName){ return document.createElement(eltName); };
Utils.isFF = function(){ return (navigator.userAgent.indexOf('Firefox') !== -1); };
Utils.isIE = function() { return (navigator.userAgent.indexOf('MSIE') !== -1); };
Utils.isIE6 = function() { return (navigator.userAgent.indexOf('MSIE 6.0') !== -1); };

Utils.setCookie = function(name, value)
{
	var days = 30;
	var exp = new Date();
	exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000);
	document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
};
Utils.getCookie = function (name)
{
	var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
	if (arr != null) return unescape(arr[2]);
	return null;
};
Utils.delCookie = function(name)
{
	var exp = new Date();
	exp.setTime(exp.getTime() - 1);
	var cval = getCookie(name);
	if (cval != null) document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
};
Utils.getValue = function(objValue)
{
	if (objValue == null)
	{
		objValue = 0;
	}
	return objValue;
};
Utils.getBoolean = function(objValue)
{
	if (objValue == null || objValue == undefined || objValue == "")
	{
		objValue = true;
	}
	return objValue;
};
Utils.getString = function(objValue)
{
	if (objValue == null)
	{
		objValue = "";
	}
	return objValue;
};


Utils.setTextBox = function(obj, value){ obj.value = value; };

Utils.chgColor = function(obj, on)
{
	if (on)
	{
		obj.style.backgroundColor = '#efffee';
	}
	else
	{
		obj.style.backgroundColor = '';
	}
}
Utils.trim = function(objValue)
{
	var str = new String(objValue);
	return str.replace(/(^\s*)|(\s*$)/gi, '');
};

Utils.redirect = function(url, replace)
{
	if(replace)
	{
		window.location.replace(url);
	}
	else
	{
		window.location.href = url;
	}
};
Utils.openWin = function(url, width, height)
{
	var wWidth = Number(width);
	var wHeight = Number(height);

	var wTop = (window.screen.availHeight - 30 - wHeight) / 2;
	var wLeft = (window.screen.availWidth - 10 - wWidth) / 2;
	var subWin = window.open(url, '', 'width=' + width + ',height=' + wHeight + ',top=' + wTop + ',left=' + wLeft + ',scrollbars=1,resizable=1;');
};

String.prototype.trim = function() { return this.replace(/(^\s*)|(\s*$)/gi, ''); };

var dropDownList = function(obj)
{
	this.list = obj;
};

dropDownList.prototype = 
{
	select:function(value)
	{
		for (var i = 0; i < this.list.options.length; i++)
		{
			if (this.list.options[i].value == value)
			{
				this.list.selectedIndex = i;
				return;
			}
		}
	},
	addItem:function(text, value)
	{
		var opt = Utils.$e('option');
		opt.value = value;
		opt.text = text;
		this.list.options.add(opt);
	},
	removeAll:function()
	{
		var i = this.list.options.length;
		while (i >= 0)
		{
			this.list.remove(i--);
		}
	},
	length:function()
	{
		return this.list.options.length;
	},
	getOptions:function()
	{
		return this.list.options;
	},
	setDisabled:function(val)
	{
		this.list.disabled = val;
	},
	setDisplay:function(value)
	{
		this.list.style.display = value;
	},
	addScript:function(source, id)
	{
		// The script was loaded, no need to load again.
		if (Utils.$(id))
		{
			return false;
		}
		var myHead = Utils.$$('head').item(0);
		var myScript = Utils.$e("script");
		myScript.type = "text/javascript";
		// myScript.defer = true;
		myScript.id = id;
		myScript.text = source;
		myHead.appendChild(myScript);
		return true;
	}
};

var Datatimer = function(obj_name, current_ms, show_date)
{
	this.current_ms = current_ms + 1000;
	this.elem = Utils.$(obj_name);
	this.show_date = show_date || false;
};

Datatimer.prototype.start = function()
{
	if (this.elem)
	{
		this.showTime();
		this.current_ms += 1000;
		var _self = this;
		setTimeout( function()
		{
			_self.start()
		}, 1000);
	}
};

Datatimer.prototype.showTime = function()
{
	if (this.elem != null)
	{
		var d = new Date(this.current_ms);
		var hours = d.getHours();
		var minutes = d.getMinutes();
		var seconds = d.getSeconds();

		if (hours < 10)
			hours = '0' + hours;
		if (minutes < 10)
			minutes = '0' + minutes;
		if (seconds < 10)
			seconds = '0' + seconds;
		var innerstr = '';
		if (this.show_date)
		{
			var year = d.getFullYear();
			var month = d.getMonth() + 1;
			var day = d.getDate();

			if (month < 10)
				month = '0' + month;
			if (day < 10)
				day = '0' + day;
			innerstr += year + '-' + month + '-' + day + ', ';
		}
		innerstr += hours + ':' + minutes + ':' + seconds;
		this.elem.innerHTML = innerstr;
	}
}; // new Datatimer('objName', currentTime, true).start();

// Menu

var menu = function(){};
menu.count = 0;
menu.timeoutsec = 800;
menu.tid = 0;
menu.action = function(menu_no)
{
	var obj = null;
	if (menu_no == -1) {
		menu.tid = setTimeout("menu.action(0);", 1000);
		return;
	}
	else {
		clearTimeout(this.tid);
	}
	this.hideDropDownList(false);
	for (var i = 1; i <= menu.count; i++) {
		obj = Utils.$('menu_top_' + i);
		if (obj) { obj.className = 'menu_sub_hide'; }
		this.highlightParent(obj, 'normal');
	}
	if (menu_no > 0) {
		obj = Utils.$('menu_top_' + menu_no);
		if (obj) { obj.className = 'menu_sub_show'; this.hideDropDownList(Utils.isIE6()); }
		this.highlightParent(obj, 'highlight');
	}
};

menu.highlightParent = function(obj, className)
{
	try
	{
		var parentID = obj.parentNode.childNodes[0].id;
		if(this.getClassName(parentID) != 'on') this.highlightElement(parentID, className);
	}catch(e){}
};

menu.getClassName = function(objName)
{
	return Utils.$(objName).className;
};
menu.highlightElement = function(objName, className)
{
	try
	{
		Utils.$(objName).className = className;
	}catch (e){}
};
menu.highlight = function(str)
{
	this.highlightElement(str, 'on');
};
menu.timeout = function()
{
	this.tid = setTimeout('menu.action(0);', this.timeoutsec);
};
menu.hideDropDownList = function(hide)
{
	var objs = Utils.$$('select');
	var i;
	if (hide)
	{
		for (i = 0; i < objs.length; i++)
		{
			objs[i].className = 'hide';
		}
	}
	else
	{
		for (i = 0; i < objs.length; i++)
		{
			objs[i].className = 'show';
		}
	}
};
