var text_input = "文字";
var adv_mode = "UBB模式：直接插入代码\n\n点击按钮将直接插入UBB内容";
var normal_mode = "UBB模式：向导插入代码\n\n点击按钮将出现提示窗口，引导你输入UBB内容";
var email_normal = "请输入Email链接所要显示的文字。\n不填将自动使用Email地址作为显示的文字。";
var email_normal_input = "请输入Email链接地址。";
var fontsize_normal = "请输入要使用此大小的文字内容。";
var font_normal = "请输入要使用此字体的文字内容。";
var bold_normal = "请输入要使用粗体效果的文字内容。";
var italicize_normal = "请输入要使用斜体效果的文字内容。";
var underline_normal = "请输入要使用下划线效果的文字内容。";
var strike_normal = "请输入要使用删除线效果的文字内容。";
var sup_normal = "请输入要使用上标效果的文字内容。";
var sub_normal = "请输入要使用下标效果的文字内容。";
var quote_normal = "请输入引用的文字内容。";
var color_normal = "请输入要使用此颜色的文字内容。";
var center_normal = "请输入要居中显示的文字内容。";
var link_normal = "请输入超链接所要显示的文字。\n不填将自动使用链接地址作为显示的文字。";
var link_normal_input = "请输入超链接的地址";
var image_normal = "请输入图片的URL地址";
var media_type = "请输入媒体文件的类型。 swf=Flash，wmp=Windows Media Player，rm=RealPlayer， qt=QuickTime";
var media_size = "请输入媒体文件显示尺寸(如：400,300)\n不填将使用默认尺寸。";
var media_url = "请输入媒体文件的网络地址。";
var code_normal = "请输入源代码的内容。";
var list_normal = "请输入文字作为列表的一条内容。不填将结束输入。";
var seperator_normal = "分割符用于手动设置日志的摘要内容。\n不设置分割符，日志将自动显示摘要。\n每篇日志仅允许有一个分割符。";

var defmode = "advmode";

if (defmode == "advmode") {
		normalmode = false;
		advmode = true;
} else {
		normalmode = true;
		advmode = false;
}

function setfocus() {
		document.inputform.message.focus();
}

function chmode(swtch){
		if (swtch == 1) {
			normalmode = false;
			advmode = true;
			alert(adv_mode);
		} else if (swtch == 0) {
			advmode = false;
			normalmode = true;
			alert(normal_mode);
		}
}

function AddText(NewCode) {
	if(document.all){
		insertAtCaret(document.inputform.message, NewCode);
		setfocus();
	} else{
		document.inputform.message.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) {
		if (document.selection && document.selection.type == "Text") {
		var range = document.selection.createRange();
		range.text = "[size=" + size + "]" + range.text + "[/size]";
		} else if (advmode) {
			AddTxt="[size="+size+"] [/size]";
			AddText(AddTxt);
		} else {                       
			txt=prompt(fontsize_normal,text_input); 
			if (txt!=null) {             
			AddTxt="[size="+size+"]"+txt;
			AddText(AddTxt);
			AddText("[/size]");
			}        
		}
}

function chfont(font) {
		if (document.selection && document.selection.type == "Text") {
			var range = document.selection.createRange();
			range.text = "[font=" + font + "]" + range.text + "[/font]";
		} else if (advmode) {
			AddTxt="[font="+font+"] [/font]";
			AddText(AddTxt);
		} else {                  
			txt=prompt(font_normal,text_input);
			if (txt!=null) {             
				AddTxt="[font="+font+"]"+txt;
				AddText(AddTxt);
				AddText("[/font]");
			}        
		}  
}


function bold() {
		if (document.selection && document.selection.type == "Text") {
		var range = document.selection.createRange();
		range.text = "[b]" + range.text + "[/b]";
		} else if (advmode) {
			AddTxt="[b] [/b]";
			AddText(AddTxt);
		} else {  
			txt=prompt(bold_normal,text_input);     
			if (txt!=null) {           
			AddTxt="[b]"+txt;
			AddText(AddTxt);
			AddText("[/b]");
			}       
		}
}

function italicize() {
		if (document.selection && document.selection.type == "Text") {
		var range = document.selection.createRange();
		range.text = "[i]" + range.text + "[/i]";
		} else if (advmode) {
			AddTxt="[i] [/i]";
			AddText(AddTxt);
		} else {   
			txt=prompt(italicize_normal,text_input);     
			if (txt!=null) {           
			AddTxt="[i]"+txt;
			AddText(AddTxt);
			AddText("[/i]");
			}               
		}
}

function underline() {
		if (document.selection && document.selection.type == "Text") {
		var range = document.selection.createRange();
		range.text = "[u]" + range.text + "[/u]";
		} else if (advmode) {
			AddTxt="[u] [/u]";
			AddText(AddTxt);
		} else {  
			txt=prompt(underline_normal,text_input);
			if (txt!=null) {           
			AddTxt="[u]"+txt;
			AddText(AddTxt);
			AddText("[/u]");
			}               
		}
}

function strike() {
		if (document.selection && document.selection.type == "Text") {
		var range = document.selection.createRange();
		range.text = "[s]" + range.text + "[/s]";
		} else if (advmode) {
			AddTxt="[s] [/s]";
			AddText(AddTxt);
		} else {  
			txt=prompt(strike_normal,text_input);
			if (txt!=null) {           
			AddTxt="[s]"+txt;
			AddText(AddTxt);
			AddText("[/s]");
			}               
		}
}

function superscript() {
		if (document.selection && document.selection.type == "Text") {
		var range = document.selection.createRange();
		range.text = "[sup]" + range.text + "[/sup]";
		} else if (advmode) {
			AddTxt="[sup] [/sup]";
			AddText(AddTxt);
		} else {  
			txt=prompt(sup_normal,text_input);
			if (txt!=null) {           
			AddTxt="[sup]"+txt;
			AddText(AddTxt);
			AddText("[/sup]");
			}               
		}
}

function subscript() {
		if (document.selection && document.selection.type == "Text") {
		var range = document.selection.createRange();
		range.text = "[sub]" + range.text + "[/sub]";
		} else if (advmode) {
			AddTxt="[sub] [/sub]";
			AddText(AddTxt);
		} else {  
			txt=prompt(sub_normal,text_input);
			if (txt!=null) {           
			AddTxt="[sub]"+txt;
			AddText(AddTxt);
			AddText("[/sub]");
			}               
		}
}

function chcolor(color) {
		if (document.selection && document.selection.type == "Text") {
		var range = document.selection.createRange();
		range.text = "[color=" + color + "]" + range.text + "[/color]";
		} else if (advmode) {
			AddTxt="[color="+color+"] [/color]";
			AddText(AddTxt);
		} else {  
		txt=prompt(color_normal,text_input);
			if(txt!=null) {
			AddTxt="[color="+color+"]"+txt;
			AddText(AddTxt);
			AddText("[/color]");
			}
		}
}

function center() {
		if (document.selection && document.selection.type == "Text") {
		var range = document.selection.createRange();
		range.text = "[align=center]" + range.text + "[/align]";
		} else if (advmode) {
			AddTxt="[align=center] [/align]";
			AddText(AddTxt);
		} else {  
			txt=prompt(center_normal,text_input);     
			if (txt!=null) {          
			AddTxt="\n[align=center]"+txt;
			AddText(AddTxt);
			AddText("[/align]");
			}              
		}
}

function hyperlink() {
		if (advmode) {
			AddTxt="[url] [/url]";
			AddText(AddTxt);
		} else { 
			txt2=prompt(link_normal,""); 
			if (txt2!=null) {
			txt=prompt(link_normal_input,"http://");      
			if (txt!=null) {
				if (txt2=="") {
						AddTxt="[url]"+txt;
						AddText(AddTxt);
						AddText("[/url]");
				} else {
						AddTxt="[url="+txt+"]"+txt2;
						AddText(AddTxt);
						AddText("[/url]");
				}         
			} 
			}
		}
}

function email() {
	if (document.selection && document.selection.type == "Text") {
		var range = document.selection.createRange();
		range.text = "[email]" + range.text + "[/email]";
	} else if (advmode) {
		AddTxt="[email] [/email]";
			AddText(AddTxt);
		} else { 
			txt2=prompt(email_normal,""); 
			if (txt2!=null) {
			txt=prompt(email_normal_input,"name@domain.com");      
			if (txt!=null) {
				if (txt2=="") {
						AddTxt="[email]"+txt+"[/email]";
			
				} else {
						AddTxt="[email="+txt+"]"+txt2+"[/email]";
				} 
				AddText(AddTxt);                
			}
			}
		}
}

function image() {
		if (advmode) {
			AddTxt="[img] [/img]";
			AddText(AddTxt);
		} else {  
			txt=prompt(image_normal,"http://");    
			if(txt!=null) {            
			AddTxt="\n[img]"+txt;
			AddText(AddTxt);
			AddText("[/img]");
			}       
		}
}

function media() {
	txt=prompt(media_type,"swf");
	while ("swf,wmp,rm,qt".indexOf(txt)<0||txt=="") {
		txt=prompt(media_type,"swf");               
	}
	txt1=prompt(media_size,"");
	txt2=prompt(media_url,"http://");
	if(txt!=null&&txt2!=null) {       
		if(txt1==""||txt1==null){
			AddTxt="\n["+txt+"]"+txt2;
		}else{
			AddTxt="\n["+txt+"="+txt1+"]"+txt2;
		}
			AddText(AddTxt);
			AddText("[/"+txt+"]");
	}       
}

function list() {
		if (advmode) {
			AddTxt="\n[list]\n[*]\n[*]\n[*]\n[/list]\n";
			AddText(AddTxt);
		} else {  
		AddTxt="\n[list]\n";
		txt="1";
		while ((txt!="") && (txt!=null)) {
			txt=prompt(list_normal,""); 
			if (txt!="") {             
					AddTxt+="[*]"+txt+"\n"; 
			}                   
		} 
			AddTxt+="[/list]\n";
			AddText(AddTxt); 
		}
}

function code() {
		if (document.selection && document.selection.type == "Text") {
		var range = document.selection.createRange();
		range.text = "[code]" + range.text + "[/code]";
		} else if (advmode) {
			AddTxt="\n[code]\n[/code]";
			AddText(AddTxt);
		} else {   
			txt=prompt(code_normal,"");     
			if (txt!=null) {          
			AddTxt="\n[code]"+txt;
			AddText(AddTxt);
			AddText("[/code]");
			}              
		}
}

function quote() {
		if (document.selection && document.selection.type == "Text") {
		var range = document.selection.createRange();
		range.text = "[quote]" + range.text + "[/quote]";
		} else if (advmode) {
			AddTxt="\n[quote]\n[/quote]";
			AddText(AddTxt);
		} else {   
			txt=prompt(quote_normal,text_input);     
			if(txt!=null) {          
			AddTxt="\n[quote]\n"+txt;
			AddText(AddTxt);
			AddText("\n[/quote]");
			}               
		}
}

function insertSmilies(strCode) {
	var txtarea = document.inputform.message;
	strCode = ' ' + strCode + ' ';
	if (txtarea.createTextRange && txtarea.caretPos) {
	var caretPos = txtarea.caretPos;
	caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? strCode + ' ' : strCode;
	txtarea.focus();
	} else {
	txtarea.value  += strCode;
	txtarea.focus();
	}
}

function seperator() {
	var txtarea = document.inputform.message;
	alert(seperator_normal);
	if (txtarea.createTextRange && txtarea.caretPos) {
	var caretPos = txtarea.caretPos;
	caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? '[separator] ' : '[separator]';
	txtarea.focus();
	} else {
	txtarea.value  += '[separator]';
	txtarea.focus();
	}
}

//Ctrl+Enter to Post
function CtrlEnter() { 
	if(event.ctrlKey && window.event.keyCode==13) document.inputform.btnSubmit.click();
}

//Check Form
function CheckInputForm(){
	var errMessage, bError, theForm;
	theForm=document.inputform;
	theForm.btnSubmit.disabled=true;
	errMessage="请将必填的项目填写完整。";
	bError=false;
	if(theForm.log_catid.value=="0"){
		errMessage+="\n - 请为日志选择一个分类";
		bError=true;
	}
	if(theForm.log_title.value==""){
		errMessage+="\n - 请输入标题";
		bError=true;
	}
	if(theForm.message.value==""){
		errMessage+="\n - 请输入正文的内容";
		bError=true;
	}
	if(bError){
		alert(errMessage);
		theForm.btnSubmit.disabled=false;
		return false;
	}else{
		theForm.submit();
		return true;
	}
}
