var stprompt = true;
var basic = false;
var helpstat = false;
var isIE = (!document.all && document.getElementById) ? 0 : 1;
var mode = 'html';

function thelp(swtch){
	if (swtch == 1){
		basic = false;
		stprompt = false;
		helpstat = true;
	} else if (swtch == 0) {
		helpstat = false;
		stprompt = false;
		basic = true;
	} else if (swtch == 2) {
		helpstat = false;
		basic = false;
		stprompt = true;
	}
}

function setfocus() {
  document.post_form.post_text.focus();
}

function getActiveText(selectedtext) { 
	text = (document.all) ? document.selection.createRange().text : document.getSelection();
	if (selectedtext.createTextRange) {	
    	selectedtext.caretPos = document.selection.createRange().duplicate(); }
	return true;
}

function AddText(NewCode) {
	setfocus();
	var postText = document.getElementById("post_text");
	/*
	if(window.getSelection) {//chrome safari  FF opera
		alert('1'+window.getSelection().toString());} 
	else if(document.getSelection){
		alert('2'+document.getSelection());}
	else if(window.document.selection){//IE
		alert('3'+window.document.selection.createRange().text);} 
	/**/
	if(isIE) {
		if (postText.createTextRange && postText.caretPos) {
			var caretPos = postText.caretPos;
			caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? NewCode + ' ' : NewCode;
		} else {
			postText.value+=NewCode; 
		}
	} else if (typeof(postText.selectionStart) == "undefined") { // safari fix
		postText.value+=NewCode;
	} else {
		pos = postText.selectionStart;
		postText.value = postText.value.substr(0, pos) + NewCode + postText.value.substr(postText.selectionEnd);
		postText.selectionStart = pos + NewCode.length;
		postText.selectionEnd = postText.selectionStart;
		//alert('postText.value.substr(0, pos)'+postText.value.substr(0, pos)+' NewCode'+ NewCode+ ' postText.value.substr(pos)'+postText.value.substr(pos));
	}
	setfocus();
}

function AddTextInRTE(NewCode) {
		Editor = YAHOO.widget.EditorInfo.getEditorById('post_text');
		Editor.execCommand('inserthtml', NewCode );
		Editor._focusWindow();
}

function email() {
	if (helpstat) {
		alert("將E-Mail位址轉成超連結。\n\n例如: <a href=\"mailto:someone\@anywhere.com\">連結文字</a>");
		}
	else if (basic) {
		AddTxt="<a href=\"mailto:"+txt+"\">"+txt+"</a>";
		AddText(AddTxt);
		}
	else { 
		txt2=prompt("請輸入連結文字，若您想以E-Mail位址為連結文字則不需輸入任何文字。",""); 
		if (txt2!=null) {
			txt=prompt("請輸入電子郵件位址。","");      
			if (txt!=null) {
				if (txt2=="") {
					AddTxt="<a href=\"mailto:"+txt+"\">"+txt+"</a>";
					AddText(AddTxt);
				} else {
					AddTxt="<a href=\"mailto:"+txt+"\">"+txt2+"</a>";
					AddText(AddTxt);
				}         
			} 
		}
	}
}

function bold() {
	setfocus();
	if (helpstat) {
		alert("將文字樣式設為粗體字。\n\n例如: <b>這是粗體字</b>");
	//} else if (basic) {
		//AddTxt="<b>"+txt+"</b>";
		//AddText(AddTxt);
	} else {  
		//txt=prompt("請輸入設為粗體字的文字。","Input text here!");     
		//if (txt!=null) { 
		if(window.getSelection) {//chrome safari FF opera
			//window.getSelection() // chrome safari are ok, but FF opera are not.
			var postText = document.getElementById("post_text");
			pos = postText.selectionStart;
			poe = postText.selectionEnd-postText.selectionStart;
			t = postText.value.substr(pos, poe);
			AddTxt="<b>"+t+"</b>";
		}
		else{
			AddTxt="<b>"+text+"</b>";
		} 
			AddText(AddTxt);
		//}       
	}
}

function italicize() {
	setfocus();
	if (helpstat) {
		alert("將文字樣式設為斜體字。\n\n例如: <i>這是斜體字</i>");
	//} else if (basic) {
		//AddTxt="<i>"+txt+"</i>";
		//AddText(AddTxt);
	} else {   
		//txt=prompt("請輸入設為斜體字的文字。","Input text here!");     
		//if (txt!=null) { 
		if(window.getSelection) {//chrome safari FF opera
			//window.getSelection() // chrome safari are ok, but FF opera are not.
			var postText = document.getElementById("post_text");
			pos = postText.selectionStart;
			poe = postText.selectionEnd-postText.selectionStart;
			t = postText.value.substr(pos, poe);
			AddTxt="<i>"+t+"</i>";
		}
		else{
			AddTxt="<i>"+text+"</i>";
		} 
			
			AddText(AddTxt);
		//}	        
	}
}

function center() {
	setfocus();
 	if (helpstat) {
		alert("將文字置中於文件。\n\n例如: <center>這是置中的文字</center>");
	//} else if (basic) {
		//AddTxt="<center>"+txt+"</center>";
		//AddText(AddTxt);
	} else {  
		//txt=prompt("請輸入設為置中的文字。","Input text here!");     
		//if (txt!=null) {
		if(window.getSelection) {//chrome safari FF opera
			//window.getSelection() // chrome safari are ok, but FF opera are not.
			var postText = document.getElementById("post_text");
			pos = postText.selectionStart;
			poe = postText.selectionEnd-postText.selectionStart;
			t = postText.value.substr(pos, poe);
			AddTxt="<center>"+t+"</center>";
		}
		else{
			AddTxt="<center>"+text+"</center>";
		} 
			AddText(AddTxt);
		//}	       
	}
}

function hyperlink() {
	if (helpstat) {
		alert("將文字設為超連結。\n\n例如: <a href=\"http://www.mydoman.com\">連結到mydoman網站</a>");
	} else if (basic) {
		AddTxt="<a href=\""+txt+"\" target=\"_blank\">"+txt+"</a>";
		AddText(AddTxt);
	} else { 
		txt2=prompt("將文字設為超連結，\n若您想以URL為連結文字則不需輸入任何文字。",""); 
		if (txt2!=null) {
			txt=prompt("欲連結的URL","http://");      
			if (txt!=null) {
				if (txt2=="") {
					AddTxt="<a href=\""+txt+"\" target=\"_blank\">"+txt+"</a>";
					AddText(AddTxt);
				} else {
					AddTxt="<a href=\""+txt+"\" target=\"_blank\">"+txt2+"</a>";
					AddText(AddTxt);
				}         
			} 
		}
	}
}

function image() {
	if (helpstat){
		alert("在文章裡插入一個圖片。\n\n例如: <img src=\"http://www.mydoman.com/image.jpg\">");
	} else if (basic) {
		AddTxt="<img src=\""+txt+"\">";
		AddText(AddTxt);
	} else {  
		txt=prompt("圖片的所在URL。","http://");    
		if(txt!=null) {            
			AddTxt="<img src=\""+txt+"\">";
			AddText(AddTxt);
		}	
	}
}

function underline() {
	setfocus();
  	if (helpstat) {
		alert("將文字樣式設為顯示底線。\n\n例如: <u>這是有底線的文字</u>");
	//} else if (basic) {
		//AddTxt="<u>"+txt+"</u>";
		//AddText(AddTxt);
	} else {  
		//txt=prompt("請輸入設為底線的文字。","Input text here!");     
		//if (txt!=null) {
		if(window.getSelection) {//chrome safari FF opera
			//window.getSelection() // chrome safari are ok, but FF opera are not.
			var postText = document.getElementById("post_text");
			pos = postText.selectionStart;
			poe = postText.selectionEnd-postText.selectionStart;
			t = postText.value.substr(pos, poe);
			AddTxt="<u>"+t+"</u>";
		}
		else{
			AddTxt="<u>"+text+"</u>";
		} 
			AddText(AddTxt);
		//}	        
	}
}

function iframe() {
	if (helpstat) {
		alert("顯示網頁於內建框架(IFRAME)。\n\n例如: <iframe src=\"http://www.mydoman.com/default.asp\"></iframe>");
		}
	else if (basic) {
		AddTxt="<iframe src=\""+txt+"\" width=\"100%\" height=\"480\"></iframe><a href=\""+txt+"\" target=\"_blank\">按這裡檢視網頁</a>";
		AddText(AddTxt);
		}
	else {
		txt=prompt("請輸入要顯示的URL。","http://");      
		if (txt!=null) {
			AddTxt="<iframe src=\""+txt+"\" width=\"100%\" height=\"480\"></iframe><a href=\""+txt+"\" target=\"_blank\">按這裡檢視網頁</a>";
			AddText(AddTxt);
			}         
		} 
}

function embedmm() {
	if (helpstat) {
		alert("在網頁內崁入Flash、MPEG、MIDI等各種影音多媒體檔案。\n\n例如: <embed src=\"http://www.mydoman.com/movie.mpeg\"></embed>");
		}
	else if (basic) {
		AddTxt="<embed src=\""+txt+"\"></embed>";
		AddText(AddTxt);
		}
	else {
		txt=prompt("請輸入多媒體檔案URL。","http://");      
		if (txt!=null) {
			AddTxt="<embed src=\""+txt+"\"></embed>";
			AddText(AddTxt);
			}         
		} 
}

function showcolor(color) {
	setfocus();
	if(window.getSelection) {//chrome safari FF opera
		//window.getSelection() // chrome safari are ok, but FF opera are not.
		var postText = document.getElementById("post_text");
		pos = postText.selectionStart;
		poe = postText.selectionEnd-postText.selectionStart;
		t = postText.value.substr(pos, poe);
		AddTxt="<font color=\""+color+"\">"+t+"</font>";
	}
	else{
		AddTxt="<font color=\""+color+"\">"+text+"</font>";
	} 
	AddText(AddTxt);
	document.post_form.color.value = "";
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function ReplyWithQuote(retext,sayername) {
  var oldtext = document.post_form.post_text.value;
  var newtext = retext.innerHTML.replace(/\+0/g," 2");
  var selectedText;
  newtext = (isIE) ? trim(newtext.replace(/<br \/>/gi,"\n")) : trim(newtext.replace(/<br \/>/gi, ""));
  newtext = (isIE) ? trim(newtext.replace(/<br>/gi,"\n")) : trim(newtext.replace(/<br>/gi, ""));

  if (navigator.userAgent.indexOf('Safari') >= 0) {
    selectedText = getSelection();
  } else {
      selectedText = (document.selection) ? document.selection.createRange().text : document.getSelection();
  }

  if (selectedText != "") {
      if (oldtext != '') {
          document.post_form.post_text.value = (mode == "html") ? oldtext+"\n<blockquote>"+sayername.innerHTML+" wrote:\n" +selectedText+"</blockquote>" : oldtext+"\n[quote]"+sayername.innerHTML+" wrote:\n" +selectedText+"[/quote]";
      } else {
          document.post_form.post_text.value = (mode == "html") ? "<blockquote>"+sayername.innerHTML+" wrote:\n" +selectedText+"</blockquote>" : "[quote]"+sayername.innerHTML+" wrote:\n" +selectedText+"[/quote]";
      }
      document.post_form.post_text.focus();
  } else {
      newtext = newtext.substr(0, 20) + "...(恕刪)";
      if (oldtext != '') {
          document.post_form.post_text.value = (mode == "html") ? oldtext+"\n<blockquote>"+sayername.innerHTML+" wrote:\n"+newtext +"</blockquote>" : oldtext+"\n[quote]"+sayername.innerHTML+" wrote:\n"+newtext +"[/quote]";
      } else {
          document.post_form.post_text.value = (mode == "html") ? "<blockquote>"+sayername.innerHTML+" wrote:\n"+newtext +"</blockquote>" : "[quote]"+sayername.innerHTML+" wrote:\n"+newtext +"[/quote]";
      }
      document.post_form.post_text.focus();
  }
}

function ReplyWithQuoteInRTE(retext,sayername) {
  Editor = YAHOO.widget.EditorInfo.getEditorById('post_text');
  var oldtext = document.post_form.post_text.value;
  var newtext = retext.innerHTML.replace(/\+0/g," 2");
  var selectedText;
  var post_value;
  newtext = (isIE) ? newtext.replace(/<br \/>/gi,"\n") : newtext.replace(/<br \/>/gi, "");
  newtext = (isIE) ? newtext.replace(/<br>/gi,"\n") : newtext.replace(/<br>/gi, "");

  if (navigator.userAgent.indexOf('Safari') >= 0) {
    selectedText = getSelection();
  } else {
      selectedText = (document.selection) ? document.selection.createRange().text : document.getSelection();
  }

  if (selectedText != "") {
      if (oldtext != '') {
          post_value = (mode == "html") ? oldtext+"\n[blockquote]"+sayername.innerHTML+" wrote:\n" +selectedText+"[/blockquote]" : oldtext+"\n[quote]"+sayername.innerHTML+" wrote:\n" +selectedText+"[/quote]";
      } else {
          post_value = (mode == "html") ? "[blockquote]"+sayername.innerHTML+" wrote:\n" +selectedText+"[/blockquote]" : "[quote]"+sayername.innerHTML+" wrote:\n" +selectedText+"[/quote]";
      }
      document.post_form.post_text.focus();
  } else {
      newtext = newtext.substr(0, 20) + "...(恕刪)";
      if (oldtext != '') {
          post_value = (mode == "html") ? oldtext+"\n[blockquote]"+sayername.innerHTML+" wrote:\n"+newtext +"[/blockquote]" : oldtext+"\n[quote]"+sayername.innerHTML+" wrote:\n"+newtext +"[/quote]";
      } else {
          post_value = (mode == "html") ? "[blockquote]"+sayername.innerHTML+" wrote:\n"+newtext +"[/blockquote]" : "[quote]"+sayername.innerHTML+" wrote:\n"+newtext +"[/quote]";
      }
      Editor.execCommand('inserthtml', post_value);
      Editor._focusWindow();
  }
}

function OpenPreview() {
	document.post_form.target = "_blank";
	document.post_form.action = "preview.php";
	document.post_form.submit();
}

function OpenUpload() {
	window.open('remoteupload.php', 'upload_page', 'resizable=yes,status=yes,scrollbars=yes,width=640,height=480');
}

function OpenZipUpload() {
	window.open('remotezipupload.php', 'upload_page', 'resizable=yes,status=yes,scrollbars=yes,width=640,height=480');
}

function OpenNewZipUpload() {
	window.open('remotenewzipupload.php', 'upload_page', 'resizable=yes,status=yes,scrollbars=yes,width=640,height=480');
}

function OpenUploadFile() {
	window.open('remoteuploadfile.php', 'upload_page', 'resizable=yes,status=yes,scrollbars=yes,width=640,height=200');
}

function showBlacklist(post_id) {
	var str_post_id = new String(post_id);
	if (document.getElementById(str_post_id).style.display == "none") {
		document.getElementById(str_post_id).style.display = "";
	} else {
		document.getElementById(str_post_id).style.display = "none";
	}
}

function MM_displayStatusMsg(msgStr) { //v1.0
  status=msgStr;
  document.MM_returnValue = true;
}

function trim(stringToTrim)
{
 return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function SwitchForm() {
  if (document.post_form.post_delete.checked == true) {
    document.post_form.post_title.disabled = true;
    document.post_form.post_text.disabled = true;
  } else {
    document.post_form.post_title.disabled = false;
    document.post_form.post_text.disabled = false;
  }
}

