﻿ 
var tag = Get_Cookie('tagmode');
if (tag == 0) {
	var stprompt = false;
	var basic = true;	
} else {
	var stprompt = true;
	var basic = false;
}
var helpstat = false;
var mode = "html";
var isIE = (!document.all && document.getElementById) ? 0 : 1;
var text;


function Get_Cookie(name) {
	var start = document.cookie.indexOf(name + "=");
	var len = start + name.length + 1;
	if ((!start) && (name != document.cookie.substring(0, name.length))) {
		return null;
	}
	if (start == -1) return null;
	var end = document.cookie.indexOf(";", len);
	if (end == -1) end = document.cookie.length;
	return unescape(document.cookie.substring(len, end));
}

function changetagmode(tagmode) {
	mode = tagmode;
}

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() {
	var postText = document.getElementById("acontent");
	postText.focus();
}

function getSel() {
	textarea = document.getElementById('acontent');
	selection = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd - textarea.selectionStart);
	return selection;
}

function getActiveText(selectedtext) { 
	//text = (isIE) ? document.selection.createRange().text : window.getSelection();
	
	text = (isIE) ? document.selection.createRange().text : getSel();
	//alert ('qq:'+text);
	if (selectedtext.createTextRange) {	
    	selectedtext.caretPos = document.selection.createRange().duplicate(); }
	return true;
}

function AddText(NewCode) {
	//setfocus();
	
	//text = (isIE) ? document.selection.createRange().text : getSel();
	
	
	var postText = document.getElementById("acontent");
	if(isIE) {
		if (postText.createTextRange && postText.caretPos) {
			var caretPos = postText.caretPos;
			//alert (postText.createTextRange +'2:'+ postText.caretPos);
			//caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? NewCode + ' ' : NewCode;
			caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? NewCode + ' ' : NewCode;
			//alert (NewCode);
		} else {
			//var caretPos = postText.caretPos;
			//caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? NewCode + ' ' : NewCode;
			postText.value+=NewCode; 
			//alert('ccc');
		}
	} else if (typeof(postText.selectionStart) == "undefined") { 
		postText.value+=NewCode;
	} else {
		pos = postText.selectionStart;
		postText.value = postText.value.substr(0, pos) + NewCode + postText.value.substr(pos);
		postText.selectionStart = pos + NewCode.length;
		postText.selectionEnd = postText.selectionStart;
	}
	setfocus();
}

function email() {
	if (helpstat) {
		helpmsg = "將E-Mail位址轉成超連結。\n\n例如: ";
		helpmsg += (mode == "html") ? "<a href=\"mailto:someone\@mobile01.com\">連結文字</a>" : "[email]someone\@mobile01.com[/email]";
		alert(helpmsg);
	} else if (basic) {
		addtxt = (mode == "html") ? "<a href=\"mailto:請以E-Mail位址覆寫\">"+text+"</a>" : "[email]"+text+"[/email]";
		AddText(addtxt);
	} else {
		if (mode == "html") {
			txt2=prompt("請輸入連結文字，若您想以E-Mail位址為連結文字則不需輸入任何文字。",""); 
			if (txt2!=null) {
				txt=prompt("請輸入電子郵件位址。","");      
				if (txt!=null) {
					if (txt2=="") {
						addtxt = "<a href=\"mailto:"+txt+"\">"+txt+"</a>";
					} else {
						addtxt = "<a href=\"mailto:"+txt+"\">"+txt2+"</a>";
					}
					AddText(addtxt);
				} 
			}
		} else {
			txt=prompt("請輸入電子郵件位址。","");      
			if (txt!=null) {
				addtxt = "[email]"+txt+"[/email]";
				AddText(addtxt);
			}		
		}
	}
}

function bold() {
		txt=prompt("請輸入設為粗體字的文字。","這裡輸入你要的文字");     
		if (txt!=null) {           
			addtxt = "[b]"+txt+"[/b]";
			AddText(addtxt);

	}
}

function italicize() {
	
		txt=prompt("請輸入設為斜體字的文字。","這裡輸入你要的文字");     
		if (txt!=null) {           
			addtxt = "[i]"+txt+"[/i]";
			AddText(addtxt);
		}	        
	
}

function center() { 
		txt=prompt("請輸入設為置中的文字。","這裡輸入你要的文字");     
		if (txt!=null) {          
			addtxt = "[center]"+txt+"[/center]";
			AddText(addtxt);		
	}
}

function hyperlink() {
		txt2=prompt("將文字設為超連結，\n若您想以URL為連結文字則不需輸入任何文字。",""); 
		if (txt2!=null) {
			txt=prompt("欲連結的URL","http://www.bingo.org.tw");      
			if (txt!=null) {
				if (txt2=="") {
					addtxt =  "[url="+txt+"]"+txt+"[/url]";					
				} else {
					addtxt =  "[url="+txt+"]"+txt2+"[/url]";
				}
				AddText(addtxt);      
			} 
	}
}

function image() {
		txt=prompt("圖片的所在URL。","http://");    
		if(txt!=null) {            
			addtxt =  "[img]"+txt+"[/img]";
			AddText(addtxt);
		}	
}

function underline() {
 
		txt=prompt("請輸入設為底線的文字。","這裡輸入你要的文字");     
		if (txt!=null) {           
			addtxt = "[u]"+txt+"[/u]";
			AddText(addtxt);
		}	        
}

function iframe() {
	if (helpstat) {
		helpmsg = "顯示網頁於內建框架(IFRAME)。\n\n例如: ";
		helpmsg += (mode == "html") ? "<iframe src=\"http://www.mobile01.com/index.php\"></iframe>" : "[iframe]http://www.mobile01.com/index.php[/iframe]";
		alert(helpmsg);
	} else if (basic) {
		addtxt = (mode == "html") ? "<iframe src=\""+text+"\" width=\"100%\" height=\"480\"></iframe>" : "[iframe]"+text+"[/iframe]";
		AddText(addtxt);
	} else {
		txt=prompt("請輸入要顯示的網址。","http://");      
		if (txt!=null) {
			addtxt = (mode == "html") ? "<iframe src=\""+txt+"\" width=\"100%\" height=\"480\"></iframe>" : "[iframe]"+txt+"[/iframe]";
			AddText(addtxt);
			}         
		} 
}

function embedmm() {
	if (helpstat) {
		helpmsg = "在網頁內崁入Flash、MPEG、MIDI等各種影音多媒體檔案。\n\n例如: ";
		helpmsg += (mode == "html") ? "<embed src=\"http://www.mobile01.com/movie.mpg\"></embed>" : "[embed]http://www.mobile01.com/movie.mpg[/embed]";
		alert(helpmsg);
	} else if (basic) {
		addtxt = (mode == "html") ? "<embed src=\""+text+"\"></embed>" : "[embed]"+text+"[/embed]";
		AddText(addtxt);
	} else {
		txt=prompt("請輸入多媒體檔案的網址。","http://");      
		if (txt!=null) {
			addtxt = (mode == "html") ? "<embed src=\""+txt+"\"></embed>" : "[embed]"+txt+"[/embed]";
			AddText(addtxt);
			}         
		} 
}

function showcolor(color) {
	//if (text == ''){
	//	text = (isIE) ? document.selection.createRange().text : getSel();
	//	alert ('cc'+text);
	//}/	
	//alert ('showa:'+typeof(text));
	//alert ('cc:'+typeof(text));
	setfocus();
	if (typeof(text) == "undefined")
	text = "";
	if(text){
	//alert ('cc:'+typeof(text))
	//alert ('assssa:'+text);
//	if (typeof(text) == 'undefined'){
//		text = '';
//	}
		addtxt =  "[color="+color+"]"+text+"[/color]";
		AddText(addtxt);
		document.post_form.color.value = "";
	}else{		
//		getActiveText(document.post_form.acontent);				
		//showcolor(color);
		addtxt =  "[color="+color+"]"+text+"[/color]";
		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) ? 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 != "") {
		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]";
		document.post_form.post_text.focus();
    } else {
		newtext = newtext.substr(0, 10) + "...(恕刪)";
		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]";
		document.post_form.post_text.focus();
    }
}





function OpenUpload(forum) {
	window.open('remoteupload.php?forum=' + forum, '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');
}

