﻿
// Attach a handler to the load event.

/*
Sys.Application.add_load(wiki_appLoadHandler);

function wiki_appLoadHandler()
{
alert(1);
}
*/



function wiki_InitEditor(clientID)
{
//alert($find(clientID)==null);
//alert($find(clientID).get_contentArea()==null);
//	$find(clientID).get_contentArea().style.backgroundImage = "url(/images/editor_background.gif)";
}

//function wiki_GetClientID(o)
//{
//	var p=o;while(p.getAttribute("c_clientID")==null || p.getAttribute("c_clientID")=="")p=p.parentNode;
//	return p.getAttribute("c_clientID");
//}

function pageLoad()
{
	
//alert(
//_id
//+ "     " + ($get(_id)==null)
//+ "     " + ($find(_id)==null)
//);
	var _id = window.v_editorID;
	try
	{
		var ed = $find(window.v_editorID);
		ed.get_contentArea().style.backgroundImage = "url(/images/editor_background.gif)";
		ed.get_contentArea().setAttribute("v_clientID",window.v_clientID);
	}
	catch(e){}
}

//-----------------------------------------------------------
function wiki_Init(clientID)
//function wiki_Init(panMainClientID)
{
	try
	{
		//var clientID = panMainClientID.substring(0, panMainClientID.lastIndexOf("_panMain"));
		var o = new __CWiki(clientID);
		o.Init();
	}
	catch(e){}

/*
	try
	{
		var hidMnu = document.getElementById(clientID+"_hidSaveMenuID");
		var hidPH = document.getElementById(clientID+"_hidPH4SaveMenuID");
		var mnu = document.getElementById( hidMnu.value );
		var ph = document.getElementById( hidPH.value );
		ph.innerHTML = "";
		ph.appendChild(mnu);
		//mnu.style.display = "block";
	}
	catch(e){}
	
	try
	{
		var hidMnu = document.getElementById(clientID+"_hidInsMenuID");
		var hidPH = document.getElementById(clientID+"_hidPH4InsMenuID");
		var mnu = document.getElementById( hidMnu.value );
		var ph = document.getElementById( hidPH.value );
		ph.innerHTML = "";
		ph.appendChild(mnu);
//alert(mnu.outerHTML + "\n-----------------------\n" + ph.innerHTML);
		//mnu.style.display = "block";
	}
	catch(e){}
	*/
}
//-----------------------------------------------------------
function wiki_InitEditor(clientID, editorID)
{
//alert(editorID);
	window.v_editorID = editorID;
	window.v_clientID = clientID;
	
//	var ed = $find(editorID);
//	alert(ed==null);
//	ed.get_contentArea().style.backgroundImage = "url(/images/editor_background.gif)";

	Telerik.Web.UI.Editor.CommandList["CustomProperties"] = function(commandName, editor, args)
	{
		wiki_SetCustomProperties(editor);
	};
	Telerik.Web.UI.Editor.CommandList["CustomInsert"] = function(commandName, editor, args)
	{
		wiki_InsertElement( editor, args.get_value() );
	};
}


function wiki_SetCustomProperties(ed)
{
//alert(ed.get_id() + "\n" + editorID);
	//var doc = ed.get_document();
//alert("doc: " + (doc==null));
	//var sel = doc.selection.createRange();
	//var sel = ed.getSelection();
	
	var c = ed.getSelectedElement();
//alert(sel==null);
//alert(typeof(sel) + "     " + sel.length);//(sel==null));
//alert(sel);//c==null);
//alert(sel.tagName);

	var t = c.getAttribute("c_type");
//var c=new Function();
//c.tagName = "IFrame";
//var t="SurveyEntries";
		
	if (c.tagName.toUpperCase()=="IFRAME" && t!=null)
	{
		//var wiki = __CWiki_GetObject(o);
		var args = new Function();
		args.editor = ed;
		args.control = c;
		args.response = null;
//args.clientID = "";//wiki.clientID;
//args.wikiID = 0;//wiki.wikiID;
		var date1 = new Date();
		var url = "/Panels/Wiki/Properties/SetProperties.aspx?_rand="+date1.getTime() + "&ct=" + t;
		
//		g_global.selectedControl = c;
//		g_global.selectedEditor = ed;
		
		//...show the dialog page , and pass the editor as newwin.dialogArguments.editor
		//...(handler,url,args,feature)
//		var handler = wiki_Handler_PropChanged;
//		var newwin = ed.ShowDialog(handler,url,args, 
//			"dialogWidth:450px; dialogHeight:500px; center:yes; edge:raised; resizable:yes; scroll:no; status:yes;");

		ed.showExternalDialog(
			   url,
			   args,
			   450,
			   535,
			   wiki_Callback_PropChanged,
			   null,
			   'Control Properties',
			   true,
			   Telerik.Web.UI.WindowBehaviors.Close + Telerik.Web.UI.WindowBehaviors.Move,
			   false,
			   false);
		

//alert(typeof(newwin));
//alert(c.outerHTML);
	}
}
//-----------------------------------------------------------
//--- wiki_Callback_PropChanged
//-----------------------------------------------------------
function wiki_Callback_PropChanged(sender, args)
{
	var c = args.control; // g_global.selectedControl;
	if (c.tagName.toUpperCase()=="IFRAME")
	{
		var t = c.getAttribute("c_type");
		var id = c.id;
		var arr = c.attributes;
		var dt = new Date();
		var ss = "?_d_t=" + dt.getTime() + "&t=" + t + "&callerID=" + id;
		for(var j=0; j<arr.length; j++)
		{
			var attr = arr.item(j);
			var name = attr.nodeName;
			var val = attr.nodeValue;
			if (name.indexOf("v_") == 0 && name.lastIndexOf("_display")==-1)
			{
				if (name!="v_permissions")
				{
					ss += "&" + name + "=" + escape(val);
				}
			}
		}
		c.src = "/Panels/Wiki/Editor/DrawControl.aspx" + ss;

		args.editor.set_html( ed.get_html(true) );
	}
}

//-----------------------------------------------------------
function wiki_GetNextIDForToken(editor,token)
{	
	var num=1;
	var doc = editor.get_document();
	while(doc.getElementById(token+""+num)!=null){ num++; }
	return token+""+num;
}
//-------------------------------------------------------------
//--- InsertElement
//-------------------------------------------------------------
function wiki_InsertElement(editor,etype)
{	
//alert("etype="+etype);
	var id = wiki_GetNextIDForToken(editor,"iframe");
	
	var hh = "100px";
	var ww = "300px";
	
//	var ind = 1;
//	var id = "iframe_";
//	var cnt = 0;
//	var doc = editor.get_document();
//	while(doc.getElementById(id+ind)!=null){ 	ind++;	cnt++; 	if(cnt > 1000){	alert("Too many controls on page.");return; } }
//	id = id + ind;
	
	var src = "/Panels/Wiki/Editor/DrawControl.aspx?t=" + etype;// + "&callerID=" + id;
	var ss = 
		"<iframe " + 
			"id='" + id + "' " + 
			"src='" + src + "' " + 
			"c_type='" + etype + "' " + 
			"style='width:"+ww+"; height:"+hh+";'>" + 
		"</iframe>";
	editor.pasteHtml( ss );
}














//----------------------------------------------
//--- OpenStandardWindow
//----------------------------------------------
function wiki_OpenStandardWindow(clientID,url,caption,width,height)
{
	window.open(url, "_blank", "width="+width+"px,height="+height+"px,location=no,menubar=no,toolbar=no,resizable=no");
	return false;
}
//----------------------------------------------
//--- OpenModalWindow
//----------------------------------------------
function wiki_OpenModalWindow(clientID,url,caption,width,height)
{
	document.getElementById(clientID+"_lModalWinCaption").innerHTML = caption;

	var pan = document.getElementById(clientID+"_panModal");
	var hed = document.getElementById(clientID+"_panModalHeader");
	var panObj = document.getElementById(clientID+"_panObject");
	
	panObj.innerHTML = "";
	pan.style.width = width + "px";
	pan.style.height= height + "px";
/*	
alert(
"appCodeName = " + window.navigator.appCodeName + "\n" + 
"appName = " + window.navigator.appName + "\n" + 
"platform = " + window.navigator.platform + "\n" + 
"userAgent = " + window.navigator.userAgent + "\n"
);
return;
*/
	var ss="";
	
	if (window.navigator.userAgent.indexOf("MSIE") != -1)
	{
		ss = "<iframe class='modalPopupIFrame' scrolling='no' " + 
				"style='width:" + width + "px; height:" + (height-19) + "px;' " + 
				"src='" + url + "' " + 
				"title='iframe for modal popup panel'>" + 
				"iframe for modal popup panel" + 
			"</iframe>";
	}
	else
	{
		ss = "<object " + 
				//"classid='clsid:25336920-03F9-11CF-8FD0-00AA00686F13' " + 
				"style='width:" + width + "px; height:" + (height-19) + "px; overflow:hidden' " + 
				"data='" + url + "' " + 
				"title='object for modal popup panel' type='text/html'>" + 
				"<p>object for modal popup panel</p>" + 
			"</object>";
	}
	
/*
	ss = "<object " + 
			(window.navigator.userAgent.indexOf("MSIE") != -1 ? "classid='clsid:25336920-03F9-11CF-8FD0-00AA00686F13' " : "") + 
			"style='width:" + width + "px; height:" + (height-19) + "px; overflow:hidden' " + 
			"data='" + url + "' " + 
			"title='object for modal popup panel' type='text/html'>" + 
			"<p>object for modal popup panel</p>" + 
		"</object>";
*/

	panObj.innerHTML = ss;
	
	//url = url.substring(7);
//alert(url);
	//var fr  = document.getElementById(clientID+"_ifrModal1");
//	fr.style.width  = width + "px";
//	fr.style.height = (height-19) + "px";
//	//fr.src = url;
//	fr.data = url;

	g_global.FireMouseEvent( document.getElementById(clientID+"_lbCallPopup"), "click");
}
//-----------------------------------------------------------
function wiki_CSSRepository(o,clientID)
{
	var ww = window.screen.availWidth - 100; if(ww>950)ww=950;
	var hh = window.screen.availHeight - 200;
	var url = "/Panels/CSS/default.aspx?clientID="+clientID+"&sessid="+g_global.sessionID+"&time="+(new Date()).getTime();
	wiki_OpenModalWindow(clientID,url,"Style Sheets Repository",ww,hh);
	return false;
}
//----------------------------------------------
//--- Modal window commands
//----------------------------------------------
function wiki_Modal_Close(clientID)
{
//alert(1);
	g_global.FireMouseEvent( document.getElementById(clientID+"_butCancelModal2"), "click");
//alert(2);
//	var fr  = document.getElementById(clientID+"_ifrModal1");
	//fr.location = "about:blank";
//	fr.data = "about:blank";
//	document.getElementById(clientID+"_panObject").innerHTML = "";
//alert(3);
}
function wiki_Modal_CSSChanged(clientID)
{
	wiki_Modal_Close(clientID);
	var prm = Sys.WebForms.PageRequestManager.getInstance();
	prm._doPostBack(clientID+"_upClients", '');
}

//-----------------------------------------------------------
function wiki_ToggleNavBar(o)
{
	var wiki = __CWiki_GetObject(o);
	wiki.ToggleNavBar();
}
//-----------------------------------------------------------
function wiki_ManageMenu(o)
{
	var wiki = __CWiki_GetObject(o);
	var url = C_GLOBALBARIABLES.CurrentProtocolAndHost + "/Panels/MenuEditor/default.htm?scope=wiki" + 
		"&clientID=" + wiki.clientID + 
		"&wikiID=" + wiki.wikiID + 
		"&callback=wiki_DoChangeMenu";
	window.open(url, "_blank", "width=550px,height=450px,location=no,menubar=no,toolbar=no,resizable=yes");
	return false;
}
//-----------------------------------------------------------
function wiki_DoChangeMenu(val,clientID)
{
//alert(clientID + "    " + val);
	document.getElementById(clientID+"_hidValue").value = val;
	document.getElementById(clientID+"_hidCommand").value = "ChangeMenu";
	eval(unescape(document.getElementById(clientID+"_lbCommand").href));
}
//-----------------------------------------------------------
function wiki_ViewPage(o)
{
	var ww = window.screen.availWidth - 100; if(ww>950)ww=950;
	var hh = window.screen.availHeight - 200;
	var wiki = __CWiki_GetObject(o);
	var url = C_GLOBALBARIABLES.CurrentProtocolAndHost + "/Panels/SelectWikiTopic/default.htm?cid=" + wiki.clientID + 
		"&wid=" + wiki.wikiID + "&callback=wiki_DoViewPage";
	window.open(url,"_blank", "width="+ww+"px,height="+hh+"px,location=no,menubar=no,toolbar=no,resizable=yes");
	return false;
}
//-----------------------------------------------------------
function wiki_DoViewPage(clientID, topicID, topicName)
{
	document.getElementById(clientID+"_hidValue").value = topicID;
	document.getElementById(clientID+"_hidName").value = topicName;
	document.getElementById(clientID+"_hidCommand").value = "ViewPage";//AddTopic";
	eval(unescape(document.getElementById(clientID+"_lbCommand").href));
}
//-----------------------------------------------------------
function wiki_Settings(o)
{
	var wiki = __CWiki_GetObject(o);
	var url = C_GLOBALBARIABLES.CurrentProtocolAndHost + "/Panels/Wiki/WikiSettings/default.htm?cid=" + wiki.clientID + 
		"&wid=" + wiki.wikiID + "&callback=wiki_SettingsChanged";
	window.open(url,"_blank", "width=850px,height=600px,location=no,menubar=no,toolbar=no,resizable=yes");
	return false;
}
//-----------------------------------------------------------
function wiki_SettingsChanged(clientID, wikiID, generalProps, wikiPerms, defPagePerms, toolbarView)
{
	var ss = 
	"<Root wiki='"+wikiID+"'>" + 
		"<Set name='GeneralProperties' changed='" + (generalProps==null ? "n" : "y") + "'>" + 
			(generalProps==null ? "" : generalProps) +
		"</Set>" + 
		"<Set name='WikiPermissions' changed='" + (wikiPerms==null ? "n" : "y") + "'>" + 
			(wikiPerms==null ? "" : wikiPerms) +
		"</Set>" + 
		"<Set name='DefaultPagePermissions' changed='" + (defPagePerms==null ? "n" : "y") + "'>" + 
			(defPagePerms==null ? "" : defPagePerms) +
		"</Set>" + 
		"<Set name='ToolbarViews' changed='" + (toolbarView==null ? "n" : "y") + "'>" + 
			(toolbarView==null ? "" : toolbarView) +
		"</Set>" + 
	"</Root>";
//alert(ss);
	document.getElementById(clientID+"_hidValue").value = ss;
	document.getElementById(clientID+"_hidCommand").value = "WikiSettingsChanged";
	eval(unescape(document.getElementById(clientID+"_lbCommand").href));
}
//-----------------------------------------------------------
function wiki_Recycle(o)
{
	var wiki = __CWiki_GetObject(o);
	var url = C_GLOBALBARIABLES.CurrentProtocolAndHost + "/Panels/Wiki/Recycle/default.htm?cid=" + wiki.clientID + 
		"&wid=" + wiki.wikiID + "&callback=wiki_RecycleChanged";
	window.open(url,"_blank", "width=850px,height=600px,location=no,menubar=no,toolbar=no,resizable=yes");
	return false;
}
//-----------------------------------------------------------
function wiki_RecycleChanged(clientID, pageIDs)
{
//alert(pageIDs);
	document.getElementById(clientID+"_hidValue").value = pageIDs;
	document.getElementById(clientID+"_hidCommand").value = "RecycleChanged";
	eval(unescape(document.getElementById(clientID+"_lbCommand").href));
}
//-----------------------------------------------------------
function wiki_Log(o)
{
	var wiki = __CWiki_GetObject(o);
	document.getElementById(wiki.clientID+"_hidCommand").value = "SearchLog";
	eval(unescape(document.getElementById(wiki.clientID+"_lbCommand").href));
}

//-----------------------------------------------------------
function wiki_PagePerms(clientID,wikiID,pageID)
{
	var ww = 800;
	var hh = 600;
//alert( g_global.sessionID + "     " + g_global.sessionToken);
	var url = C_GLOBALBARIABLES.CurrentProtocolAndHost + "/Panels/Wiki/PagePermissions/default.aspx" + 
			"?clientID=" + clientID +
			"&showas=ajaxmodalwindow" + //basewindow" + 
			"&sessid=" + g_global.sessionID +
			"&forcessl=y" + 
			"&wikiID=" + wikiID + 
			"&pageID=" + pageID + 
			"&callback=wiki_PagePermsChanged" + 
			"&tm=" + (new Date()).getTime();
//alert(url);
	wiki_OpenModalWindow(clientID,url,"Wiki Page Permissions",ww,hh);
	return false;
	
	/*
	var url = C_GLOBALBARIABLES.CurrentProtocolAndHost + "/Panels/Wiki/PagePermissions/default.htm" + 
			"?cid=" + clientID + 
			"&wid=" + wikiID + 
			"&pid=" + pageID + 
			"&callback=wiki_PagePermsChanged";
	window.open(url,"_blank", "width=800px,height=600px,location=no,menubar=no,toolbar=no,resizable=yes");
	return false;
	*/
}
//-----------------------------------------------------------
function wiki_PagePermsChanged(clientID, wikiID, pageID, perms, inh)
{
	try{ wiki_Modal_Close(clientID); }catch(e){}
	
//alert(clientID + "\n" + wikiID + "\n" + pageID + "\n" + perms + "\n" + inh);
	var ss = 
	"<Root wiki='"+wikiID+"' page='"+pageID+"'>" + 
		"<Set name='PagePermissions' changed='" + (perms==null ? "n" : "y") + "'>" + 
			(perms==null ? "" : perms) +
		"</Set>" + 
	"</Root>";
	document.getElementById(clientID+"_hidValue").value = ss;
	if(inh == "y")
	{
		document.getElementById(clientID+"_hidCommand").value = "PagePermsChanged_Inherit";
	}
	else
	{
		document.getElementById(clientID+"_hidCommand").value = "PagePermsChanged_NotInherit";
	}
	eval(unescape(document.getElementById(clientID+"_lbCommand").href));
}
//-----------------------------------------------------------
function wiki_PageProps(clientID,wikiID,pageID)
{
/*
	var url = "/Panels/Wiki/PageSettings/default.htm?cid=" + clientID + 
		"&wid=" + wikiID + "&pid=" + pageID + "&callback=wiki_PagePropsChanged";
	window.open(url,"_blank", "width=800px,height=600px,location=no,menubar=no,toolbar=no,resizable=yes");
	return false;
*/
	var elem = document.getElementById(clientID+"_divPageProps").childNodes[0];	
	var ww = 600;
	var hh = 493;
//alert( g_global.sessionID + "     " + g_global.sessionToken);
	//var url = C_GLOBALBARIABLES.CurrentProtocolAndHost + "/Panels/Wiki/PageSettings/default.aspx" + 
	var url = "/Panels/Wiki/PageSettings/default.aspx" + 
			"?clientID="+clientID+
			"&showas=ajaxmodalwindow" + //basewindow" + 
			"&sessid="+g_global.sessionID+
			"&forcessl=y" + 
			"&tm="+(new Date()).getTime();
	wiki_OpenModalWindow(clientID,url,"Wiki Page Settings",ww,hh);
	return false;
	//return wiki_OpenStandardWindow(clientID,url,"Wiki Page Settings",ww,hh);

}
function wiki_Modal_PagePropsChanged(clientID, props)
{
	wiki_Modal_Close(clientID);
//alert(props);
//return;
	document.getElementById(clientID+"_hidValue").value = props;
	document.getElementById(clientID+"_hidCommand").value = "PagePropsChanged";
	eval(unescape(document.getElementById(clientID+"_lbCommand").href));

//	var prm = Sys.WebForms.PageRequestManager.getInstance();
//	prm._doPostBack(clientID+"_upClients", '');
}
/*
//-----------------------------------------------------------
function wiki_PagePropsChanged(clientID, wikiID, pageID, props)
{
	document.getElementById(clientID+"_hidValue").value = props;
	document.getElementById(clientID+"_hidCommand").value = "PagePropsChanged";
	eval(unescape(document.getElementById(clientID+"_lbCommand").href));
}
*/
//-----------------------------------------------------------
function wiki_PageHistory(clientID,wikiID,pageID)
{
	var url = "/Panels/Wiki/PageHistory/default.htm?cid=" + clientID + 
		"&wid=" + wikiID + "&pid=" + pageID + "&callback=wiki_ShowPageFromHistory";
	window.open(url,"_blank", "width=800px,height=600px,location=no,menubar=no,toolbar=no,resizable=yes");
	return false;
}
//-----------------------------------------------------------
function wiki_ShowPageFromHistory(clientID, wikiID, pageID, itemID)
{
	document.getElementById(clientID+"_hidHistoryID").value = itemID;
	document.getElementById(clientID+"_hidCommand").value = "ShowPageFromHistory";

	eval(unescape(document.getElementById(clientID+"_lbCommand").href));
}
//-----------------------------------------------------------
function wiki_CheckOut()
{
	return confirm("Do you want to check-out this page and make it non-editable by others?");
}
//-----------------------------------------------------------
function wiki_CheckIn()
{
	return confirm("Do you want to check-in this page and make it editable by others?");
}
//-----------------------------------------------------------
function wiki_Rollback()
{
	return confirm("Do you want to rollback all changes for the page and restore last checked-in page?");
}


//-----------------------------------------------------------
/*
function wiki_Search(url,clientID)
{
	var txt = document.getElementById(clientID+"_txtSearch").value;
	txt = g_global.Trim(txt);
	if (txt=="")
	{
		alert("Search keyword is empty");
	}
	else
	{
		location.href = url + "&search=" + txt;
	}
	return false;
}
*/
//-----------------------------------------------------------
/*
function wiki_SearchKeyPressed(e)
{
	var f = g_global.GetEvent(e);
	var o = f.srcElement;
	if (f.keyCode==13)
	{
		var txt = g_global.Trim(o.value);
		if (txt=="")
		{
			alert("Search keyword is empty");
			return false;
		}
		else
		{
			o = document.getElementById(o.getAttribute("c_clientID")+"_butSearch");
			eval(unescape(o.onclick));
		}
//alert(o.outerHTML);		
		//wiki_DoSearch(o);
	}
}
*/

function wiki_SearchKeyDn(e)//clientID)
{
	var f = g_global.GetEvent(e);
	var o = f.srcElement;
	if (f.keyCode==13)
	{
		wiki_SearchClick(o.getAttribute("c_clientID"));
		/*
		try
		{
			f.eventObject.cancelBubble = true;
			f.eventObject.returnValue = false;
		}
		catch(e){}
		*/
		return false;
	}
}
//-----------------------------------------------------------
function wiki_SearchClick(clientID)
{
	var o = document.getElementById(clientID+"_txtSearch");
	var txt = g_global.Trim(o.value);
	if (txt=="")
	{
		alert("Search keyword is empty");
	}
	else
	{
		document.getElementById(clientID+"_hidCommand").value = "Search";
		eval(unescape(document.getElementById(clientID+"_lbCommand").href));
	}
}
//-----------------------------------------------------------
function wiki_SaveAsTemplate(clientID)
{
	var txt = prompt("Enter name of template","");// g_global.Trim(o.value);
	if (txt!=null)
	{
		if (txt=="")
		{
			alert("Template name is empty");
		}
		else
		{
			document.getElementById(clientID+"_hidCommand").value = "SaveAsTemplate";
			document.getElementById(clientID+"_hidValue").value = txt;
			eval(unescape(document.getElementById(clientID+"_lbCommand").href));
		}
	}
}
//-----------------------------------------------------------
function wiki_Save(clientID,cmd)
{
	var ok = false;
	if (cmd=="SaveAsTemplate")
	{
		var txt = prompt("Enter name of template","");// g_global.Trim(o.value);
		if (txt!=null)
		{
			if (txt=="")
			{
				alert("Template name is empty");
			}
			else
			{
				ok = true;
				document.getElementById(clientID+"_hidValue").value = txt;
			}
		}
	}
	else
	{
//var ed = wiki_GetEditor("CE_ctl00_ctl00_WebPartManager1_wp1864712602_wp122109507_tabControl1_Editor1_ID");
//alert(ed.getHTML);
		ok = true;
	}
	
	if (ok)
	{
		document.getElementById(clientID+"_hidCommand").value = cmd;
		eval(unescape(document.getElementById(clientID+"_lbCommand").href));
	}
	return false;
}
//-----------------------------------------------------------
//--- wiki_GetFreeID	- Get ID for IFrame
//-----------------------------------------------------------
function wiki_GetFreeID(o)
{
	var win = o.ownerDocument.parentWindow;
	var doc = win.document;//ed.GetDocument();
	var ind = 1;
	var id = "iframe_";
	var cnt = 0;
	while(doc.getElementById(id+ind)!=null)
	{ 
		ind++;
		cnt++; 
		if(cnt > 1000)
		{
			alert("Too many controls on page.");
			return null;
		}
	}
	id = id + ind;
//alert(id);
	return id;
}
//-----------------------------------------------------------
//--- Load Control
//-----------------------------------------------------------
function wiki_LoadControl(o)
{
	//alert(o.outerHTML);
	
	if (o.src=="about:blank")
	{
		var id = wiki_GetFreeID(o);//o.id;
		var t = o.getAttribute("c_type");
		o.id = id;
		
		var ss = "?t=" + t + "&callerID=" + id;
		
		if (t == "SurveyEntries")
		{
		}
		
		o.src = "/Panels/Wiki/Editor/DrawControl.aspx" + ss;
	}
}
//-----------------------------------------------------------
//--- wiki_GetEditor
//-----------------------------------------------------------
function wiki_GetEditor(o)
{
	var ed = null;
	try
	{
		ed = CuteEditor_GetEditor(o);
	}
	catch(e){}
	if (ed==null)
	{
		try
		{
			ed = document.getElementById(o.getAttribute("c_editorID"));
			//var tree = document.getElementById(o.getAttribute("c_treeID"));
		}
		catch(e){}
	}
	if (ed==null)
	{
		try
		{
			var p=o;while(p.getAttribute("c_editorClientID")==null || p.getAttribute("c_editorClientID")=="")p=p.parentNode;
			ed = document.getElementById(p.getAttribute("c_editorClientID"));
		}
		catch(e){}
	}
	return ed;
}
//-----------------------------------------------------------
//--- CuteEditor_OnCustomCommand
//-----------------------------------------------------------
function CuteEditor_OnCustomCommand(o,mode)
{
	var ed = wiki_GetEditor(o);
	if (ed==null)
	{
		alert("Can not locate Editor control.");
		return;
	}
	
	//
//alert(ed==null);
	if (mode=="Properties")
	{
		var doc = ed.GetDocument();
//alert("doc: " + (doc==null));
		var sel = doc.selection.createRange();
//alert(sel==null);
//alert(typeof(sel) + "     " + sel.length);//(sel==null));
//alert(sel);//c==null);
//alert(sel.tagName);

		if (sel.length > 0)
		{
			var c = sel.item(0);
//alert(c==null);
			var t = c.getAttribute("c_type");
//var c=new Function();
//c.tagName = "IFrame";
//var t="SurveyEntries";
			
			if (c.tagName.toUpperCase()=="IFRAME" && t!=null)
			{
				var wiki = __CWiki_GetObject(o);
				var args = new Function();
				args.editor = ed;
				args.control = c;
				args.clientID = wiki.clientID;
				args.wikiID = wiki.wikiID;
				var date1 = new Date();
				var url = "/Panels/Wiki/Properties/default.aspx?_rand="+date1.getTime() + 
							//"&cid=" + wiki.clientID + 
							//"&wid=" + wiki.wikiID +
							"&ct=" + t;
				
				g_global.selectedControl = c;
				g_global.selectedEditor = ed;
				
				//...show the dialog page , and pass the editor as newwin.dialogArguments.editor
				//...(handler,url,args,feature)
				var handler = wiki_Handler_PropChanged;
				var newwin = ed.ShowDialog(handler,url,args, 
					"dialogWidth:450px; dialogHeight:500px; center:yes; edge:raised; resizable:yes; scroll:no; status:yes;");
//alert(typeof(newwin));
//alert(c.outerHTML);
			}
		}
	}
}
//-----------------------------------------------------------
//--- wiki_Handler_PropChanged
//-----------------------------------------------------------
function wiki_Handler_PropChanged(e)
{
	if (e==true)
	{
		var c = g_global.selectedControl;
		if (c.tagName.toUpperCase()=="IFRAME")
		{
			var t = c.getAttribute("c_type");
			var id = c.id;
			var arr = c.attributes;
			var dt = new Date();
			var ss = "?_d_t=" + dt.getTime() + "&t=" + t + "&callerID=" + id;
			for(var j=0; j<arr.length; j++)
			{
				var attr = arr.item(j);
				var name = attr.nodeName;
				var val = attr.nodeValue;
				if (name.indexOf("v_") == 0 && name.lastIndexOf("_display")==-1)
				{
					if (name!="v_permissions")
					{
						ss += "&" + name + "=" + escape(val);
					}
				}
			}
			c.src = "/Panels/Wiki/Editor/DrawControl.aspx" + ss;

			try
			{
				var ed = g_global.selectedEditor;
				ed.setHTML( ed.getHTML() + " ");
			}
			catch(e){}
		}
	}
}















//-----------------------------------------------------------
function wiki_InsertControl(editorClientID,ctype,width,height)
{
//alert(ctype);
	var ed = document.getElementById(editorClientID);

	var hh = height;//"100px";
	var ww = width;//"300px";
	
	var ind = 1;
	var id = "iframe_";
	var cnt = 0;
	while(document.getElementById(id+ind)!=null)
	{ 
		ind++;
		cnt++; 
		if(cnt > 1000)
		{
			alert("Too many controls on page.");
			return;
		}
	}
	id = id + ind;
	
	var src = "/Panels/Wiki/Editor/DrawControl.aspx?t=" + ctype + "&callerID=" + id;
	var ss = 
		"<iframe " + 
//				"onreadystatechange='parent.wiki_ControlStateChange(this)' " + 
//				"onload='parent.wiki_LoadControl(this)' " + 
			//"onload='parent.wiki_LoadControl))' " + 
			"id='" + id + "' " + 
//				"src='about:blank' " + 
			"src='" + src + "' " + 
			//"src='/Panels/Wiki/Editor/DrawControl.aspx?t="+t+"&callerID="+id+"' " + 
			//   /Panels/Wiki/Editor/DrawControl.aspx?t=SurveyEntries
			"c_type='"+ctype+"' " + 
			"style='width:"+ww+"; height:"+hh+";'>" + 
//				"<div c_name='222222222222'>" +
//					"<span ct='2'></span>" +
//				"</div>" + 
		"</iframe>";
			
			/*
		var ss = 
			"<div " + 
				//"onreadystatechange='wiki_LoadControl(this)' " + 
				"onload='' " + 
				"id='" + id + "' " + 
				//"src='/Panels/Wiki/Editor/DrawControl.aspx?t="+t+"&callerID="+id+"' " + 
				"c_type='"+t+"' " + 
				"style='width:"+ww+"; height:"+hh+";'>" + 
//				"<div c_name='222222222222'>" +
//					"<span ct='2'></span>" +
//				"</div>" + 
			"</div>";
			*/
//alert(ss);
// overflow:hidden; border:dashed 1px blue;

		ed.ExecCommand('PasteHTML', false, ss);
		
//alert(document.getElementById(id) == null);
}
function wiki_ControlStateChange(o)
{
	if (o!=null && o.readyState=="complete")
	{
		//alert("ok");
	}
}
function CuteEditor_DDLCommand(ddl)
{
	alert(ddl==null);//.outerHTML);//.value + "   " + ddl.selectedValue + "   " + ddl.selectedIndex);
}

/*
//-----------------------------------------------------------
function wiki_AddTopic2(o)
{
	var wiki = __CWiki_GetObject(o);
	var n = window.prompt("Enter name for new topic:", "");
	if (n!=null && n!="")
	{
		document.getElementById(wiki.clientID+"_hidValue").value = n;
		return true;
	}
	return false;
}
*/
//-----------------------------------------------------------
function wiki_SaveChanges(o)
{
	try
	{
		var wiki = __CWiki_GetObject(o);
		var div = document.getElementById(wiki.clientID+"_panView");
//alert(div.innerHTML);

		document.getElementById(wiki.clientID+"_hidValue").value = div.innerHTML;
		return true;
	}
	catch(e)
	{
		return false;
	}
}


/*
//-----------------------------------------------------------
function wiki_OnButOver(e){		wiki_Button_Set(e,"white","black","black","white")}
function wiki_OnButOut(e){		wiki_Button_Set(e,"","","","")}
function wiki_OnButDown(e){		wiki_Button_Set(e,"black","white","white","black")}
function wiki_OnButUp(e){		wiki_Button_Set(e,"white","black","black","white")}
function wiki_Button_Set(e,t,r,b,l,mode)
{
	//::::::::::::::::::::::::::::: IE, NC, FF, Opera, Safari, IE(Mac)
	var ev = (e) ? e : window.event;
	var o = (ev.srcElement) ? ev.srcElement : ev.target;
	if (o.nodeType == 3) o = o.parentNode;	// defeat Safari bug
	//::::::::::::::::::::::::::::: 
//	var p=o;while(p.className!="wiki_Toolbar")p=p.parentNode;
	var bg = o.getAttribute("c_bordercolor");
	if(o._disabled=='y')return; 
	var s=o.style; 
	s.borderTopColor	= t=="" ? bg : t; 
	s.borderLeftColor	= l=="" ? bg : l; 
	s.borderRightColor	= r=="" ? bg : r; 
	s.borderBottomColor	= b=="" ? bg : b; 
}
*/





function wiki_GetClientID(o)
{
	var p=o;while(p.getAttribute("c_clientID")==null || p.getAttribute("c_clientID")=="")p=p.parentNode;
	return p.getAttribute("c_clientID");
}


function wiki_Export(o)
{
	var clientID = wiki_GetClientID(o);
	var wikiID = $get(clientID+"_hidWikiID").value;
//alert(wikiID);
	var bh = Telerik.Web.UI.WindowBehaviors.Move + Telerik.Web.UI.WindowBehaviors.Close;
	var args = new Function();
	args.clientID = clientID;
	//args.obj = this;
	args.response = null;
	args.callback = "wiki_Exported";
	args.wikiID = wikiID;

	var url = g_global.currentProtocolAndHost + "/Panels/Wiki/Export.aspx" +
		"?sessid=" + g_global.sessionID +
		"&forcessl=y";
	g_global.OpenWindow(url, args, 700, 550, "<strong>Export all data of current wiki</strong>", bh);

}




































//-----------------------------------------------------------
//-----------------------------------------------------------
//--- __CWiki
//-----------------------------------------------------------
//-----------------------------------------------------------
function __CWiki(clientID)
{
	this.clientID	= clientID;
	this.panMain	= document.getElementById(clientID+"_panMain");
//	this.panView	= document.getElementById(clientID+"_panView");
	
	this.panMain.setAttribute("c_token", "wiki_Main");
//	this.panView.setAttribute("c_token", "wiki_View");
	
	this.panMain.__obj = this;
//	this.panView.__obj = this;
	
	this.wikiID = document.getElementById(clientID+"_hidWikiID").value;

	try
	{	
		this.toolbar	= document.getElementById(this.clientID+"_tblToolbar");
		this.toolbar.setAttribute("c_token", "wiki_Toolbar");
		this.toolbar.__obj = this;
		this.divSelCtrl	= this.toolbar.getElementsByTagName("DIV")[0];
		this.selectedControl = null;
	}
	catch(e){}
}
//-----------------------------------------------------------
function __CWiki_GetObject(o)
{
	var p=o;while(p.getAttribute("c_token")==null || p.getAttribute("c_token").indexOf("wiki_")==-1)p=p.parentNode;
	return p.__obj;
}
//-----------------------------------------------------------
__CWiki.prototype.Init=function()
{
	var mode	= document.getElementById(this.clientID+"_hidMode").value;
//alert(mode);
	//this.SetPanelEditable(mode=="Edit");

/*
	try
	{	
		var arr = this.toolbar.getElementsByTagName("IMG");
		for(var j=0; j<arr.length; j++)
		{
			var img = arr[j];
			img.style.borderWidth = "1px";
			img.style.borderStyle = "solid";
			img.style.borderColor = this.toolbar.getAttribute("c_borderColor");
			
			img.onmouseover	= __CWiki_OnButOver;
			img.onmouseout	= __CWiki_OnButOut;
			img.onmousedown	= __CWiki_OnButDown;
			img.onmouseup	= __CWiki_OnButUp;
			img.onclick		= __CWiki_ToolbarClicked;
			
			if (img.getAttribute("c_allowDrag") == "y")
			{		
				img.ondragEnter = __CWiki_OnDragEnter;//"event.returnValue=false;";
				img.ondragstart = __CWiki_OnDragStart;
				img.ondragend	= __CWiki_OnDragEnd; 
			}
		}
		this.RefreshToolbar();
	}
	catch(e){}
	*/
	
//this.container	= _container;
//this.container.setAttribute("c_token", "SmartButtons");
//this.container.c_obj = this;
//this.callBack	= _callBack;
//this.border	= _border;
//this.direction= _direction;
//this.innerTable = this.container.children[0];
}
//-----------------------------------------------------------
__CWiki.prototype.ToggleNavBar=function()
{
	var hid = document.getElementById(this.clientID+"_hidDisplayNav");
	var td = document.getElementById(this.clientID+"_tdNB");
	var img = document.getElementById(this.clientID+"_imgNB");
	
	hid.value = hid.value=="n" ? "y" : "n";
	td.style.display = hid.value=="y" ? "block" : "none";
	img.src = "/images/9x9/" + (hid.value=="y" ? "HideNav.gif" : "ShowNav.gif");
}
//-----------------------------------------------------------
/*
__CWiki.prototype.SetPanelEditable=function(isEditable)
{
	var p = this.panView;
	p.contentEditable = isEditable;
	if (isEditable)
	{
		p.onkeydown = __CWiki_OnKeyDown;
		p.onclick	= __CWiki_OnClickEditor;
	}
	else
	{
		p.onkeydown = "";
		p.onclick	= "";
	}
	p.style.backgroundImage = isEditable ? "url(/Images/editor_background.gif)" : "";
	p.style.border = isEditable ? "solid 1px #666666" : "";
	p.style.height = isEditable ? "350" : "";
}
*/
//-----------------------------------------------------------
__CWiki.prototype.SetButtonEnabled=function(token,isEnabled)
{
	var arr = this.toolbar.getElementsByTagName("IMG");
	for(var j=0; j<arr.length; j++)
	{
		var img = arr[j];
		if (token == img.getAttribute("c_token"))
		{
			var e = new Function();
			e.srcElement = img;
			//var o = this._c.all("but_"+token);
			//var dis=disabled=='y';
			if(isEnabled==false)__CWiki_OnButOut(e);
			img.setAttribute("c_enable", isEnabled ? "y" : "n");
			//o.runtimeStyle.filter = dis?"alpha(opacity=25)":"";
			img.style.filter = isEnabled ? "" : "alpha(opacity=25)";
		}
	}
}
//-----------------------------------------------------------
__CWiki.prototype.RefreshToolbar=function()
{
	var c = this.selectedControl;
	var td,tr,tbl,indCell=-1,indRow=-1;
	var isKnownTag = __CWiki_IsKnownTag(c);
	var isCell = c!=null && c.tagName=="TD";
	if (isCell)
	{
		td  = c;	while(td.tagName!="TD")		td  = td.parentNode;
		tr  = td;	while(tr.tagName!="TR")		tr  = tr.parentNode;
		tbl = tr;	while(tbl.tagName!="TABLE")	tbl = tbl.parentNode;
		indCell = td.cellIndex;
		indRow	= tr.rowIndex;
	}
//alert(isCell);
	this.SetButtonEnabled("InsLeft",	isCell);// ? "n" : "y");
	this.SetButtonEnabled("InsRight",	isCell);// ? "n" : "y");
	this.SetButtonEnabled("InsAbove",	isCell);// ? "n" : "y");
	this.SetButtonEnabled("InsBelow",	isCell);// ? "n" : "y");
	this.SetButtonEnabled("InsCell",	isCell);// ? "n" : "y");
	this.SetButtonEnabled("Up",			isCell && indRow>0);// ? "n" : "y");
	this.SetButtonEnabled("Dn",			isCell && indRow<(tbl.rows.length-1));// ? "n" : "y");
	this.SetButtonEnabled("Left",		isCell && indCell>0);// ? "n" : "y");
	this.SetButtonEnabled("Right",		isCell && indCell<(tr.cells.length-1));// ? "n" : "y");
	this.SetButtonEnabled("DelRow",		isCell);// ? "n" : "y");
	this.SetButtonEnabled("DelColumn",	isCell);// ? "n" : "y");
	this.SetButtonEnabled("DelCell",	isCell);// ? "n" : "y");
	this.SetButtonEnabled("Props",		isKnownTag);// ? "n" : "y");
//	oSBs2.SetDisabled("CleanTags",	isCell ? "n" : "y");
}
//-----------------------------------------------------------
function __CWiki_IsKnownTag(c)
{
	if (c==null)
		return false;
//	else if (c.CUSTOMTYPE!=null)
//		return true;
	else
		return c.__etype!=null;// && c.__etype!="Question";
}
//-----------------------------------------------------------
function __CWiki_OnKeyDown(e)
{
	//PageHasBeenEdited();
}
//-----------------------------------------------------------
function __CWiki_OnButOver(e){		__CWiki_Button_Set(e,"white","black","black","white")}
function __CWiki_OnButOut(e){		__CWiki_Button_Set(e,"","","","")}
function __CWiki_OnButDown(e){		__CWiki_Button_Set(e,"black","white","white","black")}
function __CWiki_OnButUp(e){		__CWiki_Button_Set(e,"white","black","black","white")}
function __CWiki_Button_Set(e,t,r,b,l,mode)
{
	//::::::::::::::::::::::::::::: IE, NC, FF, Opera, Safari, IE(Mac)
	var ev = (e) ? e : window.event;
	var o = (ev.srcElement) ? ev.srcElement : ev.target;
	if (o.nodeType == 3) o = o.parentNode;	// defeat Safari bug
	//::::::::::::::::::::::::::::: 
	var oWiki = __CWiki_GetObject(o);
	var bg = oWiki.toolbar.getAttribute("c_borderColor");
	if(o._disabled=='y')return; 
	var s=o.style; 
	s.borderTopColor	= t=="" ? bg : t; 
	s.borderLeftColor	= l=="" ? bg : l; 
	s.borderRightColor	= r=="" ? bg : r; 
	s.borderBottomColor	= b=="" ? bg : b; 
}
function __CWiki_OnDragEnter(e)
{
	//::::::::::::::::::::::::::::: IE, NC, FF, Opera, Safari, IE(Mac)
	var ev = (e) ? e : window.event;
	var o = (ev.srcElement) ? ev.srcElement : ev.target;
	if (o.nodeType == 3) o = o.parentNode;	// defeat Safari bug
	//::::::::::::::::::::::::::::: 
//alert(event.srcElement.outerHTML);
//	if (top._dragToken==null)
//		event.returnValue=false;
//alert(event.srcElement.outerHTML);

}
function __CWiki_OnDragStart(e)
{
	//::::::::::::::::::::::::::::: IE, NC, FF, Opera, Safari, IE(Mac)
	var ev = (e) ? e : window.event;
	var o = (ev.srcElement) ? ev.srcElement : ev.target;
	if (o.nodeType == 3) o = o.parentNode;	// defeat Safari bug
	//::::::::::::::::::::::::::::: 
	top._dragToken	= o.getAttribute("c_token");
}
function __CWiki_OnDragEnd(e)
{
	//::::::::::::::::::::::::::::: IE, NC, FF, Opera, Safari, IE(Mac)
	var ev = (e) ? e : window.event;
	var o = (ev.srcElement) ? ev.srcElement : ev.target;
	if (o.nodeType == 3) o = o.parentNode;	// defeat Safari bug
	//::::::::::::::::::::::::::::: 
	
	var oWiki = __CWiki_GetObject(o);
	//var tbl=o;while(tbl.tagName!="TABLE")tbl=tbl.parentNode;
	//var oWiki = tbl.__obj;
	
	//oWiki.OnDropControl();
	oWiki.OnDropControl(o);
	top._dragToken = null;
}
//---------------------------------------------------------------------
//function __CWiki_OnDrop(oSrc)
__CWiki.prototype.OnDropControl=function(ctrl)
{
	var token = ctrl.getAttribute("c_token");
	if ((
		token != "Table" && 
		token != "Label" && 
		token != "Image" && 
		token != "CustomWebPart"
		)
		|| 
		(
		top._dragToken != "Table" && 
		top._dragToken != "Label" && 
		top._dragToken != "Image" && 
		top._dragToken != "CustomWebPart"
		)) 
	{
		return false;
	}
//alert(this==null);
//alert(this.panView==null);
//alert(this.panView.outerHTML);
	var sel		= document.selection;
	var token	= top._dragToken;
	var num		= 1;
	var arr;
	if		(token == "Table"){			arr = this.panView.getElementsByTagName("TABLE");	}
	else if (token == "Label"){			arr = this.panView.getElementsByTagName("SPAN");	}
	else if (token == "Image"){			arr = this.panView.getElementsByTagName("IMG");	}
	else if (token == "CustomWebPart"){	arr = this.panView.getElementsByTagName("LABEL");	}
	for(var j=0; j<arr.length; j++)
	{
		var num2 = 1*arr[j].id.substr(arr[j].id.lastIndexOf("_")+1);
//alert(arr[j].id + "   " + num2 + "    " + (num <= num2));
		if (num <= num2)
			num = num2 + 1;
	}
	var id = "v1_" + token + "_" + num;
	
	var _html;
	if (token == "Table")
	{
		_html = "<TABLE id='"+id+"' __etype='"+token+"' border='1' style='width:100%;'></TABLE>";
	}
	else if (token == "Label")
	{
		_html = "<SPAN id='"+id+"' __etype='"+token+"' contentEditable='true'></SPAN>";
	}
	else if (token == "Image")
	{
		_html = "<IMG id='"+id+"' __etype='"+token+"'/>";
	}
	else if (token == "CustomWebPart")
	{
		_html = "<LABEL id='"+id+"' __etype='"+token+"' contentEditable='false' style='padding:2px; border:dashed 1px #000000; background-color:#ffe4c4; width:100px;'></LABEL>";
	}
	
	var item = sel.createRange().item(0);
	var c = item.insertAdjacentElement("afterEnd",document.createElement(_html));

//	var xmlProp = eval("top.GProperties.Control."+token);
//	xmlProp = xmlProp.cloneNode(true);
//	xmlProp.selectSingleNode("Properties/G/P[@n='ID']").setAttribute("v",id);

	c.oncontrolselect	= __CWiki_OnClickEditor;//__CWiki_OnSelect;

	//...init elements
	if (token=="Table")
	{
		var tr	= c.insertRow();		tr.setAttribute("__etype", "TableRow");
		var td	= tr.insertCell();		td.setAttribute("__etype", "TableCell");	td.vAlign="top";
	}
	else if (token == "CustomWebPart")
	{
		c.innerHTML = "<b>Custom&nbsp;WebPart</b>";
	}

	sel.clear();
	
	//__CWiki_OnSelect(c);
	this.SelectControl(c);

//PageHasBeenEdited();
}
//-----------------------------------------------------------
function __CWiki_OnClickEditor(e)
{
	//::::::::::::::::::::::::::::: IE, NC, FF, Opera, Safari, IE(Mac)
	var ev = (e) ? e : window.event;
	var o = (ev.srcElement) ? ev.srcElement : ev.target;
	if (o.nodeType == 3) o = o.parentNode;	// defeat Safari bug
	//::::::::::::::::::::::::::::: 
	var oWiki = __CWiki_GetObject(o);
	oWiki.SelectControl(o);
}
//-----------------------------------------------------------
function __CWiki_OnSelect(e)
{
	//::::::::::::::::::::::::::::: IE, NC, FF, Opera, Safari, IE(Mac)
	var ev = (e) ? e : window.event;
	var o = (ev.srcElement) ? ev.srcElement : ev.target;
	if (o.nodeType == 3) o = o.parentNode;	// defeat Safari bug
	//::::::::::::::::::::::::::::: 
	var oWiki = __CWiki_GetObject(o);
	oWiki.SelectControl(o);
}
//-----------------------------------------------------------
__CWiki.prototype.SelectControl=function(c)
{
	if (c==null) c = event.srcElement;
	this.RefreshSelectedElementName(c);
}
//-----------------------------------------------------------
__CWiki.prototype.RefreshSelectedElementName=function(selCtrl)
{
	var n = null;	try{ n = selCtrl.getAttribute("__etype"); }catch(e){}
	if (n==null)
	{
		n = "?";
		this.selectedControl = null;
	}
	else
	{
		this.selectedControl = selCtrl;
	}

	var o = document.createTextNode(n);
	while(this.divSelCtrl.childNodes.length > 0)this.divSelCtrl.removeChild(this.divSelCtrl.childNodes[0]);
	this.divSelCtrl.appendChild(o);
	//document.getElementById("lwiki_SelElement").innerHTML =  c==null ? "?" : c.__etype;
	//elemSelected = c==null ? null : c;
	
	//wiki_Refresh_2ndRow_Buttons();
	this.RefreshToolbar();
}
//-----------------------------------------------------------
function __CWiki_ToolbarClicked(e)//item, _sec)
{
	//::::::::::::::::::::::::::::: IE, NC, FF, Opera, Safari, IE(Mac)
	var ev = (e) ? e : window.event;
	var o = (ev.srcElement) ? ev.srcElement : ev.target;
	if (o.nodeType == 3) o = o.parentNode;	// defeat Safari bug
	//:::::::::::::::::::::::::::::
	
	if(o==null || o.getAttribute("c_enable")=="n")return; 
	
	var oWiki = __CWiki_GetObject(o);
//	_c = o;// _sec==null ? item : _sec;
//alert(_c.outerHTML);
	//while(_c.getAttribute("_CustomType")!="Toolbar")_c=_c.parentNode;
//alert(_c.outerHTML);

	var appPath = "/";//_c.getAttribute("_appPath");
	var tk		= o.getAttribute("c_token");// item.id;
	var doc		= document;
	var fr,sel,v,v1;
	var changed = true;
	
//alert(oWiki.panView==null);
	try{ oWiki.panView.focus();		}catch(e){}
	try{ oWiki.panView.setActive();	}catch(e){}
		//_c.c_divEditor.setActive();

	try{
		sel = doc.selection.createRange();
		sel.select();
	}catch(e){}
//alert(id);

	var processed = true;

//window.top.status = id;
	if		(tk=="Cut")				doc.execCommand("Cut");
	else if (tk=="Copy")			doc.execCommand("Copy");
	else if (tk=="Paste")			doc.execCommand("Paste");
	else if (tk=="Redo")			doc.execCommand("Redo");
	else if (tk=="Undo")			doc.execCommand("Undo");
	else if (tk=="Bold")			doc.execCommand("Bold");
	else if (tk=="Italic")			doc.execCommand("Italic");
	else if (tk=="Underline")		doc.execCommand("Underline");
	else if (tk=="AlignLeft")		doc.execCommand("JustifyLeft");
	else if (tk=="AlignCenter")		doc.execCommand("JustifyCenter");
	else if (tk=="AlignRight")		doc.execCommand("JustifyRight");
	else if (tk=="IncreaseIndent")	doc.execCommand("Indent");
	else if (tk=="DecreaseIndent")	doc.execCommand("Outdent");
	else if (tk=="BulList")			doc.execCommand("InsertUnorderedList");
	else if (tk=="NumList")			doc.execCommand("InsertOrderedList");
	else if (tk=="FontColor" || tk=="BackColor")
	{
		cmd = tk=="FontColor" ? "ForeColor" : "BackColor";
		v1 = __CWiki_GetColor(sel.queryCommandValue(cmd));
		v1 = __CWiki_SelectColor(v1,appPath);
		if (v1!=null)
			doc.execCommand(cmd,false,v1);
		else 
			changed = false;
	}
	else if (tk=="Font")
	{
		v1 = new __CWiki_Font();
		var fontStyle="";
		v = sel.queryCommandValue("FontName");		if (v!=false)v1.fontName=v;
		v = sel.queryCommandValue("FontSize");		if (v!=false && v!=0 && v!=null && !isNaN(v))v1.fontSize = (v*3)+"";
		v = sel.queryCommandValue("Bold");			if (v==true)fontStyle+="b";
		v = sel.queryCommandValue("Italic");		if (v==true)fontStyle+="i";
		v = sel.queryCommandValue("Underline");		if (v==true)v1.isUnderline=true;
		v = sel.queryCommandValue("StrikeThrough");	if (v==true)v1.isStrikeout=true;
		v1.foreColor = __CWiki_GetColor(sel.queryCommandValue("ForeColor"));
		v1.backColor = __CWiki_GetColor(sel.queryCommandValue("BackColor"));
		v1.fontStyle = fontStyle;
		
		v1 = __CWiki_SelectFont(v1,appPath);
		
		if (v1 != null)
		{
			doc.execCommand("FontName", false, v1.fontName);
			doc.execCommand("FontSize", false, Math.round(v1.fontSize/3)); //+v1.fontUnit);
			doc.execCommand("ForeColor", false, v1.foreColor);
			doc.execCommand("BackColor", false, v1.backColor);
			if (v1.fontStyle.indexOf("b")!=-1) doc.execCommand("Bold");
			if (v1.fontStyle.indexOf("i")!=-1) doc.execCommand("Italic");
			if (v1.isUnderline) doc.execCommand("Underline");
			//if (v1.isOverline) doc.execCommand("FormatBlock",false,"Overline");
			//doc.execCommand("Overline");
			if (v1.isStrikeout) doc.execCommand("StrikeThrough");
		}
		else
			changed = false;
	}
	else if (tk=="Smiley")
	{
		var args = new Function();
		if (showModalDialog("/Panels/SelectSmiley/default.htm", args, "font-family:Verdana; font-size:12; dialogWidth:400px; dialogHeight:400px; help:no; status:no; resizable:no;  scroll:no;" ))
		{
			var ss = "<img src='" + args.src + "' width='" + args.width + "' height='" + args.height + "' c_token='smiley'>";
			sel.pasteHTML( ss );
		}
		else
			changed = false;
	}
	else if (tk=="Image")
	{
		var args = new Function();
		var params	= "dialogWidth:700px; dialogHeight:600px; center:yes; edge:raised; resizable:yes; scroll:no; status:yes;"
		if (showModalDialog("/Panels/InsertImage/default.htm", args, params ))
		{
			sel.pasteHTML( args.html );
		}
		else
			changed = false;
	}
	else if (tk=="Hyperlink")
	{
		var href = window.prompt("Step 1 of 2 \n ENTER URL:","");
		if (href!=null && href!="")
		{
			var name = window.prompt("Step 2 of 2 \n ENTER DISPLAY TEXT:","");
			if (name!=null && name!="")
			{
				try
				{
					sel.pasteHTML( "<a href='"+href+"' __etype='HyperLink'>" + name + "</a>" );
				}
				catch(e)
				{
					alert("Error: " + ex.message);
					changed = false;
				}
			}
		}
		else
			changed = false;
	}
	else
	{
		processed = false;
	}


	if (!processed)
	{
		var c = oWiki.selectedControl;
//alert(c==null);
		if (c==null)return;

		var ss,x,ind;
		var arr,len;
		var td  = c;	while(td.tagName!="TD")		td  = td.parentNode;
		var tr  = td;	while(tr.tagName!="TR")		tr  = tr.parentNode;
		var tbl = tr;	while(tbl.tagName!="TABLE")	tbl = tbl.parentNode;
		var indCell = td.cellIndex;
		var indRow	= tr.rowIndex;
		
		processed = true;
		
		if (tk=="InsLeft" || tk=="InsRight")
		{
			__CWiki_SelectDeselect("Column",tbl,indCell,true);
			if (confirm("Do you want to insert a new Column to the " + (tk=="InsLeft"?"Left":"Right") + " of selected one?"))
			{
				//PageHasBeenEdited();
				__CWiki_SelectDeselect("Column",tbl,indCell,false);

				ind = indCell + (tk=="InsLeft" ? 0 : 1);
				arr = tbl.rows;
				for(var i1=0; i1<arr.length; i1++)
				{
					try
					{
						td	= arr[i1].insertCell(ind);
						td.__etype = "TableCell";
						td.vAlign = "top";
					}catch(e){}
				}
				//Sync();
			}
			else
				__CWiki_SelectDeselect("Column",tbl,indCell,false);
		}
		else if (tk=="InsAbove" || tk=="InsBelow")
		{
			__CWiki_SelectDeselect("Row",tbl,indRow,true);
			if (confirm("Do you want to insert a new Row " + (tk=="InsAbove"?"above":"below") + " selected one?"))
			{
				//PageHasBeenEdited();
				__CWiki_SelectDeselect("Row",tbl,indRow,false);

				ind = indRow + (tk=="InsAbove" ? 0 : 1);
				len = tr.cells.length;
				tr = tbl.insertRow(ind);
				tr.__etype = "TableRow";
				for(var i1=0; i1<len; i1++)
				{
					try
					{
						td	= tr.insertCell();
						td.__etype = "TableCell";
						td.vAlign = "top";
					}catch(e){}
				}
			}
			else
				__CWiki_SelectDeselect("Row",tbl,indRow,false);
		}
		else if (tk=="InsCell")
		{
			td = tbl.rows[indRow].cells[indCell];
			__CWiki_SelectDeselect("Cell",td,null,true);
			if (confirm("Do you want to insert a new Cell to the Left of selected one?"))
			{
				//PageHasBeenEdited();
				__CWiki_SelectDeselect("Cell",td,null,false);

				try
				{
					td	= tbl.rows[indRow].insertCell(indCell);
					td.__etype = "TableCell";
					td.vAlign = "top";
				}catch(e){}
			}
			else
				__CWiki_SelectDeselect("Cell",td,null,false);
		}
		else if (tk=="Up" || tk=="Dn")
		{
			__CWiki_SelectDeselect("Row",tbl,indRow,true);
			if (confirm("Do you want to move selected row " + (tk=="Up"?"Up":"Down") + "?"))
			{
				//PageHasBeenEdited();
				__CWiki_SelectDeselect("Row",tbl,indRow,false);
				ind = indRow + (tk=="Up" ? -1 : 1);
				tbl.moveRow(indRow,ind);
			}
			else
				__CWiki_SelectDeselect("Row",tbl,indRow,false);
		}
		else if (tk=="Left" || tk=="Right")
		{
			__CWiki_SelectDeselect("Column",tbl,indCell,true);
			if (confirm("Do you want to move selected column to the " + (tk=="Left"?"Left":"Right") + "?"))
			{
				//PageHasBeenEdited();
				__CWiki_SelectDeselect("Column",tbl,indCell,false);
				ind = indCell + (tk=="Left" ? -1 : 1);
				var where = tk=="Left" ? "BeforeBegin" : "AfterEnd";
				
				arr = tbl.rows;
				for(var i1=0; i1<arr.length; i1++)
				{
					try
					{
						td	= arr[i1].cells[indCell];
						arr[i1].cells[ind].insertAdjacentElement(where,td);
					}catch(e){}
				}
			}
			else
				__CWiki_SelectDeselect("Column",tbl,indCell,false);
		}
		else if (tk=="DelRow" || tk=="DelColumn")
		{
			if (tk=="DelColumn" && tbl.rows[0].cells.length==1){	alert("Can not delete last column");	return;	}
			else if (tk=="DelRow" && tbl.rows.length==1){			alert("Can not delete last row");		return;	}
			var what = tk=="DelRow" ? "Row" : "Column";
			var ind	 = tk=="DelRow" ? indRow : indCell;
			__CWiki_SelectDeselect(what,tbl,ind,true);
			if(confirm("Do you want do delete selected " + what + "?"))
			{
				//PageHasBeenEdited();
				if (tk=="DelColumn")
				{
					arr = tbl.rows;
					for(var i1=0; i1<arr.length; i1++)
					{
						try{	arr[i1].cells[ind].removeNode(true);	}catch(e){}
					}
				}
				else if (tk=="DelRow")
				{
					try{	tbl.rows[ind].removeNode(true);	}catch(e){}
				}
			}
			else
				__CWiki_SelectDeselect(what,tbl,ind,false);
		}
		else if (tk=="DelCell")
		{
			if (tbl.rows.length==1 && tbl.rows[0].cells.length==1){	alert("Can not delete last cell");		return;	}
			td = tbl.rows[indRow].cells[indCell];
			__CWiki_SelectDeselect("Cell",td,null,true);
			if (confirm("Do you want to delete selected Cell?"))
			{
				//PageHasBeenEdited();
				__CWiki_SelectDeselect("Cell",td,null,false);

				try
				{
					td.removeNode(true);
				}catch(e){}
			}
			else
				__CWiki_SelectDeselect("Cell",td,null,false);
		}
		else if (tk=="Props")
		{
			Layout_CallPropGrid();
		}
		else
		{
			processed = false;
		}

		if (processed)
		{
			try{	c.click();	}catch(e){}
			try{	c.focus();	}catch(e){}
		}
	}

	

	if (changed)
	{
		//try{	eval(o._cb);	}catch(e){}
	}
}
//-----------------------------------------------------------
function __CWiki_SelectDeselect(what,tbl,ind,doSelect)
{
	var rts,arr;
	if (what == "Column")
	{
		arr = tbl.rows;
		for(var i1=0; i1<arr.length; i1++)
		{
			try
			{
				rts = arr[i1].cells[ind].runtimeStyle;
				rts.backgroundColor = doSelect ? "yellow" : "";
				rts.backgroundImage	= doSelect ? "none" : "";
			}catch(e){}
		}
	}
	else if (what == "Row")
	{
		arr = tbl.rows[ind].cells;
		for(var i1=0; i1<arr.length; i1++)
		{
			rts = arr[i1].runtimeStyle;
			rts.backgroundColor = doSelect ? "yellow" : "";
			rts.backgroundImage	= doSelect ? "none" : "";
		}
	}
	else if (what == "Cell")
	{
		rts = tbl.runtimeStyle;	//...variable tbl points to the cell
		rts.backgroundColor = doSelect ? "yellow" : "";
		rts.backgroundImage	= doSelect ? "none" : "";
	}
}
//----------------------------------------------------------- 
//... __CWiki_Font
//----------------------------------------------------------- 
function __CWiki_Font()
{
	this.fontName		= "Arial";
	this.fontStyle		= "";
	this.fontSize		= "8";
	this.fontUnit		= "pt";
	this.isOverline		= false;
	this.isUnderline	= false;
	this.isStrikeout	= false;
	this.foreColor		= "#000000";
	this.backColor		= "";
}
//----------------------------------------------------------- 
//... __CWiki_GetFont
//----------------------------------------------------------- 
function __CWiki_GetFont(xProps, propFullName)
{
	this.Name = "Arial";
	this.Size = "8pt";
	this.Color = "#000000";
	this.IsBold = "n";
	this.IsItalic = "n";
	this.IsOverline = "n";
	this.IsStrikeout = "n";
	this.IsUnderline = "n";

	try{
		var a = _System_SelectSingleNode(xProps, propFullName).childNodes;
		for (var j=0; j<a.length; j++)
		{
			var o=a[j];
			var n=o.getAttribute("name");
			if		(n=="Name")			this.Name		= _System_GetTagAttrValue(o,"value",	"Arial");
			else if (n=="Size")			this.Size		= _System_GetTagAttrValue(o,"value",	"8pt");
			else if (n=="Color")		this.Color		= _System_GetTagAttrValue(o,"value",	"#000000");
			else if (n=="Bold")			this.IsBold		= _System_GetTagAttrValue(o,"checked",	"n");
			else if (n=="Italic")		this.IsItalic	= _System_GetTagAttrValue(o,"checked",	"n");
			else if (n=="Overline")		this.IsOverline = _System_GetTagAttrValue(o,"checked",	"n");
			else if (n=="Strikeout")	this.IsStrikeout= _System_GetTagAttrValue(o,"checked",	"n");
			else if (n=="Underline")	this.IsUnderline= _System_GetTagAttrValue(o,"checked",	"n");
		}
	}catch(e){}

	var ss = ";FONT-FAMILY:" + this.Name + ";FONT-SIZE:" + this.Size + ";COLOR:" + this.Color;
	if (this.IsBold=="y") ss+=";FONT-WEIGHT:bold";
	if (this.IsItalic=="y") ss+=";FONT-STYLE:italic";
	var s1= (this.IsUnderline=="y"?" underline":"") + (this.IsOverline=="y"?" overline":"") + (this.IsStrikeout=="y"?" line-through":"");
	ss+=";TEXT-DECORATION:" + (s1==""?"none":s1);
	this.style = ss;
}
//----------------------------------------------------------- 
//... __CWiki_SelectColor
//----------------------------------------------------------- 
function __CWiki_SelectColor(val)
{
	var args = new Function();
	args.color = val;
	return showModalDialog("/Panels/SelectColor/SelectColor.aspx", args, "dialogWidth:520px; dialogHeight:300px; help:no; status:no; resizable:no;  scroll:no;" );
}
//----------------------------------------------------------- 
//... __CWiki_SelectFont
//----------------------------------------------------------- 
function __CWiki_SelectFont(val)
{
	return showModalDialog("/Panels/SelectFont/SelectFont.aspx", val, "font-family:Verdana; font-size:12; dialogWidth:350px; dialogHeight:320px; help:no; status:no; resizable:no" );
}
//----------------------------------------------------------- 
//... __CWiki_GetColor
//----------------------------------------------------------- 
function __CWiki_GetColor(v1)
{
	v1=v1.toString(16);
	while(v1.length<6)v1="0"+v1;
	var v2="";
	for(var i1=0;i1<v1.length;i1++)v2=v1.charAt(i1)+v2;
	return v2;
}






































function _SB_Click(o)
{		
	var _c=o;while(_c._type!="SmartButtons")_c=_c.parentElement;
	eval(_c._cb+"(o)");
}
//-----------------------------------------------------------
//function PageHasBeenEdited()
//{
//	top.EnableDisableButton(top.butSave,true);
//}


var _oTbr;
var elemSelected;
//---------------------------------------------------------------------
//function wiki_Object(divEditor, panXmlToolbar, divToolbar)
//{
//}
//---------------------------------------------------------------------
function RefreshLayout()
{
	var divEditor = document.getElementById("divwiki_Body");	
	var panXmlToolbar = document.getElementById("panXmlToolbar");
	var divToolbar	= document.getElementById("divwiki_Toolbar");
	divToolbar.c_divEditor = divEditor;
	_oTbr = new _TToolbar2( "/", divToolbar, panXmlToolbar.childNodes[0], "__HtmlEditor_ToolbarClicked" );
	_oTbr.Draw();
	_oTbr.Refresh();
	document.getElementById("tblwiki_Toolbar2").style.width = _oTbr.container.style.width;
	document.getElementById("tdwiki_Body").style.width = _oTbr.container.style.width;

	oSBs2 = new top._SmartButtonsH(document.getElementById("tdLayoutButs"),"top.wRight.wiki_NavBarClick");
	oSBs2.Add("InsLeft",	"Insert column to the left of selected one", "/Images/23x22/InsLeft.gif");
	oSBs2.Add("InsRight",	"Insert column to the right of selected one", "/Images/23x22/InsRight.gif");
	oSBs2.Add("InsAbove",	"Insert row above selected one", "/Images/23x22/InsAbove.gif");
	oSBs2.Add("InsBelow",	"Insert row below selected one", "/Images/23x22/InsBelow.gif");
	oSBs2.Add("InsCell",	"Insert cell to the left of selected one", "/Images/23x22/InsCell.gif");
	oSBs2.Add("");
	oSBs2.Add("Up",			"Move selected row up", "/Images/23x22/ArrUpA.gif");
	oSBs2.Add("Dn",			"Move selected row down", "../../Images/23x22/ArrDownA.gif");
	oSBs2.Add("Left",		"Move selected column left", "../../Images/23x22/ArrLeft.gif");
	oSBs2.Add("Right",		"Move selected column right", "../../Images/23x22/ArrRight.gif");
	oSBs2.Add("");
	oSBs2.Add("DelRow",		"Delete row", "../../Images/23x22/DelRow.gif");
	oSBs2.Add("DelColumn",	"Delete column", "../../Images/23x22/DelColumn.gif");
	oSBs2.Add("DelCell",	"Delete cell", "../../Images/23x22/DelCell.gif");
	oSBs2.Add("");
	oSBs2.Add("Props",		"Open properties window for selected element", "../../Images/23x22/Properties2.gif");

	elemSelected = null;
	wiki_Refresh_2ndRow_Buttons();
	
	wiki_RefreshLayout();
	
//	if (xPQ.nodeName=="Survey")
//	{
//		var divEditor = document.getElementById("divwiki_Body");	
//		divEditor.innerHTML = "ok";
//	}
//	else
//	{
//		wiki_RefreshLayout();
//	}

}
//-----------------------------------------------------------
function wiki_GetCDATA()
{
//	xPage=xPQ;while(xPage.nodeName!="Page")xPage=xPage.parentNode;
	var xAllLayouts = xPQ.selectSingleNode("Layouts");
	if(xAllLayouts==null){	xAllLayouts = parent.__AddXmlNode(xPQ,"","Layouts");}
	
	var xLayout = xPQ.selectSingleNode("Layout");
	
	if (xLayout!=null)
	{
		xAllLayouts.appendChild(xLayout);
	}
	else
	{
	}
	var arr = xAllLayouts.selectNodes("Layout");
	if (arr.length==0)
	{
		xLayout = parent.__AddXmlNode(xAllLayouts,"","Layout");
		arr = xAllLayouts.selectNodes("Layout");
	}
	xLayout = arr[0];
	
	if (xLayout.getAttribute("ver") != "1.0" 
		|| xLayout.childNodes.length == 0 
		|| xLayout.childNodes[0].nodeTypeString != "cdatasection")
	{
		xLayout.setAttribute("ver", "1.0");
		while(xLayout.childNodes.length>0)xLayout.removeChild(xLayout.childNodes[0]);
		parent.__AddXmlNode(xLayout,"cdata","","");
	}
	return xLayout.childNodes[0];
}
//-----------------------------------------------------------
function wiki_StoreData()
{
	//...clean temporal attributes
	wiki_CleanAttributes();
	//...store layout in CDATA section
//alert(xPQ.nodeName + "\n" + document.getElementById("divwiki_Body").innerHTML);
	var xCDATA = wiki_GetCDATA();
	xCDATA.nodeValue = document.getElementById("divwiki_Body").innerHTML;
//alert("1.\n" + xPQ.nodeName + "\n" + xCDATA.nodeValue);
	//...restore view (with temporal attributes)
	wiki_RefreshLayout();
}
//-----------------------------------------------------------
function wiki_CleanAttributes()
{
	var arr = document.getElementById("divwiki_Body").all;
	for(var j=0; j<arr.length; j++)
	{
		var c=arr[j];
		//var etype = c.getAttribute("__etype");
		if (c.tagName=="TABLE")
		{
			try{c.removeAttribute("__etype");}catch(e){}
			try{c.removeAttribute("border");}catch(e){}
		}
		else if (c.tagName=="TR")
		{
			try{c.removeAttribute("__etype");}catch(e){}
		}
		else if (c.tagName=="TD")
		{
			try{c.removeAttribute("__etype");}catch(e){}
		}
		
		try{c.removeAttribute("contentEditable");}catch(e){}
	}
}
//-----------------------------------------------------------
function wiki_RefreshLayout()
{
	var xCDATA = wiki_GetCDATA();
	var divEditor = document.getElementById("divwiki_Body");	
	divEditor.innerHTML = xCDATA.nodeValue;
//alert("2.\n" + xPQ.nodeName + "\n" + xCDATA.nodeValue);

	if (xPQ.nodeName=="Survey")
	{
		//...make sure placeholder for Page is in editor
		var div = divEditor.all("v1_page");
		if (div == null)
		{
			var ss ="<DIV id='v1_page' __etype='Page' contentEditable='false' " + 
					"class='Layout-Question' ondragEnter='event.returnValue=false;'>" + 
					"&nbsp;<br><br>Placeholder for Page<br><br>&nbsp;" + 
				"</DIV>";
			divEditor.insertAdjacentHTML("beforeEnd",ss);
			div = divEditor.all("v1_page");
		}
		div.contentEditable = false;
	}
	else if (xPQ.nodeName=="Page")
	{
		//...make sure each question is presented in editor
		var arrQ = xPQ.selectNodes("Questions/Q");
		for(var i1=0; i1<arrQ.length; i1++)
		{
			var xQ = arrQ[i1];
			var guidQ = xQ.getAttribute("guid");
			var div = divEditor.all("v1_qst"+guidQ);
			if (div == null)
			{
				var name = parent.GetQuestionName(xQ);
				var ss ="<DIV id='v1_qst"+guidQ+"' __etype='Question' _guid='" + guidQ + "' " + 
						"class='Layout-Question' ondragEnter='event.returnValue=false;'>" + 
						name + 
					"</DIV>";
				divEditor.insertAdjacentHTML("beforeEnd",ss);
				div = divEditor.all("v1_qst"+guidQ);
			}
			div.contentEditable = false;
		}
	}
	//...check HTML tags
	var arr = divEditor.all;
	for(var j=0; j<arr.length; j++)
	{
		var c=arr[j];
		if (c.tagName=="DIV" && c.getAttribute("__etype")=="Question")
		{
			//...make sure all removed questions are not in editor panel
			if (xPQ.selectSingleNode("Questions/Q[@guid='" + c._guid + "']") == null)
			{
				c.removeNode(true);
			}
		}
		else if (c.tagName=="TABLE"){	c.__etype = "Table";	c.border="1";	}
		else if (c.tagName=="TR"){		c.__etype = "TableRow";		}
		else if (c.tagName=="TD"){		c.__etype = "TableCell";	}
		else if (c.tagName=="IMG"){		c.__etype = "Image";	}
		//else if (c.tagName=="LABEL"){	c.__etype = "CustomWebPart";}
	}
}
//-----------------------------------------------------------
function wiki_Refresh_2ndRow_Buttons()
{
	var c = elemSelected;
	var td,tr,tbl,indCell=-1,indRow=-1;
	var isKnownTag = wiki_IsKnownTag(c);
	var isCell = c!=null && c.tagName=="TD";
	if (isCell)
	{
		td  = c;	while(td.tagName!="TD")		td  = td.parentElement;
		tr  = td;	while(tr.tagName!="TR")		tr  = tr.parentElement;
		tbl = tr;	while(tbl.tagName!="TABLE")	tbl = tbl.parentElement;
		indCell = td.cellIndex;
		indRow	= tr.rowIndex;
	}

	oSBs2.SetDisabled("InsLeft",	isCell ? "n" : "y");
	oSBs2.SetDisabled("InsRight",	isCell ? "n" : "y");
	oSBs2.SetDisabled("InsAbove",	isCell ? "n" : "y");
	oSBs2.SetDisabled("InsBelow",	isCell ? "n" : "y");
	oSBs2.SetDisabled("InsCell",	isCell ? "n" : "y");
	oSBs2.SetDisabled("Up",			isCell && indRow>0 ? "n" : "y");
	oSBs2.SetDisabled("Dn",			isCell && indRow<(tbl.rows.length-1) ? "n" : "y");
	oSBs2.SetDisabled("Left",		isCell && indCell>0 ? "n" : "y");
	oSBs2.SetDisabled("Right",		isCell && indCell<(tr.cells.length-1) ? "n" : "y");
	oSBs2.SetDisabled("DelRow",		isCell ? "n" : "y");
	oSBs2.SetDisabled("DelColumn",	isCell ? "n" : "y");
	oSBs2.SetDisabled("DelCell",	isCell ? "n" : "y");
	oSBs2.SetDisabled("Props",		isKnownTag ? "n" : "y");
//	oSBs2.SetDisabled("CleanTags",	isCell ? "n" : "y");

//alert(1);
//	_oTbr.container.style.width = "100%";
//	_oTbr.Draw();
//alert(2);
}
//-----------------------------------------------------------
function wiki_IsKnownTag(c)
{
	if (c==null)
		return false;
//	else if (c.CUSTOMTYPE!=null)
//		return true;
	else
		return c.__etype!=null && c.__etype!="Question";
}
//-----------------------------------------------------------
function wiki_NavBarClick(o)
{
	event.returnValue = false;
	event.cancelBubble = true;

	if(o._disabled=='y')return;
	var c = elemSelected;
	if (c==null)return;
	
//	Sync();
//alert(c.outerHTML);

	var ss,x,ind;
	var arr,len;
	var td  = c;	while(td.tagName!="TD")		td  = td.parentElement;
	var tr  = td;	while(tr.tagName!="TR")		tr  = tr.parentElement;
	var tbl = tr;	while(tbl.tagName!="TABLE")	tbl = tbl.parentElement;
	var indCell = td.cellIndex;
	var indRow	= tr.rowIndex;
	
	if (o._t=="InsLeft" || o._t=="InsRight")
	{
		wiki_SelectDeselect("Column",tbl,indCell,true);
		if (confirm("Do you want to insert a new Column to the " + (o._t=="InsLeft"?"Left":"Right") + " of selected one?"))
		{
			//PageHasBeenEdited();
			wiki_SelectDeselect("Column",tbl,indCell,false);

			ind = indCell + (o._t=="InsLeft" ? 0 : 1);
			arr = tbl.rows;
			for(var i1=0; i1<arr.length; i1++)
			{
				try
				{
					td	= arr[i1].insertCell(ind);
					td.__etype = "TableCell";
					td.vAlign = "top";
				}catch(e){}
			}
			//Sync();
		}
		else
			wiki_SelectDeselect("Column",tbl,indCell,false);
	}
	else if (o._t=="InsAbove" || o._t=="InsBelow")
	{
		wiki_SelectDeselect("Row",tbl,indRow,true);
		if (confirm("Do you want to insert a new Row " + (o._t=="InsAbove"?"above":"below") + " selected one?"))
		{
			//PageHasBeenEdited();
			wiki_SelectDeselect("Row",tbl,indRow,false);

			ind = indRow + (o._t=="InsAbove" ? 0 : 1);
			len = tr.cells.length;
			tr = tbl.insertRow(ind);
			tr.__etype = "TableRow";
			for(var i1=0; i1<len; i1++)
			{
				try
				{
					td	= tr.insertCell();
					td.__etype = "TableCell";
					td.vAlign = "top";
				}catch(e){}
			}
		}
		else
			wiki_SelectDeselect("Row",tbl,indRow,false);
	}
	if (o._t=="InsCell")
	{
		td = tbl.rows[indRow].cells[indCell];
		wiki_SelectDeselect("Cell",td,null,true);
		if (confirm("Do you want to insert a new Cell to the Left of selected one?"))
		{
			//PageHasBeenEdited();
			wiki_SelectDeselect("Cell",td,null,false);

			try
			{
				td	= tbl.rows[indRow].insertCell(indCell);
				td.__etype = "TableCell";
				td.vAlign = "top";
			}catch(e){}
		}
		else
			wiki_SelectDeselect("Cell",td,null,false);
	}
	else if (o._t=="Up" || o._t=="Dn")
	{
		wiki_SelectDeselect("Row",tbl,indRow,true);
		if (confirm("Do you want to move selected row " + (o._t=="Up"?"Up":"Down") + "?"))
		{
			//PageHasBeenEdited();
			wiki_SelectDeselect("Row",tbl,indRow,false);
			ind = indRow + (o._t=="Up" ? -1 : 1);
			tbl.moveRow(indRow,ind);
		}
		else
			wiki_SelectDeselect("Row",tbl,indRow,false);
	}
	else if (o._t=="Left" || o._t=="Right")
	{
		wiki_SelectDeselect("Column",tbl,indCell,true);
		if (confirm("Do you want to move selected column to the " + (o._t=="Left"?"Left":"Right") + "?"))
		{
			//PageHasBeenEdited();
			wiki_SelectDeselect("Column",tbl,indCell,false);
			ind = indCell + (o._t=="Left" ? -1 : 1);
			var where = o._t=="Left" ? "BeforeBegin" : "AfterEnd";
			
			arr = tbl.rows;
			for(var i1=0; i1<arr.length; i1++)
			{
				try
				{
					td	= arr[i1].cells[indCell];
					arr[i1].cells[ind].insertAdjacentElement(where,td);
				}catch(e){}
			}
		}
		else
			wiki_SelectDeselect("Column",tbl,indCell,false);
	}
	else if (o._t=="DelRow" || o._t=="DelColumn")
	{
		if (o._t=="DelColumn" && tbl.rows[0].cells.length==1){	alert("Can not delete last column");	return;	}
		else if (o._t=="DelRow" && tbl.rows.length==1){			alert("Can not delete last row");		return;	}
		var what = o._t=="DelRow" ? "Row" : "Column";
		var ind	 = o._t=="DelRow" ? indRow : indCell;
		wiki_SelectDeselect(what,tbl,ind,true);
		if(confirm("Do you want do delete selected " + what + "?"))
		{
			//PageHasBeenEdited();
			if (o._t=="DelColumn")
			{
				arr = tbl.rows;
				for(var i1=0; i1<arr.length; i1++)
				{
					try{	arr[i1].cells[ind].removeNode(true);	}catch(e){}
				}
			}
			else if (o._t=="DelRow")
			{
				try{	tbl.rows[ind].removeNode(true);	}catch(e){}
			}
		}
		else
			wiki_SelectDeselect(what,tbl,ind,false);
	}
	else if (o._t=="DelCell")
	{
		if (tbl.rows.length==1 && tbl.rows[0].cells.length==1){	alert("Can not delete last cell");		return;	}
		td = tbl.rows[indRow].cells[indCell];
		wiki_SelectDeselect("Cell",td,null,true);
		if (confirm("Do you want to delete selected Cell?"))
		{
			//PageHasBeenEdited();
			wiki_SelectDeselect("Cell",td,null,false);

			try
			{
				td.removeNode(true);
			}catch(e){}
		}
		else
			wiki_SelectDeselect("Cell",td,null,false);
	}
	else if (o._t=="Props")
	{
		wiki_CallPropGrid();
	}
}
//-----------------------------------------------------------
function wiki_SelectDeselect(what,tbl,ind,doSelect)
{
	var rts,arr;
	if (what == "Column")
	{
		arr = tbl.rows;
		for(var i1=0; i1<arr.length; i1++)
		{
			try
			{
				rts = arr[i1].cells[ind].runtimeStyle;
				rts.backgroundColor = doSelect ? "yellow" : "";
				rts.backgroundImage	= doSelect ? "none" : "";
			}catch(e){}
		}
	}
	else if (what == "Row")
	{
		arr = tbl.rows[ind].cells;
		for(var i1=0; i1<arr.length; i1++)
		{
			rts = arr[i1].runtimeStyle;
			rts.backgroundColor = doSelect ? "yellow" : "";
			rts.backgroundImage	= doSelect ? "none" : "";
		}
	}
	else if (what == "Cell")
	{
		rts = tbl.runtimeStyle;	//...variable tbl points to the cell
		rts.backgroundColor = doSelect ? "yellow" : "";
		rts.backgroundImage	= doSelect ? "none" : "";
	}
}
//---------------------------------------------------------------------
function wiki_OnDrop()
{
	if ((
		event.srcElement.id != "imgwiki_Table" && 
		event.srcElement.id != "imgwiki_Label" && 
		event.srcElement.id != "imgwiki_Image" && 
		event.srcElement.id != "imgwiki_CustomWebPart"
		)
		|| 
		(
		top._dragToken != "Table" && 
		top._dragToken != "Label" && 
		top._dragToken != "Image" && 
		top._dragToken != "CustomWebPart"
		)) 
	{
		return false;
	}
	
	var sel		= document.selection;
	var token	= top._dragToken;
	var num=1;while(document.getElementById("v1_"+token+""+num)!=null){ num++; }
	var id = "v1_"+token+""+num;
	
	var _html;
	if (token == "Table")
	{
		_html = "<TABLE id='"+id+"' __etype='"+token+"' border='1' style='width:100%;'></TABLE>";
	}
	else if (token == "Label")
	{
		_html = "<SPAN id='"+id+"' __etype='"+token+"' contentEditable='true'></SPAN>";
	}
	else if (token == "Image")
	{
		_html = "<IMG id='"+id+"' __etype='"+token+"'/>";
	}
	else if (token == "CustomWebPart")
	{
		_html = "<LABEL id='"+id+"' __etype='"+token+"' contentEditable='false' style='padding:2px; border:dashed 1px #000000; background-color:#ffe4c4; width:100px;'></LABEL>";
	}
	
	var item = sel.createRange().item(0);
	var c = item.insertAdjacentElement("afterEnd",document.createElement(_html));

//	var xmlProp = eval("top.GProperties.Control."+token);
//	xmlProp = xmlProp.cloneNode(true);
//	xmlProp.selectSingleNode("Properties/G/P[@n='ID']").setAttribute("v",id);

	c.oncontrolselect	= wiki_OnSelect;

	//...init elements
	if (token=="Table")
	{
		var tr	= c.insertRow();		tr.setAttribute("__etype", "TableRow");
		var td	= tr.insertCell();		td.setAttribute("__etype", "TableCell");	td.vAlign="top";
	}
	else if (token == "CustomWebPart")
	{
		c.innerHTML = "<b>Custom&nbsp;WebPart</b>";
	}

	sel.clear();
	
	wiki_OnSelect(c);

//PageHasBeenEdited();
}
//-----------------------------------------------------------
function wiki_OnDragEnter()
{
//alert(event.srcElement.outerHTML);
//	if (top._dragToken==null)
//		event.returnValue=false;
//alert(event.srcElement.outerHTML);
}
//-----------------------------------------------------------
function wiki_OnSelect(c)
{
	if (c==null) c = event.srcElement;
	wiki_RefreshSelectedElementName(c);
}
//-----------------------------------------------------------
function wiki_RefreshSelectedElementName(c)
{
	document.getElementById("lwiki_SelElement").innerHTML =  c==null ? "?" : c.__etype;
	elemSelected = c==null ? null : c;
	wiki_Refresh_2ndRow_Buttons();
}
//---------------------------------------------------------------------
function wiki_OnDragStart(){ top._dragToken	= event.srcElement.c_token;	}
//---------------------------------------------------------------------
function wiki_OnDragEnd()
{
	wiki_OnDrop();
	top._dragToken = null;
}
//-----------------------------------------------------------
function wiki_MouseUp()
{
	wiki_AnyClick();
}
//-----------------------------------------------------------
function wiki_AnyClick()
{
	var sel = document.selection;
	if (sel!=null && sel.type=="Control")
	{
		sel=sel.createRange();
		var c = sel.item(0);
		wiki_RefreshSelectedElementName(c);
	}
	else
	{
		var c = event.srcElement;
		try
		{
			while (!wiki_IsKnownTag(c)){	c=c.parentElement;	}
		}
		catch(e){ c = null; }
	
		wiki_RefreshSelectedElementName(c);
	}
}

//---------------------------------------------------------------------
function wiki_OnKeyDown()
{
return true;
//	event.returnValue = false;
//	event.cancelBubble = true;
//	return false;
}
//---------------------------------------------------------------------
function wiki_OnTblSelect()
{
//alert(xPQ.ownerDocument.documentElement.xml);
	var td=event.srcElement;while(td.tagName!="TD")td=td.parentElement;
	var tr=td;while(tr.tagName!="TR")tr=tr.parentElement;
	var tbl,ind,arr,o,o2,rts,s;
	var isRow = td._t.indexOf(".R")!=-1;
	
	//...
//	if (td._t.indexOf("InputTable.")!=-1)
//		tbl = _panInputTable.all("divTable").children[0];
//	else if (td._t.indexOf("Layout.")!=-1)

	tbl = _panLayout.children[0];

	//...unselect previous row/column
	if (tbl._selType == "Row")
	{
		o2 = tbl.rows[tbl._selInd];
		arr=o2.cells;
		for(var i1=1; i1<arr.length; i1++){	rts=arr[i1].runtimeStyle; rts.backgroundColor=""; rts.backgroundImage="";	}
	}
	else if (tbl._selType == "Column")
	{
		arr=tbl.rows;
		for(var i1=1; i1<arr.length; i1++)
		{
			try
			{
				rts=arr[i1].cells[tbl._selInd].runtimeStyle; rts.backgroundColor=""; rts.backgroundImage="";
			}catch(e){}}
	}
	
	//...
	ind = isRow ? tr.rowIndex : td.cellIndex;
	
	//...
	if (((tbl._selType=="Row" && isRow) || (tbl._selType=="Column" && !isRow)) && ind==tbl._selInd)
	{
		tbl._selType = "";
		tbl._selInd = "";
		
//		if (td._t.indexOf("InputTable.")!=-1)
//			_InputTable_RefreshNavBar();
		//else if (td._t.indexOf("Layout.")!=-1)
		
		return;
	}
	
	//...
	tbl._selInd = ind;
	tbl._selType = isRow?"Row":"Column";

	//...
	if (isRow)
	{
		o2 = tbl.rows[tbl._selInd];
		arr=o2.cells;
		for(var i1=1; i1<arr.length; i1++)
		{
			rts = arr[i1].runtimeStyle;
			//rts.backgroundImage = "url(none)";
			rts.backgroundColor = "yellow";
			rts.backgroundImage="none";
		}
	}
	else
	{
		arr=tbl.rows;
		for(var i1=1; i1<arr.length; i1++)
		{
			try
			{
				o = arr[i1].cells[tbl._selInd]; 
				rts = o.runtimeStyle;
				rts.backgroundColor = "yellow";
				rts.backgroundImage="none";
//				s = o.Style;
//				o._backgroundImage = s.backgroundImage;
//				s.backgroundImage = "";
				//rts.backgroundImage="url(none)"; 
//				rts.backgroundColor = "yellow";
			}catch(e){}
		}
	}

	//...
//	if (td._t.indexOf("InputTable.")!=-1)
//		_InputTable_RefreshNavBar();
	//else if (td._t.indexOf("Layout.")!=-1)
}

//-----------------------------------------------------------
function wiki_CallPropGrid(elem)
{
//alert(1);
	if (elem!=null || elemSelected!=null)
	{
		var v;
		var c = elem;
		if (c==null) c = elemSelected;
		
		var token = c.__etype;

//alert(token + "      " + c.outerHTML);
		var etype = c.__etype;
		
		var args = new Function();
		
//		args.xmlStr = c.CUSTOMPROPERTIES;
		var xRootSync = eval("top.GProperties."+token+".cloneNode(true)");
		var xRoot = xRootSync.cloneNode(true);
		var xProp = xRoot.selectSingleNode("Properties");
		
		//...Background
		try{xProp.selectSingleNode("G/P[@n='Background']/P[@n='Color']").setAttribute("v", c.style.backgroundColor);}catch(e){}
		try{xProp.selectSingleNode("G/P[@n='Background']/P[@n='Image']").setAttribute("v", GetValue_Image(c.style.backgroundImage));}catch(e){}
		
		//...Border
		try{xProp.selectSingleNode("G/P[@n='Borders']/P[@n='Color']").setAttribute("v", c.style.borderColor);}catch(e){}
		try{xProp.selectSingleNode("G/P[@n='Borders']/P[@n='Width']").setAttribute("v", c.style.borderWidth);}catch(e){}
		try{xProp.selectSingleNode("G/P[@n='Borders']/P[@n='Style']").setAttribute("v", c.style.borderStyle);}catch(e){}
		wiki_RestoreSideBorder(c,xProp,"Right");
		wiki_RestoreSideBorder(c,xProp,"Left");
		wiki_RestoreSideBorder(c,xProp,"Top");
		wiki_RestoreSideBorder(c,xProp,"Bottom");
		
		//...Font
		try{xProp.selectSingleNode("G/P[@n='Font']/P[@n='Name']").setAttribute("v",	c.style.fontFamily);}catch(e){}
		try{xProp.selectSingleNode("G/P[@n='Font']/P[@n='Size']").setAttribute("v",	c.style.fontSize);}catch(e){}
		try{xProp.selectSingleNode("G/P[@n='Font']/P[@n='Color']").setAttribute("v",c.style.color);}catch(e){}
		try{xProp.selectSingleNode("G/P[@n='Font']/P[@n='Bold']").setAttribute("c",		c.style.fontWeight=="bold"?"y":"n");}catch(e){}
		try{xProp.selectSingleNode("G/P[@n='Font']/P[@n='Italic']").setAttribute("c",	c.style.fontStyle=="italic"?"y":"n");}catch(e){}
		try{xProp.selectSingleNode("G/P[@n='Font']/P[@n='Overline']").setAttribute("c", c.style.textDecorationOverline==true?"y":"n");}catch(e){}
		try{xProp.selectSingleNode("G/P[@n='Font']/P[@n='Underline']").setAttribute("c",c.style.textDecorationUnderline==true?"y":"n");}catch(e){}
		try{xProp.selectSingleNode("G/P[@n='Font']/P[@n='Strikeout']").setAttribute("c",c.style.textDecorationLineThrough==true?"y":"n");}catch(e){}

		//...other common settings
		try{	xProp.selectSingleNode("G/P[@n='ID']").setAttribute("v",c.id);	}catch(e){}
		if (etype!="Image")
		{
			try{xProp.selectSingleNode("G/P[@n='Width']").setAttribute("v",c.style.width);	}catch(e){}
			try{xProp.selectSingleNode("G/P[@n='Height']").setAttribute("v",c.style.height);}catch(e){}		
		}
		try{xProp.selectSingleNode("G/P[@n='HorizontalAlign']").setAttribute("v",	c.align);}catch(e){}
		try{xProp.selectSingleNode("G/P[@n='VerticalAlign']").setAttribute("v",		c.vAlign);}catch(e){}
		try{xProp.selectSingleNode("G/P[@n='ToolTip']").setAttribute("v",			escape(c.title));}catch(e){}
		
		//......................................
		//...SPECIFIC settings
		//......................................
		if (etype=="Label" || etype=="HyperLink")
		{
			try
			{
				v = c.innerHTML;
				while(v.indexOf("&nbsp;")!=-1)v=v.replace("&nbsp;"," ");
				xProp.selectSingleNode("G/P[@n='Text']").setAttribute("v",escape(v));
			}catch(e){}
			
			if (etype=="HyperLink")
			{
				try{xProp.selectSingleNode("G/P[@n='NavigateURL']").setAttribute("v",wiki_GetValue_HRef(c.href));	}catch(e){}
				try{xProp.selectSingleNode("G/P[@n='Target']").setAttribute("v", (c.target=="_blank" ? "OpenNewWindow" : "UseSameWindow"));	}catch(e){}
			}
		}
		else if (etype=="Table")
		{
			try{xProp.selectSingleNode("G/P[@n='CellPadding']").setAttribute("v",c.cellPadding);	}catch(e){}
			try{xProp.selectSingleNode("G/P[@n='CellSpacing']").setAttribute("v",c.cellSpacing);}catch(e){}
		}
		else if (etype=="TableCell")
		{
			try{xProp.selectSingleNode("G/P[@n='ColumnSpan']").setAttribute("v",		c.colSpan);}catch(e){}
			try{xProp.selectSingleNode("G/P[@n='RowSpan']").setAttribute("v",			c.rowSpan);}catch(e){}
			try{xProp.selectSingleNode("G/P[@n='Wrap']").setAttribute("c",				c.noWrap==false?"y":"n");}catch(e){}
		}
		else if (etype=="Image")
		{
			try{xProp.selectSingleNode("G/P[@n='ImageAlign']").setAttribute("v",		c.align);}catch(e){}
			try{xProp.selectSingleNode("G/P[@n='AlternateText']").setAttribute("v",		escape(c.alt));}catch(e){}
			try{xProp.selectSingleNode("G/P[@n='HorizontalMargin']").setAttribute("v",	c.hspace);}catch(e){}
			try{xProp.selectSingleNode("G/P[@n='VerticalMargin']").setAttribute("v",	c.vspace);}catch(e){}
			try{xProp.selectSingleNode("G/P[@n='ImageUrl']").setAttribute("v",			wiki_GetValue_HRef(c.src));}catch(e){}
			try{v=c.style.width; if(v==null || v=="")v=c.width; if(v!=null)xProp.selectSingleNode("G/P[@n='Width']").setAttribute("v",v);}catch(e){}
			try{v=c.style.height;if(v==null || v=="")v=c.height;if(v!=null)xProp.selectSingleNode("G/P[@n='Height']").setAttribute("v",v);}catch(e){}
		}
		else if (etype=="CustomWebPart")
		{
			v = c.getAttribute("c_FileName");if(v==null)v="";
			try{xProp.selectSingleNode("G/P[@n='FileName']").setAttribute("v", v);}catch(e){}
		}
		
		args.syncXml = xRootSync;
		args.xmlStr = xRoot.xml;
		args.elemType = "Control";
		args.elemToken = token;
		if (window.showModalDialog("/Panels/Properties/default.aspx", args, 'dialogWidth:450px; dialogHeight:500px; center:yes; edge:raised; resizable:yes; scroll:yes; status:yes;'))
		{
			//PageHasBeenEdited();
			
			wiki_SetPropertiesAndRefreshControlView( c, args.xRoot);
		}
	}
}
//-----------------------------------------------------------
function wiki_SetPropertiesAndRefreshControlView( c, xRoot )
{
	var xProp = xRoot.selectSingleNode("Properties");
	var v;
	var etype = c.__etype;

	
	if (	etype=="Label"
		||	etype=="HyperLink"
		||	etype=="Table"
		||	etype=="TableCell"
		||	etype=="Image" 
		)
	{
		//...Background
		v="";try{v = xProp.selectSingleNode("G/P[@n='Background']/P[@n='Color']").getAttribute("v");}catch(e){}	c.style.backgroundColor	= v;	
		v="";try{v = xProp.selectSingleNode("G/P[@n='Background']/P[@n='Image']").getAttribute("v");}catch(e){}	c.style.backgroundImage	= v=="" ? "" : ("url("+v+")");
	
		//...Border
		v="";try{v = xProp.selectSingleNode("G/P[@n='Borders']/P[@n='Color']").getAttribute("v");}catch(e){}	c.style.borderColor	= v==null ? "" : v;
		v="";try{v = wiki_GetUnitValue(xProp.selectSingleNode("G/P[@n='Borders']/P[@n='Width']").getAttribute("v"));}catch(e){}	c.style.borderWidth	= v;
		v="";try{v = xProp.selectSingleNode("G/P[@n='Borders']/P[@n='Style']").getAttribute("v");}catch(e){}	c.style.borderStyle	= v;
		wiki_SetSideBorder(c,xProp,"Right");
		wiki_SetSideBorder(c,xProp,"Left");
		wiki_SetSideBorder(c,xProp,"Top");
		wiki_SetSideBorder(c,xProp,"Bottom");

		if (etype != "Image")
		{
			//...Font
			v="";try{v = xProp.selectSingleNode("G/P[@n='Font']/P[@n='Name']").getAttribute("v");}catch(e){}		c.style.fontFamily	= v;
			v="";try{v = xProp.selectSingleNode("G/P[@n='Font']/P[@n='Size']").getAttribute("v");}catch(e){}		c.style.fontSize	= v;
			v="";try{v = xProp.selectSingleNode("G/P[@n='Font']/P[@n='Color']").getAttribute("v");}catch(e){}		c.style.color		= v;
			v="";try{v = xProp.selectSingleNode("G/P[@n='Font']/P[@n='Bold']").getAttribute("c");}catch(e){}		c.style.fontWeight 	= v=="y" ? "bold" : "";
			v="";try{v = xProp.selectSingleNode("G/P[@n='Font']/P[@n='Italic']").getAttribute("c");}catch(e){}		c.style.fontStyle 	= v=="y" ? "italic" : "";
			v="";try{v = xProp.selectSingleNode("G/P[@n='Font']/P[@n='Overline']").getAttribute("c");}catch(e){}	c.style.textDecorationOverline	= v=="y";
			v="";try{v = xProp.selectSingleNode("G/P[@n='Font']/P[@n='Underline']").getAttribute("c");}catch(e){}	c.style.textDecorationUnderline = v=="y";
			v="";try{v = xProp.selectSingleNode("G/P[@n='Font']/P[@n='Strikeout']").getAttribute("c");}catch(e){}	c.style.textDecorationLineThrough = v=="y";

			//...Width, Height		
			v="";try{v = wiki_GetUnitValue(xProp.selectSingleNode("G/P[@n='Width']").getAttribute("v"));}catch(e){}		c.style.width	= v;
			v="";try{v = wiki_GetUnitValue(xProp.selectSingleNode("G/P[@n='Height']").getAttribute("v"));}catch(e){}		c.style.height	= v;
		}
		
		v="";try{v = xProp.selectSingleNode("G/P[@n='ToolTip']").getAttribute("v");}catch(e){}	c.title	= unescape(v);	
	}
	
	if (etype=="HyperLink")
	{
		v="";try{v = xProp.selectSingleNode("G/P[@n='Text']").getAttribute("v");}catch(e){}	c.innerHTML	= wiki_GetValue_Unescaped(v);
		v="";try{v = xProp.selectSingleNode("G/P[@n='NavigateURL']").getAttribute("v");}catch(e){}	c.href = v;
		v="";try{v = xProp.selectSingleNode("G/P[@n='Target']").getAttribute("v");}catch(e){}	c.removeAttribute("target"); if(v=="OpenNewWindow")c.target="_blank";
	}
	else if (etype=="Table")
	{
		v="";try{v = wiki_GetUnitValue(xProp.selectSingleNode("G/P[@n='Width']").getAttribute("v"));}catch(e){} if(v=="")v="100%"; c.style.width = v;
		v="";try{v = xProp.selectSingleNode("G/P[@n='HorizontalAlign']").getAttribute("v");	}catch(e){}	if(v=="")c.removeAttribute("align");else c.align = v;
		v="";try{v = xProp.selectSingleNode("G/P[@n='CellPadding']").getAttribute("v");		}catch(e){}	c.cellPadding = v;
		v="";try{v = xProp.selectSingleNode("G/P[@n='CellSpacing']").getAttribute("v");		}catch(e){}	c.cellSpacing = v;
	}
	else if (etype=="TableCell")
	{
		v="";try{v = xProp.selectSingleNode("G/P[@n='HorizontalAlign']").getAttribute("v");	}catch(e){}	if(v=="")c.removeAttribute("align");else c.align = v;	
		v="";try{v = xProp.selectSingleNode("G/P[@n='VerticalAlign']").getAttribute("v");	}catch(e){}	if(v=="")c.removeAttribute("vAlign");else c.vAlign = v;
		v="";try{v = xProp.selectSingleNode("G/P[@n='ColumnSpan']").getAttribute("v");		}catch(e){}	if(v=="")c.removeAttribute("colSpan");else c.colSpan = v;
		v="";try{v = xProp.selectSingleNode("G/P[@n='RowSpan']").getAttribute("v");			}catch(e){}	if(v=="")c.removeAttribute("rowSpan");else c.rowSpan = v;
		v="";try{v = xProp.selectSingleNode("G/P[@n='Wrap']").getAttribute("c");			}catch(e){}	if(v=="")c.removeAttribute("noWrap");else c.noWrap = v=="n";
	}
	else if (etype=="Image")
	{
		v="";try{v = xProp.selectSingleNode("G/P[@n='AlternateText']").getAttribute("v");	}catch(e){}	if(v=="")c.removeAttribute("alt");else c.alt = unescape(v);
		v="";try{v = xProp.selectSingleNode("G/P[@n='HorizontalMargin']").getAttribute("v");}catch(e){}	if(v=="")c.removeAttribute("hspace");else c.hspace = v;
		v="";try{v = xProp.selectSingleNode("G/P[@n='VerticalMargin']").getAttribute("v");	}catch(e){}	if(v=="")c.removeAttribute("vspace");else c.vspace = v;
		v="";try{v = xProp.selectSingleNode("G/P[@n='ImageUrl']").getAttribute("v");		}catch(e){}	if(v=="")c.removeAttribute("src");else c.src = v;
		v="";try{v = xProp.selectSingleNode("G/P[@n='ImageAlign']").getAttribute("v");		}catch(e){} if(v=="")c.removeAttribute("align");else c.align = v;
		//...Image width/height
		c.style.width="";	
		c.style.height="";	
		v="";try{v = wiki_GetUnitValue(xProp.selectSingleNode("G/P[@n='Width']").getAttribute("v"));	}catch(e){}	if (v=="")c.removeAttribute("width");else c.width = v;
		v="";try{v = wiki_GetUnitValue(xProp.selectSingleNode("G/P[@n='Height']").getAttribute("v"));}catch(e){}	if (v=="")c.removeAttribute("height");else c.height = v;
	}
	else if (etype=="CustomWebPart")
	{
		v="";try{v = xProp.selectSingleNode("G/P[@n='FileName']").getAttribute("v");	}catch(e){}	c.setAttribute("c_FileName",v);
	}
}
//-----------------------------------------------------------
function wiki_SetSideBorder(c,xProp,name)
{
	var v;
	v="";try{v = xProp.selectSingleNode("G/P[@n='Borders']/P[@n='"+name+"']/P[@n='Color']").getAttribute("v");}catch(e){}	
		eval("c.style.border"+name+"Color = v==null ? '' : v;");
	v="";try{v = wiki_GetUnitValue(xProp.selectSingleNode("G/P[@n='Borders']/P[@n='"+name+"']/P[@n='Width']").getAttribute("v"));}catch(e){}	
		eval("c.style.border"+name+"Width = v;");
	v="";try{v = xProp.selectSingleNode("G/P[@n='Borders']/P[@n='"+name+"']/P[@n='Style']").getAttribute("v");}catch(e){}	
		eval("c.style.border"+name+"Style	= v;");
}
//-----------------------------------------------------------
function wiki_RestoreSideBorder(c,xProp,name)
{
	try{xProp.selectSingleNode("G/P[@n='Borders']/P[@n='"+name+"']/P[@n='Color']").setAttribute("v", eval("c.style.border"+name+"Color"));}catch(e){}
	try{xProp.selectSingleNode("G/P[@n='Borders']/P[@n='"+name+"']/P[@n='Width']").setAttribute("v", eval("c.style.border"+name+"Width"));}catch(e){}
	try{xProp.selectSingleNode("G/P[@n='Borders']/P[@n='"+name+"']/P[@n='Style']").setAttribute("v", eval("c.style.border"+name+"Style"));}catch(e){}
}
//-----------------------------------------------------------
function wiki_GetUnitValue(v,defaultValue)
{
	return v==null || v=='px' || v=='%' ? "" : v;
}
//-----------------------------------------------------------
function wiki_GetValue_HRef(v)
{
	var host = document.getElementById("hidHost").value;
	var port = document.getElementById("hidPort").value;
	var isDefaultPort = document.getElementById("hidIsDefaultPort").value=="y";
	var ss = v.toLowerCase();
	if (ss.indexOf("http://") != -1 || ss.indexOf("https://") != -1 || ss.indexOf("ftp://") != -1)
	{
		var i1 = v.indexOf("://");
		ss = v.substring(i1+3);
		i1 = ss.indexOf("/");
		if (i1!=-1)
		{
			var host2 = ss.substr(0,i1).toLowerCase();
			var isLocal = (isDefaultPort && host2==host) || (!isDefaultPort && host2==(host+":"+port))
			if (isLocal)
				v = ss.substring(i1);
		}
	}
	return v;
}
//-----------------------------------------------------------
function wiki_GetValue_Unescaped(v)
{
	if (v==null) v="";	
	v = unescape(v);	
	if (v=="") v="&nbsp;";
	while(v.indexOf(" ")!=-1)v=v.replace(" ","&nbsp;");
	return v;
}




