﻿function ctlUserCredentialsPopUp() 
{
    var This = this;
    var Container; // Container is "userCredentialsDiv"

    var mainTable;
    var userNameControl;
    var emailControl;
    var okButtonImage;

    var OkCallBackMethod;
    var warningMessageDescription;

    this.Init = function(cont) 
    {
        Container = cont;
        This.Container = Container;
        
        mainTable = document.createElement('table');
        mainTable.cellSpacing = "0";
        mainTable.cellPadding = "0";
        
        var headerRow = mainTable.insertRow(-1);
        //headerRow.className = 'userCredentialsHeaderStyle';
        
        // Header -------------------------------------------
        var headerMainCell = headerRow.insertCell(-1);
        var headerTable = document.createElement('table');
        headerTable.cellSpacing = "0";
        headerTable.cellPadding = "0";
        headerMainCell.appendChild(headerTable);
        
        var headerInternalTableRow = headerTable.insertRow(-1);
        
        var headerLeftCornerCell = headerInternalTableRow.insertCell(-1);
        headerLeftCornerCell.className = 'userCredentialsLeftCornerHeaderStyle';
        
        var headerCell = headerInternalTableRow.insertCell(-1);
        headerCell.className = 'userCredentialsHeaderStyle';
        
        var headerRightCornerCell = headerInternalTableRow.insertCell(-1);
        headerRightCornerCell.className = 'userCredentialsRightCornerHeaderStyle';
        
        var headerDescription = document.createElement('span');
        headerDescription.innerHTML = MSG("WC_LOGIN");
        headerCell.appendChild(headerDescription);
        // End Header ----------------------------------------
        
        var bodyRow = mainTable.insertRow(-1);
        var bodyCell = bodyRow.insertCell(-1);
        bodyCell.style.height = '115px';        
        
        var bodyTable = document.createElement('table');
        bodyTable.className = 'credentialBodyTableStyle';
        
        var userNameRow = bodyTable.insertRow(-1);        
        var userNameLabelCell = userNameRow.insertCell(-1);
        var userNameLabelDescription = document.createElement('span');
        userNameLabelDescription.innerHTML = 'Name';
        userNameLabelCell.appendChild(userNameLabelDescription);
        var userNameControlCell = userNameRow.insertCell(-1);
        userNameControl = document.createElement('input');
        userNameControlCell.appendChild(userNameControl);
        
        var emailNameRow = bodyTable.insertRow(-1);
        var emailLabelCell = emailNameRow.insertCell(-1);
        var emailLabelDescription = document.createElement('span');
        emailLabelDescription.innerHTML = 'Email';
        emailLabelCell.appendChild(emailLabelDescription);
        var emailControlCell = emailNameRow.insertCell(-1);
        emailControl = document.createElement('input');
        emailControl.onkeypress = OnEmailKeyPressed;
        emailControlCell.appendChild(emailControl);
                
        var footerRow = bodyTable.insertRow(-1);
        footerRow.className = 'userCredentialsFooterStyle';
        var footerCell = footerRow.insertCell(-1);
        footerCell.colSpan = 2;
        okButtonImage = document.createElement('img');
        okButtonImage.src = 'images/send-request-blue-button-dark.png';
        okButtonImage.onclick = OnCredentialsOK;
        okButtonImage.onmouseover = OnOkButtonMouseOver;
        okButtonImage.onmouseout = OnOkButtonMouseOut;
        footerCell.appendChild(okButtonImage);
        
        var warningMessageRow = bodyTable.insertRow(-1);
        warningMessageRow.className = 'credentialWarningMessageStyle';
        var warningMessageCell = warningMessageRow.insertCell(-1);
        warningMessageCell.colSpan = 2;
        warningMessageDescription = document.createElement('span');
        warningMessageDescription.innerHTML = '';
        warningMessageCell.appendChild(warningMessageDescription);

        bodyCell.appendChild(bodyTable);
        Container.appendChild(mainTable);

        var popupBlockerTest = window.open("about:blank", "", "directories=no,height=10,width=10,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=no,top=0,location=no");
        if (!popupBlockerTest || popupBlockerTest.closed || parseInt(popupBlockerTest.innerWidth) == 0) {
            //SetWarningText(MSG("WC_POPUP"));
        } else {
            popupBlockerTest.close();
        }
    }

    this.Show = function(responseMethod) 
    {
        // open the login pop up and keeps a reference to the method that must be called when user 
        // press ok/login button
        if (Container != null && userNameControl != null) 
        {
            OkCallBackMethod = responseMethod;
            Container.style.display = '';
            userNameControl.focus();
        }
        
        CheckQueueAvailability();
    }
    
    function OnOkButtonMouseOver()
    {
        if (okButtonImage != null)
        {
            okButtonImage.src = 'images/send-request-blue-button-light.png';
        }
    }
    function OnOkButtonMouseOut()
    {
        if (okButtonImage != null)
        {
            okButtonImage.src = 'images/send-request-blue-button-dark.png';
        }
    }

    function OnEmailKeyPressed(e)
    {
        if (!e)
            e = window.event;

        if (e.keyCode == 13) 
        {
            OnCredentialsOK();
        }
    }

    function OnCredentialsOK()
    {
        if (userNameControl != null && emailControl != null) {

            if (userNameControl.value != '' && emailControl.value != '') 
            {

                if (!ValidateEmail(emailControl.value)) 
                {
                    SetWarningText(MSG("WC_INVALIDEMAIL"));
                    emailControl.focus();
                } 
                else 
                {
                    Hide();
                    // Executes the method passed as parameter on the "this.Show" method
                    OkCallBackMethod(userNameControl.value, emailControl.value);
                }
            }
            else 
            {
                    //  TODO: Message for empty username or email
                if (userNameControl.value == '') 
                {
                    userNameControl.focus();
                }
                else 
                {
                    if (emailControl.value == '') 
                    {
                        emailControl.focus();
                    }
                }
            }
        }
    }

    function ValidateEmail(emailaddress) {
        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
        return reg.test(emailaddress);
    }

    function Hide()
    {
        if (Container != null) 
        {
            Container.style.display = 'none';
        }
    }

    function CheckQueueAvailability() 
    {
       // Identify if queue is available/opened
       var queues = GetOfficesArrayFromConfig();
        if (queues != null && queues.length > 0) 
        {
            var queueFound = false;
            for (idx = 0; idx < queues.length; idx++) 
            {
                if (CURRENT_QUEUE_NAME == queues[idx].GetKey()) 
                {
                    queueFound = true;
                    PostHttpResponse(ROOT + "Chat.asmx/GetChatQueueByAddress", "WebServiceLocation=" + queues[idx].GetValue() + "&queueAddress=" + queues[idx].GetKey(), CheckQueueAvailabilityResponse, CheckQueueAvailabilityError);
                    break;
                }
            }

            if (!queueFound)
            {
                SetWarningText(MSG("WC_QNF",{QUEUE_NAME:CURRENT_QUEUE_NAME}));
            }
        }
        else 
        {
            SetWarningText(MSG("WC_ERR01"));
        }
    }

    function CheckQueueAvailabilityResponse(json) 
    {
        if (json != null && json.Queue != null) 
        {
            if (json.Queue.length > 0) 
            {
                var queueItem = json.Queue[0];
                if (queueItem.IsQueueClosed) 
                {
                    DisableControls();
                    if (queueItem.AnnounceMessage != null && queueItem.AnnounceMessage != '') SetWarningText(MSG(queueItem.AnnounceMessage));
                    else SetWarningText(MSG("WC_ERR02"));
                }
            }
            else 
            {
                DisableControls();
                SetWarningText(MSG("WC_QND",{QUEUE_NAME:CURRENT_QUEUE_NAME}));
            }
        }
        else
        {
            removeAllChildren(Container);
            alert(MSG("WC_NOCON"));
        }
    }

    function CheckQueueAvailabilityError(err) 
    {
        alert(MSG(err));
    }

    function SetWarningText(txt) 
    {
        if (warningMessageDescription != null) 
        {
            warningMessageDescription.innerHTML = txt;
        }
    }

    function DisableControls() 
    {
        userNameControl.className = 'controlDisabled';
        userNameControl.disabled = true;

        emailControl.className = 'controlDisabled';
        emailControl.disabled = true;
        emailControl.onkeypress = null;
        
        okButtonImage.src = 'images/send-request-gray-button.png';
        okButtonImage.onclick = null;
        okButtonImage.onmouseover = null;
        okButtonImage.onmouseout = null;
    
    }
}

