PostLink = function(id,url,staticparams,dynamicparams,onsuccess,onfailure, method)
{
	this.id=id;
	this.elem=document.getElementById(id);
	this.doPost = PostLink_doPost;
	if(onsuccess===null || typeof(onsuccess)=='undefined')
	{
		onsuccess=PostLink_defaultSuccessHandler;
	}
	
	var methtab=method.split(':');
	var rmethod='POST';
	if(methtab.length==1)
	{
		rmethod=methtab[0];
	}
	else
	{
		this.form=document.createElement('form');
		var repnode=document.getElementById(methtab[1]);
		var rparent=repnode.parentNode;
		var rnext=repnode.nextSibling;
		while(rnext.nodeType!=1)
		{
			rnext=rnext.nextSibling;
			if(rnext==null)
				break;
		}
		
		this.form.insertBefore(repnode,null);	
		this.form.setAttribute("action","javascript:void(0);");
		this.form.setAttribute("method","POST");
		for(i=0;i<this.form.firstChild.childNodes.length;i++)
		{
			if(typeof(this.form.firstChild.childNodes[i]['form'])!=='undefined')
			{
				this.form.firstChild.childNodes[i].form=this.form;
			}
		}
		rparent.insertBefore(this.form,rnext);
	}
	this.preDispatch = [];
	this.postDispatch = [];
	this.addPreDispatch = PostLink_addPreDispatch;
	this.addPostDispatch = PostLink_addPostDispatch;
	this.request = new Request(url,staticparams,dynamicparams,onsuccess,onfailure, rmethod);
	this.request.callback.postlink=this;
	this.dispatchResponse=PostLink_DispatchResponse;
	this.request.dispatchResponse=PostLink_DispatchResponse;
	this.setStaticParams = PostLink_SetStaticParams;
	this.setStaticParam = PostLink_SetStaticParam;
	this.setDynamicParam=PostLink_SetDynamicParam;
	this.emptyDynamicParam=PostLink_EmptyDynamicParam;
	this.setUpdatedSections=PostLink_setUpdatedSections;
	this.updatedSections=null;
	this.eventHandler=PostLink_eventHandler;
};

PostLink_setUpdatedSections=function(sections)
{
	this.updatedSections=sections;
};

PostLink_addPostDispatch = function(fn){
	this.postDispatch.push(fn);
}

PostLink_addPreDispatch = function(fn){
	this.preDispatch.push(fn);
}

PostLink_defaultSuccessHandler=function(o)
{
	var response={};
	if(o==null)
	{
	  return;
	}
	if(o.responseText.charAt(0)!=='{')
	{
		response={'exception':''+o.responseText};
	}
	else
	{
		eval('response='+o.responseText+';');
	}

	if(this.postlink.loading_img){
		this.postlink.loading_img.style.display = 'none'		
	}
	
	if(this.postlink.preDispatch.length > 0){
		for(var i=0;i<this.postlink.preDispatch.length;i++){			
			this.postlink.preDispatch[i].call();
		}
	}
	this.postlink.dispatchResponse(response,this.postlink.updatedSections);
};

PostLink_SetStaticParams = function(params)
{
	this.request.setStaticParams(params);
};
PostLink_SetStaticParam = function(key, value)
{
	this.request.setStaticParam(key, value);
};

PostLink_SetDynamicParam=function(paramstr)
{
	var sep="&";
	if(this.request.indynamicparams==="")
	{
		sep="";
	}
	if(this.request.indynamicparams.indexOf(paramstr)==-1)
	{
		this.request.indynamicparams+=sep+paramstr;
	}
};

PostLink_EmptyDynamicParam=function(){
	this.request.indynamicparams = "";
}

PostLink_DispatchResponse=function(response,sections)
{	
	sections="";
	for (key  in response)
	{
		sections+=key;
		sections+=",";
	}
	sections=sections.substr(0,sections.length-1);
	var sectiontab=sections.split(',');
	
	if(typeof(response['exception'])!=='undefined')
	{
		var rpart=response.exception;
		getCustomTag(sectiontab[0]).setContent(rpart);
	}
	else
	{
		for(si=0;si<sectiontab.length;si++)
		{
			var sectionid=sectiontab[si];
			var responsepart=response[sectionid];
			var secinst=getCustomTag(sectionid);
			if(secinst!=null && responsepart!=null)
			{
				secinst.setContent(responsepart);
				// secinst.show();
			}else if(secinst!=null){
				secinst.setContent('')
			}
		}

		if(this.postDispatch.length > 0){
			var fnsize = this.postDispatch.length;
			for (var i = 0; i < fnsize; i++) {
				this.postDispatch[i].call();
			}
		}
	}
};

PostLink_eventHandler=function(targetid,eventname,need_confirm)
{
	var cb=null;
	var cbname=targetid+'_'+eventname+'_callback';
	if(window[cbname])
	{
		var cb=window[cbname];
	}
	var pass=true;
	if(cb!=null)
	{
		pass=cb();
	}
	if(pass)
	{
		if(need_confirm){
			if(confirm('Etes-vous sûr ?')){
				this.doPost();
			}
		}else{
			this.doPost();
		}
	}
}

PostLink_doPost=function()
{
	// for(tid in window.transactions)
	// {
	// 	YAHOO.util.Connect.abort(window.transactions[tid]);
	// }
	// console.log(window.transactions);

	// =============================
	// = TRY TO MANAGE SCROLL UP   =
	// = Too tricky for the moment =
	// =============================
	// var yud = YAHOO.util.Dom;
	// var region = yud.getRegion(yud.get("mainMenu"));	
	// var win = yud.get("window");
	// console.log(win.scrollTop);
	// console.log(region.top);
	// if (win.scrollTop > region.top) {
	// 	this.addPostDispatch(function(){
	// 		var yus = YAHOO.util.Scroll;
	// 		var scroll = new yus(win,{ scroll: {to: [0,0] } },0);
	// 		scroll.animate();
	// 	});
	// }
	this.setStaticParam('_plid',this.id);
	this.request.send();
};
