﻿
//NOTES : Create/Update cookie for member once they logged in to the site, If they remove cookie by that time then system will not able to track it.
// This javascript file will act as a plugin for storefront and non storefront sites.

function LoyaltyPointEngine(instanceName, showAlerts) {

    //Member
    function Member() {

        var memberId;
        var memberSiteId;
        var siteId;
        var date;
        var loyaltyCode;
        var referralId;
        var clientCode;
        var storeId;

        this.InitJson = function(jsonData) {

            if (jsonData.Status === "success") {
                this.clientCode = jsonData.ClientCode;
                this.memberId = jsonData.MemberId;
                this.memberSiteId = jsonData.MemberSiteId;
                this.siteId = jsonData.SiteId;
                this.loyaltyCode = jsonData.LoyaltyCode;
                this.referralId = jsonData.ReferralLoyaltyId;
                this.storeId = jsonData.StoreId;
                this.date = new Date();
            }
            else {
                this.clientCode = "";
                this.memberId = 0;
                this.memberSiteId = 0;
                this.storeId = 0;
                this.loyaltyCode = "";
                this.siteId = 0;
            }
        }

        this.Init = function(memberid, membersiteId, siteid, loyaltycode, ReferralId, clientcode, storeid) {
            this.memberId = memberid;
            this.memberSiteId = membersiteId;
            this.siteId = siteid;
            this.loyaltyCode = loyaltycode;
            this.referralId = ReferralId;
            this.date = new Date();
            this.clientCode = clientcode;
            this.storeId = storeid;
        }

        this.InitFromCookie = function(cookieValue, separator) {

            _cookieMgr = new CookieManager();

            var readCookieValue = _cookieMgr.GetCookieValue(mCookieName);

            if (readCookieValue != null && readCookieValue.length > 0) {
                var parts = readCookieValue.split(separator);
                this.memberId = parts[0];
                this.memberSiteId = parts[1];
                this.siteId = parts[2];
                this.loyaltyCode = parts[3];
                this.referralId = parts[4];
                this.date = new Date(parts[5]);
                this.clientCode = parts[6];
                this.storeId = parts[7];
            }
            else {
                this.memberId = 0;
                this.memberSiteId = 0;
                this.siteId = 0;
                this.loyaltyCode = "";
                this.referralId = 0;
                this.clientCode = "";
                this.storeId = 0;

                _cookieMgr.UpdateCookie(this.ToValueString(";"));
            }
        }

        this.SetMemberId = function(memberid) {
            this.memberId = memberId;
        }
        this.GetMemberId = function() {
            return this.memberId;
        }
        this.SetMemberSiteId = function(memberSiteid) {
            this.memberSiteId = memberSiteid;
        }
        this.GetMemberSiteId = function() {
            return this.memberSiteId;
        }
        this.SetSiteId = function(siteid) {
            this.siteId = siteid;
        }
        this.GetSiteId = function() {
            return this.siteId;
        }
        this.GetLastVistiDate = function() {
            return this.date;
        }
        this.GetLoyaltyCode = function() {
            return this.loyaltyCode;
        }

        this.GetReferralId = function() {
            return this.referralId;
        }

        this.GetClientCode = function() {
            return this.clientCode;
        }

        this.GetStoreId = function() {
            return this.storeId;
        }

        this.Compares = function(member) {
            if (this.memberId == member.memberId &&
        this.memberSiteId == member.memberSiteId &&
        this.siteId == member.siteId &&
        this.loyaltyCode == member.loyaltyCode &&
        this.referralId == member.referralId &&
        this.clientCode == member.clientCode &&
        this.storeId == member.storeId) {
                return true;
            }
            else {
                return false;
            }
        }

        this.ToValueString = function(separator) {
            return decodeURI(this.GetMemberId() + separator +
                         this.GetMemberSiteId() + separator +
                         this.GetSiteId() + separator +
                         this.GetLoyaltyCode() + separator +
                         this.GetReferralId() + separator +
                         new Date().toDateString() + separator +
                         this.GetClientCode() + separator +
                         this.GetStoreId());
        }
    }

    //ConstructURLData
    function ConstructURLData() {
        //To Generate WebServiceURL
        this.GetBaseURL = function() {
            //return "http://localhost/LoyaltyPointEngine/LoyaltyService.asmx";
            return "http://www.eggheadstaging2.co.uk/EElpeservice/LoyaltyService.asmx";
	    //return "https://www.aspdnsf.co.uk/EELPEServicelive/LoyaltyService.asmx";
        }

        this.GetQStrBase = function(clientCode, storeId) {
            return "?clientCode=" + clientCode + "&domain=" + window.location.hostname + "&storeId=" + storeId;
        }

        this.GetQStrBasePopulate = function(clientCode, storeId, memberSiteId) {
            return this.GetQStrBase(clientCode, storeId) + "&memberSiteId=" + memberSiteId;
        }

        this.GetQStrBaseWithEmail = function(clientCode, storeId, memberSiteId, email) {
            if (email == null) {
                email = '';
            }
            return this.GetQStrBasePopulate(clientCode, storeId, memberSiteId) + "&email=" + email;
        }

        this.GetQStrForLoyaltyPoints = function(clientCode, storeId, memberSiteId, points) {
            return this.GetQStrBasePopulate(clientCode, storeId, memberSiteId) + "&points=" + points;
        }

        this.GetQStrForLoyaltyCode = function(clientCode, storeId, memberSiteId, loyaltyCode) {
            return this.GetQStrBasePopulate(clientCode, storeId, memberSiteId) + "&loyaltyCode=" + loyaltyCode;
        }

        this.GetQStrForLoyaltyCodeReferrer = function(clientCode, storeId, referrelMemberSiteId, loyaltyCode) {
            return this.GetQStrBase(clientCode, storeId) + "&referrerMemberSiteId=" + referrelMemberSiteId
                + "&loyaltyCode=" + loyaltyCode;
        }

        this.GetQStrForReferral = function(clientCode, storeId, memberSiteId, referredMemberId, referredEmail) {
            return this.GetQStrBasePopulate(clientCode, storeId, memberSiteId)
        + "&referredmemberSiteId=" + referredMemberId + "&referredEmail=" + referredEmail;
        }

        this.GetQStrForReferralByEmail = function(clientCode, storeId, memberSiteId, email, loyaltyCode) {
            return this.GetQStrBasePopulate(clientCode, storeId, memberSiteId)
        + "&email=" + email + "&loyaltyCode=" + loyaltyCode;
        }

        this.GetQStrForReferralBySelf = function(clientCode, storeId, memberSiteId, loyaltyCode) {
            return this.GetQStrBasePopulate(clientCode, storeId, memberSiteId)
        + "&loyaltyCode=" + loyaltyCode;
        }

        this.GetQStrForEventLog = function(clientCode, storeId, memberSiteId, eventCode, description) {
            return this.GetQStrBasePopulate(clientCode, storeId, memberSiteId) + "&eventCode=" + eventCode + "&description=" + description
        }
    }

    function ConstructParameterData() {
        //To Generate WebServiceURL
        this.GetBaseURL = function() {
            //return "http://localhost/LoyaltyPointEngine/LoyaltyService.asmx";
            return "http://www.eggheadstaging2.co.uk/EElpeservice/LoyaltyService.asmx";
	    //return "https://www.aspdnsf.co.uk/EELPEServicelive/LoyaltyService.asmx";
        }

        this.GetQStrBaseOpen = function(clientCode, storeId) {
            return "'clientCode':'" + clientCode + "','domain':'" + window.location.hostname + "','storeId':'" + storeId + "'";
        }

        this.GetQStrBaseOpenWithRef = function(clientCode, storeId, ref) {
            return "'clientCode':'" + clientCode + "','domain':'" + window.location.hostname + "','storeId':'" + storeId + "','ref':'" + ref + "'";
        }


        this.GetQStrBaseWithRef = function(clientCode, storeId, ref) {
            return "'clientCode':'" + clientCode + "','domain':'" + window.location.hostname + "','storeId':'" + storeId + "','ref':'" + ref + "'";
        }

        this.GetQStrBase = function(clientCode, storeId) {
            return " 'clientCode':'" + clientCode + "','domain':'" + window.location.hostname + "','storeId':'" + storeId + "'";
        }

        this.GetQStrBaseLoyaltyId = function(clientCode, storeId, loyaltyId) {
            return this.GetQStrBaseOpen(clientCode, storeId) + ",'loyaltyId':'" + loyaltyId + "'";
        }

        this.GetQStrBaseLoyaltyIdWithRef = function(clientCode, storeId, loyaltyId, ref) {
            return this.GetQStrBaseOpenWithRef(clientCode, storeId, ref) + ",'loyaltyId':'" + loyaltyId + "'";
        }

        this.GetQStrBasePopulate = function(clientCode, storeId, memberSiteId) {
            return this.GetQStrBaseOpen(clientCode, storeId) + ",'memberSiteId':'" + memberSiteId + "'";
        }

        this.GetQStrBasePopulateWithRef = function(clientCode, storeId, memberSiteId, ref) {
            return this.GetQStrBaseOpenWithRef(clientCode, storeId, ref) + ",'memberSiteId':'" + memberSiteId + "'";
        }

        this.GetQStrBasePopulateOpen = function(clientCode, storeId, memberSiteId) {
            return this.GetQStrBaseOpen(clientCode, storeId) + ",'memberSiteId':'" + memberSiteId + "'";
        }

        this.GetQStrBasePopulateOpenWithRef = function(clientCode, storeId, memberSiteId, ref) {
            return this.GetQStrBaseOpenWithRef(clientCode, storeId, ref) + ",'memberSiteId':'" + memberSiteId + "'";
        }

        this.GetQStrBaseWithEmail = function(clientCode, storeId, memberSiteId, email) {
            if (email == null) {
                email = "";
            }
            return this.GetQStrBasePopulateOpen(clientCode, storeId, memberSiteId) + ",'email':'" + email + "'";
        }

        this.GetQStrBaseWithEmailWithRef = function(clientCode, storeId, memberSiteId, email, ref) {
            if (email == null) {
                email = "";
            }
            return this.GetQStrBasePopulateOpenWithRef(clientCode, storeId, memberSiteId, ref) + ",'email':'" + email + "'";
        }

        this.GetQStrForLoyaltyPoints = function(clientCode, storeId, memberSiteId, points) {
            return this.GetQStrBasePopulateOpen(clientCode, storeId, memberSiteId) + ",'points':'" + points + "'";
        }

        this.GetQStrForLoyaltyPointsWithRef = function(clientCode, storeId, memberSiteId, points, ref) {
            return this.GetQStrBasePopulateOpenWithRef(clientCode, storeId, memberSiteId, ref) + ",'points':'" + points + "'";
        }

        this.GetQStrForLoyaltyCode = function(clientCode, storeId, memberSiteId, loyaltyCode) {
            return this.GetQStrBasePopulateOpen(clientCode, storeId, memberSiteId) + ",'loyaltyCode':'" + loyaltyCode + "'";
        }

        this.GetQStrForLoyaltyCodeWithRef = function(clientCode, storeId, memberSiteId, loyaltyCode, ref) {
            return this.GetQStrBasePopulateOpenWithRef(clientCode, storeId, memberSiteId, ref) + ",'loyaltyCode':'" + loyaltyCode + "'";
        }

        this.GetQStrForLoyaltyCodeReferrer = function(clientCode, storeId, referrelMemberSiteId, loyaltyCode) {
            return this.GetQStrBaseOpen(clientCode, storeId) + ",'referrerMemberSiteId':'" + referrelMemberSiteId
                + "','loyaltyCode':'" + loyaltyCode + "'";
        }

        this.GetQStrForLoyaltyCodeReferrerWithRef = function(clientCode, storeId, referrelMemberSiteId, loyaltyCode, ref) {
            return this.GetQStrBaseOpenWithRef(clientCode, storeId, ref) + ",'referrerMemberSiteId':'" + referrelMemberSiteId
                + "','loyaltyCode':'" + loyaltyCode + "'";
        }

        this.GetQStrForReferral = function(clientCode, storeId, memberSiteId, referredMemberId, referredEmail) {
            return this.GetQStrBasePopulateOpen(clientCode, storeId, memberSiteId)
        + ",'referredmemberSiteId':'" + referredMemberId + "','referredEmail':'" + referredEmail + "'";
        }

        this.GetQStrForReferralWithRef = function(clientCode, storeId, memberSiteId, referredMemberId, referredEmail, ref) {
            return this.GetQStrBasePopulateOpenWithRef(clientCode, storeId, memberSiteId, ref)
        + ",'referredmemberSiteId':'" + referredMemberId + "','referredEmail':'" + referredEmail + "'";
        }

        this.GetQStrForReferralByEmail = function(clientCode, storeId, memberSiteId, email, loyaltyCode) {
            return this.GetQStrBasePopulateOpen(clientCode, storeId, memberSiteId)
        + ",'email':'" + email + "','loyaltyCode':'" + loyaltyCode + "'";
        }

        this.GetQStrForReferralByEmailWithRef = function(clientCode, storeId, memberSiteId, email, loyaltyCode, ref) {
            return this.GetQStrBasePopulateOpenWithRef(clientCode, storeId, memberSiteId, ref)
        + ",'email':'" + email + "','loyaltyCode':'" + loyaltyCode + "'";
        }

        this.GetQStrForReferralBySelf = function(clientCode, storeId, memberSiteId, loyaltyCode) {
            return this.GetQStrBasePopulateOpen(clientCode, storeId, memberSiteId)
        + ",'loyaltyCode':'" + loyaltyCode + "'";
        }

        this.GetQStrForReferralBySelfWithRef = function(clientCode, storeId, memberSiteId, loyaltyCode, ref) {
            return this.GetQStrBasePopulateOpenWithRef(clientCode, storeId, memberSiteId, ref)
        + ",'loyaltyCode':'" + loyaltyCode + "'";
        }

        this.GetQStrForEventLog = function(clientCode, storeId, memberSiteId, eventCode, description) {
            return this.GetQStrBasePopulateOpen(clientCode, storeId, memberSiteId) + ",'eventCode':'" + eventCode + "','description':'" + description + "'";
        }

        this.GetQStrForEventLogWithRef = function(clientCode, storeId, memberSiteId, eventCode, description, ref) {
            return this.GetQStrBasePopulateOpenWithRef(clientCode, storeId, memberSiteId, ref) + ",'eventCode':'" + eventCode + "','description':'" + description + "'";
        }
    }


    //Cookie Manager
    function CookieManager() {
    }

    //This function will collect cookie from the client and return its value
    CookieManager.prototype.GetCookieValue = function(cookieName) {
        var i, x, y, ArrayCookies = document.cookie.split(";");
        for (i = 0; i < ArrayCookies.length; i++) {
            x = ArrayCookies[i].substr(0, ArrayCookies[i].indexOf("="));
            y = ArrayCookies[i].substr(ArrayCookies[i].indexOf("=") + 1);
            x = x.replace(/^\s+|\s+$/g, "");

            if (x == cookieName) {
                return unescape(y);
            }
        }
    }

    //This function will Create/Update value of the cookie
    CookieManager.prototype.UpdateCookie = function(cookieName, value, exdays) {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + exdays);

        var cookieValue = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
        document.cookie = cookieName + "=" + cookieValue;
    }


    //This function will return whether cookie with given name exist or not
    CookieManager.prototype.IsCookieExist = function(cookieName) {
        var oCookie = this.GetCookieValue(cookieName);
        if (oCookie != null && oCookie != "") {
            return false;
        }
        else {
            //oCookie = prompt("Please enter your name:", "");
            if (oCookie != null && oCookie != "") {
                //UpdateCookie(cookieName, oCookie, 365);
                return true;
            }
            return false;
        }
    }

    CookieManager.prototype.DeleteCookie = function(cookieName) {
        //document.cookie = cookieName + ';expires=Monday, 19-Aug-1996 05:00:00 GMT';
        var cookie_date = new Date();  // current date & time
        cookie_date.setTime(cookie_date.getTime() - 1);
        document.cookie = cookieName += "=; expires=" + cookie_date.toGMTString();
    }

    //End DataMembers

    var serviceResult = "";
    //var cCookieName = "eeple_clinf";
    var resultMember = new Member();
    var mSeparator = ";";
    var _cookieMgr = new CookieManager();
    var _constructURL = new ConstructURLData();
    var _constructParamData = new ConstructParameterData();

    var displayLCodeControl = null;
    var displayLPointControl = null;
    var getLPointControl = null;
    var getLCodeControl = null;


    this.GetFieldSeparator = function() {
        return mSeparator;
    }

    var mCookieName = "eeple_minf";

    //referralcodeusagediscount
    var mV1 = "eeplev1";

    //base discount
    var mV2 = "eeplev2";

    //redeem point factore
    var mV3 = "eeplev3";

    var muc = "eeplerc";
    var mrp = "eeplerp";

    this.GetMemberCookieName = function() {
        return mCookieName;
    }

    var httpVerbGet = "GET";
    var httpVerbPost = "POST";

    this.DeleteCookies = function() {
        _cookieMgr.DeleteCookie(this.GetMemberCookieName());
        _cookieMgr.DeleteCookie(mV1);
        _cookieMgr.DeleteCookie(mV2);
        _cookieMgr.DeleteCookie(mV3);
        _cookieMgr.DeleteCookie(mrp);
        _cookieMgr.DeleteCookie(muc);
    }

    //Called from Sigin.ascx control and CreateAccount.aspx files
    this.IsRegisteredMember = function(clientCode, storeId, memberSiteId, email) {

        this.DeleteCookies();

        this.CheckValidMemberSiteId(memberSiteId, true);

        var url = _constructURL.GetBaseURL() + "/GetMemberIdByStoreId";

        var data = { 'clientCode': "'" + clientCode + "'", 'domain': "'" + window.location.hostname + "'", 'storeId': storeId, 'memberSiteId': memberSiteId, 'email': "'" + email + "'", 'ref': instanceName + '.ProcessResultWithCookie' };

        this.SendGetRequest(url, data, this.ProcessResultWithCookie);
    }


    //Called from Account.aspx 
    this.GetLoyaltyCode = function() {

        _cookieMgr.GetCookieValue(this.GetMemberCookieName());

        rMember = new Member();

        rMember.InitFromCookie(_cookieMgr.GetCookieValue(this.GetMemberCookieName()), mSeparator);

        return rMember.GetLoyaltyCode();
    }

    //Called from Account.aspx
    this.GetMemberLoyaltyPoints = function(controlToUpdate) {
        var rMember = new Member();

        rMember.InitFromCookie(_cookieMgr.GetCookieValue(this.GetMemberCookieName()), mSeparator);

        if (this.ValidateRequestData(rMember.GetClientCode(), rMember.GetSiteId(), rMember.GetMemberSiteId())) {

            var url = _constructURL.GetBaseURL() + "/GetPointsByStoreId";
            var data = { 'clientCode': "'" + rMember.GetClientCode() + "'", 'domain': "'" + window.location.hostname + "'", 'storeId': rMember.GetStoreId(), 'memberSiteId': rMember.GetMemberSiteId(), 'ref': instanceName + '.GetMemberLoyaltyPointsSuccess' };

            getLCodeControl = controlToUpdate;

            this.SendGetRequest(url, data, this.GetMemberLoyaltyPointsSuccess);
        }
    }

    this.GetMemberLoyaltyPointsSuccess = function(result) {
        try {
            try {
                if (getLCodeControl != null && getLCodeControl != undefined) {
                    $(getLCodeControl).text(result.d);
                }
            } catch (e) { }

            try {
                if (displayLCodeControl != null && getLCodeControl != undefined) {
                    $(getLCodeControl).val(result.d);
                }
            } catch (e) { }

            getLCodeControl = null;
        }
        catch (e) { }
    }

    //Called from Account.aspx 
    this.SetMemberInfo = function(email) {
        _cookieMgr.GetCookieValue(this.GetMemberCookieName());

        rMember = new Member();

        rMember.InitFromCookie(_cookieMgr.GetCookieValue(this.GetMemberCookieName()), mSeparator);

        var url = _constructURL.GetBaseURL() + "/UpdateMemberInfo";
        var data = { 'clientCode': "'" + rMember.GetClientCode() + "'", 'domain': "'" + window.location.hostname + "'", 'storeId': rMember.GetStoreId(), 'memberSiteId': rMember.GetMemberSiteId(), 'email': "'" + email + "'", 'ref': instanceName + '.ProcessResult' };

        this.SendGetRequest(url, data, this.ProcessResult);
    }

    //Send Invitation to Friend to Use Loyalty Code
    this.SetLoyaltyCodeUsage = function(loyaltyCode, email) {
        _cookieMgr.GetCookieValue(this.GetMemberCookieName());

        rMember = new Member();

        rMember.InitFromCookie(_cookieMgr.GetCookieValue(this.GetMemberCookieName()), mSeparator);

        var url = _constructURL.GetBaseURL() + "/SetReferralLoyaltyByEmail";
        var data = { 'clientCode': "'" + rMember.GetClientCode() + "'", 'domain': "'" + window.location.hostname + "'", 'storeId': rMember.GetStoreId(), 'memberSiteId': rMember.GetMemberSiteId(), 'email': "'" + email + "'", 'loyaltyCode': "'" + loyaltyCode + "'", 'ref': instanceName + '.ProcessResult' };

        this.SendGetRequest(url, data, this.ProcessResult);
    }

    //Set Redeem Loyalty Points ,Called from shoppingcart.aspx
    this.SetLoyaltyPointUsage = function(points) {
        _cookieMgr.GetCookieValue(this.GetMemberCookieName());

        rMember = new Member();

        rMember.InitFromCookie(_cookieMgr.GetCookieValue(this.GetMemberCookieName()), mSeparator);

        var url = _constructURL.GetBaseURL() + "/SetLoyaltyPointUsage";
        var data = { 'clientCode': "'" + rMember.GetClientCode() + "'", 'domain': "'" + window.location.hostname + "'", 'storeId': rMember.GetStoreId(), 'memberSiteId': rMember.GetMemberSiteId(), 'points': "'" + points + "'", 'ref': instanceName + '.ProcessResult' };

        //alert(data);

        this.SendGetRequest(url, data, this.ProcessResult);
    }

    //Get Usage Loyalty Code ,Called from shoppingcart.aspx
    this.GetUsageLoyaltyCode = function(controlToUpdate) {
        _cookieMgr.GetCookieValue(this.GetMemberCookieName());

        rMember = new Member();

        rMember.InitFromCookie(_cookieMgr.GetCookieValue(this.GetMemberCookieName()), mSeparator);

        var url = _constructURL.GetBaseURL() + "/GetCurrentLoyaltyCodeToUse";

        var data = { 'clientCode': "'" + rMember.GetClientCode() + "'", 'domain': "'" + window.location.hostname + "'", 'storeId': rMember.GetStoreId(), 'referredMemberId': rMember.GetMemberSiteId(), 'ref': instanceName + '.GetLoyaltyCodeUsageSuccess' };

        displayLCodeControl = controlToUpdate;

        if (this.ValidateRequestData(rMember.GetClientCode(), rMember.GetSiteId(), rMember.GetMemberSiteId())) {
            this.SendGetRequest(url, data, this.GetLoyaltyCodeUsageSuccess);
        }
    }

    //Success method for GetUsageLoyaltyCode, which is called on shoppingcart.aspx page
    this.GetLoyaltyCodeUsageSuccess = function(result) {
        try {

            try {
                if (displayLCodeControl != null && displayLCodeControl != undefined) {
                    $(displayLCodeControl).text(result.d);
                }
            } catch (e) { }

            try {
                if (displayLCodeControl != null && displayLCodeControl != undefined) {
                    $(displayLCodeControl).val(result.d);
                }
            } catch (e) { }

            displayLCodeControl = null;
        }
        catch (e) { }
    }

    //To Get Requested Redeem Points of the User, used on shoppingcart Page
    this.GetUsagePoints = function(controlToUpdate) {
        _cookieMgr.GetCookieValue(this.GetMemberCookieName());

        rMember = new Member();

        rMember.InitFromCookie(_cookieMgr.GetCookieValue(this.GetMemberCookieName()), mSeparator);

        var url = _constructURL.GetBaseURL() + "/GetUsagePoints";

        getLPointControl = controlToUpdate;

        var data = { 'clientCode': "'" + rMember.GetClientCode() + "'", 'domain': "'" + window.location.hostname + "'", 'storeId': rMember.GetStoreId(), 'memberSiteId': rMember.GetMemberSiteId(), 'ref': instanceName + '.GetUsagePointSuccessMethod' };

        if (this.ValidateRequestData(rMember.GetClientCode(), rMember.GetSiteId(), rMember.GetMemberSiteId())) {
            this.SendGetRequest(url, data, this.GetUsagePointSuccessMethod);
        }
    }

    //Success method for GetUsagePoints, used on Shoppingcart page
    this.GetUsagePointSuccessMethod = function(result) {

        try {

            try {
                if (getLPointControl != null && getLPointControl != undefined) {
                    $(getLPointControl).text(result.d);
                }
            } catch (e) { }

            try {
                if (getLPointControl != null && getLPointControl != undefined) {
                    $(getLPointControl).val(result.d);
                }
            } catch (e) { }

            getLPointControl = null;
        }
        catch (e) { }

    }

    //To Select one of the Loyalty Code Assigned to current User
    this.SetLoyaltyCodeUsageBySelf = function(loyaltyCode) {
        _cookieMgr.GetCookieValue(this.GetMemberCookieName());

        rMember = new Member();

        rMember.InitFromCookie(_cookieMgr.GetCookieValue(this.GetMemberCookieName()), mSeparator);

        var url = _constructURL.GetBaseURL() + "/SetReferralLoyaltyBySelf";
        var data = { 'clientCode': "'" + rMember.GetClientCode() + "'", 'domain': "'" + window.location.hostname + "'", 'storeId': rMember.GetStoreId(), 'memberSiteId': rMember.GetMemberSiteId(), 'loyaltyCode': "'" + loyaltyCode + "'", 'ref': instanceName + '.ProcessResult' };

        this.SendGetRequest(url, data, this.ProcessResult);
    }




    this.MarkCodeAsUsed = function(orderId) {
        _cookieMgr.GetCookieValue(this.GetMemberCookieName());

        rMember = new Member();

        rMember.InitFromCookie(_cookieMgr.GetCookieValue(this.GetMemberCookieName()), mSeparator);

        var url = _constructURL.GetBaseURL() + "/MarkReferrerCodeAsUsed";
        var data = { 'clientCode': "'" + rMember.GetClientCode() + "'", 'domain': "'" + window.location.hostname + "'", 'storeId': rMember.GetStoreId(), 'referredMemberId': rMember.GetMemberSiteId(), 'orderId': orderId, 'ref': instanceName + '.ProcessSingleResult' };

        this.SendGetRequest(url, data, this.ProcessSingleResult);
    }

    this.MarkPointsAsUsed = function(orderId) {
        _cookieMgr.GetCookieValue(this.GetMemberCookieName());

        rMember = new Member();

        rMember.InitFromCookie(_cookieMgr.GetCookieValue(this.GetMemberCookieName()), mSeparator);

        var url = _constructURL.GetBaseURL() + "/MarkLoyaltyPointsAsUsed";
        var data = { 'clientCode': "'" + rMember.GetClientCode() + "'", 'domain': "'" + window.location.hostname + "'", 'storeId': rMember.GetStoreId(), 'memberSiteId': rMember.GetMemberSiteId(), 'orderId': orderId, 'ref': instanceName + '.ProcessSingleResult' };

        this.SendGetRequest(url, data, this.ProcessSingleResult);
    }



    //To Retrieve base Loyalty Point Factor For Current Member
    this.GetBaseLoyaltyPointFactor = function() {

        _cookieMgr.GetCookieValue(this.GetMemberCookieName());

        rMember = new Member();

        rMember.InitFromCookie(_cookieMgr.GetCookieValue(this.GetMemberCookieName()), mSeparator);

        var url = _constructURL.GetBaseURL() + "/GetBaseLoyaltyPointFactor";

        var data = { 'clientCode': "'" + rMember.GetClientCode() + "'", 'domain': "'" + window.location.hostname + "'", 'storeId': rMember.GetStoreId(), 'ref': instanceName + '.GetBaseLoyaltyPointFactorSuccess' };

        this.SendGetRequest(url, data, this.GetBaseLoyaltyPointFactorSuccess);
    }

    this.GetBaseLoyaltyPointFactorSuccess = function(result) {
        _cookieMgr.DeleteCookie(mV1);
        UpdateCookieFromSingleResult(result, mV1);
    }

    //To Retrieve Referral Discount On Code Usage
    this.GetReferralDiscountOnUsage = function() {

        _cookieMgr.GetCookieValue(this.GetMemberCookieName());

        rMember = new Member();

        rMember.InitFromCookie(_cookieMgr.GetCookieValue(this.GetMemberCookieName()), mSeparator);

        var url = _constructURL.GetBaseURL() + "/GetReferralDiscount";

        var data = { 'clientCode': "'" + rMember.GetClientCode() + "'", 'domain': "'" + window.location.hostname + "'", 'storeId': rMember.GetStoreId(), 'ref': instanceName + '.GetReferralDiscountSuccess' };

        this.SendGetRequest(url, data, this.GetReferralDiscountSuccess);
    }


    this.GetReferralDiscountSuccess = function(result) {
        _cookieMgr.DeleteCookie(mV2);
        UpdateCookieFromSingleResult(result, mV2);
    }

    //To Retrieve Referral Discount On Code Usage
    this.GetRedeemPointFactor = function() {

        _cookieMgr.GetCookieValue(this.GetMemberCookieName());

        rMember = new Member();

        rMember.InitFromCookie(_cookieMgr.GetCookieValue(this.GetMemberCookieName()), mSeparator);

        var url = _constructURL.GetBaseURL() + "/GetRedeemPointFactor";

        var data = { 'clientCode': "'" + rMember.GetClientCode() + "'", 'domain': "'" + window.location.hostname + "'", 'storeId': rMember.GetStoreId(), 'ref': instanceName + '.GetRedeemPointFactorSuccess' };

        this.SendGetRequest(url, data, this.GetRedeemPointFactorSuccess);
    }


    this.GetRedeemPointFactorSuccess = function(result) {
        _cookieMgr.DeleteCookie(mV3);
        UpdateCookieFromSingleResult(result, mV3);
    }

    this.PopulateUsageCode = function() {
        _cookieMgr.GetCookieValue(this.GetMemberCookieName());

        rMember = new Member();

        rMember.InitFromCookie(_cookieMgr.GetCookieValue(this.GetMemberCookieName()), mSeparator);

        var url = _constructURL.GetBaseURL() + "/GetCurrentLoyaltyCodeToUseCookie";
        var data = { 'clientCode': "'" + rMember.GetClientCode() + "'", 'domain': "'" + window.location.hostname + "'", 'storeId': rMember.GetStoreId(), 'referredMemberId': rMember.GetMemberSiteId(), 'ref': instanceName + '.PopulateUsageCodeSuccess' };

        this.SendGetRequest(url, data, this.PopulateUsageCodeSuccess);
    }

    this.PopulateUsageCodeSuccess = function(result) {
        _cookieMgr.DeleteCookie(muc);
        UpdateCookieFromSingleResult(result, muc);
    }

    this.PopulateRPoints = function() {
        _cookieMgr.GetCookieValue(this.GetMemberCookieName());

        rMember = new Member();

        rMember.InitFromCookie(_cookieMgr.GetCookieValue(this.GetMemberCookieName()), mSeparator);

        var url = _constructURL.GetBaseURL() + "/GetUsagePointsCookie";
        var data = { 'clientCode': "'" + rMember.GetClientCode() + "'", 'domain': "'" + window.location.hostname + "'", 'storeId': rMember.GetStoreId(), 'memberSiteId': rMember.GetMemberSiteId(), 'ref': instanceName + '.PopulateRPointsSuccess' };

        this.SendGetRequest(url, data, this.PopulateRPointsSuccess);
    }

    this.PopulateRPointsSuccess = function(result) {
        _cookieMgr.DeleteCookie(mrp);
        UpdateCookieFromSingleResult(result, mrp);
    }


    this.CheckValidMemberSiteId = function(memberSiteId, deleteCookie) {
        if (memberSiteId <= 0) {
            if (deleteCookie) {
                //_cookieMgr.DeleteCookie(this.GetMemberCookieName());
                this.DeleteCookies();
            }
        }
    }

    this.ValidateRequestData = function(clientCode, storeId, domain) {
        if (clientCode.length > 0 && storeId > 0 && domain.length > 0) {
            return true;
        }
        else {
            return false;
        }
    }




    this.SendGetRequest = function(getUrl, dataToSend, callbackHandler) {

        //alert(getUrl);
        //alert(dataToSend);

        $.ajax({
            type: "GET",
            url: getUrl,
            data: dataToSend,
            contentType: "application/json; charset=utf-8",
            dataType: "jsonp",
            success: callbackHandler,
            error: function() {
                //alert("Hit error fn!");
            }
        });
    }

    this.ProcessResult = function(resultData) {
        try {

            var jsonData = resultData.d;

            if (showAlerts === true) {
                if (jsonData.Display == true || jsonData.Status == "error") {
                    alert(jsonData.ResultMsg);
                }

                if (jsonData.LoyaltyCode != null && jsonData.LoyaltyCode != undefined) {
                    alert(jsonData.LoyaltyCode);
                }
            }

        } catch (e)
            { alert(e); }
    }

    this.ProcessResultWithCookie = function(resultData) {
        try {
            var jsonData = resultData.d;

            //Copy Existing Member instance
            var copyMember = resultMember;

            resultMember = new Member();

            resultMember.InitJson(jsonData);

            UpdateCookieFromResult(resultMember, this.GetMemberCookieName());

        } catch (e)
            { alert(e); }
    }

    this.ProcessSingleResult = function(resultData) {
        try {
            var result = resultData.d;

            //this.UpdateControlValue(updateControlName, result);

        } catch (e)
            { alert(e); }
    }


    this.UpdateControlValue = function(controlName, value) {
        if (controlName != null && controlName != undefined) {
            try {
                $(controlName).text(value);

            } catch (e) { }
            try {
                $(controlName).val(value);

            } catch (e) { }
        }
    }

    function UpdateCookieFromSingleResult(result, cookieName) {
        _cookieMgr.UpdateCookie(cookieName, decodeURI(result.d), 365);
    }

    function UpdateCookieFromResult(memberResult, cookieName) {
        _cookieMgr.UpdateCookie(cookieName, memberResult.ToValueString(mSeparator), 365);

        //To Reset global resultMember instance of the Member
        resultMember = new Member();
    }

    this.CreateHiddenField = function(val) {
        var input = document.createElement("input");

        input.setAttribute("type", "hidden");
        input.setAttribute("name", "ctrlName");
        input.setAttribute("value", val);
        document.getElementById("aspnetForm").appendChild(input);
    }

}


