<!--
/******************************************************************************
$Workfile: TimeRangeWP.js $
$Revision: 7 $
$Author: Cnorton $

Copyright © 2004 OSIsoft, Inc. All rights reserved.
Unpublished - rights reserved under the copyright law of the United States. 
USE OF A COPYRIGHT NOTICE IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION 
OR DISCLOSURE. THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND TRADE SECRETS
OF OSIsoft, Inc. USE, DISCLOSURE, OR REPRODUCTION IS PROHIBITED WITHOUT THE 
PRIOR EXPRESS WRITTEN PERMISSION OF OSIsoft, Inc. RESTRICTED RIGHTS LEGEND
Use, duplication, or disclosure by the Government is subject to restrictions as
set forth in subparagraph (c)(1)(ii) of the Rights in Technical Data and 
Computer Software clause at DFARS 252.227.7013

OSIsoft, Inc.
777 Davis St., San Leandro CA 94577
*******************************************************************************/

// ======================================================================
// Class for the RtTimeRange WebPart. Handles time range client events
// ======================================================================
function RtTimeRange(WPQ)
{
	this.InitializeObject          = init;
	this.Update                    = update;
	this.RevertTimeRange           = revertTimeRange;
	
	var thisobj                    = this;
	thisobj.wpq                    = WPQ;
	thisobj.TimeRangeData_Init     = null; // Only used if connected client-side
	thisobj.StartTime              = eval("document.all.startTime" + thisobj.wpq);
	thisobj.EndTime                = eval("document.all.endTime" + thisobj.wpq);
	thisobj.WebPartProperties      = new Object();
	thisobj.lcid                   = "";
	thisobj.iCallID                = 0;
	thisobj.dateTimeFormatString   = "";
	thisobj.webServiceName         = ""; // Name of the web service
	thisobj.webServiceURL          = ""; // URL to the web service
	thisobj.webMethodName          = ""; // Name of the web service method
	thisobj.CalendarStart          = null;
	thisobj.CalendarEnd            = null;
	thisobj.ConnectionType         = "none";
	thisobj.Connected              = false;
	thisobj.WebServiceNotAvailable = ""; // Localized text
	thisobj.WebServiceNotFound     = ""; // Localized text
	thisobj.TryAgainLater          = ""; // Localized text
	thisobj.LblWebServiceName      = ""; // Localized text
	thisobj.LblWebServiceUrl       = ""; // Localized text
	
	
	// Initialize some needed variables
	function init(dateTimeFormatString, webServiceName, webServiceURL, webMethodName, 
	              wpTitle, connectionType, timeRangeData_Init)
	{
		try
		{
			if( typeof(RtTimeRange_LblWebServiceName) != "undefined" )
				thisobj.LblWebServiceName = RtTimeRange_LblWebServiceName;
			if( typeof(RtTimeRange_LblWebServiceUrl) != "undefined" )
				thisobj.LblWebServiceUrl = RtTimeRange_LblWebServiceUrl;
			if( typeof(RtTimeRange_WebServiceNotAvailable) != "undefined" )
				thisobj.WebServiceNotAvailable = RtTimeRange_WebServiceNotAvailable;
			if( typeof(RtTimeRange_WebServiceNotFound) != "undefined" )
				thisobj.WebServiceNotFound = RtTimeRange_WebServiceNotFound;
			if( typeof(RtTimeRange_TryAgainLater) != "undefined" )
				thisobj.TryAgainLater = RtTimeRange_TryAgainLater;
			
			thisobj.dateTimeFormatString = dateTimeFormatString;
			thisobj.Title = wpTitle;
			thisobj.lcid = rtLocale;
			thisobj.ConnectionType = connectionType;
			if( thisobj.ConnectionType != "none" )
				thisobj.Connected = true;
			
			// Next eval returns "undefined" if there is no client-side connection
			if( typeof(timeRangeData_Init) != "undefined" )
				thisobj.TimeRangeData_Init = eval(timeRangeData_Init);
			else
				thisobj.TimeRangeData_Init = null;
			
			if( thisobj.dateTimeFormatString.charAt(0) == "M" )
			{
				thisobj.CalendarStart = new calendar2(thisobj.StartTime);
				thisobj.CalendarStart.year_scroll = true;
				thisobj.CalendarStart.time_comp = true;
				thisobj.CalendarEnd = new calendar2(thisobj.EndTime);
				thisobj.CalendarEnd.year_scroll = true;
				thisobj.CalendarEnd.time_comp = true;
			} else {
				thisobj.CalendarStart = new calendar1(thisobj.StartTime);
				thisobj.CalendarStart.year_scroll = true;
				thisobj.CalendarStart.time_comp = true;
				thisobj.CalendarEnd = new calendar1(thisobj.EndTime);
				thisobj.CalendarEnd.year_scroll = true;
				thisobj.CalendarEnd.time_comp = true;
			}

			if( !thisobj.Connected )
				WPSC.RegisterForEvent("webPartDataConfig_WPQ_", "DataConfigModified", dataConfigModifiedCB);

			if( webServiceURL != "" && thisobj.ConnectionType != "server" ) {
				thisobj.webServiceName = webServiceName;
				thisobj.webServiceURL = webServiceURL;
				thisobj.webMethodName = webMethodName;
				RtWebService_Add(thisobj.webServiceName, thisobj.webServiceURL, webServiceAvailable, true);
			}
			// Get and store the original start/end times as set in the tool part
			var propXmlDoc;
			propXmlDoc = RtWP_LoadXML(propXmlDoc, "WebPartProperties" + thisobj.wpq); // WebPartProperties_WPQ_.XMLDocument.xml
			thisobj.WebPartProperties.OrigStartTime = propXmlDoc.selectSingleNode("TimeRangeWebPartProperties/OrigStartTime").text;
			thisobj.WebPartProperties.OrigEndTime = propXmlDoc.selectSingleNode("TimeRangeWebPartProperties/OrigEndTime").text;
		}
		catch(ex)
		{
			AddToRtWPErrors(thisobj.wpq, "[init] " + ex.message);
		}
	}
	
	function dataConfigModifiedCB(){}

	function webServiceAvailable(RtService) {
		if( RtService.available ) {
			EnableTimeRangeBtns(thisobj.wpq);
		} else {
			AddToRtWPErrors(thisobj.wpq, thisobj.WebServiceNotFound + "<br>" +
								"<br>" + thisobj.LblWebServiceName + " = " + RtService.friendlyName + 
								"<br>" + thisobj.LblWebServiceUrl + " = " + RtService.url);
		}
	}
	
	function update(cmd)
	{
		try
		{
			if( !RtWebService.RtServices[thisobj.webServiceName].available ) {
				AddToRtWPErrors(thisobj.wpq, thisobj.WebServiceNotAvailable + "<br>" +
								"<br>" + thisobj.LblWebServiceName + " = " + RtWebService.RtServices[thisobj.webServiceName].friendlyName + 
								"<br>" + thisobj.LblWebServiceUrl + " = " + RtWebService.RtServices[thisobj.webServiceName].url + 
								"<br><br>" + thisobj.TryAgainLater);
				return;
			}
			DisableTimeRangeBtns(thisobj.wpq);
			thisobj.iCallID = RtWebService[thisobj.webServiceName].callService(
								webServiceCallback, thisobj.webMethodName, 
								RtGetTimeZoneInfo(), thisobj.lcid, thisobj.dateTimeFormatString,
								thisobj.StartTime.value,
								thisobj.EndTime.value, cmd);
		}
		catch(ex)
		{
			AddToRtWPErrors(thisobj.wpq, "[update] " + ex.message);
			EnableTimeRangeBtns(thisobj.wpq);
		}
	}
	
	function webServiceCallback(result)
	{
		if(result.error)
		{
			var faultCode   = result.errorDetail.code;
			var faultString = result.errorDetail.string;
			AddToRtWPErrors(thisobj.wpq, "Web service invocation error:<br>Code = " + faultCode + "<br>String = " + faultString);
			EnableTimeRangeBtns(thisobj.wpq);
			return;
		}
		var timeRangeData = result.value;
		try
		{
			ClearRtWPErrors(thisobj.wpq);
			if( timeRangeData.ErrorMessage != "" )
			{
				AddToRtWPErrors(thisobj.wpq, timeRangeData.ErrorMessage); // Error Message
				EnableTimeRangeBtns(thisobj.wpq);
				return;
			}
			thisobj.StartTime.value = timeRangeData.StartTime;
			thisobj.EndTime.value = timeRangeData.EndTime;
			
			if( !thisobj.Connected ) {
				WPSC.RaiseEvent("webPartDataConfig", "TimeConfigModified", timeRangeData);
			} else {
			    parametersOutReadyEventArgs = new ParametersOutReadyEventArgs();
			    parametersOutReadyEventArgs.ParameterValues     = new Array(11);
			    parametersOutReadyEventArgs.ParameterValues[0]  = thisobj.StartTime.value;
			    parametersOutReadyEventArgs.ParameterValues[1]  = thisobj.EndTime.value;
			    parametersOutReadyEventArgs.ParameterValues[2]  = timeRangeData.StartTimeLocalized;
			    parametersOutReadyEventArgs.ParameterValues[3]  = timeRangeData.EndTimeLocalized;
			    parametersOutReadyEventArgs.ParameterValues[4]  = timeRangeData.CurrentTimeLocalized;
			    parametersOutReadyEventArgs.ParameterValues[5]  = timeRangeData.IsUpdating.toString().toLowerCase();
			    parametersOutReadyEventArgs.ParameterValues[6]  = timeRangeData.TimeRange;
			    parametersOutReadyEventArgs.ParameterValues[7]  = timeRangeData.TimeRangeLocalized;
			    parametersOutReadyEventArgs.ParameterValues[8]  = timeRangeData.TimeRangeUTF;
			    parametersOutReadyEventArgs.ParameterValues[9]  = timeRangeData.StartTimeUTF;
			    parametersOutReadyEventArgs.ParameterValues[10] = timeRangeData.EndTimeUTF;
				EnableTimeRangeBtns(thisobj.wpq);
				thisobj.ParametersOutReady(parametersOutReadyEventArgs);
			}
		}
		catch(ex)
		{
			AddToRtWPErrors(thisobj.wpq, "[webServiceCallback] " + ex.message);
		}
		EnableTimeRangeBtns(thisobj.wpq);
	}

	function revertTimeRange()
	{
		ClearRtWPErrors(thisobj.wpq);
		thisobj.StartTime.value = thisobj.WebPartProperties.OrigStartTime;
		thisobj.EndTime.value = thisobj.WebPartProperties.OrigEndTime;
		update("A");
	}


	// ========================================================================
	// RtTimeRange ParametersOutProvider web part communication interface class
	// ========================================================================
	thisobj.PartCommunicationInit = _partCommunicationInit;
	thisobj.PartCommunicationMain = _partCommunicationMain;
	thisobj.ParametersOutReady    = _parametersOutReady;
	thisobj.NoParametersOut       = _noParametersOut;
	
	thisobj.ConsumerErrorNotice   = "A consumer of the web part connection caused an unhandled exception: ";
	thisobj.IAmATimeRange         = true;
	thisobj.Title                 = "UNDEFINED";
	thisobj.Qualifier             = thisobj.wpq;
	

	function _partCommunicationInit()
	{
		try
		{
			// Localized error message
			if( typeof(RtTimeRange_ConsumerError) != "undefined" )
				thisobj.ConsumerErrorNotice = RtTimeRange_ConsumerError;
			
			// Set the ParameterOutProperties.
			var popInitEvArgs = new ParametersOutProviderInitEventArgs();
			popInitEvArgs.ParameterOutProperties = new Array(11);
			
			for(ctr=0; ctr<popInitEvArgs.ParameterOutProperties.length; ctr++)
			{
			    popInitEvArgs.ParameterOutProperties[ctr] = new ParameterOutProperty();
			    // Two arrays should be output inline on the page just once by RtTimeRange
				popInitEvArgs.ParameterOutProperties[ctr].Description = RtTimeRange_ParameterDescriptions[ctr];
				popInitEvArgs.ParameterOutProperties[ctr].ParameterDisplayName = RtTimeRange_ParameterDisplayNames[ctr];
				popInitEvArgs.ParameterOutProperties[ctr].ParameterName = RtTimeRange_ParameterNames[ctr];
			}
			WPSC.RaiseConnectionEvent("timeRangeParamsOut" + thisobj.Qualifier, 
			                          "ParametersOutProviderInit", popInitEvArgs);
			thisobj.Connected = true;
		}
		catch(ex)
		{
			AddToRtWPErrors(thisobj.Qualifier, "[PartCommunicationInit] " + ex.message);
		}
	}
	function _partCommunicationMain()
	{
	    pOutReadyEvArgs = new ParametersOutReadyEventArgs();
	    pOutReadyEvArgs.ParameterValues     = new Array(11);
	    pOutReadyEvArgs.ParameterValues[0]  = thisobj.TimeRangeData_Init.StartTime; // thisobj.StartTime.value;
	    pOutReadyEvArgs.ParameterValues[1]  = thisobj.TimeRangeData_Init.EndTime; // thisobj.EndTime.value;
	    pOutReadyEvArgs.ParameterValues[2]  = thisobj.TimeRangeData_Init.StartTimeLocalized; // Localized start time
	    pOutReadyEvArgs.ParameterValues[3]  = thisobj.TimeRangeData_Init.EndTimeLocalized; // Localized end time
	    pOutReadyEvArgs.ParameterValues[4]  = thisobj.TimeRangeData_Init.CurrentTimeLocalized; // Localized Current time
	    pOutReadyEvArgs.ParameterValues[5]  = thisobj.TimeRangeData_Init.IsUpdating.toString().toLowerCase();
	    pOutReadyEvArgs.ParameterValues[6]  = thisobj.TimeRangeData_Init.TimeRange;
	    pOutReadyEvArgs.ParameterValues[7]  = thisobj.TimeRangeData_Init.TimeRangeLocalized;
	    pOutReadyEvArgs.ParameterValues[8]  = thisobj.TimeRangeData_Init.TimeRangeUTF;
	    pOutReadyEvArgs.ParameterValues[9]  = thisobj.TimeRangeData_Init.StartTimeUTF;
	    pOutReadyEvArgs.ParameterValues[10] = thisobj.TimeRangeData_Init.EndTimeUTF;
		_parametersOutReady(pOutReadyEvArgs);
		delete(pOutReadyEvArgs);
	}
	function _parametersOutReady(pOutEventArgs)
	{
		try
		{
			if( thisobj.Connected )
				WPSC.RaiseConnectionEvent("timeRangeParamsOut" + thisobj.Qualifier, "ParametersOutReady", pOutEventArgs);
		}
		catch(ex)
		{
			AddToRtWPErrors(thisobj.Qualifier, "[ParametersOutReady] " + thisobj.ConsumerErrorNotice + ex.message);
		}
	}
	function _noParametersOut(){
	}
}
-->