var userAgent = navigator.userAgent.toLowerCase();
var is_opera = userAgent.indexOf('opera') != -1 && opera.version();
var is_moz = (navigator.product == 'Gecko') && userAgent.substr(userAgent.indexOf('firefox') + 8, 3);
var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);
var allowbbcode = true;
var forumallowhtml = false;
var allowhtml = false;
var allowsmilies = false;
var allowimgcode = true;

var isguest = true;
if(nickname != ""&& nickname != "游客" && nickname !='undefined')
{
	isguest = false;
}
function insertNodeAtSelection(text) 
{
	var sel = editor.getSelection();
	var range = sel ? sel.getRangeAt(0) : editdoc.createRange();
	sel.removeAllRanges();
	range.deleteContents();
	var node = range.startContainer;
	var pos = range.startOffset;
	switch(node.nodeType) 
	{
		case Node.ELEMENT_NODE:
			if(text.nodeType == Node.DOCUMENT_FRAGMENT_NODE) 
			{
				selNode = text.firstChild;
			} 
			else 
			{
				selNode = text;
			}
			node.insertBefore(text, node.childNodes[pos]);
			add_range(selNode);
			break;

		case Node.TEXT_NODE:
			if(text.nodeType == Node.TEXT_NODE) 
			{
				var text_length = pos + text.length;
				node.insertData(pos, text.data);
				range = editdoc.createRange();
				range.setEnd(node, text_length);
				range.setStart(node, text_length);
				sel.addRange(range);
			} 
			else 
			{
				node = node.splitText(pos);
				var selNode;
				if(text.nodeType == Node.DOCUMENT_FRAGMENT_NODE) 
				{
					selNode = text.firstChild;
				} else 
				{
					selNode = text;
				}
				node.parentNode.insertBefore(text, node);
				add_range(selNode);
			}
			break;
	}
}
function add_range(node) 
{
	var sel = editor.getSelection();
	var range = editor.document.createRange();
	range.selectNodeContents(node);
	sel.removeAllRanges();
	sel.addRange(range);
}
function insertText(selector,val)
{
	var tmpVal,theObj;
	editor.focus();
	theObj = val;
	
	
	if(is_ie)
	{
		/* 
			selector.execCommand('InsertImage',false,tmpVal);
			//var reg=new RegExp('<IMG>','ig');
			editor.document.body.innerHTML=editor.document.body.innerHTML.replace('<IMG>',theObj);
			selector.moveStart('character',editor.document.body.innerHTML.length);
	 		selector.moveEnd('character',0);
	 		selector.select();
 		*/
 		selector.pasteHTML(val);
 		selector.select();

	}
	else
	{
		
		var fragment = editor.document.createDocumentFragment();
		var holder = editor.document.createElement('span');
		holder.innerHTML = val;

		while(holder.firstChild) 
		{
			fragment.appendChild(holder.firstChild);
		}
		insertNodeAtSelection(fragment);
	}	
}
function moveSelecter(selector,movestart,length)
{	
	if(is_ie)
	{
		//alert(selector.htmlText);
		var moveend = editor.document.body.innerHTML.length-length;
		//alert(movestart);
		//alert(moveend);
		selector.moveStart('character',movestart);
 		selector.moveEnd('character',0);
 		selector.select();
	}
	
}
function setmediatype(url) 
{
	
	var ext = url.lastIndexOf('.') == -1 ? '' : url.substr(url.lastIndexOf('.') + 1, url.length).toLowerCase();
	
	if(ext == 'rmvb')
	{
		ext = 'rm';
	}
	if(ext =='rm' || ext == 'wmv' || ext =='mp3' || ext =='mov' || ext =='ra' || ext=='wma' || ext=='mpg')
		return ext;
	else
		return undefined;
}
function readNodes(root, toptag) 
{
	var html = "";
	var moz_check = /_moz/i;

	switch(root.nodeType) {
		case Node.ELEMENT_NODE:
		case Node.DOCUMENT_FRAGMENT_NODE:
			var closed;
			if(toptag) {
				closed = !root.hasChildNodes();
				html = '<' + root.tagName.toLowerCase();
				var attr = root.attributes;
				for(var i = 0; i < attr.length; ++i) {
					var a = attr.item(i);
					if(!a.specified || a.name.match(moz_check) || a.value.match(moz_check)) {
						continue;
					}
					html += " " + a.name.toLowerCase() + '="' + a.value + '"';
				}
				html += closed ? " />" : ">";
			}
			for(var i = root.firstChild; i; i = i.nextSibling) {
				html += readNodes(i, true);
			}
			if(toptag && !closed) {
				html += "</" + root.tagName.toLowerCase() + ">";
			}
			break;

		case Node.TEXT_NODE:
			html = htmlspecialchars(root.data);
			break;
	}
	return html;
}





var hidepop = 0;
var selector = new Object();  
var editor = new Object();
var clickname = "";
var selectText = "";
var submitkeydown = 0;
$(document).ready(function()
{

	editor=document.getElementById('f_content').contentWindow;
	editor.document.designMode='on';
	editor.document.contentEditable=true;
	editor.document.open();
	editor.document.writeln('<html><head><link rel="stylesheet" type="text/css" href="'+pathroot+'css/accnew.css"><style type="text/css"><!-- .postcontent {height:auto!important;min-height: 20em;height: 20em;font-size:14px;line-height:1.5em;}.postcontent .content_table{border: 1px solid #CCCCCC;empty-cells: show;border-collapse: collapse;}.postcontent .content_table td {padding: 4px;font-size:14px;border: 1px solid #CCCCCC; overflow: hidden;}.postcontent p {margin:0 0 10px 0;color:#444444}.postcontent font {line-height:1.5em !important;} .postcontent a {color:blue;text-decoration: none;} .quote, .blockcode{margin:10px 0;padding:10px 10px 10px 65px;}.quote{background:#F9F9F9 url(../images/discuz6/icon_quote_s.gif) no-repeat scroll 20px 6px;padding-bottom:5px;}.quote blockquote{background:transparent url(../images/discuz6/icon_quote_e.gif) no-repeat scroll 100% 100%;line-height:1.6em;margin:0;padding:0 65px 5px 0;} .postcontentform{background-repeat: no-repeat; background-position: 50% 50%;background-attachment: fixed;} //--></style></head><body class="postcontent postcontentform" ></body></html>');
	editor.document.close();
 	
 	// Ctrl + Enter 发贴
 	  
 	
 	$(editor.document).keydown(function(e)
 	{
 		if(e.ctrlKey && e.keyCode == 13)
 		{
 			
 			if(parseInt($("#uploading").val()) == 0)
 				
 				if($("#submitpostbtn").attr('disabled') != true)
 				{
 					$("#submitpostbtn").click();
 					submitkeydown += 1;
 				}
 		}
 		
 	});
 	 
 	
 	
 	var tophref = top.document.location.toString();
 	
 	if(from == 'new')
 		editor.focus();
 	
 	if(typeof(contT)!='undefined' && contT!='' && contT!='false')
 	{
    	editor.document.body.innerHTML=document.mainform.content.value=contT;
  	}
 	
 	$(".toolbar a").click(function()
 	{
 		
 		
 		if(is_ie)
 		{
	 		editor.focus();
	 		selector = editor.document.selection.createRange();
	 		selectText = selector.htmlText;
	 		
 		}
 		else
 		{
 			selection = editor.getSelection();
 			range = selection ? selection.getRangeAt(0) : editdoc.createRange();
 			selectText =readNodes(range.cloneContents(),false);
 			
 		}
 		var pop_menu_name = "#pop_"+this.name.toLowerCase();
 		var actdivname = "pop_"+this.name.toLowerCase();
 		var thetop = $(this).offset().top+20;
 		var theleft = $(this).offset().left;
 		thetop+="px";
 		theleft+="px";
 		
 		if(pop_menu_name == "#pop_createlink")
 		{
 			
 			$("#createlinkval").keydown(function(e)
 			{
 				if(e.keyCode == 13)
 				 return false;
 			})
 			if(is_ie)
 			{
 				var rng = selector;
 				var elm = selector.item?selector.item(0):selector.parentElement();
 				var p = elm ;
 				while(p.tagName.toLowerCase()!='a'&&p.tagName.toLowerCase()!='body')
 				{
					p=p.parentNode;
				}
				
				if(p.tagName.toLowerCase()=='a' && selectText != "")
				{
					$("#createlinkval").val(p);
				}
				else
				{
					$("#createlinkval").val("http://");
					
				}
 			}
 			else
 			{
 				var sel = selection;
				var rng=null;
				if(sel.rangeCount>0)
				{
					rng=sel.getRangeAt(0);
				}
				else{
					return;
				}
				
				var elm=rng.commonAncestorContainer;
				
				if(!rng.collapsed)
				{
					if(rng.startContainer==rng.endContainer)
					{
						if(rng.startOffset-rng.endOffset<2)
						{
							if(rng.startContainer.hasChildNodes())
								elm=rng.startContainer.childNodes[rng.startOffset];
						}
					}
				}
				var p=elm;
				while(p.nodeName.toLowerCase()!='a'&&p.nodeName.toLowerCase()!='body')
				{
					p=p.parentNode;
				}
				if(p.nodeName.toLowerCase()=='a' && selectText != "")
				{
					//alert(p);
					$("#createlinkval").val(p);
					//this.insertAnything.showLink(true,p);
				}else
				{
					$("#createlinkval").val("http://");
				}
 				
 			
 			}
 		}

 		if($(pop_menu_name).is("div")) // && pop_menu_name != "#pop_createlink"
 		{
 			
 			$("#"+optdivname).hide();
 			
 			$(pop_menu_name).show();
 			optdivname = actdivname;
 			clearInterval(hideintval);
			hideintval = setInterval("hideadminopt()",500);
			
			$(pop_menu_name).css({left:theleft});
			
	 		$(pop_menu_name).mouseover(function()
	 		{
	 			//if(pop_menu_name != "#pop_createlink"&&pop_menu_name != "#pop_insertmedia")
	 			//	$(pop_menu_name).show();
	 			onoptlist = 1;	
	 		})
	 		
			$(pop_menu_name).mousemove(function(e)
			{
				//$(pop_menu_name).show();
			})
			$(pop_menu_name).mouseout(function()
	 		{
	 			if(pop_menu_name != "#pop_createlink"&&pop_menu_name != "#pop_insertmedia"&&pop_menu_name != "#pop_inserttable")
	 				//$(pop_menu_name).hide();	
	 				onoptlist = 0;
	 		})
 		}
 		else
 		{
 			editor.document.execCommand(this.name,false,'');
 		}
 		clickname = this.name;
 	
 	})
 	
 	//Fontsize	
 	$("#pop_fontsize li").mouseover(function()
 	{
 		//$(this).css("color","#CCCCCC");
 	})
 	$("#pop_fontsize li").mouseout(function()
 	{
 		//$(this).css("color","#000000");
 	})
 	$("#pop_fontsize ul li").click(function()
 	{
 		//alert(this.id);
 		editor.document.execCommand('Fontsize',false,this.id);
 		$("#pop_fontsize").hide();
 	})
 	// font name
 	$("#pop_fontname ul li").click(function()
 	{
 		//alert(this.id);
 		//return false;
 		editor.document.execCommand('Fontname',false,this.id);
 		
 		$("#pop_fontname").hide();
 	})
 	//	TextAlign
 	$("#pop_textalign ul li").click(function()
 	{	
 		
 		editor.document.execCommand(this.id,false,'');
 		return "";	
 		$("#pop_textalign").hide();	
 	})
 	
 	//FontColor
 	$("#pop_fontcolor ul li").click(function()
 	{		
 		
 		editor.document.execCommand('ForeColor',false,this.id);	
 		$("#pop_fontcolor").hide();
 	})
 	
 	//Createlink
 	
 	$("#createlinkval").keydown(function(e)
 	{
 		if(e.keyCode == '13')
 		{
 			$("#pop_createlink #submit").click();
 		}
 	})
 	$("#pop_createlink button").click(function()
 	{
 		
 		var url = encodeURI($("#pop_createlink input").val());	
 		
 		if(this.id =='submit')
 		{
 			
 			if(url != "")
 			{
 				if(url.indexOf('http:') === -1 && url.indexOf("ftp") === -1 && url.indexOf("mms") ===-1 && url.indexOf("rstp") === -1 && url.indexOf("thunder")===-1)
 				{
 					$("#pop_createlink li span").html("<font color='red'>请输入有效链接</font>");
 					return false;
 				}
 				//alert(selectText);
 				if(selectText == undefined || selectText == "" || selectText.indexOf("<P>&nbsp;</P>") != -1)
 				{
	 				
	 				var linktext = "<a href='"+url+"' >"+$("#pop_createlink input").val()+"</a>";
	 				insertText(selector,linktext);
	 			}
	 			else
	 			{
	 				if(!is_ie)
	 				{
	  					
	  					editor.document.execCommand('CreateLink',false,url);
	  					
	 				}
	 				else
	 				{	
	 					
	 					selector.execCommand('CreateLink',false,url);						
	 				}
 				}
 				$("#pop_createlink input").val('');	
 				$("#pop_createlink").hide();
 			}
 			else
 			{
 				$("#pop_createlink").hide();
 			}
 		}
 		else
 			$("#pop_createlink").hide();
  	})
  	
  	//插入图片
  	
  	$("#insertimageurl").keydown(function(e)
 	{
 		if(e.keyCode == '13')
 		{
 			$("#pop_insertimage #submit").click();
 		}
 	}
 	)
  	$("#pop_insertimage button").click(function()
  	{
  		
  			
  		var url = encodeURI($("#pop_insertimage input").val());	
  		if(this.id == 'submit')
  		{
  			
  			if(url == "")
 				$("#pop_insertimage").hide();
 			else
 			{
	  			
	  			if(is_ie)
	  			{
		  			var imageval = "<img src='"+url+"' border=0 >";
		  			//editor.focus();
		  			//editor.document.execCommand('InsertImage',false,url);
		  			selector.pasteHTML(imageval);
	  			}
	  			else
	  			{
	  				editor.document.execCommand('InsertImage',false,url);
	  			}
	  			//insertText(selector,imageval);
	  			$("#pop_insertimage").hide();
	  			$("#pop_insertimage input").val('');
 				
 				
	  			
	  			
  			}
  		}
  		else
  		$("#pop_insertimage").hide();
  		
  		return false;
  	})
	
	//插入Flash
  	
  	$("#createflashval").keydown(function(e)
 	{
 		if(e.keyCode == '13')
 		{
 			$("#pop_insertflash #submit").click();
 		}
 	}
 	)
  	$("#pop_insertflash button").click(function()
  	{
  		
  			
  		var url = encodeURI($("#pop_insertflash input").val());	
  		if(this.id == 'submit')
  		{
  			
  			if(url == "")
 				$("#pop_insertflash").hide();
 			else
 			{
	  			
	  			var flashval = "[swf=500,400]"+url+"[/swf]";
		  			
		  		insertText(selector,flashval);
		  		
	  			$("#pop_insertflash").hide();
	  			$("#pop_insertflash input").val('');
 
  			}
  		}
  		else
  		$("#pop_insertflash").hide();
  		
  		return false;
  	})
  	
  	$("#pop_insertmedia button").click(function()
  	{
  		var url = $("#pop_insertmedia input").val();
  		if(this.id == 'submit')
  		{
	  		
	  		if(url == "")
 				$("#pop_insertmedia").hide();
 			else
 			{
		  		var mediatype = $('#pop_insertmedia input[@name=mediatype][@checked]').val();
		  		
		  		var autoplay = $('#pop_insertmedia input[@name=autoplay][@checked]').val();
		  		
		  		autoplay = autoplay==1?1:0;
		  		
		  		
		  		if(mediatype==undefined||mediatype=='')
		  			mediatype = setmediatype(url);
		  		
		  		if(mediatype==undefined || mediatype=='')
		  		{
		  			
		  			$("#pop_insertmedia #alert").html("<font color=red>未知媒体类型,请重新输入</font>");
		  		}
		  		else
		  		{	 
			  		var width = $('#pop_insertmedia input[@id=mediawidth]').val();
			  		var height = $('#pop_insertmedia input[@id=mediaheight]').val();
			  		var mediaval = "[media="+mediatype+","+width+","+height+","+autoplay+"]"+encodeURI(url)+"[/media]";
			  		insertText(selector,mediaval);
			  	
			  			
			  		$("#pop_insertmedia").hide();
		  		}
	  		}
	  		return false;
  		}
  		else
  		{
  			$("#pop_insertmedia").hide();
  		}
  		return false;
  			
  	})
  	
  	$("#pop_insertorderlist li").mouseover(function()
 	{
 		$(this).css("color","#666666");
 	})
 	$("#pop_insertorderlist li").mouseout(function()
 	{
 		$(this).css("color","#000000");
 	})
  	$("#pop_insertorderlist ul li").click(function()
 	{		
 		editor.document.execCommand(this.id,false,'');	
 		$("#pop_insertorderlist").hide();
 	})
 	$("#pop_makeindent li").mouseover(function()
 	{
 		$(this).css("color","#666666");
 	})
 	$("#pop_makeindent li").mouseout(function()
 	{
 		$(this).css("color","#000000");
 	})
  	$("#pop_makeindent ul li").click(function()
 	{		
 		editor.document.execCommand(this.id,false,'');	
 		$("#pop_pop_makeindent").hide();
 	})
 	
 	
 	$("#pop_inserttable #tablebgcolorsel").click(function()
 	{
 		
 		
 		var bgdivtop = $(this).offset().top+20;
 		var bgdivleft = $(this).offset().left-100;
 		//alert(bgdivtop);
 		bgdivtop+="px";
 		bgdivleft+="px";
 		
 		$("#pop_table_bgcolor").show();
		$("#pop_table_bgcolor").css({top:'100px',left:'100px'});
		
		$("#pop_table_bgcolor ul li").click(function()
		{
			$("#tablebgcolorsel").css("background-color",this.id);
			$("#tablebgcolor").val(this.id);
			$("#pop_table_bgcolor").hide();
			
		})
 	})
 	
 	$("#pop_inserttable button").click(function()
 	{
 		
 		if(this.id =='submit')
 		{
 			//
 			if(is_ie)
 			{
 				editor.focus();
 				if(typeof selector == 'undefined')
		 		{
		 			
		 			selector = editor.document.selection.createRange();
		 		}
 			}
 			else
 				selector = "";
 				
 			var tablewidth = $("#pop_inserttable #tablewidth").val();
 			var tablerows =  $("#pop_inserttable #tablerows").val();
 			var tablecolumns =  $("#pop_inserttable #tablecolumns").val();
 			var tablebgcolor =  $("#pop_inserttable #tablebgcolor").val();;
 			
 			var table = '<table cellspacing="0" border=1 class="content_table" cellpadding="0" width="' + (tablewidth ? tablewidth : '50%') + '" style="' + (tablebgcolor ? ' background-color:' + tablebgcolor: '') + '" >';
				for (var row = 0; row < tablerows; row++) {
					table += '<tr>\n';
					for (col = 0; col < tablecolumns; col++) {
						table += '<td>&nbsp;</td>\n';
					}
					table+= '</tr>\n';
				}
				table += '</table>\n';
			
			insertText(selector,table);
 			$("#pop_inserttable").hide();
 		}
 		else
 		{
 			$("#pop_inserttable").hide();
 			$("#pop_table_bgcolor").hide();
 		}
 		return false;
 	})
 	
 	//smile
 	
 	$("#pop_smile ul li").click(function()
 	{		
//alert(domain); 			
 		var movestart = editor.document.body.innerHTML.length;
 		
 		//var smileurl = "http://"+domain+"/images/smilies/"+this.id+".gif";
 		var smileurl = "http://"+domain+"/images/smilies/"+this.id+".gif";
 		var smileimg = "<img src='"+smileurl+"' unselectable='on'>";	
 		var insml = '<img src="'+smileurl+'">';	
 		if(is_ie)
 		{
	 		editor.focus();
	 		if(typeof selector == 'undefined')
	 		{
	 			
	 			selector = editor.document.selection.createRange();
	 		}
	 		selector.pasteHTML(smileimg);
	 		selector.select();

 		}
 		else
 		{
 			editor.document.execCommand('InsertImage',false,smileurl);
 		}
 	})
 	var firstclick = 0;
 	var addatthide = 0;
 	var inputnum = 0;
 	var totalatt = 0;
 	var insertid = "";	

 	//alert(f_content_height);
 	
 	$("#addatt").click(function()
 	{
		var f_content_height = $("#f_content").height();
		if(addatthide == 0)
		{
			//$("#f_content").height((f_content_height-155));
	 		
	 		$("#attachlist").show();
	 		
	 		$("#addatt").html("隐藏附件")
	 		addatthide = 1;
 		}
 		else
 		{
 			//$("#f_content").height(f_content_height+155);
 		
 			$("#attachlist").hide();
 			
 			$("#addatt").html("添加附件")
 			addatthide = 0;
 		}
 		
 	})
 	$("#attachlist #hidethis").click(function()
 	{
 			//var n_content_height = $("#f_content").height();
 			
 			//$("#f_content").height(n_content_height+155);
 		
 			$("#attachlist").hide();
 			addatthide = 0;
 	})
 	$("#uploading").val(0);	
 	
 	var insertrids = "";
 	
 	$("#addupload").click(function()
 	{
 		var tagid = inputnum.toString();
 		var listnum =inputnum+1;
 		var sizelimit = parseInt(uploadsize*1024);
 		var li="<li id='attli"+inputnum+"'><!--<span id='listnum'>"+(inputnum+1)+".</span>--><em id='attop"+inputnum+"' style='display:none'> <a href='javascript:' name='"+inputnum+"' title='' id='insert' unselectable='on'>插入到正文</a> <a href='javascript:' title='' name='"+inputnum+"' id='del' >删除</a></em><span id='attname"+inputnum+"'></span><span id='input"+inputnum+"'><input  name='attfile' id='attfile"+inputnum+"' title='"+inputnum+"' type='file' ></span></li>";
 			
 		$("#attachlist ul").prepend(li);
 		
 		$("span[@id=deleteswf]").click(function()
		{
					alert(this.id);
					$("#"+this.name).hide();
		})
 		
		try
 		{
 			if (sid=='undefind') sid = '';
 		
	 		$("#attfile"+inputnum).fileUpload
			({
				'uploader': pathroot+'js/uploader_att.swf',
				'cancelImg': pathroot+'images/discuz6/cancel.png',
				'width':76,
				'height':23,
				'folder': '_files&SID='+sid,
				'script': pathroot+'uploadattach.php',
				'fileDesc': 'CDEL Attach Files',
				'fileExt': '*.zip;*.rar;*.jpg;*.jpeg;*.gif;*.png;*.bmp;*.chm;*.pdf;*.doc;*.docx;*.xls;*.xlsx;*.ppt;*.txt;*.mp3;*.wma;*.rm;*.mpg;*.mov;*.swf;*.mpeg;*.flv;',
				'displayData': 'speed',
				'auto': true,
				'sizeLimit':sizelimit,
				'onComplete':function (a, b ,c, d)
				{
						
					//showCropDiv(d);
					
					var rid = getvalue(d,'rid');
					var isimg = getvalue(d,'isimg');
					var ismedia = getvalue(d,'ismedia');
					
					var filename = c.name;
					$("#attli"+tagid).css("height","25px");
					$("#attli"+tagid).css("line-height","25px");
					
					
					
					var inputhtml = "<label>"+listnum+". 描述：<input value='"+filename+"' type='text' id='attinfo' name='attinfo["+rid+"]'></label>"
					
					
					$("#input"+tagid+" #swfuploader").hide();
				
					inner = $("#input"+tagid).html();
					if(inner.indexOf("attinfo["+rid+"]") == -1)
					$("#input"+tagid).append(inputhtml);
					
					//document.getElementById("input"+tagid).innerHTML = inputhtml;
					$("#attop"+tagid).show();
					
					$("a[@id=del][@name="+tagid+"]").attr("title",rid);
					$("a[@id=insert][@name="+tagid+"]").attr("title",d);
					
					
					var uploading = $("#uploading").val();
					if(uploading >0)
						$("#uploading").val(uploading-1);
						
					if($("#uploading").val()==0)
					{
						$("#submitpost").html("发布");
						$("#submitpost").attr("disabled",false);
					}
					
					
				},
				'onError':function(e,q,f,r)
				{
					//alert(f.size);
					
					var uploading = parseInt($("#uploading").val());
					if(uploading >0)
						$("#uploading").val(uploading-1);
					if(parseInt(f.size) ==0)
						$(".fileName").html("上传文件大小不能为0");
					else if(parseInt(f.size)>=sizelimit)
					{
						var sizeinfo = (sizelimit/1024/1024);
						sizeinfo = sizeinfo.toFixed(2);
						$(".fileName").html("上传文件大小不能超过"+sizeinfo+"M");
					}	
					$("#cancelatt").click(function()
					{
						$(".fileUploadQueueItem").hide();
						
					})
					if($("#uploading").val()==0)
					{
						$("#submitpost").html("发布");
						$("#submitpost").attr("disabled",false);
					}
					
					
				},
				'onSelect':function(e,q,f)
				{
					var uploading = parseInt($("#uploading").val());
					
					$("#uploading").val(uploading+1);
					
					$("#submitpost").html("正在上传附件，请稍候...");
					$("#submitpost").attr("disabled",true);
					
				},
				'onCancel':function(e,q,f,d)
				{
					var uploading = parseInt($("#uploading").val());
					if(uploading >0)
						$("#uploading").val(uploading-1);
						
					if(parseInt($("#uploading").val())==0)
					{
						$("#submitpost").html("发布");
						$("#submitpost").attr("disabled",false);
					}
				}
				
				
			});
 		}
 		catch(err)
 		{
 			$("#attachlist").html("您的FlashPlayer插件有问题，附件上传没有成功打开。<BR>解决方法：请重新安装Flashplayer 插件");
 		}
		
		$("#attachlist a[@id=del]").click(function()
		{
			
	 		var postid = $("#postid").val();
	 		var thisrid = this.title;
	 		var liid = this.name;		
	 		 $.ajax  
			({
			 	type: "POST",
				url: pathroot+"ajaxxml.php",
			 	data:   "act=deleteattach&rid="+thisrid+"&postid="+postid+"&rand="+randStr(),
			 	timeout: ajaxtimeout,
			 	beforeSend:function(){loading(true)},
			 	success: 
			 	function(xml)
			 	{ 
			 		var status = parseInt($(xml).find('status').text());
			 		//if(status == 1)
			 		//{
						$("#attli"+liid).remove();
			 		//}
			 	},
			 	error: function() 
			 	{
			 		
			 		doerror();
			 				
			 	},
			 	complete:function(){}
			}); 
	 	})
		
		$("#attachlist a[@id=insert]").click(function()
		{
			var postid = $("#postid").val();
	 		var thisrid = getvalue(this.title,'rid');
	 				
	 		var isimg = parseInt(getvalue(this.title,'isimg'));
	 		var ismedia = parseInt(getvalue(this.title,'ismedia'));
			var filename = getvalue(this.title,'filename');
			var insert = "";		
			
			if(isimg)
				var resurl  = pathroot+"getresource.php?thumb=1&rid="+thisrid;
				//var resurl  = pathroot+"resources/img-"+thisrid+".gif";
			else
				var resurl  = pathroot+"getresource.php?rid="+thisrid;
					
			editor.focus();
			if(is_ie)
				selector = editor.document.selection.createRange();
			else
				selector = "";
			
			
			if(insertrids.indexOf("rid="+thisrid) == -1)
			{
				insertrids = insertrids+"rid="+thisrid;
				
			}
			else
			{
				
				return ;
			}	
			
			if(isimg == 1)
			{
				//alert('2');
				insert = "<img  src='"+resurl+"' >\r\n";
				//resurl = encodeURI(resurl);
				//insertText(selector,insert);	
				
				
				
				
				$.ajax 
				({
					type: "get",
					url: resurl,
					data:   "",
					timeout: ajaxtimeout,
					success: 
					function(xml)
					{ 
						editor.document.execCommand('InsertImage',false,resurl);
					},
					error: function() 
					{
						editor.document.execCommand('InsertImage',false,resurl);
					}
				})

			}
			
			if(ismedia != 0)
			{
				var mediatype = new Array();
				mediatype[13]="mp3";
				mediatype[14]="wma";
				mediatype[15]="rm";
				mediatype[16]="mpg";
				mediatype[17]="mov";
				mediatype[18]="wmv";
				mediatype[19]="swf";
				mediatype[20]="flv";
				mediatype[21]="asf";
				mediatype[22]="rm";
				
				insert = "[media="+mediatype[ismedia]+",400,50,1]"+resurl+"[/media]";
				
				insertText(selector,insert);	
				
			}
			if(isimg == 0 && ismedia == 0)
			{
				insert = "<a href='"+resurl+"'>"+filename+"</a>";
				insertText(selector,insert);	
			}
			
			$(editor.document).find(".postcontent img").each(function()
			{
				//$(this).hide();
			})
			
			
		})
 		firstclick = 1;
 		
 		$("#attachnum").val(inputnum.toString());
 		
	 	inputnum = inputnum +1;
	 	totalatt = totalatt +1;
	 	if(totalatt >= 10)
 		{
 			$("#addupload").hide();
 		}
	 	
	 	
 	})
 	
 	if(firstclick ==0 )
 	{
 		if(uploadsize > 0)
 		{
	 		$("#addupload").click();
	 		
 		}
 		else
 		{
 			$("#attachlist").html("您目前的等级，不能上传附件");
 		 	
 		}
 		firstclick = 1;
 	}
 	
 	if($("input[@id=oldattinfo]").get(0))
 	{
 		$("#addatt").click();
 	}
	$("#editorbox #errormeg .menubar span").click(function()
	{
		$("#editorbox #errormeg").hide();
	})
	$("#editorbox #errormeg ul").click(function()
	{
		$("#editorbox #errormeg").hide();
	})
	$("#editorbox #errormeg #sure").click(function()
	{
		$("#editorbox #errormeg").hide();
	})
	
	
	$("#vdcode3").keydown(function(e)
 	{
 		
 		if(e.keyCode == 13)
 		{
 			if(parseInt($("#uploading").val()) == 0)
 				
 			if($("#submitpostbtn").attr('disabled') != true)
 			{
 				$("#submitpostbtn").click();
 				submitkeydown += 1;
 			}
 			return false;
 		}
 		
 		
 		
 	});
 	
 	function loadImage(url, callback) 
	{
	    var img = new Image(); 
	    img.src = url;
	  
	    if (img.complete) 
	  	{
	
	        callback.call(img);
	        return;
	    }
	    img.onload = function () 
	    { 
	        callback.call(img);
	    };
	};
	
 	$("#vdcode6img").click(function()
 	{
 		//$("#vdcode6img").attr("src","/randcode.php")
 		this.src = 	"/randcode.php?rand="+randStr();
 	})
 	
 	$("#editorcommon .vdcodeimg").click(function()
 	{
 		this.src = 	"/randcode.php?rand="+randStr();
 	})
 	
 	
	$("#submitpostbtn").click(function()
	{	
		var content = html2bbcode(editor.document.body.innerHTML);	
 		content = content.replace(/(^\s*)|(\s*$)/g, "");
 		
	
 		
 		if(from == 'quickpost')
 		{
 			
 			
 			topicid = thetopicid;
 			
 			var username = $("#username3").val();
 			
 			var password = $("#password3").val();
 			
 			var vdcode = $("#vdcode3").val();
 			
 			var showcode = $("#showvdcode").val();
 			
			if((vdcode == "" || vdcode == "请输入验证码" ) && showcode == 1)
			{
				$("#errormeg ul").html("<li>请输入验证码</li>");
				$("#errormeg").show();
				submitkeydown = 0;
	 			return false;
			}
			
			
 			if(content == "")
	 		{
	 			
	 			$("#errormeg ul").html("<li>内容不能为空</li>");
	 			
	 			$("#errormeg").show();
	 			
	 			//$("#errormeg").css({top:($(document).scrollTop()+100)+"px"});
				$("#errormeg").css({left:(($(document).width()/2)-320)+"px"});
				
				submitkeydown = 0;
	 			return false;
	 		}
 			
 			
 			$("#submitpostbtn").html('发布中请等待...');
 			$("#submitpostbtn").attr('disabled',true);
 			
 			content =  content.replace("&","|*w*|*w*|");
 			
 			content =  content.replace("+","|*jia*|");
 			
 			$.ajax  
			({
				
				
				type: "post",
				url: pathroot+"quickpost.php",
				data: "topicid="+topicid+"&username="+username+"&password="+password+"&vdcode6="+vdcode+"&content="+content,
				timeout: ajaxtimeout,
				async:true,
				beforeSend:function()
				{
					$("#submitpostbtn").html('请等待...');
 					$("#submitpostbtn").attr('disabled',true);
				},
				success: function(xml)
				{
					var status = parseInt($(xml).find('status').text());
					if(status == 1)
					{
						
						var newlogin = parseInt($(xml).find('newlogin').text());
						
						
						if(newlogin == 1)
						{
							var returnurl = $(xml).find('returnurl').text();
							
							
							
							window.location = pathroot+returnurl;
							
							return ;
						}
						
						var cdiv = $(xml).find('postcontentdiv').text();
						
						$("#quickpostshow").append(cdiv);
						
						editor.document.body.innerHTML = "";
						$("#vdcode3").val("");
						$("#vdcode6img").attr("src","/randcode.php?rand="+randStr());
						$("#submitpostbtn").html('发布');
 						$("#submitpostbtn").attr('disabled',false);
 						submitkeydown = 0 ;
						
					}
					else
					{
						var errors = $(xml).find('errormeg').each(function()
						{
							$("#errormeg ul").html("<li>"+$(this).text()+"</li>")
						
						});
						$("#vdcode6img").attr("src","/randcode.php?rand="+randStr())
						$("#errormeg").css({left:(($(document).width()/2)-320)+"px"});
						$("#errormeg").show();
						
						$("#submitpostbtn").html('发布');
 						$("#submitpostbtn").attr('disabled',false);
						
						return false;
					}
					
				}
			})
 		}
 		
 		else
 		{
 			
 			
 			var subject = $("#subject").val();
			var vdcode = $("#vdcode3").val();
			var showcode = $("#showvdcode").val();
			
			if((vdcode == "" || vdcode == "请输入验证码") && showcode == 1)
			{
				$("#errormeg ul").html("<li>请输入验证码</li>");
				$("#errormeg").show();
	 			return false;
			}
			if( subject != undefined && subject.replace(/(^\s*)|(\s*$)/g, "")=="")
	 		{
	 			$("#errormeg ul").html("<li>标题不能为空</li>");
	 			$("#errormeg").show();
	 			return false;
	 		}
	 		
	 		if(content == "")
	 		{
	 			
	 			$("#errormeg ul").html("<li>内容不能为空</li>");
	 			$("#errormeg").show();
	 			
	 			return false;
	 		}
	 		
	 		
	 		$("#content").val(content);
	 		
	 		if(edittype == 'poll')
	 		$("#mainform").attr("action",pathroot+"newpostiframe.php");
	 		else
	 		$("#mainform").attr("action",pathroot+"newpostiframe.php");
	 		
	 		$("#mainform").attr("target","_self");
	 		$("#submittype").val('submit');
	 		
	 		
	 		$("#submitpostbtn").html('请等待...');
 			$("#submitpostbtn").attr('disabled',true);
	 		$("#submitpost").click();
 		}
 		
	})
	
 	$("#reviewpost").click(function ()
 	{
 		
 		var content = html2bbcode(editor.document.body.innerHTML);	
 		content = content.replace(/(^\s*)|(\s*$)/g, "");
 		var subject = $("#subject").val();
 		
 		if( subject != undefined && subject.replace(/(^\s*)|(\s*$)/g, "")=="")
 		{
 			$("#errormeg ul").html("<li>标题不能为空</li>");
 			$("#errormeg").show();
 			return false;
 		}
 		if(content == "")
 		{
 			
 			$("#errormeg ul").html("<li>内容不能为空</li>");
 			$("#errormeg").show();
 			
 			return false;
 		}
 		
 		$("#content").val(content);
 		
 		$("#mainform").attr("action",pathroot+"review.php");
 		$("#mainform").attr("target","_blank");
 		$("#submittype").val('review');
 		$("#submitpost").click();
 		
 		
 		//document.forms["mainform"].submit();
 	})
 	var polltotalnum = $("#pollnum").val();
	var pollnum = polltotalnum - 1;
	$("#polloptions ul li a").click(function()
	{
			$("#polloptions ul li[@id="+this.id+"]").remove();
			polltotalnum --;
			if(polltotalnum<=10)
				$("#polloptions p").show();
	})
	$("#polloptions p a[@id=addoption]").click(function()
	{
		
		polltotalnum++;	
		
		pollnum++;
		var li = "<li id='"+pollnum+"'><input type='text' id='pollopt' name='opt[]' class='options'><a href='javascript:' id='"+pollnum+"'>删除</a></li>";
		
		$("#polloptions ul").append(li);
			
		$("#polloptions ul li a").click(function()
		{
			$("#polloptions ul li[@id="+this.id+"]").remove();
			polltotalnum --;
			if(polltotalnum<=10)
				$("#polloptions a[@id=addoption]").show();
		})
		
		if(polltotalnum>10)
		{
			$("#polloptions a[@id=addoption]").hide();
		}
		
	})
	$("#pollbox #expiration").change(function()
	{
		if($("#pollbox #expiration").val() != 0)
		{
			$("#pollbox #haveexpiration").attr("checked",true);
		}
		else
			$("#pollbox #haveexpiration").attr("checked",false);
	})
	$("#pollbox #maxchoices").change(function()
	{
		$("#pollbox #maxchoices").val($("#pollbox #maxchoices").val().replace(/[^\d]/g,''));
		
		maxchoices = parseInt($("#pollbox #maxchoices").val());
		if(maxchoices>polltotalnum)
		{
			$("#maxchoicestag").html("<font color='red'>最多选择数不能大于总选项</font>");
			$("#pollbox #maxchoices").val('');
			return ;
		}
		else if (maxchoices == 0)
		{
			$("#maxchoicestag").html("<font color='red'>最多选择数不能为0</font>");
			$("#pollbox #maxchoices").val('');
		}
		else
			$("#maxchoicestag").html("最多选择数");
		
		if($("#pollbox #maxchoices").val() > 1 && $("#pollbox #maxchoices").val() != "")
		{
			$("#pollbox #polltype").attr("checked",true);
		}
		else
			$("#pollbox #polltype").attr("checked",false);
	})

	$("#quickpostmenu #reply").click(function()
	{
		//alert("33");
		//var d=$(parent.document).find("#replybtn").attr("id");
		//alert("d"+d);
		top.location = "/newpost.php?act=newpost&topicid="+thetopicid+"&boardid="+boardid;
		//$(parent.document).find("#replybtn").click();
	})
	
	$("#quickpostmenu #newtopic").click(function()
	{
		top.location = "/newtopic.php?act=newtopic&forumid="+forumid+"&boardid="+boardid;
		
	})
	
})