// Frame resize dragging code
var m_bDrag = false;
var nOldMouseX;

function GetMouseX(
	i_clEvent
	)
{
	var nMouseX = null;
	var clEvent = null;
	
	clEvent = GetEvent(i_clEvent);

	if (clEvent != null) {
		if (clEvent.clientX) {
			nMouseX = clEvent.clientX;
		}
		else {
			nMouseX = clEvent.pageX;
		}
	}

	return nMouseX;
}

function onMouseDown(
	i_clEvent
	)
{
	m_bDrag = true;
	nOldMouseX = GetMouseX(i_clEvent);
	
	// Prevent the default action, otherwise things will get selected while dragging.
	// IE works fine without calling this function, as long as onMouseMove returns false, but Netscape
	// requires this call.
	//PreventEventDefaultAction(i_clEvent);
}

function onMouseMove(
	i_clEvent
	)
{
	var nMouseX;
	
	if (m_bDrag) {
		nMouseX = GetMouseX(i_clEvent);
		if (nMouseX != null && nOldMouseX != null) {
			RepositionSpanTable(nMouseX - nOldMouseX);
			nOldMouseX = nMouseX;
		}
	}

	// Tell the handler to not allow selection
	// Otherwise things would get selected while dragging
	//return false;
	return true;
}

function onMouseUp(
	i_clEvent
	)
{
	m_bDrag = false;
}

document.onmouseup = onMouseUp;
document.onmousemove = onMouseMove;

function onPageLoad() {
	onPageLoadGlobal();
	onLayoutResize(true);

	if (typeof(ObjWdsForm.RF_ReportName) != 'undefined') {
		setTimeout("myfocus(ObjWdsForm.RF_ReportName);", 1);
	}
	setTimeout("onLayoutResize(false);", 10);
}

function onSubmit() {
   if (typeof(ObjWdsForm.RF_ReportName) == "undefined") {
      // We are performing a search... 
      CreateHiddenFormField(ObjWdsForm, "RF_Command", "Search");
      var strSearchString = TrimWhiteSpace(ObjWdsForm.RF_SearchString.value);
      ObjWdsForm.sRF_SearchFromMap.value = "";   
      ObjWdsForm.sRF_SearchPerform.value = "True";   
      ObjWdsForm.sRF_SearchReportIDs.value = "";   

      if (strSearchString != "") {
         SetBasicSearch(strSearchString)
         executeWait(ObjWdsForm);
      }
   }
   else {
	  ObjWdsForm.RF_ReportName.value = TrimWhiteSpace(ObjWdsForm.RF_ReportName.value);
      var strReportName = ObjWdsForm.RF_ReportName.value;
      if (!ValidateName(strReportName, false)) {
         return false;
      }
      var strUserName = ObjWdsForm.LG_login.value;
      var strPassword = (typeof(ObjWdsForm.LG_pwd) == "undefined") ? "" : ObjWdsForm.LG_pwd.value; //no LG_pwd field if using Windows authentication 
      var nFolderId = ObjWdsForm.IF_ActiveFolder.value;
      var nReportType = ObjWdsForm.IF_ReportType.value;
      var strLang = ObjWdsForm.sCS_ChosenLang.value;
      
      var clRequest = new wdsRequest();
		clRequest.Handler = NotifySaveReport;
		clRequest.Lang = strLang;
		clRequest.ExecGetFolderReportInfo(strUserName, strPassword, strReportName, nReportType, nFolderId);
   }
   return false;
}

function NotifySaveReport(i_oResult, i_oUserData) {
	if (i_oResult.status) {
      var strReportName = i_oResult.reportName;
      var aclReport = i_oResult.reports;
		var nReports = p_GetReportCount(aclReport);
      if (nReports > 1) {
			alert(resReportAlreadyExists);
			return false;
		}
		else if (nReports == 1) {
			var nReportId = -1;
			for (var i = 0; i < aclReport.length; i++) {
				if (aclReport[i].status == 2) {
					nReportId = aclReport[i].reportId;
				}
			}
			var strUILang = GetUiLanguage(ObjWdsForm.sCS_ChosenLang.value);
			var bMatchInCurrentLang = aclReport[0].reportName[strUILang] == strReportName;
			if (nReportId != ObjWdsForm.sWD_ReportId.value || !bMatchInCurrentLang) {
				var strMessage = resFileExists;
				if (!bMatchInCurrentLang) {
					for (var strLang in aclReport[0].reportName) {
						if (strLang != strUILang) {
							if (aclReport[0].reportName[strLang] == strReportName) {
								var strMatchingReportName;
								if (typeof(aclReport[0].reportName[strUILang]) == "undefined") {
									// Take the first one.
									for (var strLang in aclReport[0].reportName) {
										strMatchingReportName = aclReport[0].reportName[strLang];
										break;
									}
								}
								else {
									strMatchingReportName = aclReport[0].reportName[strUILang];
								}
 								strMessage = resFileExistsInOtherLang;
								strMessage = strMessage.replace("{LANGNAME}", strReportName);
								strMessage = strMessage.replace("{LANGUAGE}", eval("resLanguage_" + strLang));
								strMessage = strMessage.replace("{NAME}", strMatchingReportName);
								strMessage += "\n\n" + resReplaceReport;
								break;
							}
						}
					}
				}
				if (!confirm(strMessage)) {
					return false;
				}
			}
		}
      CreateHiddenFormField(ObjWdsForm, "WD_Command", "SaveReport");
      var strNextPage = ObjWdsForm.RF_NextPage.value;
      if (strNextPage.length == 0) {
			var nType = parseInt(ObjWdsForm.RF_ReportType.value);
			strNextPage = gastrUrl[nType];
		}
      ObjWdsForm.action = strNextPage;
      SyncIFParams();
      executeWait(ObjWdsForm);
   }
   else {
		alert(i_oResult.message);
   }
   return false;
}

function p_GetReportCount(i_aclReport) {
	var nReports = i_aclReport.length;
	if (nReports < 2) {
		return nReports;
	}
	else {
		var bFound;
		var anExternalIds = new Array();
		var nExternalId = -1;
		for (var i = 0; i < nReports; i++) {
			nExternalId = i_aclReport[i].externalId;
			bFound = false;
			for (var j = 0; j < anExternalIds.length; j++) {
				if (nExternalId == anExternalIds[j]) {
					bFound = true;
					break;
				}
			}
			if (!bFound) {
				anExternalIds.push(nExternalId);
			}
		}
	}
	
	return anExternalIds.length;
}

function OnExpandTop() {
   ObjWdsForm.sRF_Task.value = TASK_BROWSE;
   ObjWdsForm.sRF_ViewTop.value = 1;
   ObjWdsForm.sRF_ActivePath.value = "";
   ObjWdsForm.action = "reportFolders.aspx";
   executeWait(ObjWdsForm);
}

function ShowFolders(i_nShow) {
	ObjWdsForm.sRF_ShowFolders.value = i_nShow;
	ObjWdsForm.action = "reportFolders.aspx";
	executeWait(ObjWdsForm);
}

function onSelectAll(obj)
{
	var nShown = ganId.length;
	var bChecked;

	bChecked = obj.checked;
	if (nShown == 1) {
		ObjWdsForm.RF_Rep.checked = bChecked;
	}
	else if (nShown > 1) {
		for(var i=0; i < nShown; i++) {
			ObjWdsForm.RF_Rep[i].checked = bChecked;
		}
	}
}

function OnCancelSave() {
	ObjWdsForm.CS_SaveMode.value = "False";
	ObjWdsForm.action = ObjWdsForm.RF_NextPage.value;
	ObjWdsForm.IF_Mode.value = -1;
	ObjWdsForm.sRF_Mode.value = 0;
	executeWait(ObjWdsForm);
}

function SetSort(i_strSortField, i_bAscending) {
	ObjWdsForm.sRF_SortField.value = i_strSortField;
	ObjWdsForm.sRF_SortAscending.value = i_bAscending;
	ObjWdsForm.action = "reportFolders.aspx";
	executeWait(ObjWdsForm);
}

function OnRearrangeReports() {
	if (typeof(ganId) == "undefined") {
		alert(resNothingToRearrange);
		return;
	}
	
	//rearranging doesn't make sense if the folder contains 0 or 1 items
	if (ganId.length < 2) {
		alert(resNothingToRearrange);
		return;
	}
	ObjWdsForm.action = "RearrangeReports.aspx";
	executeWait(ObjWdsForm);
}

