﻿window.onload = function() {
    var R = document.getElementById('rater');
    var OR = document.getElementById('orgRater');
    //rater    display block
    R.style.visibility = 'visible';
    //orgRater display none
    OR.style.display = 'none';

}

//copyright 2008 Jarrett Vance
//http://jvance.com
$.fn.rater = function(options) {
    var opts = $.extend({}, $.fn.rater.defaults, options);
    return this.each(function() {
        var $this = $(this);
        var $on = $this.find('.ui-rater-starsOn');
        var $off = $this.find('.ui-rater-starsOff');
        opts.size = $on.height();
        if (opts.rating == undefined) opts.rating = $on.width() / opts.size;
        if (opts.id == undefined) opts.id = $this.attr('id');

        $off.mousemove(function(e) {
            var left = e.clientX - $off.offset().left;
            var width = $off.width() - ($off.width() - left);
            width = Math.ceil(width / (opts.size / opts.step)) * opts.size / opts.step;
            $on.width(width);
        }).hover(function(e) { $on.addClass('ui-rater-starsHover'); }, function(e) {
            $on.removeClass('ui-rater-starsHover'); $on.width(opts.rating * opts.size);
        }).click(function(e) {
            var r = Math.round($on.width() / $off.width() * (opts.units * opts.step)) / opts.step;
            $off.unbind('click').unbind('mousemove').unbind('mouseenter').unbind('mouseleave');
            $off.css('cursor', 'default'); $on.css('cursor', 'default');
            //alert(getSoapHeader());
            $.fn.rater.rate($this, opts, r);
        }).css('cursor', 'pointer'); $on.css('cursor', 'pointer');
    });
};

$.fn.rater.defaults = {
    postHref: location.href,
    units: 5,
    step: 1
};


$.fn.rater.rate = function($this, opts, rating) {
    var $on = $this.find('.ui-rater-starsOn');
    var $off = $this.find('.ui-rater-starsOff');

    $off.fadeTo(600, 0.4, function() {

        var newRating = setRating(rating * 20);

        if (newRating) {

            var $count = $this.find('.ui-rater-rateCount');
            $count.text(newRating.Count);
            $this.find('.ui-rater-rating').text((newRating.Mean / 20));
            $this.find('.ui-rater-yourrating').text('Your rating was ' + (rating)); // was (rating * 20)
        }

    });
};

function getSoapHeader() {
    return '<?xml version="1.0" encoding="utf-8"?>'
            + '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'
            + '<soap:Body>';
}
function getSoapFooter() {
    return '</soap:Body>'
           + '</soap:Envelope>';
}
function setRating(rating) {
    var objid = new ObjectID;
    objid.Set(sObjectId, sObjectTypeId);
    var usrid = new UserID();
    usrid.Set(sMemberId, sUniqueId, sIpAddress);

    var newRating = SetRatingByObject(objid, usrid, rating, true, true);

    return newRating;
    // alert(newRating.Count);
    // window.location.reload();
}

function SetRatingByObject(objId, usrId, rating, overwriteExisting, returnUserRating) {
    var webkey = "123gasld345k3245fght43bvc";
    var target = "/webservices/RatingsAndReviews.asmx";
    var action = "http://www.nuffieldtrust.org.uk/SetRatingByObject";
    var payload = '<SetRatingByObject xmlns="http://www.nuffieldtrust.org.uk">'
                    + '<webkey>' + webkey + '</webkey>'
                    + '<objId>'
                        + '<ObjectId>' + objId.objectId + '</ObjectId>'
                        + '<ObjectTypeId>' + objId.objectTypeId + '</ObjectTypeId>'
                    + '</objId>'
                    + '<usrId>'
                        + '<MemId>' + usrId.memId + '</MemId>'
                        + '<UniqueId>' + usrId.uniqueId + '</UniqueId>'
                        + '<IpAddress>' + usrId.ipAddress + '</IpAddress>'
                    + '</usrId>'
                    
                    + '<rating>' + rating + '</rating>'
                    + '<overwriteExisting>' + overwriteExisting + '</overwriteExisting>'
                    + '<returnUserRating>' + returnUserRating + '</returnUserRating>'
                + '</SetRatingByObject>';
    
    //alert(payload);

    var xml = doSoapRequest(target, action, payload);

    xml = $(xml).decHTML();


    var oRating = new Rating;

    $(xml).find('SetRatingByObjectResult').each(function() {

        $(this).find('Rating').each(function() {

            var Id = $(this).find('Id').text();
            var Count = $(this).find('Count').text();
            var Minimum = $(this).find('Minimum').text();
            var Maximum = $(this).find('Maximum').text();
            var Mean = $(this).find('Mean').text();

            oRating.Set(Id, Count, Minimum, Maximum, Mean);

        });

    });

    return oRating;
}

function ObjectID() {
    this.objectId = 0;
    this.objectTypeId = 0;
}

ObjectID.prototype.Set = function(obj, objType) {
    this.objectId = obj;
    this.objectTypeId = objType;
}

function UserID() {
    this.memId = 0;
    this.uniqueId = "";
    this.ipAddress = "";
}

UserID.prototype.Set = function(mem, unique, ip) {
    this.memId = mem;
    this.uniqueId = unique;
    this.ipAddress = ip;
}
function Rating() {
    this.Id = 0;
    this.Count = 0;
    this.Minimum = 0.0;
    this.Maximum = 0.0;
    this.Mean = 0.0;
}

Rating.prototype.Set = function(id, count, minimum, maximum, mean) {
    this.Id = id;
    this.Count = count;
    this.Minimum = minimum;
    this.Maximum = maximum;
    this.Mean = mean;
}

function doSoapRequest(target, action, payload) {
    var xmlHttp = getHttpRequestObject();
    if (xmlHttp) {
        xmlDoc = getSoapHeader();
        xmlDoc += payload;
        xmlDoc += getSoapFooter();
        xmlHttp.open("POST", target, false);
        xmlHttp.setRequestHeader("Man", "POST" + " " + target + " HTTP/1.1");
        xmlHttp.setRequestHeader("MessageType", "CALL");
        xmlHttp.setRequestHeader("Content-Type", "text/xml");
        xmlHttp.setRequestHeader("charset", "utf-8");
        xmlHttp.setRequestHeader("Content-Length", xmlDoc.length);
        xmlHttp.setRequestHeader("SOAPAction", action);
        xmlHttp.send(xmlDoc);
        //alert(xmlHttp.responseText);
        return xmlHttp.responseText;
    }
    else {
        return "No action done!";
    }
}

function getHttpRequestObject() {
    var xmlhttp = false;
    /*@cc_on@*/
    /*@if (@_jscript_version >= 5)
    // JScript gives us Conditional compilation, we can cope with old IE versions.
    // and security blocked creation of the objects.
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            xmlhttp = false;
        }
    }
    @end@*/
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        try {
            xmlhttp = new XMLHttpRequest();
        } catch (e) {
            xmlhttp = false;
        }
    }
    if (!xmlhttp && window.createRequest) {
        try {
            xmlhttp = window.createRequest();
        } catch (e) {
            xmlhttp = false;
        }
    }
    return xmlhttp;
}

jQuery.fn.encHTML = function() {
    return this.each(function() {
        var me = jQuery(this);
        var html = me.html();
        me.html(html.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;'));
    });
};

jQuery.fn.decHTML = function() {
    return this.each(function() {
        var me = jQuery(this);
        var html = me.html();
        me.html(html.replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>'));
    });
};

jQuery.fn.isEncHTML = function(str) {
    if (str.search(/&amp;/g) != -1 || str.search(/&lt;/g) != -1 || str.search(/&gt;/g) != -1)
        return true;
    else
        return false;
};

jQuery.fn.decHTMLifEnc = function() {
    return this.each(function() {
        var me = jQuery(this);
        var html = me.html();
        if (jQuery.fn.isEncHTML(html))
            me.html(html.replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>'));
    });
}

