function trim(inputString) {
     	if (typeof inputString != "string") { return inputString; }
 	var retValue = inputString;
   	var ch = retValue.substring(0, 1);
   	while (ch == " ") {
      		retValue = retValue.substring(1, retValue.length);
      		ch = retValue.substring(0, 1);
   		}
   	ch = retValue.substring(retValue.length-1, retValue.length);
   	while (ch == " ") {
      		retValue = retValue.substring(0, retValue.length-1);
      		ch = retValue.substring(retValue.length-1, retValue.length);
   		}
   	while (retValue.indexOf("  ") != -1) {
      		retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length);
   		}
   	return retValue;
	}

function addComment(article) {
	var form = document.getElementById('commentForm');
	var name = form.commentName.value;
	var email = form.commentEmail.value;
	var body = form.commentBody.value;
	var validate = form.imageValidation.value;
	var passed = 1;

	if(trim(validate) == '') { passed = 0; form.imageValidation.focus(); }
	if(trim(body) == '') { passed = 0; form.commentBody.focus(); }
	if(trim(email) == '') { passed = 0; form.commentEmail.focus(); }
	if(trim(name) == '') { passed = 0; form.commentName.focus(); }

	if(passed == 1) {
    		var theUrl = mainUrl+'news.php?displayType=comment&articleID='+article+'&name='+name+'&email='+email+'&content='+body+'&validate='+validate;
    		httpComment.open("GET",theUrl,true);

	 	httpComment.onreadystatechange = function() {
        	        if (httpComment.readyState == 4) {
                	        if (httpComment.status == 200) { handleCommentHttpResponse("commentSent"); }
                		}
	                }
 		httpComment.send(null);
		}
	}
function showComment(article,page) {
	var theUrl = mainUrl+'news.php?displayType=commentPage&articleID='+article+'&page='+page;
	httpComment.open("GET",theUrl,true);

 	httpComment.onreadystatechange = function() {
       	        if (httpComment.readyState == 4) {
               	        if (httpComment.status == 200) { handleCommentHttpResponse("commentPage"); }
               		}
                }
	httpComment.send(null);
	}

function handleCommentHttpResponse(type) {
  	var parsedResults = '';

	//Get the feedback
	var results = httpComment.responseXML;

	if(type == "commentSent") {
		//Parse the data
		var commentElement = results.getElementsByTagName("commentStatus").item(0);
		var commentStatus = commentElement.firstChild.nodeValue;

		if(commentStatus == 0) {
			var commentElement = results.getElementsByTagName("commentImage").item(0);
			var commentImg = commentElement.firstChild.nodeValue;

			document.getElementById('validateImage').src = commentImg;
			document.getElementById('imageValidation').value = '';
			}
			else {
				var commentElement = results.getElementsByTagName("commentText").item(0);
				var commentText = commentElement.firstChild.nodeValue;

				//Update our content
				document.getElementById('commentSubmission').innerHTML = commentText;
				}
		}
	if(type == "commentPage") {
		//Start the table
		var commentsText = '<table width="100%" border="0" cellspacing="0" cellpadding="3">';

		var commentInfo = results.getElementsByTagName("commentInfo");

		var commentElement = commentInfo[0].getElementsByTagName("topic").item(0);
		var topic = commentElement.firstChild.nodeValue;
		var commentElement = commentInfo[0].getElementsByTagName("nav").item(0);
		var nav = commentElement.firstChild.nodeValue;

		commentsText += '<tr><td class="header">'+topic+'</td></tr>';

		var allComments = results.getElementsByTagName("commentData");

		//Loop through the comments
		for (var i=0;i<allComments.length;i++) {
    			var currentComment = allComments[i];

			var commentElement = currentComment.getElementsByTagName("commentText").item(0);
			var commentText = commentElement.firstChild.nodeValue;
			var commentElement = currentComment.getElementsByTagName("commentClass").item(0);
			var commentClass = commentElement.firstChild.nodeValue;

			commentsText += '<tr><td '+commentClass+'>'+commentText+'</td></tr>';
			}

		commentsText += nav;

		commentsText += '</table>';

		//Update our content
		document.getElementById('commentText').innerHTML = commentsText;
		}

	}

function getHTTPObject() {
  	var xmlhttp;

	/*@cc_on
	@if (@_jscript_version >= 5)
		try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
		catch (e) {
			try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
			catch (E) { xmlhttp = false; }
		}
		@else xmlhttp = false;
  	@end @*/

	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try { xmlhttp = new XMLHttpRequest(); }
		catch (e) { xmlhttp = false; }
		}

	return xmlhttp;
	}

var httpComment = getHTTPObject();

function checkLogin(form) {
	if (trim(form.username.value) == "") {
		form.username.focus();
		return false;
		}
	if (trim(form.password.value) == "") {
		form.password.focus();
		return false;
		}
	return true;
	}

function checkCreate(form) {
	if (trim(form.username.value) == "") {
		form.username.focus();
		return false;
		}
	if (trim(form.email.value) == "") {
		form.email.focus();
		return false;
		}
	if (form.email.value != "") {
	        var email=form.email.value;
	        var check_space = email.indexOf(' ');
	        var check_ast = email.indexOf('@');
	        var check_dot = email.indexOf('.');
	        if ((check_space != -1) || (check_ast == -1) || (check_dot == -1)) {
		         form.email.focus();
		         return false;
		         }
		}
	if (trim(form.password1.value) == "") {
		form.password1.focus();
		return false;
		}
	if (trim(form.password2.value) == "") {
		form.password2.focus();
		return false;
		}
	if (trim(form.password1.value) != trim(form.password2.value)) {
		form.password1.focus();
		return false;
		}
	return true;
	}

function checkReset(form) {
	if (trim(form.email.value) == "") {
		form.email.focus();
		return false;
		}
	if (form.email.value != "") {
	        var email=form.email.value;
	        var check_space = email.indexOf(' ');
	        var check_ast = email.indexOf('@');
	        var check_dot = email.indexOf('.');
	        if ((check_space != -1) || (check_ast == -1) || (check_dot == -1)) {
		         form.email.focus();
		         return false;
		         }
		}
	return true;
	}

function checkResetVerify(form) {
	if (trim(form.code.value) == "") {
		form.code.focus();
		return false;
		}
	if (trim(form.pwd1.value) == "") {
		form.pwd1.focus();
		return false;
		}
	if (trim(form.pwd2.value) == "") {
		form.pwd2.focus();
		return false;
		}
	if (trim(form.pwd1.value) != trim(form.pwd1.value)) {
		form.pwd1.focus();
		return false;
		}
	return true;
	}

function checkSubmission(form) {
	if (trim(form.newTitle.value) == "") {
		form.newTitle.focus();
		return false;
		}
	if (trim(form.newBody.value) == "") {
		form.newBody.focus();
		return false;
		}
	if (trim(form.imageValidation.value) == "") {
		form.imageValidation.focus();
		return false;
		}
	return true;
	}
