﻿// represents a proxy that communicates to the Compare Tracker.svc
function wsTrackingProxy(TrackingUrl) {
    this.sTrackingUrl = TrackingUrl;

    wsTrackingProxy.prototype.submitTrackingEvent = function(TrackingEvent) {

        var xmlHttpObj = CreateXmlHttpRequestObject();
        if (xmlHttpObj) {
            var url = this.sTrackingUrl + "/SubmitEventAsJson";
            xmlHttpObj.open("POST", url, true);
            xmlHttpObj.onreadystatechange = function() { }
            var body = TrackingEvent.toJSON();
            xmlHttpObj.setRequestHeader("Content-type", "application/json");
            xmlHttpObj.send(body);
        }
    }
}

// represents the object of the service side TrackingEventInput
function TrackingEvent() {

    TrackingEvent.prototype.UserGuid = '';
    TrackingEvent.prototype.DisplayConfigurationId = 0;
    TrackingEvent.prototype.Products = null;
    TrackingEvent.prototype.Questions = null;
    TrackingEvent.prototype.SessionGuid = '';
    TrackingEvent.prototype.CheckBoxState = false;
    TrackingEvent.prototype.CheckBoxId = 0;
    TrackingEvent.prototype.ResponseId = 0;
    TrackingEvent.prototype.UserId = -1;

    TrackingEvent.prototype.ActivityTypeID = 1;
    TrackingEvent.prototype.ReportRecommendedProductID = -1;
    TrackingEvent.prototype.HasExportProductReportCheckboxBeenSelected = false;
    TrackingEvent.prototype.HasExportProductComparisonCheckboxBeenSelected = false;
    TrackingEvent.prototype.HasExportBrochureCheckboxBeenSelected = false;
    TrackingEvent.prototype.HaveClientDetailsBeenEnteredIntoReport = false;
    TrackingEvent.prototype.FeaturedQuestions = null;

    // returns the class formatted as JSON
    TrackingEvent.prototype.toJSON = function() {
        var sRet = "{";

        sRet += "\"checkBoxId\":" + this.CheckBoxId + ",";
        sRet += "\"checkBoxState\":" + this.CheckBoxState + ",";
        sRet += "\"displayConfigurationId\":" + this.DisplayConfigurationId + ",";
        
        // products
        sRet += "\"products\":[";

        if (this.Products != null) {
            for (var i = 0; i < this.Products.length; i++) {
                if (i == 0) {
                    sRet += this.Products[i];
                }
                else {
                    sRet += "," + this.Products[i];
                }
            }
        }
        sRet += "],";

        // questions
        sRet += "\"questions\":[";

        if (this.Questions != null) {
            for (var i = 0; i < this.Questions.length; i++) {
                if (i == 0) {
                    sRet += this.Questions[i];
                }
                else {
                    sRet += "," + this.Questions[i];
                }
            }
        }
        sRet += "],";

        sRet += "\"responseId\":" + this.ResponseId + ",";
        sRet += "\"sessionToken\":\"" + this.SessionGuid + "\",";
        sRet += "\"userToken\":\"" + this.UserGuid + "\",";
        sRet += "\"userId\":" + this.UserId + ",";
        sRet += "\"reportRecommendedProductID\":" + this.ReportRecommendedProductID + ",";
        sRet += "\"hasExportProductReportCheckboxBeenSelected\":" + this.HasExportProductReportCheckboxBeenSelected + ",";
        sRet += "\"hasExportProductComparisonCheckboxBeenSelected\":" + this.HasExportProductComparisonCheckboxBeenSelected + ",";
        sRet += "\"hasExportBrochureCheckboxBeenSelected\":" + this.HasExportBrochureCheckboxBeenSelected + ",";
        sRet += "\"haveClientDetailsBeenEnteredIntoReport\":" + this.HaveClientDetailsBeenEnteredIntoReport + ",";
        sRet += "\"activityType\":" + this.ActivityTypeID + ",";

        sRet += "\"featuredQuestions\":[";

        if (this.FeaturedQuestions != null) {
            for (var i = 0; i < this.FeaturedQuestions.length; i++) {
                if (i == 0) {
                    sRet += this.FeaturedQuestions[i];
                }
                else {
                    sRet += "," + this.FeaturedQuestions[i];
                }
            }
        }
        sRet += "]";        
        
        sRet += "}";

        return sRet;
    }
}
