		// JavaScript Document
var _xRootUrl = new String(location.href);
_RootUrl = _xRootUrl.slice(0, _xRootUrl.lastIndexOf("/"));
function xWriteFlash(flashtxt) {
	document.write(flashtxt);
}

function xListExch(src, dst) {
	var os = $(src).options, od = $(dst).options;
	for (var k = os.length-1; k >= 0; k--)
		if (os[k].selected) {
			var opt = new Option(os[k].text, os[k].value, false, true);
			od.add(opt);
			os[k] = null;
		}
}

function xListSelectAll(list) {
	var oOptions = $(list).options;
	for (var k = 0, len = oOptions.length; k < len; k++)
		oOptions[k].selected = true;																
}

function xEditCharLimit(obj, maxChars, msgBox) {
	var chars = $(obj).value;
	if (chars.length > maxChars) {
		chars = chars.substring(0, maxChars);
		$(obj).value = chars;
	}
	$(msgBox).innerHTML = (maxChars-chars.length);
}

function xAnyEmpty(obj) {
	for (var i in obj) {
		if (typeof obj[i] != "Function" && (!obj[i] || obj[i].length == 0))
			return i;
	}
	return false;
}

function xAlertReturn(msg, ret) {
	ret = ret ? ret : false;
	alert(msg);
	return ret;
}

function xDebug(msg) {
	if (xDEBUG) {
		alert(msg);
	}
}

var _xVarEmailRegEx = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;
function xValidEmail(str) {
	return _xVarEmailRegEx.test(str);
}

function xWriteFlashDiv(flashtxt, divname) {
 	document.getElementById(divname).innerHTML = flashtxt;
}

function xDeleteConfirm(title, enc) {
	title = enc ? decodeURIComponent(title) : title;
	var ans = confirm("You are about to delete '"+title+"'. ARE YOU SURE?");
	if (ans) {
		var ans2 = confirm("THIS CANNOT BE UNDONE. DELETE '"+title+"'?");
		if (ans2) {
			return true;
		}
	}
	return false;
}
function xClearOnFocus(elemId, defValue, fnValidate) {
	var elem = $(elemId);
	elem.onfocus = function () {
		var val = elem.value;
		if (val == defValue || (typeof fnValidate == "function" && !fnValidate(val)))
			elem.value = "";
	}
	elem.onblur = function () {
		var val = elem.value;
		if (val.length == 0 || val == defValue || (typeof fnValidate == "function" && !fnValidate(val)))
			elem.value = defValue;
	}
	var val = elem.value;
	if (val == defValue || (typeof fnValidate == "function" && !fnValidate(val)))
		elem.value = defValue;
}

function xValidateCalDate(dateStr) {
	var msec = Date.parse(dateStr);
	if (isNaN(msec)) return false;
	return true;
}

var _xVarErrorsVisible = true;

function xHideErrors() {
	var elements = document.getElementsByClassName("error");
	nelems = elements.length;
	for(var k = 0; k < nelems; k++) {
		var elem = elements[k];
		elem.style.display = "none";
	}
	_xVarErrorsVisible = false;
}
function xShowErrors() {
	var elements = document.getElementsByClassName("error");
	nelems = elements.length;
	for(var k = 0; k < nelems; k++) {
		var elem = elements[k];
		elem.style.display = "";
	}
	_xVarErrorsVisible = true;
}
function xToggleErrors() {
	if (_xVarErrorsVisible)
		xHideErrors();
	else
		xShowErrors();
	return 	_xVarErrorsVisible;
}
function xDeleteErrors() {
	var elements = document.getElementsByClassName("error");
	nelems = elements.length;
	for(var k = 0; k < nelems; k++) {
		var elem = elements[k];
		elem.parentNode.removeChild(elem);
	}
}
var XRssScroller = function (scroller, options){
	this.scroller = $(scroller);
	if (this.scroller._xPropRssScroller) this.scroller._xPropRssScroller.remove();
	if (typeof options == "undefined") options = {};
	this.url = options.url ? options.url : _xRootUrl+"/rss.php?";
	this.delay = options.delay ? options.delay : 2000;
	this.delta = options.delta ? options.delta : 2;
	this.scroller._xPropRssScroller = this;
	this.updatingCache = false;
	this.updatingScroller = false;
	this.noScroll = true;
	this.stopped = true;
	this.scrollerTimer = false;
	this.rssAux = null;
	this.initRssAux();
	this.updateCache();
}
XRssScroller.prototype = {
	remove: function() {
		delete this.scroller._xPropRssScroller;
	},
	// Update the scroller from cache
	updateScroller: function () {
		// Do not overlap with cache updates
		if (this.updatingCache || this.noScroll) return;
		this.updatingScroller = true;
		var top = parseInt(this.rssAux.style.top);
		var first = $(this.rssAux.childNodes[0]);
		if (top+Element.getHeight(first) < 0) {
			this.noScroll = true;
			top = 0;
			var marginTop = first.getStyle("margin-top");
			var marginBottom = first.getStyle("margin-bottom");
			var paddingTop = first.getStyle("padding-top");
			var paddingBottom = first.getStyle("padding-bottom");
			top -= (isNaN(marginTop) ? 0 : marginTop)+(isNaN(marginBottom) ? 0 : marginBottom);
			top -= (isNaN(paddingTop) ? 0 : paddingTop)+(isNaN(paddingBottom) ? 0 : paddingBottom);
			first.remove();
			this.rssAux.appendChild(first);
			this.noScroll = false;
		}
		this.rssAux.style.top = (top-this.delta)+"px";
		this.updatingScroller = false;
	},
	// Update the cache
	updateCache: function () {
		if (this.updatingCache)	return;
		fOnSuccess = function(req) {
			this.stop();
			if (req.responseText && req.responseText.length > 0) {
				this.updatingCache = true;
				this.handleCacheUpdate(req.responseText);
				this.updatingCache = false;
				this.noScroll = false;
				this.start();
			}
		}
		fOnFail = function (req, exception) {
			if (!exception) {
				error = req.getAllResponseHeaders()+'\n'+req.responseText;
				alert("RSS Error: \n"+error);
			}
		};
		var opt = {method: 'post', postBody: 'feed='+encodeURIComponent(this.url), onSuccess: fOnSuccess.bind(this), onFail: fOnFail.bind(this), onException: fOnFail.bind(this)};
		var obj = new Ajax.Request(_RootUrl+'/ajax.rssread.php', opt);		
	},
	initRssAux: function() {
		var auxId = this.scroller.id+"-aux";
		if ($(auxId)) {
			this.rssAux = $(auxId);
			return this.rssAux;
		}
		this.scroller.innerHTML = "";
		var scrollDims = this.scroller.getDimensions();
		var rssAux = document.createElement("div");
		this.scroller.appendChild(rssAux);
		rssAux.id = auxId;
		rssAux.style.display = "block";
		rssAux.style.position = "relative";
		rssAux.style.top = scrollDims.height+"px";
		this.rssAux = $(rssAux);
		return this.rssAux;
	},
	// Cache update ajax handler
	handleCacheUpdate: function(xml) {
		if (this.updatingScroller || this.noScroll)
			// Delay if we are updating the scroller
			setTimeout(this.handleCacheUpdate.bind(this, xml), 100);
		var rssAux = this.initRssAux();
		rssAux.innerHTML = xml;
		rssAux.cleanWhitespace();
		rssAux.onmouseout = this.start.bind(this);
		rssAux.onmouseover = this.stop.bind(this);
		var len = rssAux.childNodes.length;
		for (var k = 0; k < len; k++) {
			var node = $(rssAux.childNodes[k]);
		}
	},
	start: function () {
		this.stopped = false;
		if (this.scrollerTimer) return;
		var timer = setInterval(this.updateScroller.bind(this), this.delay);
		this.scrollerTimer = timer;
	},
	stop: function () {
		if (this.stopped) return;
		this.stopped = true;
		var timer = this.scrollerTimer;
		this.scrollerTimer = false;
		if (timer) clearInterval(timer);
	}
}
function xUintDef(val, def) {
	def = (typeof def == "undefined") ? 0 : def;
	val = (typeof def == "undefined") ? def : parseInt(val);
	return val >= 0 ? val : def;
}
function xNintDef(val, def) {
	def = (typeof def == "undefined") ? 1 : def;
	val = (typeof def == "undefined") ? def : parseInt(val);
	return val > 0 ? val : def;
}
function xImageDims(w, h, mw, mh, p) {
	if (w*h == 0 || typeof w == "undefined" || typeof h == "undefined") return [0, 0];
	p		= xNintDef(p, 100);
	w		= xNintDef(Math.round(w*(p/100))); // New width adjusted to percentage
	h		= xNintDef(Math.round(h*(p/100))); // New height adjusted to percentage
	mw		= xNintDef(mw, w);
	mh		= xNintDef(mh, h);
	ratio		= Math.min(1, Math.min(mw/w, mh/h));
	return {width: Math.round(w*ratio), height: Math.round(h*ratio)};
}

function xFormSubmit(frm, fields) {
	frm = $(frm);
	if (!frm) return false;
	for (var f in fields) {
		var ff = $(f);
		if (!ff) continue;
		var tag = (ff.tagName || '').toLowerCase();
		if (tag && !['input', 'select', 'textarea'].include(tag)) continue;
		ff.value = fields[f];
		if (tag == 'input' && ff.type && ['radio', 'checkbox'].include(ff.type.toLowerCase())) {
			ff.checked = true;
		} else if (tag == 'select') {
			if (ff.type == 'select-one') {
				for (var k = 0, len = ff.options.length; k < len-1; ++k)
					if (ff.options[k].value == fields[f]) {
						ff.selectedIndex = k;
						break;
					}
			} else {
				for (var k = 0, len = ff.options.length; k < len-1; ++k)
					if (fields[f].indexOf(ff.options[k].value))
						ff.options[k].selected = true;
					else
						ff.options[k].selected = false;
			}
		}
	}
	frm.submit();
}

var XContentScroller = function (options) {
	this.direction = options.direction || 'vertical';
	this.src = $(options.src);
	this.clipper = $(options.clipper || this.src.up());
	this.upHandler = options.up ? $(options.up) : null;
	this.downHandler = options.down ? $(options.down) : null;
	this.leftHandler = options.left ? $(options.left) : null;
	this.rightHandler = options.right ? $(options.right) : null;
	this.step = options.step || 2;
	this.interval = options.interval || 40;
	this.timerId = null;
	this.bindedUpHandler = null;
	this.bindedDownHandler = null;
	this.bindedLeftHandler = null;
	this.bindedRightHandler = null;
	this.bindedCancelHandler = null;
	this.autoConnect = (typeof options.auto != "undefined") ? options.auto : true;
	if (this.autoConnect)
		this.connect();
}

XContentScroller.prototype = {
	addHandlers: function () {
		this.bindedCancelHandler = this.scrollCancelHandler.bindAsEventListener(this);
		if (this.upHandler) {
			this.bindedUpHandler = this.scrollUpHandler.bindAsEventListener(this);
			Event.observe(this.upHandler, 'mouseover', this.bindedUpHandler);
			Event.observe(this.upHandler, 'mouseout', this.bindedCancelHandler);
		}
		if (this.downHandler) {
			this.bindedDownHandler = this.scrollDownHandler.bindAsEventListener(this);	
			Event.observe(this.downHandler, 'mouseover', this.bindedDownHandler);
			Event.observe(this.downHandler, 'mouseout', this.bindedCancelHandler);
		}
		if (this.leftHandler) {
			this.bindedLeftHandler = this.scrollLeftHandler.bindAsEventListener(this);	
			Event.observe(this.leftHandler, 'mouseover', this.bindedLeftHandler);
			Event.observe(this.leftHandler, 'mouseout', this.bindedCancelHandler);			
		}
		if (this.rightHandler) {
			this.bindedRightHandler = this.scrollRightHandler.bindAsEventListener(this);	
			Event.observe(this.rightHandler, 'mouseover', this.bindedRightHandler);
			Event.observe(this.rightHandler, 'mouseout', this.bindedCancelHandler);			
		}
	},
	removeHandlers: function () {
		if (this.upHandler) {
			Event.stopObserving(this.upHandler, 'mouseover', this.bindedUpHandler);
			Event.stopObserving(this.upHandler, 'mouseout', this.bindedCancelHandler);
		}
		if (this.downHandler) {
			Event.stopObserving(this.downHandler, 'mouseover', this.bindedDownHandler);
			Event.stopObserving(this.downHandler, 'mouseout', this.bindedCancelHandler);			
		}
		if (this.leftHandler) {
			Event.stopObserving(this.leftHandler, 'mouseover', this.bindedLeftHandler);
			Event.stopObserving(this.leftHandler, 'mouseout', this.bindedCancelHandler);			
		}
		if (this.rightHandler) {
			Event.stopObserving(this.rightHandler, 'mouseover', this.bindedRightHandler);
			Event.stopObserving(this.rightHandler, 'mouseout', this.bindedCancelHandler);			
		}
	},
	connect: function () {
		var clipperDims = this.clipper.getDimensions();
		this.clipperHeight = clipperDims.height;
		this.clipperWidth = clipperDims.width;
		var srcDims = this.src.getDimensions();
		this.srcHeight = srcDims.height;
		this.srcWidth = srcDims.width;
		this.addHandlers();
	},
	disconnect: function () {
		this.removeHandlers();
	},
	scrollUp: function () {
		var srcTop = parseInt(this.src.getStyle('top'));
		if (srcTop >= 0) return;
		this.src.style.top = (srcTop+this.step)+"px";
	},
	scrollDown: function () {
		var srcTop = parseInt(this.src.getStyle('top'));
		if (srcTop+this.srcHeight <= this.clipperHeight) return;
		this.src.style.top = (srcTop-this.step)+"px";
	},
	scrollLeft: function () {
		var srcLeft = parseInt(this.src.getStyle('left'));
		if (srcLeft >= 0) return;
		this.src.style.left = (srcLeft+this.step)+"px";
	},
	scrollRight: function () {
		var srcLeft = parseInt(this.src.getStyle('left'));
		if (srcLeft+this.srcWidth <= this.clipperWidth) return;
		this.src.style.left = (srcLeft-this.step)+"px";
	},
	scrollCancelHandler: function () {
		clearInterval(this.timerId);
	},	
	scrollUpHandler: function () {
		this.timerId = setInterval(this.scrollUp.bind(this), this.interval);
	},
	scrollDownHandler: function () {
		this.timerId = setInterval(this.scrollDown.bind(this), this.interval);
	},
	scrollLeftHandler: function () {
		this.timerId = setInterval(this.scrollLeft.bind(this), this.interval);
	},
	scrollRightHandler: function () {
		this.timerId = setInterval(this.scrollRight.bind(this), this.interval);
	}
}

function xOpenCenteredWindow(url, width, height) {
    var width = width ? width : 640;
    var height = height ? height : 480;
    var left = parseInt((screen.availWidth/2) - (width/2));
    var top = parseInt((screen.availHeight/2) - (height/2));
    var windowFeatures = "width=" + width + ",height=" + height + 
        ",status,resizable,left=" + left + ",top=" + top + 
        ",screenX=" + left + ",screenY=" + top;
    myWindow = window.open(url, "CENTERED", windowFeatures);
    myWindow.focus();
    return myWindow;
}