debug = false;

function jGUI_FrameWork(langCode)
{
  return new __FrameWork(langCode);
}

function __FrameWork(langCode)
{
  if(document.forms.length == 0)
  {
	alert('<action /> element missing !!!');
	return null;
  }
  if(document.forms.length > 1)
  {
	alert('Too many forms !!!');
	return null;
  }
  this.form = document.forms[0];
	
  this.getVAR = get_cookie;
  this.setVAR = set_cookie;
  this.showMessage = __showMessage;
  this.reloadPage = __reloadPage;
  this.confirm = __confirm;
  this.submit = __submit;

  this.__formats = Array();
  this.formatData = __formatData;
  this.__roundings = Array();
  this.roundData = __roundData;
  
  this.addModule = __addModule;
  
  this.getMessage = __getMessage;

  this.__callMethod = __callMethod;
  this.__lastTimer = null;
  
  this.__langCode = langCode
  this.__messages = new __jGUI_Messages();

  this.targetURL = null;
  this.queryMethod = null;
  
//  setTimeout("_app_.setMessage('&nbsp;')", 5000);
}

function jGUI_Element(objID, show_error)
{
	if(show_error == undefined)
		show_error = true;
	return new __Element(objID, show_error);
}

function __Element(objID, show_error)
{
	if(show_error == undefined)
		show_error = true;
	this.id = objID;
	this.me = get_obj(objID);
	if(this.me == null)
	{
		if(show_error)
		{
			alert("Object ID `" + objID + "` doesn't exist");
		}
		return null;
	}
	this.getValue = __getValue;
	this.setValue = __setValue;
	this.checkValue = __checkValue;
	this.setFocus = __setFocus;
	this.setEnabled = __setEnabled;
	this.onTAB = __onTAB;
	this.toggleClass = __toggleClass;
	this.setClass = __setClass;
	this.display = __setDisplay;

	this.click = function() {this.me.onclick();};

	this.__showFocus = __showFocus;
	
	this.__dont_submit = false;
}

function __addModule(cls_name, methods)
{
	eval("this." + cls_name + " = new __module('" + cls_name + "',methods)");
}

function __module(name, methods)
{
	var i;
	for(i = 0; i < methods.length; i++)
	{
		eval("this." + methods[i] + " = function __" + name + "__" + methods[i] + "(){return _app_.__callMethod('" + name + "." + methods[i] + "', arguments);}");
	}
}

function __callMethod(method, args, post)
{
	if(_app_.__dont_submit)
		return;
	if(method == null || method.length < 3)
		return;
	if(args == undefined || args == null)
		args = Array();
	if(post == undefined || post == null)
		post = true;
		
	var i, params = '';
	for(i = 0; i < args.length; i++)
	{
		params+= "'" + args[i] + "'";
		if(i + 1 < args.length)
		{
			params+= ","
		}
	}
	
	post = true;
	this.form.elements["ACTION"].value = method;
	this.form.elements["PARAMS"].value = params;
	if(this.targetURL != null)
		this.form.action = this.targetURL;
	this.form.method = (this.queryMethod != null ? this.queryMethod : "POST");
	this.form.submit();
	return false;
}

function __onTAB(method, post)
{
	if(_app_.event.keyCode == 9)
	{
		if(me.getValue().length == 0)
		{
			if(method.substr(0,1) == "#")
			{
				_app_.callMethod(method.substr(1), post);
			}
			else
			{
				eval(method);
			}
			return false;
		}
	}
	return true;	
}

function __getValue()
{
	if(this.me.value == undefined)
	{
		var data = this.me.innerHTML;
		var i = 0;
		var c = data.substr(i, 1);
		while(c == '\n' || c == '\r')
		{
			c = data.substr(++i, 1);
		}
		ret = data.substr(i);
	}
	else
	{
		ret = this.me.value;
	}
	return ret;
}

function __setValue(val)
{
	if(this.me.value == undefined)
		this.me.innerHTML = val;
	else
		this.me.value = val;
}

function __setFocus()
{
	this.me.focus();
}

function __setEnabled(enable)
{
	this.me.disabled = (enable != null && !enable);
}

function __setDisplay(show)
{
	if(show == undefined || show == null)
		show = true;
	this.me.style.display = (show ? "" : "none");
}

function __reloadPage(add_prms)
{
	var prms = location.search;
	if(prms.length > 1)
	{
		prms+= "&" + add_prms;
		location.href = "/" + prms;
	}
	else
	{
		alert("MAYBE POST Method");
	}
}

function __showMessage(msg, type, to)
{
	var objMsg = jGUI_Element("MESSAGE", false)
	if(objMsg.me == null)
		return
	objMsg.me.className = "MESSAGE_" + type;
	objMsg.setValue(msg);
	if(_app_.__lastTimer != null)
	{
		clearTimeout(_app_.__lastTimer);
	}
	if(to == undefined || to == null)
		to = 8000;
	if(to > 0)
		_app_.__lastTimer = setTimeout('_app_.showMessage("&nbsp;", "INFO", 0);_app_.__lastTimer = null;', to);
}

function __confirm(msg)
{
	return confirm(msg);
}

function __toggleClass(cls1, cls2)
{
	if(this.me.className == cls1)
		this.me.className = cls2;
	else
		this.me.className = cls1;
}

function __setClass(cls)
{
	this.me.className = cls;
}

last_focus = null;
function __showFocus()
{
	if(last_focus != null)
		last_focus[0].className = last_focus[1];
	last_focus = Array(this.me, this.me.className);
	this.me.className+= '_FOCUS';
}

function __submit(action, method)
{
	if(action != undefined && action != null)
	{
		this.form.action = action;
	}
	if(method != undefined && method != null)
	{
		this.form.method = method;
	}
	this.form.submit();
}

function __getFormat(fmtName)
{
	var format = null;
	for(var i = 0; i < _app_.__formats.length; i+= 2)
	{
		if(_app_.__formats[i] == fmtName)
		{
			format = _app_.__formats[i + 1];
			break
		}
	}
	if(format == null)
	{
		format = eval("new __" + fmtName + "_DataFormat()");
		if(format == null)
		{
			alert("Illegal format name: " + fmtName);
			return null;
		}
		_app_.__formats.push(fmtName);
		_app_.__formats.push(format);
	}
	return format;
}

function __checkValue(fmtName, errMsgPfx, prm1, prm2, prm3, prm4)
{
	_app_.__dont_submit = false;
	var value = this.getValue();
	if(value.length == 0)
		return true;
	var format = __getFormat(fmtName);
	if(format == null)
		return false;
	var ret = format.checkData(value, prm1, prm2, prm3, prm4);
	_app_.__dont_submit = !ret;
	if(ret)
	{
		_app_.showMessage("", "INFO", 0);
	}
	else
	{
		if(errMsgPfx == undefined || errMsgPfx == null)
			errMsgPfx = fmtName;
		if(toString(errMsgPfx).length > 0)
			errMsgPfx+= ".";
		_app_.showMessage(_app_.getMessage('FORMAT.' + errMsgPfx + format.lastError), "ERROR")
		setTimeout("jGUI_Element(\"" + me.id + "\").setFocus();", 1);
	}
	return ret;
}

function __formatData(data, fmtName, prm1, prm2, prm3, prm4)
{
	var format = __getFormat(fmtName);
	if(format == null)
		return null;
	return format.formatData(data, prm1, prm2, prm3, prm4);
}

function __roundData(data, rc, prm1, prm2, prm3, prm4)
{
	var rounding = null;
	for(var i = 0; i < this.__roundings.length; i+= 2)
	{
		if(this.__roundings[i] == rc)
		{
			rounding = this.__roundings[i + 1];
			break
		}
	}
	if(rounding == null)
	{
		rounding = eval("new __" + rc + "_RoundingContext()");
		if(rounding == null)
		{
			alert("Illegal rounding context: " + rc);
			return null;
		}
		this.__roundings.push(rc);
		this.__roundings.push(rounding);
	}
	return rounding.roundData(data, prm1, prm2, prm3, prm4);
}

function __getMessage(msgID)
{
	return this.__messages.get(msgID + '.' + this.__langCode);
}

function test_func(index)
{
	alert(index);
}
