// 浏览器版本检测
var BrowserInfo = new Object() ;
BrowserInfo.MajorVer = navigator.appVersion.match(/MSIE (.)/)[1] ;
BrowserInfo.MinorVer = navigator.appVersion.match(/MSIE .\.(.)/)[1] ;
BrowserInfo.IsIE55OrMore = BrowserInfo.MajorVer >= 6 || ( BrowserInfo.MajorVer >= 5 && BrowserInfo.MinorVer >= 5 ) ;

// 从Word中粘贴，去除格式
function PasteWord(){
	//if (validateMode()) return;
	oDiv.focus();
	if (BrowserInfo.IsIE55OrMore)
	{
	    var re = /<\w[^>]* class="?MsoNormal"?/gi ;
		var sHTML = GetClipboardHTML() ;
		/*
		if ( re.test(sHTML))
		{
			if ( confirm( "你要粘贴的内容好象是从Word中拷出来的，是否要先清除Word格式再粘贴？" ) )
			{
				cleanAndPaste( sHTML ) ;				
			}			
		}
		*/
		cleanAndPaste( sHTML ) ;
	}
	else if ( confirm( "此功能要求IE5.5版本以上，你当前的浏览器不支持，是否按常规粘贴进行？" ) )		
	oDiv.focus();
	
}

// 检测当前是否允许编辑
function validateMode() {
	if (!bTextMode) return true;
	alert("需转换为编辑状态后才能使用编辑功能！");
	oDiv.focus();
	return false;
}

// 取剪粘板中的HTML格式数据
function GetClipboardHTML() {
	
	oDivTemp.innerHTML = "" ;
	
	var oTextRange = document.body.createTextRange() ;
	oTextRange.moveToElementText(oDivTemp) ;
	
	oTextRange.execCommand("Paste") ;
	var sData = oDivTemp.innerHTML ;
	oDivTemp.innerHTML = "" ;		
	return sData ;
}


// 清除WORD冗余格式并粘贴
function cleanAndPaste( html ) {

	// Remove all SPAN tags
	html = html.replace(/<\/?SPAN[^>]*>/gi, "" );
	// Remove Class attributes
	html = html.replace(/<(\w[^>]*) class=([^ |>]*)([^>]*)/gi, "<$1$3") ;
	// Remove Style attributes
	html = html.replace(/<(\w[^>]*) style="([^"]*)"([^>]*)/gi, "<$1$3") ;
	// Remove Lang attributes
	html = html.replace(/<(\w[^>]*) lang=([^ |>]*)([^>]*)/gi, "<$1$3") ;
	// Remove XML elements and declarations
	html = html.replace(/<\\?\?xml[^>]*>/gi, "") ;
	// Remove Tags with XML namespace declarations: <o:p></o:p>
	html = html.replace(/<\/?\w+:[^>]*>/gi, "") ;
	// Replace the &nbsp;
	html = html.replace(/&nbsp;/, " " );
	// Transform <P> to <DIV>
	var re = new RegExp("(<P)([^>]*>.*?)(<\/P>)","gi") ;	// Different because of a IE 5.0 error
	html = html.replace( re, "<p$2</p>" ) ;	
	insertHTML( html ) ;
}

// 在当前文档位置插入.
function insertHTML(html) {
	//if (bTextMode) return false;

	if (oDiv.document.selection.type.toLowerCase() != "none"){
		oDiv.document.selection.clear() ;
	}
	if (bTextMode){
		html=HTMLEncode(html);
	}
	oDiv.document.selection.createRange().pasteHTML(html) ; 
	get_table();	
}

// 替换特殊字符
function HTMLEncode(text){
	text = text.replace(/&/g, "&amp;") ;
	text = text.replace(/"/g, "&quot;") ;
	text = text.replace(/</g, "&lt;") ;
	text = text.replace(/>/g, "&gt;") ;
	text = text.replace(/'/g, "&#146;") ;
	text = text.replace(/\ /g,"&nbsp;");
	text = text.replace(/\n/g,"<br>");
	text = text.replace(/\t/g,"&nbsp;&nbsp;&nbsp;&nbsp;");
	return text;
}