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 = "BBCode";
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("post_text");
	//postText.focus();
}

function getSel() {
	textarea = document.getElementById('post_text');
	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();
	if (selectedtext.createTextRange) {	
    	selectedtext.caretPos = document.selection.createRange().duplicate(); }
	return true;
}

function AddText(NewCode,obj) {
	//setfocus();
	var postText = obj; //document.getElementById("post_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(pos);
		postText.selectionStart = pos + NewCode.length;
		postText.selectionEnd = postText.selectionStart;
	}
	setfocus();
}

function email(obj) {
	if (helpstat) {
		helpmsg = "將E-Mail位址轉成超連結。\n\n例如: ";
		helpmsg += (mode == "html") ? "<a href=\"mailto:someone\@iseaphoto.com\">連結文字</a>" : "[email]someone\@iseaphoto.com[/email]";
		alert(helpmsg);
	} else if (basic) {
		addtxt = (mode == "html") ? "<a href=\"mailto:請以E-Mail位址覆寫\">"+text+"</a>" : "[email]"+text+"[/email]";
		AddText(addtxt,obj);
	} 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,obj);
				} 
			}
		} else {
			txt=prompt("請輸入電子郵件位址。","");      
			if (txt!=null) {
				addtxt = "[email]"+txt+"[/email]";
				AddText(addtxt,obj);
			}		
		}
	}
}

function bold(obj) {
	if (helpstat) {
		helpmsg = "將文字樣式設為粗體字。\n\n例如: ";
		helpmsg += (mode == "html") ? "<b>這是粗體字</b>" : "[b]這是粗體字[/b]";
		alert(helpmsg);
	} else if (basic) {
		addtxt = (mode == "html") ? "<b>"+text+"</b>" : "[b]"+text+"[/b]";
		AddText(addtxt,obj);
	} else {  
		txt=prompt("請輸入設為粗體字的文字。","這裡輸入你要的文字");     
		if (txt!=null) {           
			addtxt = "[b]"+txt+"[/b]";
			AddText(addtxt,obj);
		}       
	}
}

function italicize(obj) {
	if (helpstat) {
		helpmsg = "將文字樣式設為斜體字。\n\n例如: ";
		helpmsg += (mode == "html") ? "<i>這是斜體字</i>" : "[i]這是斜體字[/i]";
		alert(helpmsg);
	} else if (basic) {
		addtxt = (mode == "html") ? "<i>"+text+"</i>" : "[i]"+text+"[/i]";
		AddText(addtxt,obj);
	} else {   
		txt=prompt("請輸入設為斜體字的文字。","這裡輸入你要的文字");     
		if (txt!=null) {           
			addtxt = (mode == "html") ? "<i>"+txt+"</i>" : "[i]"+txt+"[/i]";
			AddText(addtxt,obj);
		}	        
	}
}

function center(obj) {
 	if (helpstat) {
		helpmsg = "將文字置中於文件。\n\n例如: ";
		helpmsg += (mode == "html") ? "<center>這是置中的文字</center>" : "[center]這是置中的文字[/center]";
		alert(helpmsg);
	} else if (basic) {
		addtxt = (mode == "html") ? "<center>"+text+"</center>" : "[center]"+text+"[/center]";
		AddText(addtxt,obj);
	} else {  
		txt=prompt("請輸入設為置中的文字。","這裡輸入你要的文字");     
		if (txt!=null) {          
			addtxt = (mode == "html") ? "<center>"+txt+"</center>" : "[center]"+txt+"[/center]";
			AddText(addtxt,obj);
		}	       
	}
}

function hyperlink(obj) {
	if (helpstat) {
		helpmsg = "將文字設為超連結。\n\n例如: ";
		helpmsg += (mode == "html") ? "<a href=\"http://www.iseaphoto.com\">連結到ISEAPHOTO</a>" : "[url=http://www.iseaphoto.com]連結到ISEAPHOTO[/url]";
		alert(helpmsg);
	} else if (basic) {
		addtxt = (mode == "html") ? "<a href=\"這裡輸入你要的網址\" target=\"_blank\">"+text+"</a>" : "[url=這裡輸入你要的網址]"+text+"[/url]";
		AddText(addtxt,obj);
	} else { 
		txt2=prompt("將文字設為超連結，\n若您想以URL為連結文字則不需輸入任何文字。",""); 
		if (txt2!=null) {
			txt=prompt("欲連結的URL","http://www.iseaphoto.com");      
			if (txt!=null) {
				if (txt2=="") {
					addtxt = (mode == "html") ? "<a href=\""+txt+"\" target=\"_blank\">"+txt+"</a>" : "[url=\""+txt+"\"]"+txt+"[/url]";					
				} else {
					addtxt = (mode == "html") ? "<a href=\""+txt+"\" target=\"_blank\">"+txt2+"</a>" : "[url=\""+txt+"\"]"+txt2+"[/url]";
				}
				AddText(addtxt,obj);      
			} 
		}
	}
}

function image(obj) {
	if (helpstat){
		helpmsg = "在文章裡插入一個圖片。\n\n例如: ";
		helpmsg += (mode == "html") ? "<img src=\"http://www.iseaphoto.com/image.jpg\">" : "[img]http://www.iseaphoto.com/image.jpg[/img]";
		alert(helpmsg);
	} else if (basic) {
		addtxt = (mode == "html") ? "<img src=\""+text+"\">" : "[img]"+text+"[/img]";
		AddText(addtxt,obj);
	} else {  
		txt=prompt("圖片的所在URL。","http://");    
		if(txt!=null) {            
			addtxt = (mode == "html") ? "<img src=\""+txt+"\">" : "[img]"+txt+"[/img]";
			AddText(addtxt,obj);
		}	
	}
}

function underline(obj) {
  	if (helpstat) {
		helpmsg = "將文字樣式設為顯示底線。\n\n例如: ";
		helpmsg += (mode == "html") ? "<u>這是有底線的文字</u>" : "[u]這是有底線的文字[/u]";
		alert(helpmsg);
	} else if (basic) {
		addtxt = (mode == "html") ? "<u>"+text+"</u>" : "[u]"+text+"[/u]";
		AddText(addtxt,obj);
	} else {  
		txt=prompt("請輸入設為底線的文字。","這裡輸入你要的文字");     
		if (txt!=null) {           
			addtxt = (mode == "html") ? "<u>"+txt+"</u>" : "[u]"+txt+"[/u]";
			AddText(addtxt,obj);
		}	        
	}
}

function iframe(obj) {
	if (helpstat) {
		helpmsg = "顯示網頁於內建框架(IFRAME)。\n\n例如: ";
		helpmsg += (mode == "html") ? "<iframe src=\"http://www.iseaphoto.com/\"></iframe>" : "[iframe]http://www.iseaphoto.com/[/iframe]";
		alert(helpmsg);
	} else if (basic) {
		addtxt = (mode == "html") ? "<iframe src=\""+text+"\" width=\"100%\" height=\"480\"></iframe>" : "[iframe]"+text+"[/iframe]";
		AddText(addtxt,obj);
	} else {
		txt=prompt("請輸入要顯示的網址。","http://");      
		if (txt!=null) {
			addtxt = (mode == "html") ? "<iframe src=\""+txt+"\" width=\"100%\" height=\"480\"></iframe>" : "[iframe]"+txt+"[/iframe]";
			AddText(addtxt,obj);
			}         
		} 
}

function embedmm() {
	if (helpstat) {
		helpmsg = "在網頁內崁入Flash、MPEG、MIDI等各種影音多媒體檔案。\n\n例如: ";
		helpmsg += (mode == "html") ? "<embed src=\"http://www.iseaphoto.com/movie.mpg\"></embed>" : "[embed]http://www.iseaphoto.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) {
	addtxt = (mode == "html") ? "<font color=\""+color+"\">"+text+"</font>" : "[color="+color+"]"+text+"[/color]";
	AddText(addtxt);
	document.post_form.color.value = "";
}

function MM_openBrWindow(theURL,winName,features) {
	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 OpenPreview() {
	document.post_form.target = "_blank";
	document.post_form.action = "preview.php";
	document.post_form.submit();
}

function OpenUpload(aid) {
	window.open('/isea/member/AtUpload.aspx?Cid=' + aid, 'Attatch_Upload', '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');
}
