// TODO: REMOVE
document.write("<script type=\"text/javascript\" charset=\"utf-8\" src=\"js/Action.js\"></script>");

// Via namespace
Via = {};

var arrInitFuncs = [];
var arrToolInitFuncs = [];

function ExecInitFuncs() {
	for ( var i = 0; i < arrInitFuncs.length; i++ ) {
		arrInitFuncs[i]();
		arrInitFuncs[i] = null;
	}
	arrInitFuncs = [];
}

function AddToolInitFunc(typeId, initFunc) {
	if (arrToolInitFuncs[typeId] == null ) {
		arrToolInitFuncs[typeId] = [];
	}
	arrToolInitFuncs[typeId].push(initFunc);
}

function ExecToolInitFuncs(typeId) {
	if (arrToolInitFuncs[typeId] != null) {
		for ( var i = 0; i < arrToolInitFuncs[typeId].length; i++ ) {
			arrToolInitFuncs[typeId][i]();
		}
	}
}

var customTexts = {};
function AddCustomText(name, value) {
	customTexts[name] = value;
}
function GetCustomText(name, value) {
	if(customTexts[name]) {
		return customTexts[name];	
	}
	else {
		return "#" + name;
	}
}

function Dispose() {
//	debug("paneinclude.Dispose()");
	document.getElementById("FormContainer").removeNode(true);
}

// returns the current pane
function GetPane() {
	if(iPaneId=="") {
		iPaneId = GetPaneId();
		if(iPaneId=="") {
			// debug("No pane id, cannot get pane");
			return null;
		}
	}
	// debug("GetPane " + iPaneId);
	if(parent.ArrPanes == null || parent.ArrPanes[iPaneId]==null) {
		// debug("Pane has been removed");
		return null;
	}
	return parent.ArrPanes[iPaneId];
}

// returns the parent pane (if any)
function GetParentPane() {
	if(iParentId=="") {
		iParentId = GetParentPaneId();
		if(iParentId=="") {
			// debug("WARNING: No parent id, cannot get parent pane");
			return;
		}
	}		
	if(parent.ArrPanes[iParentId]==null) {
		return null;
	}
	return parent.ArrPanes[iParentId];
}

var arrPostProcessingSnippets = new Array();
function AddPostProcessingSnippet(functionname){
	arrPostProcessingSnippets.push(functionname);
}

function ExecutePostProcessingSnippets() {
	for ( i = 0; i < arrPostProcessingSnippets.length; i++ ) {
		eval(arrPostProcessingSnippets[i] + "();");
	}
}

var bClosePane = false;
var bReloadParentPane = false;

// get the pane number from the query string
var iPaneId = GetPaneId();
var iParentId = GetParentPaneId();
//debug("page: " +document.location.toString());
function GetPaneId() {
	return QueryString('iPaneId', document.location.toString());
}
function GetParentPaneId() {
	return QueryString('iParentId', document.location.toString());
}

//var sPaneType = QueryString('sPaneType', document.location.toString());
var sPaneType = "";
// if(iPaneId!="") {
if (GetPane() != null) {
    sPaneType = GetPane().PaneType;
}
var sParentSpawnPaneElementName = QueryString('SpawnPaneElement', document.location.toString());
//var bSubmitParentOnPostBack = parseInt(QueryString('bSubmitParentOnPostBack', document.location.toString()));
var bSubmitParentOnPostBack = false;
// if(iPaneId!="") {
if (GetPane() != null) {
    bSubmitParentOnPostBack = GetPane().SubmitParentOnPostBack;
}
var bPostBackToParent = false;
// if(iPaneId!="") {
if (GetPane() != null) {
    bPostBackToParent = GetPane().PostBackToParent;
}
var oLastFormFieldInFocus = null;
var ArrOriginalFormValues = null;

var bFocusCausedByTreeNode = false;

var bClosed = false;

var oEventManager = new EventManager();
var mouseOverIsActive = true;

var oShortcutManager = new ShortcutManager();

eventSink = function(e) {
	if (document.all) 
	{
		e = (e == null)? event : e;				
		e.returnValue = false;
		e.cancelBubble = true; 
		return false;
	}
	else 
	{
		e.preventDefault();
		e.preventBubble();
		e.stopPropagation();
	}
}

formFieldSelectStart = function(e) { // bind to fields on form to enable selection in fields
	e = (e)?e:event;
	if ( document.all ) {
		e.returnValue = true;
		e.cancelBubble = true;
	}
	else {
		e.preventBubble();
		e.stopPropagation();
	}
	
	return true;
}

function InitEventHandlers() {
	// onmousedown event handler (when clicking on the pane content area)
	
	oEventManager.QueueEvent(document, "mousedown", OnMouseDown, false);
	
	// tree specific events
	if(document.getElementById("treecontainer")!=null) {
		// onmouseup event handler (when releaseing on the pane content area)
		oEventManager.QueueEvent(document, "mouseup", OnMouseUp, false);
		
		// onmouseup event handler (when releaseing on the pane content area)
		oEventManager.QueueEvent(document, "mousemove", OnMouseMove, false);
		
		// scrollhandler for JIT-initialization of treenodes - CORE-4037
		oEventManager.QueueEvent('FormContainer', 'scroll', TreeOnScroll, false);
	}
	// onkeydown event handler
	oEventManager.QueueEvent(document, "keydown", OnKeyDown, false);
	// oncontextmenu event handler
	oEventManager.QueueEvent(document, "contextmenu", OnContextMenu, false);
	
	if ( !enableSelect ) {
		oEventManager.QueueEvent(document.body, "selectstart", eventSink, false);
	}
	
	oEventManager.RegisterEvent(document, "mouseover", ChildOnMouseOver, false);
}

/* CORE-3525 */
ChildOnMouseOver = function(e) {
	e = ( e ) ? e : event;
	parent.ClearTopMenu();
}

// onmousedown event handler
var arrMouseDownHandlers = new Array();
function AddMouseDownHandler(handler, executeOnRightClick) {
    var handlerObj = new Object();
    handlerObj.Handler = handler;
    handlerObj.ExecuteOnRightClick = executeOnRightClick;
    arrMouseDownHandlers.push(handlerObj);
}
function OnMouseDown(){
	// clear selection
	if(bFocusCausedByTreeNode==false) {
		// only clear selection in a tree if CTRL or SHIFT is not pressed (CORE-40)
		if(document.getElementById("treecontainer")!=null) {
			if(event.ctrlKey==false && event.shiftKey==false) {
				parent.Selection.Clear();
			}
		}
		else {
			// always clear selection in ordinary panes
			parent.Selection.Clear();
		}
		// don't start a drag-selection if CTRL or SHIFT is pressed (CORE-40)
		if(document.getElementById("treecontainer")!=null && event.ctrlKey==false && event.shiftKey==false) {
//			GetPane().SetStatusBarText("");
			UpdateStatusBar();
			if(document.getElementById("TreeViewListTableContent")!=null) {
				// clear location bar
				SetLocationBarText("");
			}
			StartMultiSelect();
		}
	}
	if(iPaneId!=null)	{
		GetPane().SetFocus();
	}
	for(var i=0; i<arrMouseDownHandlers.length; i++) {
		eval(arrMouseDownHandlers[i].Handler);
	}
	return true;
}
// onmouseup event handler
function OnMouseUp(){
//	debug("PaneInclude onmouseup");
	bFocusCausedByTreeNode = false;
	if(document.getElementById("treecontainer")!=null) {
		StopMultiSelect();
	}
}
// onmousemove event handler
function OnMouseMove(){
//	debug("PaneInclude onmousemove");
	if(document.getElementById("treecontainer")!=null) {
		DrawMultiSelect();
	}
}

// onkeydown event handler
function OnKeyDown(e){
	e = ( e ) ? e : event;
	
	if(document.getElementById("treecontainer")!=null) {
		if ( TreeOnKeyDown(e) ) {
			return false;
		}
	}
	
	if ( oShortcutManager.ProcessKeypress(e) ) {
		return false;
	}	
	
	if ( parent.oShortcutManager.ProcessKeypress(e) ) {
		return false;
	}
	
	return true;
}


// oncontextmenu event handler
function OnContextMenu(){
//	debug("OnContextMenu: selection size="+parent.Selection.Size());
	
//	debug("event.srcElement = 	" + event.srcElement.type);
	
	// TODO: remove this hack (the hack ensures that we can use the right mouse button on panes!)
	if(event.ctrlKey && event.shiftKey) {
		return true;
	}
	
	// allow the context menu to be shown over text areas and text input fields
	var tagName = event.srcElement.tagName;
	var type = event.srcElement.type;
	if(tagName=="TEXTAREA" || (tagName=="INPUT" && type=="text")) {
		return true;
	}
	
	for(var i=0; i<arrMouseDownHandlers.length; i++) {
	    if(arrMouseDownHandlers[i].ExecuteOnRightClick == true) {
    		eval(arrMouseDownHandlers[i].Handler);
    	}
	}
		
	return false;
}

// set focus on the last form field in focus 
// (or the first form field in the form if no form field has been in focus yet)
function SetFocusOnForm() {
	document.body.focus();
	return;

	if(oLastFormFieldInFocus!=null) {
		// set focus on last form field in focus
		try {
			oLastFormFieldInFocus.focus();
		}
		catch (e) {
			document.body.focus();
		}	
	}
	else {
		var bFocusSet = false;
		// traverse form to find the first form field that is not a button or a hidden field
		for(var i=0;i<document.forms[0].length;i++){
			// if the form field is not a button or a submitbutton or a hidden field, set focus on it
//			if(document.forms[0][i].type!="submit" && document.forms[0][i].type!="button" && document.forms[0][i].type!="hidden" && document.forms[0][i].parentNode.style.visibility!="hidden"){
			if(document.forms[0][i].type!="submit" && document.forms[0][i].type!="button" && document.forms[0][i].type!="hidden"){
				try {
					document.forms[0][i].focus();
					bFocusSet = true;
				}
				catch (e) {
//					debug("Error setting focus on form element "+document.forms[0][i].name);
					continue;
				}
				break;
			}
		}
		if(bFocusSet==false) {
//			debug("setting focus on body");
			document.body.focus();
		}
	}
}

function SetPaneTitle(sTitle) {
	if(GetPane()!=null) {
		GetPane().SetPaneTitle(sTitle);
	}
}

function BlurForm(){
	document.forms[0].blur();
}

function Disable() {
	return;
}

function Enable() {
	document.body.focus();
	// TODO: text against bug CORE-37 (see also Pane.SetFocusOnForm)
	if(oLastFormFieldInFocus!=null) {
		// set focus on last form field in focus
		try {
			oLastFormFieldInFocus.focus();
		}
		catch (e) {
			return;
		}
	}
}

// handle submit from pane
function DoSubmit(){
	// save any changes made to the form
	SaveFormValues();

	var valueobj = GetFormValueObject();
	GetPane().PublishMessage("Pane.Submit", valueobj);
	
	if (GetPane().ClientsideSubmit) {
		GetPane().Close();
		return;
	}
	
	if(sPaneType=="utility"){
		if(bPostBackToParent && bSubmitParentOnPostBack) {
			PostBack();
			SubmitParentPane();
			GetPane().Close();
		}
		else {
			GetPane().HidePopupTool();
			// clean up to avoid javascript permission violation
			CleanUpBeforeSubmit();
			SubmitForm();
		}
	}

	else {
		// MUST hide popup tool to set focus correct!!
		GetPane().HidePopupTool();
		// clean up to avoid javascript permission violation
		CleanUpBeforeSubmit();
		SubmitForm();
	}
}

function Close(bForced) {
	oEventManager.Cleanup();
	bClosed = true;
	// CORE-573: let Pane.Close() handle the "set focus on parent pane"
//	if(GetParentPane()!=null) {
//		GetParentPane().SetFocus();
//	}
	// hide the popup tool if it is visible
	GetPane().HidePopupTool();
	// close
	GetPane().Close(bForced);
}

function ReloadParentPane() {
	// make sure the parent pane exists
	if(GetParentPane()!=null) {
//		GetParentPane().SetFocus();
		GetParentPane().Reload();
	}
}

function SubmitParentPane() {
	// make sure the parent pane exists
	if(GetParentPane()==null) {
		return;
	}
	GetParentPane().SetFocus();
	// make sure the parent pane knows that a child pane caused the submit
	GetParentPane().ChildPane.SubmittedByChildPane();
	// force a submit of the parent pane no matter if the pane content is valid
	// - TODO: WHAT IF THE PARENT PANE CONTENT IS NOT VALID?
	GetParentPane().ChildPane.DoSubmit();
}

paneInitCallback = function(initData) {
	for (var i = 0; i < initData.formdata.fields.length; i++ ) {
		var thisElement = document.forms[0].elements[initData.formdata.fields[i].name];
		if ( thisElement != null ) {						
			switch ( thisElement.type ) {
				case 'select':
				case 'select-one':
				case 'select-multiple':
					var j = 0;
					while ( j < thisElement.options.length && thisElement.options[j].value != initData.formdata.fields[i].value ) {
						j++;
					}
					if ( j < thisElement.options.length ) {
						thisElement.selectedIndex = j;
					}
				break;
				default:
					thisElement.value = initData.formdata.fields[i].value;
				break;
			}
		}
	}
}

// init method (runs onLoad in the pane content iframe)
function Init() {
    if (GetPane() == null) {
        return;
    }
    if (bReloadParentPane) {
		if(GetParentPane()!=null) {
			// CORE-???
			GetParentPane().PostBack("swPreviousSubmitCausedBy", "ChildPane");
			// only set focus on the parent pabe if this pane is closing...
			var setFocusOnParent = bClosePane;
			GetParentPane().Reload(setFocusOnParent);
		}
	}
	if(bClosePane) {
		ExecutePostProcessingSnippets();
		Close(true);
	}
	if(bClosed) {
		// don't initialize if Close() has been called!
		return;
	}
	
	InitEventHandlers();

	// hide the popup tool if it is visible
	GetPane().HidePopupTool();
	
	if (parent.ArrPanes!=null) {
		GetPane().Init({
			OkButton: bPaneOkButton,
			ApplyButton: bPaneApplyButton,
			CancelButton: bPaneCancelButton,
			PreviewButton: bPanePreviewButton,
			SelectTreeSelectButton: bPaneSelectTreeSelectButton,
			SelectTreeCancelButton: bPaneSelectTreeCancelButton,
			CloseButton: bPaneCloseButton,
			StepBackButton: bPaneStepBackButton,
			StepForwardButton: bPaneStepForwardButton,
			FinishButton: bPaneFinishButton,
			WizardProgress: sPaneWizardProgress
		});
	}
	
	if(sPaneType=="utility"){
		// attempt to get form field values from parent if the pane is a utility pane
		GetFormFieldValues();
	}

	// append onfocus and onmousedown event handlers to all form fields that are not buttons or hidden fields
	for(var i=0;i<document.forms[0].length;i++){
		// if the form field is not a button or a submitbutton or a hidden field, set focus on it
		if(document.forms[0][i].type!="submit" && document.forms[0][i].type!="button" && document.forms[0][i].type!="hidden"){
//			document.forms[0][i].onmousedown = new Function ("SaveFocusFormField(this)");
			document.forms[0][i].onfocus = new Function ("SaveFocusFormField(this)");
		}
	}	

	// save the original form values 
	SaveFormValues();

	// set focus on the first form field in the form	
	SetFocusOnForm();

	DragDropInit();

	// set scroll position
	var formContainer = document.getElementById("FormContainer");
	if(GetPane().ScrollTop!=0) {
		formContainer.scrollTop = GetPane().ScrollTop;
	}

	if(GetPane().ScrollLeft!=0) {
		formContainer.scrollLeft = GetPane().ScrollLeft;
	}

	// increment pane submit counter (QA watch)
	GetPane().SubmitCounter++;

	if ( !enableSelect ) {
		// run through all formfields and disable select
		for ( var i = 0; i < document.forms[0].length; i++ ) {
			oEventManager.QueueEvent(document.forms[0][i], "selectstart", formFieldSelectStart, false);
		}
	}

	oEventManager.ProcessQueuedEvents();
	parent.ClearTopMenu();

	GetPane().PublishMessage("Pane.Init", {formdata: GetFormValueObject(), initcallback: paneInitCallback});
}

// save reference to the form field in focus
function SaveFocusFormField(oFormField) {
	oLastFormFieldInFocus = oFormField;
}

// post all form variables to parent Pane
function PostBack(){
	// iterate through form fields
	for(var i=0;i<document.forms[0].length;i++){
		// if the form field is not a button or a submitbutton, post it back to parent pane
		if(document.forms[0][i].type!="submit" && document.forms[0][i].type!="button"){
			// prefix the post back variable name with the name of the spawnpane element that spawned the utility window
			var sName = sParentSpawnPaneElementName + "_" + document.forms[0][i].name;
			GetParentPane().PostBack(sName, document.forms[0][i].value);
		}
	}
}

function GetFormValueObject() {
	var obj = { fields: [] };
	
	for ( var i=0; i < document.forms[0].length; i++ ) {
		if ( document.forms[0][i].type != "submit" && document.forms[0][i].type != "button" && document.forms[0][i].name.indexOf('sw') != 0 ) {
			obj.fields.push({name: document.forms[0][i].name, value: document.forms[0][i].value});
		}
	}
	
	return obj;
}

// get form field values from parent pane if they exist
function GetFormFieldValues() {
	// iterate through form fields
	for(var i=0;i<document.forms[0].length;i++){
		// if the form field is not a button or a submitbutton, post it back to parent pane
		if(document.forms[0][i].type!="submit" && document.forms[0][i].type!="button"){
			// get the value of the form field if it exists
			sValue = GetParentPane().GetFormFieldValue(document.forms[0][i].name);
			if(sValue!=null) {
				// the field exists on parent - set field value 
				document.forms[0][i].value = sValue;
			}
		}
	}
}

// save the current values of all form fields that are not buttons or hidden fields
// this enables the "continuing will discard all changes..." dialog
function SaveFormValues() {
	ArrOriginalFormValues = new Array();
	var iIndex;
	for(var i=0;i<document.forms[0].length;i++){
		// if the form field is not a button or a submitbutton, save it's value
		if(document.forms[0][i].type!="submit" && document.forms[0][i].type!="button"){
			iIndex = ArrOriginalFormValues.length;
			// create new object to hold name and value of the form field
			ArrOriginalFormValues[iIndex] = new Object();
			ArrOriginalFormValues[iIndex].name = document.forms[0][i].name;
			ArrOriginalFormValues[iIndex].value = document.forms[0][i].value;
//			debug("["+ArrOriginalFormValues[iIndex].name+"] = ["+ArrOriginalFormValues[iIndex].value+"]");
		}
	}
}

// check for any changes in the form fields since last call to SaveFormValues()
function CheckFormForChanges() {
	var bFormFieldFound;
	// traverse document form
	for(var i=0;i<document.forms[0].length;i++){
		// if the form field is not a button or a submitbutton or a hidden field, check it
		if(document.forms[0][i].type!="submit" && document.forms[0][i].type!="button"){
			// traverse the array for form fields and values
			bFormFieldFound = false;
			for(var j=0; j<ArrOriginalFormValues.length; j++) {
				// if the names are identical, but the values are not, changes have benn made since last save
				if(ArrOriginalFormValues[j].name==document.forms[0][i].name) {
					if(ArrOriginalFormValues[j].value!=document.forms[0][i].value) {
						// no need to look any further - at this point, at least one form field has changed
						return true;
					}
					// this indicates that the form field has an entry in the ArrOriginalFormValues[] array
					bFormFieldFound = true;
				}
			}
			// if the form field does not have an entry in the ArrOriginalFormValues[] array, 
			// the form field has been added since last save (eg. at PostBack). Hence changes have
			// been made since last save
			if(bFormFieldFound==false) {
				return true;
			}
		}
	}
	// no changes detected
	return false;
}

function CleanUpBeforeSubmit() {
	parent.Selection.Clear();
	ClearClipboard();
	parent.ChangeMode = 0;
}

function SubmittedBySubmitButton(){
	//Make sure we can recognize that this is a "real" submit
	document.forms[0].swSubmitCausedBy.value = "SubmitButton";
}

function SubmittedByApplyButton(){
	//Make sure we can recognize that this is an "apply" submit
	document.forms[0].swSubmitCausedBy.value = "ApplyButton";
}

function SubmittedByChildPane(){
	//Make sure we can recognize that this is achild pane submit
	document.forms[0].swSubmitCausedBy.value = "ChildPane";
}

function SubmittedBySearchTool(){
	//Make sure we can recognize that this is a search tool submit
	document.forms[0].swSubmitCausedBy.value = "SearchTool";
}

function SubmittedByNewNodeTool(){
	//Make sure we can recognize that this is a "create new node" submit
	document.forms[0].swSubmitCausedBy.value = "NewNodeTool";
}
function SubmittedByPopupWindow(){
	//Make sure we can recognize that this is a "popup window" submit
	document.forms[0].swSubmitCausedBy.value = "PopUpWindow";
}
function SubmittedByCancelButton() {
	//Make sure we can recognize that this is a "cancel button" submit
	document.forms[0].swSubmitCausedBy.value = "CancelButton";
	// clean up to avoid javascript permission violation
	CleanUpBeforeSubmit();
	// submit the form when the user clicks the Cancel button without validating the form.
	SubmitForm();
}
function SubmittedByReloadButton() {
	//Make sure we can recognize that this is a "reload button" submit
	document.forms[0].swSubmitCausedBy.value = "ReloadButton";
	// clean up to avoid javascript permission violation
	CleanUpBeforeSubmit();
	// submit the form when the user clicks the Reload button without validating the form.
	SubmitForm();
}

function SubmittedByForwardButton(){
	//Make sure we can recognize that this is a submit from Forward
	document.forms[0].swSubmitCausedBy.value = "ForwardButton";
}
function SubmittedByBackButton(){
	//Make sure we can recognize that this is a submit from Back
	document.forms[0].swSubmitCausedBy.value = "BackButton";
	// clean up to avoid javascript permission violation
	CleanUpBeforeSubmit();
	// submit the form when the user clicks the Back button without validating the form.
	SubmitForm();
}
function SubmittedByFinishButton(){
	//Make sure we can recognize that this is a submit from Finish
	document.forms[0].swSubmitCausedBy.value = "FinishButton";
}

function SubmittedByGuibElement(iActionId, iTypeId, iNodeId, sCustomId, sGuibElementName) {
	AddFieldToForm("swGuibActionId", iActionId);
	AddFieldToForm("swGuibActionTypeId", iTypeId);
	AddFieldToForm("swGuibActionNodeId", iNodeId);
	AddFieldToForm("swGuibActionCustomId", sCustomId);
	AddFieldToForm("swGuibElementName", sGuibElementName);
	//Make sure we can recognize that this is a "guib element" submit
	document.forms[0].swSubmitCausedBy.value = "GuibElement";
}

// CORE-1480
function SubmittedByUnlockButton() {
	document.forms[0].swSubmitCausedBy.value = "Unlock";
	CleanUpBeforeSubmit();
	SubmitForm();
}

function IsVersionedObject(){
	var versionedObject = document.forms[0].swIsVersionedObject;
	return ( versionedObject != null && versionedObject.value == "1" );
}

function SubmitForm() {
	// make sure any visual editor content has been fetched (e.g. on Reload or SubmittedByGuibElement)
	GetVisualEditorContent();
	// retain scroll position:
	var formContainer = document.getElementById("FormContainer");
	
	GetPane().Reloading = true; // always set??
	
	GetPane().ScrollTop = formContainer.scrollTop;
	GetPane().ScrollLeft = formContainer.scrollLeft;
//	debug("scrollLeft: " + formContainer.scrollLeft + ", scrollTop: " + formContainer.scrollTop); 

	// show the wait layer
	GetPane().ShowWaitLayer();
	
	// CORE-406: avoid js error when enabling/closing parent pane if the form has not yet finished loaded
	GetPane().ChildPane = null;
	
	// submit form
	document.forms[0].submit();
}

xmlEntityReplace = function(m) {
	switch ( m ) {
		case '<':
			return '&lt;';
		break;
		case '>':
			return '&gt;';
		break;
		case '"':
			return '&quot;';
		break;
		case "'":
			return '&apos;';
		break;
		case '&':
			return '&amp;';
		break;
	}
}
//Will suck content from the iframes containing visual editors to the hidden fields on the main form
// status variable - make sure the visual editor content is only fetched once on submit
//CORE-1496: status variable caused this issue
function GetVisualEditorContent(){
	for(i=0;i<frames.length;i++){
		var frame = frames.item(i);
		if (frame.name.indexOf('EditorFrame_')>-1) {
			hiddenFieldName = frame.name.substr(12);
			
			var veXml = frame.getHtml();
			
			// CORE-4851: 
			// for some reason the editor sometimes puts an empty SPAN tag in the beginning
			// of the editor content. if not deleted this will be wrapped in a SPAN with FONT-SIZE=14
			// when the editor is submitted next time.
			veXml = veXml.replace("<span></span>", "");
			
			// veXml = veXml.replace(/(["'>])/g,function(m){return xmlEntityReplace(m);}); // CORE-3408 workaround for editor bug ( '>' is not encoded )
			document.forms[0][hiddenFieldName].value = veXml;
		}
	}
}

function Help() {	
	var additionalArgs = new Array();	
	// add typeid to help url if any typeid exists
	var typeId = QueryString("typeid", document.location.toString());
	if(typeId!=null && typeId!="") {
		additionalArgs.push(MakeHelpArgument("typeid", typeId));
	}
	

    //if PageTemplateParams exist, then push it into the help link
    if(QueryString('PageTemplateParams', document.location.toString())=='True')
    {
        additionalArgs.push(MakeHelpArgument("PageTemplateParams",'True'));
    }


    var re = new RegExp("\\/(\\w+)\\.aspx");
    var RegMatch = re.exec(document.location.href)[1];

    if(RegMatch!=null)
    {
        additionalArgs.push(MakeHelpArgument("ref",  RegMatch));
    }
	

	parent.DisplayHelp(PageId, additionalArgs);
}

function MakeHelpArgument(name, value) {
	var arg = new Object();
	arg.Name = name;
	arg.Value = value;
	return arg;
}

function CancelOnClose() {
	return cancelOnClose;
}



