﻿
function PredictiveIntent() {

    this.init = function() { }

    this.NotifyWithSearchKeywork = function(searchTerm) {
        try {
            if (searchTerm.length > 0) {
                var jData = this.PopulateJsonData("search", searchTerm);

                this.NotifyPI(jData);
            }
        }
        catch (ex) {
            alert(ex);
        }
    }

    this.NotifyWithEventType = function(type, productID, searchTerm, notify) {
        try {
            var jData = this.PopulateJsonDataWithProductId(type, searchTerm, productID, notify);

            this.NotifyPI(jData);
        }
        catch (ex) {
            alert(ex);
        }
    }


    this.NotifyPIForSearch = function() {
        try {

            var send = this.GetControlValue("piSend");

            if (send == "true" || send == "True" || send == "TRUE") {

                var jData = this.PopulateJsonData("search");

                this.NotifyPI(jData);
            }
        }
        catch (ex) {
            alert(ex);
        }
    }

    this.PopulateJsonData = function(notifyType) {

        var customerId = this.GetControlValue("piCid");
        var productId = this.GetControlValue("piPid");
        var sessionId = this.GetControlValue("piSid");
        var cookieId = this.GetControlValue("picookie");
        var termName = this.GetControlValue("piTermName");
        var storeId = this.GetControlValue("piStoreId");

        var jsonDataVal = "{'type':'" + notifyType + "','customerId':'" + customerId + "','productId':'" + productId + "','sessionId':'" + sessionId + "','cookieId':'" + cookieId + "','termName':'" + termName + "','storeId':'" + storeId + "'}";

        return jsonDataVal;
    }

    this.PopulateJsonData = function(notifyType, searchTerm) {

        var customerId = this.GetControlValue("piCid");
        var productId = this.GetControlValue("piPid");
        var sessionId = this.GetControlValue("piSid");
        var cookieId = this.GetControlValue("picookie");
        var storeId = this.GetControlValue("piStoreId");

        var jsonDataVal = "{'type':'" + notifyType + "','customerId':'" + customerId + "','productId':'" + productId + "','sessionId':'" + sessionId + "','cookieId':'" + cookieId + "','termName':'" + searchTerm + "','storeId':'" + storeId + "','isSuggestible':'true'}";

        return jsonDataVal;
    }

    this.GetControlValue = function(ctrlName) {
        var ctrl = document.getElementById(ctrlName);

        if (ctrl != null && ctrl != undefined) {
            return ctrl.value;
        }
        else {
            return '';
        }
    }

    this.PopulateJsonDataWithProductId = function(notifyType, searchTerm, ProductId, suggestible) {

        var customerId = this.GetControlValue("piCid");
        var sessionId = this.GetControlValue("piSid");
        var cookieId = this.GetControlValue("picookie");
        var storeId = this.GetControlValue("piStoreId");

        var jsonDataVal = "{'type':'" + notifyType + "','customerId':'" + customerId + "','productId':'" + ProductId + "','sessionId':'" + sessionId + "','cookieId':'" + cookieId + "','termName':'" + searchTerm + "','storeId':'" + storeId + "','isSuggestible':'" + suggestible + "'}";

        return jsonDataVal;
    }

    this.PopulateJsonDataForSuggestion = function(xmlPackage, noOfResult, requestCode, storeId) {
        var cookieId = this.GetControlValue("picookie");
        return "{'xmlPackage':'" + xmlPackage + "','noOfResult':'" + noOfResult + "','requestCode':'" + requestCode + "','storeId':'" + storeId + "','cookieId':'" + cookieId + "'}";
    }

    this.GetSiteUrl = function(methodName) {
        var siteURL = Egghead.application.path() + "/PIWebService.asmx/" + methodName;
        return siteURL;
    }

    this.IsAllowed = function() {
        //Check whether client is allowed to access pi or not
        var execute = this.GetControlValue("piAllow");

        if (execute == "false" || execute == "False" || execute == "FALSE") {
            return false;
        }
        return true;
    }

    this.PopulateSuggestResult = function(xmlPackage, noOfResult, requestCode, storeId, controlToUpdate) {
        try {
            if (!this.IsAllowed()) {
                return;
            }

            var passData = this.PopulateJsonDataForSuggestion(xmlPackage, noOfResult, requestCode, storeId);

            var siteURL = this.GetSiteUrl("PopulateSuggestResult");

            $.ajax({
                type: "POST",
                url: siteURL,
                data: passData,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                beforeSend: function() { },
                success: function(msg) { if (controlToUpdate != null && document.getElementById(controlToUpdate) != undefined) { document.getElementById(controlToUpdate).innerHTML = msg.d; } },
                error: function(msg) { alert("Error:" + msg); },
                complete: function() { }
            });


        } catch (e)
         { alert(e); }
    }

    this.NotifyLoginEvent = function(type) {

        try {

            var passData = this.PopulateJsonDataWithProductId(type, '', 0, false);

            this.NotifyPI(passData);
        }
        catch (ex) {
            alert(ex);
        }
    }

    this.NotifyPI = function(passData) {
        try {

            if (!this.IsAllowed()) {
                return;
            }

            var siteURL = this.GetSiteUrl("SendNotification");

            $.ajax({
                type: "POST",
                url: siteURL,
                data: passData,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                beforeSend: function() { },
                success: function(msg) { },
                error: function(msg) { },
                complete: function() { }
            });
        }
        catch (ex) {
            alert(ex);
        }
    }
}
