﻿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;
}

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 ObjectID() {
    this.objectId = 0;
    this.objectTypeId = 0;
}
ObjectID.prototype.Set = function(obj, objType) {
    this.objectId = obj;
    this.objectTypeId = objType;
}
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 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 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);
        return xmlHttp.responseText;
    }
    else {
        return "No action done!";
    }
}
function SetRatingByObject(objId, usrId, rating, overwriteExisting, returnUserRating) {
    var webkey = "123gasld345k3245fght43bvc";
    var target = "/WebServices/RatingsAndReviews.asmx";
    var action = "http://www.brightonfestivalfringe.org.uk/SetRatingByObject";
    var payload = '<SetRatingByObject xmlns="http://www.brightonfestivalfringe.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>'
                + '<approved>false</approved>'
                + '<overwriteExisting>' + overwriteExisting + '</overwriteExisting>'
                + '<returnUserRating>' + returnUserRating + '</returnUserRating>'
                + '</SetRatingByObject>';
    doSoapRequest(target, action, payload);
    return new Rating();
}
function AddReviewByObject(objId, usrId, name, location, review, overwriteExisting) {
    var webkey = "123gasld345k3245fght43bvc";
    var target = "/WebServices/RatingsAndReviews.asmx";
    var action = "http://www.brightonfestivalfringe.org.uk/AddReviewByObject";
    var payload = '<AddReviewByObject xmlns="http://www.brightonfestivalfringe.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>'
                + '<name><![CDATA[' + name + ']]></name>'
                + '<location><![CDATA[' + location + ']]></location>'
                + '<review><![CDATA[' + review + ']]></review>'
                + '<approved>false</approved>'
                + '<overwriteExisting>' + overwriteExisting + '</overwriteExisting>'
                + '</AddReviewByObject>';
    doSoapRequest(target, action, payload);
    return new Rating();
}

function setRating(listingid, memberid, uniqueid, ipaddress, rating) {
    var objid = new ObjectID;
    objid.Set(listingid, 1);
    var usrid = new UserID();
    usrid.Set(memberid, uniqueid, ipaddress);
    var newRating = SetRatingByObject(objid, usrid, rating, true, false);
    //window.location.reload();
}

function addReview(listingid, memberid, uniqueid, ipaddress, name, location, review) {
    var objid = new ObjectID;
    objid.Set(listingid, 1);
    var usrid = new UserID();
    usrid.Set(memberid, uniqueid, ipaddress);
    var newReview = AddReviewByObject(objid, usrid, name, location, review, true);
    //window.location.reload();
}