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 checkSubmission(form) {
	if (trim(form.title.value) == "") {
		form.title.focus();
		return false;
		}
	if (trim(form.body.value) == "") {
		form.body.focus();
		return false;
		}
	return true;
	}

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 addComment(form) {
	var article = form.articleID.value;
	var page = form.showPage.value;
	var name = form.commentName.value;
	var email = form.commentEmail.value;
	var body = form.commentBody.value;
	var passed = 1;

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

	if(passed == 1) {
    		var theUrl = mainUrl+'newsdriver.php?comment='+article+'&page='+page+'&name='+name+'&email='+email+'&content='+body;
    		http.open("GET",theUrl,true);

	 	http.onreadystatechange = function() {
        	        if (http.readyState == 4) {
                	        if (http.status == 200) { handleHttpResponse('a',article); }
                		}
	                }
 		http.send(null);
		}
	}

function rei_replace (regexp, replacement, subject){
    	result = subject.replace( new RegExp(regexp,'gi'),  replacement);
    	return result;
	}

function stripPreview(text) {
	text = rei_replace("\\[([^\\[]*?)\\]","",text);
	}

function toNewsHtml(text){
    	// Convert near-URL tags to HTML
    	text = rei_replace ("([\n ])([a-z]+?)://([^, \n\r]+)", "$1<a href=\"$2://$3\" target=\"_blank\">$2://$3</a>", text);
    	text = rei_replace ("([\n ])www\\.([a-z0-9\-]+)\.([a-z0-9\\-.\\~]+)((?:/[^, \n\r]*)?)", "$1<a href=\"http://www.$2.$3$4\" target=\"_blank\">www.$2.$3$4</a>", text);
    	text = rei_replace ("([\n ])([a-z0-9\\-_.]+?)@([^, \n\r]+)","$1<a href=\"mailto:$2@$3\">$2@$3</a>", text);

    	// Convert URL tags to HTML
    	text = rei_replace ("\\[url\\]ftp://([^\\[]*?)\\[/url\\]", "<a href=\"ftp://$1\" target=\"_blank\">ftp://$1</a>", text);
    	text = rei_replace("\\[url\\]http://([^\\[]*?)\\[/url\\]","<a href=\"http://$1\" target=\"_blank\">http://$1</a>",text);
    	text = rei_replace("\\[url\\]https://([^\\[]*?)\\[/url\\]","<a href=\"https://$1\" target=\"_blank\">https://$1</a>",text);
    	text = rei_replace("\\[url\\]([^\\[]*?)\\[/url\\]","<a href=\"http://$1\" target=\"_blank\">$1</a>",text);
    	text = rei_replace("\\[url=http://(.*?)\\](.*?)\\[/url\\]","<a href=\"http://$1\" target=\"_blank\">$2</a>",text);
    	text = rei_replace("\\[url=https://(.*?)\\](.*?)\\[/url\\]","<a href=\"https://$1\" target=\"_blank\">$2</a>",text);
    	text = rei_replace("\\[url=(.*?)\\](.*?)\\[/url\\]","<a href=\"http://$1\" target=\"_blank\">$2</a>",text);

    	// Convert IMG tags to HTML
    	text = rei_replace("\\[img\\]([^\\[]*?)\\[/img\\]","<img src=\"$1\">",text);
    	text = rei_replace("\\[image=([^\\[]*?)\\]","<img src=\"../images/$1\" align=\"left\">",text);

    	// Convert bolds,italics,underline and strike
    	text = rei_replace ("\\[b\\]","<b>",text);
    	text = rei_replace ("\\[\\/b\\]","</b>",text);
    	text = rei_replace ("\\[i\\]","<i>",text);
    	text = rei_replace ("\\[\\/i\\]","</i>",text);
    	text = rei_replace ("\\[u\\]","<u>",text);
    	text = rei_replace ("\\[\\/u\\]","</u>",text);
    	text = rei_replace ("\\[s\\]","<strike>",text);
    	text = rei_replace ("\\[\\/s\\]","</strike>",text);

    	// Alignment
    	text = rei_replace ("\\[right\\]","<div style=\"text-align:right\">",text);
    	text = rei_replace ("\\[\\/right\\]","</div>",text);
    	text = rei_replace ("\\[justify\\]","<div style=\"text-align:justify;\">",text);
    	text = rei_replace ("\\[\\/justify\\]","</div>",text);
    	text = rei_replace ("\\[center\\]","<div style=\"text-align:center\">",text);
    	text = rei_replace ("\\[\\/center\\]","</div>",text);

    	// Convert the color codes
    	text = rei_replace("\\[color=(.*?)\\](.*?)","<span style=\"color:$1\">$2",text);
    	text = rei_replace("\\[\\/color\\]","</span>",text);

    	// Convert sizes
    	text = rei_replace("\\[size=(.*?)\\](.*?)","<span style=\"font-size:$1pt\">$2",text);
    	text = rei_replace("\\[\\/size\\]","</span>",text);

    	// Convert fonts
    	text = rei_replace("\\[font=(.*?)\\](.*?)","<span style=\"font-family:$1\">$2",text);
    	text = rei_replace("\\[\\/font\\]","</span>",text);

    	// Do list elements
    	text = rei_replace("(\\[list\\])\n?\r?(.+?)(\\[\\/list\\])s","<ul type=\"square\">$2</ul>",text);
    	text = rei_replace("(\\[list=)(A|1)(\\])\n?\r?(.+?)(\\[\\/list\\])s","<ol type=\"$2\">$4</ol>",text);
    	text = rei_replace("\n?\r?(\\[\\*\\])s","<li>",text);

    	// Quote markup
    	text = rei_replace ("\\[quote\\]","<blockquote>Quote:<hr /><br />",text);
    	text = rei_replace ("\\[\\/quote\\]","<br /><br /><hr /></blockquote>",text);

    	// Convert newlines
    	text = rei_replace ("\n","<br clear=\"all\">",text);
    	text = rei_replace ("\r\n","<br clear=\"all\">",text);
    	text = rei_replace ("\r","",text);

    	//Show Pages
    	text = rei_replace ("\\[page\\]","<hr>",text);

     	return text;
	}

function showArticle(articleID,page,comment) {
	var theUrl = mainUrl+'newsdriver.php?article='+articleID+'&showPage='+page+'&showCommentPage='+comment;
 	http.open("GET", theUrl, true);
 	http.onreadystatechange = function() {
                if (http.readyState == 4) {
                        if (http.status == 200) { handleHttpResponse('a',articleID); }
                	}
                }
 	http.send(null);
 	}

function showCategory(category,archive) {
	var theUrl = mainUrl+'newsdriver.php?category='+category+'&archive='+archive;
 	http.open("GET", theUrl, true);
 	http.onreadystatechange = function() {
                if (http.readyState == 4) {
                        if (http.status == 200) { handleHttpResponse('c',category); }
                	}
                }
 	http.send(null);
 	}

function handleHttpResponse(type,value) {
  	var parsedResults = '';

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

	//Parse the data

	//Article
	if(type == 'a') {
		var articleIDElement = results.getElementsByTagName("articleID").item(0);
    		var articleID = articleIDElement.firstChild.nodeValue;
    		var articleTitleElement = results.getElementsByTagName("articleTitle").item(0);
		var articleTitle = articleTitleElement.firstChild.nodeValue;
		var articleBodyElement = results.getElementsByTagName("articleBody").item(0);
		var articleBody = articleBodyElement.firstChild.nodeValue;
		var commentBodyElement = results.getElementsByTagName("articleCommentBlock").item(0);
		var commentBody = commentBodyElement.firstChild.nodeValue;

		parsedResults = '<div id="newsTable"><table width="'+tableWidth+'" border="0" cellspacing="0" cellpadding="3">';
		parsedResults += '<tr><td class="articleTitle"><a onMouseOver="self.status=\'\'; return true;" onMouseOut="self.status=\'\';" class="articleLink" href="javascript:void(0);" onClick="javascript:showArticle('+articleID+',1);">'+articleTitle+'</a></td></tr>';
		parsedResults += '<tr><td>'+articleBody+'</td></tr>';
		if(commentBody != 'disabled') { parsedResults += '<tr><td>'+commentBody+'</td></tr>'; }
		parsedResults += '</table></div>';
		}

	//Category/categories
	if(type == 'c') {
		var allCategories = results.getElementsByTagName("category");

		//Start the table
		parsedResults = '<div id="newsTable"><table width="'+tableWidth+'" border="0" cellspacing="0" cellpadding="3">';

		//Loop through the categories
		for (var i=0;i<allCategories.length;i++) {
    			var currentCategory = allCategories[i];

    			var catIDElement = currentCategory.getElementsByTagName("categoryID").item(0);
    			var catID = catIDElement.firstChild.nodeValue;
    			if(catID != 0) {
    				var catTitleElement = currentCategory.getElementsByTagName("categoryTitle").item(0);
    				var catTitle = catTitleElement.firstChild.nodeValue;
    				parsedResults += '<tr><td class="categoryTitle"><a onMouseOver="self.status=\'\'; return true;" onMouseOut="self.status=\'\';" class="categoryLink" href="javascript:void(0);" onClick="javascript:showCategory('+catID+',0);">'+catTitle+'</a></td></tr>';
    				}
    				else { catTitle = ''; }

			//Loop through the articles
			var allArticles = currentCategory.getElementsByTagName("article");
			for (var j=0;j<allArticles.length;j++) {
    				var currentArticle = allArticles[j];

	    			var articleIDElement = currentArticle.getElementsByTagName("articleID").item(0);
    				var articleID = articleIDElement.firstChild.nodeValue;
    				var articleTitleElement = currentArticle.getElementsByTagName("articleTitle").item(0);
    				var articleTitle = articleTitleElement.firstChild.nodeValue;
    				var articleBodyElement = currentArticle.getElementsByTagName("articleBody").item(0);
    				var articleBody = articleBodyElement.firstChild.nodeValue;
    				var articleImageElement = currentArticle.getElementsByTagName("articleImage").item(0);
				var articleImage = articleImageElement.firstChild.nodeValue;

				if(articleImage != 'none' && articleBody != 'nodisplay') {
    					articleBody = '<a onMouseOver="self.status=\'\'; return true;" onMouseOut="self.status=\'\';" href="javascript:void(0);" onClick="javascript:showArticle('+articleID+',1,1);"><img src="'+mainUrl+'images/'+articleImage+'" class="thumbnail" align="left"></a>'+articleBody;
   					}

    				parsedResults += '<tr><td class="articleTitle"><a onMouseOver="self.status=\'\'; return true;" onMouseOut="self.status=\'\';" class="articleLink" href="javascript:void(0);" onClick="javascript:showArticle('+articleID+',1,1);">'+articleTitle+'</a></td></tr>';
    				if(articleBody != 'nodisplay') { parsedResults += '<tr><td>'+articleBody+'</td></tr>'; }
    				}
			}
		if(value != 0) { parsedResults += '<tr><td align="right" class="smallText">'+returnTo+': <a onMouseOver="self.status=\'\'; return true;" onMouseOut="self.status=\'\';" class="smallLink" href="javascript:void(0);" onClick="javascript:showCategory(0,0);">'+index+'</a></td></tr>'; }
		if(value != 0 && showArchive != 0) { parsedResults += '<tr><td align="right" class="smallText"><a onMouseOver="self.status=\'\'; return true;" onMouseOut="self.status=\'\';" class="smallLink" href="javascript:void(0);" onClick="javascript:showCategory('+value+',1);">'+archive+'</a></td></tr>'; }
		if(post == 1) { parsedResults += '<tr><td class="smallText"><a class="smallLink" href="'+mainUrl+'admin/" target="_blank">'+postArticle+'</a></td></tr>'; }
		parsedResults += '</table></div>';
		}

	//Update our content
	document.getElementById('newsDisplay').innerHTML = parsedResults;
	}

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 http = getHTTPObject();