var waiting = false;

function $(id) { return document.getElementById(id); }

String.prototype.trim = function() { return this.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); }
String.prototype.startsWith = function(s) { return (this.length >= s.length && this.substring(0,s.length) == s); }
String.prototype.endsWith = function(s) { return (this.length >= s.length && this.substring(this.length - s.length) == s); }
String.prototype.fillLeft = function(filler, len) { var s = this; while(s.length < len) s = filler + s; return s.substring(s.length - len); }
Number.prototype.fillLeft = function(filler, len) { return (new String(this)).fillLeft(filler, len); }

Number.prototype.toCurrency = function() {
	var i = isNaN(this) ? 0.00 : this;
	var s = (i < 0 ? '-' : '') + (new String(parseInt((Math.abs(i) + .005) * 100) / 100));
	return s + (s.indexOf('.') < 0 ? '.00' : (s.indexOf('.') == s.length - 2 ? '0' : ''));
}

function toggleComboVisibility(vis)
{
	var selects = document.getElementsByTagName("select");

	for(var idx = 0; idx < selects.length; idx++) {
		var s = selects.item(idx);
		s.style.visibility = (vis ? 'visible' : 'hidden');
	}
}

function openPopupWindow(wndName, w, h, cmd, resiz)
{
	var sw = (screen.availWidth ? screen.availWidth : 800);
	var sh = (screen.availHeight ? screen.availHeight : 600);

	if(w < 0) w = sw + w; if(w == 0 || w > sw - 80)  w = sw - 80;
	if(h < 0) h = sh + h; if(h == 0 || h > sh - 140) h = sh - 140;

	var props = "dependent,scrollbars," + (resiz ? "resizable," : "");
	props += "left=" + ((sw - w - 10) / 2) + ",top=" + ((sh - h) / 2 - 20) + ",";
	props += "width=" + w + ",height=" + h;

	return window.open(cmd, wndName, props);
}

function setupPopupWindow(popup)
{
	popup.focus();

	try {
		var opener = popup.opener;
		var document = opener.document;

		popup.openerLocation = opener.location.href;
		
		popup.checkOpener = function()
		{
			try {
				if(!this.opener || this.opener.closed || this.opener.location.href != this.openerLocation) self.close();
				else this.setTimeout("checkOpener()", 1000);
			}
			catch(e) {
				self.close();
			}
		}
		
		popup.checkOpener();

		if(!document.windowGlass) {
			var _body = document.getElementsByTagName("body").item(0);

			toggleComboVisibility(false, document);

			document.windowGlass = document.createElement('div');
			document.windowGlass.id = 'GLASS';
			document.windowGlass.style.height = '100%';

			document.body.appendChild(document.windowGlass);

			if(document.windowGlass.offsetHeight < _body.offsetHeight) document.windowGlass.style.height = _body.offsetHeight + 'px';
		}

		popup.onunload = function()
		{
			var opener = this.opener;
			var document = opener.document;

			opener.focus();

			document.body.removeChild(document.windowGlass);

			document.windowGlass = null;
	
			toggleComboVisibility(true, document);
		}
	}
	catch(e) { }
}

function zedFCKOnPaste(editor)
{
	var html = editor.GetClipboardHTML();

	html = html.replace(/\s/g, " ");
	html = html.replace(/\&nbsp\;/gi, " ");
	html = html.replace(/  /g, " ").trim();
	html = html.replace(/<\s*script.*?<\/\s*script\s*>/gi, "");
	html = html.replace(/<\s*div.*?>/gi, "<p>");
	html = html.replace(/<\/\s*div.*?>/gi, "</p>");
	html = html.replace(/<\s*p.*?>/gi, "<p>");
	html = html.replace(/<\/\s*p.*?>/gi, "</p>");
	html = html.replace(/style\s*?=\s*?/gi, "");
	html = html.replace(/class\s*?=\s*?/gi, "");
	html = html.replace(/\.\s*<br[ \/]*?>/gi, "\.</p><p>");
	html = html.replace(/<br[ \/]*>/gi, " ");

	editor.InsertHtml(html);
	editor.Commands.GetCommand('SelectAll').Execute();
	editor.Commands.GetCommand('RemoveFormat').Execute();

	return false;
}

function getBackUrl()
{
	return escape(document.location.pathname + document.location.search + document.location.hash);
}

function gotoAndBack(uri)
{
	document.location = uri + (uri.indexOf('?') >= 0 ? '&' : '?') + "backUrl=" + getBackUrl();
}

function ajaxGet(_url, _callBack)
{
	return YAHOO.util.Connect.asyncRequest('GET', _url, {
		cache: false,
		success: function(o) {
			if(_callBack) _callBack(o.responseText.trim());
		},
		failure: function(o) {
			if(_callBack) _callBack(null);
		}
	});
}

function ajaxCall(method /*,...*/)
{
	var args = []; for(var i = 1; i < arguments.length; i++) args.push(arguments[i]);
	return _ajaxCall(method, null, args);
}

function ajaxPost(method, form /*,...*/)
{
	var args = []; for(var i = 2; i < arguments.length; i++) args.push(arguments[i]);
	return _ajaxCall(method, form, args);
}

function _ajaxCall(method, form, args)
{
	var pos = method.lastIndexOf('/');
	var page = document.location.pathname;

	if(pos > 0) {
		page = method.substring(0, pos) + '.zsp';
		method = method.substring(pos + 1);
	}

	var _url = page + '?ajaxCommand=' + method;
	var callBack = null;

	for(var i = 0; i < args.length; i++) {
		var a = args[i];

		if(YAHOO.lang.isFunction(a)) {
			if(i < args.length - 1) return null;

			callBack = a;
			break;
		}

		_url += '&ajaxParam' + (i + 1) + '=' + escape(args[i]);
	}

	if(form) YAHOO.util.Connect.setForm(form, form.encoding.indexOf('multipart') >= 0);

	var res = YAHOO.util.Connect.asyncRequest(form ? 'POST' : 'GET', _url, {
		cache: false,
		success: function(o) {
			if(callBack) {
				var r = o.responseText.trim();

				if(r.startsWith('<PRE>') && r.endsWith('</PRE>')) {
					r = r.substring(5, r.length - 6);
				}

				if(r != '' && YAHOO.lang.JSON.isValid(r)) {
					r = YAHOO.lang.JSON.parse(r);
				}
				else if(YAHOO.lang.isNumber(r)) {
					r = parseFloat(r);
				}

				callBack(r);
			}

			if(form) YAHOO.util.Connect.resetFormState();
		},
		failure: function(o) {
			if(callBack) callBack(null);
			if(form) YAHOO.util.Connect.resetFormState();
		},
		upload: function(o) { this.success(o); },
		abort: function(o) { this.failure(o); }
	});
	
	return res;
}
