userRatingObj = function(ratingID, showratings, showcomments, filledstar, emptystar,waitingstar, reducedstar, domain, assetid, userid, posturl)
{
  this.ratingID = ratingID;
  this.showratings = showratings;
  this.showcomments = showcomments;
  this.filledstar = filledstar;
  this.reducedstar = reducedstar;
  this.emptystar = emptystar;
  this.waitingstar = waitingstar;
  this.domain = domain;
  this.assetid = assetid;
  this.userid = userid;
  this.ratingPick = 0;
  this.titleVal = "";
  this.descriptionVal = "";
  this.posturl = posturl;
  this.stars = {}; 
 
  if (this.showratings) {
    this.initRatings();
  }
  if (this.showcomments) {
    this.initComments();
  }
}

userRatingObj.prototype.initRatings = function() {
  var star;
  var count = 1;

  while (1) {
    star = document.getElementById(this.ratingID + "_Star_" + count);
    if (!star) {
      break;
    }

    this.addClass(star, this.emptystar);
    this.addEvent(star, "mouseover", this.starMouseOver);
    this.addEvent(star, "mouseout", this.starMouseOut);
    this.addEvent(star, "click", this.starClicked);
    star.ratingObj = this; //keep pointer to rating obj

    this.stars[count] = star; //keep a pointer to them
    count++;
  }
}

userRatingObj.prototype.initComments = function() {
  this.commentDiv = document.getElementById(this.ratingID + "_CommentDiv");
  this.title = document.getElementById(this.ratingID + "_Title");
  this.description = document.getElementById(this.ratingID + "_Description");
  this.close = document.getElementById(this.ratingID + "_Close"); 
  this.close.ratingObj = this;
  this.addEvent(this.close, "click", this.hideComments);
  this.submit = document.getElementById(this.ratingID + "_submit"); 
  this.submit.ratingObj = this;
  this.addEvent(this.submit, "click", this.submitClick);

  if (!this.showratings) {
     this.commentLink = document.getElementById(this.ratingID + "_openComment");
     this.commentLink.ratingObj = this;
     this.addClass(this.commentLink, "commentLink");
     this.addEvent(this.commentLink, "click", this.showComments);
  }
}

userRatingObj.prototype.showThankYou = function() {
  this.thankyouDiv = document.getElementById(this.ratingID + "_thankYouMessage");
    if (this.thankyouDiv != null)
	    this.thankyouDiv.style.display = 'block';
}

userRatingObj.prototype.submitRating = function() {
  var title = encodeURI(this.titleVal);
  var desc = encodeURI(this.descriptionVal);
  var url = this.posturl + "?domain=" + this.domain + "&UserId=" + this.userid + "&AssetID=" + this.assetid + "&Rating=" + this.ratingPick + "&Title=" + title + "&desc=" + desc;
  AsynchronousPost(url);
  if (this.showratings)
    injectReadOnlyUserRating();
  if (this.showcomments)
    injectUserComments(0);
}
  
userRatingObj.prototype.hideComments = function(ev) {
  var thisObj = getEventObj(ev);
  thisObj.ratingObj.commentDiv.style.display = "none";
}

userRatingObj.prototype.showComments = function(ev) {
  var thisObj = getEventObj(ev);
  thisObj.ratingObj.commentDiv.style.display = "block";
}

userRatingObj.prototype.submitClick= function(ev) {
  var thisObj = getEventObj(ev);
  var ratingObj = thisObj.ratingObj;

  ratingObj.commentDiv.style.display = "none";
  ratingObj.titleVal = ratingObj.title.value;
  ratingObj.descriptionVal = ratingObj.description.value;
  ratingObj.submitRating();
  ratingObj.showThankYou();
}

userRatingObj.prototype.starMouseOver = function(ev) {
  var thisObj = getEventObj(ev);

  var ratingObj = thisObj.ratingObj;
  var stars = ratingObj.stars;

  for (var keyvar in stars) {
    ratingObj.removeClass(stars[keyvar], ratingObj.emptystar);
    if (ratingObj.removeClass(stars[keyvar], ratingObj.waitingstar)) {
      ratingObj.addClass(stars[keyvar], ratingObj.reducedstar);
    } else {
      ratingObj.addClass(stars[keyvar], ratingObj.filledstar);
    }
    if (stars[keyvar] == thisObj) {
       break;
    }
  }
}

userRatingObj.prototype.starClicked = function(ev) {
  var thisObj = getEventObj(ev);
  var ratingObj = thisObj.ratingObj;
  var stars = ratingObj.stars;

  for (var keyvar in stars) {
    ratingObj.removeClass(stars[keyvar], ratingObj.filledstar);
    ratingObj.removeClass(stars[keyvar], ratingObj.waitingstar);
    ratingObj.addClass(stars[keyvar], ratingObj.emptystar);
  }

  for (var keyvar in stars) {
    ratingObj.addClass(stars[keyvar], ratingObj.waitingstar);
    if (stars[keyvar] == thisObj) {
       ratingObj.ratingPick = keyvar;
       break;
    }
  }

  if (ratingObj.showcomments) {
    ratingObj.commentDiv.style.display = "block";
    ratingObj.submitRating();
  } else {
    ratingObj.submitRating();
    ratingObj.showThankYou();
    return false;
  }
}

userRatingObj.prototype.starMouseOut = function(ev) {
  var thisObj = getEventObj(ev);
  var ratingObj = thisObj.ratingObj;
  var stars = ratingObj.stars;

  for (var keyvar in stars) {
    ratingObj.removeClass(stars[keyvar], ratingObj.filledstar);
    if (ratingObj.removeClass(stars[keyvar], ratingObj.reducedstar)) {
      ratingObj.addClass(stars[keyvar], ratingObj.waitingstar);
    } else {
      ratingObj.addClass(stars[keyvar], ratingObj.emptystar);
    }
  }

}

userRatingObj.prototype.removeClass = function(el, className) {
        if (!(el && el.className)) {
                return;
        }
        var cls = el.className.split(" ");
        var ar = new Array();
        var found = false;
        for (var i = cls.length; i > 0;) {
                if (cls[--i] != className) {
                        ar[ar.length] = cls[i];
                } else {
                  found = true;
                }
        }
        el.className = ar.join(" ");
        return found;
}

userRatingObj.prototype.addClass = function(el, className) {
  this.removeClass(el, className);
  el.className += " " + className;
}

userRatingObj.prototype.addEvent = function(el, evname, func) {
  if (el.attachEvent) { 
    el.attachEvent("on" + evname, func);
  } else if (el.addEventListener) {
    el.addEventListener(evname, func, true);
  } else {
    el["on" + evname] = func;
  }
};

getEventObj = function(ev) {
  var obj;

  if (window.event && window.event.srcElement) {
    obj = window.event.srcElement;
    while (obj.nodeType != 1)
      obj = obj.parentNode;
  } else {
    obj = ev.currentTarget;
  }
  return obj;
}


AsynchronousPost = function(url)
{
  var rand = Math.random()*10000000000000000;
  url = url + '&rand=' + rand;
  if(window.XMLHttpRequest && !(window.ActiveXObject)) {
    try {
      req = new XMLHttpRequest();
    } catch(e) {
      req = false;
    }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
      try {
          req = new XMLHttpRequest();
          if (req == null) {
    		req = new ActiveXObject("Msxml2.XMLHTTP");
            }      
    } catch(e) {
       try {
          req = new ActiveXObject("Microsoft.XMLHTTP");
          } catch(e) {
            req = false;
          }
       }
     }
     try {
      req.open('GET', url, false); 
      req.send(null); 
      } 
     catch (e) { 
     }

}