﻿var step = 0;
var type = null;
var stop;
var complete = false;

$( document ).ready( function(){

						$( 'form' ).submit( function(){ return complete; } );
						$( '#next,#validate' ).click( function(){
												
												switch( step ){
												
													case 0 :
														var types = $( '.type' );
														$.each( types, function( i, val ){
																			
																			if( val.checked ){
																			
																				step = 1;
																				type = parseInt( val.value );
																				$( '#step0' ).hide();
																				$( '#step1' ).show();
																				if( type == 2 ){
																				
																					$( '.hospitalier' ).css( 'display', 'inline' );
																				}
																				return;
																			}
																		} );
														return;
														
													case 1 :
														
														var inputs = $( '#step1 input' );
														stop = false;
														$.each( inputs, function( i, val ){
																			
																			if( stop ) return;

																			if( ( val.value == '' && val.name != 'travail' ) || ( val.value == '' && val.name == 'travail' && type == 2 )  ){
																				
																				stop = true;
																				alert( 'Certains champs n\'ont pas été remplis. Merci de les complèter avant de poursuivre.' );
																				return;
																			}
																		} );
														if( stop ) return;
														
														step = 2;
														$( '#step1' ).hide();
														switch( type ){
															
															case 0 :

																$( '#step2p' ).show();
																return;

															case 1 :
																$( '#step2m' ).show();
																return;
																
															case 2 :
																$( '#step2h' ).show();
																return;
														}	
														return;
																								
													case 2 :
														
														switch( type ){
														
															case 0 : 
																$( '#step2p' ).hide(); 
																$( '#next' ).hide();
																$( '#validate' ).show();
																break;
															case 1 : 
																$( '#step2m' ).hide();
																$( '#next' ).hide();
																$( '#validate' ).show();
																break;
															case 2 : 
																$( '#step2h' ).hide();
																break;
														}
														
														step = 3;
														$( '#step3' ).show(); 
														return;
														
													case 3 :
														
														$( '#step3' ).hide();
														if( type > 1 ) {
														
															step = 4;
															$( '#next' ).hide();
															$( '#validate' ).show();
															$( '#step4' ).show();
															return;
														}
														
														complete = true;
														return;
														
													case 4 :
													
														complete = true;
														return;
												}
											});
											
						$( '.priceUpdate' ).change( function() {
						
														switch( type ){
														
															case 0 :
																
																var fields = $( '#step2p .priceUpdate' );
																var total = 0;
																$.each( fields, function( i, val ){
																
																					if( val.name == 'p_voyage' ){
																					
																						switch( val.value ){
																						
																							case 'car' :
																								var price = 860;
																								break;
																							case 'avion' :
																								var price = 1150;
																								break;
																							case 'train' :
																								var price = 850;
																								break;
																						}
																						$( '#p_voyage' ).text( price );
																						total += price;
																					}
																					else if( val.name == 'p_accident' ){
																					
																						var price = 0;
																						if( val.checked )
																							var price = 14;
																							
																						$( '#p_accident' ).text( price );
																						total += price;
																					}
																					else if( val.name == 'p_annulation' ){
																					
																						var price = 0;
																						if( val.checked )
																							price = 30;
																						
																						$( '#p_annulation' ).text( price );
																						total += price;
																					}
																					else if( val.name == 'p_sup1lit' ){
																					
																						var price = 0;
																						if( val.checked )
																							price = 250;
																						
																						$( '#p_1lit' ).text( price );
																						total += price;
																					}
																					else if( val.name == 'p_hotelSup' ){
																					
																						var price = 0;
																						if( val.checked )
																							price = 200;
																						
																						$( '#p_category' ).text( price );
																						total += price;
																					}
																					
																					$( '#p_total' ).text( total );

																				} );
																return;
																
															case 1 :
																
																var fields = $( '#step2m .priceUpdate' );
																var total = 0;
																$.each( fields, function( i, val ){
																
																					if( val.name == 'm_voyage' ){
																					
																						switch( val.value ){
																						
																							case 'avion1' :
																								var price = 950;
																								break;
																							case 'avion2' :
																								var price = 1150;
																								break;
																							case 'train1' :
																								var price = 650;
																								break;
																							case 'train2' :
																								var price = 850;
																								break;

																						}
																						$( '#m_voyage' ).text( price );
																						total += price;
																					}
																					else if( val.name == 'm_annulation' ){
																					
																						var price = 0;
																						if( val.checked )
																							price = 30;
																						
																						$( '#m_annulation' ).text( price );
																						total += price;
																					}
																					else if( val.name == 'm_sup1lit' ){
																					
																						var price = 0;
																						if( val.checked )
																							price = 200;
																						
																						$( '#m_1lit' ).text( price );
																						total += price;
																					}
																					
																					$( '#m_total' ).text( total );

																				} );
																return;														

															case 2 :
																
																var fields = $( '#step2h .priceUpdate' );
																var total = 0;
																$.each( fields, function( i, val ){
																					
																					if( val.id == 'details_h_voyage' ){
																					
																						switch( parseInt( val.value ) ){
																						
																							case 1 :
																								var price = 850;
																								break;
																							case 2 :
																								var price = 850;
																								break;
																							case 3 :
																								var price = 860;
																								break;
																							case 4 :
																								var price = 1150;
																								break;
																						}
																						$( '#h_voyage' ).text( price );
																						total += price;
																					}
																					else if( val.name == 'h_accident' ){
																					
																						var price = 0;
																						if( val.checked )
																							var price = 14;
																							
																						$( '#h_accident' ).text( price );
																						total += price;
																					}
																					else if( val.name == 'h_annulation' ){
																					
																						var price = 0;
																						if( val.checked )
																							price = 30;
																						
																						$( '#h_annulation' ).text( price );
																						total += price;
																					}
																					else if( val.name == 'h_sup1lit' ){
																					
																						var price = 0;
																						if( val.checked )
																							price = 200;
																						
																						$( '#h_1lit' ).text( price );
																						total += price;
																					}
																					
																					$( '#h_total' ).text( total );

																				} );
																return;


														}
													} );
					} );


