// the_scripts.js

function fn_init_simul()
{
   init_user_dlg() ;
}

var array_errmsg = new Array(); 
var array_errnames = new Array();
array_errmsg[1] = "doit être une valeur numérique" ;
array_errmsg[2] = "doit être une valeur entière" ;
array_errnames["Input_NbMois"] = "Le nombre de mois";
array_errnames["Input_NbJoursParMois"] = "Le nombre de jours par mois" ;
array_errnames["Input_FacturationParJour"] = "La facturation journalière" ;
array_errnames["Input_PourcentageFraisGestion"] = "La pourcentage de frais de gestion" ;

function test_integer(input_name)
{
   var intval, txt, input_name2 ;
   
   input_name2 = input_name.replace("Adv_", "_") ;
   
   intval = new Number($the_jquery("#" + input_name).val()) ;
   if (isNaN(intval))
   {
     alert (array_errnames[input_name2] + "\n" + array_errmsg[1]) ;
     $the_jquery("#" + input_name).focus() ;
     return 1 ;
   }
   if (intval <= 0)
   {
     alert (array_errnames[input_name2] + "\n" + array_errmsg[2]) ;
     $the_jquery("#" + input_name).focus() ;
     return 1 ;
   }
   if (intval != Math.round(intval))
   {
     alert (array_errnames[input_name2] + "\n" + array_errmsg[2]) ;
     $the_jquery("#" + input_name).focus() ;
     return 1 ;
   }
   return 0 ;
}

function validateFormInput(formref)
{
   var brc, obj, txt, intval, err ;
   
   brc = false;
   
   // vérificaton valeurs
   if (test_integer("Input_NbMois") != 0) 
   {
      return brc ;
   }
   if (test_integer("Input_NbJoursParMois") != 0) 
   {
      return brc ;
   }
   intval = new Number($the_jquery("#Input_NbJoursParMois").val()) ;
   if (intval > 22) 
   {
      alert ("le nombre de jours doit être compris entre 1 et 22") ;
      $the_jquery("#Input_NbJoursParMois").focus()
      return false ;
   }
   if (test_integer("Input_FacturationParJour") != 0) 
   {
      return brc ;
   }
   if (test_integer("Input_PourcentageFraisGestion") != 0) 
   {
      return brc ;
   }

/*   
   $the_jquery("#the_user").val(the_user);
   $the_jquery("#Advthe_user").val(the_user);
   
   if (the_user == "")
   {
      frm_to_validate = formref ;
      $the_jquery( "#dialog-form" ).dialog( "open" );
      return brc ;
   }
*/  
   brc = true ;
   return brc ;
}


function validateFormInput2(formref)
{
   var brc, obj, txt, intval, err ;
   
   brc = false;
   
   // vérificaton valeurs
   if (test_integer("InputAdv_NbMois") != 0) 
   {
      return brc ;
   }
   if (test_integer("InputAdv_NbJoursParMois") != 0) 
   {
      return brc ;
   }
   intval = new Number($the_jquery("#InputAdv_NbJoursParMois").val()) ;
   if (intval > 22) 
   {
      alert ("le nombre de jours doit être compris entre 2 et 22") ;
      $the_jquery("#InputAdv_NbJoursParMois").focus()
      return false ;
   }
   if (test_integer("InputAdv_FacturationParJour") != 0) 
   {
      return brc ;
   }
   if (test_integer("InputAdv_PourcentageFraisGestion") != 0) 
   {
      return brc ;
   }
/*   
   $the_jquery("#the_user").val(the_user);
   $the_jquery("#Advthe_user").val(the_user);
   
   if (the_user == "")
   {
      frm_to_validate = formref ;
      $the_jquery( "#dialog-form" ).dialog( "open" );
      return brc ;
   }
*/   
   brc = true ;
   
   return brc ;
}

function init_user_dlg()
{
   $the_jquery( "#dialog-form" ).dialog({
			autoOpen: false,
			height: 300,
			width: 440,
			modal: true,
			buttons: {
				"Connexion": function() {
					var bValid = true;
					allFields.removeClass( "ui-state-error" );

					bValid = bValid && checkLength( email, "mail", 6, 80 );
					bValid = bValid && checkLength( password, "mot de passe", 3, 20 );

					// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
					bValid = bValid && checkRegexp( email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "exemple de mail : info@portage-si.fr" );

					if ( bValid ) 
          {
					   if (test_id(email, password) == true)
					   {
						    $the_jquery( this ).dialog( "close" );
						 }
						 else
						 {
						    password.val('') ;
             }
					}
				},
				"Aller à la page d'inscription": function() 
        {
           location.href='https://bureau.portage-si.fr/create_account.php';
				},
				"Annuler": function() {
					$the_jquery( this ).dialog( "close" );
				}
			},
			close: function() 
      {
        $the_jquery.unblockUI() ;
				allFields.val( "" ).removeClass( "ui-state-error" );
			}
		});
		
		email = $the_jquery( "#email" ),
		password = $the_jquery( "#password" ),
		allFields = $the_jquery( [] ).add( email ).add( password ) ;
		tips = $the_jquery( ".validateTips" );
}

var fn_id_get_result = "" ;
var resp_id_valide ;
var email, password , allFields, tips ;
var frm_to_validate ;

function test_id(mail, pw)
{
   var rtn = false ;
   $the_jquery.blockUI({ css: { backgroundColor: '#f00', color: '#fff'} });
   resp_id_valide = false ;
   fn_id_get_result = "" ;
   $the_jquery.ajax({url :"id_user.php?u=" + the_user2,
      success : function(data, textStatus, jqXHR){fn_id_get_ok(data, textStatus, jqXHR);} ,
      complete : function(jqXHR, textStatus){fn_ndf_id_complete(jqXHR, textStatus);},
      error : function(jqXHR, textStatus, errorThrown){fn_id_get_ko(jqXHR, textStatus, errorThrown);},
      dataType : "json",
      data : {m: mail.val(), p: pw.val()} ,
      type : "POST" ,
      async : false 
   }) ;
   
   //alert(resp_id_valide['error']) ;
   if (resp_id_valide['error'] == 'OK')
   {
      the_user = resp_id_valide['id'] ;
      $the_jquery("#the_user").val(the_user);
      $the_jquery("#Advthe_user").val(the_user);
      rtn = true ;
      frm_to_validate.submit() ;
   }
   else
   {
      alert ("Mail ou mot de passe invalide") ;
      the_user = "" ;
      $the_jquery.unblockUI() ;
   }
   return rtn ;
}

function checkLength( o, n, min, max )
{
	if ( o.val().length > max || o.val().length < min ) 
  {
		o.addClass( "ui-state-error" );
		updateTips( "La longueur du " + n + " doit être entre " +
			min + " et " + max + "." );
		return false;
	} else {
		return true;
	}
}

function checkRegexp( o, regexp, n ) 
{
	if ( !( regexp.test( o.val() ) ) ) 
  {
		o.addClass( "ui-state-error" );
		updateTips( n );
		return false;
	} else {
		return true;
	}
}

function updateTips( t ) 
{
			tips
				.text( t )
				.addClass( "ui-state-highlight" );
			setTimeout(function() {
				tips.removeClass( "ui-state-highlight", 1500 );
			}, 500 );
}

function fn_id_get_ok(data, textStatus, jqXHR)
{
  //alert ("fn_ndf_get_OK") ;
  resp_id_valide = data ;
  fn_id_get_result = fn_id_get_result + "fn_id_get_OK ** " + textStatus ;
}

function fn_id_get_ko(jqXHR, textStatus, errorThrown)
{
  //alert ("fn_ndf_get_KO " + textStatus) ;
  fn_id_get_result = fn_id_get_result + "fn_id_get_KO " + textStatus + " **";
}

function fn_id_get_complete(jqXHR, textStatus)
{
  $the_jquery.unblockUI() ;
  //alert(fn_ndf_get_result) ;
  //alert(textStatus) ;
}

// EOF the_scripts.js
