var EditMode=true;
var bInitialized = false;
var AttributeStr='';
var OpenWindowObjID='';


function document.onreadystatechange()
{
	if (document.readyState!="complete") return;
	if (bInitialized) return;
	bInitialized = true;
	var i,j,s,curr;
	for (i=0; i<document.body.all.length;i++)
	{
		curr=document.body.all[i];
		if (curr.className == "ToolBtn") InitBtn(curr);
	}
	//DisabledBtn(Edit);
	//EditArea.document.onclick=HideMenu;
	//window.onfocus=HideMenu;
	//var TempStr='';
	//TempStr=TempStr+'<head><link href="../../CSS/css.css" type="text/css" rel="stylesheet"></head><body topmargin="0" leftmargin="0" MONOSPACE>';
	//EditArea.document.designMode="On";
	//EditArea.document.open();
	//EditArea.document.write(TempStr);
	//EditArea.document.body.contentEditable="true";
	//EditArea.document.close();
}

function InitBtn(btn) 
{
	btn.onmouseover = BtnMouseOver;
	btn.onmouseout = BtnMouseOut;
	btn.onmousedown = BtnMouseDown;
	btn.onmouseup = BtnMouseOut;
	btn.ondragstart = YCancelEvent;
	btn.onselectstart = YCancelEvent;
	btn.onselect = YCancelEvent;
	btn.disabled=false;
	return true;
}

function BtnMouseOver() 
{
	var image = event.srcElement;
	image.className = "ToolBtnMouseOver";
	event.cancelBubble = true;
}

function BtnMouseOut() 
{
	var image = event.srcElement;
	image.className = "ToolBtn";
	event.cancelBubble = true;
}

function BtnMouseDown() 
{
	var image = event.srcElement;
	image.className = "ToolBtnMouseDown";
	event.cancelBubble = true;
	event.returnValue=false;
	return false;
}

function DisabledBtn(btn)
{
	btn.className='ToolBtnMouseDown';
	btn.onmouseover = YCancelEvent;
	btn.onmouseout = YCancelEvent;
	btn.onmousedown = YCancelEvent;
	btn.onmouseup = YCancelEvent;
	btn.ondragstart = YCancelEvent;
	btn.onselectstart = YCancelEvent;
	btn.onselect = YCancelEvent;
	btn.disabled=true;
	return true;
}

function ShowCode(Obj,ObjEdit)
{
	EditMode=false;
	EditArea.focus();
	//var TempHTML=Preview.document.body.innerHTML;
	var TempHTML=GetPreviewHTMLCode();
	EditArea.document.body.innerText=TempHTML;
	//DisabledBtn(Obj);
	//InitBtn(ObjEdit);
	//ObjEdit.className='ToolBtn';
}

function ShowEdit(Obj,ObjHTML)
{
	EditMode=true;
	EditArea.focus();
	var TempText=EditArea.document.body.innerText;
	EditArea.document.body.innerHTML=TempText;
	//DisabledBtn(Obj);
	//InitBtn(ObjHTML);
	//ObjHTML.className='Btn';
}

function insertHTML(html) 
{
	if (false) return false;
	Preview.focus();
	if (Preview.document.selection.type.toLowerCase() != "none")
	{
		Preview.document.selection.clear();
	}
	Preview.document.selection.createRange().pasteHTML(html) ; 
}

function OpenLableAttributeWindow(Url,Width,Height,WindowObj)
{
	 var ReturnValue=OpenWindow('Frame.asp?FileName='+Url,Width,Height,WindowObj);
	// if ((EditMode==true)&&(document.TempletForm.AutoInsertLable.checked==true))
	 //{
		// insertHTML(ReturnValue);
	// }
}

function SearchLable()
{
	if (document.TempletForm.LableList.selectedIndex!=-1)
	{
		SearchStr(document.TempletForm.LableList.options(document.TempletForm.LableList.selectedIndex).text);
	}
}

function SearchStr(Str)
{
    if (Str!='') 
	{
		Preview.focus();
		if (Preview.document.selection.type!='control')
		{
			var SelectionObj=Preview.document.selection.createRange();
			SelectionObj.collapse(false);
			SelectionObj.expand("textedit");
			if (SelectionObj.findText(Str,100)) SelectionObj.select();
			else 
			{
				if (confirm("搜索完成，是否要从顶部开始重新搜索？")==true) 
				{
					SelectionObj.expand("textedit");
					SelectionObj.collapse();
					SelectionObj.select();
					SearchStr();
				}
			}
		}
    }
}

function AddLableList(SelectObj,Lable,LableContent)
{
	var i=0,AddOption;
	if (!SearchOptionExists(SelectObj,Lable))
	{
		AddOption = document.createElement("OPTION");
		AddOption.text=Lable;
		AddOption.value=LableContent;
		SelectObj.add(AddOption);
		SelectObj.options(SelectObj.length-1).selected=true;
	}
}

function EditLableList(SelectObj,OldLable,Lable,LableContent)
{
	var i=0;
	if (SearchOptionExists(SelectObj,OldLable))
	{
		for (i=0;i<SelectObj.length;i++)
		{
			if (SelectObj.options(i).text==OldLable)
			{
				SelectObj.options(i).text=Lable;
				SelectObj.options(i).value=LableContent;
			}
		}
		ReplaceStr(OldLable,Lable);
	}
	else AddLableList(SelectObj,Lable,LableContent);
}

function DelLableList(SelectObj)
{
	if (SelectObj.selectedIndex!=-1) 
	{
		if (EditMode==true)
		{
			var SearchValue = SelectObj.options(SelectObj.selectedIndex).text;
			ReplaceStr(SearchValue,'');
		}
		SelectObj.options.remove(SelectObj.selectedIndex);
	}
}

function ReplaceStr(SourceStr,ObjStr)
{
	Preview.focus();
	var SelectionObj=Preview.document.selection.createRange();
	SelectionObj.expand("textedit");
	SelectionObj.collapse();
	SelectionObj.select();
	while (SelectionObj.findText(SourceStr,100))
	{
		SelectionObj.select();
		SelectionObj.text =ObjStr;
	}
}

function InsertLable(SelectObj)
{
	if (EditMode==true)
	{
		if (SelectObj.selectedIndex!=-1) insertHTML(SelectObj.options(SelectObj.selectedIndex).text);
	}
}

function SearchOptionExists(Obj,SearchText)
{
	var i;
	for(i=0;i<Obj.length;i++)
	{
		if (Obj.options(i).text==SearchText) return true;
	}
	return false;
}

function EditLableAttribute(Str)
{
	if (Str!='')
	{
		var IDStr=Str.substring(0,Str.indexOf(';',0))
		BeforeOpenLableAttributeWindow(IDStr,Str.slice(Str.indexOf(';',0)+1));
		document.all(IDStr).onclick();
	}
}

function BeforeOpenLableAttributeWindow(IDStr,TempStr)
{
	AttributeStr=TempStr;
	OpenWindowObjID=IDStr;
}

function GetPreviewHTMLCode()
{
	var i=0;
	for (i=0;i<EditArea.document.all.length;i++)
	{
		if (EditArea.document.all(i).tagName.toLowerCase()=='html') return '<html>'+EditArea.document.all(i).innerHTML+'</html>';
	}
}

function SaveSubmit()
{
	var i=0,InputObj,FormObj;
	FormObj=document.createElement('form');
	FormObj.name='TempForm';
	FormObj.action='Templet_Save.asp';
	FormObj.method='post';
	FormObj=document.body.appendChild(FormObj);
	for (i=0;i<document.TempletForm.LableList.length;i++)
	{
		InputObj=document.createElement('input');
		InputObj.type='hidden';
		InputObj.name=document.TempletForm.LableList.options(i).text;
		InputObj.value=document.TempletForm.LableList.options(i).value;
		FormObj.appendChild(InputObj);
	}
	InputObj=document.createElement('input');
	InputObj.type='hidden';
	InputObj.name='TempletName';
	InputObj.value=GetPreviewHTMLCode();
	FormObj.appendChild(InputObj);
	FormObj.submit();
}

function Format(Operation,Val) 
{
	EditArea.focus();
	if (Val==null) EditArea.document.execCommand(Operation);
	else EditArea.document.execCommand(Operation,true,Val);
	EditArea.focus();
}

function ShowChildMenu(menu)
{
  var i, s, s1, items, disabled, element, sts;
  
  // Note which scriptlet this menu is going into
  //menu.TBLEVEL = nestingDepth;
  
  // We need to wrap each menu body in a couple of divs and a table. Here's the outer string for this.
  s = '<div id=' + menu.id + '><div><table id="TBMENUBODYTABLE" cellpadding=0 cellspacing=0>';
  
  // Go through all the children in the menu, looking for menu body elements.
  items = menu.children.tags("DIV");
  for (i=0; i<items.length; i++) 
  {
    element = items[i];
    
    if (element.TBSTATE == "gray"){
      disabled = 'style = "filter:' + TB_DISABLED_OPACITY_FILTER + '"';
    } else {
      disabled = "";
    }
    
    switch (element.className) {
      case "DownMenuItem" :
        if (element.id == "") {
          return TB_E_NO_ID;
        }
        
        // Save away the user's onclick event handler
       // if (element.TBINITIALIZED == null) {
       //   element.TBUSERONCLICK = element.onclick; 
       //   element.onclick = TBCancelEvent;
       //   element.TBINITIALIZED = true;
       // }

        // Note which scriptlet this menu item is going into
        //element.TBLEVEL = nestingDepth;
        
        // Wrap a table row around the menu item and put in its icon if it exists. 
        //s1 = TBCheckMenuIcon(element, disabled); 
        s += '<tr TBELEMENTID="' + element.id + '" ><td noWrap >' + element.outerHTML + '</td><td ><span>&nbsp</span></td></tr>';
      break;
        
      case "tbSubmenu" :
        if (!buildSubmenus) {
          return;
        }
        
        if (element.id == "") {
          return TB_E_NO_ID;
        }
                
        // Call ourselves recursively to populate the subMenu.
        if ((sts = TBBuildMenu(element, nestingDepth + 1, true)) != TB_STS_OK) {
          return sts;
        }
        
        // Wrap a table row around the menu item, put the icon in if it exists and 
        // put the subMenu arrow in.
        s1 = TBCheckMenuIcon(element, disabled);
        s += '<tr TBELEMENTID="' + element.id + '" >' + s1 + '<td noWrap ' + disabled +'>' + element.outerHTML + '</td><td ' + disabled +'><span class="tbSubmenuGlyph">4</span></td></tr>';
      break;
        
      case "tbSeparator" :
        // Change the class to tbMenuSeparator. That way the separators aren't
        // visible on the main page.
        element.className = "tbMenuSeparator";
        
        // Save a separator as a couple of divs that span the entire width of the menu.
        s += '<tr class="tbMenuSeparator"><td align=center colspan=3><div class="tbMenuSeparatorTop"></div><div class="tbMenuSeparatorBottom"></div></td></tr>';
      break;
        
    }
  }  

  // We need to turn any relative URLs in the menu body into absolute URLs, because
  // relative URLs will not work in the scriptlet (IE makes them absolute relative
  // to the scriptlet when we paste them into the scriptlet). Do this by inserting 
  // the menu body into the document and extracting the outerHTML. This is a rather
  // expensive way of doing this, but its the only way to guarantee that all 
  // user-supplied URLs also gets updated...
  s += "</table></div></div>";
	//alert(s);
  document.body.insertAdjacentHTML("AfterBegin", s);
  s = document.body.all[0].outerHTML;
  document.body.all[0].outerHTML = "";
  //alert(s.style.posTop);
  // Save the menu body string on the menu object.
  menu.TBMENUBODY = s;
  //return TB_STS_OK;
}