var _loginDivId = '_ajaxLogin';
var _loginTitleDivId = '_ajaxLoginTitle';
var _loginEmailId = '_txtAjaxLoginEmail';
var _loginPasswordId = '_txtAjaxLoginPassword';
var _loginActionId = '_hiddenAjaxAction';

function GetAjaxLoginHTML(title, emailLabel, passwordLabel, okLabel, cancelLabel, okAction)
{
    var loginHTML = ' \
        <div id="_ajaxLoginTitle" class="sessionExpired">' + title + '</div> \
        <div class="entry"> \
            <div class="email"> \
                <label for="_txtAjaxLoginEmail">' + emailLabel + '</label> \
                <input type="text" id="_txtAjaxLoginEmail" onkeydown="IsEnterKey(event);"/> \
            </div> \
            <div class="password"> \
                <label for="_txtAjaxLoginPassword">' + passwordLabel + '</label> \
                <input type="password" id="_txtAjaxLoginPassword" onkeydown="if(IsEnterKey(event)){AjaxLogin(); return false;}"/> \
            </div> \
        </div> \
        <input id="_hiddenAjaxAction" type="hidden"  value="' + okAction + '"/> \
        <div class="btns"> \
          <a class="secondary" href="#" onfocus="RmvBrd(this);" onclick="HideAjaxLogin();return false;" tabindex="-1">' + cancelLabel + '</a> \
          <a  class="primary" href="#" onfocus="RmvBrd(this);" onclick="AjaxLogin();return false;" tabindex="-1">' + okLabel +'</a> \
        </div> \
';

    return loginHTML;
}

//creates nthe login div based on data supplied from server and shows it to the user
function ShowAjaxLogin(title, emailLabel, passwordLabel, okLabel, cancelLabel, defaultEmailValue, okAction)
{
    var dialogDiv = document.getElementById(_loginDivId);
    var isLoginDialogExists = dialogDiv!=null;
    //if can't find it we load it from the server
    if(!isLoginDialogExists)
    {
        var loginHTML = GetAjaxLoginHTML(title, emailLabel, passwordLabel, okLabel, cancelLabel, okAction);
        dialogDiv = document.createElement('div');
        dialogDiv.innerHTML = loginHTML;
        dialogDiv.className = 'dialog loginDialog';
        dialogDiv.id = _loginDivId
        
        document.body.appendChild(dialogDiv);
        
    }
    else //if item was found, we refresh the message
    {
        //updates title
        var titleDiv = document.getElementById(_loginTitleDivId);
        if (titleDiv!=null)
            titleDiv.innerText=title;
           
        //updates action
        var actionField = document.getElementById(_loginActionId);
        if (actionField!=null)
            actionField.value=okAction;
    }
    
    //if email was specified and dialog doesn't have an email, we set it as the default
    if (defaultEmailValue!='' && document.getElementById(_loginEmailId).value=='')
        document.getElementById(_loginEmailId).value=defaultEmailValue;
    
    HideAllDialogs();
    ShowDialog(_loginDivId);
    
    document.getElementById(_loginEmailId).select();
}

//logs the user into the system again
function AjaxLogin() 
{
    var email = document.getElementById(_loginEmailId).value;
    var password = document.getElementById(_loginPasswordId).value;
    var action = document.getElementById(_loginActionId).value;
    
    var serverRequest = 'login|' + email + '|' + password + '|' + action;
    Get(serverRequest,"");
}
//hides the ajax login dialog
function HideAjaxLogin() 
{
    document.getElementById(_loginPasswordId).value="";
        
    HideDialog(_loginDivId);    
}

