String.prototype.noSpace = function() {
	return this.replace(/\s/g, "");
}

String.prototype.filter = function() {
	return this.replace(/[^-a-zA-Z0-9_:./\s]/g, "");
}

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

String.prototype.toDate = function() {
	var date = this.replace(/^[^0-9]+|[^0-9]+$/g, "").replace(/[^0-9]+/g, "*").split("*");
	for (var i = 0; i < date.length; ++i) date[i] = parseInt(date[i], 10);
	if (date[0] > 1e9) {
		var res = new Date(date[0] * 1000);
		if (isNaN(res)) date = new Array(0, 0, 0, 0, 0); else return res;
	}
	var now = new Date();
	if ((!date[0]) || (date[0] > 31)) date[0] = now.getDate();
	if ((!date[1]) || (date[1] > 12)) date[1] = now.getMonth() + 1;
	if ((!date[2]) || (date[2] > 2100)) date[2] = now.getFullYear(); else if (date[2] < 100) date[2] += 2000;
	if ((!date[3]) || (date[3] > 23)) date[3] = 0;
	if ((!date[4]) || (date[4] > 59)) date[4] = 0;
	return new Date(date[2], date[1] - 1, date[0], date[3], date[4], 0);
}

String.prototype.toLocation = function() {
	var str = this.trim();
	if (!str.length) return str; 
	if (str.length <= 2) return str.toUpperCase();
	str = str.charAt(0).toUpperCase() + str.substring(1);
	return str;
}

String.prototype.toDuration = function() {
	time = parseInt(this);
	if (time <= 0) return "progress expired";
	if (time < 60) return "< 1 minute left";
	time = Math.floor(time / 60);
	if (time < 60) return "~ " + time + " minute" + ((time > 1) ? "s":"") + " left";
	time = Math.floor(time / 60);
	if (time < 24) return "~ " + time + " hour" + ((time > 1) ? "s":"") + " left";
	time = Math.floor(time / 24);
	if (time < 7) return "~ " + time + " day" + ((time > 1) ? "s":"") + " left";
	time = Math.floor(time / 7);
	return "~ " + time + " week" + ((time > 1) ? "s":"") + " left";
}


Date.prototype.toStr = function() {
	days = Array("Su", "Mo", "Tu", "We", "Th", "Fr", "Sa");
	d = days[this.getDay()];
	D = this.getDate();
	if (D < 10) D = "0" + D;
	M = this.getMonth() + 1;
	if (M < 10) M = "0" + M;
	Y = this.getFullYear();
	h = this.getHours();
	if (h < 10) h = "0" + h;
	m = this.getMinutes();
	if (m < 10) m = "0" + m;
	return d + " " + D + "." + M + "." + Y + " " + h + ":" + m;
}

Date.prototype.toMiniStr = function() {
	D = this.getDate();
	if (D < 10) D = "0" + D;
	M = this.getMonth() + 1;
	if (M < 10) M = "0" + M;
	Y = this.getFullYear() % 100;
	if (Y < 10) Y = "0" + Y;
	return D + "." + M + "." + Y;
}

Date.prototype.toMaxiStr = function() {
	D = this.getDate();
	if (D < 10) D = "0" + D;
	M = this.getMonth() + 1;
	if (M < 10) M = "0" + M;
	Y = this.getFullYear();
	return D + "." + M + "." + Y;
}

Date.prototype.toFullView = function() {
	D = this;
	if (D < 10) D = "0" + D;
	M = this.getMonth() + 1;
	if (M < 10) M = "0" + M;
	Y = this.getFullYear();
	return D;
}

Date.prototype.nextWeek = function(/*time*/) {
	/*var now = new Date();
  if (day = now.getDay()) now.setDate(now.getDate() + 7 - day + (time / 86400));
  time %= 86400;
  now.setHours(time / 3600);
  time %= 3600;
  now.setMinutes(time / 60);
  now.setSeconds(time % 60);
  return now;*/
	var now = new Date();
	if (day = now.getDay()) now.setDate(now.getDate() + 7);
	return now;
}

Date.prototype.today = function() {
	var now = new Date();
	return now;
}

Date.prototype.toTime = function() {
	return Math.floor(this / 1000);
}

var mmFullToMonth = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
var mmShortToMonth = new Array("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12");

function showLocalDate(timestamp)
{
	var dt  = new Date(timestamp * 1000);
	var mm  = mmShortToMonth[dt.getMonth()];
	var dd  = (dt.getDate() < 10) ? "0" + dt.getDate() : dt.getDate();
	var hh  = (dt.getHours() < 10) ? "0" + dt.getHours() : dt.getHours();
	var min = (dt.getMinutes() < 10) ? "0" + dt.getMinutes() : dt.getMinutes();
	var sec = (dt.getSeconds() < 10) ? "0" + dt.getSeconds() : dt.getSeconds();
	return dd + "." + mm + "." + dt.getFullYear() + " " + hh + ":" + min + ":" + sec;
} 

Array.prototype.inArray = function(val) {
	for (var i = 0; i < this.length; ++i) if (this[i] == val) return true;
	return false;
}

function beep(times) {
	java.awt.Toolkit.getDefaultToolkit().beep();
	for (var i = 1; i < times; ++i)
		setTimeout(function() {java.awt.Toolkit.getDefaultToolkit().beep();}, 150 * i);
}

function chr(code) {
	return String.fromCharCode(code);
}

function getScrollHeight() {
	var h = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
	return h ? h:0;
}

////TOOLBAR ////

//toolOn
function toolOn(id) {
	document.getElementById("tool" + id).className = "toolbarhover";
}
//toolOff
function toolOff(id) {
	document.getElementById("tool" + id).className = "toolbaritem";
}

////MENU ////

//menuSectionOn
function menuSectionOn(id, cssClass) {
	document.getElementById("menusection_" + id).className = cssClass;
}

//menuSectionOff
function menuSectionOff(id, cssClass) {
	document.getElementById("menusection_" + id).className = cssClass;
}



////HEADER ////

//headerOn
function headerOn(id) {
	document.getElementById("header" + id).className = "headerhover";
	document.getElementById("headertitle" + id).className = "headerhover";
	document.getElementById("headerbutton" + id).className = "headerbuttonshow";
}
//headerOff
function headerOff(id) {
	document.getElementById("header" + id).className = "headeritem";
	document.getElementById("headertitle" + id).className = "headeritem";
	document.getElementById("headerbutton" + id).className = "headerbutton";
}
//headerClick
function headerClick(id) {
	alert(id);
}

//getStyle
function getStyle(obj, style) {
	if (document.all) switch (style) {
	case "top": return obj.offsetTop;
	case "left": return obj.offsetLeft;
	case "width": return obj.offsetWidth;
	case "height": return obj.offsetHeight;
	}
	return parseInt(document.defaultView.getComputedStyle(obj, "").getPropertyValue(style));
}


var mouseObj = null;
var mouseOver = false;

//checkMouse
function checkMouse(obj) {
	if (mouseObj) {
		mouseObj.onmouseout = null;
		mouseObj.onmouseover = null;
	}
	if (mouseObj = obj) { 
		mouseObj.onmouseout = function() {mouseOver = false;};
		mouseObj.onmouseover = function() {mouseOver = true;};
	}
}

var result = null;

//alert
function alert(msg, ok) {
	showPopupWnd(ok, null, "<table class='popup' style='position: relative;z-index: 10000;'><tr><td height='3'></td></tr><tr><td><table><tr><td>&nbsp;<img src='public/images/web/alert.png'>&nbsp;</td><td align='center'>&nbsp;" + msg + "&nbsp;</td></tr></table></td></tr><tr><td height='3'></td></tr><tr><td align=center>&nbsp;<button onclick='result=true;' style='width:80px' id='alertok'>OK</button>&nbsp;</td></tr><tr><td height='8'></td></tr></table>");
	document.getElementById("alertok").focus(); 
}

//confirm
function confirm(msg, yes, no) {
	showPopupWnd(yes, no, "<table class='calender'><tr><td height=3></td></tr><tr><td><table><tr><td>&nbsp;<img src=img/alert.png>&nbsp;</td><td align=center>&nbsp;" + msg + "&nbsp;</td></tr></table></td></tr><tr><td height=3></td></tr><tr><td align=center>&nbsp;<button onclick='result=true;' style='width:80px'>Yes</button>&nbsp;<button onclick='result=false;' style='width:80px' id=confirmno>No</button>&nbsp;</td></tr><tr><td height=8></td></tr></table>");
	document.getElementById("confirmno").focus(); 
}

//prompt
function prompt(msg, value, ok, cancel) {
	showPopupWnd(ok, cancel, "<table class='calender'><tr><td height=8></td></tr><tr align=center><td><table width=100%><tr><td>&nbsp;" + msg + "&nbsp;</td><td>&nbsp;<input type=text id=promptinput value='" + value + "'>&nbsp;</td></tr></table></td></tr><tr><td height=8></td></tr><tr align=center><td>&nbsp;<button onclick='result=document.getElementById(" + '"promptinput"' + ").value;' style='width:80px' id=promptok>OK</button>&nbsp;<button onclick='result=false;' style='width:80px' id=promptcancel>Cancel</button>&nbsp;</td></tr><tr><td height=8></td></tr></table>");
	document.getElementById("promptinput").focus();  
}

//showPopupWnd
function showPopupWnd(yes, no, innerHTML) {
	result = null;  

	var obj = document.getElementById("popupwnd");
	obj.innerHTML = innerHTML; 

	var bg = document.getElementById("black");
	var x = getStyle(bg, "width");
	var y = getStyle(bg, "height");

	x = (x - getStyle(obj, "width")) >> 1;
	y = (y - getStyle(obj, "height")) >> 1;
	obj.style.left = Math.max(x, 10) + "px";
	obj.style.top = Math.min(Math.max(y, 10), 100) + getScrollHeight() + 100 + "px";

	animOpacity(bg, 70);
	obj.style.visibility = "visible";
	wait(obj, yes, no); 
}

//wait
function wait(obj, yes, no) {
	if (result == null) setTimeout(function() {wait(obj, yes, no);}, 50); else {
		obj.style.visibility = "hidden";
		document.getElementById("black").style.visibility = "hidden";
		if (result) {
			if (yes) {
				if (result == true) yes(); else yes(result);
			}
		} else {if (no) no();}
		result = null;
	}
}

//animOpacity
function animOpacity(obj, alpha, current) {
	if (!current) {
		obj.style.visibility = "visible";
		obj.style.top = getScrollHeight();
		setOpacity(obj, current = 0);
	} else {
		if (current > alpha) current = alpha;
		setOpacity(obj, current);
	}
	current += 10;
	if (current <= alpha) setTimeout(function(){animOpacity(obj, alpha, current);}, 50);
}

//setOpacity
function setOpacity(obj, percent) {
	if (window.ActiveXObject) obj.style.filter = "alpha(opacity=" + percent + ")"; else obj.style.MozOpacity = percent / 100;
	obj.style.opacity = percent / 100;
}

function checkDate(chkdate)
{
	if(chkdate.length != 0)
	{	
		if(chkdate.match(/^(0[1-9]|[12][0-9]|3[01])\.(0[1-9]|1[012])\.[0-9]{4}/))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else{
		return true
	}
}


