$(document).ready( function(){  

	// alert('formcheckkkkk')  ;
	
	
	// si plusieurs formulaires , loader une seule  fois les fonctions 
	if(typeof(form_check_is_loaded) == 'undefined') { form_check_is_loaded =  true ; } else { return false ; }
	
   function no_action() { return true  ; }


//-------------------------------------------------------------------------	
 
 // setting
 if(typeof(browser)=='undefined'){ browser = 'unknow' ;}
 
 // Global Var , setting by behavior change , submit , etc ..
 var form_id ;
 var form  ;

 
// ------- BEHAVIOR --------

// date and time
$("select[id^='birth_']").change(
								 
		function()
		 {
		 form_id = '#' + $(this).parents("form").attr('id') +' ';
		 form = $(this).parents("form") ;
		 
		  var birth_day = $(form_id + '#birth_day').val();
		  var birth_month = $(form_id + '#birth_month').val();
		  var birth_year = $(form_id + '#birth_year').val();
		  
		  var birth_hour = $(form_id + '#birth_hour').val();
		  var birth_min = $(form_id + '#birth_min').val();
		    
		 if(birth_day != '' && birth_month != '' &&  birth_year != '')
		 { 
		  if(birth_day<10) {birth_day = '0'+ birth_day ;}
		  if(birth_month<10){birth_month = '0'+ birth_month ;}
		  var  birth_date =  birth_year + '-' + birth_month + '-' + birth_day ;
		  
		  // checkdate
	      // var page_check='/lab/ajax/_check_date';
		  var page_check='_check_date';
		  var date_values = new Object ; 
		  date_values.birth_day =  birth_day ;
		  date_values.birth_month =  birth_month ;
		  date_values.birth_year =  birth_year ;
		  
		  $.post( page_check , date_values  ,  	  
				  function(response)  
				  { 
					 if(response != 'ok' )
					 {
					  $(form_id + '#birth_day').val('');
					  var  birth_date = '';
					 }
				   }
				 , "text"						
			    );
		  
		 } else {
		  var  birth_date = '';
		 }
		 
		 if(birth_hour != '' && birth_min != '' )
		 {
		  if(birth_hour<10){birth_hour = '0'+ birth_hour ;}
		  if(birth_min<10) {birth_min = '0'+ birth_min ;}
		  var  birth_time =  birth_hour + ':' + birth_min  ;
		 } else {
		  var  birth_time = '';
		 }
		 
		 $(form_id + '#birth_date').val(birth_date);
		 $(form_id + '#birth_time').val(birth_time);
		 
		 
		 // si astro appellé par class Form, sette les values zodiac , chines , decan , ascendant
		 if(typeof(getAstro)=='function') {  getAstro(); }
		 
		 }
 );

 

$("*[id='submit_bt']").click( check_form ) ; // new bind jquery 1.4


// function check_form(form)
function check_form()
{	
	form = $(this).parents("form") ;
	form_id = '#' + $(this).parents("form").attr('id') +' ';
	 
	
	form_values = form.serialize() ; 
	
	var action =  form.attr('action') ;
	
	// patch horoscope_switch
	// var page_check='/lab/ajax/_check_form';
	var page_check='_check_form';
	
	$.post(  page_check ,     
			 form_values  ,  	  
			 function(response)  
			 {   
				
				
				//  return true ;
				if( response.action_after_check.on_response  != false)
				{
				  //  alert(response.statut + ' / case 1') ;
				  // return false  ;
				  
				  eval(response.action_after_check.on_response + '(form,response)') ;	
				  return true ;
				}
				
				
				if(response.statut == true )
				{    
  
				  // alert(response.statut + ' / case 2') ;
				  // return false  ;

				  if ( response.action_after_check.on_success != false  )			
				  { 	
				  eval(response.action_after_check.on_success + '(form,response)');	
				  } else {   
				  form.submit() ;	
			      }
				
				} else {
					 
				  
				  // alert(response.statut + ' / case 3') ;
				  // return false  ;
				  
				  if ( response.action_after_check.on_failure != false  )		
				  {
				  eval(response.action_after_check.on_failure + '(form,response)') ;			
				  } else {  
				    
				  highLightError(form_id , response )  ;
				  if( response.action_after_check.after_failure != false ) {eval(response.action_after_check.after_failure + '(form,response)');	}
				  }
			   }
			 }
			  , "json"						
			);
	
 
}


// alert(typeof(check_form)) ;


  function highLightError(form_id , response )
  {  
									
	var data_collection = response.data_collection ; // tout les datas retournés , trimés
    var error_collection =  response.error_collection ; // les champs a probléme
	
	element_to_ignore  = ["form_id","birth_day","birth_month","birth_year","birth_hour","birth_min","horo_agree","mailing_agree","partner_agree" ];
	
	
	//cleanage  / refresh ajax
	$(form_id + "span[id='icon_field']").remove();
	$(form_id +"div[id='error_message']").remove();
	
	$(form_id +".valideElement").removeClass("valideElement");
	$(form_id +".errorElement").removeClass("errorElement");
		  
	  
   // alert(form_id);
  
  $.each( data_collection, function(key,value)
		 { 		
			verif = error_collection[key] ;
				
			// pas trouvé dans l'objet erreur => champs ok
			if(typeof(verif) == 'undefined')  { higlight_type = 'valide'; } else { higlight_type = 'error';}
							   
			var type_field = $(form_id + '#' +  key ).attr('type');
			var id_field = $(form_id + '#' +  key ).attr('id');
			   
			var element_to_higlight  =  $(form_id + '#' +  key );  //default
			var element_to_higlight  =  $(form_id).find('#' +  key);  //default
				   
			 /* Old gender list en radio button, keep tant que nouveau not validate
			 // exception ; champs liés => ex: gender false  => higlight sur last radio gender
			 if(key == 'gender' )
			 { 
				element_to_higlight = ["gender3"];
				highlight_me (element_to_higlight , higlight_type ) ;
				return true;
			 }
			 */
				   
				 if(key == 'birth_city' ) // si champs city rempli et le nom trouvé en Db => higlight positive, sinon nada
				 { 					  
					 // if(data_collection.birth_city_id !='')
					 if(data_collection.birth_city_id !='' || typeof(error_collection.birth_city_id) != 'undefined')
					 {
					 if(typeof(error_collection.birth_city_id)!='undefined' ) {higlight_type = 'error'; }
					 element_to_higlight = ["birth_city"];
					 highlight_me (element_to_higlight , higlight_type ) ;
					 }
					 return true;
				 }
				  
				  
				
				 if(key == 'gender' )
				 { 
					 element_to_higlight = ["gender"];
					 highlight_me (element_to_higlight , higlight_type ) ;
					 return true;
				 }
				   
 
				 if(key == 'birth_date' )
				 { 
					 element_to_higlight = ["birth_year"];
					 highlight_me (element_to_higlight , higlight_type ) ;
					 return true;
				 }
				   
				    if(key == 'birth_time' )
				   { 
					   element_to_higlight = ["birth_min"];
					   highlight_me (element_to_higlight , higlight_type ) ;
					   return true;
				   }
				   
				   // champs sans highlight
				   if(type_field=='hidden' || type_field=='radio'){ return true; }  
				   if(jQuery.inArray( key, element_to_ignore ) != -1) { return true; } 
							
				   if(higlight_type == 'valide' && data_collection[key] =='' ) { return true ; }
				  
				   if($(element_to_higlight).attr('id') == 'password' ) 
				   {								
				    if(typeof(error_collection.password2) !='undefined') { higlight_type == 'error' } 								
				   } 
				  
				  highlight_me (element_to_higlight , higlight_type ) ;
   				  
			}
			);
  
  }



/* /images/img_form/ok_icon.png */ 


function highlight_me (element_to_higlight , higlight_type  )
{
			
	path_image = '/images/1/img_form/';
	

	if(typeof(browser)!='undefined' && browser == 'light'){var size_icon = " width='14' height='14' ";} else { /*var size_icon = " width='20' height='19' ";*/ var size_icon = " width='20' height='20' "; }

	var ok_icon =  "<span id='icon_field' class='icon_field'><img src='/images/img_form/ok_icon.png' alt='ok' ";  
	
	ok_icon += size_icon ;
	ok_icon += "/></span>" ;
	  
	var error_icon =  "<span id='icon_field' class='icon_field'><img src='/images/img_form/error_icon.png' alt='ok' "; 
	
	error_icon += size_icon ;
	error_icon += "/></span>" ;
	
	if(higlight_type == 'error') // pas trouvé dans l'objet erreur => champs ok
	{
	var classToAdd =  "errorElement" ;
	var check_icon =  error_icon ;
	} else {
	var classToAdd =  "valideElement" ;
	var check_icon =  ok_icon ;
	}
		
	if($.isArray(element_to_higlight)) 
		{ 
			$.each(element_to_higlight, function(key,value) 
			{
			 var element_to_higlight =  form_id + ' #' + value ;	 
			  
			// si label suit l'element , add le check_icon  à la suite du label
	        var next_is_label =  $(element_to_higlight).next('label').length ;
	        if( next_is_label != 0 ) { element_to_higlight = $(element_to_higlight).next('label') ;  }
			  
			 // $(element_to_higlight).addClass(classToAdd) ;
			 $(element_to_higlight).after(check_icon) ;
		 });

	  } else {
	  	  
	  
	  // si label suit l'element , add le check_icon à la suite du label
	  var next_is_label =  $(element_to_higlight).next('label').length ;
	  if( next_is_label != 0 ) { element_to_higlight = $(element_to_higlight).next('label') ;  }
	  
	  // element_to_higlight.addClass(classToAdd) ;
	  element_to_higlight.after(check_icon) ;
	  }
	
	
}


}); // End Document Ready