/*
 * Gestion du formulaire de contact
 */
toutou.require("toutou.html.dom");
toutou.require("toutou.js.ajax");
toutou.require("toutou.effect.fade");
toutou.require("toutou.html.dnd");
toutou.require("toutou.html.layout");
toutou.require("toutou.html.style");

if(typeof js == "undefined") {
	js = {};
	js.imgPath = "";
}

js.form = new function() {
	this.langue = "";
	this.formId = "";
	this.requestUri = "";
}


js.form.setFormRequirements = function(uri, formId){
	this.requestUri = uri;
	this.formId = formId;
}

js.form.submit =  function(langue){
	var myForm = tt$(this.formId);
	this.langue = langue; 
	if (myForm==null){
		 toutou.debugLogger.log("ERROR",this.formId+" introuvable");
		 return false;
	}
	
	//Build the param list
	var params = "method=enregistrer";
	
	var i=0;
	for (i=0;i<myForm.elements.length;i++){
		params += "&"+myForm.elements[i].name+"="+myForm.elements[i].value;
	}
	
	
	xhr = new toutou.js.ajax.xhr();
	
	xhr.asynchronous=true;
	
	//Set the url
	xhr.url = this.requestUri;
	
	xhr.method = "post";
	//Get the options ready

	xhr.parameters = params;
	
	//Set up callbacks
	xhr.tt_eventManager.addListener("loading", js.form.on_request_start);
	xhr.tt_eventManager.addListener("success", js.form.on_request_end);

		
	
	//Fire it off
	xhr.request();
	
	return false;
	
	
}

/*
 * This function is called as soon as the ajax request goes off, it disables the form
 */
js.form.on_request_start = function(){
//	//Initialisation
	var myForm = tt$(js.form.formId);
	toutou.effect.fade.extend(myForm);
	myForm.tt_effect.fade.fade(50, 500);
	return true;
}


/*
 * This function is called as soon as the ajax request returns
 * It re-activates the form, and if need be, shows any errors
 */
js.form.on_request_end = function(eventData){
	var myForm = tt$(js.form.formId);
	toutou.effect.fade.extend(myForm);
	myForm.tt_effect.fade.fade(100, 500);
	
	var data = eventData.xhr.getResponseText();
	
	var content_zone_erreurs = tt$("contenu_zone_erreurs");
	toutou.html.layout.extend(content_zone_erreurs);
	var zone_erreurs = tt$("zone_erreurs");
	toutou.html.layout.extend(zone_erreurs);
	var attention = js.i18n.getLocalString("FILMS","Attention !",js.form.langue);
	
		//Did everything go OK?
	if (data.match(/^Ok\|/)){
		data = data.replace(/^Ok\|/,"");
		js.popup.popup_window("Europa Film Treasures", data, "#abd1e0", "#000", false);
	}else{
		//Remplace les | par des <BR>
		data = data.replace(/\|/g,"<br>");
		js.popup.popup_window(attention, data, "#8b0121", "#FFF", true);
}

	return true;
}


