//====================================================================================
//====================================================================================
//== EEM6Alarms.js
//====================================================================================
//== JavaScript file that contains the objects used to deal with alarms/events
//====================================================================================
//====================================================================================

function AlarmObject(){
	this.AlarmID								= null;
	this.Active									= null;
	this.Name										= null;
	this.Type										= null;
	this.Processed							= null;
	this.StartDate							= null;
	this.EndDate								= null;
}


function AlarmsManager(ObjectName){
	this.ObjectName						= ObjectName;
	this.WindowFrameset					= null;
	this.WindowHeader					= null;
	this.WindowMain						= null;
	this.WindowControl					= null;
	this.Frameset						= null;
	this.Action						= ALARM_WINDOW_LIST;
	this.AlarmWindow					= null;
	this.AlarmSelected					= new AlarmObject();
	this.EventsAlarmID					= -1;
	this.AlarmID						= function(oAlarm){
		if (NotNull(oAlarm)){
			return oAlarm.AlarmID;
		}else{
			return null;
		}
	}
	//== _Loading								Set the main alarm output window to "loading..."
	this._Loading								= function(sGIF){
		if (NotNull(this.WindowMain)){
			this.Frameset.rows			= "20,*,0,0";
			this.WindowMain.location.assign(PATH_HTML + "AlarmLoading.html");
		}
		if (NotNull(this.WindowHeader)){
			//== Set the header image
			this.WindowHeader.SetImage(sGIF);
		}
	}
	this._CheckWindows					= function(){
		var bRet								= false;
		if (NotNull(this.WindowMain)){
			if (NotNull(this.WindowControl)){
				bRet							= true;
			}else{
				alert("Alarm control window has not been set");
			}
		}else{
			alert("Alarm main output window has not been set");
		}
		return bRet;
	}
	this.OpenWindow							= function(iAction){	
		var sURL									= PATH_HTML + "fsetAlarms.html";
		var sOptions							= "directories=no,location=no,menubar=no,toolbar=no,resizable=yes,height=400,width=620";
		sOptions									= sOptions + ",top=" + (screen.height-400)/2;
		sOptions									= sOptions + ",left=" + (screen.width-600)/2;
		//== Set the Action so "fsetAlarms.html" knows what screen to open up with
		this.Action 							= iAction;
		var oWin									= this.AlarmWindow;
		if (NotNull(oWin)){
			if (oWin.closed){
				oWin									= null;
			}
		}
		if (NotNull(oWin)){
			oWin.location						= sURL;
			oWin.focus();
		}else{
			oWin										= window.open(sURL, "_blank", sOptions);
		}
		this.AlarmWindow					= oWin;
	}
	//== ContentsSet						Decide what type of information we are displaying
	this.ContentsSet						= function(iAlarmWindow){
		var sControl							= this.WindowControl.location;
		var sRows									= this.Frameset.rows;
		var sTemp;
		this.Action								= iAlarmWindow;
		switch (iAlarmWindow){
			case ALARM_WINDOW_LIST:
				//== Load the main "control" frame
				sControl							= PATH_HTML + "fAlarmControl.html";
				sRows								= "20,0,*,82";
				break;
			case ALARM_WINDOW_NEW:
				sControl							= PATH_HTML + "fAlarmCreateControl.html";
				sRows								= "20,0,*,58";
				break;
			case ALARM_WINDOW_MODIFY:
				sControl 							= PATH_HTML + "fAlarmDetailControl.html";
				sRows								= "20,0,*,30";
				break;
			case ALARM_WINDOW_RECIPIENT_MODIFY:
				sControl 							= PATH_HTML + "fAlarmRecipientControl.html";
				sRows								= "20,0,*,30";
				break;
			case ALARM_WINDOW_RECIPIENT_ADD:
				sGIF								= "alarmHeaderRecAdd";
				sControl							= PATH_HTML + "fAlarmAccountControl.html";
				sRows								= "20,0,*,30";
				break;
			case ALARM_WINDOW_EVENTS:
				sControl							= PATH_HTML + "fAlarmEventControl.html";
				sRows								= "20,0,*,70";
				break;
			default:
				alert("SetContents: Invalid Alarm Window ID of " + iAlarmWindow);
				return false;
		}
		//== Only reset the location if it has changed
		sTemp											= "" + this.WindowControl.location;
		sTemp											= sTemp.toUpperCase();
		if (sTemp.indexOf(sControl.toUpperCase()) < 0){
			this.WindowControl.location			= sControl;
		}
		if (this.Frameset.rows != sRows){
			this.Frameset.rows							= sRows;
		}
	}
	//== AlarmList							Get the list of alarms based on current filter
	this.AlarmList							= function(bRefresh){
		bRefresh						= DefaultValue(bRefresh, false);
		if (this._CheckWindows()){
			var dest					= this.WindowMain;
			var doc						= this.WindowControl.document;
			this._Loading("alarmHeaderList");
			var frm						= doc.forms(0);	
			var sParams					= "";
                        DebugText("==> AlarmList: Checking document's form");
			if (NotNull(frm)){
                                DebugText("==> AlarmList: has forms(0)");
				if (doc.IsAlarmList == true){
                                        DebugText("==> AlarmList: IsAlarmList is true");
					sParams				= EEM6().Session.FormParams(frm);
				}
			}
			if (bRefresh){
				sParams					= "&Refresh=true" + sParams;
			}
			with (EEM6().Session){
				var oReq				= new RequestObj(dest, "/AlarmList", sParams, true);
				oReq.onLoad				= "EEM6().Alarms.ContentsSet(" + ALARM_WINDOW_LIST + ");" ;
				AddRequest(oReq);
			}
		}
	}
	//== AlarmDetail							display the detail screen for an alarm
	this.AlarmDetail							= function(AlarmID){
		if (this._CheckWindows()){
			this._Loading("alarmHeaderModify");
			var dest									= this.WindowMain;
			with (EEM6().Session){
				var oReq								= new RequestObj(dest, "/AlarmDetail", "AlarmID=" + AlarmID, true);
				oReq.onLoad							= "EEM6().Alarms.ContentsSet(" + ALARM_WINDOW_MODIFY + ");" ;
				AddRequest(oReq);
			}
		}
	}
	//== AlarmUpdate							updates information for an alarm
	this.AlarmUpdate							= function(sParams){
		if (this._CheckWindows()){
			var dest									= this.WindowMain;
			this._Loading("alarmHeaderList");
			with (EEM6().Session){
				var oReq								= new RequestObj(dest, "/AlarmUpdate", sParams, true);
				oReq.onLoad							= "EEM6().Alarms.ContentsSet(" + ALARM_WINDOW_LIST + ");" ;
				AddRequest(oReq);
			}
		}
	}
	//== AlarmDelete							delete a alarm
	this.AlarmDelete							= function(AlarmID){
		if (this._CheckWindows()){
			var dest									= this.WindowMain;
			this._Loading("alarmHeaderList");
			with (EEM6().Session){
				var oReq								= new RequestObj(dest, "/AlarmDelete", "AlarmID=" + AlarmID, true);
				oReq.onLoad							= "EEM6().Alarms.ContentsSet(" + ALARM_WINDOW_LIST + ");" ;
				AddRequest(oReq);
			}
		}
	}
	//== AlarmCandidates				Get the list of reports that we may want to create an alarm for
	this.AlarmCandidates				= function(){
		if (this._CheckWindows()){
			var dest								= this.WindowMain;
			this._Loading("alarmHeaderNew");
			with (EEM6().Session){
				var oReq								= new RequestObj(dest, "/AlarmCandidates", "", true);
				oReq.onLoad							= "EEM6().Alarms.ContentsSet(" + ALARM_WINDOW_NEW + ");" ;
				AddRequest(oReq);
			}
		}
	}
	//== AlarmCreate						create a new alarm from a report ID
	this.AlarmCreate						= function(ReportID){
		if (this._CheckWindows()){
			var dest								= this.WindowMain;
			this._Loading("alarmHeaderList");
			with (EEM6().Session){
				var oReq							= new RequestObj(dest, "/AlarmCreate", "ReportID=" + ReportID, true);
//  below commented/replaced out on 9/18/03 to fix ET#1830
// 				oReq.onLoad							= "EEM6().Alarms.Frameset.rows='25,0,*,30';";
				oReq.onLoad							= "EEM6().Alarms.ContentsSet(" + ALARM_WINDOW_LIST + ");" ;
				AddRequest(oReq);
			}
		}
	}
	//== RecipientRemove				remove a recipient from an alarm
	this.RecipientRemove				= function(AlarmID, AccountID){
		if (this._CheckWindows()){
			var dest								= this.WindowMain;
			this._Loading("alarmHeaderModify");
			with (EEM6().Session){
				var sParams						= "&AlarmID=" + AlarmID + "&AccountID=" + AccountID;
				var oReq							= new RequestObj(dest, "/AlarmRecipientDelete", sParams, true);
				oReq.onLoad						= "EEM6().Alarms.ContentsSet(" + ALARM_WINDOW_MODIFY + ");" ;
				AddRequest(oReq);
			}
		}
	}
	//== RecipientCandidates		Get the list of candidate accounts for adding as recipients
	this.RecipientCandidates		= function(AlarmID){
		if (this._CheckWindows()){
			var dest								= this.WindowMain;
			this._Loading("alarmHeaderAccounts");
			with (EEM6().Session){
				var sParams						= "&AlarmID=" + AlarmID;
				var oReq								= new RequestObj(dest, "/AlarmRecipientCandidates", sParams, true);
				oReq.onLoad							= "EEM6().Alarms.ContentsSet(" + ALARM_WINDOW_RECIPIENT_ADD + ");" ;
				AddRequest(oReq);
			}
		}
	}
	//== RecipientsAdd					Add one or more recipients to an alarm	
	this.RecipientsAdd					= function(sParams){
		if (this._CheckWindows()){
			var dest								= this.WindowMain;
			this._Loading("alarmHeaderModify");
			with (EEM6().Session){
				var oReq								= new RequestObj(dest, "/AlarmRecipientAdd", sParams, true);
				oReq.onLoad							= "EEM6().Alarms.ContentsSet(" + ALARM_WINDOW_MODIFY + ");" ;
				AddRequest(oReq);
			}
		}
	}
	//== RecipientModify				Display the detail page for a recipient so the user can edit the information
	this.RecipientModify				= function(AccountID){
		if (this._CheckWindows()){
			var dest								= this.WindowMain;
			this._Loading("alarmHeaderRecipient");
			with (EEM6().Session){
				var sParams						= "&AccountID=" + AccountID;
				var oReq							= new RequestObj(dest, "/AlarmRecipientDetail", sParams, true);
				oReq.onLoad						= "EEM6().Alarms.ContentsSet(" + ALARM_WINDOW_RECIPIENT_MODIFY + ");" ;
				AddRequest(oReq);
			}
		}
	}
	//== RecipientUpdate				update a recipients information
	this.RecipientUpdate				= function(sParams){
		if (this._CheckWindows()){
			var dest								= this.WindowMain;
			this._Loading("alarmHeaderModify");
			with (EEM6().Session){
				var oReq								= new RequestObj(dest, "/AlarmRecipientUpdate", sParams, true);
				oReq.onLoad							= "EEM6().Alarms.ContentsSet(" + ALARM_WINDOW_MODIFY + ");" ;
				AddRequest(oReq);
			}
		}
	}
	//== EventList							Get the list of events based on current filter
	this.EventList							= function(AlarmID){
		AlarmID										= DefaultValue(AlarmID, -1);
		this.EventsAlarmID				= AlarmID;
		if (this._CheckWindows()){
			var dest								= this.WindowMain;
			var doc									= this.WindowControl.document;
			this._Loading("alarmHeaderEvents");
			var frm									= doc.forms(0);	
			var sParams							= "&AlarmID=" + AlarmID;
			if (NotNull(frm)){
				if (frm.id == "form_events") {
					sParams								= sParams + "&" + EEM6().Session.FormParams(frm);
				}
			}
			with (EEM6().Session){
				var oReq								= new RequestObj(dest, "/AlarmEventList", sParams, true);
				oReq.onLoad							= "EEM6().Alarms.ContentsSet(" + ALARM_WINDOW_EVENTS + ");" ;
				AddRequest(oReq);
			}
		}
	}
	//== Graph
	this.Graph										= function(ReportID, sStart, sEnd){
		//== Find the corresponding button on the side menu bar
		var oButton									= EEM6().SideMenuBar.ButtonFromReportID(ReportID);
		if (NotNull(oButton)){
			//== Select the button, make it visible, but do NOT fire off normal code
			EEM6().SideMenuBar.SelectButton(oButton.ArrayIndex, true, false);
			//== Calculate the extra parameters to pass to the /ReportLoad request

			//== If start/end dates are the same day (pricing alarms can be) then:
			//   - increment end date by one day
			//   - set time of day and time range using the time section of the dates

			arStart = sStart.split(" ");
			arEnd   = sEnd.split(" ");
			tRange = "";
			if(arStart[0] == arEnd[0])
			  {sEnd = this.IncDay(arEnd[0]);
			   TimeFrom = this.TimeRange(arStart[1], arStart[2], "F");
			   TimeTo = this.TimeRange(arEnd[1], arEnd[2], "T");
			   tRange = TimeFrom + TimeTo;}
			  
			var sParams								= "&AutoChart=true" +
												  "&StartDate=" + sStart +
												  "&EndDate=" + sEnd;
			if(tRange != "")
			  sParams += "&TimeOfDay=i" + tRange;
			
			//== Now select the report
			EEM6().Reports.ReportSelect(ReportID, true, sParams)
			//== Set focus back to the main window
			this.WindowFrameset.blur();
			EEM6().focus();
		}else{
			alert("Could not graph related report.  You might not have access to the corresponding report.");
		}
	}
	//== IncDay
	this.IncDay							= function(tDate) {
		arDate = tDate.split("/");
		myDate = new Date(arDate[2], arDate[0]-1, arDate[1]);
		myDate.setDate(myDate.getDate()+1);
		m = myDate.getMonth() + 1;
		d = myDate.getDate();
		y = myDate.getFullYear();
		return m + "/" + d + "/" + y;
	}
	//== TimeRange
	this.TimeRange							= function(tTime, Period, src) {
		arTime = tTime.split(":");
		if(arTime[0].length == 2 && arTime[0].charAt(0) == "0")
		  arTime[0] = arTime[0].charAt(1);
		if(Period.toLowerCase() == "pm" && arTime[0] != "12")
		  arTime[0] = parseInt(arTime[0], 10) + 12;
		if(Period.toLowerCase() == "am" && arTime[0] == "12")
		  arTime[0] = "0";
		if(src == "F" && arTime[0] == "0" && arTime[1] == "00")
		  arTime[1] = "15";
		if(src == "T" && arTime[0] == "24" && arTime[1] != "00")
		  arTime[1] = "00";
		  
		if(src == "F")
		  return "&TimeFromHr=" + arTime[0] + "&TimeFromMIN=" + arTime[1];
		else
		  return "&TimeToHr=" + arTime[0] + "&TimeToMIN=" + arTime[1];
	}
}

