function SearchToArray (theText, matchReg) {	//Dear whoever designs JavaScript: the search() method for strings should already do this.
	if (theText.match (matchReg)) {
		var matchArray = theText.match (matchReg);
		var returnArray = new Array();
		var matchPos = 0;
		for (var i = 0; i < matchArray.length; i++) {
			var match = matchArray [i];
			matchPos = theText.indexOf (match, matchPos);
			returnArray.push (matchPos);
		}
		return returnArray;
	} else {
		return false;
	}
}

function setImageSize (pic, picFactor, picRatio) {
	var picWidth = 2880*picFactor;	//	This is a metre-wide screen at 72dpi.
	if (window.innerWidth > 0) {
		picWidth = window.innerWidth*picFactor;
	} else if (body.clientWidth > 0) {
		picWidth = body.clientWidth*picFactor;
	}
	var picHeight = (picWidth*picRatio).toFixed();
	picWidth = picWidth.toFixed();
	if (pic.src.match (/image\.php\?id=\d+/)) {
		pic.src = pic.src.replace (/&fw=\d+/g, '');
		pic.src = pic.src.replace (/&fh=\d+/g, '');
		pic.src += '&fw=' + picWidth + '&fh=' + picHeight;
	}
}

function MarkUptoHTML (theText) {	
	
	var returnText = theText.replace (/</gi, "&lt;");
	returnText = returnText.replace (/>/gi, "&gt;");
	returnText = returnText.replace (/&#13;+/g, "<br>\n");
	
		//	DOUBLE COLUMNS
		
		//First get rid of any upper-case tags or it'll just make life difficult.
	returnText = returnText.replace (/\[split\]/gi, "[split]");
	returnText = returnText.replace (/\[\/split\]/gi, "[/split]");
	var splitArray = returnText.split ("[split]");		//Now catch the text following any [split] tag...
	var preText = splitArray [0];
	for (var i = 1; i < splitArray.length; i++) {
		var splitText = splitArray [i];
		splitSubArray = splitText.split ("[/split]");	//...and preceding its [/split] tag.
		splitText = splitSubArray [0];			//You will be working with the text between the tags,
		var partText = splitSubArray [1];		//but don't lose the text following [/split] and preceding the next [split].
		var postText = "";				//And you'll also need to keep hold of everything following the next [split].
		if (i < splitArray.length) {				//So slap that together now for safekeeping.
			for (j = i + 1; j < splitArray.length; j++) {
				postText = postText + splitArray [j];
			}
		}
			//Find the midpoint of your split text.  Of course, splitting it in the middle of a word would be ugly.
			//So find a space not too far after the midpoint.
			//Or a line break, if there is one.  But especially don't split up a line item in a list.
		var splitPt = (splitText.length/2).toFixed();
		var cutOff = (splitText.length*3/5).toFixed();
		if (splitText.indexOf ("[*]", splitPt) < cutOff && splitText.indexOf ("[*]", splitPt) > -1) {
			splitPt = splitText.indexOf ("[*]", splitPt);
		} else if (splitText.indexOf ("<br>\n", splitPt) < cutOff && splitText.indexOf ("<br>\n", splitPt) > -1) {
			splitPt = splitText.indexOf ("<br>\n", splitPt);
		} else if (splitText.indexOf (" ", splitPt) < cutOff && splitText.indexOf (" ", splitPt) > -1) {
			splitPt = splitText.indexOf (" ", splitPt);
		}
		var splitLeft = splitText.substr (0, splitPt);				//Catch your two halves.
		var splitRight = splitText.substr (splitPt);
		splitLeft = "<div class='splitL'>" + splitLeft.replace (/^((<br>)*\n|\s)?/, "") + "</div>";	//Create the division.
		splitRight = "\n<div class='splitR'>" + splitRight.replace (/(^((<br>)*\n|\s)?|((<br>)*\n|\s)?$)/, "") + "</div>";
		splitText = splitLeft + splitRight;
		preText = preText + splitText + partText;	//Next time round, everything before the NEXT [split] will be your preceding text.
		returnText = preText + postText;		//Add the post-text, and your modified text is complete.
		if (returnText.lastIndexOf ("</div>") == returnText.length - 6) {
			returnText = returnText + "</p>";
		}
		returnText = returnText.replace (/(<br>)+\n<div/, "\n<div");
		returnText = returnText.replace (/<\/div>\n(<br>)+/, "</div>\n");
	}
	
		//	HEADINGS
		
		//First get rid of any upper-case closers; they're more trouble than they're worth.
	returnText = returnText.replace (/\[\/head\]/gi, "[/head]");
	if (returnText.lastIndexOf ("[/head]") < returnText.length - 7) {
		returnText = returnText + "</p>";
	}
	var headStart = false;
	for (var i = 1; i <= 4; i++) {
		var headReg = new RegExp();
		headReg.compile ("\\[head=" + i.toString() + "\\]", "gi");
		var headArray = SearchToArray (returnText, headReg);
		returnText = returnText.replace (headReg, "</p><h" + i.toString() + ">");
		returnText = returnText.replace (/^<\/p>/i, "");
		if (headArray) {
			if (headArray [0] == 0) {
				headStart = true;
			}
			for (var j = 0; j < headArray.length; j++) {
				var headClose = headArray [j];
				var preTag = returnText.slice (0, headClose);
				var postTag = returnText.slice (headClose);
				var repText = "</h" + i.toString() + ">";
				var clPos = postTag.indexOf ("[/head]");
				if (postTag.length - clPos > 7) {	//If this particular "[/head]" is not the end of the text...
					repText = repText + "<p>";	//then start a new paragraph for the next part.
				}
				returnText = preTag + postTag.replace (/\[\/head\]/i, repText);
			}
		}
	}
	if (headStart == false) {			//If the text does not begin with a heading,
		returnText = "<p>" + returnText;	//then it must begin with a paragraph.
	}
	returnText = returnText.replace (/\[head(=\d)?\]/gi, "<br><strong>");
	returnText = returnText.replace (/\[\/head\]/gi, "</strong><br>");
	
		//	LISTS
		
	var listArray = returnText.match (/\n?\[\/?list(=[ai1])?\]\n?/gi);		//Find all the list beginnings and endings.
	if (listArray) {
		var listString = "";						//You'll need to know the order of nesting.
		for (var i = 0; i < listArray.length; i++) {
			var tagText = listArray [i];
			var tagPos = returnText.indexOf (tagText);		//Split off everything before the tag you're dealing with.
			var preText = returnText.substr (0, tagPos);
			var postText = returnText.substr (tagPos + tagText.length);
			if (tagText == "[list]") {				//Lists otherwise unspecified become unordered lists.
				tagText = "</p>\n<ul>";
				listString = listString + "u";
			} else if (tagText == "[/list]") {			//End the current list, and remove it from the nesting order.
				if (listString.length > 0) {
					tagText = "</" + listString.charAt (listString.length - 1) + "l>\n<p>";
					listString = listString.substr (0, listString.length - 1);
				}
			} else {						//Ordered lists come in five varieties:
				var typeChar = tagText.charAt (6);
				tagText = "</p>\n<ol";
				switch (typeChar) {
					case "a" : tagText = tagText + " class='lca'"; break;	//lower-case alphabetic (a., b., c.)
					case "A" : tagText = tagText + " class='uca'"; break;	//upper-case alphabetic (A., B., C.)
					case "i" : tagText = tagText + " class='lcr'"; break;	//lower-case Roman (i., ii., iii.)
					case "I" : tagText = tagText + " class='ucr'"; break;	//upper-case Roman (I., II., III.)
					case "1" : tagText = tagText + " class='n'";		//numeric (1., 2., 3.)
				}
				tagText = tagText + ">";
				listString = listString + "o";
			}
			returnText = preText + tagText + postText;
		}
	}
	returnText = returnText.replace (/(<br>)?\n?\[\*\]/gi, "\n<li>");
	
		//	BLOCK QUOTES
	
	returnText = returnText.replace (/\n?\[block\]/gi, "</p>\n<blockquote>");
	returnText = returnText.replace (/\[\/block\]\n?/gi, "</blockquote>\n<p>");
		
		//	LINKS
		
	if (returnText.match (/\[link=[^\]]+\]/gi)) {
		var linkArray = returnText.match (/\[link=[^\]]+\]/gi);
		for (var i = 0; i < linkArray.length; i++) {
			linkText = linkArray [i];
			urlText = linkText.substring (6, linkText.length - 1);
			if (!urlText || urlText.length == 0) {
				urlText = "#";
			}
			returnText = returnText.replace (linkText, "<a href='" + urlText + "'>");
		}
	}
	returnText = returnText.replace (/\[link\]/gi, "<a href='#'>");
	returnText = returnText.replace (/\[\/link\]/gi, "</a>");
	
		//	TEXT DECORATION
	
	returnText = returnText.replace (/\[b\]/gi, "<strong>");
	returnText = returnText.replace (/\[\/b\]/gi, "</strong>");
	returnText = returnText.replace (/\[i\]/gi, "<em>");
	returnText = returnText.replace (/\[\/i\]/gi, "</em>");

//	returnText = returnText.replace (/(<\/p>)+/gi, "</p>");
	return returnText;
}
