function postComment(projectId, commentEl) {
	var comment = commentEl.value;
	if (comment == "")
		comment = commentEl.innerText;
	if (comment == "") {
		alert("There was an error processing your comment.");
		return;
	}
	comment = comment.replace(/<[^>]*>/g, "");
	comment = comment.replace(/</g, "(");
	comment = comment.replace(/>/g, ")");
	commentEl.innerHTML = comment;

	// Hide comment form and show loading banner
	document.getElementById('addCommentDiv').style.display = "none";
	document.getElementById('loadingDiv').style.display = "block";
	
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', "addProjectComment.aspx", true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
	        if (self.xmlHttpReq.responseText == "ok")
	        	window.location = "memberProject.aspx?projectId=" + projectId;
	        else {
		        // error management
		        alert("There was an error posting your comment.  Please try again.");
	        }
        }
    }
    self.xmlHttpReq.send("projectId=" + projectId + "&comment=" + encodeURIComponent(comment));
}


function loadComments(projectId, startId) {
	var commentDiv = document.getElementById('viewCommentsDiv');
	
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('GET', "_getComments.aspx?projectId=" + projectId + "&startId=" + startId, true);
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
	        commentDiv.innerHTML = self.xmlHttpReq.responseText;
       }
    }
    self.xmlHttpReq.send(null);
}

function deleteComment(commentId, projectId, startId) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('GET', "deleteComment.aspx?commentId=" + commentId, true);
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
	        loadComments(projectId, startId);
        }
	}
    self.xmlHttpReq.send(null);
}

function addToFavorites(id, thisPathEncoded) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('GET', "/qk2/ideas/addToFavorites.aspx?projectSource=M&id=" + id, true);
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
			switch(self.xmlHttpReq.responseText)
			{
				case "error":
					alert("There was an error while trying to add this project to your favorites.");
					break;
				case "success":
					window.location.reload();
					break;
				case "timeout":
					window.location = "/qk2/loginCustomer.aspx?redirect=" + thisPathEncoded;
					break;
			}
        }
    }
    self.xmlHttpReq.send(null);
}

function reportContent(contType, contId, tattler, offender) {
	if (!confirm("Are you sure you want to report this content as inappropriate (you will remain anonymous)?"))
		return;
	
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('GET', "/qk2/myqk/reportContent.aspx?contType=" + contType + "&contId=" + contId + "&cca=" + tattler + "&ccb=" + offender, true);
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
	        alert("Thank you for reporting inappropriate content to help keep QuicKutz.com a happy place.  QuicKutz representatives review inappropriate content notifications in the order of receipt.");
        }
    }
    self.xmlHttpReq.send(null);
}

