
var KEvent = YAHOO.util.Event;
var $ = YAHOO.util.Dom.get;
var KDom = YAHOO.util.Dom;

var FK = {};

FK.addEvent = function(el, ev, call, args){
	args.push(el);
	KEvent.addListener(el, ev, call.createDelegate(this, args));
}

FK.updateElement = function(url, target, options, source){
	new FK.Updater(url, target, options, source);
}

FK.Updater = function(url, target, options, source){
	
	this.url = url;
	this.target = target;
	
	if(options){
		if(options.callback)
			this.callback = eval(options.callback);
		if(options.scope)
			this.callbackScope = eval(options.scope);
		if(options.onclick)
			this.onclick = eval(options.onclick);
	}
	if(this.onclick){
		this.onclick(source);
	}
	this.request();
}

FK.Updater.prototype = {
	request: function(){
		YAHOO.util.Connect.asyncRequest(
			'POST',  this.url, 
			{success: this.receive, 
			 failure: this.failure, 
			 scope: this}
		);
	},
	
	receive: function(o){
		eval("var response = " + o.responseText);
		if(response && response.k_html !== undefined){
			document.getElementById(this.target).innerHTML = response.k_html;
		}
		if(this.callback){
			if(!this.callbackScope)
				this.callback(response);
			else
				this.callback.createDelegate(this.callbackScope, [response])();
		}
	}
}
	
FK.submitForm = function(e, args, formId, url, target, loading, success){

	if(loading)
		loading();
	
	var args = YAHOO.util.Connect.setForm(formId);
	
	updateTarget = target;
	YAHOO.util.Connect.asyncRequest(
		'POST',  url,
		{success: FK.receive,
		 argument: [success]}, args
	);
	
	Event.preventDefault(e);
	
}

FK.receive = function(o){
	
	eval("var response = " + o.responseText);
	
	if(response.k_html !== undefined){
		document.getElementById(updateTarget).innerHTML = response.k_html;
	}
	
	if(response.error !== undefined){
		
	}

	o.argument[0]();
	
}

FK.Util = {};

FK.Util.url = function(path){
	return "";
}


//Thank to ExtJS for this
Function.prototype.createDelegate = function(C,B,A){
	var D=this;
	return function(){
		var F=B||arguments;
		if(A===true){
			F=Array.prototype.slice.call(arguments,0);
			F=F.concat(B)
		}
		else{
			if(typeof A=="number"){
				F=Array.prototype.slice.call(arguments,0);
				var E=[A,0].concat(B);
				Array.prototype.splice.apply(F,E)
			}
		}
		return D.apply(C||window,F)
	}
}
