var tables;
var regexMail = new RegExp( "^[a-zA-Z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$" );

function init_billeterie( ) {
	$.getJSON( 'ajax/billeterieJSON.php', { "php" : "pt" }, function( json ) {
		tables = json;
	} );
	$( '#form-acc-1' ).hide( );
	$( '#form-acc-2' ).hide( );
	$( '#form-acc-3' ).hide( );
	
	$( '#resa-repas' ).hide( );
	
	$( '#warning-pwd' ).hide( );
	$( '#warning-nom-elv' ).hide( );
	$( '#warning-prenom-elv' ).hide( );
	$( '#warning-mail-elv' ).hide( );
	
	$( '#pwd-repas-confirm' ).click( pwd_repas_confirm );
	$( '#resa-repas-confirm' ).click( resa_repas_confirm );
	
	$( '#nb_acc_0' ).click( function( ) { nb_acc( 0 ) } );
	$( '#nb_acc_1' ).click( function( ) { nb_acc( 1 ) } );
	$( '#nb_acc_2' ).click( function( ) { nb_acc( 2 ) } );
	$( '#nb_acc_3' ).click( function( ) { nb_acc( 3 ) } );
}

function table_dispo( idx ) {
	$( '#table-place-dispo' ).html( 'Il reste actuellement '+tables[ idx ]+' places disponibles à cette table.' );
}

function nb_acc( nb ) {
	if( nb==0 ) {
		$( '#form-acc-1' ).hide( 500 );
		$( '#form-acc-2' ).hide( 500 );
		$( '#form-acc-3' ).hide( 500 );
	} else if( nb==1 ) {
		$( '#form-acc-1' ).show( 500 );
		$( '#form-acc-2' ).hide( 500 );
		$( '#form-acc-3' ).hide( 500 );
	} else if( nb==2 ) {
		$( '#form-acc-1' ).show( 500 );
		$( '#form-acc-2' ).show( 500 );
		$( '#form-acc-3' ).hide( 500 );
	} else {
		$( '#form-acc-1' ).show( 500 );
		$( '#form-acc-2' ).show( 500 );
		$( '#form-acc-3' ).show( 500 );
	}
}

function pwd_repas_confirm( ) {
	var get = {
		"php" : "pwd",
		"pwd" : $( '#pwd-repas' ).val( )
	}
	$.getJSON( 'ajax/billeterieJSON.php', get, function( json ) {
		if( json[ 'ret' ]=='true' ) {
			$( '#resa-pwd' ).hide( 200 );
			$( '#resa-repas' ).show( 200 );
		} else {
			$( '#warning-pwd' ).show( );
		}
	} );
}

function resa_repas_confirm( ) {
	var nom_elv = $( '#input-nom-elv' ).val( );
	var prenom_elv = $( '#input-prenom-elv' ).val( );
	var tel_elv = $( '#input-tel-elv' ).val( );
	var mail_elv = $( '#input-mail-elv' ).val( );
	
	var warning = false;
	
	if( nom_elv=='' ) {
		$( '#warning-nom-elv' ).show( );
		warning = true;
	}
	if( prenom_elv=='' ) {
		$( '#warning-prenom-elv' ).show( );
		warning = true;
	}
	if( mail_elv=='' && !regexMail.test( mail_elv ) ) {
		$( '#warning-mail-elv' ).show( );
		warning = true;
	}
	
	if( !warning ) {
		var nb_acc = parseInt( $( 'input[name="nb_acc"]:checked' ).val( ) );
		
		var txt = 'Votre reservation :\n\n';
		txt += 'Vous avez réservé '+( nb_acc+1 )+' place'+( ( nb_acc==0 ) ? '' : 's' )+'.\n\n';
		txt += 'Votre nom : '+nom_elv+'\n';
		txt += 'Votre prénom : '+prenom_elv+'\n';
		if( tel_elv!='' ) txt+= 'Votre téléphone : '+tel_elv+'\n';
		txt += 'Votre mail : '+mail_elv+'\n\n';
		var i=1;
		for( i ; i<=nb_acc ; i++ ) {
			if( $( '#input-nom-acc-'+i ).val( )!='' ) txt += 'Nom de l\'accompagnateur '+i+' : '+$( '#input-nom-acc-'+i ).val( )+'\n';
			if( $( '#input-prenom-acc-'+i ).val( )!='' ) txt += 'Prénom de l\'accompagnateur '+i+' : '+$( '#input-prenom-acc-'+i ).val( )+'\n\n';
		}
		for( i ; i<=3 ; i++ ) {
			$( '#input-nom-acc-'+i ).val( '' );
			$( '#input-prenom-acc-'+i ).val( '' );
		}
		txt += 'Confirmez-vous cette réservation ?';
		
		if( confirm( txt ) ) {
			var get = {
				"php" : "resa",
				"ti" : parseInt( $( '#select-table' ).val( ) ),
				"ne" : convertHTML( nom_elv ),
				"pe" : convertHTML( prenom_elv ),
				"te" : convertHTML( tel_elv ),
				"me" : convertHTML( mail_elv ),
				"na" : nb_acc,
				"n1" : convertHTML( $( '#input-nom-acc-1' ).val( ) ),
				"p1" : convertHTML( $( '#input-prenom-acc-1' ).val( ) ),
				"n2" : convertHTML( $( '#input-nom-acc-2' ).val( ) ),
				"p2" : convertHTML( $( '#input-prenom-acc-2' ).val( ) ),
				"n3" : convertHTML( $( '#input-nom-acc-3' ).val( ) ),
				"p3" : convertHTML( $( '#input-prenom-acc-3' ).val( ) )
			};
			
			$.getJSON( 'ajax/billeterieJSON.php', get, function( json ) {
				var conf = 'Numéro de réservation : '+json[ "ret" ]+'\n\n' ;
				conf += 'Votre réservation a été prise en compte, elle sera validée suite à la réception d\'un chèque de '+ ( ( nb_acc+1 )*30 )+' euros à l\'ordre du Cercle des Eleves de l\'EMN (plus le prix des places pour le Gala).\n\n' ;
				conf += 'Adresse d\'envoi du chèque :\n' ;
				conf += 'Cercle des Eleves de l\'EMN\n' ;
				conf += '6, avenue Augustin-Louis Cauchy\n' ;
				conf += '44300 Nantes\n' ;
				conf += 'A l\'attention du trésorier du Gala\n\n' ;
				conf += 'Veuillez accompagner votre chèque d\'un mot comportant les informations suivantes :\n' ;
				conf += '- Numéro de réservation ('+json[ "ret" ]+')\n' ;
				conf += '- Vos nom et prénom\n\n' ;
				conf += 'Merci d\'avoir réservé ';
				if( nb_acc==0 ) conf += 'votre place'; else conf += 'vos places';
				conf += ' pour le repas du Gala !\n';
				conf += 'Rendez-vous le 21 Octobre à 19h !';
				
				alert( conf );
			} );
		}
	}
}

function convertHTML( str ) {
  str = str.replace( /&/g, "&amp;" );
  str = str.replace( />/g, "&gt;" );
  str = str.replace( /</g, "&lt;" );
  str = str.replace( /"/g, "&quot;" );
  str = str.replace( /'/g, "&#039;" );
  return str ;
}
