var homeUrl = '';
var ajaxUrl = '';

function deconnexion_internaute(type){
  jQuery.getJSON(ajaxUrl + 'systeme_1/scripts/deconnexion.php', { connType:type }, function(data){ 
    window.location.href = homeUrl;
    if (type == 'prescripteur')
    	window.opener.location.href = window.opener.location.href;
    	fenetreClose("fencentEspP");    	
  });
}

(function($){
  
  $.fn.buildDecoLink = function(){
    return this.each(function(i){
      if ( $(this).hasClass('decoBuilt') ) return;
      
      var type = this.id.replace('deco_', '');
      
      var text = $(this).html();
      $(this)
       .html('<a href="#" class="decoLink">'+text+'</a>')
        .addClass('decoBuilt')
         .click(function(e){ 
		   e.preventDefault(); 
		   deconnexion_internaute(type); 
         });
	});
  };
  
})(jQuery);

jQuery(document).ready(function($){
  var redirColoriste    = 'index.php?idrub=127&idmeta=76&idlang=1&idsite=1';
  var redirPrescripteur = 'index.php';
  
  function connexion(type){
    if ( !check(type) ) return false;
    
    var params = {};
	params.connType  = type.toLowerCase();
	params.login = $("form[name='form"+type+"'] input[name='str_login']").val();
	params.pwd   = $("form[name='form"+type+"'] input[name='str_pwd']").val();
	params.keep  = ( $("form[name='form"+type+"'] input[type='checkbox'][name='chk_pwd']").attr("checked") );
    
    $.getJSON(ajaxUrl+'systeme_1/scripts/connexion.php', params, onDone);
  }
  function check(type){
    if ( type == "deconnexion" ) return true;
    if ( type != "Coloriste" && type != "Prescripteur" ) return false;
    
    var login = $("form[name='form"+type+"'] input[name='str_login']");
    var pwd   = $("form[name='form"+type+"'] input[name='str_pwd']");
    
	login.val( $.trim(login.val()) );
	pwd.val( $.trim(pwd.val()) );
    
    var message = "";
    if ( login.val() == "" ) message+= "Vous devez saisir votre login.\n";
    if ( pwd.val()   == "" ) message+= "Vous devez saisir un mot de passe.\n";
    
	if ( message ){
      alert(message);
      return false;
    }
    
	return true;
  }
  function onDone(data){  
	  
    switch(data.message){
      case "OK": 
	    if      ( data.type=='coloriste' )    window.location.href = homeUrl+redirColoriste; 
	    else if ( data.type=='prescripteur' ) {
	    	fenetreCent(homeUrl+'index.php?idrub=309&idmeta=308&idlang=1&idsite=1','fencentEspP',955,600,'menubar=no,scrollbars=yes,statusbar=no,resizable=yes');
	    	window.location.href = homeUrl+redirPrescripteur;
	    }
	    else                                  window.location.href = homeUrl; 
		break;
      case "KO":
        alert("Votre Login ou Mot de passe est incorrect!"); 
        break;
    }
  }
  
  $("span.deco").buildDecoLink();
  
  $("form[name='formColoriste']").keypress(function(e){ if ( e.keyCode == 13 ) {return connexion('Coloriste');} });
  $("form[name='formColoriste']").find("a#val_formColoriste").click(function(e){ e.preventDefault(); return connexion('Coloriste'); });
  
  $("form[name='formPrescripteur']").keypress(function(e){ if ( e.keyCode == 13 ) return connexion('Prescripteur'); });
  $("form[name='formPrescripteur']").find("a#val_formPrescripteur").click(function(e){ e.preventDefault(); return connexion('Prescripteur'); });
  
});




