var initESW = function(gslbBaseURL) {
    embedded_svc.settings.displayHelpButton = false;
    embedded_svc.settings.language = 'en';
    embedded_svc.settings.offlineSupportMinimizedText = 'Offline - Email Us';

    embedded_svc.settings.extraPrechatFormDetails = [{
        'label':'First Name',
        'transcriptFields': ['First_Name__c']
    },{
        'label':'Last Name',
        'transcriptFields': ['Last_Name__c']
    },{
        'label':'Email',
        'transcriptFields': ['Email__c']
    },{
        'label':'Phone',
        'transcriptFields': ['Phone__c']
    },{
        'label':'Company',
        'transcriptFields': ['Company__c']
    },{
        'label':'Product Area',
        'value':'Global Knowledge',
        'displayToAgent': false
    }];

    // Contact and Case field mappings
    // We do not use the OOTB Contact creation as there is already custom code on Case insert trigger that does this
    // But will use the lookup option to link a matched Contact to the Transcript by email
    embedded_svc.settings.extraPrechatInfo = [{
        'entityName': 'Case',
        'entityFieldMaps': [{
            'doCreate': true,
            'doFind': false,
            'fieldName': 'Supplied_First_Name__c',
            'isExactMatch': false,
            'label': 'First Name'
        },{
            'doCreate': true,
            'doFind': false,
            'fieldName': 'Supplied_Last_Name__c',
            'isExactMatch': false,
            'label': 'Last Name'
        },{
            'doCreate': true,
            'doFind': false,
            'fieldName': 'Supplied_Email__c',
            'isExactMatch': false,
            'label': 'Email'
        },{
            'doCreate': true,
            'doFind': false,
            'fieldName': 'Supplied_Phone__c',
            'isExactMatch': false,
            'label': 'Phone'
        },{
            'doCreate': true,
            'doFind': false,
            'fieldName': 'Supplied_Company__c',
            'isExactMatch': false,
            'label': 'Company'
        },{
            'doCreate': true,
            'doFind': false,
            'fieldName': 'Product_Area__c',
            'isExactMatch': false,
            'label': 'Product Area'
        }],
        'saveToTranscript': 'CaseId'
    },{
        'entityName': 'Contact',
        'entityFieldMaps': [{
            'doCreate': false,
            'doFind': false,
            'fieldName': 'FirstName',
            'isExactMatch': false,
            'label': 'First Name'
        },{
            'doCreate': false,
            'doFind': false,
            'fieldName': 'LastName',
            'isExactMatch': false,
            'label': 'Last Name'
        },{
            'doCreate': false,
            'doFind': false,
            'fieldName': 'Email',
            'isExactMatch': true,
            'label': 'Email'
        }],
        'saveToTranscript': 'ContactId',
        'showOnCreate': false
    }];

    embedded_svc.settings.enabledFeatures = ['LiveAgent'];
    embedded_svc.settings.entryFeature = 'LiveAgent';

    embedded_svc.init(
        'https://skillsoft.my.salesforce.com',
        'https://skillsoft.my.salesforce-sites.com/',
        gslbBaseURL,
        '00D30000001Fwjk',
        'Global_Knowledge',
        {
            baseLiveAgentContentURL: 'https://c.la1-c1-ia4.salesforceliveagent.com/content',
            deploymentId: '572a00000000T2d',
            buttonId: '5734Q000000PX0U',
            baseLiveAgentURL: 'https://d.la1-c1-ia4.salesforceliveagent.com/chat',
            eswLiveAgentDevName: 'EmbeddedServiceLiveAgent_Parent04I4Q0000008OLxUAM_17cb8cf54af',
            isOfflineSupportEnabled: true
        }
    );
};

if (!window.embedded_svc) {
    var s = document.createElement('script');
    s.setAttribute('src', 'https://skillsoft.my.salesforce.com/embeddedservice/5.0/esw.min.js');
    s.onload = function() {
        initESW(null);
    };
    document.body.appendChild(s);
} else {
    initESW('https://service.force.com');
}