var curEditor = null;
var contentIframeLoaded =false;
function InsertModuleTag(editor){
curEditor = editor;
var Argument = new Object();
    Argument.Editor = editor;
	Argument.ExtraTagsData = document.getElementById(editor.Id + "_extraTagsData").XMLDocument;
    editor.ShowDialog(
	editor.RunTimeSystemFilePath+'inserttags.aspx'
, Argument/*argument*/
, 520
, 300
, callBackFunctionPtr
, null
, 'XML置标');
 
};
function callBackFunctionPtr(returnValue)
{
	if (returnValue)
	{
		curEditor.PasteHtml(returnValue.value);
	}
}
function pasteHtml(str)
{
    curEditor.PasteHtml(str);
}
function hidePasteWordProgress()
{
    curEditor.ModeButton.HideProgress();
}

function CustomIndent(editor)
{
	var strHtml = editor.GetHtml();
    var regx=/(<)(p|br)(\/?)(>)([^\x00-\xff　]|[\w]|<)/ig;//add "<" to support P start with html tag, tph
    var html = strHtml.replace(regx,"$1$2$3$4　　$5");
	editor.SetHtml(html, 'CustomIndent')
}
function GetWordImagePath(str)
{
    var arr=[];
    str.replace(/file:\/\/\/(.+?)(\.(gif|jpg|jpeg|bmp|png))/ig,function (re,s1,s2){arr.push(s1.replace("file:///", "")+s2);} );
    return(arr[0]);
}
function CustomPasteWord(editor)
{
    curEditor = editor;
	var uploadIframe = document.getElementById('WheWordUpLoad'+editor.Id);
    if(uploadIframe == null) 
    {
        uploadIframe = document.createElement("iframe");
        uploadIframe.id="WheWordUpLoad"+ editor.Id;
        uploadIframe.marginheight=0;
        uploadIframe.marginwidth=0;
        uploadIframe.frameborder=0;
        uploadIframe.style.width="0px";
        uploadIframe.style.height="0px";
	//get tabid
	var curTabId = -1;
	var tabidIdx = window.location.href.indexOf("tabid");
	if(tabidIdx!=-1){
	var tabidIdxEnd = window.location.href.indexOf("/",tabidIdx+6);
	curTabId = window.location.href.substring(tabidIdx+6,tabidIdxEnd);
	}
        uploadIframe.src=editor.RunTimeSystemFilePath+"Editor_UploadWord.aspx?editorId="+ editor.Id + "&tabid="+curTabId;        
        uploadIframe.onreadystatechange=function(){
        if(this.contentWindow.document.readyState=="complete" && !contentIframeLoaded){
         contentIframeLoaded = true;
         CustomPasteWord(curEditor);
         }
        };
        document.body.appendChild(uploadIframe);
        return;
    }
    editor.ModeButton.ShowProgress();

    var b = editor.GetClipboardAsHtml();

    if(!b || b==''){return;}
    var regx = /(<v:shape [^>]*>.+?<\/?v:shape>)/ig;
    var macth=null;
    var returnStr=''; 
    var index=0; 
    var imagePath = [];
    if(regx.test(b))
    {
	regx.lastIndex=0;//reset lastIndex
        while ((macth=regx.exec(b)) != null)
        {
            var c = macth[0];		                            
            returnStr+=b.substring(index,regx.lastIndex-c.length); 
            returnStr+='<img>';
            imagePath.push(GetWordImagePath(c));
            index=regx.lastIndex;     
         }
	 returnStr+=b.substring(index);	
         regx = /<img>/ig;
         returnStr = HtmlClearFormat(returnStr, 'WORD_ALL');
         var returnStr_=''; 
         index = 0;
         var step = 0;
        while ((macth=regx.exec(returnStr)) != null)
        {
            var c = macth[0];		                            
            returnStr_+=returnStr.substring(index,regx.lastIndex-c.length);
            if(imagePath[step])
            { 
                returnStr_+='<img src=&quot;'+ imagePath[step] +'&quot; />';
            }
            index=regx.lastIndex; 
            step++;
        }
	returnStr_+=returnStr.substring(index);
        uploadIframe.contentWindow.document.getElementById('UploadText').value = returnStr_;		   
        uploadIframe.contentWindow.OnSubmit(); 
    }    
    else
    {
        editor.PasteHtml(HtmlClearFormat(b,"WORD_ALL"));
        editor.ModeButton.HideProgress();
    }   
}