var userId = 0;
var waitTime = false;
var currentCounter = false;
var counterTimer = false;

Object.extend(String.prototype, {
	
	isValidEmail: function()
	{
		var filter = /^[_\w-]+(\.[_\w-]+)*@[\w-]+(\.[\w-]+)+$/;
		return filter.test(this);
	},

	encodeUTF8: function()
	{
		var utftext = "";
		var string = this.replace(/\r\n/g, "\n");
		string.length.times(function(n)
		{
			var c = string.charCodeAt(n);
			if (c < 128) {
				utftext += String.fromCharCode(c);
			} else if(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;
	},

	decodeUTF8: function ()
	{
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
		while (i < this.length) {
			c = this.charCodeAt(i);
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			} else if(c > 191 && c < 224) {
				c2 = this.charCodeAt(i+1);
				string += String.fromCharCode(((c&31)<<6)|(c2&63));
				i += 2;
			} else {
				c2 = this.charCodeAt(i+1);
				c3 = this.charCodeAt(i+2);
				string += String.fromCharCode(((c&15)<<12)|((c2&63)<<6)|(c3&63));
				i += 3;
			}
		}
		return string;
	},
	
	escapeAlert: function()
	{
		var text = this.replace(/\n/, "::n::");
		return text.unescapeHTML().replace(/::n::/, "\n");
	}
});

function winPopup(url, win, features)
{
	var newWindow = window.open(url, win, features);
	newWindow.focus();
	return newWindow;
}

function imgPopup(image, win, title)
{
	var tmp = new Image();
	tmp.title = title;
	tmp.newWin = window.open('', win, "width=100, height=100");
	tmp.onload = function()
	{
		this.newWin.document.open();
		this.newWin.document.write('<html><title>'+this.title+'</title>');
		this.newWin.document.write('<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginheight="0" marginwidth="0" onBlur="self.close()">');
		this.newWin.document.write('<center><img name="myImg" src='+this.src+'></center>');
		this.newWin.document.write('</body></html>');
		this.newWin.document.close();
		this.newWin.moveTo(Math.round((screen.width-this.width)/2), Math.round((screen.height-this.width)/2));
		this.newWin.resizeBy(this.width-this.newWin.document.body.clientWidth, this.height-this.newWin.document.body.clientHeight);
		this.newWin.focus();
	}
	tmp.src = image;
}

function clickCounter(button, counter, step, limit, callback)
{	
	if (!waitTime && $(counter)) {
		currentCounter = $(counter);
		var newval = parseInt($F(currentCounter))+step;
		if (limit === false || (step > 0 && newval <= limit) || (step < 0 && newval >= limit))
			currentCounter.value = newval;
			
		if (callback) button.onmouseup = button.onmouseout = function() { unclickCounter(button, callback); };
		else button.onmouseup = button.onmouseout = unclickCounter;
		timerCounter(step, limit);
	}
}

function timerCounter(step, limit)
{
	if (counterTimer) {
		var newval = parseInt($F(currentCounter))+step;
		if (limit === false || (step > 0 && newval <= limit) || (step < 0 && newval >= limit))
			currentCounter.value = newval;
		
		clearTimeout(counterTimer);
		counterTimer = timerCounter.delay(0.05, step, limit);
	} else {
		counterTimer =  timerCounter.delay(0.5, step, limit);
	}
}

function unclickCounter(button, callback)
{
	if (counterTimer) {
		clearTimeout(counterTimer);
		counterTimer = false;
	}
	
	if (button) {
		button.onmouseup = button.onmouseout = null;
		if (!callback || eval(callback+'()')) currentCounter = false;
	}
}

function updateCounter(counter, step, minval, maxval, callback)
{
	currentCounter = $(counter);
	var newval = parseInt($F(currentCounter));
	if (isNaN(newval)) newval = 0;
	if (minval) newval = Math.max(minval, newval);
	if (maxval) newval = Math.min(maxval, newval);
	if ((newval % step) > 0) newval = Math.ceil(newVal/step)*step;
	currentCounter.value = newval;
	if (!callback || eval(callback+'()')) currentCounter = false;
}

function selectAllItems(selObj, sel)
{
	$A($(selObj).options).each(function(item)
	{
		item.selected = sel;
	});
}