//************************************************************************************
//File:		/NR/System/WBC/PopUp/Cross-Browser/PageOperation/AuthoringMode/PageAuthoring.js
//Author:	Microsoft Corporation.
//Purpose:	Display functions used for saving and transfering info from the NewPageSaveDlg.asp(for getting
//          page properties) to NewPageSave.asp.  Entry pt is Authoring Form when Save button is pressed. 
//			Exit pt is when NewPageSave.asp for saving the page information.
//
//*************************************************************************************

var g_NewPageSaveDlgForm;

// ---- [Public] Set up default variables
function WBC_onLoadHandler() 
{	
	// TODO: give form a better name
	g_NewPageSaveDlgForm = window.document.NewPageSaveDlg;
}

function WBC_getMainPostbackFormWindow()
{
	var pTargetWin = window.top.opener;
	
	if (pTargetWin) 
	{
		// try the window itself first
		if (IsMainPostbackFormWindow(pTargetWin) == true)
		{
			return pTargetWin;
		}
		// if not found, look into the children frames
		for (i=0; i < pTargetWin.top.frames.length; i++) 
		{
			if (IsMainPostbackFormWindow(pTargetWin.top.frames[i]) == true)
			{
				return pTargetWin.top.frames[i];
			}
		}
	}

	return null;
}

function IsMainPostbackFormWindow(pWindow)
{
    if (pWindow.__CMS_PostbackForm)
    {
	    if ((pWindow.__CMS_PostbackForm.WBC_fieldNewPageName != null) &&
		    (pWindow.__CMS_PostbackForm.WBC_fieldNewPageDisplayName != null))
	    {
		    return true;
	    }
	}

	return false;
}

//	---- [Public] Transfer content to authoring form from NewPageSaveDlg so that when authoring does
//  a submit, it also has the information that is from the NewPageSaveDlg
function WBC_onOkayClick()
{
    //	Check if the informatin in the NewPageSaveDlg.aspx is valid or not.
	var strNewPageSaveDlgName;
	strNewPageSaveDlgName = g_NewPageSaveDlgForm.NC_intxtName;
	
	// If data on the NewPageSaveDlg is valid
	if (WBC_verifyInputName(strNewPageSaveDlgName))
	{
	    var pFuncWin = WBC_getMainPostbackFormWindow();
    	
	    if (pFuncWin) 
	    {
    		//move the name and display name from the display name form to edit form successful	    	
		    pFuncWin.WBC_setNewPageInfo(g_NewPageSaveDlgForm.NC_intxtName.value, g_NewPageSaveDlgForm.NC_intxtDisplayName.value);

			pFuncWin.WBC_doAuthoringNewSaveAndExit();

			window.close();
		    return true;
	    }
	    else
	    {
		    alert(IDS_FUNCTION_NOT_FOUND);
		    return false;
	    }	
	}
	else
	{
		alert(IDS_INVALID_NAME);	
	    return false;
	}
}
