function BrowserUtils() {
}

/** check if the browser is gecko. */
BrowserUtils.isGecko = function() {
	if(navigator) {
		if (navigator.userAgent) {
			if(navigator.userAgent.indexOf("Gecko/") != -1) {
				return true;
			} else {
				return false;
			}
		}
	}
}

/** check if the browser is ie. */
BrowserUtils.isIE = function() {
	if(navigator) {
		if (navigator.userAgent) {
			if(navigator.userAgent.indexOf("MSIE") != -1) {
				return true;
			} else {
				return false;
			}
		}
	}
}

BrowserUtils.getBrowserLanguage = function() {
	if (BrowserUtils.isIE()) {
		return window.navigator.browserLanguage.substring(0, 2);
	} else {
		return window.navigator.language.substring(0, 2);
	}
}

BrowserUtils.getBrowserLocale = function() {
	if (BrowserUtils.isIE()) {
		return window.navigator.browserLanguage.substring(3, 5);
	} else {
		return window.navigator.language.substring(3, 5);
	}
}

function MessageBox() {
	this.CONFIRM = 1;
	this.ALERT = 2;
}

/** show a message dialog. */
MessageBox.show = function(type, message) {
	if (type == MessageBox.CONFIRM) {
		return confirm(message);
	} else if (type == MessageBox.ALERT) {
		return alert(message);
	} else {
		return alert("Unknown messagebox type <" + type + ">");
	}
}

function TextUtils() {
}

/** convert multi-byte character to single-byte character. */
TextUtils.convertZenToHan = function(value)
{
	var han = "0123456789.,-+@!\"#$%&'()=<>*/?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 
	var zen = "０１２３４５６７８９．，－＋＠！”＃＄％＆’（）＝＜＞＊／？ＡＢＣＤＥＦＧＨＩＪＫＬＭＮＯＰＱＲＳＴＵＶＷＸＹＺａｂｃｄｅｆｇｈｉｊｋｌｍｎｏｐｑｒｓｔｕｖｗｘｙｚ";
	var str = "";
	
	for (i=0; i < value.length; i++)
	{ 
		c = value.charAt(i);
		n = zen.indexOf(c, 0);
		hn = han.indexOf(c, 0);
		
		if (n >= 0) 
		{
			c = han.charAt(n);
			str += c; 
		}
		else if (hn >= 0)
		{
			str += c;
		}
	} 
	
	return str;
}

TextUtils.keepNumericCharacters = function(value)
{
	var numerics = "0123456789.-";
	var str = "";
	
	var converted = TextUtils.convertZenToHan(value);
	
	for (i = 0; i < converted.length; i++)
	{ 
		c = converted.charAt(i);
		n = numerics.indexOf(c, 0);
		
		if (n >= 0) 
		{
			str += c; 
		}
	} 
	return str;
}

TextUtils.formatNumber = function(x) {
	var separator = "${numberSeparator}";
	
    var s = "" + x;
    var p = s.indexOf(".");
    if (p < 0) {
        p = s.length;
    }
    
    var r = s.substring(p, s.length);
    
    for (var i = 0; i < p; i++) {
        var c = s.substring(p - 1 - i, p - 1 - i + 1);
        if (c < "0" || c > "9") {
            r = s.substring(0, p - i) + r;
            break;
        }
        if (i > 0 && i % 3 == 0) {
            r = separator + r;
        }
        r = c + r;
    }
    return r;
}

/** object represents cookie. */
function Cookie() {
	/** cookie name */
 	var name = "";
 	/** cookie value */
 	var value = "";
 	/** cookie path */
 	var path = "";
 	/** domain */
 	var domain = "";
 	/** expire date */
 	var expiredate = null;
 	/** secure */
 	var secure = false;
 	
 	/** serialize this object to string. */
 	this.serialize = function() {
	 	var serialized = this.name + "=" + escape(this.value);
	 	
	 	if (this.path != null) {
	 		serialized += "; path=" + this.path;
	 	}
	 	if (this.domain != null) {
	 		serialized += "; domain=" + this.domain;
	 	}
	 	if (this.expiredate != null) {
	 		serialized += "; expires=" + this.expiredate.toGMTString();
	 	}
	 	if (this.secure == true) {
	 		serialized += "; secure";
	 	}
	 	return serialized;
 	}
}
 
/** object to manage cookies. */
function CookieUtils() {
}

/** get all cookies. */
CookieUtils.getCookies = function() {
	var cookieList = document.cookie.split("; ");
	var cookies = new Array();
	
	for (i = 0; i < cookieList.length; i++) {
		var index = cookieList[i].indexOf("=");
		var name = cookieList[i].substring(0, index);
		var value = unescape(cookieList[i].substring(index + 1, cookieList[i].length));
		var cookie = new Cookie();
		cookie.name = name;
		cookie.value = value;
		cookies[cookie.name] = cookie;
	}
	
	return cookies;
}
 	
/** get a cookie by name. */
CookieUtils.getCookie = function(name) {
	var cookies = CookieUtils.getCookies();
	
	for (var cookieName in cookies) {
		if (cookies[cookieName].name == name) {
			return cookies[cookieName];
		}
	}
	
	return null;
}
 	
/** check if the cookie exists. */
CookieUtils.exists = function(name) {
	return CookieUtils.getCookie(name) != null;
}
 	
/** set cookie. */
CookieUtils.setCookie = function(cookie) {
	CookieUtils.removeCookie(cookie);
	document.cookie = cookie.serialize();
}

/** remove cookie. */
CookieUtils.removeCookie = function(name, path, domain) {
	var cookie = CookieUtils.getCookie(name);
	
	if (cookie != null) {
		var expiredate = new Date();
 		expiredate.setYear(expiredate.getFullYear() - 1);
 		cookie.expiredate = expiredate;	
 		cookie.path = path;
 		cookie.domain = domain;
 		this.setCookie(cookie);
	}
}

function AjaxUpdater() {
}

/** update a block asynchronously. */
AjaxUpdater.updateAsync = function(target, url) {
	new Ajax.Updater(target, url, {asynchronous:true});
}

/** update a block synchronously. */
AjaxUpdater.updateSync = function(target, url) {
	new Ajax.Updater(target, url, {asynchronous:false});
}