﻿MessageRate = function(spnRateID, spnAnswersCount, spnStatus, spnRatingWordID, divYesID, divNoID, imgStarID, ancChangeRatingID, divChangeRatingID, msgid, stage, sameUserThread, answers, isAskOnce, showChangeRating) {
	this.YesRateValue = MessageRate.YesValue_Non_Asker;
	this.spnRate = $(spnRateID);
	this.divYes = $(divYesID);
	this.divNo = $(divNoID);
	this.imgStar = $(imgStarID);
	this.ancChangeRating = $(ancChangeRatingID);
	this.divChangeRating = $(divChangeRatingID);
	this.msgid = msgid;
	this.answers = answers;
	this.spnRatingWord = $(spnRatingWordID);
	this.spnAnswersCount = $(spnAnswersCount);
	this.spnStatus = $(spnStatus);
	this.stage = stage;
	this.container = this.spnRate.parentNode;
	this.isLastYes = null;
	this.noCalls = false;
	this.isLockedButtons = false;
	this.isAskOnce = isAskOnce;
	this.showChangeRating = showChangeRating;
	if (this.spnRatingWord !== null)
		this.hasAskerRated = (this.spnRatingWord.style.display != 'none');
	this.sameUserThread = sameUserThread;
	if (this.sameUserThread && !this.isAskOnce)
		this.YesRateValue = 90;
	this._yes_click_binder = this.sendMessage.bind(this, this.YesRateValue, true);
	this._no_click_binder = this.sendMessage.bind(this, MessageRate.NoValue, false);
	this.divYes.observe('click', this._yes_click_binder);
	this.divNo.observe('click', this._no_click_binder);
	if (this.showChangeRating) {
		if (typeof this.ancChangeRating != 'undefined')
			if (this.ancChangeRating !== null)
			this.ancChangeRating.observe('click', this.showRater.bind(this));
	}
	else {
		if (this.ancChangeRating !== null)
			this.ancChangeRating.style.display = 'none';
	}
	if (!this.isAskOnce) {
		if (this.sameUserThread && this.spnAnswersCount != null)
			this.spnAnswersCount.style.display = 'none';
	}
}
MessageRate.prototype =
{
    sendMessage: function(val, isYesValue) {
        if (this.noCalls || this.isLockedButtons)
            return;
        this.isLockedButtons = true;
        var _extraRate = '';
        var _lastYes = (this.isLastYes) ? 1 : 0;
        if (this.isLastYes !== null) _extraRate = '&lst=' + _lastYes;
        if (isYesValue) {
            logEvent(5799);
            this.isLastYes = true;
        }
        else
            logEvent(5798);
        new Ajax.Request(MessageRate.RateUrl, { method: 'post', postBody: 'stg=' + this.stage + '&har=' + this.hasAskerRated + '&rt=' + val + '&thid=' + MessageRate.ThreadID + '&prdid=' + MessageRate.ProductID + '&msgid=' + this.msgid + '&ia=' + this.sameUserThread + _extraRate, onSuccess: this.onMsgReturn.bind(this, isYesValue) });
    },
    onMsgReturn: function(isYes) {
        this.divYes.fire('rt:onRated');
        var _returned_object = null;
        if (arguments.length > 1)
            eval("_returned_object = " + arguments[1].responseText);
        if (_returned_object !== null) {
            if (this.imgStar !== null) {
                if (this.sameUserThread && this.stage == 1 || this.isAskOnce || !this.sameUserThread) {
                    this.imgStar.src = _returned_object.RateImage;
                    this.imgStar.alt = _returned_object.Title;
                    this.imgStar.title = _returned_object.Title;
                }
            }
            this.answers = _returned_object.Answers;
            if (_returned_object.IsAskerRating) {
                if (this.spnRatingWord !== null) {
                    this.spnRatingWord.style.display = 'block';
                }
                this.hasAskerRated = true;
            }
            else
                if (this.spnRatingWord !== null)
                this.spnRatingWord.style.display = 'none';
        }
        if (this.spnAnswersCount != null && this.answers > 0) {
            if (this.answers == 1)
                this.spnAnswersCount.innerHTML = MessageRate.OneHelpfullText;
            else
                this.spnAnswersCount.innerHTML = MessageRate.MoreHelpfullTextFormat.replace(/\{0\}/g, this.answers);
             

             var moreCntDiv = $(this.spnAnswersCount.id + '1')
             if(moreCntDiv != null)
			 {
				moreCntDiv.innerHTML = this.answers + ' people thought this was helpful';
			 }
        }
        if (this.sameUserThread == 1 && !this.isAskOnce) {
            var _anim = new Animation(this.container, 50, 2, 90);
            _anim.onFadeOut = function(_anim) {
                this.spnStatus.innerHTML = MessageRate.StatusText;
                if (isYes)
                    this.spnRate.innerHTML = 'Did it solve your problem?';
                else
                    this.spnRate.innerHTML = 'Did the Solution have anything to do with your problem?';
                _anim.FadeIn(500);
            } .bind(this, _anim);
            _anim.FadeOut(600);

        }
        if (this.sameUserThread == 1 && this.stage == 0 && !this.isAskOnce) {
            this.stage++;
        }
        else {
            if (this.stage == 1) {
                this.divYes.fire('rt:onRated_stage2');
                this.divChangeRating.style.display = 'block';
                if (_returned_object !== null)
                    this.divChangeRating.select('div[class="ratedAS"] span')[0].innerHTML = _returned_object.ChosenText;
                this.container.style.display = 'none';
            }
            if (this.sameUserThread == 0 || this.isAskOnce) {
                this.divYes.stopObserving('click', this._yes_click_binder);
                this.divNo.stopObserving('click', this._no_click_binder);
                if (isYes && this.stage == 0) {
                    this.divNo.className = 'grey';
                    this.divYes.className = 'yes y_grey'
                }
                else {
                    this.divYes.className = 'grey';
                    this.divNo.className = 'no n_grey'
                }
                this.noCalls = true;
            }
        }
        this.isLockedButtons = false;
    },
    showRater: function() {
        this.container.style.display = 'block';
        this.divChangeRating.style.display = 'none';
        this.stage = 0;
        this.spnStatus.innerHTML = '';
        this.isLastYes = null;
        this.stage = 0;
        if (this.sameUserThread == 0)
            this.spnRate.innerHTML = MessageRate.FirstQuestion;
        else
            this.spnRate.innerHTML = MessageRate.FirstQuestion_Asker;
    },
    add_onRated: function(handler) {
        this.divYes.observe('rt:onRated', handler);
    },
    add_onRated_stage2: function(handler) {
        this.divYes.observe('rt:onRated_stage2', handler);
    }
}
MessageRate.RateUrl = '/httphandlers/msg/MessageReview.ashx';
MessageRate.OneHelpfullText = '1 person thought this was helpful';
MessageRate.MoreHelpfullTextFormat = '{0} people thought this was helpful';
MessageRate.StatusText = 'Thank you for your feedback! Please answer one follow up question.';
MessageRate.YesValue_Non_Asker = 100;
MessageRate.NoValue = 75;
MessageRate.FirstQuestion = 'Did you think this was helpful?';
MessageRate.FirstQuestion_Asker = 'Please rate this solution. Was this helpful?';
MessageRate.ThreadID = 0;
MessageRate.ProductID = 0;