﻿
	function compute(ctl) {
		var curlen = ctl.value.length;
		if (curlen > 400) {
			ctl.value = ctl.value.substring(0, 399);
		}
		document.getElementById('maxchar').innerHTML = curlen + ' / 400 maximum characters';
	}
	
	function computeAdmin(ctl, maxchar) {
		var curlen = document.getElementById(ctl).value.length;
		if (curlen > 400) {
			document.getElementById(ctl).value = document.getElementById(ctl).value.substring(0, 399);
		}
		document.getElementById(maxchar).innerHTML = curlen + ' / 400 maximum characters';
	}
	
	function computeComment(ctl, commentSubID) {
		var curlen = ctl.value.length;
		if (curlen > 300) {
			ctl.value = ctl.value.substring(0, 300);
		}
		document.getElementById('maxchar' + commentSubID).innerHTML = curlen + ' / 300 maximum characters';
	}


	function replyComment(itemnum, commentSubID) {
		var http = false;
		try {
			http = new XMLHttpRequest();
		} catch (trymicrosoft) {
			try {
				http = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (othermicrosoft) {
				try {
					http = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (failed) {
					http = false;
				}
			}
		}
		
		http.open('GET', '/ajax/replycomment.aspx?itemID=' + itemnum + '&commentSubID=' + commentSubID);
				
		http.onreadystatechange = function() {
			if (http.readyState == 4) {
				
				document.getElementById('replyBox' + commentSubID).innerHTML = http.responseText;
				document.getElementById('reply' + commentSubID).style.display = '';
				
			}
		}
		http.send(null);
	
	}

	function commentSubmit(itemnum, commentSubID) {
		var http = false;
		try {
			http = new XMLHttpRequest();
		} catch (trymicrosoft) {
			try {
				http = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (othermicrosoft) {
				try {
					http = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (failed) {
					http = false;
				}
			}
		}

		var url = '/ajax/addComment.aspx?itemnum=' + itemnum + '&comment=' + escape(document.getElementById('msg' + commentSubID).value) + '&commentSubID=' + commentSubID;

		document.getElementById('commentError' + commentSubID).style.display = 'none';
		document.getElementById('commentSubmitted' + commentSubID).style.display = 'none';
					
		var responseTxt;
		
		http.open("GET", url);
		http.onreadystatechange = function() {
			if (http.readyState == 4) {

				responseTxt = http.responseText;

				if (responseTxt == 'ERROR:LENGTH') {
					document.getElementById('commentError' + commentSubID).style.display = 'block';
					document.getElementById('commentError' + commentSubID).innerHTML = 'You somehow entered more than 300 characters. Reduce the length of your comment and try again.';
				}
				else if (responseTxt == 'ERROR:EMPTY') {
					document.getElementById('commentError' + commentSubID).style.display = 'block';
					document.getElementById('commentError' + commentSubID).innerHTML = 'You can\'t submit blank comments! What were you thinking!';
				}

				else if (responseTxt == 'ERROR:NOTLOGGEDIN') {
					window.location = '/login/?goto=' + window.location + '#addcomments';
				}
				else if (responseTxt == 'SUCCESS') {
					document.getElementById('commentSubmitted' + commentSubID).style.display = 'block';
					document.getElementById('hideCommentAdd' + commentSubID).style.display = 'none';
					
					//document.getElementById('commentError').innerHTML = 'Comment submitted! Press refresh to see your comment.';
				}
				else {
					document.getElementById('commentError' + commentSubID).style.display = 'block';
					document.getElementById('commentError' + commentSubID).innerHTML = 'A <i>not so funny</i> error occurred.<b>Try again later</b>';
				}

			}
		}
		http.send(null);
	}
