function initEvent(e) {
	Event.stop(e);
	$('goSoumission').observe('click', function(e){
		( $('code').value != '' ) ? formSubmit(e) : alert("Recopiez le code dans le champ correspondant");
	});

	//Stop la touche entrée sur le champ code
	Event.observe ('code','keypress', function(evt){
  	if(evt.keyCode==Event.KEY_RETURN)
    	Event.stop(evt);
    	return false;
    }
  );
}
Event.observe(window, "load", initEvent);

function formSubmit(e){
	Event.stop(e);
	new Effect.ScrollTo('header',{duration:0.5});
	//cache le bouton de soumission
	$('goSoumission').hide();
	var opt = {
    method: 'post',
    postBody: "action=submitFirst&code="+ $('code').value +"&description=" + FCKeditorAPI.GetInstance('description').GetXHTML(),
    onSuccess: function(transport)
    {
     	var t = transport.responseText;
      var rep = t.substring(0,1);
      var txt = t.substring(1, t.length );
      if ( rep == 0 )
      {
      	alert( txt );
      	$('goSoumission').show();
      }
      else
      {
      	new Effect.Appear('result', { duration:0.5, from:1.0, to:0.0 ,
      	afterFinish : function(){
      		$('result').innerHTML = txt;
      		new Effect.Appear('result', { duration:0.5, from:0.0, to:1.0,
      			 afterFinish : function(){ observeSubmitForm2(); }
      		} );
      		}
      	});
      }
   	}
	};
	new Ajax.Request('dispatcherForm.php', opt);
}

function observeSubmitForm2() {
	var champs = $A( $('result').getElementsByTagName('input') );
		champs.each( function( ele ) {
				var name = ele.id;
				var tdID = 'td_' + name;
				$(ele).observe('mouseover', function(e) {
						Event.stop(e);
						$(ele).src = 'img/bt_form_ajout_terme_hover.gif';
						$( tdID ).className = 'td_over';
					});
				$(ele).observe('mouseout', function(e) {
						Event.stop(e);
						$(ele).src = 'img/bt_form_ajout_terme.gif';
						$( tdID ).className = 'selected';
					});
				$(ele).observe('click', function(e) {
						Event.stop(e);
						affPopup( name, tdID );
					});
			}
		);
	Event.observe('submitFin', 'click', function () {
		var opt = {
			method: 'post',
			postBody: "action=submitSecond",
			onSuccess: function( transport ) {
				var val = transport.responseText;
				var bool = val.substring(0,1);
				if( bool == true )
				{
					new Effect.Appear('result', {duration:0.5, fps:25, from:1.0, to:0.0,
						afterFinish: function() {
							new Ajax.Updater('result','dispatcherForm.php',{
								method: 'post',
								postBody: "action=submitEventGood",
								onComplete: function(){
									new Effect.Appear('result', {duration:0.5, fps:25, from:0.0, to:1.0} );
									}
							} );
					}} );
				}
				else
					alert( val.substr( 1, val.length ) );
			}
		};
 		new Ajax.Request('dispatcherForm.php', opt);
	});
}

function writeHTML() {
	writeTAG(0,"js/window/default.css");
	writeTAG(0,"js/window/spread.css");
	writeTAG(0,"css/popup.css");
	writeTAG(1,"js/window/window.js");
	writeTAG(1,"js/window/windoweffects.js");
}

function writeTAG( type, chemin ){
	if( type == 1 )
	{
		var script = document.createElement("script");
		script.src = "http://www.zicanotes.com/" + chemin;
		script.type = "text/javascript";
		document.getElementsByTagName("head")[0].appendChild(script);
	}
	else if( type == 0 )
	{
		var css = document.createElement("link");
		css.href = "http://www.zicanotes.com/" + chemin;
		css.type = "text/css";
		document.getElementsByTagName("head")[0].appendChild(css);
	}
}

function affPopup( nomPopup, tdID ){
	win =	new Window( {
		url: "http://www.zicanotes.com/popup/"+ nomPopup +".php",
		className: "spread",
		minWidth:420,
		minHeight:arrHeight[nomPopup],
		maxHeight:arrHeight[nomPopup],
		width:440,
		height:arrHeight[nomPopup],
		title: arrTitle[nomPopup],
		effectOptions: {duration:0.5},
		destroyOnClose:"true"
		}
	);
	win.showCenter();
}
function closeWin(){
	win.close();
}
function submitReporting(){
	var star = $('star').value;
	var comment = $('comment').value;
	var opt = {
    method: 'post',
    postBody: "action=reporting&star="+ star +"&comment=" + comment,
    onSuccess: function(transport)
    {
     	$('reporting').hide();
     	alert("Nous vous remerçions pour vos commentaires !");
   	}
	};
	new Ajax.Request('dispatcherForm.php', opt);
}
var arrTitle = new Array;
arrTitle["type"] 	= "Type de l'évènement";
arrTitle["date"] 	= "Date de l'évènement";
arrTitle["salle"] = "Salle";
arrTitle["lieu"] 	= "Lieu de l'évènement";
arrTitle["heure"] = "Heure d'ouverture";
arrTitle["style"] = "Genre musical";
arrTitle["prix"] 	= "Tarification";
arrTitle["site"] 	= "Adresse d'un site Internet";
arrTitle["phone"] = "Numéro de téléphone";
arrTitle["email"] = "Email";
arrTitle["artiste"] = "Artiste(s)";
arrTitle["extrait"] = "Extrait musical";
arrTitle["fly"] = "Affiche de l'évènement";
arrTitle["nom"] = "Nom de l'évènement";

var arrHeight = new Array;
arrHeight["nom"]		= 150;
arrHeight["type"]		= 100;
arrHeight["date"] 	= 430;
arrHeight["salle"]	= 350;
arrHeight["lieu"] 	= 200;
arrHeight["heure"] 	= 100;
arrHeight["style"] 	= 100;
arrHeight["prix"] 	= 200;
arrHeight["site"] 	= 100;
arrHeight["phone"] 	= 100;
arrHeight["email"] 	= 100;
arrHeight["artiste"] = 280;
arrHeight["extrait"] = 100;
arrHeight["fly"] = 100;
