//simple CSS class manipulation stuff -- useful when i'm not using jquery
CSS = {
	hasclass: function(e, c) {
		if(typeof(e) == "string") {
			e = document.getElementById(e);
		}
		if(e.className.match(new RegExp("\\b" + c + "\\b", "i"))) {
			return true;
		}
		return false;
	},
	
	addclass: function(e, c) {
		if(typeof(e) == "string") {
			e = document.getElementById(e);
		}
		if(!CSS.hasclass(e, c)) {
			e.className += (e.className ? " " : "") + c;
		}
	},

	removeclass: function(e, c) {
		if(typeof(e) == "string") {
			e = document.getElementById(e);
		}
		e.className = e.className.replace(new RegExp(" \\b" + c + "\\b|\\b" + c + "\\b ?", "gi"), "");
	},
	
	toggleclass: function(e, c) {
		if(CSS.hasclass(e, c)) {
			CSS.removeclass(e, c);
		} else {
			CSS.addclass(e, c);
		}
	}
};

//htmlentities
function htmlentities(str) {
	str = str.replace(/&/g, "&amp;");
	str = str.replace(/</g, "&lt;").replace(/>/g, "&gt;");
	//str = str.replace(/[^\.]\.{3,4}[^\.]/g, "&hellip;");
	return str;
}
function htmlentities_decode(str) {
    try {
		var tarea=document.createElement('textarea');
		tarea.innerHTML = str; return tarea.value;
		tarea.parentNode.removeChild(tarea);
	} catch(e) {
		//for IE add <div id="htmlconverter" style="display:none;"></div> to the page
		document.getElementById("htmlconverter").innerHTML = '<textarea id="innerConverter">' + str + '</textarea>';
		var content = document.getElementById("innerConverter").value;
		document.getElementById("htmlconverter").innerHTML = "";
		return content;
	}
}

//html to plain text
function htmltotext(string) {
	if(/^\s*$/.test(string)) {
		return "";
	}
	
	//replace whitespace with space, collapse whitespace
	//todo: preserve whitespace within <pre> tags
	string = string.replace(/\s+/g, " ");
	
	//newlines
	string = string.replace(/<br\b[^>]*>/gi, "\n");
	
	//rule
	string = string.replace(/<hr\b[^>]*>/, "\n\n-------------------\n\n");
	
	//bold, italic, underline, strikethrough
	string = string.replace(/<(b|strong)\b[^>]*>(.*?)<\/\1>/gi, "*$2*");
	string = string.replace(/<(i|em)\b[^>]*>(.*?)<\/\1>/gi, "/$2/");
	string = string.replace(/<u\b[^>]*>(.*?)<\/u>/gi, "_$1_");
	string = string.replace(/<(s|strike)\b[^>]*>(.*?)<\/\1>/gi, "[$2]");
	
	//subscript, superscript
	string = string.replace(/<sub\b[^>]*>(.*?)<\/sub>/gi, "_$1");
	string = string.replace(/<sup\b[^>]*>(.*?)<\/sup>/gi, "^$1");
	
	//anchor
	string = string.replace(/<a\b[^>]*\bhref=(["'])([^\1]*?)\1[^>]*>(.*?)<\/a>/gi, "$3 ($2)");
	
	//image
	string = string.replace(/<img\b[^>]*\bsrc=(["'])([^\1]*?)\1[^>]*>/gi, "(image: $2)");
	
	//comment
	string = string.replace(/<!\s*(--([^-]|\s|-[^-])*--\s*)>/, "");
	
	//headings
	string = string.replace(/<(h[123456])\b[^>]*>\n*(.*?)\n*<\/\1>/gi, "$2\n-------------------");
	
	//block-level elements -- just replace start and end with double newlines
	string = string.replace(/<\/?(p|div|address|center|dl|dt|dd|html|li|ol|pre|ul)\b[^>]*>/gi, "\n\n");
	
	//quotations (note no <p> around the <cite> -- above command removed it)
	string = string.replace(/<cite\b[^>]*>\s*(.*?)\s*<\/cite>\s*<blockquote\b[^>]*>\s*(.*?)\s*<\/blockquote>/gi, "\n\n$1 said:\n$2\n-----eoq-----\n\n");
	string = string.replace(/<blockquote\b[^>]*>\s*(.*?)\s*<\/blockquote>/gi, "\n\n\"$1\"\n\n");
	string = string.replace(/<q\b[^>]*>(.*?)<\/q>/gi, "\"$1\"");
	
	//misc inline tags of which we want to keep the contents -- just delete start and end tags
	string = string.replace(/<\/?(abbr|acronym|bdo|big|body|cite|code|del|dfn|font|ins|kbd|label|samp|small|span|tt|var)\b[^>]*>/, "");
	
	//kill all other tags and their contents
	string = string.replace(/<([A-Z][A-Z0-9]*)\b[^>]*>(.*?)<\/\1>/gi, "");
	string = string.replace(/<\/?([A-Z][A-Z0-9]*)\b[^>]*>/gi, "");
	
	//trim whitespace at the start and end
	string = string.replace(/^\s+/, "").replace(/\s+$/, "");
	
	//lose spaces right after newlines
	string = string.replace(/\n +/g, "\n");
	
	//max two newlines in a row
	string = string.replace(/\n\n\n+/g, "\n\n");
	
	//htmlentities
	string = htmlentities_decode(string);
	
	if(/^\s*$/.test(string)) {
		return "";
	}
	return string;
}

//plain text to html
function texttohtml(string) {
	if(/^\s*$/.test(string)) {
		return "";
	}
	
	//endings -> unix
	string = string.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
	
	//htmlentities
	string = htmlentities(string);
	
	//strip whitespace from the start
	string = string.replace(/^\s+/g, "");
	
	//wrap in a paragraph tag
	string = "<p>" + string + "</p>";
	
	//paragraphs
	string = string.replace(/\s*\n\s*\n\s*/g, "</p><p>");
	
	//newlines
	string = string.replace(/\n/g, "<br>");
	
	//quotations
	string = string.replace(/<p>([^<]+?) said:<br>(.*?)(<br>|<\/p><p>)-----eoq-----(<br>)?<\/p>/g, "<p><cite>$1</cite></p><blockquote><p>$2</p></blockquote>");
	
	//rules
	string = string.replace(/<p>[-_\*oO#=]{5,}(<br>)?<\/p>/g, "<hr>");
	
	//headings
	string = string.replace(/<p>([^<]+?)<br>[-_\*oO#=]{5,}(<br>)?<\/p>/g, "<h2>$1</h2>");
	
	//urls
	string = string.replace(/((([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?)/gi, "<a href=\"$1\">$1</a>");
	
	//bold, italic and underline (limit to single words)
	//these cause problems -- disabled for now
	/*string = string.replace(/(\/[^\/\s]+\/)/gi, "<i>$1</i>");
	string = string.replace(/(\*[^\*\s]+\*)/gi, "<b>$1</b>");
	string = string.replace(/(_[^_\s]+_)/gi, "<u>$1</u>");*/
	
	//lose empty paragraphs
	string = string.replace(/<p><\/p>/g, "");
	
	//put in some newlines
	string = string.replace(/<(br|\/(p|h2|blockquote|cite))>/g, "<$1>\n");
	
	return string;
}

function cleariffirstclick(el) {
	if(typeof(el.beenclicked) == "undefined" || !el.beenclicked) {
		el.beenclicked = true;
		$(el).val("").removeClass("dim");
	}
}
