/**
 * Displays the invitation form
 */
function showInvitationForm() {
    trackRequest("INVITATION_CLICKED");
    var popupPanel = document.getElementById('popupPanel');
    if(popupPanel) {
        // title
        var popupPanelTitle = document.getElementById('popupPanelTitle');
        popupPanelTitle.innerHTML = "Invitations";
        // close button
        preparePopupCloseButton();
        // body
        var popupPanelBody = document.getElementById('popupPanelBody');
        popupPanelBody.innerHTML = prepareInvitationHTML();
        hideGraph();
        // set overlay, to avoid any other user interaction
        overlay(true);
        // display popup panel
        popupPanel.className = 'registrationPanel';
        // focus field
        focus('email');
    }
}

/**
 * Returns inner html to be displayed for invitation
 */
function prepareInvitationHTML() {
    var invitationText = "<form onsubmit='return false;'>" +
    " <table class='normaltext'>" +
    "   <tr><td colspan='2'><div style='visibility:hidden;' id='errormessage'><span class='errorsmalltext'>&nbsp;</span></div></td></tr>" +
    "   <tr><td colspan='2'><hr class='ruler' width='485px' /></td></tr>" +
    "   <tr>" +
    "     <td colspan='2'>" +
    "       To invite other people, type their email addresses (separated by commas) & add a note to the invitation(optional) ." +
    "     </td></tr>" +
    "   <tr><td colspan='2'><hr/></td></tr>" +
    "   <tr>" +
    "     <td class='smalltext'>Email:</td>" +
    "     <td><input tabindex='1' type='text' class='verybigtextbox' name='email' id='email' value='' onkeypress='submitInvitation(event);' /></td>" +
    "   </tr>" +
    "   <tr>" +
    "     <td class='smalltext'>Note:</td>" +
    "     <td><textarea tabindex='2' class='verybigtextarea' name='notes' id='notes' rows='5'>I've been using TalentGraphz and thought you might like to try it out. Here's an invitation to create an account.</textarea></td>" +
    "   </tr>" +
    "   <tr>" +
    "     <td colspan='2'><hr/></td>" +
    "   </tr>" +
    "   <tr>" +
    "     <td colspan='2' align='right'>" +
    "       <input tabindex='3' type='button' class='proformaButton' title='Invite Friend' value='Invite' onclick='invite();' />&nbsp;&nbsp;" +
    "       <input tabindex='4' id='cancel' type='button' class='proformaButton' title='Cancel' value='Cancel' onclick='cancelInvite();' />" +
    "     </td></tr>" +
    " </table>" +
    "</form>";

    return invitationText;
}

/**
 * Checks if the data is proper for invitation. If the
 * information is not proper, display the same form, with
 * an error message.
 * Otherwise, the request should be submitted
 */
function invite() {
    var emailId = document.getElementById('email');
    var errorMessage = "";

    if(!emailId || trim(emailId.value) == "") {
        errorMessage = "Email must have a value.";
    }
    // Check if the email id is valid, then continue validating
    if(errorMessage == "")
    {
        // Get the email id separated by comma
        var emailList = emailId.value.split(",");
        var invalidEmailId = "";
        for(i=0;i<emailList.length;i++) {
            if (!checkEmail(trim(emailList[i]))) {
                invalidEmailId += ", "+trim(emailList[i]);
            }
        }
        if(invalidEmailId.length > 0) {
            errorMessage = "Please provide a valid email id ["+invalidEmailId.substring(2)+"]"
        }
    }

    // check if there are any errors
    if(errorMessage != "") {
        showError(errorMessage);
        focus('email');
    }
    else {
        trackRequest("INVITATION_SUBMITTED");
        // processing indicator
        var processingImage = document.getElementById('errormessage');
        if(processingImage) {
            processingImage.style.visibility = 'visible';
            processingImage.innerHTML = "<span class='errorsmalltext'><center>&nbsp;<span style='position:absolute'><img src='imagesEx/green_rot.gif'/></span></center></span>";
        }
        // prepare url
        var url = "invite.do?submit=submit";
        url += "&email=" + document.getElementById("email").value;
        url += "&notes=" + document.getElementById("notes").value;

        // Make the request
        new AjaxRequest(url, {
            method:'get',
            onSuccess: function(transport) {
                if ("close" == transport.responseText) {
                    // hide window
                    hidePopupPanel();
                }
                else {
                    showError(transport.responseText);
                    focus('email');
                }
            },
            onFailure: function() {
                showError("Error trying to invite a friend, please try again later.");
                focus('email');
            }
        });
    }
}

/**
 * Cancel invite
 */
function cancelInvite() {
    trackRequest("INVITATION_CANCELLED");
    hidePopupPanel();
}

/**
 * Checks if the user pressed enter while typing the
 * form field like password or username.
 */
function submitInvitation(e){
    evt = e || window.event;
    var keyPressed = evt.which || evt.keyCode;
    if(keyPressed==13)
    {
        invite();
    }
}

