/**
 * Displays the Tell a friend form
 */
function showFriendForm(guid, name) {
    var popupPanel = document.getElementById('popupPanel');
    if(popupPanel) {
        // title
        var popupPanelTitle = document.getElementById('popupPanelTitle');
        popupPanelTitle.innerHTML = "Tell a friend";
        // close button
        preparePopupCloseButton();
        // body
        var popupPanelBody = document.getElementById('popupPanelBody');
        popupPanelBody.innerHTML = prepareFriendHTML(guid, name);
        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 tell a friend
 */
function prepareFriendHTML(guid, name) {
    // The friend innerHTML should come thru ajax request
    var friendText = "<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 tell your friend(s) about <b>"+name+"</b>, type your friend's email addresses (separated by commas) & add a message(optional) ." +
        "    <input type='hidden' id='companyGuid' name='companyGuid' value='"+guid+"'>" +
        "    </td></tr>" +
        "  <tr><td colspan='2'><hr class='ruler' /></td></tr>" +
        "  <tr>" +
        "    <td class='smalltext'>Email:</td>" +
        "    <td><input tabindex='1' type='text' class='verybigtextbox' name='email' id='email' value='' onkeypress='submitFriendForm(event);' /></td>" +
        "  </tr>" +
        "  <tr>" +
        "    <td class='smalltext'>Message:</td>" +
        "    <td><textarea tabindex='2' class='verybigtextarea' name='notes' id='notes' rows='4'>Thought you might be interested in checking this company ("+name+") out.</textarea></td>" +
        "  </tr>" +
        "  <tr>" +
        "    <td colspan='2'><hr class='ruler' /></td>" +
        "  </tr>" +
        "  <tr>" +
        "    <td colspan='2' align='right'>" +
        "    <input tabindex='3' type='button' class='proformaButton' title='Tell a Friend' value='Submit' onclick='notifyFriend();' />&nbsp;&nbsp;" +
        "    <input tabindex='4' type='button' class='proformaButton' title='Cancel' value='Cancel' onclick='hidePopupPanel();' />" +
        "    </td></tr>" +
        "</table>" +
        "</form>";

    return friendText;
}

/**
 * Checks if the data is proper for tell a friend. If the
 * information is not proper, display the same form, with
 * an error message.
 * Otherwise, the request should be submitted
 */
function notifyFriend() {
    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 {
        // 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>";
        }

        var emailId = document.getElementById('email');
        var url = "invite.do?submit=submit&action=friend";
        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 tell a friend, please try again later.");
                focus('email');
            }
        });

        return false;
    }
}

/**
 * Checks if the user pressed enter while typing the
 * form field like password or username.
 */
function submitFriendForm(e){
    evt = e || window.event;
    var keyPressed = evt.which || evt.keyCode;
    if(keyPressed==13)
    {
        notifyFriend();
    }
}

