var GanDimIds;
var GnCurDim = null;
var GnCurDimBeforeSummary = null;
var GstrAction = null;
var GstrItemID = null;
var GstrItemCode = null;
var GstrSummary = null;
var GstrPageBeforeSummary = null;
var G_nOldSize = -1;

// ----------------------------------------------------------------------------
// this function is used to monitor if the browsers font size is changed.  If so,
// it forces a resize of the 'data table' which ensures the scroll bars are properly shown.
function GHandleFontSizeChange() {
	var nNewSize;

	if (PWdsapp_bIsIE) {
		nNewSize = parseInt(document.body.currentStyle.fontSize);
	}
	else {
		nNewSize = parseInt(document.defaultView.getComputedStyle(document.body,'').getPropertyValue("font-size"))
	}
	if (G_nOldSize != -1) {
		if (G_nOldSize != nNewSize) {
			Resize();
		}
	}
	G_nOldSize = nNewSize;
}

//------------------------------------------------------------------------------
function zeroScrollPosition()
{
	if (ObjWdsForm.sWD_PosX != null) {
		ObjWdsForm.sWD_PosX.value = 0;
		ObjWdsForm.sWD_PosY.value = 0;
	}
	if (ObjWdsForm.WD_VPos != null) {
		ObjWdsForm.WD_VPos.value = 0;
	}
}

//------------------------------------------------------------------------------
// Save document position
function savePosition()
{
	// cancel this stuff for pop-up windows
	if (ObjWdsForm.sWD_PosX == null) return;
	if (!PWdsapp_bIsIE) {
		ObjWdsForm.sWD_PosX.value = window.pageXOffset;
		ObjWdsForm.sWD_PosY.value = window.pageYOffset;
	}
	else {
		ObjWdsForm.sWD_PosX.value = document.all['SpanTable'].scrollLeft;
		ObjWdsForm.sWD_PosY.value = document.all['SpanTable'].scrollTop;
	}
}

// -----------------------------------------------------------------------------
function getPositionX()
{
	return adjustPosition(ObjWdsForm.sWD_PosX.value);
}

// -----------------------------------------------------------------------------
function getPositionY()
{
	return adjustPosition(ObjWdsForm.sWD_PosY.value);
}

// ----------------------------------------------------------------------------
// Return the an array containing all dimension's names
function GetDimensions()
{
   return GaszDimensions;
}

// ----------------------------------------------------------------------------
// Return the view index of a stored dimension index
function GetViewDim(i_nStoredDim)
{
	var nDim;
	var nViewDim = 0;

	for (nDim = 0; nDim < GanDimIds.length; nDim++) {
		if (GanDimIds[nDim] == i_nStoredDim) {
			nViewDim = nDim;
			break;
		}
	}

	return nViewDim;
}

// ----------------------------------------------------------------------------
// Return true if the specified dimension has selection(s)
function HasSelection(i_unDim)
{
	var szCheckedValue;
	var szEval;

	szEval = "if (ObjWdsForm.sWD_Chk" + i_unDim + "!=null) szCheckedValue=ObjWdsForm.oWD_Chk" + i_unDim + ".value;";
	eval(szEval);
	if (szCheckedValue == null) {
		return false;
	}
	else {
		return szCheckedValue.length > 1;
	}
}

//------------------------------------------------------------------------------
// Verifies that the given string is not empty
function IsControlEmpty(obj, strMsg)
{
	var str = obj.value;
	if (str == null || str == "") {
		alert(strMsg);
		myfocus(obj);
		return true;
	}

	return false;
}

//------------------------------------------------------------------------------
// Checks the length of the given string
function CheckForLength(obj, maxLen, strMessage)
{
	if (obj.value.length > maxLen) {
		alert(strMessage);
		obj.select();
		myfocus(obj);
		return false;
	}
	return true;
}

// ----------------------------------------------------------------------------
// Returns true if the string contains only AlphaNumeric characters.
// i_bIgnoreSpaces specifies whether a space is permitted.
function IsAlphaNum(i_szString, i_bIgnoreSpaces, i_bLogin)
{
	var nIndex;
	var nCode;
	var nTemp;
	for (nIndex = 0; nIndex < i_szString.length; nIndex++) {
		nTemp = i_szString.charAt(nIndex);
		nCode = i_szString.charCodeAt(nIndex);
		if (i_bIgnoreSpaces == false) {
			if (nCode == 32)
				return false;
		}  
		if (i_bLogin) {
			if (((nCode >= 0) && (nCode <= 31)) ||    // invalid chars
				((nCode >= 33) && (nCode <= 47)) ||    // invalid chars
				((nCode >= 58) && (nCode <= 64)) ||    // :;<=>?@
				((nCode >= 91) && (nCode <= 96)) ||    // [\]^_`
				((nCode >= 123) && (nCode <= 127)))
			{
				return false;
			}
		}
	}
	return true;
}

// ----------------------------------------------------------------------------
function ShowView(i_nType)
{
	var nMaxDim = eval(GetMaxDim());
	var strUrl = gastrUrl[i_nType];

	// Any changes made to any item should be save to the hidden fields
	if (typeof(SaveSelectionState) != 'undefined') {
		SaveSelectionState();
	}
	if (nMaxDim == -1) // if error
		return;
	if (typeof(ObjWdsForm.sWD_CurDim) != "undefined") {
		zeroScrollPosition();
		ObjWdsForm.CS_InHelp.value = "False";
		
		switch(i_nType) {
			case REPORT_TYPE_ITEMSEL:
				ResetChartPagination();
				ObjWdsForm.action = strUrl;
				break;
			case REPORT_TYPE_CHART:
				ObjWdsForm.sWD_CurDim.value = nMaxDim;
				ObjWdsForm.action = strUrl;
				break;
			case REPORT_TYPE_MAP:
				ObjWdsForm.sWD_CurDim.value = nMaxDim;
				ObjWdsForm.action = strUrl;
				break;							
			default:
				ResetChartPagination();
				ObjWdsForm.sWD_CurDim.value = nMaxDim;
				ObjWdsForm.action = gastrUrl[REPORT_TYPE_TABLE];
				break;
		}
		executeWait(ObjWdsForm);
	}
}

// ----------------------------------------------------------------------------
function ShowMap()
{
	var myWidth = 0, myHeight = 0;

	// Any changes made to any item should be save to the hidden fields
		if (typeof(SaveSelectionState) != 'undefined') {
			SaveSelectionState();
		}
		//need to reset the current dimension spr 010362
		ObjWdsForm.sWD_CurDim.value =  ObjWdsForm.sWD_MaxDim.value;
		ObjWdsForm.sWD_CurDimName.value = "";

	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else {
		if( document.documentElement &&
			( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			myWidth = document.documentElement.clientWidth;
			myHeight = document.documentElement.clientHeight;
		} else {
			if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			myWidth = document.body.clientWidth;
			myHeight = document.body.clientHeight;
			}
		}
	}

	ResetChartPagination();

	ObjWdsForm.WD_ClientSize.value = myHeight + "," + myWidth;
	ObjWdsForm.action = "../ThematicMaps/mapView.aspx";
	executeWait(ObjWdsForm);
}

// ----------------------------------------------------------------------------
// Tell the server to display the specified dimension.
function ShowDim(i_unDimId)
{
	var unViewDimIndex

	// Any changes made to any item should be save to the hidden fields
	if (typeof(SaveSelectionState) != 'undefined') {
		SaveSelectionState();
	}

	unViewDimIndex = GetViewDim(i_unDimId)
	if (unViewDimIndex != eval(GetCurrentDim())) {
		ObjWdsForm.WD_SearchStringBuffer.value = "";
		ObjWdsForm.WD_DateLowerBuffer.value = "";
		ObjWdsForm.WD_DateUpperBuffer.value = "";
	}
	if (typeof(ObjWdsForm) != "undefined") {
		if (typeof(ObjWdsForm.sWD_CurDim) != "undefined") {
			zeroScrollPosition();
			ResetChartPagination();

			ObjWdsForm.sWD_CurDim.value = unViewDimIndex;
			ObjWdsForm.WD_SelLevelRequest.value = -1;
			ObjWdsForm.action = "../TableViewer/dimView.aspx";
			executeWait(ObjWdsForm);
		}
	}
}

// WD_Summary's value is defined in global.inc
// In all the functions for displaying summary, in case of spawn window as the 
// form information will be submitted to a new window, after displaying the 
// the summary, form fields which have been changed should be reset.
//------------------------------------------------------------------------------
function OnTableSummary()
{
	var bIsSpawnWindow;

	bIsSpawnWindow = IsSpawnWindow();
	if (bIsSpawnWindow) {
		GetFormFields();
	}
	
	if (!bIsSpawnWindow) 
		if (typeof(SaveSelectionState) != 'undefined') 
			SaveSelectionState();

	ObjWdsForm.action = "../TableViewer/summary.aspx";
	ObjWdsForm.WD_Summary.value = "TableSummary";
	ObjWdsForm.WD_PageBeforeSummary.value = document.location.pathname;
	WaitForProperPage();
	if (bIsSpawnWindow) {
		SetFormFields();
	}
}

// ----------------------------------------------------------------------------    
function OnDimensionSummary(i_nDim)
{
	var bIsSpawnWindow;

	i_nDim = GetViewDim(i_nDim);
	bIsSpawnWindow = IsSpawnWindow();
	if (bIsSpawnWindow) {
		GetFormFields();
	}
	
	if (!bIsSpawnWindow) 
		if (typeof(SaveSelectionState) != 'undefined') 
			SaveSelectionState();

	ObjWdsForm.WD_PageBeforeSummary.value = document.location.pathname;
	ObjWdsForm.WD_CurDimBeforeSummary.value = ObjWdsForm.sWD_CurDim.value;
	ObjWdsForm.sWD_CurDim.value = i_nDim;
	ObjWdsForm.action = "summary.aspx";
	ObjWdsForm.WD_Summary.value = "DimensionSummary";
	WaitForProperPage();
	if (bIsSpawnWindow) {
		SetFormFields();
	}
}

// ----------------------------------------------------------------------------    
function OnItemSummary(i_strItemID, i_nDim)
{
	var bIsSpawnWindow;

	i_nDim = GetViewDim(i_nDim);
	
	ObjWdsForm.WD_CurDimBeforeSummary.value = ObjWdsForm.sWD_CurDim.value;
	bIsSpawnWindow = IsSpawnWindow();
	if (bIsSpawnWindow) {
		GetFormFields();
	}
	if (!bIsSpawnWindow) 
		if (typeof(SaveSelectionState) != 'undefined') 
			SaveSelectionState();

	ObjWdsForm.sWD_CurDim.value = i_nDim;
	ObjWdsForm.sWD_ItemID.value = i_strItemID;
	ObjWdsForm.sWD_ItemCode.value = "";
	ObjWdsForm.action = "summary.aspx";
	ObjWdsForm.WD_Summary.value = "ItemSummary";
	ObjWdsForm.WD_PageBeforeSummary.value = document.location.pathname;
	WaitForProperPage();
	if (bIsSpawnWindow) {
		SetFormFields();
	}
}

// ----------------------------------------------------------------------------    
function OnItemSummaryByCode(i_strItemCode, i_nDim)
{
	var bIsSpawnWindow;
	var oForm = ObjWdsForm;

	oForm.WD_CurDimBeforeSummary.value = oForm.sWD_CurDim.value;
	bIsSpawnWindow = IsSpawnWindow();   
	if (bIsSpawnWindow) {
		GetFormFields();
	}
	
	// the value of i_nDim is already converted to a "ViewDim" when a table is filled
	// on a client side
	oForm.sWD_CurDim.value = i_nDim;
	oForm.sWD_ItemID.value = "-1";
	oForm.sWD_ItemCode.value = i_strItemCode;
	oForm.action = "summary.aspx";
	oForm.WD_Summary.value = "ItemSummary";
	oForm.WD_PageBeforeSummary.value = document.location.pathname;
	WaitForProperPage()
	if (bIsSpawnWindow) {
		SetFormFields();
	}
}

// ----------------------------------------------------------------------------
function OnLoadView(i_szViewName, i_bShared, i_eType)
{
	var szActionPage;

	if (i_eType == 2) {
		szActionPage = "ChartView";
	}
	else {
		szActionPage = "TableView";
	}
	if ((i_szViewName != null) && (i_szViewName.length > 0)) {
		ObjWdsForm.action = "../TableViewer/" + szActionPage + ".aspx";
		ObjWdsForm.ViewName.value = i_szViewName;
		ObjWdsForm.bShared.value = i_bShared;
		executeWait(ObjWdsForm);
	}
}

//==============================================================================
// HELP RELATED FUNCTIONS

//------------------------------------------------------------------------------
function OnHelpWindow(bOnTab)
{
	var szAnchor = "WDShelp.htm";
	var szMainFrame;
	var wndHelp;
	var nDim, nMaxDim;

	szMainFrame = location.toString().toLowerCase()
	if (szMainFrame.indexOf("dimview.aspx") >= 0) {
		// Any changes made to any item should be save to the hidden fields
		if (typeof(SaveSelectionState) != 'undefined') {
			SaveSelectionState();
		}
		szAnchor = "selectitems.htm";
	}
	else if (szMainFrame.indexOf("tableview.aspx") >= 0) {
		szAnchor = "WDShelp.htm";
	}
	else if (szMainFrame.indexOf("chartview.aspx") >= 0) {
		szAnchor = "wdshelpcharts.htm";

	}

	OpenHelpWindow(szAnchor, bOnTab);

}

//-----------------------------------------------------------------------------
function M_Wdsapp_DisplayTableTitle(io_clWindow) 
{
	if (szTableTitle) {
		io_clWindow.document.write("<DIV id=MESSAGE-HEAD>" + 
		szTableTitle + "</DIV>"); 
	}
}

// --------------------------------------------------------------------------
function OnSaveReport(i_object)
{
	i_object.CS_SaveMode.value="True";
	i_object.action = "../ReportFolders/reportFolders.aspx?IF_Mode=1&IF_ReportName=" + URIencode(ObjWdsForm.CS_ReportTitle.value);
	executeWait(i_object);
}

// --------------------------------------------------------------------------
function OnDimOrder(i_object)
{
	i_object.action = "DimOrder.aspx";
	i_object.CS_InHelp.value = "False";
	executeWait(i_object);
}

//-----------------------------------------------------------------------------
function DisplayError(i_lErrorNumber) 
{
	if (i_lErrorNumber == "7016")
	{
		alert(resErrorItemLimit);
		window.history.back();
	}
	else {
		alert(resXMLLoad);
	}
}

// ----------------------------------------------------------------------------
function OnPageSetup(i_object)
{
	i_object.action = "PrintOptions.aspx";
	i_object.IF_ReportType.value = REPORT_TYPE_TABLE;
	i_object.CS_InHelp.value = "False";
	executeWait(i_object);
}

// ----------------------------------------------------------------------------
function WaitForProperPage()
{
	if (IsSpawnWindow()) {
		SpawnWindow();
	}
	else {
		executeWait(ObjWdsForm);
	}
}

// ----------------------------------------------------------------------------
function GetFormFields()
{
	var oForm = ObjWdsForm;

	if (typeof(oForm.sWD_CurDim) != "undefined") {
		GnCurDim = oForm.sWD_CurDim.value;
	}
	if (typeof(oForm.WD_CurDimBeforeSummary) != "undefined") {
		GnCurDimBeforeSummary = oForm.WD_CurDimBeforeSummary.value;
	}	
	if (typeof(oForm.action) != "undefined") {
		GstrAction = oForm.action;
	}
	if (typeof(oForm.sWD_ItemID) != "undefined") {
		GstrItemID = oForm.sWD_ItemID.value;
	}		
	if (typeof(oForm.sWD_ItemCode) != "undefined") {
		GstrItemCode = oForm.sWD_ItemCode.value;
	}
	if (typeof(oForm.WD_Summary) != "undefined") {
		GstrSummary = oForm.WD_Summary.value;
	}
	if (typeof(oForm.WD_PageBeforeSummary) != "undefined") {
		GstrPageBeforeSummary = oForm.WD_PageBeforeSummary.value;
	}
}

// ----------------------------------------------------------------------------
function SetFormFields()
{
	var oForm = ObjWdsForm;

	if (typeof(oForm.sWD_CurDim) != "undefined") {
		oForm.sWD_CurDim.value = GnCurDim;
	}
	if (typeof(oForm.WD_CurDimBeforeSummary) != "undefined") {
		oForm.WD_CurDimBeforeSummary.value = GnCurDimBeforeSummary;
	}	
	if (typeof(oForm.action) != "undefined") {
		oForm.action = GstrAction;
	}
	if (typeof(oForm.sWD_ItemID) != "undefined") {
		oForm.sWD_ItemID.value = GstrItemID;
	}		
	if (typeof(oForm.sWD_ItemCode) != "undefined") {
		oForm.sWD_ItemCode.value = GstrItemCode;
	}
	if (typeof(oForm.WD_Summary) != "undefined") {
		oForm.WD_Summary.value = GstrSummary;
	}
	if (typeof(oForm.WD_PageBeforeSummary) != "undefined") {
		oForm.WD_PageBeforeSummary.value = GstrPageBeforeSummary;
	}
}
// ----------------------------------------------------------------------------
function ShowRetrievingDataMessage(i_objTargetElement)
{
	var objDataTable = document.getElementById(i_objTargetElement);
	var nOffsetTop = objDataTable.offsetTop;
	var nOffsetLeft = objDataTable.offsetLeft;
	var objParent = objDataTable.offsetParent;
	var objDiv = document.createElement("DIV");

	while (objParent != null && objParent.tagName.toUpperCase() != "BODY") {
		nOffsetLeft += objParent.offsetLeft;
		nOffsetTop += objParent.offsetTop;
		objParent = objParent.offsetParent;
	}

	objDiv.id = "RetrieveData";
	objDiv.align = "left";
	objDiv.style.position="absolute";
	objDiv.innerHTML = "<FONT class='TVGeneralText'>" + resRetrievingData + "...</FONT>";
	document.body.appendChild(objDiv);

	if (PWdsapp_bIsIE) {
		objDiv.style.posTop = nOffsetTop;
		objDiv.style.posLeft = nOffsetLeft + 3;
	}
	else {
		objDiv.style.top = nOffsetTop;
		objDiv.style.left = nOffsetLeft+ 3;
	}
}

// ----------------------------------------------------------------------------    
function RemoveRetrievingDataMessage()
{
	if (document.getElementById("RetrieveData")) {
		document.getElementById("RetrieveData").style.visibility = "hidden";
	}
}

// ----------------------------------------------------------------------------
// The first time this function is called, it creates an IFRAME inside the page 
// document and then make a call to the server using the new IFRAME, otherwise 
// it just makes to the server using the IFRAME.
function loadPage(i_url)
{
	var iframeDoc;

	try {
		if (M_iframeObj == undefined && document.createElement) {
			// Create the IFrame and assign a reference to the
			// object to our global variable M_iframeObj.
			// this will only happen the first time loadPage() is called
			var tempIFrame=document.createElement('iframe');
			tempIFrame.setAttribute('id','MyIFrame');
			tempIFrame.style.border='0px';
			tempIFrame.style.width='0px';
			tempIFrame.style.height='0px';
			M_iframeObj = document.body.appendChild(tempIFrame);
			if (document.frames) {
				// Access the document object of the IFrame
				// through the document.frames array
				M_iframeObj = document.frames['MyIFrame'];
			}
		}
		if (M_iframeObj.document) {
			iframeDoc = M_iframeObj.document;
		}
	}
	catch(ex) {
		alert("failure to access IFrame");
	}

	// call a server routine from client code
	iframeDoc.POST = LoadDataToIFrame;
	// i_url: href to asp file 
	iframeDoc.POST(i_url);
}

// ----------------------------------------------------------------------------
// This function adds all the hidden fields to IFRAME form and then submits the form.
function LoadDataToIFrame(i_url)
{
	var clFormField;
	var clFormFields;
	var strName;
	var strTag;
	var strValue;
	var unIndex;

	this.open();
	this.write('<html><body>');
	this.write('<form name="iframeForm" method="post" target="" ');
	this.write(' action="' + i_url + '">');
	// Make sure the form object is defined.
	if (ObjWdsForm != undefined)
	{
		clFormFields = ObjWdsForm.childNodes;
		// Add all INPUT form field to the page.
		for (unIndex = 0; unIndex < clFormFields.length; unIndex++) {
			clFormField = clFormFields.item(unIndex);
			// Only use "element" nodes.
			if (clFormField.nodeType == 1) {
				strTag = clFormField.tagName;
				// Only use INPUT tags.
				if (strTag.toUpperCase() == "INPUT") {
					strName = clFormField.getAttribute("name"); 
					strValue = clFormField.getAttribute("value");
					this.write('<input type="hidden" name="' + strName + '" value="' + strValue + '">');
				}
			}
		}
	}
	this.write('</form></body></html>');
	this.location.replace(i_url);
	this.close();
	this.forms['iframeForm'].submit();
}
