var undefined;

var TASK_BROWSE = 0;
var TASK_SEARCH = 2;
var TASK_SEARCH_RESULTS = 3;

var VIEW_PRIVATE = 0
var VIEW_PUBLIC = 1
var VIEW_GROUP = 2

var REPORT_TYPE_FOLDER = 0;

function zeroScrollPosition()
{
	if (ObjWdsForm.sRF_PosX != null) {
		ObjWdsForm.sRF_PosX.value = 0;
		ObjWdsForm.sRF_PosY.value = 0;
	}
}

// Save document position
function savePosition()
{
	if (!PWdsapp_bIsIE) {
		ObjWdsForm.sRF_PosX.value = window.pageXOffset;
		ObjWdsForm.sRF_PosY.value = window.pageYOffset;
	}
	else if (document.all) {
		ObjWdsForm.sRF_PosX.value = document.all['SpanTable'].scrollLeft;
		ObjWdsForm.sRF_PosY.value = document.all['SpanTable'].scrollTop;
	}
}
function getPositionX()
{
	return adjustPosition(ObjWdsForm.sRF_PosX.value);
}

function getPositionY()
{
	return adjustPosition(ObjWdsForm.sRF_PosY.value);
}

function OnCancelSearch(i_bClearSearchString)
{
   if (i_bClearSearchString) ObjWdsForm.sRF_SearchStringBuf.value = "";
   ObjWdsForm.sRF_Task.value = ObjWdsForm.sRF_PreviousTask.value;
   ObjWdsForm.action = "reportFolders.aspx";
   executeWait(ObjWdsForm);
}

function OnNavMaps()
{
   ObjWdsForm.action = G_strRootPath + "/NavMaps/navMap.aspx";
   executeWait(ObjWdsForm);
}

function OnHelpWindow(bOnTab) {
	var strPage = "rfhelp.htm";
	var strAnchor = "";

	if (ObjWdsForm.sRF_Task.value == TASK_SEARCH) {
		strPage = "rfadvancedsearch.htm";
	}
	if (ObjWdsForm.sRF_Mode.value == 1) {
		strPage = "save.htm";
	}
	if (ObjWdsForm.sRF_Task.value == TASK_SEARCH_RESULTS) {
		strPage = "rfsearchresults.htm";
	}
	OpenHelpWindow(strPage, bOnTab, strAnchor);
}

function SetBasicSearch(i_strSearch) {
	ObjWdsForm.sRF_SearchStringBuf.value = i_strSearch;
	ObjWdsForm.sRF_SearchTypeBuf.value = "AND";
	ObjWdsForm.sRF_SearchExactWordBuf.value = "";
	ObjWdsForm.sRF_SearchRangeBuf.value = "SHARED";
	ObjWdsForm.sRF_SearchProperties.value = "";
	ObjWdsForm.sRF_Task.value = TASK_SEARCH_RESULTS;
}

function Task(i_nTask)
{
	ObjWdsForm.sRF_PreviousTask.value = ObjWdsForm.sRF_Task.value;
	ObjWdsForm.sRF_Task.value = i_nTask;
	ObjWdsForm.action = "reportFolders.aspx";
	if (i_nTask == TASK_SEARCH) {
		ObjWdsForm.action = "AdvancedSearch.aspx";
	}
	executeWait(ObjWdsForm);
}

function OnReportClick(i_nExternalId, i_nType, i_nReportId) {
	CleanupRearrangeFields();
	var nRep;
	var strUrl;

	if (ObjWdsForm.RF_NextPage.value.length > 0) {
		strUrl = ObjWdsForm.RF_NextPage.value;
	}
	else {
		strUrl = gastrUrl[i_nType];
	}
	if (ObjWdsForm.sRF_Mode.value == 1) {
		var nShown = GetShownReportCount();
		var nIndex = -1;
		for (nRep = 0; nRep < nShown && nIndex == -1; nRep++) {
			if (ganId[nRep] == i_nReportId) {
				nIndex = nRep;
			}
		}
		ObjWdsForm.RF_ReportName.value = unescape(gastrName[nIndex]);
	}
	else {
		ObjWdsForm.RF_ReportID.value = i_nReportId;
		ObjWdsForm.action = strUrl + "?ReportId=" + i_nExternalId;
		ObjWdsForm.IF_ReportType.value = i_nType;
		if (i_nType == 12) {
			// If we have to login, this tells the login page that we've come from WDS.
			CreateHiddenFormField(ObjWdsForm, "CS_TargetPage", document.location.pathname);
		}
		SyncIFParams();
		executeWait(ObjWdsForm);
	}
}

function OnSearchReportClick(i_nExternalId, i_nType) {
	var nRep;
	var nShown = GetShownReportCount();
	var nIndex = -1;
	var strUrl;

	if (ObjWdsForm.RF_NextPage.value.length > 0) {
		strUrl = ObjWdsForm.RF_NextPage.value;
	}
	else {
		strUrl = gastrUrl[i_nType];
	}
	if (ObjWdsForm.sRF_Mode.value == 1) {
		for (nRep = 0; nRep < nShown && nIndex == -1; nRep++) {
			if (ganId[nRep] == i_nExternalId) {
				nIndex = nRep;
			}
		}
		ObjWdsForm.RF_ReportName.value = unescape(gastrName[nIndex]);
	}
	else {
		CreateHiddenFormField(ObjWdsForm, "WD_Command", "SearchLoad");
		CreateHiddenFormField(ObjWdsForm, "RF_ReportID", i_nExternalId);
		CreateHiddenFormField(ObjWdsForm, "IF_ReportType", i_nType);
		SyncIFParams();
		ObjWdsForm.action = strUrl + "?ReportId=" + i_nExternalId;
		executeWait(ObjWdsForm);
	}
}

function OnDocumentClick(i_nExternalId, i_nType, i_nReportId) {
	CleanupRearrangeFields();

	if (ObjWdsForm.sRF_Mode.value == 1) {
		var nShown = GetShownReportCount();
		var nIndex = -1;
		for (nRep = 0; nRep < nShown && nIndex == -1; nRep++) {
			if (ganId[nRep] == i_nReportId) {
				nIndex = nRep;
			}
		}
		ObjWdsForm.RF_ReportName.value = unescape(gastrName[nIndex]);
		return;
	}

	// Spr 8805: Save the actual URL to be used after the PDF/Excel document is submitted.
	var strPreviousPage = ObjWdsForm.action;
	
	if (i_nType == REPORT_TYPE_PDF && PWdsapp_bIsIE && !PWdsapp_bActiveXEnabled) {
		alert(resEnableActiveXControls);
	}
	else {
		ObjWdsForm.action = G_strRootPath + "/TableViewer/document.aspx" + "?ReportId=" + i_nExternalId;
		ObjWdsForm.submit();
		ObjWdsForm.action = strPreviousPage;
	}
}

// Sets a folder as active.
function OnFolderClick(i_strExpand, i_bInContentsPane, i_strPath)
{
	ObjWdsForm.RF_Action.value = "Open";
	ObjWdsForm.RF_ActionFolderPath.value = i_strPath;
	ObjWdsForm.RF_ActionInContentsPane.value = i_bInContentsPane;
	ObjWdsForm.action = "reportFolders.aspx";

	if(typeof(bJustRemovedCurrentReport) != "undefined") 
	{
		CreateHiddenFormField(ObjWdsForm, "sWD_ReportId", "");
		CreateHiddenFormField(ObjWdsForm, "sEV_ReportId", "");
		CreateHiddenFormField(ObjWdsForm, "CS_ReportTitle", "");//this makes the tabs disappear
	}

	executeWait(ObjWdsForm);
}

// add/remove a folder from the expansion state.
function ExpandFolder(i_strExpand, i_bInContentsPane, i_strPath)
{
	if (i_strExpand == "Expand") {
		ObjWdsForm.RF_Action.value = "Expand";
	}
	else {
		ObjWdsForm.RF_Action.value = "Collapse";
	}
	ObjWdsForm.RF_ActionFolderPath.value = i_strPath;
	ObjWdsForm.RF_ActionInContentsPane.value = "False";
	ObjWdsForm.action = "reportFolders.aspx";
	executeWait(ObjWdsForm);
}

function ValidateName(i_strName, i_bFolder) {
	var RegWhitespace = /^\s+$/;

	if (i_strName == null) {
		return false;
	}
	if (i_strName.length == 0 || RegWhitespace.test(i_strName)) {
		alert(i_bFolder?resNewFolderName:resNewReportName);
		return false;
	}
	if (i_strName.length > 255) {
		alert(resNameBig);
		return false;
	}
	return true;
}

function OnReport() {
	ObjWdsForm.CS_InHelp.value = "False";
	ObjWdsForm.action = ObjWdsForm.CS_NextPage.value;
	executeWait(ObjWdsForm);
}

function OnNewFolder() {
	do {
		var strName;
		do {
			strName = prompt(resNewFolderName, "");
			if (strName == null) {
				return;
			}
		} while (!ValidateName(strName, true));
		strName = TrimWhiteSpace(strName);
		if (!ReportExists(strName, REPORT_TYPE_FOLDER)) {
			ObjWdsForm.RF_Action.value = "NewFolder";
			ObjWdsForm.RF_NewFolder.value = strName;
			executeWait(ObjWdsForm);
			return;
		}
		else {
			alert(resFolderExists.replace("XYZ", strName));
		}
	} while (true);//keep prompting until receiving Cancel or a valid name that doesn't already exist
}

// Returns true if a report or folder of a given name and type exists, else returns false.
function ReportExists(i_strName, i_nType, i_nExcludedId)
{
	var nShown = GetShownReportCount();
	var nRep;
	var bExists = false;

	for (nRep = 0; nRep < nShown; nRep++) {
		if (i_strName.toLowerCase() == gastrName[nRep].toLowerCase()) {
			if (i_nType == ganType[nRep] && i_nExcludedId != ganId[nRep]) {
				bExists = true;
			}
		}
	}
	return bExists;
}

// Returns the sourceId associated with the named report.
function GetReportSourceId(i_strReportName, i_nType, i_nExcludedId) {
	var nShown = GetShownReportCount();
	var nRep;
	var nSourceId = -1;

	for (nRep = 0; nRep < nShown; nRep++) {
		if (i_strReportName.toLowerCase() == gastrName[nRep].toLowerCase()) {
			if (i_nType == ganType[nRep] && i_nExcludedId != ganSourceId[nRep]) {
				nSourceId = ganSourceId[nRep];
				break;
			}
		}
	}
	return nSourceId;
}


// Returns the number of reports shown on the page
function GetShownReportCount() {
	var nItems = 0;

	if (typeof(gastrName) != "undefined") {
		nItems = gastrName.length;
	}

	return nItems;
}

// Returns the number of folders/reports checked.
function GetCheckedCount()
{
	var obj = ObjWdsForm;
	var nSelected = 0;
	var nShown = GetShownReportCount();

	if (nShown == 0) {
		return nSelected;
	}
	if (nShown == 1) {
		if (obj.RF_Rep.checked) {
			return 1;
		}
		else {
			return 0;
		}
	}
	else {
		for (var i = 0; i < nShown; i++) {
			if (obj.RF_Rep[i].checked) {
				nSelected++;
			}
		}
	}

	return nSelected;
}

function OnCopy()
{
	var anID = new Array();
	var anType = new Array();
	var astrName = new Array();
	var nCount = GetCheckedCount();
	var nShown = GetShownReportCount();
	var nIndex = 0;
	var obj = ObjWdsForm;

	HideAllMenus();

	if (nCount == 0) {
		alert(resNoSelection);
		return;
	}
	else {
		// prepare an array of reports that were copied.
		// this must contain internal reportID, name and type
		if (nShown == 1) {
			if (obj.RF_Rep.checked) {
				anID[nIndex] = obj.RF_Rep.value;
				astrName[nIndex] = gastrName[0];
				anType[nIndex] = ganType[0];
			}
		}
		else if (nShown > 1) {
			for (var i = 0; i < nShown; i++) {
				if (obj.RF_Rep[i].checked) {
					anID[nIndex] = obj.RF_Rep[i].value;
					astrName[nIndex] = escape(gastrName[i]);
					anType[nIndex] = ganType[i];
					nIndex++
				}
			}
		}
		obj.RF_CopyId.value = anID.join();
		obj.RF_CopyName.value = astrName.join();
		obj.RF_CopyType.value = anType.join();
		obj.RF_CopyPathSource.value = obj.sRF_ActivePath.value;
	}
}

function OnPaste() {
	//check that something has been copied to the clipboard
	if (ObjWdsForm.RF_CopyId.value.length == 0) {
		alert(resNoPaste);
		return;
	}

	//submit the paste command
	ObjWdsForm.RF_Action.value = "Paste";
	executeWait(ObjWdsForm);
}

function OnRename() {
	var nReportId;
	var nCount = 0;
	var aclCheckBoxes = document.getElementsByName("RF_Rep");
	for (var i = 0; i < aclCheckBoxes.length; i++) {
		if (aclCheckBoxes[i].checked) {
			nReportId = ganId[i];
			nCount++;
		}
	}
	if (nCount == 0) {
		alert(resNoSelection);
		return;
	}
	else if (nCount != 1) {
		alert(resCannotRename);
		return;
	}
	RemoveFormField(ObjWdsForm, "sRF_ReportName");
	ObjWdsForm.action = G_strRootPath + "/Common/Util/RenameReport.aspx";
	CreateHiddenFormField(ObjWdsForm, "sRF_ReportId", nReportId);
	CreateHiddenFormField(ObjWdsForm, "sRF_IncludePA", "True");
	CreateHiddenFormField(ObjWdsForm, "CS_NextPage", G_strRootPath + "/ReportFolders/reportFolders.aspx");
	executeWait(ObjWdsForm);
}

function OnDelete() {
	var strFolderName;
	var obj = ObjWdsForm;
	var nSelected = 0;
	var nShown = GetShownReportCount();
	var bShared = false;
	var strMsg = resConfirmDelete;

	if (nShown == 0) {
		alert(resNoSelection);
		return;
	}	

	//do not allow deleting the current report, if in Save mode
	if(obj.IF_Mode.value == "1")
	{
		var strCrtTable = obj.sWD_ReportId.value;
		for (var i = 0; i < nShown; i++) {
			if (obj.RF_Rep[i].checked && obj.RF_Rep[i].value == strCrtTable) {
				alert(resCannotDeleteCurrentReport);
				return;
			}
		}
	}

	if (nShown == 1) {
		if (obj.RF_Rep.checked) {
			nSelected = 1;
			if (ganShared[0] == 1) bShared = true;
		}
	}
	else {
		for (var i = 0; i < nShown; i++) {
			if (obj.RF_Rep[i].checked) {
				nSelected++;
				if (ganShared[i] == 1) bShared = true;
			}
		}
	}
	if (nSelected > 0) {
		if (bShared && obj.sRF_Task.value == "3") strMsg = resConfirmDeleteShared; //only if we're in search results
		if (confirm(strMsg)) {
			ObjWdsForm.RF_Action.value = "Delete";
				executeWait(ObjWdsForm);
		}
	}
	else {
		alert(resNoSelection);
		return;
	}
}

function SyncIFParams() {
	ObjWdsForm.IF_Mode.value = ObjWdsForm.sRF_Mode.value;
	ObjWdsForm.sRF_Mode.value = "";
	ObjWdsForm.IF_ReportID.value = ObjWdsForm.RF_ReportID.value;

	if (typeof(ObjWdsForm.RF_ReportName) != "undefined") {
		ObjWdsForm.IF_ReportName.value = ObjWdsForm.RF_ReportName.value;
	}
}

function OnReportFolderTableSummary(i_nReportId, i_nExternalId, i_nType, i_nTableId) {
	switch(i_nType) {	
		case 12: strSummaryPath = "/ExtractViewer/ExtractSummary.aspx"; //IVX
					break;
					
		case 14: strSummaryPath = "/TableViewer/DocumentSummary.aspx"; //B2R
					CreateHiddenFormField(ObjWdsForm, "WD_TableIdForSummary", i_nTableId.toString());
					break;
					
		default: strSummaryPath = "/TableViewer/summary.aspx"; //IVT (table viewer 1, item selection 6, chart 3, map 13)
					break;
	}

	CreateHiddenFormField(ObjWdsForm, "WD_GetDefaultView", true);
	CreateHiddenFormField(ObjWdsForm, "WD_Command", "ShowTableSummary");
	CreateHiddenFormField(ObjWdsForm, "WD_PageBeforeSummary", document.location.pathname);
	CreateHiddenFormField(ObjWdsForm, "EV_PageBeforeSummary", document.location.pathname);
	if (IsSpawnWindow()) {
		var strSummaryPage = "_WdsSummary";
		if (typeof(ObjWdsForm.IF_PageBeforeSummary) != "undefined") {
			GstrPageBeforeSummary = ObjWdsForm.IF_PageBeforeSummary.value;
		}
		if (typeof(ObjWdsForm.sWD_ReportId) != "undefined") {
			GstrIFReportID = ObjWdsForm.IF_ReportID.value;
		}
		GenerateNewWindow(strSummaryPage, strSummaryPath + "?ReportId=" + i_nExternalId);
		if (typeof(ObjWdsForm.WD_PageBeforeSummary) != "undefined") {
			ObjWdsForm.WD_PageBeforeSummary.value = GstrPageBeforeSummary;
		}
		if (typeof(ObjWdsForm.sWD_ReportId) != "undefined" && GstrIFReportID != "") {
			ObjWdsForm.sWD_ReportId.value = GstrIFReportID;
		}
	}
	else {
		CreateHiddenFormField(ObjWdsForm, "IF_ReportID", i_nReportId);
		ObjWdsForm.action = G_strRootPath + strSummaryPath;
		executeWait(ObjWdsForm);
	}  
}

function OnDownloadTable(i_nExternalId) {
	var strPreviousPage = ObjWdsForm.action;

	if (typeof(ObjWdsForm.sWD_ReportId) != "undefined") {
		ObjWdsForm.sWD_ReportId.value = "";
	}
	if (typeof(ObjWdsForm.sWD_TableId) != "undefined") {
		ObjWdsForm.sWD_TableId.value = "";
	}
	ObjWdsForm.action = G_strRootPath + "/TableViewer/download.aspx" + "?ReportId=" + i_nExternalId;
	ObjWdsForm.submit();
	ObjWdsForm.action = strPreviousPage;
}

//i_nDefaultDim is only needed in thematic maps, to open the geographic dimension instead of 0 at the first visit to the item selection page
function ShowView(i_nType, i_nDefaultDim) {
	if (i_nType == 6) //moving to item selection is a special case
		return ShowLastVisitedDim(i_nDefaultDim);

	CreateHiddenFormField(ObjWdsForm, "CS_InHelp", "False");
	CreateHiddenFormField(ObjWdsForm, "WD_Command", "SetViewType");
	CreateHiddenFormField(ObjWdsForm, "WD_ViewType", i_nType);
	ObjWdsForm.action = gastrUrl[i_nType];
	executeWait(ObjWdsForm);
}

//Simulates a click on the hyperlink for the currently selected dimension. For example if you click the Item Selection tab while in table/chart/map, this will
//take you to the item selection page for the same dimension you visited last time. If this is the first visit to item selection, we open the first dimension.
function ShowLastVisitedDim(i_nDefaultDim) {
	if (typeof(ObjWdsForm) != "undefined") {
		zeroScrollPosition();
		ResetChartPagination();
		var clActiveDim = ObjWdsForm.sWD_ActiveDim;
		if(i_nDefaultDim == null) i_nDefaultDim = 0;
		var strCrtDim = (clActiveDim == null)?i_nDefaultDim.toString():clActiveDim.value;
		if (strCrtDim == "-1") strCrtDim = i_nDefaultDim.toString();

		CreateHiddenFormField(ObjWdsForm, "WD_Dim", strCrtDim);
		CreateHiddenFormField(ObjWdsForm, "WD_Command", "SetActiveDim");
		CreateHiddenFormField(ObjWdsForm, "CS_InHelp", "False");
		ObjWdsForm.action = "../TableViewer/dimView.aspx";
		executeWait(ObjWdsForm);
	}
}

function SwitchToAccessibleView() {
	//find the current folder
	var strActiveFolderId = ObjWdsForm.IF_ActiveFolder.value;
	if (strActiveFolderId == "0") {
		var strViewTop = ObjWdsForm.sRF_ViewTop.value;
		var strActivePath = ObjWdsForm.sRF_ActivePath.value;
		if(strActivePath == "M")
			strActiveFolderId = -3; //My reports root
		if (strActivePath == "P")
			strActiveFolderId = -2; //Public reports root
		if (strViewTop == "1")
			strActiveFolderId = -4; //All reports root
	}

	//sort type
	var strSortField = ObjWdsForm.sRF_SortField.value;
	var strSortAscending = ObjWdsForm.sRF_SortAscending.value;
	var nSortType = 0;
	if (strSortField == "Name") nSortType = (strSortAscending == "True") ? 1 : 2; 
	if (strSortField == "Date") nSortType = (strSortAscending == "True") ? 3 : 4;

	//command
	var strCommand = "WD_C_2_" + ((nSortType == 0) ? "0" : ("2" + "_" + nSortType.toString())) + "_" + strActiveFolderId;
	CreateHiddenFormField(ObjWdsForm, strCommand, "");
	ObjWdsForm.action = "../p.aspx";
	executeWait(ObjWdsForm);
}

