//**** VNRTE Configuration ****/

if(typeof(VNRTE_SKIN)=="undefined") VNRTE_SKIN='htmlarea';
if(typeof(VNRTE_LANGUAGE)=="undefined") VNRTE_LANGUAGE='en';
if(typeof(VNRTE_RICH_EDITOR)=="undefined") VNRTE_RICH_EDITOR=true; //turn all textarea to rich text editor
if(typeof(VNRTE_FORMATHTMLTEXT)=="undefined") VNRTE_FORMATHTMLTEXT=false;//set it false if you don't want to format the html text before it's submitted
if(typeof(VNRTE_EDITOR_STYLE)=="undefined") VNRTE_EDITOR_STYLE='<style>body { margin:2px 2px 2px 2px;background-color: #fff; font-family: verdana,sans-serif; font-size: 12px;}</style>';
if(typeof(VNRTE_ASCII_EMOTICONS)=="undefined") VNRTE_ASCII_EMOTICONS=true;
if(typeof(VNRTE_CLEAN_SOURCE)=="undefined") VNRTE_CLEAN_SOURCE=true;
if(typeof(VNRTE_STATUS_BAR)=="undefined") VNRTE_STATUS_BAR=false;
//put names that you don't want vietnamese input supported, use ^ and $ for exactly match the word
if(typeof(VNRTE_ENGLISH_INPUTS)=="undefined") VNRTE_ENGLISH_INPUTS=/^(username|email)$/i;

//get domain address <= comment it if you want keep absolute url
LOCAL_DOMAIN= document.location.href;
LOCAL_DOMAIN=LOCAL_DOMAIN.substring(0,LOCAL_DOMAIN.indexOf('/',10));

if (document.all)
	VNRTE_MOZILLA=false;
else
	VNRTE_MOZILLA=true;

//This is a modification of vietdev function
function getFullScriptPath(script)
{
	var i=0, path='';
	while(document.getElementsByTagName('script')[i]) {
		var src=document.getElementsByTagName('script')[i].src;
		if (src && src.lastIndexOf(script)>=0 ) {
			path=src.substring(0,src.lastIndexOf(script));
			break;
		}
		i++;
	}
	if (path.indexOf("://")>=0) return path;

	var href= document.location.href;
	href= href.substring(0,href.lastIndexOf('/'));

	if (path=='.' || path=='') return href;
	if (path.charAt(0)=='/') return path;
	if (path.substring(0,2)=="./") path=path.substring(2);

	return href+'/'+path;
}

function contentHTMLCode(textarea) {
	var value=textarea.value;
	var point=0;
	if (value.search(/<BR>/gi)>0) point++;
	if (value.search(/<\/P>/gi)>0) point++;
	if (value.search(/<\/DIV>/gi)>0) point++;
	if (value.search(/<\/SPAN>/gi)>0) point++;
	if (value.search(/<\/TABLE>/gi)>0) point++;
	return (point>0);
}
//this will be called when all scripts are loaded
function startVNRTE() {
	if (typeof(vietEmbed)=="function") {
		var inputs = document.getElementsByTagName("input");
		for (var i=0;i<inputs.length;i++) {
			if ((inputs[i].type=="text") && (inputs[i].lang!="plain")) {
				if (VNRTE_ENGLISH_INPUTS && (inputs[i].name.length>0) && inputs[i].name.match(VNRTE_ENGLISH_INPUTS))
					continue;
				vietEmbed(inputs[i]);
			}
		}
		//if user doesn't want to use rich text editor, textareas still support vietnamese input
		var inputs = document.getElementsByTagName("textarea");
		for (var i=0;i<inputs.length;i++) {
			if (inputs[i].lang!="plain") {
				if (VNRTE_ENGLISH_INPUTS && (inputs[i].name.length>0) && inputs[i].name.match(VNRTE_ENGLISH_INPUTS))
					continue;
				vietEmbed(inputs[i]);
				if (VNRTE_RICH_EDITOR && contentHTMLCode(inputs[i]) && inputs [i].name != "desc")
					switchNormalRTE(inputs[i]);
				else if (inputs[i].name=="richcontent") switchNormalRTE(inputs[i]);
			}
		}
	} else {
		//try again
		setTimeout("startVNRTE()",500);
	}
}

//convert an textarea to richtexteditor
function switchNormalRTE(textarea) {
	if (textarea.nodeName && textarea.nodeName=="TEXTAREA") {
		if (typeof(textarea._editMode)=="undefined" || textarea._editMode=="plain")
	 		(new HTMLArea(textarea)).generate();
	}
}

//format the text inside editor before the form is submitted
function formatHTMLText(text) {
	//remove all local links in tag (src, href)
	//remove all dangerous code: javascript (event action), form, input
	if (VNRTE_CLEAN_SOURCE)
		text = cleanHTMLCode(text);
	//replace emoticons
	if (VNRTE_ASCII_EMOTICONS)
		text = replaceEmoticons(text);
	return text;
}

function removeDomain(link) {
	if ((link.length>0) && (link.indexOf("://")>0)) {
		return link.substring(link.indexOf("/",10));
	}
	return link;
}

function vnrteStringReplace(text,look,pharse) {
	var pos = text.length;
	pos = text.lastIndexOf(look,pos);
	while (pos>=0)
	{
		text = text.substring(0,pos)+pharse+text.substring(pos+look.length);
		pos = text.lastIndexOf(look,pos);
	}
	return text;
}

function replaceEmoticons(text) {
	/***
	Yahoo Emoticons:
	http://help.yahoo.com/help/us/mesg/use/use-38.html
	http://help.yahoo.com/help/us/mesg/use/use-44.html
	***/
	var EmoticonPath = removeDomain(VNRTE_PATH)+"/images/emoticons/";
	var Emoticons = {
		" :) ":"01.gif",//happy //01 02 03 04 05 07 08 09 10 11 13 14 15 16 17 19 20 21 22 23
		" :( ":"02.gif", //sad
		" ;) ":"03.gif", //wink
		" :D ":"04.gif", //big grin
		" ;;) ":"05.gif", //batting eyelashes
		" :-/ ":"07.gif", //confused
		" :x ":"08.gif", //love struck
		" :\"> ":"09.gif", //blushing
		" :p ":"10.gif", //tongue
		" :* ":"11.gif", //kiss
		" :O ":"13.gif", //shock
		" X-( ":"14.gif", //angry
		" :> ":"15.gif", //smug
		" B-) ":"16.gif", //cool
		" :-s ":"17.gif", //worried
		" >:) ":"19.gif", //devilish
		" :(( ":"20.gif", //crying
		" :)) ":"21.gif", //laughing
		"  :| ":"22.gif", //straight face
		" /:) ":"23.gif", //raised eyebrow
		" O:) ":"25.gif", //angel : 25 26 27 28 29 30 32 33 34 35 37 38 39 40 41
		" :-B ":"26.gif", //nerd
		" =; ":"27.gif", //talk to the hand
		" I-) ":"28.gif", //sleep
		" 8-| ":"29.gif", //rolling eyes
		" :-& ":"30.gif", //sick
		" :-$ ":"32.gif", //shhh
		" [-( ":"33.gif", //not talking
		" :o) ":"34.gif", //clown
		" 8-} ":"35.gif", //silly
		" (:| ":"37.gif", //tired
		" =P~ ":"38.gif", //drooling
		" :-? ":"39.gif", //thinking
		" #-o ":"40.gif", //d'oh!
		" =D> ":"41.gif" //applause
	}
	var reg=null;
	for (var icon in Emoticons) {
		text = vnrteStringReplace(text,icon,'&nbsp;<img src="'+EmoticonPath+Emoticons[icon]+'" />&nbsp;');
	}
	return text;
};


VNRTE_PATH = getFullScriptPath('/vnrte.js');

document.writeln('<style>@import url("' + VNRTE_PATH + '/skin/'+ VNRTE_SKIN +'.css");</style>');
document.writeln('<div id="VNRTE_Status" name="VNRTE_Status" style="position:absolute;visibility:hidden;width:240;height:50;" class="VNRTEStatus"></div>');
document.writeln('<script src="'+VNRTE_PATH+'/cookie.js"></script>');
if (VNRTE_RICH_EDITOR) {
	document.writeln('<script src="'+VNRTE_PATH+'/htmlarea.js"></script>');
	document.writeln('<script src="'+VNRTE_PATH+'/lang/'+VNRTE_LANGUAGE+'.js"></script>');
	document.writeln('<script src="'+VNRTE_PATH+'/dialog.js"></script>');
	if (VNRTE_CLEAN_SOURCE)
		document.writeln('<script src="'+VNRTE_PATH+'/htmltags.js"></script>');
}
document.writeln('<script src="'+VNRTE_PATH+'/vietuni.js"></script>');

//make startVNRTE the onload function
var _VNRTE_patched_onload = window.onload;
if (typeof(_VNRTE_patched_onload) != 'function') {
	onload = startVNRTE
} else {
	onload = function() {
			_VNRTE_patched_onload ();
			startVNRTE ();
		}
}