/// ---------------------------------------------------------------------------
///
///		Console.js
///
///		This file contains the Javascript handlers for CMS2002 WebAuthor Console
///
///		Copyright (c) 2002 Microsoft Corporation. All rights reserved.
///
/// ---------------------------------------------------------------------------

/// <summary>
/// Prepare postback Url for all PostbackActions
/// </summary>
function CMS_preparePostbackUrl( urlPostback )
{
	if( (getTree()!=null) && (urlPostback.indexOf("NRMODE=Update")!=-1) )
			urlPostback += "&RefreshWAETree";
	__CMS_PostbackForm.action = urlPostback;
	
	if(typeof(__CMS_PostbackFormBeenReset) != "undefined")
		__CMS_PostbackFormBeenReset = true; // SP2
}

/// <summary>
/// Javascript function in repsonse to "Delete" Console link in
/// the PresentationUnpublished mode.
/// </summary>
function CMS_onClickDelete()
{
	// put up confirm() dialog first!
	var confirmedDelete = confirm(L_DeleteConfirm_Message);
	if (confirmedDelete == true)
	{
		__CMS_PostbackForm.__CMS_ConfirmedDelete.value = "true";
	}

	return confirmedDelete;
}

/// <summary>
/// Javascript function in repsonse to "Exit" Console link in
/// the Save New Page or Edit Page properties 
/// </summary>
function CMS_onClickExit()
{
	// put up confirm() dialog first!
	return confirm(L_ExitConfirm_Message);
}

var g_strOriginalFormAction;
var g_strOriginalFormTarget;

/// <summary>
/// Javascript function to prepare for the Preview cases:
///	AuthoringPreivew, PresentationUnpublishedPreview
/// </summary>
function CMS_preparePreviewPostback( strUrl )
{
	// cache original state of the Form
	g_strOriginalFormAction = __CMS_PostbackForm.action;
	g_strOriginalFormTarget = __CMS_PostbackForm.target;

	__CMS_PostbackForm.action = strUrl;
	__CMS_PostbackForm.target = "WBC_winPreview";

	if(typeof(__CMS_PostbackFormBeenReset) != "undefined")
		__CMS_PostbackFormBeenReset = true; // SP2

	// open a blank window for the target Preview postback
	WBC_openWindowOnFocus("about:blank", "WBC_winPreview", WBC_UseDefaultSizing(window.top));
}

/// <summary>
/// Javascript function to restore state of page after the Preview cases:
///	AuthoringPreivew, PresentationUnpublishedPreview
/// </summary>
function CMS_restoreAfterPreviewPostback()
{
	// restore state of form
	__CMS_PostbackForm.action = g_strOriginalFormAction;
	__CMS_PostbackForm.target = g_strOriginalFormTarget;

	if(typeof(__CMS_PostbackFormBeenReset) != "undefined")
		__CMS_PostbackFormBeenReset = true; // SP2
	
	g_bWarnBeforeLeave = true;
}

/// <summary>
/// Javascript function to refresh page with a postback
/// </summary>
function WBC_refreshPageWithPostback()
{
	__CMS_PostbackForm.action = __CMS_CurrentUrl;
	__CMS_PostbackForm.submit();        
}

/// <summary>
/// Javascript function to update all sibling frames to Unpublished
/// mode after "Switch To Live Site" has been clicked. This is done
/// in case the action is in a framed site.
/// </summary>
function UpdateSiblingFramesInPublishedMode()
{
}

/// <summary>
/// Javascript function to update all sibling frames to Unpublished
/// mode after "Switch To Edit Site" has been clicked. This is done
/// in case the action is in a framed site.
/// </summary>
function UpdateSiblingFramesInUnpublishedMode()
{
}

/// <summary>
/// Javascript function to return a Url in Published mode, based
/// on the passed in URL. 
/// </summary>
function GetUrlModePublished( strUrl )
{
	if (strUrl.indexOf("NRMODE=") > 0)
	{
		strUrl = strUrl.replace(/NRMODE=Unpublished/, "NRMODE=Published");
		strUrl = strUrl.replace(/NRMODE%3dUnpublished/, "NRMODE%3dPublished");
		strUrl = strUrl.replace(/NRMODE=Update/, "NRMODE=Published");
		strUrl = strUrl.replace(/NRMODE%3dUpdate/, "NRMODE%3dPublished");
	}
	else
	{
		strUrl = AppendQueryString( strUrl, "NRMODE", "Published" );
	}

    // need to get rid of WBCMODE and wbc_purpose in query string
	strUrl = RemoveQueryString( strUrl, "WBCMODE");
	strUrl = RemoveQueryString( strUrl, "wbc_purpose");
    
	return strUrl;
}

/// <summary>
/// Javascript function to return a Url in Unpublished mode, based
/// on the passed in URL. 
/// </summary>
function GetUrlModeUnpublished( strUrl )
{
	if (strUrl.indexOf("NRMODE=") > 0)
	{
		strUrl = strUrl.replace(/NRMODE=Published/, "NRMODE=Unpublished");
		strUrl = strUrl.replace(/NRMODE%3dPublished/, "NRMODE%3dUnpublished");
		strUrl = strUrl.replace(/NRMODE=Update/, "NRMODE=Unpublished");
		strUrl = strUrl.replace(/NRMODE%3dUpdate/, "NRMODE%3dUnpublished");
	}
	else
	{
		strUrl = AppendQueryString( strUrl, "NRMODE", "Unpublished" );
	}

    // need to append WBCMODE and wbc_purpose querystring parameters
    // for PresentationUnpublished mode.
	strUrl = AppendQueryString( strUrl, "WBCMODE", "PresentationUnpublished" );
	strUrl = AppendQueryString( strUrl, "wbc_purpose", "Basic" );

	return strUrl;
}

// -- WAE --
function openDialog(url, title, width, height, dlgArguments, modal, parentWnd)
{
	if(width==null)
		width = (screen.width-(screen.height*0.1)).toString() + "px";
	if(height==null)
		height = (screen.height-(screen.height*0.1)).toString() + "px";

	if(parentWnd==null)
		parentWnd = this;
	var style = "dialogHeight: "+height+"; dialogWidth: "+width+"; edge: Raised; center: Yes; help: No; resizable: Yes; status: No";
	var p = (typeof(__CMS_WEBAPP_PATH)=="undefined") ? "/MCMS/" : __CMS_WEBAPP_PATH;
		
	var url = p + "CMS/WAE/PostbackDlgHelper.aspx?DlgTitle="+title+" - IT Hit MCMS WAE&Url="+url;
	if(!modal)
		return parentWnd.showModelessDialog(url, dlgArguments, style);
	else
		return parentWnd.showModalDialog(url, dlgArguments, style);
}
function navigateWindow(url)
{
	if( (window.isFloating!=null) && (window.isFloating()==true) )
	{
		window.dialogArguments.frames("SelectFrameset").document.frames("Preview").navigate(url); // workspace
		return;
	}
	
	if(top.frames["SelectFrameset"]!=null)
		top.frames("SelectFrameset").document.frames("Preview").navigate(url); // workspace
	else
	{
		if(window.__CMS_PostbackForm!=null)
			window.location = url;
		else
			window.open(url);
	}
}
function getTree()
{
	if(top.frames["SelectFrameset"]==null)
		return null;

	var selectFrameset = top.frames("SelectFrameset");
	try
	{
		var selectFramesetDoc = selectFrameset.document; // document may be from different domain
	}
	catch(e)
	{
		return null;
	} 
		
	if(top.frames("SelectFrameset").document.frames["Tree"]==null)
		return null;
							
	return top.getTree();
}
function refreshAfterEditProperties(id)
{
	// refresh tree node	
	var tree = getTree();
	if(tree!=null)
	{
		var treeNode = tree.getNode(id);
		if(treeNode!=null)
			treeNode.parent.refresh();
	}
	
	// refresh window
	window.navigate(document.location.href);
}
function refreshAfterCreateItem(id)
{
	// refresh tree node
	var tree = getTree();
	if(tree!=null)
	{
		var treeNode = tree.getNode(id);
		if(treeNode!=null)
			treeNode.refresh();
	}
	
	// refresh window
	window.navigate(document.location);
}
function refreshAfterCopy(newPostingID, dstID)
{
	// refresh tree node
	var tree = getTree();
	if(tree!=null)
	{
		var treeNode = tree.getNode(dstID);
		if(treeNode!=null)
			treeNode.refresh();
	}

	// refresh window
	navigateWindow("/NR/exeres/"+newPostingID+"?NRMODE=Unpublished&WBCMODE=PresentationUnpublished&wbc_purpose=Basic");
}
function refreshAfterMove(srcParentID, dstID)
{
	// refresh tree nodes
	var tree = getTree();
	if(tree!=null)
	{
		var aSrcItemIDs = new Array();
		aSrcItemIDs[0] = srcParentID;
		aSrcItemIDs[1] = dstID;
		tree.refreshNodes(aSrcItemIDs);
	}

	// refresh window
	window.navigate(document.location);
}
// -- end WAE --

/// --------------------------------------
///
///		Actions that open new window
///
/// --------------------------------------
/// <summary>
///		Javascript function in repsonse to "Production Manager" Console link clicked
/// </summary>
function CMS_openWindowProductionManager( strDlgURL )
{
	openDialog(strDlgURL, "MCMS", null, null, navigateWindow, false);
}

/// <summary>
///		Javascript function in repsonse to "Approval Assistant" Console link clicked
/// </summary>
function CMS_openWindowApprovalAssistant( strDlgURL )
{
	openDialog(strDlgURL, "MCMS", null, null, navigateWindow, false);
}

/// <summary>
///		Javascript function in repsonse to "Resource Manager" Console link clicked
/// </summary>
function CMS_openWindowResourceManager( strDlgURL )
{
	var p = (typeof(__CMS_WEBAPP_PATH)=="undefined") ? "/MCMS/" : __CMS_WEBAPP_PATH;
	openDialog(p + "CMS/WAE/ResourceManager/Manager.aspx", 
		"Resource Manager", (screen.width/1.2).toString()+"px", (screen.height/1.2).toString()+"px", null, false);
}

/// <summary>
///		Javascript function in repsonse to "Resource Manager" Console link clicked
/// </summary>
function CMS_openWindowPresentationPreview( strDlgURL )
{
	WBC_openWindowOnFocus(strDlgURL, "WBC_winPreview", WBC_UseDefaultSizing(window.top));
}

/// <summary>
///		Javascript function in repsonse to "Resource Manager" Console link clicked
/// </summary>
function CMS_openWindowGoToConnected( strDlgURL )
{
	openDialog(strDlgURL, "MCMS", null, null, navigateWindow, false);
}

/// <summary>
///		Javascript function in repsonse to "Create New Page" Console link clicked
/// </summary>
function CMS_openWindowCreateNewPage( strDlgURL )
{
	openDialog(strDlgURL, "MCMS", (screen.width/1.2).toString()+"px", 
		(screen.height/1.2).toString()+"px", navigateWindow, false);
}

/// <summary>
///		Javascript function in repsonse to "Create Connected Page" Console link clicked
/// </summary>
function CMS_openWindowCreateConnectedPage( strDlgURL )
{
	openDialog(strDlgURL, "MCMS", (screen.width/1.2).toString()+"px", 
		(screen.height/1.2).toString()+"px", navigateWindow, false);
}

/// <summary>
///		Javascript function in repsonse to "Copy" Console link clicked
/// </summary>

function CMS_openWindowCopy( strDlgURL )
{
	openDialog(strDlgURL, "Copy Page", (screen.width/2.8).toString()+"px", 
		(screen.height/1.6).toString()+"px", refreshAfterCopy, false);
}

/// <summary>
///		Javascript function in repsonse to "Move" Console link clicked
/// </summary>
function CMS_openWindowMove( strDlgURL )
{
	openDialog(strDlgURL, "Move Page", (screen.width/2.8).toString()+"px", 
		(screen.height/1.6).toString()+"px", refreshAfterMove, false);
}

/// <summary>
///		Javascript function in repsonse to "Page Properties" Console link clicked
/// </summary>
function CMS_openWindowPageProperties( strDlgURL )
{
	openDialog(strDlgURL, "MCMS", "800px", "650px", refreshAfterEditProperties, false);
}

/// <summary>
///		Javascript function in repsonse to "Revision History" Console link clicked
/// </summary>
function CMS_openWindowRevisionHistory( strDlgURL )
{
	openDialog(strDlgURL, "MCMS", null, null, null, false);
}

/// <summary>
///		Javascript function in repsonse to "View Revision By Date" Console link clicked
/// </summary>
function CMS_openWindowViewRevisionByDate( strDlgURL )
{
	openDialog(strDlgURL, "MCMS", "600px", "450px", null, false);
}

/// <summary>
///		Javascript function in repsonse to "Channel Properties" Console link clicked
/// </summary>
function CMS_openWindowChannelProperties( strDlgURL )
{
	openDialog(strDlgURL, "MCMS", "800px", "660px", refreshAfterEditProperties, false);
}

/// <summary>
/// Javascript function to open the "Save New Page" dialog
/// for filling out Name and Display Name.
/// </summary>
function CMS_openWindowAuthoringSaveNew( strNavigateUrl )
{
	openDialog(strNavigateUrl, "MCMS", "600px", "320px", window, false);
}

/// <summary>
/// Javascript function in repsonse to "Preview Other Connected Pages"
/// Console link clicked in any Authoring case.
/// </summary>
function CMS_openWindowAuthoringPreviewConnected( strDlgURL )
{	
	// open a blank window and set the form target to that window
	WBC_openWindowOnFocus(strDlgURL, "WBC_winPreviewConnected", WBC_UseDefaultSizing(window.top));
}
function CMS_openWindowAuthoringCreateChannel( strNavigateUrl )
{
	openDialog(strNavigateUrl, "MCMS", "580px", "480px", refreshAfterCreateItem, false);
}
function CMS_switchToWorkspace( strNavigateUrl )
{
	window.top.location = strNavigateUrl;
}
