// 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, param_id)
{
	this.id = id;
	this.property = property;
	this.callback = callback;
	this.error_msg = error_msg;
	this.error_id = error_id;
	this.param_id = param_id;
	
	this.validate = function()
	{
		var o = document.getElementById(this.id);
		var ret;
		if (this.param_id)
			eval('ret = '+this.callback+'(o.'+this.property+', '+this.param_id+')');
		else
			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(event)
{
	var e = event?event:window.event;
	if ((e.which && e.which == 13) || 
		(e.keyCode && e.keyCode == 13))
		return true;
	else
		return false;
}