/*
uses the presense of the "sorryMsg" div to trigger logged in / out behavior
*/
function Comment(target, comment, id, title, contentType, categoryid)
  {
  this.validator = new Validator("comments", "error", "highlight");
  this.target = target;

	this.id = id;
	this.title = title;
	this.contentType = contentType;
	this.categoryid = categoryid;

  var buttons = document.getElementsByClassName("share-view", this.target);
  if (buttons.length == 0)
    return false;
  this.buttons = buttons;

  // create a sub object to handle the comment box
  this.comment = new Object();
  this.comment.body = document.getElementById(comment);
  this.sorry = document.getElementById("sorryMsg");
  
  //changes text of login button for Omniture tracking
  if(document.getElementById("sorry_msg_login_btn")) {
	  this.sorryLoginBtn = document.getElementById("sorry_msg_login_btn");
	  this.sorryLoginBtn.innerHTML = 'Article Comment Login';
  }
  
  var loginMessage = document.getElementsByClassName("loginMessage", this.target);
  this.loginMessage = loginMessage[0];
  if(this.sorry != undefined) {
    var o = this;
    for(var i=0; i<this.buttons.length; i++) {
      this.buttons[i].onclick = function () {
        o.sorry.style.display = "block";
        o.sorry.style.left = _findPosX(this) - 10 + "px";
		o.sorry.style.top = _findPosY(this) - 40 + "px";
		//o.loginMessage.appendChild(o.sorry);
        o.loginMessage.style.zIndex = 9999;
		var cattitle = "/" + categoryid + "/" + title;
		//_dolWAObjSubmitLink('commentSorry', cattitle, null);
				return false;
      }
    }
  } else {
    button = document.getElementsByClassName("comment-cancel", this.comment.body);
    if (button == undefined)
      return false;
    this.comment.cancel = button[0];
    this.setupButtons(categoryid, title);
  }
}

Comment.prototype.setupButtons = function(categoryid, title) {
  var o = this;
  for(var i=0; i<this.buttons.length; i++) {
    this.buttons[i].onclick = function() {
      o.comment.body.style.display = "block";
      var cattitle = "/" + categoryid + "/" + title;
      _dolWAObjSubmitLink('addComment', cattitle, null);
			return false;
    }
  }
  this.comment.cancel.onclick = function() {
    Element.hide(o.comment.body);
		return false;
  }
}


Comment.prototype.validate = function(categoryid, title) {
	if (this.validator.validate() == true) {
  
		var contentRefId = this.id;
		var subject = this.title;
		var commentText = replaceSmartQuoteInString(encodeURIComponent(this.comment.body.getElementsByTagName('textarea')[0].value));
		var contentType = this.contentType;
		
		var str = "contentRefId=" + contentRefId + "&subject=" + subject + "&commentText=" + commentText + "&contentType=" + contentType;
		
		AJAXRequest('POST', '/createComment.do', str, function(){});
		var contents = YAHOO.util.Dom.getElementsByClassName('content', 'div', 'membersAddComments');
		contents[0].style.display = 'none';
		contents[1].style.display = 'block';
		
		var o = this;
		contents[1].getElementsByTagName('a')[0].onclick = function() { 
			Element.hide(o.comment.body);
			var cattitle = "/" + categoryid + "/" + title;
			_dolWAObjSubmitLink('commentThanks', cattitle, null);
			window.location.reload();
			return false;
		}
		return false;
	} else {
		return false;
	}
}



function replaceSmartQuoteInString(data) {
	if(data != null && data != "") {
		var returnValue = data;
				
		returnValue = returnValue.replace(/\u2018/g, "\'");
		returnValue = returnValue.replace(/\u2019/g, "\'");
		returnValue = returnValue.replace(/\u201c/g, "\"");
		returnValue = returnValue.replace(/\u201d/g, "\"");

		return returnValue;
	}else {
		return data;
	}
}
