﻿function TrackingProcessor(containerId,cookieGuid,sessionGuid,displayConfigurationId,dropDownIds,questions, trackingUrl, userId, trackingEnabled) 
{
    this.ContainerId = containerId;
    this.UserGuid = cookieGuid;
    this.SessionGuid = sessionGuid; 
    this.DisplayConfigurationId = displayConfigurationId;
    this.DropDownIds = dropDownIds;
    this.Questions = questions;
    this.TrackingUrl = document.location.protocol + '//' + trackingUrl;
    this.UserId = userId;
    this.TrackingEnabled = trackingEnabled;
    
    this.ActivityTypeID = 1;
    this.ReportRecommendedProductID = -1;
    this.HasExportProductReportCheckboxBeenSelected = false;
    this.HasExportProductComparisonCheckboxBeenSelected = false;
    this.HasExportBrochureCheckboxBeenSelected = false;
    this.HaveClientDetailsBeenEnteredIntoReport = false;
    this.FeaturedQuestions = null;

     
    // recieves calls from button clicks, builds the grid state into an TrackingEvent and submits it to the TrackingProxy.
    TrackingProcessor.prototype.ButtonClick = function(scope, buttonId) {

        if (scope.TrackingEnabled == 'False') {
            return;
        }

        scope.ResetGrid(scope);

        // build up the MI params
        var event = new TrackingEvent();
        event.UserGuid = scope.UserGuid;
        event.DisplayConfigurationId = scope.DisplayConfigurationId;
        event.Products = scope.GetProductIds(scope);
        event.Questions = scope.GetQuestionIds(scope);
        event.SessionGuid = this.SessionGuid;
        event.CheckBoxState = 0;
        event.CheckBoxId = -1;
        event.ResponseId = buttonId;
        event.UserId = scope.UserId;
        event.ActivityTypeID = 1; // Standard Usage. 

        // call the MI proxy
        var sTrackingUrl = scope.TrackingUrl;
        
        var proxy = new wsTrackingProxy(sTrackingUrl);
        proxy.submitTrackingEvent(event);
    }

    // gets the IDs of the selected product Ids in the dropdowns.
    TrackingProcessor.prototype.GetProductIds = function(scope) {

        if (scope.TrackingEnabled == 'False') {
            return;
        }
    
        var arProductIds = new Array(scope.DropDownIds.length);

        //max number of products to record is 4...error will be thrown on > 4
        var maximumRecordableProducts = scope.DropDownIds.length;
        if (maximumRecordableProducts > 4) {
            maximumRecordableProducts = 4;
        } 

        for (var i = 0; i < maximumRecordableProducts; i++) 
        {
            arProductIds[i] = document.getElementById(scope.ContainerId +'_ddl'+ scope.DropDownIds[i]).value;
        }
        return arProductIds;
    }

    // recieves calls from check box clicks, builds the grid state into an TrackingEvent and submits it to the TrackingProxy.
    TrackingProcessor.prototype.CheckBoxChanged = function(scope, rowIndex, checkbox) {


        if (scope.TrackingEnabled == 'False') {
            return;
        }
        
        var iCheckBoxState = 0;
        if(checkbox.checked)
            iCheckBoxState = 1;
        else if(!checkbox.checked)
            iCheckBoxState = 0;

        // build up the MI params
        var event = new TrackingEvent();
        event.UserGuid = scope.UserGuid;
        event.DisplayConfigurationId = scope.DisplayConfigurationId;
        event.Products = scope.GetProductIds(scope);
        event.Questions = scope.GetQuestionIds(scope);
        event.SessionGuid = this.SessionGuid;     
        event.CheckBoxState = iCheckBoxState;
        event.CheckBoxId = scope.GetQuestionIdByRowIndex(scope, rowIndex);
        event.ResponseId = -1;
        event.UserId = scope.UserId;
        event.ActivityTypeID = 1; // Standard Usage.

       // call the MI proxy
        var sTrackingUrl = scope.TrackingUrl;
        var proxy = new wsTrackingProxy(sTrackingUrl);
        proxy.submitTrackingEvent(event);
    }

    // recieves calls from individual drop down controls, builds the grid state into an TrackingEvent and submits it to the TrackingProxy.
    TrackingProcessor.prototype.ProductDropDownChanged = function (scope) {

        if (scope.TrackingEnabled == 'False') {
            return;
        }
    
        scope.ResetGrid(scope);

        // build up the MI params
        var event = new TrackingEvent();
        event.UserGuid = scope.UserGuid;
        event.DisplayConfigurationId = scope.DisplayConfigurationId;
        event.Products = scope.GetProductIds(scope);
        event.Questions = scope.GetQuestionIds(scope);
        event.SessionGuid = this.SessionGuid;
        event.CheckBoxState = 0;
        event.CheckBoxId = -1;
        event.ResponseId = -1;
        event.UserId = scope.UserId;
        event.ActivityTypeID = 1; // Standard Usage.

       // call the MI proxy
        var sTrackingUrl = scope.TrackingUrl;
        var proxy = new wsTrackingProxy(sTrackingUrl);
        proxy.submitTrackingEvent(event);
    }

    // Called when a report has been generated!
    //
    TrackingProcessor.prototype.ReportGenerated = function(scope, parentId) {

        if (scope.TrackingEnabled == 'False') {
            return;
        }

        // build up the MI params
        var event = new TrackingEvent();

        event.UserGuid = scope.UserGuid;
        event.DisplayConfigurationId = scope.DisplayConfigurationId;
        event.Products = scope.GetProductIds(scope);
        event.Questions = scope.GetQuestionIds(scope);
        event.SessionGuid = this.SessionGuid;
        event.CheckBoxState = 0;
        event.CheckBoxId = -1;
        event.ResponseId = -1;
        event.UserId = scope.UserId;
        event.ActivityTypeID = 2; // Reporting. 

        event.ReportRecommendedProductID = scope.GetRecommendedProductIDForReport(parentId);
        event.HasExportProductReportCheckboxBeenSelected = scope.GetCheckedStatusOfProductReportExportCheckbox(parentId);
        event.HasExportProductComparisonCheckboxBeenSelected = scope.GetCheckedStatusOfProductComparisonCheckbox(parentId);
        event.HasExportBrochureCheckboxBeenSelected = scope.GetCheckedStatusOfBrochureExportCheckbox(parentId);
        event.HaveClientDetailsBeenEnteredIntoReport = scope.GetInputStatusOfClientDetailFields(parentId);
        event.FeaturedQuestions = scope.GetFeaturedQuestionIDs(scope);

        // call the MI proxy
        var sTrackingUrl = scope.TrackingUrl;
        var proxy = new wsTrackingProxy(sTrackingUrl);
        proxy.submitTrackingEvent(event);
    }

    TrackingProcessor.prototype.GetRecommendedProductIDForReport = function(parentId) {
        var dropdownID = parentId + "_ddlPopup";
        var dropdown = document.getElementById(dropdownID);

        if (dropdown == null) {
            return -1;
        }

        return dropdown.options[dropdown.selectedIndex].value;
    }

    TrackingProcessor.prototype.GetCheckedStatusOfProductReportExportCheckbox = function(parentId) {
        var checkBoxID = parentId + "_chkJustify";
        var checkBox = document.getElementById(checkBoxID);

        if (checkBox == null) {
            return false;
        }

        return checkBox.checked;
    }

    TrackingProcessor.prototype.GetCheckedStatusOfProductComparisonCheckbox = function(parentId) {
        var checkBoxID = parentId + "_chkImage";
        var checkBox = document.getElementById(checkBoxID);

        if (checkBox == null) {
            return false;
        }

        return checkBox.checked;
    }

    TrackingProcessor.prototype.GetCheckedStatusOfBrochureExportCheckbox = function(parentId) {
        var checkBoxID = parentId + "_chkDescription";
        var checkBox = document.getElementById(checkBoxID);

        if (checkBox == null) {
            return false;
        }

        return checkBox.checked;
    }

    TrackingProcessor.prototype.GetInputStatusOfClientDetailFields = function(parentId) {
        var title = $('input[name=' + parentId + '_txtTitle]').val();
        var forename = $('input[name=' + parentId + '_txtForename]').val();
        var surname = $('input[name=' + parentId + '_txtSurname]').val();
        var clientRef = $('input[name=' + parentId + '_txtClientRef]').val();
        var addressLine1 = $('input[name=' + parentId + '_txtAddress1]').val();
        var addressLine2 = $('input[name=' + parentId + '_txtAddress2]').val();
        var addressLine3 = $('input[name=' + parentId + '_txtAddress3]').val();
        var addressLine4 = $('input[name=' + parentId + '_txtAddress4]').val();
        var addressLine5 = $('input[name=' + parentId + '_txtAddress5]').val();
        var preparedBy = $('input[name=' + parentId + '_txtPreparedBy]').val();
        var hasTitleBeenSet = (title.length > 0);
        var hasForenameBeenSet = (forename.length > 0);
        var hasSurnameBeenSet = (surname.length > 0);
        var hasNameBeenSet = ((hasTitleBeenSet && hasForenameBeenSet && hasSurnameBeenSet) ||
                            (hasForenameBeenSet && hasSurnameBeenSet) ||
                            (hasTitleBeenSet && hasSurnameBeenSet));
        var hasClientRefBeenSet = (clientRef.length > 0);
        var hasAddressLine1BeenSet = (addressLine1.length > 0);
        var hasAddressLine2BeenSet = (addressLine2.length > 0);
        var hasAddressLine3BeenSet = (addressLine3.length > 0);
        var hasAddressLine4BeenSet = (addressLine4.length > 0);
        var hasAddressLine5BeenSet = (addressLine5.length > 0);
        var hasPreparedByBeenSet = (preparedBy.length > 0);

        return (hasNameBeenSet || hasClientRefBeenSet || hasAddressLine1BeenSet || hasAddressLine2BeenSet ||
                hasAddressLine3BeenSet || hasAddressLine4BeenSet || hasAddressLine5BeenSet || hasPreparedByBeenSet);
    }

    // Called when the print button is clicked inside of compare - not called when the browser's print button
    // is used. 
    // 
    TrackingProcessor.prototype.PrintButtonClicked = function(scope) {

        if (scope.TrackingEnabled == 'False') {
            return;
        }
        
        // build up the MI params
        var event = new TrackingEvent();
        event.UserGuid = scope.UserGuid;
        event.DisplayConfigurationId = scope.DisplayConfigurationId;
        event.Products = scope.GetProductIds(scope);
        event.Questions = scope.GetQuestionIds(scope);
        event.SessionGuid = this.SessionGuid;
        event.CheckBoxState = 0;
        event.CheckBoxId = -1;
        event.ResponseId = -1;
        event.UserId = scope.UserId;
        event.ActivityTypeID = 3; // Printing.

        // call the MI proxy
        var sTrackingUrl = scope.TrackingUrl;
        var proxy = new wsTrackingProxy(sTrackingUrl);
        proxy.submitTrackingEvent(event);
    }    

    // gets the question and question header IDs where checkbox.value == true
    TrackingProcessor.prototype.GetQuestionIds = function(scope) {
        var containerID = scope.ContainerId;

        if (scope.TrackingEnabled == 'False') {
            return;
        }

        var arServerQuestions = scope.Questions;
        var arQuestionIds = new Array;
        var questions = $('#' + scope.ContainerId + '_tblCompareQuestions .questionCell');

        //max number of questions to record is 20...error will be thrown on > 20 
        var maximumRecordableQuestions = questions.length;
        if (maximumRecordableQuestions > 20) {
            maximumRecordableQuestions = 20;
        }

        for (var i = 0; i < maximumRecordableQuestions; i++) {
            if (questions[i].childNodes[0].tagName == 'SELECT') {

                var dropDowns = questions[i].childNodes[0];
                for (var c = 0; c < dropDowns.length; c++) {
                    if (dropDowns.options[c].selected == true) {
                        arQuestionIds[i] =parseInt(dropDowns.options[c].value);
                    }
                }
            }
            else {
                arQuestionIds[i] = parseInt(arServerQuestions[i]);
            }
        }

        return arQuestionIds;
    }

    TrackingProcessor.prototype.GetFeaturedQuestionIDs = function(scope) {

        if (scope.TrackingEnabled == 'False') {
            return;
        }

        var containerID = scope.ContainerId;
        var arServerQuestions = scope.GetQuestionIds(scope);
        var arQuestionIds = new Array;
        var featureCheckboxes = $('#' + scope.ContainerId + '_tblCompareQuestions .featureCheckbox');
        var checkbox;

        for (var i = 0; i < featureCheckboxes.length; i++) {

            checkbox = featureCheckboxes[i].childNodes[0];

            if (checkbox.checked) {
                arQuestionIds[arQuestionIds.length] = parseInt(arServerQuestions[i]);
            }
        }

        return arQuestionIds;
    }

    // gets the question and question header IDs where checkbox.value == true
    TrackingProcessor.prototype.GetQuestionIdByRowIndex = function(scope, index) {


        if (scope.TrackingEnabled == 'False') {
            return;
        }
    
        var table = document.getElementById(scope.ContainerId + '_tblCompareQuestions');
        var grid = null;
        var arServerQuestions = scope.Questions;
        
        if (table.childNodes.length > 0) {
            grid = table.childNodes[0];
            if (grid.childNodes.length > index) {
                var row = grid.childNodes[index];

                var iLastCell = row.childNodes.length - 1;
                if (row.childNodes[iLastCell].childNodes.length > 0              // check a checkbox exists
                && row.childNodes[iLastCell].childNodes[0].tagName == 'INPUT')   // check the cell has controls
                {
                    var iQuestionCol = 1;

                    // if the row is a dropdown question then get the question id from the drop down
                    // otherwise get the question id from the array of question Ids kept in inpQuestions

                    // get the question id
                    // first check if its a drop down
                    if (row.childNodes.length > iQuestionCol                             // check the row has cells
                    && row.childNodes[iQuestionCol].childNodes.length > 0               // check the cell has controls
                    && row.childNodes[iQuestionCol].childNodes[0].tagName == 'SELECT')  // check the control is a select
                    {
                        // add the question ID
                        return parseInt(row.childNodes[iQuestionCol].childNodes[0].value);
                    }
                    // now check if its a label
                    if (row.childNodes.length > iQuestionCol
                    && row.childNodes[iQuestionCol].childNodes.length > 0
                    && row.childNodes[iQuestionCol].childNodes[0].tagName == 'SPAN') {
                        // add the questionID
                        return parseInt(arServerQuestions[index - 1]);
                    }
                }
            }
        }
        return -1;
    }

    // sets all check boxes to be unchecked
    TrackingProcessor.prototype.ResetGrid = function(scope) {

        if (scope.TrackingEnabled == 'False') {
            return;
        }
    
        var table = document.getElementById(scope.ContainerId + '_tblCompareQuestions');
        if (table.childNodes.length > 0) 
        {
            grid = table.childNodes[0];
            for (var i = 1; i < grid.childNodes.length; i++) 
            {
                var row = grid.childNodes[i];
                
                var iLastCell = row.childNodes.length - 1;
                if(row.childNodes[iLastCell].childNodes.length > 0 && row.childNodes[iLastCell].childNodes[0].tagName == 'INPUT') 
                {
                    row.childNodes[iLastCell].childNodes[0].checked = false;
                }
            }
        }
    }
}
