// Report Folders common functions and constants.

// Ensure 'undefined' is defined.
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 szPage = "RFHelp.htm";
	var szAnchor = "";
	var objForm = ObjWdsForm;

	if (objForm.sRF_Task.value == TASK_SEARCH) {
		szPage = "RFAdvancedSearch.htm";
	}
	if (objForm.sRF_Mode.value == 1) {
		szPage = "Save.htm";
	}
	if (objForm.sRF_Task.value == TASK_SEARCH_RESULTS) {
		szPage = "RFSearchResults.htm";
	}
	OpenHelpWindow(szPage, bOnTab, szAnchor);
}

// ----------------------------------------------------------------------------    
function SetBasicSearch(i_strSearch)
{
	var objForm = ObjWdsForm;

	objForm.sRF_SearchStringBuf.value = i_strSearch;
	objForm.sRF_SearchTypeBuf.value = "AND";
	objForm.sRF_SearchExactWordBuf.value = "";
	objForm.sRF_SearchRangeBuf.value = "SHARED";
	objForm.sRF_SearchProperties.value = "";
	objForm.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_lID, i_nType)
{
	var nRep;
	var nShown = GetShownItemCount();
	var objForm = ObjWdsForm;
	var nIndex = -1;
	var strUrl;

	if (objForm.RF_NextPage.value.length > 0) {
		strUrl = objForm.RF_NextPage.value;
	}
	else {
		strUrl = gastrUrl[i_nType];
	}
	if (objForm.sRF_Mode.value == 1) {
		for (nRep=0; nRep < nShown && nIndex == -1; nRep++) {
			if (ganId[nRep] == i_lID) {
				nIndex = nRep
			}
		}
		objForm.RF_ReportName.value = unescape(gastrName[nIndex]);
	}
	else {
		objForm.RF_ReportID.value = i_lID;
		objForm.action = strUrl + "?ReportId=" + i_lID;
		objForm.IF_ReportType.value = i_nType;
		SyncIFParams();
		executeWait(objForm);
	}
}

//-----------------------------------------------------------------------------
function OnDocumentClick(i_lID, i_nType)
{
	var objForm = ObjWdsForm;

	// Spr 8805: Save the actual URL to be used after the PDF/Excel document is submitted.
	var strPreviousPage = objForm.action;
	
	if ((i_nType == REPORT_TYPE_PDF) && PWdsapp_bIsIE && !PWdsapp_bActiveXEnabled) {
			alert(resEnableActiveXControls);
	}
	else {
		objForm.action = G_strRootPath + "/tableviewer/document.aspx" + "?FileId=" + i_lID;
		objForm.submit();
		objForm.action = strPreviousPage;
	}
}

//-----------------------------------------------------------------------------
function IsViewFolder(i_nType)
{
	var strPath;
	var nType;

	strPath = ObjWdsForm.sRF_ActivePath.value
	if (strPath.substr(0,1) == "P") {
		nType = VIEW_PUBLIC;
	}
	else if (strPath.substr(0,1) == "M") {
		nType = VIEW_PRIVATE;
	}
	else nType = VIEW_GROUP;

	return nType == i_nType;
}

// -----------------------------------------------------------------------------
// 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";
	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)
{
	var RegWhitespace = /^\s+$/

	if (i_strName == null) {
		return false;
	}
	if (i_strName.length == 0) {
		alert(resNewFolderName);
		return false;
	}
	if (RegWhitespace.test(i_strName)) {
		alert(resNewFolderName);
		return false;
	}
	if (i_strName.length > 255) {
		alert(resNameBig);
		return false;
	}
	return true;
}

// ----------------------------------------------------------------------------    
function OnReport()
{
	var objForm = ObjWdsForm;

	// TBD the use of a sWD param violates our architecture.  this must be fixed post-beta.
	ObjWdsForm.CS_InHelp.value = "False";
	objForm.action = objForm.CS_NextPage.value;
	executeWait(objForm);
}

// ----------------------------------------------------------------------------    
function OnNewFolder()
{
	var strFolderName;

	strFolderName = prompt(resNewFolderName, "");
	if (!ValidateName(strFolderName)) {
		return;
	}
	strFolderName = TrimWhiteSpace(strFolderName);
	if (!ReportExists(strFolderName, REPORT_TYPE_FOLDER)) {
		ObjWdsForm.RF_Action.value = "NewFolder";
		ObjWdsForm.RF_NewFolder.value = strFolderName;
		executeWait(ObjWdsForm);
	}
	else {
		alert(resFolderExists);
	}
}

// ----------------------------------------------------------------------------    
// Returns true if a report or folder of a given name and type exists, else returns false.
function ReportExists(i_strReportName, i_nType, i_nExcludedId)
{
	var nShown = GetShownItemCount();
	var nRep;
	var bExists = false;

	for (nRep=0; nRep < nShown; nRep++) {
		if (i_strReportName.toLowerCase() == gastrName[nRep].toLowerCase()) {
			if (i_nType == ganType[nRep] && i_nExcludedId != ganId[nRep]) {
				bExists = true;
			}
		}
	}
	return bExists;
}

// ----------------------------------------------------------------------------    
// Returns the number of items shown on the page
function GetShownItemCount()
{
	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 = GetShownItemCount();

	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 = GetShownItemCount();
	var nIndex = 0;
	var obj = ObjWdsForm;

	if (nCount == 0) {
		alert(resNoSelection);
		return;
	}
	else {
		// prepare an array of reports that were copied.
		// this must contain 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;
	}
}

// ----------------------------------------------------------------------------    
// Submits a string of report ID's to copy.  If pasting to another folder a
// prompt for overwrite is shown if there is a duplicate name/type pair in the
// active (destination) folder.
function OnPaste()
{
	var anCopyID = new Array();
	var astrCopyName = new Array();
	var bCopy;
	var nCopyCount;
	var nIndex = 0;
	var nShown = GetShownItemCount();
	var nToCopyIndex = 0;
	var obj = ObjWdsForm;
	var strMsg;

	if (obj.RF_CopyId.value.length == 0) {
		alert(resNoPaste);
		return;
	}
	if (ObjWdsForm.RF_CopyPathSource.value != ObjWdsForm.sRF_ActivePath.value) {
		if (nShown > 0) {
			anCopyID = obj.RF_CopyId.value.split(",");
			nToCopyIndex = anCopyID.length;
			obj.RF_CopyId.value = anCopyID.join();
		}
	}
	else {
		nToCopyIndex = 1;
	}
	if (nToCopyIndex > 0 || nShown ==0) {
		ObjWdsForm.RF_Action.value = "Paste";
		executeWait(ObjWdsForm);
	}
}

// ----------------------------------------------------------------------------    
function OnRename()
{
	var strReportName;
	var obj = ObjWdsForm;
	var nSelected = GetCheckedCount();
	var nShown = GetShownItemCount();
	var nType;
	var nId;

	if (nSelected == 0) {
		alert(resNoSelection);
		return;
	}
	if (nSelected != 1) {
		alert(resCannotRename);
		return;
	}

	szOldName = "";
	if (nSelected == 1) {
		if (nShown == 1) {
			szOldName = unescape(gastrName[0]);
			nType = ganType[0];
		}
		else {
			for (var i = 0; i < nShown; i++) {
				if (obj.RF_Rep[i].checked) {
					szOldName = unescape(gastrName[i]);
					nType = ganType[i];
					nId = ganId[i];
					break;
				}
			}
		}
		}

	strReportName = prompt(resRenamePleaseSpecify, szOldName);
	if (!ValidateName(strReportName)) {
		return;
	}
	strReportName = TrimWhiteSpace(strReportName);
	if (ReportExists(strReportName, nType, nId)) {
	    if (nType == REPORT_TYPE_FOLDER){
		   alert(resFolderExists);
		} else {
		   alert(resReportExists);
		}
		return;
	}
	ObjWdsForm.RF_Rename.value = strReportName;
	ObjWdsForm.RF_Action.value = "Rename";
	executeWait(ObjWdsForm);
}

// ----------------------------------------------------------------------------    
function OnDelete()
{
	var strFolderName;
	var obj = ObjWdsForm;
	var nSelected = 0;
	var nShown = GetShownItemCount();
	var bShared = false;
	var strMsg = resConfirmDelete;

	if (nShown == 0) 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) strMsg = resConfirmDeleteShared;
		if (confirm(strMsg)) {
			ObjWdsForm.RF_Action.value = "Delete";
				executeWait(ObjWdsForm);
		}
	}
	else {
		alert(resNoSelection);
		return;
	}
}

// ----------------------------------------------------------------------------    
function SyncIFParams()
{
	var objForm = ObjWdsForm;

	objForm.IF_Mode.value = objForm.sRF_Mode.value;
	objForm.sRF_Mode.value = "";
	objForm.IF_ReportID.value = objForm.RF_ReportID.value;

	if (typeof(objForm.RF_ReportName) != "undefined") {
		objForm.IF_ReportName.value = objForm.RF_ReportName.value;
	}
}


function OnTableSum(reportID, ntype)

{
     var oForm = ObjWdsForm;
     var szSummaryPage = "_WdsSummary";
     if (ntype == 12) {
	    var szSummaryPath = "/ExtractViewer/ExtractSummary.aspx"; 
	 }
	 else   {
	    var szSummaryPath = "/TableViewer/summary.aspx"; 
	 }	 
	 ObjWdsForm.IF_PageBeforeSummary.value=document.location.pathname;
     ObjWdsForm.IF_ReportID.value = reportID;      
     if (IsSpawnWindow()) {
        if (typeof(ObjWdsForm.IF_PageBeforeSummary) != "undefined") {
		    GstrPageBeforeSummary = ObjWdsForm.IF_PageBeforeSummary.value;
	    }
	    if (typeof(ObjWdsForm.IF_ReportID) != "undefined") {
		    GstrIFReportID = ObjWdsForm.IF_ReportID.value;
	    }
     GenerateNewWindow(szSummaryPage, szSummaryPath);
     
        if (typeof(oForm.WD_PageBeforeSummary) != "undefined") {
		    oForm.WD_PageBeforeSummary.value = GstrPageBeforeSummary;
	    }
	    if (typeof(oForm.sWD_reportId) != "undefined") {
		    oForm.sWD_ReportId.value = GstrIFReportID;
	    }
   }
   else {
    ObjWdsForm.action =G_strRootPath +  szSummaryPath;   
    executeWait(ObjWdsForm);
	}  
}

function OnDownloadTable(i_lID) {
	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_lID;
	ObjWdsForm.submit();
	ObjWdsForm.action = strPreviousPage;
}
