/**
 * Show user registration panel
 */
function showRegistrationForm(trackerName) {
    if(trackerName)
        trackRequest(trackerName);
    else
        trackRequest("REGISTRATION_CLICKED");
    var popupPanel = document.getElementById('popupPanel');
    if(popupPanel) {
        // title
        var popupPanelTitle = document.getElementById('popupPanelTitle');
        popupPanelTitle.innerHTML = "User Registration";
        // close button
        preparePopupCloseButton();
        // body
        var popupPanelBody = document.getElementById('popupPanelBody');
        popupPanelBody.innerHTML = prepareRegistrationHTML();
        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 registration
 */
function prepareRegistrationHTML() {
    // The registration innerHTML should come thru ajax request
    var registrationText = "" +
        "<form onsubmit='return false;'>" +
        "   <table class='normaltext'>" +
        "       <tr><td colspan='3'><div style='visibility:hidden;' id='errormessage'><span class='errorsmalltext'>&nbsp;</span></div></td></tr>" +
        "       <tr><td colspan='3'><hr class='ruler' width='485px' /></td></tr>" +
        "       <tr>" +
        "           <td colspan='3'>" +
        "           Please enter and verify your e-mail address to start the registration process. This will become your username." +
        "           </td></tr>" +
        "       <tr><td colspan='3'><hr class='ruler' /></td></tr>" +
        "       <tr>" +
        "           <td class='smalltext'>E-Mail</td>" +
        "           <td class='smalltext'>:</td>" +
        "           <td><input tabindex='11' type='text' class='bigtextbox' name='email' id='email' onkeypress='submitRegister(event);' value='' /></td>" +
        "       </tr>" +
        "       <tr>" +
        "           <td class='smalltext'>Verify</td>" +
        "           <td class='smalltext'>:</td>" +
        "           <td><input tabindex='12' type='text' class='bigtextbox' name='confirm' id='confirm' onkeypress='submitRegister(event);'/></td>" +
        "       </tr>" +
        "       <tr>" +
        "           <td id='wait' colspan='3' height='26px' align='center'>&nbsp;</td>" +
        "       </tr>" +
        "       <tr>" +
        "           <td class='smallnormaltext' colspan='3'><input tabindex='13' type='checkbox' name='acceptterms' id='acceptterms' value='true' />&nbsp;Join TalentGraphz</td>" +
        "       </tr>" +
        "       <tr>" +
        "           <td colspan='3' id='agreement' class='smallnormaltext'><i>By clicking \"Join TalentGraphz\", you are indicating that you have read, understood, and agree to TalentGraphz's <a class='underlined' href='"+pagelinks['TERMS_OF_USE']+"'>User Agreement</a> and <a class='underlined' href='"+pagelinks['PRIVACY_POLICY']+"'>Privacy Policy</a>.</i></td>" +
        "       </tr>" +
        "       <tr>" +
        "           <td colspan='3'><hr class='ruler' /></td>" +
        "       </tr>" +
        "       <tr>" +
        "           <td colspan='3' align='right'>" +
        "           <input tabindex='14' id='register' type='button' class='proformaButton' title='Register User' value='Register' onclick='registerUser();' />&nbsp;&nbsp;" +
        "           <input tabindex='15' id='cancel' type='button' class='proformaButton' title='Cancel' value='Cancel' onclick='cancelRegistration();' />" +
        "           </td>" +
        "       </tr>" +
        "   </table>" +
        "</form>";

    return registrationText;
}

/**
 * Checks if proper data is keyed in in the registration
 * form. If the information is not proper, display the same
 * form, with an error message. Otherwise displays the
 * activation form
 */
function registerUser() {
    var emailId = document.getElementById('email');
    var confirm = document.getElementById('confirm');
    var acceptterms = document.getElementById('acceptterms');
    var errorMessage = "";

    if(!emailId || trim(emailId.value) == "") {
        errorMessage = "Email must have a value.";
    }
    else if(!confirm || trim(confirm.value) == "") {
        errorMessage = "Confirm must have a value.";
    }
    else if (emailId.value != confirm.value) {
        errorMessage = "Email and confirm must have the same value.";
    }
    else if (!checkEmail(emailId.value))
    {
        errorMessage = "Please provide a valid email id.";
    }
    else if(!acceptterms.checked) {
        errorMessage = "Please confirm acceptance of TalentGraphz's \"User Agreement\" and \"Privacy Policy\".";
    }

    // check if there are any errors
    if(errorMessage != "") {
        showError(errorMessage);
        focus('email');
    }
    else {
        trackRequest("REGISTRATION_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>";
        }

        // @TODO
        // Server side checks still pending for
        // 1) email pattern validation
        // 2) if the email id is already there with the system
        var url = "register.do?check=check";
        url += "&email=" + emailId.value;
        new AjaxRequest(url, {
            method:'get',
            onSuccess: function(transport) {
                var resultText;
                if(trim(transport.responseText) != "" && "activate" != trim(transport.responseText)) {
                    showError(trim(transport.responseText));
                    // set focus to email
                    focus('email');
                }
                else {
                    var email = emailId.value;
                    // hide pop up
                    hidePopupPanel();
                    // Prepare inner HTML for activation
                    showActivationForm(email, "");
                }
            },
            onFailure: function() {
                showError("Error trying to register the user, please try again later.");
                // set focus to email
                focus('email');
            }
        });
    }
}

/**
 * Cancels user registration
 */
function cancelRegistration() {
    trackRequest("REGISTRATION_CANCELLED");
    hidePopupPanel();
}

/**
 * Checks if the user pressed enter while typing the
 * form field like password or username.
 */
function submitRegister(e){
    evt = e || window.event;
    var keyPressed = evt.which || evt.keyCode;
    if(keyPressed==13)
    {
        registerUser();
    }
}

/**
 * Show user activation form
 */
function showActivationForm(emailId, code) {
    var popupPanel = document.getElementById('popupPanel');
    if(popupPanel) {
        // title
        var popupPanelTitle = document.getElementById('popupPanelTitle');
        popupPanelTitle.innerHTML = "User Activation";
        // close button
        preparePopupCloseButton();
        // body
        var popupPanelBody = document.getElementById('popupPanelBody');
        popupPanelBody.innerHTML = prepareActivationHTML(emailId, code);
        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 user activation
 */
function prepareActivationHTML(emailId, activationCode) {
    // The activation innerHTML should come thru ajax request
    var activationText = "<form onsubmit='return false;'>" +
        "   <table class='normaltext' cellspacing='0' cellpadding='2' border='0'>" +
        "       <tr><td colspan='3'><div style='visibility:hidden;' id='errormessage'><span class='errorsmalltext'>&nbsp;</span></div></td></tr>" +
        "       <tr><td colspan='3'><hr class='ruler' width='485px' /></td></tr>" +
        "       <tr>" +
        "           <td colspan='3'>" +
        "               Upon receiving your activation code, complete the following form to activate your account." +
        "           </td></tr>" +
        "       <tr><td colspan='3'><hr class='ruler' /></td></tr>" +
        "       <tr>" +
        "           <td class='smalltext'>E-Mail</td>" +
        "           <td class='smalltext'>:</td>" +
        "           <td><input tabindex='11' type='text' class='verybigtextbox' name='email' id='email' onkeypress='submitActivation(event);' "+(trim(emailId)!=""?"readonly":"")+" value='"+emailId+"' /></td>" +
        "       </tr>" +
        "       <tr>" +
        "           <td class='smalltext'>Activation</td>" +
        "           <td class='smalltext'>:</td>" +
        "           <td><input tabindex='12' type='text' class='verybigtextbox' name='code' id='code' onkeypress='submitActivation(event);' value='"+activationCode+"'/></td>" +
        "       </tr>" +
        "       <tr>" +
        "           <td class='smalltext'>Password</td>" +
        "           <td class='smalltext'>:</td>" +
        "           <td><input tabindex='13' type='password' class='verybigtextbox' name='newpassword' id='newpassword' onkeypress='submitActivation(event);' value='' /></td>" +
        "       </tr>" +
        "       <tr>" +
        "           <td class='smalltext'>Confirm</td>" +
        "           <td class='smalltext'>:</td>" +
        "           <td><input tabindex='14' type='password' class='verybigtextbox' name='confirm' id='confirm' onkeypress='submitActivation(event);' value='' /></td>" +
        "       </tr>" +
        "       <tr>" +
        "           <td colspan='2'>&nbsp;</td>" +
        "           <td class='smallnormaltext'><input tabindex='15' type='checkbox' name='rememberMe' id='rememberMe' value='true' />Remember Me</td>" +
        "       </tr>" +
        "       <tr>" +
        "           <td colspan='3'><hr class='ruler' /></td>" +
        "       </tr>" +
        "       <tr>" +
        "           <td colspan='3' align='right'>" +
        "           <input tabindex='16' id='activate' type='button' class='proformaButton' title='Activate User' value='Activate' onclick='activateUser();' />&nbsp;&nbsp;" +
        "           <input tabindex='17' id='cancel' type='button' class='proformaButton' title='Cancel' value='Cancel' onclick='hidePopupPanel();' />" +
        "           </td>" +
        "       </tr>" +
        "   </table>" +
        "</form>";

    return activationText;
}

/**
 * Checks if proper data is keyed in in the activation
 * form. If the information is not proper, display the same
 * form, with an error message.
 * Otherwise, the request should be submitted
 */
function activateUser() {
    var emailId = document.getElementById('email');
    var code = document.getElementById('code');
    var password = document.getElementById('newpassword');
    var confirm = document.getElementById('confirm');
    var errorMessage = "";

    if(!emailId || emailId.value == "") {
        errorMessage = "Email must have a value.";
    }
    else if(!code || trim(code.value) == "") {
        errorMessage = "Code should be provided to activate account.";
    }
    else if(!password || trim(password.value) == "") {
        errorMessage = "Password must have a value.";
    }
    else if(!confirm || trim(confirm.value) == "") {
        errorMessage = "Confirm must have a value.";
    }
    else if (trim(password.value) != trim(confirm.value)) {
        errorMessage = "Password and confirm must have the same value.";
    }

    // 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>";
        }
        // submit request to server
        // Setup the URL
        var url = "activation.do?check=check";
        url += "&email=" + emailId.value;
        url += "&code=" + code.value;
        url += "&password=" + password.value;
        url += "&confirm=" + confirm.value;
        url += "&rememberMe="+document.getElementById("rememberMe").checked;
        // differentiator between the old and new site
        url += "&domain=genotrope";

        // Make the request
        new AjaxRequest(url, {
            method:'get',
            onSuccess: function(transport) {
                var resultText;
                if(trim(transport.responseText) != "" && (trim(transport.responseText).indexOf("close") == -1)) {
                    showError(transport.responseText);
                    focus('email');
                }
                else {
                    // hide window
                    hidePopupPanel();
                    if ("closeManager" == transport.responseText) {
                        // set cookie for Hiring Manager
                        createCookie('usertype','manager',1);
                        // forward to the home page
                        window.location=pagelinks["COMPANY_PROFILE"];
                    }
                    else{
                        // set cookie for normal user
                        createCookie('usertype','standard',1);
                        // As ticket #103, successful registration
                        // and activation should land in profile page.
                        window.location=pagelinks["PROFILE_LIST"];
                    }
                }
            },
            onFailure: function() {
                showError("Error trying to activate user, please try again later.");
                focus('email');
            }
        });
    }
}

/**
 * Returns true if this is an activation request
 */
function isActivationRequest() {
    var queryString = window.location.search + "";
    return (queryString.indexOf("action=activateAccount") != -1);
}

/**
 * Returns activation tokens
 */
function getActivationTokens() {
    var queryString = window.location.search + "";
    var parameters = queryString.split("&");
    var email = "";
    var code = "";
    var tokens = null;
    if(parameters != null) {
        for(var k = 0; k < parameters.length; k++) {
            if(parameters[k].indexOf("code") != -1)
                code = parameters[k].split("=")[1];
            if(parameters[k].indexOf("email") != -1)
                email = parameters[k].split("=")[1];
        }
        if(email != "" && code != "") {
            tokens = new Array();
            tokens[0] = email;
            tokens[1] = code;
        }
    }
    return tokens;
}

/**
 * Checks if the user pressed enter while typing the
 * form field like password or username.
 */
function submitActivation(e){
    evt = e || window.event;
    var keyPressed = evt.which || evt.keyCode;
    if(keyPressed==13)
    {
        activateUser();
    }
}

/**
 * Displays the company registration pop up form
 */
function showCompanyRegistrationForm() {
    trackRequest("COMPANY_REGISTRATION_CLICKED");
    var popupPanel = document.getElementById('popupPanel');
    if(popupPanel) {
        // parse out the user name from welcome message
        var loginObj = document.getElementById("LOGIN");
        var loginText = "";
        if(loginObj) {
            loginText = loginObj.innerHTML;
            if(loginText.indexOf("Welcome ") > -1) {
                loginText = loginText.replace("Welcome ", "");
                loginText = trim(loginText.replace("!", ""));
                if(loginText.indexOf("nbsp;") > -1) {
                    loginText = trim(loginText.substr(0, loginText.indexOf("nbsp;")-1));
                }
            }
        }
        // title
        var popupPanelTitle = document.getElementById('popupPanelTitle');
        popupPanelTitle.innerHTML = "Company Registration";
        // close button
        preparePopupCloseButton();
        // body
        var popupPanelBody = document.getElementById('popupPanelBody');
        popupPanelBody.innerHTML = prepareCompanyRegistrationHTML(loginText);
        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 company registration
 */
function prepareCompanyRegistrationHTML(emailId) {
    // The registration innerHTML should come thru ajax request
    var registrationText = "" +
        "<form onsubmit='return false;'>" +
        "   <table class='normaltext' border='0' cellspacing='0' cellpadding='2'>" +
        "       <tr><td colspan='3'><div style='visibility:hidden;' id='errormessage'><span class='errorsmalltext'>&nbsp;</span></div></td></tr>" +
        "       <tr><td colspan='3'><hr class='ruler' width='485px' /></td></tr>" +
        "       <tr>" +
        "           <td colspan='3'>" +
        "           Please enter and verify your e-mail address to start the company registration process. Our service advisors will reply to you in the email provided within 3 working days." +
        "           </td></tr>" +
        "       <tr><td colspan='3'><hr class='ruler' /></td></tr>" +
        "       <tr>" +
        "           <td class='smalltext'>E-Mail</td>" +
        "           <td class='smalltext'>:</td>" +
        "           <td><input tabindex='11' type='text' class='bigtextbox' style='width:206px' name='email' id='email' onkeypress='submitCompanyRegister(event);' value='"+emailId+"' /></td>" +
        "       </tr>" +
        "       <tr>" +
        "           <td class='smalltext'>Verify</td>" +
        "           <td class='smalltext'>:</td>" +
        "           <td><input tabindex='12' type='text' class='bigtextbox' style='width:206px' name='confirm' id='confirm' onkeypress='submitCompanyRegister(event);' value='"+emailId+"'/></td>" +
        "       </tr>" +
        "       <tr>" +
        "           <td class='smalltext'>Company</td>" +
        "           <td class='smalltext'>:</td>" +
        "           <td>" +
        "           <input tabindex='13' type='text' class='bigtextbox' title='Company for registration' name='searchField' id='searchField' value='' onKeyDown='keyPressHandler(event);' onKeyUp='document.getElementById(\"company_name\").value=this.value;document.getElementById(\"company_id\").value=\"\";' />" +
        "           <input type='hidden' name='company_id' id='company_id' value='' /><input type='hidden' name='company_name' id='company_name' value='' /><input type='hidden' name='toRegister' id='toRegister' value='1' />" +
        "           <div/><div name='divCompanyList' id='divCompanyList' class='smallnormaltext'></div>" +
        "           </td>" +
        "       </tr>" +
        "       <tr>" +
        "       <tr>" +
        "           <td colspan='2'>&nbsp;</td>" +
        "           <td class='smallnormaltext'><i>(as appears in database or as you would like it to appear)</i></td>" +
        "       </tr>" +
        "       <tr>" +
        "       <tr>" +
        "           <td class='smallnormaltext' colspan='3'><input tabindex='14' type='checkbox' name='acceptterms' id='acceptterms' value='true' />&nbsp;Join TalentGraphz</td>" +
        "       </tr>" +
        "           <td colspan='3' id='agreement' class='smallnormaltext'><i>By clicking \"Join TalentGraphz\", you are indicating that you have read, understood, and agree to TalentGraphz's <a class='underlined' href='"+pagelinks['TERMS_OF_USE']+"'>User Agreement</a> and <a class='underlined' href='"+pagelinks['PRIVACY_POLICY']+"'>Privacy Policy</a>.</i></td>" +
        "       </tr>" +
        "       <tr>" +
        "           <td colspan='3'><hr class='ruler' /></td>" +
        "       </tr>" +
        "       <tr>" +
        "           <td colspan='3' align='right'>" +
        "           <input tabindex='15' id='registercompany' type='button' class='proformaButton' title='Register Company' value='Register Company' onclick='registerCompany();' /> &nbsp;" +
        "           <input tabindex='16' id='cancel' type='button' class='proformaButton' title='Cancel' value='Cancel' onclick='cancelCompanyRegistration();' />" +
        "       </td></tr>" +
        "   </table>" +
        "</form>";

    return registrationText;
}

/**
 * Set registration company details
 */
function setRegistrationCompany(name, companyId) {
    var companyName = document.getElementById('company_name');
    if(companyName) {
        companyName.value = name;
    }
    var searchField = document.getElementById('searchField');
    if(searchField) {
        searchField.value = companyName.value;
    }
    var company_id = document.getElementById('company_id');
    if(company_id) {
        company_id.value = companyId;
    }
    document.getElementById('divCompanyList').style.visibility = 'hidden';
}

/**
 * Checks if proper data is keyed in in the registration
 * form. If the information is not proper, display the same
 * form, with an error message. Otherwise displays the
 * activation form
 */
function registerCompany() {
    var emailId = document.getElementById('email');
    var confirm = document.getElementById('confirm');
    var company_name = document.getElementById('company_name');
    var company_id = document.getElementById('company_id');
    var acceptterms = document.getElementById('acceptterms');
    var errorMessage = "";

    if(!emailId || trim(emailId.value) == "") {
        errorMessage = "Email must have a value.";
    }
    else if(!confirm || trim(confirm.value) == "") {
        errorMessage = "Confirm must have a value.";
    }
    else if(!company_name || trim(company_name.value) == "") {
        errorMessage = "Company must have a value.";
    }
    else if (emailId.value != confirm.value) {
        errorMessage = "Email and confirm must have the same value.";
    }
    else if (!checkEmail(emailId.value))
    {
        errorMessage = "Please provide a valid email id.";
    }
    else if(!acceptterms.checked) {
        errorMessage = "Please confirm acceptance of TalentGraphz's \"User Agreement\" and \"Privacy Policy\".";
    }

    // check if there are any errors
    if(errorMessage != "") {
        showError(errorMessage);
        focus('email');
    }
    else {
        trackRequest("COMPANY_REGISTRATION_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>";
        }
        // @TODO
        // Server side checks are pending for
        // 1) email pattern validation
        // 2) if the email id is already there with the system
        var url = "register.do?check=check&companyregistration=true";
        url += "&email=" + trim(emailId.value);
        url += "&companyName=" + trim(company_name.value);
        url += "&companyId=" + trim(company_id.value);
        new AjaxRequest(url, {
            method:'get',
            onSuccess: function(transport) {
                if(trim(transport.responseText) != "" && "close" != trim(transport.responseText)){
                    showError(transport.responseText);
                    // set focus to email
                    focus('email');
                }
                else{
                    // success message
                    var el = document.getElementById('popupPanelBody');
                    if (el) {
                        el.innerHTML = "<hr class='ruler' width='485px' /><br /><center>We have received your company registration request. Our service advisors will reply to you within 3 working days.</center><br />";
                    }
                }
            },
            onFailure: function() {
                showError("Error trying to register company, please try again later. ");
                // set focus to email
                focus('email');
            }
        });
    }
}

/**
 * Cancel company registration
 */
function cancelCompanyRegistration() {
    trackRequest("COMPANY_REGISTRATION_CANCELLED");
    hidePopupPanel();
}

/**
 * Checks if the user pressed enter while typing the
 * form field like password or username.
 */
function submitCompanyRegister(e){
    evt = e || window.event;
    var keyPressed = evt.which || evt.keyCode;
    if(keyPressed==13)
    {
        registerCompany();
    }
}

/**
 * Displays the company registration form
 */
function showAddCompanyRegistrationForm(guid) {
    var popupPanel = document.getElementById('popupPanel');
    if(popupPanel) {
        // title
        var popupPanelTitle = document.getElementById('popupPanelTitle');
        popupPanelTitle.innerHTML = "Add Licensed User";
        // close button
        preparePopupCloseButton();
        // body
        var companyName = trim(document.getElementById('COMPANIES').innerHTML);
        var popupPanelBody = document.getElementById('popupPanelBody');
        popupPanelBody.innerHTML = prepareAddCompanyRegistrationHTML(guid, companyName);
        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 company registration
 */
function prepareAddCompanyRegistrationHTML(companyGUID, companyName) {
    var companyRegistrationText = ""+
        "<form onsubmit='return false;'>" +
        "   <table class='normaltext'>" +
        "       <tr><td colspan='3'><div style='visibility:hidden;' id='errormessage'><span class='errorsmalltext'>&nbsp;</span></div></td></tr>" +
        "       <tr><td colspan='3'><hr class='ruler' width='485px' /></td></tr>" +
        "       <tr>" +
        "           <td colspan='3'>" +
        "           Please enter user's (Manager) e-mail address (E-Mail & E-Mail Verify) & Company Name. Email address provided will be the Hiring Manager of the company name provided. Please make sure you provide a valid e-mail id." +
        "           </td></tr>" +
        "       <tr><td colspan='3'><hr class='ruler' /></td></tr>" +
        "       <tr>" +
        "           <td class='smalltext'>E-Mail</td>" +
        "           <td class='smalltext'>:</td>" +
        "           <td><input tabindex='11' type='text' class='verybigtextbox' name='email' id='email' size='40' onkeypress='submitAddCompanyRegister(event);' value='' /></td>" +
        "       </tr>" +
        "       <tr>" +
        "           <td class='smalltext'>E-Mail Verify</td>" +
        "           <td class='smalltext'>:</td>" +
        "           <td><input tabindex='12' type='text' class='verybigtextbox' name='confirm' id='confirm' size='40' onkeypress='submitAddCompanyRegister(event);'/></td>" +
        "       </tr>" +
        "       <tr>" +
        "           <td class='smalltext'>Company</td>" +
        "           <td class='smalltext'>:</td>" +
        "           <td><input tabindex='13' type='text' class='verybigtextbox' name='companyname' id='companyname' size='40' onkeypress='submitAddCompanyRegister(event);' value='" + companyName + "' readonly /><input type='hidden' name='company' id='company' value='" + companyGUID + "' /></td>" +
        "       </tr>" +
        "       <tr>" +
        "           <td colspan='3'><hr class='ruler' /></td>" +
        "       </tr>" +
        "       <tr>" +
        "           <td colspan='3' align='right'>" +
        "           <input tabindex='14' id='registercompany' type='button' class='proformaButton' title='Add Licensed User' value='Add Licensed User' onclick='addRegisteredCompany();' /> &nbsp;" +
        "           <input tabindex='15' id='cancel' type='button' class='proformaButton' title='Cancel' value='Cancel' onclick='hidePopupPanel();' />" +
        "           </td>" +
        "       </tr>" +
        "   </table>" +
        "</form>";

    return companyRegistrationText;
}

/**
 * Add registered company along with its licensee
 */
function addRegisteredCompany(companyId) {
    var emailId = document.getElementById('email');
    var confirm = document.getElementById('confirm');
    var companyId = document.getElementById('company');
    var errorMessage = "";

    if(!emailId || trim(emailId.value) == "") {
        errorMessage = "E-Mail must have a value.";
    }
    else if(!confirm || trim(confirm.value) == "") {
        errorMessage = "E-Mail Verify must have a value.";
    }
    else if (emailId.value != confirm.value) {
        errorMessage = "E-Mail and E-Mail Verify must have the same value.";
    }
    else if (!checkEmail(emailId.value)) {
        errorMessage = "Please provide a valid email id.";
    }
    else if(!companyId || trim(companyId.value) == "") {
        errorMessage = "Company must have a value.";
    }

    // 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 url = "register.do?check=check&companyregistration=true&licenseregistration=true";
        url += "&licenseuser=" + emailId.value;
        url += "&licensecompany=" + companyId.value;

        // Make the request
        new AjaxRequest(url, {
            method:'get',
            onSuccess: function(transport) {
                // hide pop up
                hidePopupPanel();
                // refresh company page
                loadCompany2(trim(companyId.value), null, false);
            },
            onFailure: function(transport) {
                showError("Error trying to register company with the user, please try again later. ");
                // set focus to email
                focus('email');
            }
        });
    }
}

/**
 * Checks if the user pressed enter while typing the
 * form field like password or username.
 */
function submitAddCompanyRegister(e){
    evt = e || window.event;
    var keyPressed = evt.which || evt.keyCode;
    if(keyPressed==13)
    {
        addRegisteredCompany();
    }
}

/**
 * Submit a reset password request.
 */
function resetPassword()
{
    var url = "resetPassword.do?action=resetNotify&username=";
    var username = document.getElementById("username");
    if(username)
    {
        if(!checkEmail(username.value))
        {
            var el = document.getElementById('message');
            if (el) {
                el.innerHTML = "<label class='errorsmalltext' valign='top'>To reset your password please provide a valid email.</label>&nbsp;&nbsp;<a class='underlined' onclick='resetPassword()'>Reset</a><label class='errorsmalltext'> your password.</label>";
                focus('username');
            }
            return false;
        }
        url+=username.value;
        // Make the request
        new AjaxRequest(url, {
            method:'get',
            onSuccess: function(transport) {
                var el = document.getElementById('message');

                if (el) {
                    el.innerHTML = transport.responseText;
                }
            },
            onFailure: function() {
            }
        });
   }
}

/**
 * Returns true if this is a reset password request
 */
function isResetPasswordRequest() {
    var queryString = window.location.search + "";
    return (queryString.indexOf("action=resetPassword") != -1);
}

/**
 * Displays the reset password pop up form
 */
function showResetPasswordForm() {
    var popupPanel = document.getElementById('popupPanel');
    if(popupPanel) {
        // title
        var popupPanelTitle = document.getElementById('popupPanelTitle');
        popupPanelTitle.innerHTML = "Reset password & Activate account";
        // close button
        preparePopupCloseButton();
        // body
        var popupPanelBody = document.getElementById('popupPanelBody');
        popupPanelBody.innerHTML = prepareResetPassHTML();
        // 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 reset password
 */
function prepareResetPassHTML() {
    // The activation innerHTML should come thru ajax request
    var activationText = "<form onsubmit='return false;'>" +
        "<table class='normaltext'>" +
        "  <tr><td colspan='3'><div style='visibility:hidden;' id='errormessage'><span class='errorsmalltext'>&nbsp;</span></div></td></tr>" +
        "  <tr><td colspan='3'><hr class='ruler' width='485px' /></td></tr>" +
        "  <tr><td colspan='2'>" +
        "    Upon receiving your activation code, complete the following form to reset your password & activate your account.<hr class='ruler'/>" +
        "  </td></tr>" +
        "  <tr>" +
        "    <td class='smalltext'>E-Mail:</td>" +
        "    <td><input type='text' tabindex='11' class='verybigtextbox' name='email' id='email' onkeypress='submitResetActivation(event);' value='' /></td>" +
        "  </tr>" +
        "  <tr>" +
        "    <td class='smalltext'>Activation:</td>" +
        "    <td><input type='text' tabindex='12' class='verybigtextbox' name='code' id='code' onkeypress='submitResetActivation(event);'/></td>" +
        "  </tr>" +
        "  <tr>" +
        "    <td class='smalltext'>Password:</td>" +
        "    <td><input type='password' tabindex='13' class='verybigtextbox' name='newpassword' onkeypress='submitResetActivation(event);' id='newpassword' /></td>" +
        "  </tr>" +
        "  <tr>" +
        "    <td class='smalltext'>Confirm:</td>" +
        "    <td><input type='password' tabindex='14' class='verybigtextbox' name='confirm' id='confirm' onkeypress='submitResetActivation(event);'/></td>" +
        "  </tr>" +
        "  <tr>" +
        "    <td/>" +
        "    <td class='smalltext'><input type='checkbox' tabindex='15' id='rememberMe' name='rememberMe' onkeypress='submitResetActivation(event);'/>Remember me</td>" +
        "  </tr>" +
        "  <tr>" +
        "    <td colspan='2'><hr class='ruler' /></td>" +
        "  </tr>" +
        "  <tr>" +
        "    <td colspan='2' align='right'>" +
        "    <input tabindex='16' id='activateuser' type='button' class='proformaButton' title='Activate User' value='Activate' onclick='resetAndActivate();' />&nbsp;&nbsp;" +
        "    <input tabindex='17' id='cancel' type='button' class='proformaButton' title='Cancel' value='Cancel' onclick='hidePopupPanel();' />" +
        "    </td></tr>" +
        "</table>" +
        "</form>";

    return activationText;
}

/**
 * Submit a reset password request.
 */
function resetAndActivate()
{
    var emailId = document.getElementById('email');
    var code = document.getElementById('code');
    var password = document.getElementById('newpassword');
    var confirm = document.getElementById('confirm');
    var errorMessage = "";

    if(!emailId || emailId.value == "") {
        errorMessage = "Email must have a value.";
    }
    else if(!code || trim(code.value) == "") {
        errorMessage = "Code should be provided to activate account.";
    }
    else if(!password || trim(password.value) == "") {
        errorMessage = "Password must have a value.";
    }
    else if(!confirm || trim(confirm.value) == "") {
        errorMessage = "Confirm must have a value.";
    }
    else if (trim(password.value) != trim(confirm.value)) {
        errorMessage = "Password and confirm must have the same value.";
    }

    // 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>";
        }
        // submit request to server
        // Setup the URL
        var url = "resetPassword.do?action=resetPassword&confirmed=true";
        url += "&username=" + emailId.value;
        url += "&code=" + code.value;
        url += "&password=" + password.value;
        url += "&confirm=" + confirm.value;
        url += "&rememberMe="+document.getElementById("rememberMe").checked;
        // differentiator between the old and new site
        url += "&domain=genotrope";

        // Make the request
        new AjaxRequest(url, {
            method:'get',
            onSuccess: function(transport) {
                var resultText;
                if(trim(transport.responseText) != "" && (trim(transport.responseText).indexOf("close") == -1)) {
                    showError(transport.responseText);
                    // set focus to email
                    focus('email');
                }
                else {
                    // hide window
                    hidePopupPanel();
                    if ("closeManager" == transport.responseText) {
                        // set cookie for Hiring Manager
                        createCookie('usertype','manager',1);
                        // forward to the home page
                        window.location = pagelinks["COMPANY_PROFILE"];
                    }
                    else{
                        // set cookie for normal user
                        createCookie('usertype','standard',1);
                        // As ticket #103, successful registration
                        // and activation should land in profile page.
                        window.location = pagelinks["PROFILE_LIST"];
                    }
                }
            },
            onFailure: function() {
                showError("Error trying to activate user, please try again later.");
                focus('email');
            }
        });
    }
}

/**
 * Checks if the user pressed enter while typing the
 * form field like password or username.
 */
function submitResetActivation(e){
    evt = e || window.event;
    var keyPressed = evt.which || evt.keyCode;
    if(keyPressed==13)
    {
        resetAndActivate();
    }
}

