var text_input = "Text";
var adv_mode = "UBB Code - Insert Immediately\n\nClick on the button will insert the UBB Code immediately";
var normal_mode = "UBB Code - Prompt before Insert\n\nClick on the button will display the propmt window which will guide you to insert UBB Code";
var email_normal = "Please input the display text of the Email Link.\nLeave blank will use the Email Link itself.";
var email_normal_input = "Please input the Email Link.";
var fontsize_normal = "Please input the text which will use this size.";
var font_normal = "Please input the text which will use this font.";
var bold_normal = "Please input the text which will use bold style.";
var italicize_normal = "Please input the text which will use italic style.";
var underline_normal = "Please input the text which will use underline style.";
var strike_normal = "Please input the text which will use strike-line style.";
var sup_normal = "Please input the text which will use superscript style.";
var sub_normal = "Please input the text which will use subscript style.";
var quote_normal = "Please input the quoted text.";
var color_normal = "Please input the text which will use this color.";
var center_normal = "Please input the text which you want to align center.";
var link_normal = "Please input the display text of the URL.\nLeave blank will use the URL itself.";
var link_normal_input = "Please input the URL.";
var image_normal = "Please input the URL of the image file.";
var media_type = "Please input the media type. swf=Flash, wmp=Windows Media Player, rm=RealPlayer, qt=QuickTime.";
var media_size = "Please input the media screen size(Width,Height like 400,300)\nLeave blank will using default size.";
var media_url = "Please input the URL of the media source.";
var code_normal = "Please input the code segment.";
var list_normal = "Please input the list items. Leave blank to end input.";
var seperator_normal = "This Seperator is used for manually dividing Excerpt & Further Text Content.\nWithout inserting the Seperator, the Text Content will be seperated automatically.\nOnly One seperator is allowed in Text Content.";

var errMessage1 = "Some Required Field has not filled.";
var errMessage2 = "\n - Please Select a Category";
var errMessage3 = "\n - Please Input the Title";
var errMessage4 = "\n - Please Input the Content";
var text_seperator = "[#seperator#]";

var defmode = "normalmode";

if (defmode == "advmode") {
	normalmode = false;
	advmode = true;
} else {
	normalmode = true;
	advmode = false;
}

function setLangcn() {
	text_input = "文字";
	adv_mode = "UBB代码-立即插入\n\n单击按钮可以直接插入UBB代码.";
	normal_mode = "UBB代码-插入前提示\n\n单击按钮将显示帮助你插入UBB代码的提示窗口.";
	email_normal = "请输入邮件显示部分.\n选择空时默认显示邮件.";
	email_normal_input = "请输入邮件显示地址.";
	fontsize_normal = "请输入选择字号内的文字.";
	font_normal = "请输入选择字体内的文字.";
	bold_normal = "请输入要粗字体内的文字.";
	italicize_normal = "请输入要斜字体的文字.";
	underline_normal = "请输入要下划线的文字.";
	strike_normal = "请输入要删除线的文字.";
	sup_normal = "请输入上标.";
	sub_normal = "请输入下标.";
	quote_normal = "请输入引用文字.";
	color_normal = "请输入选择颜色内的文字.";
	center_normal = "请选择文字居中.";
	link_normal = "请输入链接显示内容:";
	link_normal_input = "请输入链接网站地址:";
	image_normal = "请输入图片路径."
	media_type = "请输入媒体类型. swf=Flash, wmp=Windows Media Player, rm=RealPlayer, qt=QuickTime.";
	media_size = "请输入媒体大小(宽,高 例如 400,300)\n选择空时默认大小.";
	media_url = "请输入媒体源链接路径.";
	code_normal = "请输入代码的文字.";
	list_normal = "请输入列表内容. 选择空时结束输入.";
	seperator_normal = "手动分割过长的文字.\n 没有这个时会自动分.\n在一份文章中只能有一个分割.";
	errMessage1 = "还有一些未写完.";
	errMessage2 = "\n - 没有选择分类.";
	errMessage3 = "\n - 没有输入文章标题.";
	errMessage4 = "\n - 文章还没有写.";
	text_seperator = "[#分割#]";
}

function setfocus() {
	document.inputform.HtmlMessage.focus();
}

function chmode(swtch){
	if (swtch == 0) {
		normalmode = false;
		advmode = true;
		alert(adv_mode);
	} else if (swtch == 2) {
		advmode = false;
		normalmode = true;
		alert(normal_mode);
	}
}

function AddText(NewCode) {
	var range = document.selection.createRange();
	if (range.text!="")
		range.text = NewCode;
	else
		if(document.all){
			insertAtCaret(document.inputform.HtmlMessage, NewCode);
			setfocus();
		} else{
			document.inputform.HtmlMessage.value += NewCode;
			setfocus();
		}
}

function storeCaret(cursorPosition) {
	if (cursorPosition.createTextRange) cursorPosition.caretPos = document.selection.createRange().duplicate();
}

function insertAtCaret (textEl, text){
	if (textEl.createTextRange && textEl.caretPos){
		var caretPos = textEl.caretPos;
		caretPos.text += caretPos.text.charAt(caretPos.text.length - 2) == ' ' ? text + ' ' : text;
	} else if(textEl) {
		textEl.value += text;
	} else {
		textEl.value = text;
	}
}

function chsize(size) {
	var range = document.selection.createRange();
	if (document.selection && document.selection.type == "Text" && range.text!="") {
		range.text = "[size=" + size + "]" + range.text + "[/size]";
	} else if (advmode) {
		AddText("[size="+size+"] [/size]");
	} else {
		txt=prompt(fontsize_normal,text_input);
		if (txt!=null) {
			AddText("[size="+size+"]"+txt+"[/size]");
		}
	}
}

function chfont(font) {
	var range = document.selection.createRange();
	if (document.selection && document.selection.type == "Text" && range.text!="") {
		range.text = "[font=" + font + "]" + range.text + "[/font]";
	} else if (advmode) {
		AddText("[font="+font+"] [/font]");
	} else {
		txt=prompt(font_normal,text_input);
		if (txt!=null) {						 
			AddText("[font="+font+"]"+txt+"[/font]");
		}
	}
}

function bold() {
	var range = document.selection.createRange();
	if (document.selection && document.selection.type == "Text" && range.text!="") {
		range.text = "[b]" + range.text + "[/b]";
	} else if (advmode) {
		AddText("[b] [/b]");
	} else {
		txt=prompt(bold_normal,text_input);
		if (txt!=null) {
			AddText("[b]"+txt+"[/b]");
		}
	}
}

function italicize() {
	var range = document.selection.createRange();
	if (document.selection && document.selection.type == "Text" && range.text!="") {
		range.text = "[i]" + range.text + "[/i]";
	} else if (advmode) {
		AddText("[i] [/i]");
	} else {	 
		txt=prompt(italicize_normal,text_input);
		if (txt!=null) {
			AddText("[i]"+txt+"[/i]");
		}
	}
}

function underline() {
	var range = document.selection.createRange();
	if (document.selection && document.selection.type == "Text" && range.text!="") {
		range.text = "[u]" + range.text + "[/u]";
	} else if (advmode) {
		AddText("[u] [/u]");
	} else {	
		txt=prompt(underline_normal,text_input);
		if (txt!=null) {					 
			AddText("[u]"+txt+"[/u]");
		}							 
	}
}

function strike() {
	var range = document.selection.createRange();
	if (document.selection && document.selection.type == "Text" && range.text!="") {
		range.text = "[s]" + range.text + "[/s]";
	} else if (advmode) {
		AddText("[s] [/s]");
	} else {	
		txt=prompt(strike_normal,text_input);
		if (txt!=null) {					 
			AddText("[s]"+txt+"[/s]");
		}							 
	}
}

function superscript() {
	var range = document.selection.createRange();
	if (document.selection && document.selection.type == "Text" && range.text!="") {
		range.text = "[sup]" + range.text + "[/sup]";
	} else if (advmode) {
		AddText("[sup] [/sup]");
	} else {	
		txt=prompt(sup_normal,text_input);
		if (txt!=null) {					 
			AddText("[sup]"+txt+"[/sup]");
		}							 
	}
}

function subscript() {
	var range = document.selection.createRange();
	if (document.selection && document.selection.type == "Text" && range.text!="") {
		range.text = "[sub]" + range.text + "[/sub]";
	} else if (advmode) {
		AddText("[sub] [/sub]");
	} else {	
		txt=prompt(sub_normal,text_input);
		if (txt!=null) {					 
			AddText("[sub]"+txt+"[/sub]");
		}							 
	}
}

function chcolor(color) {
	var range = document.selection.createRange();
	if (document.selection && document.selection.type == "Text" && range.text!="") {
		range.text = "[color=" + color + "]" + range.text + "[/color]";
	} else if (advmode) {
		AddText("[color="+color+"] [/color]");
	} else {	
		txt=prompt(color_normal,text_input);
		if(txt!=null) {
			AddText("[color="+color+"]"+txt+"[/color]");
		}
	}
}

function center() {
	var range = document.selection.createRange();
	if (document.selection && document.selection.type == "Text" && range.text!="") {
		range.text = "[align=center]" + range.text + "[/align]";
	} else if (advmode) {
		AddText("[align=center] [/align]");
	} else {	
		txt=prompt(center_normal,text_input);		 
		if (txt!=null) {					
			AddText("\r[align=center]"+txt+"[/align]");
		}							
	}
}

function hyperlink() {
	if (advmode) {
		AddText("[url] [/url]");
	} else { 
		var range = document.selection.createRange();
		txt2=prompt(link_normal,range.text); 
		if (txt2!=null) {
			txt=prompt(link_normal_input,"http://");			
			if (txt!=null) {
				if (txt2=="") {
					AddText("[url]"+txt+"[/url]");
				} else {
					AddText("[url="+txt+"]"+txt2+"[/url]");
				}
			}
		}
	}
}

function email() {
	var range = document.selection.createRange();
	if (document.selection && document.selection.type == "Text" && range.text!="") {
		range.text = "[email]" + range.text + "[/email]";
	} else if (advmode) {
		AddText("[email] [/email]");
	} else { 
		txt2=prompt(email_normal,""); 
		if (txt2!=null) {
			txt=prompt(email_normal_input,"name@domain.com");			
			if (txt!=null) {
				if (txt2=="") {
					AddText("[email]"+txt+"[/email]");
				} else {
					AddText("[email="+txt+"]"+txt2+"[/email]");
				} 
			}
		}
	}
}

function image() {
	if (advmode) {
		AddText("[img] [/img]");
	} else {	
		txt=prompt(image_normal,"http://");		
		if(txt!=null) {						
			AddText("\r[img]"+txt+"[/img]");
		}			 
	}
}

function media() {
	txt=prompt(media_type,"swf");
	while ("swf,wmp,rm,qt".indexOf(txt)<0&&txt!=null) {
		txt=prompt(media_type,"swf");
	}
	if(txt!=null) {
		txt1=prompt(media_size,"");
		txt2=prompt(media_url,"http://");
		if(txt2!=null) {
			if(txt1==""||txt1==null){
				AddText("\r["+txt+"]"+txt2+"[/"+txt+"]");
			}else{
				AddText("\r["+txt+"="+txt1+"]"+txt2+"[/"+txt+"]");
			}
		}
	}
}

function list() {
	if (advmode) {
		AddText("\r[list]\r[*]\r[*]\r[*]\r[/list]");
	} else {	
		AddTxt="\r[list]\r\n";
		txt="1";
		while ((txt!="") && (txt!=null)) {
			txt=prompt(list_normal,""); 
			if (txt!="") {						 
					AddTxt+="[*]"+txt+"\r"; 
			}									 
		} 
		AddTxt+="[/list]\r\n";
		AddText(AddTxt); 
	}
}

function code() {
	var range = document.selection.createRange();
	if (document.selection && document.selection.type == "Text" && range.text!="") {
		range.text = "[code]" + range.text + "[/code]";
	} else if (advmode) {
		AddText("\r[code]\r[/code]");
	} else {	 
		txt=prompt(code_normal,"");		 
		if (txt!=null) {					
			AddText("\r[code]"+txt+"[/code]");
		}							
	}
}

function quote() {
	var range = document.selection.createRange();
	if (document.selection && document.selection.type == "Text" && range.text!="") {
		range.text = "[quote]" + range.text + "[/quote]";
	} else if (advmode) {
		AddText("\r[quote]\r[/quote]");
	} else {
		txt=prompt(quote_normal,text_input);
		if(txt!=null) {
			AddText("\r[quote]\r"+txt+"\r[/quote]");
		}
	}
}

function insertIcon(iconCode) {
	var txtarea = document.inputform.HtmlMessage;
	iconCode = ' ' + iconCode + ' ';
	if (txtarea.createTextRange && txtarea.caretPos) {
		var caretPos = txtarea.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? iconCode + ' ' : iconCode;
		txtarea.focus();
	} else {
		txtarea.value	+= iconCode;
		txtarea.focus();
	}
}

function seperator() {
	var txtarea = document.inputform.HtmlMessage;
	alert(seperator_normal);
	if (txtarea.createTextRange && txtarea.caretPos) {
		var caretPos = txtarea.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text_seperator+' ' : text_seperator+'';
		txtarea.focus();
	} else {
		txtarea.value	+= text_seperator;
		txtarea.focus();
	}
}

//Ctrl+Enter to Post
function CtrlEnter() { 
	if(event.ctrlKey && window.event.keyCode==13) document.inputform.SubmitBtn.click();
}

//Check Form
function CheckInputForm(){
	var errMessage, bError, theForm;
	theForm=document.inputform;
	theForm.SubmitBtn.disabled=true;
	errMessage=errMessage1;
	bError=false;
/*	if(theForm.htm_CateID.value=="0"){
		errMessage+=errMessage2;
		bError=true;
	}*/
	if(theForm.Htm_Title.value==""){
		errMessage+=errMessage3;
		bError=true;
	}
	if(theForm.HtmlMessage.value=="" && theForm.Htm_FromURL.value==""){
		errMessage+=errMessage4;
		bError=true;
	}
	if(bError){
		alert(errMessage);
		theForm.SubmitBtn.disabled=false;
		return false;
	}else{
		theForm.submit();
		return true;
	}
}
