function $C(tag)
{
	return document.createElement(tag);
}

Object.extend
(
	Event,
	{
		wheel: function(event)
		{
			var delta = 0;
			if (!event) event = window.event;
			if (event.preventDefault)
			{
				event.preventDefault();
				event.stopPropagation();
			}
			else
			{
				event.cancelBubble = true;
				event.returnValue = false;
			}
			if (event.wheelDelta)
			{
				delta = event.wheelDelta/120;
				if (window.opera) delta = -delta;
			}
			else if (event.detail)
			{
				delta = -event.detail/3;
			}
			return Math.round(delta); //Safari Round
		}
	}
);

Object.extend(
	Element,
	{
		getWidth: function(element) {
		   	element = $(element);
		   	return element.offsetWidth;
		},
		setWidth: function(element,w) {
		   	element = $(element);
	    	element.style.width = w +"px";
		},
		setHeight: function(element,h) {
	   		element = $(element);
	    	element.style.height = h +"px";
		},
		setLeft: function(element,t) {
		   	element = $(element);
	    	element.style.left = t +"px";
		},
		setTop: function(element,t) {
		   	element = $(element);
	    	element.style.top = t +"px";
		},
		setSrc: function(element,src) {
	    	element = $(element);
	    	element.src = src;
		},
		setHref: function(element,href) {
	    	element = $(element);
	    	element.href = href;
		},
		setInnerHTML: function(element,content) {
			element = $(element);
			element.innerHTML = content;
		}
	}
);

String.prototype.Trim = function() {
	return this.replace(/^\s+|\s+$/g, '');
	//return this.replace(/^\s+|\s+$/, '');
};

// Removes starting whitespaces
String.prototype.LTrim = function() {
	return this.replace(/\s*((\S+\s*)*)/, "$1");
	//return this.replace(/^s*/, "");
};

// Removes ending whitespaces
String.prototype.RTrim = function() {
	return this.replace(/((\s*\S+)*)\s*/, "$1");
	///s*$/
};

String.prototype.stripSlash = function() {
	var str = this;
	//str = str.replace(/\\/g, '\\\\');
  //str = str.replace(/"/g, '\\"');
  //str = str.replace(/'/g, '\\\'');
  str = str.replace(/&lt;|%3C/g, '<');
  str = str.replace(/&gt;|%3E/g, '>');
  str = str.replace(/%20/g, ' ');
  str = str.replace(/%22/g, '\"');
  str = str.replace(/%27/g, '\'');
  return str;
};

Function.prototype.delay = function() {
	var __method = this, args = $A(arguments), ms = args.shift(), obj = args.shift();
	var fnc = function () {
		__method.apply(obj || __method, args);
	};
	this.timer = setTimeout(fnc, ms);
};

Function.prototype.periodical = function() {
	var __method = this, args = $A(arguments), ms = args.shift(), obj = args.shift();
	var fnc = function () {
		__method.apply(obj || __method, args);
	};
	this.timer = setInterval(fnc, ms);
};

Function.prototype.stop = function() {
	if (!this.timer) return;
	clearTimeout(this.timer);
	clearInterval(this.timer);
	this.timer = null;
};

/*
 * end of extension
 */

$notice_login_link = "/notice_login";

/*function Main()
{
	EvtViet();
}*/

function relogin(url)
{
	var redirect = url || window.location.pathname + window.location.search;
	window.location = $notice_login_link + '?url=' + redirect;
}

function ChangePageTitle(title) {
	document.title = title;
}

// GetURLArguments
function $UA() {
	//used: x=$UA('x'); alert(x);
	var idx = location.href.indexOf('?');
	var params = new Array();
	if (idx != -1)
	{
		var pairs = location.href.substring(idx+1, location.href.length).split('&');
		for (var i=0; i<pairs.length; i++)
		{
			nameVal = pairs[i].split('=');
			for (var j=0; j<arguments.length; j++)
			{
				if (nameVal[0] == arguments[j])
				{
					params[j] = nameVal[1];
					params[nameVal[0]] = nameVal[1];
					break;
				}
			}
		}
	}
	if (typeof(params.reduce()) == "undefined")
	{
		return params;
	}
	else
	{
		return params.reduce();
	}
}

// Get extended URLArguments
function $XUA() {
	//used: x=$XUA('x'); alert(x);
	var idx = location.href.indexOf('#');
	var params = new Array();
	if (idx != -1)
	{
		var pairs = location.href.substring(idx+1, location.href.length).split('&');
		for (var i=0; i<pairs.length; i++)
		{
			nameVal = pairs[i].split('=');
			for (var j=0; j<arguments.length; j++)
			{
				if (nameVal[0] == arguments[j])
				{
					params[j] = nameVal[1];
					params[nameVal[0]] = nameVal[1];
					break;
				}
			}
		}
	}
	if (typeof(params.reduce()) == "undefined")
	{
		return params;
	}
	else
	{
		return params.reduce();
	}
}

// Get base url
var url = document.location.href;
var xend = url.lastIndexOf("/") + 1;
var base_url = url.substring(0, xend);
var root_url = window.location.protocol + "//" + window.location.host;

function xUrlChange(xUrl) {
	var url = document.location.href;
	var idx = url.indexOf('#');
	if (idx == -1)
		idx = url.length;
	if (xUrl)
		location.href =	url.substring(0, idx) + '#' + xUrl;
	else
		location.href =	url.substring(0, idx) + '#';
}


function getHTML(url, pars, el, callback, notShowLoading) {
	// Get base url
	var root_url = window.location.protocol + "//" + window.location.host;
	
	notShowLoading	 = (typeof(notShowLoading) == 'undefined' || notShowLoading == '') ? false : notShowLoading;
	var complete = function()
	{
		if (callback && typeof(callback)=="function")
			callback();
		Div2Box(el);
	};


	if (url.substring(0, 1) == '/') {
		url = root_url + url;
	}
	// Does URL begin with http?
	else if (url.substring(0, 4) != 'http') {
		url = root_url +'/'+ url;
	}

	var reportError = function(request) {
		try {
			$(el).innerHTML = "Sorry. There was an error.";
		}
		catch (e) {
			//alert(request.status+ " | " + request.statusText);
			alert("Sorry. There was an error.");
		}
	};

	if ($(el))
	{
		if(!notShowLoading)
		{
			var h=parseInt($(el).offsetHeight);
			h=(h==0)?'auto':(h+"px");
			$(el).innerHTML = '<div class="ajaxloading" style="height:'+h+'"><span>Loading ...</span></div>';
		}

		var myAjax = new Ajax.Updater(
			{success: el},
			url,
			{
				//method: 'get',
				method: 'post',
				//parameters: pars,
				postBody: pars,
				evalScripts: true,
				//asynchronous: false,
				onFailure: reportError,
				onComplete: complete
			}
		);
/**/
	}
	else
	{
		var myAjax = new Ajax.Request(
			url,
			{
				method: 'post',
				parameters: pars,
				evalScripts: true,
				//asynchronous: false,
				onComplete: complete,
				onFailure:reportError
			}
		);
	}
}

function convertStringToBoolean(inputString)
{
	if (typeof(inputString)=="string")
		inputString=inputString.toLowerCase();

	switch(inputString)
	{
		case "1":
		case "true":
		case "yes":
		case "y":
		case 1:
		case true:
		return true;
		break;
		default: return false;
	}
}

function getTimeRange(TimeRange)
{
	var now = new Date();
	var __StarDayOfWeek = 1;
	var y, m, d, fromDate, toDate;
	switch (TimeRange)
	{
		case "ToDay":
			d = now.getDate();
			m = now.getMonth();
			y = now.getFullYear();
			fromDate = new Date(y, m, d);
			toDate = new Date(y, m, d+1);
			break;
		case "ThisWeek":
			d = now.getDate() - now.getDay() + __StarDayOfWeek;
			m = now.getMonth();
			y = now.getFullYear();
			fromDate = new Date(y, m, d);
			toDate = new Date(y, m, d+6);
			break;
		case "ThisMonth":
			m = now.getMonth();
			y = now.getFullYear();
			fromDate = new Date(y, m);
			toDate = new Date(y, m+1);
			break;
		case "ThisYear":
			y = now.getFullYear();
			fromDate = new Date(y, 0, 1);
			toDate = new Date(y+1, 0, 0);
			break;
	}
	//alert([fromDate, fromDate.valueOf(), toDate, toDate.valueOf()]);
	fromDate = fromDate.valueOf()/1000;
	toDate = toDate.valueOf()/1000;
	return ([fromDate, toDate]);
}

function change_language(strlang) {
	var frm=$("frmLanguage");
	frm.language.value=strlang;
	frm.submit();
}


/**
*
* Base64 encode / decode
* http://www.webtoolkit.info/
*
**/

var Base64 = {

    // private property
    _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

    // public method for encoding
    encode : function (input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        input = Base64._utf8_encode(input);

        while (i < input.length) {

            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output = output +
            this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
            this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

        }

        return output;
    },

    // public method for decoding
    decode : function (input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;

        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

        while (i < input.length) {

            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));

            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;

            output = output + String.fromCharCode(chr1);

            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }

        }

        output = Base64._utf8_decode(output);

        return output;

    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}