﻿var oPushButtonRetour;
var oPushButtonValid;
var ButtonMaskForm;
var ButtonSendForm;
var oPushButtonFermePopup;

function initPopup()
{
    var popupBody = "<script>function back(){window.location='GenericHome.aspx';} ";
    popupBody += "</script>";
    
    popupBody += "<table id='contentTableLogin' style='width:100%; height:140px; font-family:Calibri;' class='yui-skin-sam'>";
    popupBody += "<tr height='80'>";
    popupBody += "<td colspan='3' style='padding-top:20px;'>";
    popupBody += "<span id='spanContent'>";
    popupBody += "<img src='Style/Images/wait.gif' style='width:20px; height:20px;'/>";
    popupBody += "</span>";
    popupBody += "</td>";
    popupBody += "</tr>";
    popupBody += "<tr>";
    popupBody += "<td colspan='3'>";
    popupBody +=" <a id='linkShowLostForm' style='font-size:12px; padding-top:2px;' onclick='javascript:showLostForm();'>Mot de passe perdu</a>";
    popupBody += "</td>";
    popupBody += "</tr>";
    popupBody += "<tr>";
    popupBody += "<td style='text-align:right; width:50%;'>";
    popupBody += " <div id='containerBouton'></div>";
    popupBody += "</td>";
    popupBody += "<td style='text-align:left; width:50%;'>";
    popupBody += " <div id='containerBouton2'></div>";
    popupBody += "</td>";
    popupBody += "</tr>";
    popupBody += "</table>"; 
    
    popupBody += "<table id='contentLostPwd' style='width:100%; height:140px;  display:none; font-family:Calibri;' class='yui-skin-sam'>";    
    popupBody += "<tr>";
    popupBody += "<td colspan='3'>";
    popupBody += "<span>Veuillez saisir votre mail et votre identifiant afin que nous puissions vous renvoyer un nouveau mot de passe. ";    
    popupBody += "</span>";
    popupBody += "</td>";
    popupBody += "</tr>";
    popupBody += "<tr>";
    popupBody += "<td style='width:20%;'>";
    popupBody += " Mail";
    popupBody += "</td>";
    popupBody += "<td style='width:80%;'>";
    popupBody += "<input type='text' id='mailLostForm'/>";
    popupBody += "</td>";
    popupBody += "</tr>";
    popupBody += "<tr>";
    popupBody += "<td style='width:20%;'>";
    popupBody += "Identifiant";
    popupBody += "</td>";
    popupBody += "<td style='width:80%;'>";
    popupBody += "<input type='text' id='loginLostForm'/>";
    popupBody += "</td>";
    popupBody += "</tr>";
    popupBody += "<tr>";
    popupBody += "<td colspan='3'>";
    popupBody += "<span id='mailResult'>";    
    popupBody += "</span>";
    popupBody += "</td>";
    popupBody += "</tr>";
    popupBody += "<tr style='padding-top:5px;'>";
    popupBody += "<td style='text-align:right; width:50%;'>";
    popupBody += " <div id='maskLostForm'></div>";
    popupBody += "</td>";
    popupBody += "<td style='text-align:left; width:50%;'>";
    popupBody += " <div id='sendLostForm'></div></td>";
    popupBody += "</tr>";
    popupBody += "</table>"; 
              	
	dialogLogin.setTitle('Identification requise');	
	dialogLogin.setContent(popupBody);	
	
	oPushButtonRetour = new YAHOO.widget.Button({label:'Annuler',id:'retour',container:'containerBouton' });
    oPushButtonValid = new YAHOO.widget.Button({label:'Connexion',id:'valider',container:'containerBouton2'});            
    
    ButtonMaskForm = new YAHOO.widget.Button({label:'Retour',id:'maskForm',container:'maskLostForm' });
    ButtonSendForm = new YAHOO.widget.Button({label:'Envoyer',id:'sendForm',container:'sendLostForm'});            
}

function initPopupWait()
{
    var popupBody = "";
    popupBody += "<table style='width:100%; font-family:Calibri;' class='yui-skin-sam'>";
    popupBody += "  <tr height='30px'>";
    popupBody += "      <td style='text-align:center;'>";
    popupBody += "          <div style='text-align:center; font-family:calibri;'>";
    popupBody += "              Pratique, facile, sécurisé et rapide: gagnez du temps en rechargeant votre badge, en quelques minutes, à l’aide des bornes de rechargement à l’entrée de votre restaurant";
    popupBody += "          </div>";
    popupBody += "      </td>";
    popupBody += "  </tr>";
    popupBody += "  <tr>";
    popupBody += "      <td style='text-align:center; width:100%; padding-top: 20px;'>";
    popupBody += "          <div id='echarBouton'></div>";
    popupBody += "      </td>";    
    popupBody += "  </tr>";
    popupBody += "</table>";   
    
    oPushButtonFermePopup = new YAHOO.widget.Button({label:'Fermer',id:'fermeEchar',container:'echarBouton' });         
	
    dialogEcharWait.setTitle('eChargement');	
    dialogEcharWait.setContent(popupBody);	
}

function showEcharPopup()
{    
    dialogEcharWait.setVisible(true);	
    $(".modal-dialog").css("opacity", "1");
    $(".modal-dialog").css("height", "auto");
    $(".modal-dialog-content").css("height", "auto");
}

function hideEcharPopup()
{
    $(".modal-dialog").css("opacity", 0);
    dialogEcharWait.setVisible(false);	
}

function controlEmailValide(mail)     
{
   var test= mail;
   var arroIndex=0;
   var pointIndex=0;
   for(var k = 0; k < test.length;k++){
   var c = test.substring(k,k+1);
       if(c == "@"){
         arroIndex=k; 
       } 
       if(c=="."){
         pointIndex=k;
       }        
   }
   if(arroIndex>0 && pointIndex>arroIndex && test.length>(pointIndex+1))
     return true;  
   else       
    return false;
}

function processSendMail()
{
    var mail = $("#mailLostForm").attr("value");
    var login = $("#loginLostForm").attr("value");
    if(controlEmailValide(mail) == true)
    {
        $("#mailResult").html("Vérification du mail en cours ...");
        $("#mailResult").css("color", "red");
        $.post("ajaxUtil.aspx",{mail:mail,param:"resetPwd", login:login},
		    function success(data)
		    {												
			    if(data == "OK")
			    {
			        $("#mailResult").html("Nouveau mot de passe envoyé.");
			        $("#mailResult").css("color", "green");   
			        setTimeout(functionMaskForm, 2000);
			    }
			    else if(data == "NOK1"){
			        $("#mailResult").html("Ce mail ne correspond à aucun utilisateur.");
			        $("#mailResult").css("color", "red");   
			    }
			    else if(data == "NOK2"){
			        $("#mailResult").html("Echec de l'envoi du mot de passe.");
			         $("#mailResult").css("color", "red");   
			    }
		    }
	    );	
	}
	else{
	    $("#mailResult").html("Veuillez saisir un email valide.");
	    $("#mailResult").css("color", "red");   
	}
}

function functionMaskForm()
{
    $("#contentTableLogin").css("display", "");    
    $("#contentLostPwd").css("display", "none");
    $("#mailResult").html("");
    $("#mailLostForm").attr("value", "");
    $("#loginLostForm").attr("value", "");
}

function showLostForm()
{
    $("#contentTableLogin").css("display", "none");    
    $("#contentLostPwd").css("display", "");
    $("#mailResult").html("");
    $("#mailLostForm").attr("value", "");
    $("#loginLostForm").attr("value", "");
}

function showPopupLogin(i)
{   
    dialogLogin.setVisible(true);
    CallLoginFormWithAjax();   	
    $(".modal-dialog").animate({
        opacity: 1               
        }, 2500, function() {
     });      
     $("#goto").attr("value", i);     
}

function showPageLogin(i)
{                
    CallLoginFormWithAjax();       
    $("#goto").attr("value", i);     
}

function callHideLoginPopup()
{    
    $(".modal-dialog").animate({
        opacity: 0
        }, 2500, function() {
        dialogLogin.setVisible(false);	
     });
}

function SubmitFormLogin()
{
    $("#GlobalFormAdoria").submit();
}

function CallLoginFormWithAjax()
{
    $("#spanContent").html("<img src='Style/Images/wait.gif' style='width:20px; height:20px;'/>"); 
    $.post("login.aspx",{},
		function success(data)
		{												
			$("#spanContent").html(data);	

			if(oPushButtonRetour == null)// || $("#containerBouton").html().indexOf("retour") <= 0)
			    oPushButtonRetour = new YAHOO.widget.Button({label:'Annuler',id:'retour',container:'containerBouton' });
  
			if(oPushButtonValid == null)// || $("#containerBouton").html().indexOf("retour") <= 0)
                oPushButtonValid = new YAHOO.widget.Button({label:'Connexion',id:'valider',container:'containerBouton2'});
		}
	);	
}

function CallLoginFormWithAjaxArguments()
{
    $("#spanContent").html("<img src='Style/Images/wait.gif' style='width:20px; height:20px;'/>"); 
    var desti = $("#dest").attr("value");
    $.post("login.aspx",{dest:desti},
		function success(data)
		{													
			$("#spanContent").html(data);		        
		}
	);	
}

function ShowPopupToRedirectionAndHideForClient(url)
{
    $("#dest").attr("value", url);           
    $("#spanContent").html("<img src='Style/Images/wait.gif' style='width:20px; height:20px;'/>"); 
    CallLoginFormWithAjaxArguments();    
    showPopupLogin(0);
}

