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.setDynamicParam=PostLink_SetDynamicParam;
	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);
	if(this.postlink.postDispatch.length > 0){
		for(var i=0;i<this.postlink.postDispatch.length;i++){			
			this.postlink.postDispatch[i].call();
		}
	}
};

PostLink_SetStaticParams = function(params)
{
	this.request.setStaticParams(params);
};

PostLink_SetDynamicParam=function(paramstr)
{
	var sep="&";
	if(this.request.indynamicparams==="")
	{
		sep="";
	}
	if(this.request.indynamicparams.indexOf(paramstr)==-1)
	{
		this.request.indynamicparams+=sep+paramstr;
	}
};

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('')
			}
		}
	}
};

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('êtes-vous sur ?')){
				this.doPost();
			}
		}else{
			this.doPost();
		}
	}
}

PostLink_doPost=function()
{
	// for(tid in window.transactions)
	// {
	// 	YAHOO.util.Connect.abort(window.transactions[tid]);
	// }
	// console.log(window.transactions);
	this.request.send();
};
