// JavaScript Document
var current_form_check;
$(document).ready( function(){  
	
$("select[id='birth_day'],select[id='birth_month'],select[id='birth_year'],select[id='birth_hour'],select[id='birth_min']").live('focus' ,auto_set_input) ;


  function auto_set_input()
  {
			 
			 switch(this.id)
			 {
				 case 'birth_day' : if( $(this).val() =='')  { $(this).val('15') ;    $(this).change();} break ;
				 case 'birth_month' : if( $(this).val() =='')  if( $(this).val() =='')  { $(this).val('6') ; $(this).change(); } break ;
				 case 'birth_year' : if( $(this).val() =='')  { $(this).val('1975') ;  $(this).change(); }  break ;
			   // case 'birth_hour' : if( $(this).val() =='0'  || $(this).val() =='')  { $(this).val('12') ;  $(this).change(); }  break ;
				 // case 'birth_min' :  if( $(this).val() =='0'  || $(this).val() =='')  { $(this).val('30') ;  $(this).change(); }  break ;
			 }
  }

 

	$('#submit_bt').die('click').live('click', check_form);

	// 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();			
		  if(birth_day<10) {birth_day = '0'+ birth_day ;}
		  if(birth_month<10){birth_month = '0'+ birth_month ;}
		  var birth_hour = $(form_id + '#birth_hour').val();
		  var birth_min = $(form_id + '#birth_min').val(); 
			
		  var  birth_date =  birth_year + '-' + birth_month + '-' + birth_day ;
			
			if($(this).parents('form').hasClass('no_check')==false){
		    
		 if(birth_day != '' && birth_month != '' &&  birth_year != '')
		 { 
		  // if(birth_day<10) {birth_day = '0'+ parseInt(birth_day) ;}
		  // if(birth_month<10){birth_month = '0'+ parseInt(birth_month) ;}
		  if(birth_day.length<2) {birth_day = '0'+ birth_day ;}
		  if(birth_month.length<2){birth_month = '0'+ birth_month ;}
		  var  birth_date =  birth_year + '-' + birth_month + '-' + birth_day ;
		  
		  // checkdate
	    var page_check='/lab/ajax/_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(); }
		 
		 }
 );
$("select[id^='meet_']").change(
							 
		function()
		 {
		 		 
		 form_id = '#' + $(this).parents("form").attr('id') +' ';
		 form = $(this).parents("form") ;
		 
		  var meet_day = $(form_id + '#meet_day').val();
		  var meet_month = $(form_id + '#meet_month').val();
		  var meet_year = $(form_id + '#meet_year').val();			
		  if(meet_day<10) {meet_day = '0'+ meet_day ;}
		  if(meet_month<10){meet_month = '0'+ meet_month ;}
			
		  var  meet_date =  meet_year + '-' + meet_month + '-' + meet_day ;
			
			if($(this).parents('form').hasClass('no_check')==false){
		    
		 if(meet_day != '' && meet_month != '' &&  meet_year != '')
		 { 
		  if(meet_day.length<2) {meet_day = '0'+ meet_day ;}
		  if(meet_month.length<2){meet_month = '0'+ meet_month ;}
		  var  meet_date =  meet_year + '-' + meet_month + '-' + meet_day ;
		  
		  // checkdate
	    var page_check='/lab/ajax/_check_date';
		  var date_values = new Object ; 
		  date_values.meet_day =  meet_day ;
		  date_values.meet_month =  meet_month ;
		  date_values.meet_year =  meet_year ;
		  
		  $.post( page_check , date_values  ,  	  
				  function(response)  
				  { 
					 /*
					 if(response != 'ok' )
					 {
						$(form_id + '#birth_day').val('');
					  var  birth_date = '';
					 }
					 */
				   }
				 , "text"						
			    );
		  
		 } else {
		  var  meet_date = '';
		 }
		 
		 
			}
		 $(form_id + '#meet_date').val(meet_date);
		 
		 
		 // si astro appellé par class Form, sette les values zodiac , chines , decan , ascendant
		 if(typeof(getAstro)=='function') {  getAstro(); }
		 
		 }
 );
$("select[id^='pbirth_']").change(
							 
		function()
		 {
		 form_id = '#' + $(this).parents("form").attr('id') +' ';
		 form = $(this).parents("form") ;
		 
		  var birth_day = $(form_id + '#pbirth_day').val();
		  var birth_month = $(form_id + '#pbirth_month').val();
		  var birth_year = $(form_id + '#pbirth_year').val();			
		  if(birth_day<10) {birth_day = '0'+ birth_day ;}
		  if(birth_month<10){birth_month = '0'+ birth_month ;}
		  var birth_hour = $(form_id + '#pbirth_hour').val();
		  var birth_min = $(form_id + '#pbirth_min').val(); 
			
		  var  birth_date =  birth_year + '-' + birth_month + '-' + birth_day ;
			
			if($(this).parents('form').hasClass('no_check')==false){
		    
				 if(birth_day != '' && birth_month != '' &&  birth_year != '')
				 { 
					// if(birth_day<10) {birth_day = '0'+ parseInt(birth_day) ;}
					// if(birth_month<10){birth_month = '0'+ parseInt(birth_month) ;}
					if(birth_day.length<2) {birth_day = '0'+ birth_day ;}
					if(birth_month.length<2){birth_month = '0'+ birth_month ;}
					var  birth_date =  birth_year + '-' + birth_month + '-' + birth_day ;
					
					// checkdate
						var page_check='/lab/ajax/_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 + '#pbirth_date').val(birth_date);
		 $(form_id + '#pbirth_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 ) ; 


// Click Enter => Trigge Click sur submit_bt
$("input").keyup(function(e) { 
  if(e.keyCode == 13) 
  {      
   if( typeof($(this).stopTime) != 'undefined')  $(this).stopTime();
   $(this).blur(); 
   
	// patch 'submit_bt' peut être en dehors du <form> => si c'est le cas cherchez  le next 'submit_bt'  aprés form 
	var bt_in_form = $(this).parents("form").find("a[id='submit_bt']").length ;
	if(bt_in_form == 0 ) 
	{
	 $(this).parents("form").next("*[id='submit_bt']").click();
	}
   return false ;
  }
});



}); // End Document Ready

function check_form( e )
{	
	e.preventDefault();
	var form = $(e.currentTarget).parents("form") ;
	current_form_check = form;
	//alert('class = '  +$(this).attr('class'));
	//alert( 'form_id => ' + $(this).parents("form").attr('id')) ;
	
	var form_id = '#' + $(form).attr('id') +' ';
	
	// $(this).remove();
	
	
	form_values = form.serialize() ; 
	if(form.attr('id')=='form_login'){
		var remember_checkbox = $('#log_pop .remember_check input');
		if(remember_checkbox.length>0){
			$.cookie('remember', true, { path: '/', expires: 7});
			$.cookie('email', form.find('#email').val(), { path: '/', expires: 7});
			$.cookie('password', form.find('#password').val(), { path: '/', expires: 7});
		}
	}
	var action =  form.attr('action') ;
	var page_check='/lab/ajax/_check_form';
	$.post(  page_check ,     
			 form_values  ,  	  
			 function(response)  
			 {  
				 
				 //  return true ;
				if( response.action_after_check.on_response  != false)
				{
				  eval(response.action_after_check.on_response + '(form,response)') ;	
				  return true ;
				}
				
				if(response.statut == true )
				{    
  
				  if ( response.action_after_check.on_success != false  )			
				  { 	
				  eval(response.action_after_check.on_success + '(form,response)') ;	
				  } else {   
						if(parseInt($('#birth_city_id',form).val())<10000){
							displayPopCarteCiel(2);
							return;
						} else 
							form.submit() ;	
			      }
				
				} else {
					 
				  if ( response.action_after_check.on_failure != false  )		
				  {
				  eval(response.action_after_check.on_failure + '(form,response)') ;			
				  } else {  
				    
				  highLightError(form , response )  ;
				  if( response.action_after_check.after_failure != false ) {eval(response.action_after_check.after_failure + '(form,response)');	}
				  }
			   }
			 }
			  , "json"						
			);
	
 
}

function check_form_without_submit(form,callback)
{	
	
	
	form_values = form.serialize() ; 
	
	var action =  form.attr('action') ;
	var page_check='/lab/ajax/_check_form';
	$.post(  page_check ,     
			 form_values  ,  	  
			 function(response)  
			 {  
				if(typeof callback!='undefined') callback();
			 }
			  , "json"						
			);
	
 
}


// alert(typeof(check_form)) ;


  function highLightError(form , 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","meet_day","meet_month","meet_year","birth_hour","birth_min","horo_agree","mailing_agree","partner_agree" ];
	
	
	//cleanage  / refresh ajax
	$("span[id='icon_field']",form).remove();
	$("div[id='error_message']",form).remove();
	
	$(".valideElement",form).removeClass("valideElement");
	$(".errorElement",form).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 = $('#'+key,form).attr('type');
			var id_field = $('#'+key,form).attr('id');
			   
			var element_to_higlight  =  $('#'+key,form);  //default
			// var element_to_higlight  =  $('#'+key,form);  //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') && error_collection.birth_city_id!="empty")
					 {
						 element_to_higlight = ["birth_city"];
						 higlight_type = 'valide';
						 highlight_me (element_to_higlight , higlight_type, form ) ;
					 } else {
						 element_to_higlight = ["birth_city"];
						 higlight_type = 'error';
						 highlight_me (element_to_higlight , higlight_type, form ) ;
					 }
					 return true;
				 }
				 if(key == 'pbirth_city' ) // si champs city rempli et le nom trouvé en Db => higlight positive, sinon nada
				 { 					  
					 // if(data_collection.birth_city_id !='')
					 if((data_collection.pbirth_city_id !='' || typeof(error_collection.pbirth_city_id) != 'undefined') && error_collection.pbirth_city_id!="empty")
					 {
						 element_to_higlight = ["pbirth_city"];
						 higlight_type = 'valide';
						 highlight_me (element_to_higlight , higlight_type, form ) ;
					 } else {
						 element_to_higlight = ["pbirth_city"];
						 higlight_type = 'error';
						 highlight_me (element_to_higlight , higlight_type, form ) ;
					 }
					 return true;
				 }
				  
				  
				
				 if(key == 'gender' )
				 { 
					 if(data_collection.gender ==false){
						 element_to_higlight = ["gender"];
						 higlight_type = 'error';
						 highlight_me (element_to_higlight , higlight_type, form ) ;					
					 } else {
						 higlight_type = 'valide';
						 element_to_higlight = ["gender"];
						 highlight_me (element_to_higlight , higlight_type, form ) ;
					 }
					 return true;
				 }
				   
 
				 if(key == 'meet_date' )
				 { 
					 element_to_higlight = ["meet_year"];
					 highlight_me (element_to_higlight , higlight_type, form ) ;
					 return true;
				 }
				   
				 if(key == 'birth_date' )
				 { 
					 element_to_higlight = ["birth_year"];
					 highlight_me (element_to_higlight , higlight_type, form ) ;
					 return true;
				 }
				   
				    if(key == 'birth_time' )
				   { 
					   element_to_higlight = ["birth_min"];
					   highlight_me (element_to_higlight , higlight_type, form ) ;
					   return true;
				   }
				   
				 if(key == 'pbirth_date' )
				 { 
					 element_to_higlight = ["pbirth_year"];
					 highlight_me (element_to_higlight , higlight_type, form ) ;
					 return true;
				 }
				   
				    if(key == 'pbirth_time' )
				   { 
					   element_to_higlight = ["pbirth_min"];
					   highlight_me (element_to_higlight , higlight_type, form ) ;
					   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' } 								
				   } 
				  switch(key){
						case 'horo_agree':
						case 'partner_agree':
						case 'mailing_agree':
						case 'horoscope':
						case 'voyance':
						case 'chance':
						case 'tarot':
						case 'psychologie':
						case 'bien_etre':
						case 'numerologie':
						case 'travail':
						case 'loisirs':
						case 'divination':
						case 'argent':
						case 'avenir':
						case 'astrologie':
						case 'amour':
						case 'spiritualite':
						case 'pbirth_day':
						case 'pbirth_month':
						case 'pbirth_year':
						case 'pbirth_hour':
						case 'pbirth_min':
							
						break;
						case 'password':
							if(form.attr('id')!='form_login')	highlight_me (element_to_higlight , higlight_type, form ) ;
						break;
						default:
							highlight_me (element_to_higlight , higlight_type, form ) ;
						break;
						
					}
   				  
			}
			);
  
  }




function highlight_me (element_to_higlight , higlight_type, form , position )
{
	if(typeof position=='undefined') position = 'after';
	path_image = CDNPATH+'1/img_form/';
	
	// debug(element_to_higlight);
	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='"+CDNPATH+"icons/ok.png' alt='ok' "; 
	
	ok_icon += size_icon ;
	ok_icon += "/></span>" ;
	  
	var error_icon =  "<span id='icon_field' class='icon_field'><img src='"+CDNPATH+"icons/error.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) 
			{
				if(value!='birth_city_id'){
				 var element_to_higlight =  '#'+value ;	 
					
				// si label suit l'element , add le check_icon  à la suite du label
						var next_is_label =  $(element_to_higlight,form).next('label').length ;
						if( next_is_label != 0 ) { element_to_higlight = $(element_to_higlight,form).next('label') ;  }
					
				 // $(element_to_higlight).addClass(classToAdd) 
				if(element_to_higlight=="#gender" || element_to_higlight=="#birth_min"  || element_to_higlight=="#birth_year" || element_to_higlight=="#pbirth_min"  || element_to_higlight=="#pbirth_year"  || element_to_higlight=="#meet_year" ){
					var parent = $(element_to_higlight,form).parent();
					if(form=='#form_mail_change' || position=='before') parent.before(check_icon);
					else	parent.after(check_icon);
				 } else {
					if(form=='#form_mail_change' || position=='before') $(element_to_higlight,form).before(check_icon) ;
					else $(element_to_higlight,form).after(check_icon) ;
				}
			}
		 });

	  } else {
	  	  
			if($(element_to_higlight,form).attr('id')!='birth_city_id'){
				// debug($(element_to_higlight,form).attr('id'));
				if($(element_to_higlight,form).attr('id')=="cgv_agree"){
					if(element_to_higlight.parent('span').length>0)
						element_to_higlight.parent('span').parent('div').before(check_icon) ;	
					else
						element_to_higlight.parent('div').before(check_icon) ;	
				} else {
					// si label suit l'element , add le check_icon à la suite du label
					var next_is_label =  $(element_to_higlight,form).next('label').length ;
					if( next_is_label != 0 ) { element_to_higlight = $(element_to_higlight,form).next('label') ;  }
				
					// element_to_higlight.addClass(classToAdd) ;
					var _id = element_to_higlight.attr('id');
					 if(_id=="gender" || _id=="birth_min"  || _id=="birth_year" || _id=="pbirth_min"  || _id=="pbirth_year"  || _id=="meet_year" ){
						var parent = $(element_to_higlight,form).parent();
						if(form=='#form_mail_change' || position=='before') parent.before(check_icon);
						else if(position=='append') parent.append(check_icon);
						else	parent.after(check_icon);
					 } else {
						if(form=='#form_mail_change' || position=='before') element_to_higlight.before(check_icon);
						else if(position=='append') element_to_higlight.parent().append(check_icon);
						else	element_to_higlight.after(check_icon);
					}
				}
		}
	  }
	
	
}

function restart_after_ncity(ncid){
	$('#birth_city_id',current_form_check).val(ncid);
	$('#submit_bt',current_form_check).click();
}


