// JavaScript Document

function redirect(url)
{
	var prefix = index();
	if (url.indexOf(prefix)>-1)
		window.location.href = url;
	else
		window.location.href = prefix+'/App/page/'+url;
}

function request()
{
	var s = "";
    var c = document.forms[0].elements;
	for(var i=0; i<c.length; i++) 
	{
		obj = c[i];		
		if (obj.type == 'checkbox' || obj.type == 'radio') 
		{	
			if (obj.checked)
			{
				if (obj.value)
					s += obj.name+"="+obj.value+"&";
				else
					s += obj.name+"=on&";
			}
		} else {
			// urldecode in PHP
			s += obj.name+"="+escape(encodeURI(obj.value))+"&";
		}
	}
	return s;
}

function __dopostback(target, args)
{
	var f = document.forms[0];
	f.__EVENTTARGET.value = target;
	f.__EVENTARGUMENT.value = args;
	f.submit();
}

function __docallback(target, args, callback, func)
{
	var f = document.forms[0];
	f.__EVENTTARGET.value = target;
	f.__EVENTARGUMENT.value = args;
	if (callback)
		ajaxRun_post(f.action, request(), callback, func);
	else
		ajaxRun_post(f.action, request(), doit, func);
}

var clientUI =
{	
	addItem : function (id, property, callback, error_msg, error_id)
	{
		var item = new ValidateItem(id, property, callback, error_msg, error_id);
		clientUI.clients[clientUI.clients.length] = item;
	},	
			
	clients : []
};

function validateItems()
{
	for (var i=0; i<clientUI.clients.length; i++)
	{
		if (!clientUI.clients[i].validate())
		{
			clientUI.clients[i].focus();
			return false;
		}
	}
	return true;
}

ValidateItem = function(id, property, callback, error_msg, error_id)
{
	this.id = id;
	this.property = property;
	this.callback = callback;
	this.error_msg = error_msg;
	this.error_id = error_id;
	
	this.validate = function()
	{
		var o = document.getElementById(this.id);
		var ret;
		eval('ret = '+this.callback+'(o.'+this.property+')');
		if (ret == false)
		{
			if (this.error_id)
			{
				var e = document.getElementById(this.error_id);
				e.innerHTML = this.error_msg;
			} else
				alert(this.error_msg);
			return false;
		}
		
		return true;
	}
	
	this.focus = function()
	{
		var o = document.getElementById(this.id);
		o.focus();
	}
}

function onEnter(evt)
{
	var e = eventObject(evt);
	if ((e.which && e.which == 13) || 
		(e.keyChar && e.keyChar == 13) ||
		(e.keyCode && e.keyCode == 13))
		return true;
	else
		return false;
}

function registerEvent(o, t, f) 
{
	if (o.addEventListener)
	{
		if (t=='mousewheel')
		{
			o.addEventListener('DOMMouseScroll', f, false);
		} 
		o.addEventListener(t, f, false);
	}
	else if (o.attachEvent) 
		o.attachEvent('on'+ t, f);
	else 
		o['on'+ t] = f;
};

function unregisterEvent(o, t, f) 
{
	if (o.removeEventListener) 
	{
		if (t=='mousewheel')
		{
			o.removeEventListener('DOMMouseScroll', f, false);
		} 		
		o.removeEventListener(t, f, false);
	} else if (o.detachEvent) 
		o.detachEvent('on'+ t, f);
	else 
		o['on'+ t] = null;
};

function eventObject(evt) 
{
    return (evt) ? evt : window.event;
}

function senderObject(evt) 
{
    evt = eventObject(evt);
	return (evt.target) ? evt.target : evt.srcElement;
}

function elementObject(id)
{
	if (document.getElementById)
		return document.getElementById(id);
	else if (document.all)
		return document.all[id];
	else if (document.layers)
		return document.layers[id];
	return null;
}

function pxUnit()
{
	return document.childNodes ? 'px' : 0;
}


