﻿function CreateLivedriveLoginControl(ElementId, APIKeyMD5, SSLDefaultValue, IncludeSSL, IncludeBusiness) {

    try {

        var LivedriveDomain = 'www.livedrive.com';
                
        
        if (LDendsWith(window.location.host, '.livedrivedev.com')) LivedriveDomain = 'www.livedrivedev.com';
        if (LDendsWith(window.location.host, '.livedrivedevlocal.com')) LivedriveDomain = 'www.livedrivedevlocal.com'; 
    
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //  Error Handling
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        if (LDgetQueryVariable('error') == '') {
            ErrorHtml = '';
        } else {
            switch (LDgetQueryVariable('error')) {
                case 'loginincorrect': Error = "Your email or password is incorrect.";
                    break;
                case 'bothmissing': Error = "An email and password are required.";
                    break;
                case 'passmissing': Error = "A password is required.";
                    break;
                case 'emailmissing': Error = "A email is required. ";
                    break;
                case 'notadministrator': Error = "Account is not an administrator account";
                    break;
                default: Error = "Unknown";
            }

            ErrorHtml = '<li class="LDLoginListItem">' +
                            '<div class="LDLoginError"><span>' + Error + '</span>' +
                            '</div>' +
                        '</li>';
        }

        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // Business Handling
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        if (IncludeBusiness == true) {
            if (LDgetQueryVariable('business')) {
                BusinessHtml = '<li class="LDLoginListItem">' +
                                            '<label for="LoginBusinessAdmin" class="LDLoginCheckBoxLabel"></label>' +
                                            '<input id="LoginBusinessAdmin" type="checkbox" value="true" checked="true" name="BusinessAdminLogin" onclick="LDToggleLoginMethod(\'' + LivedriveDomain + '\');" class="LDLoginValueCheckbox">' +
                                            'Login to Business Admin' +
                                        '</li>';
            } else {
                BusinessHtml = '<li class="LDLoginListItem">' +
                                            '<label for="LoginBusinessAdmin" class="LDLoginCheckBoxLabel"></label>' +
                                            '<input id="LoginBusinessAdmin" type="checkbox" value="true" name="BusinessAdminLogin" onclick="LDToggleLoginMethod(\'' + LivedriveDomain + '\');" class="LDLoginValueCheckbox">' +
                                            'Login to Business Admin' +
                                        '</li>';
            }
        } else {
            BusinessHtml = '';
        }

        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // SSL Handling
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        if (IncludeSSL) {
            if (LDgetQueryVariable('ssl')) {
                SSLHtml = '<input id="usessl" type="checkbox" value="true" checked="true" name="usessl">' +
                        'SSL' +
                        '<img class="sslTooltip" src="http://' + LivedriveDomain + '/Content/images/form-input-question.png" title=" - If ticked, SSL will be used to encrypt all data as it travels between your web browser and servers. Browsing your web portal and downloading or uploading files will be slower." style="margin-left: 5px;">';
            }
            else {
                if (SSLDefaultValue == true) {
                    SSLHtml = '<input id="usessl" type="checkbox" value="true" name="usessl" checked="true" >' +
                            'SSL' +
                            '<img class="sslTooltip" src="http://' + LivedriveDomain + '/Content/images/form-input-question.png" title=" - If ticked, SSL will be used to encrypt all data as it travels between your web browser and servers. Browsing your web portal and downloading or uploading files will be slower." style="margin-left: 5px;">';
                } else {
                    SSLHtml = '<input id="usessl" type="checkbox" value="true" name="usessl" >' +
                            'SSL' +
                            '<img class="sslTooltip" src="http://' + LivedriveDomain + '/Content/images/form-input-question.png" title=" - If ticked, SSL will be used to encrypt all data as it travels between your web browser and servers. Browsing your web portal and downloading or uploading files will be slower." style="margin-left: 5px;">';
                }
            }
        } else {
            if (SSLDefaultValue == true) {
                SSLHtml = '<input type="hidden" value="true" name="usessl" >';
            }
            else {
                SSLHtml = '<input type="hidden" value="false" name="usessl">';
            }
        }

        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //  Control Creation
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        document.getElementById(ElementId).innerHTML =
                '<div id="LDLoginBox">' +
                '<form id="LoginForm" class="LDLoginForm" method="post" action="http://' + LivedriveDomain + '/Login">' +
                    '<ul class="LDLoginList">' +
                        ErrorHtml +
                        '<li class="LDLoginListItem">' +
                            '<label for="Username" class="LDLoginLabel">Email Address</label>' +
                            '<input id="username" type="text" value="' + LDgetQueryVariable('email') + '" name="ctlUsername" class="LDLoginValue"  >' +
                        '</li>' +
                        '<li class="LDLoginListItem">' +
                            '<label for="Password" class="LDLoginLabel">Password</label>' +
                            '<input id="password" type="password" name="ctlPassword" class="LDLoginValue">' +
                        '</li>' +
                        BusinessHtml +
                        '<li class="LDLoginListItem">' +
                            '<label for="LoginButton"></label>' +
                            '<div class="LDLoginButtonBorder">' +
                                '<input type="submit" value="Login" name="LoginButton" class="LDLoginButton">' +
                                '</div>' +
                                SSLHtml +
                                '<input type="hidden" value="' + APIKeyMD5 + '" name="APIKeyMP5" >' +
                                '<input type="hidden" value="' + window.location.protocol + '//' + window.location.host + window.location.pathname + '" name="SourceUrl" >' +
                                '<input type="hidden" value="' + window.location.search + '" name="QueryString" >' +
                        '</li>' +
                    '</ul>' +
                '</form>' +
                '</div>';

        LDToggleLoginMethod(LivedriveDomain);
        return true;
    }
    catch (ex) {

        return ex;
    }
}

function LDToggleLoginMethod(LivedriveDomain) {
    if (document.getElementById('LoginBusinessAdmin') != null && document.getElementById('LoginBusinessAdmin').checked) {
        document.getElementById('LoginForm').action = 'http://'+ LivedriveDomain + '/BusinessPortal/Authorisation/ResellerLogin/Stats';

    } else {
    document.getElementById('LoginForm').action = 'http://'+ LivedriveDomain + '/Login';
    }
}


function LDgetQueryVariable(variable) {

    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == 'ld' + variable) {
            return pair[1];
        }
    }
    return '';
}

function LDendsWith(str, suffix) {
    return str.indexOf(suffix, str.length - suffix.length) !== -1;
}

