
/* External javascript file Copyright 2006 Honeywell

OVERVIEW:
Contains globally used variables and functions 

Original Author:  Bill Marshall - Honeywell

CHANGE LOG:
Init	Date		Change
----	--------	-----------------------------------------
WM 		11/05/2006  Adding Scheduler support
WM 		12/08/2006  Added new tool summary 2
WM		02/09/2007	Added means to control showing of schedule tab
WM      03/07/2007	Fixed minor issue with fixed width date and time function
WM      03/07/2007  Added dedicated function to launch tab so that auto refresh timer can be cleared 
WM      10/18/2007  Added support functions for prev next component on detail related pages
WM		02/12/2008	Fixed HHMMSSFormat function so that it returns 00:00:01 if passed a negative number
WM		02/25/2008	Added sign out link on right footer
WM		05/07/2008	Added function for inserting commas into number for more readability
WM		06/19/2008	Changed HHMMSS routine to return 0:00:00 for zero seconds
WM		09/15/2008  Added Performance Tab 
WM		11/10/2008	Added wait cursor when launching a main tab (v 3.28)
*/

// Note The VERSION appears as a footer on every page
var FactoryViewerVersion = "<table width=800><tr style='font-size: 10; color=gray'><td id='tdLeftFooter' width=250 align=left></td><td width=250></td><td width=250 align=middle>Factory Viewer™ Version 3.28</td><td id=tdRightFooter width=50><A href='SignIn.htm' style='font-size: 10; color=gray'>Sign Out</A></td></tr></table>";

var Presstop = "<table border='0' width='800' cellpadding='2'><tr align='center'><td width='191' background='tab195clear.gif' ><a id=A1 href='javascript:LaunchMainTab(1)'><b>Equipment</a></b></td><td width='191' background='tab195grey.gif'><a id=A2 href='javascript:LaunchMainTab(2)'><b>Tools</a></b></td><td width='191' background='tab195grey.gif' id=tdPerfTab><a id=A3 href='javascript:LaunchMainTab(3)'><b>Performance</a></b></td><td width='191' background='tab195grey.gif' id=tdSchedTab><a id=A4 href='javascript:LaunchMainTab(4)'><b>Schedule</a></b></td><td width='4'><img src='pix.gif' vspace='10'</td></tr></table>";

var Tooltop = "<table border='0' width='800' cellpadding='2'><tr align='center'><td width='191' background='tab195grey.gif'><a id=A1 href='javascript:LaunchMainTab(1)'><b>Equipment</a></b></td><td width='191' background='tab195clear.gif'><a id=A2 href='javascript:LaunchMainTab(2)'><b>Tools</a></b></td><td width='191' background='tab195grey.gif' id=tdPerfTab><a id=A3 href='javascript:LaunchMainTab(3)'><b>Performance</a></b></td><td width='191' background='tab195grey.gif' id=tdSchedTab><a id=A4 href='javascript:LaunchMainTab(4)'><b>Schedule</a></b></td><td width='4'><img src='pix.gif' vspace='10'</td></tr></table>";

var Performancetop = "<table border='0' width='800' cellpadding='2'><tr align='center'><td width='191' background='tab195grey.gif'><a id=A1 href='javascript:LaunchMainTab(1)'><b>Equipment</a></b></td><td width='191' background='tab195grey.gif'><a id=A2 href='javascript:LaunchMainTab(2)'><b>Tools</a></b></td><td width='191' background='tab195clear.gif' id=tdPerfTab><a id=A3 href='javascript:LaunchMainTab(3)'><b>Performance</a></b></td><td width='191' background='tab195grey.gif' id=tdSchedTab><a id=A4 href='javascript:LaunchMainTab(4)'><b>Schedule</a></b></td><td width='4'><img src='pix.gif' vspace='10'</td></tr></table>";

var Scheduletop  = "<table border='0' width='800' cellpadding='2'><tr align='center'><td width='191' background='tab195grey.gif'><a id=A1 href='javascript:LaunchMainTab(1)'><b>Equipment</a></b></td><td width='191' background='tab195grey.gif' ><a id=A2 href='javascript:LaunchMainTab(2)'><b>Tools</a></b></td><td width='191' background='tab195grey.gif' id=tdPerfTab><a id=A3 href='javascript:LaunchMainTab(3)'><b>Performance</a></b></td><td width='191' background='tab195clear.gif' id=tdSchedTab><a id=A4 href='javascript:LaunchMainTab(4)'><b>Schedule</a></b></td><td width='4'><img src='pix.gif' vspace='10'</td></tr></table>";

var Configtop = "<table border='0' width='800' cellpadding='2'><tr align='center'><td width='256' background='tab260clear.gif'><a id=A11 href='javascript:LaunchMainTab(11)'><b>Configuration</a></b></td><td width='256' background='tab260grey.gif'><a id=A1 href='javascript:LaunchMainTab(1)'><b>Equipment</a></b><td width='256'></td><td width='4'><img src='pix.gif' vspace='10'</td></tr></table>";

var strStateText = ["Offline", "Running", "2", "3", "Unplanned Downtime", "Changeover", "Idle", "Planned Downtime", "8", "9"];

function MsSince1970() { // essentially provides a unique number
	
	var dtDate = new Date();

	return dtDate.getTime();	

}

function LaunchMainTab(Ref) {
	
	if (typeof IntervalID != 'undefined') {
		if (IntervalID != "") {
			window.clearInterval(IntervalID); // this will clear auto refresh so that the pending page load can continue
		}
	}

	document.body.style.cursor = "wait";
	eval("A" + Ref + ".style.cursor = 'wait';"); // force chosen link cursor to wait
	
	switch (Ref) {
		case 1: window.location = "PressSummary.htm?" + MsSince1970(); break;
		case 2: window.location = "ToolSummary2.htm?" + MsSince1970(); break;
		case 3: window.location = "PerformanceSummary.htm?" + MsSince1970(); break;
		case 4: window.location = "ScheduleSummary.htm?" + MsSince1970(); break;
		case 11: window.location = "ConfigSummary.htm?" + MsSince1970(); break;
		case 21: window.location = "ToolSummary.htm?" + MsSince1970(); break;
		
		default: window.location = "PressSummary.htm?";
	}

}


function CreateArray(dimx) { // allows creation of javascript 2 dimensional arrays

	if (CreateArray.arguments.length == 1) {
		return new Array(dimx);

	} else {
		var multiArray = new Array(dimx)

		for (var i = 0; i < dimx; i++) {
    
			multiArray[i] = new Array(CreateArray.arguments[1]);
		}
	}
	return multiArray;
}

function HHMMFormat(Seconds) {

	var HH, MM, SS;

	HH = Trunc(Seconds / 3600);
	MM = Trunc(Seconds / 60) - HH * 60;

	if (MM < 10) MM = "0" + MM;
   
	return HH + ":" + MM;
}

function HHMMSSFormat(Seconds) {

	var HH, MM, SS;
	
	if (Seconds <= 0) {
		return "00:00:00";
	}

	HH = Trunc(Seconds / 3600);
	MM = Trunc(Seconds / 60) - HH * 60;

	SS = Seconds - (HH * 3600 + MM * 60); 

	if (HH < 10) HH = "0" + HH;

	if (MM < 10) MM = "0" + MM;

	if (SS < 10) SS = "0" + SS;
   
	return HH + ":" + MM + ":" + SS;
}


function FixedWidthDateTime(strDateTime) {

// expected format of strDateTime is DD-MM-YYYY HH:MM:SS XM (slashes in the date are also allowed)

// this function will fix the width if leading day or hour is missing.
// e.g. 2-23-2006 1:34:05 PM will become 02-23-2006 01:34:05 PM
 
	if (strDateTime.length < 19) { // then it's probably not the expected format
 
		return strDateTime; // just return what was passed in
	}

	var MainSeg = strDateTime.split(" ")

	if (strDateTime.indexOf("/", 0) > 0) {

		var DateSeg = MainSeg[0].split("/");
		var DateChar = "/"
	}
	else if (strDateTime.indexOf("-", 0) > 0) {
		var DateSeg = MainSeg[0].split("-");
		var DateChar = "-"
	}

	if (DateSeg[0] < 10 && DateSeg[0].length < 2) DateSeg[0] = "0" + DateSeg[0]; // month
	if (DateSeg[1] < 10 && DateSeg[1].length < 2) DateSeg[1] = "0" + DateSeg[1]; // day

	var TimeSeg = MainSeg[1].split(":")

	if (TimeSeg[0] < 10 && TimeSeg[0].length < 2) TimeSeg[0] = "0" + TimeSeg[0]; // hr
	if (TimeSeg[1] < 10 && TimeSeg[1].length < 2) TimeSeg[1] = "0" + TimeSeg[1]; // min
	
	return DateSeg[0] + DateChar + DateSeg[1] + DateChar + DateSeg[2] + " " + TimeSeg[0] + ":" + TimeSeg[1] + ":" + TimeSeg[2] + " " + MainSeg[2];
	
}


function Get24HourFormat(strDateTime) { 

	// we assume strDateTime will be in this fixed format: MM/DD/YYYY HH:NN:SS XM
	// example of = "03/17/2006 11:03:22 PM"
	
	var strTemp = new String();
	var str24HourFormat = new String();
	var strHour; 
	var str24;

	strTemp = strDateTime;

	if (strTemp.length < 22) return strDateTime; // wrong format

	strHour =  strTemp.slice(11,13);

	if (strTemp.indexOf("PM", 0) > 0 && 1 * strHour < 12) { 

		str24 = 1 * strHour + 12 + ":";

		str24HourFormat = strTemp.replace(strHour + ":", str24);
	
		str24HourFormat = str24HourFormat.substring(0,19); // nix the PM

		return str24HourFormat;

	}
	else {

		return strTemp.substring(0,19); // nix the AM/PM
	}

}

function Commafy(strNumber) {
	
	var strBuild = "";
	var Count = 1;
	
	strNumber = "" + strNumber; // cast into string
	
	if (strNumber.length <= 3 || strNumber.search("-") > 0 || strNumber.search(",") > 0) return strNumber;
	
	for (var i = strNumber.length-1; i >=0; --i) {
		
		if (Count > 3) {
			strBuild = strNumber.charAt(i) + "," + strBuild;  
			Count = 1;
		}
		else strBuild = strNumber.charAt(i) + strBuild;
		
		++Count;
	}
	
	return strBuild;
}

function Trunc(n) { return (n|0) }

function SaveDataToCookie() {
	
	if (DigestData()) {
		
		SetCookie("NetName", strNetNames);
		SetCookie("PressName", strPressNames);
		SetCookie("UserName", txtUserName.value );
		
		return true;
	}
	else
	{
		return false;
	}
}

function ShowWait() { // for long links

	window.status = "Please Wait, Loading...";

}

// note: we may want to separate out the subtab functions into separate .js

var nCurrentSubTab; // set at page level

var bLaunchingSubTab = false;

function LaunchSubTab(nTab, strPage) {
	
	if (strPage != "") {

		bLaunchingSubTab = true;
	
		ShowWait();

		eval("tdSubTab" + nTab + ".style.cssText = 'FONT-WEIGHT: bold; TEXT-DECORATION: underline';");
	
		eval("tdSubTab" + nTab + ".style.backgroundImage = 'url(" + "tab100clear.gif" + ")'");

		eval("tdSubTab" + nCurrentSubTab + ".style.backgroundImage = 'url(" + "tab100grey.gif" + ")'");
		eval("tdSubTab" + nCurrentSubTab + ".style.cssText = 'FONT-WEIGHT: normal; TEXT-DECORATION: none';");


		eval("tdSubContent.innerHTML = '<BR><BR><BR><BR><BR>&nbsp&nbsp;Loading...';");		

		eval("document.body.style.cursor='wait';");
		
		for (var i = 1; i < 9; ++i) { eval("tdSubTab" + i + ".style.cursor='wait';"); }

		if (typeof IntervalID != 'undefined') window.clearTimeout(IntervalID);

		window.setTimeout("window.location = '" + strPage + "'", 200); // using a delay before launch will allow tabs to properly behave

	} 

	nCurrentSubTab = nTab;

}

function ShowSubTabHover(nTab) {

	if (nTab != nCurrentSubTab && !bLaunchingSubTab) {

		eval("aSubText" + nTab + ".style.backgroundColor = 'yellow'");
	}
}

function ShowSubTabOut(nTab) {

	if (nTab != nCurrentSubTab) {

		eval("aSubText" + nTab + ".style.backgroundColor = 'transparent'");
	}
}

function ShowSubTabs(nTabToShow) {

	nCurrentSubTab = nTabToShow;

	document.write("<TABLE width=802 cellspacing=0 cellpadding=0 border=0>"); // table for tabs

	document.write("<TR height=24 style='CURSOR: pointer; FONT-SIZE: 8pt; FONT-WEIGHT: normal; COLOR: #0000FF'>");

	document.write("<td><img src='pix.gif' vspace=11></td>");

	//switch (red) {
		//case 1: result = 'one'; break;
		//case 2: result = 'two'; break;
		//default: result = 'unknown';
	//}

	document.write("<td id=tdSubTab1 align='center' width=100 background='tab100grey.gif' title='Click for Machine Detail' onmouseover='ShowSubTabHover(1)' onmouseout = 'ShowSubTabOut(1)' onclick=\"javascript:LaunchSubTab(1, 'PressDetail.htm?ComponentID=' + ComponentID + '&UM=' + MsSince1970())\"><A id=aSubText1>Detail</A></td>");
	document.write("<td id=tdSubTab2 align='center' width=100 background='tab100grey.gif' title='Click for Machine Event Log' onmouseover='ShowSubTabHover(2)' onmouseout = 'ShowSubTabOut(2)' onclick=\"javascript:LaunchSubTab(2, 'EventLog.htm?ComponentID=' + ComponentID + '&UM=' + MsSince1970())\"><A id=aSubText2>Event Log</A></td>");
	document.write("<td id=tdSubTab3 align='center' width=100 background='tab100grey.gif' title='Click for Machine Downtime Summary' onmouseover='ShowSubTabHover(3)' onmouseout = 'ShowSubTabOut(3)' onclick=\"javascript:LaunchSubTab(3, 'Downtime.htm?ComponentID=' + ComponentID + '&UM=' + MsSince1970())\"><A id=aSubText3>Downtime</A></td>");
	document.write("<td id=tdSubTab4 align='center' width=100 background='tab100grey.gif' title='Click for Machine Job Summary' onmouseover='ShowSubTabHover(4)' onmouseout = 'ShowSubTabOut(4)' onclick=\"javascript:LaunchSubTab(4, 'JobSummary.htm?ComponentID=' + ComponentID + '&UM=' + MsSince1970())\"><A id=aSubText4>Job List</A></td>");
	document.write("<td id=tdSubTab5 align='center' width=100 background='tab100grey.gif' title='Click for Machine Graphs' onmouseover='ShowSubTabHover(5)' onmouseout = 'ShowSubTabOut(5)' onclick=\"javascript:LaunchSubTab(5, 'SpeedTrend.htm?ComponentID=' + ComponentID + '&UM=' + MsSince1970())\"><A id=aSubText5>Graphs</A></td>");
	document.write("<td id=tdSubTab6 align='center' width=100 background='tab100line.gif'>&nbsp;</td>");
	document.write("<td id=tdSubTab7 align='center' width=100 background='tab100line.gif'>&nbsp;</td>");
	document.write("<td id=tdSubTab8 align='center' width=100 background='tab100line.gif'>&nbsp;</td>");
	document.write("<td></td>");
	
	eval("tdSubTab" + nTabToShow + ".style.cssText = 'FONT-WEIGHT: bold; TEXT-DECORATION: underline';");
	
	eval("tdSubTab" + nTabToShow + ".style.backgroundImage = 'url(" + "tab100clear.gif" + ")'");
	
	document.write("</TR>");
	document.write("<TR>"); // Tab content area row start 
	document.write("<TD width=1 bgcolor='black'></TD>"); // Top tab folder line
	document.write("<TD id=tdSubContent colspan='8' align='left'>");

}

function ShowConfigSubTab() {
	var nTabToShow = 1;

	nCurrentSubTab = 1;

	document.write("<TABLE width=802 cellspacing=0 cellpadding=0 border=0>"); // table for tabs

	document.write("<TR height=24 style='CURSOR: pointer; FONT-SIZE: 8pt; FONT-WEIGHT: normal; COLOR: #0000FF'>");

	document.write("<td><img src='pix.gif' vspace=11></td>");


	document.write("<td id=tdSubTab1 align='center' width=100 background='tab100grey.gif' title='Config Detail' onmouseover='ShowSubTabHover(1)' onmouseout = 'ShowSubTabOut(1)' onclick=\"javascript:LaunchSubTab(1, 'ConfigDetail.htm?ComponentID=' + ComponentID + '&UM=' + MsSince1970())\"><A id=aSubText1>Reasons</A></td>");
	
	document.write("<td id=tdSubTab2 align='center' width=100 background='tab100line.gif'>&nbsp;</td>");
	document.write("<td id=tdSubTab3 align='center' width=100 background='tab100line.gif'>&nbsp;</td>");
	document.write("<td id=tdSubTab4 align='center' width=100 background='tab100line.gif'>&nbsp;</td>");
	document.write("<td id=tdSubTab5 align='center' width=100 background='tab100line.gif'>&nbsp;</td>");
	document.write("<td id=tdSubTab6 align='center' width=100 background='tab100line.gif'>&nbsp;</td>");
	document.write("<td id=tdSubTab7 align='center' width=100 background='tab100line.gif'>&nbsp;</td>");
	document.write("<td id=tdSubTab8 align='center' width=100 background='tab100line.gif'>&nbsp;</td>");
	document.write("<td></td>");
	
	eval("tdSubTab" + nTabToShow + ".style.cssText = 'FONT-WEIGHT: bold; TEXT-DECORATION: underline';");
	
	eval("tdSubTab" + nTabToShow + ".style.backgroundImage = 'url(" + "tab100clear.gif" + ")'");
	
	document.write("</TR>");
	document.write("<TR>"); // Tab content area row start 
	document.write("<TD width=1 bgcolor='black'></TD>"); // Top tab folder line
	document.write("<TD id=tdSubContent colspan='8' align='left'>");

}

function GotoCompPage(strURL) {
	document.body.style.cursor = "wait";
	aCompPrev.style.cursor = "wait";
	aCompNext.style.cursor = "wait";
	
	window.status = "Please wait, loading page..."
	window.location = strURL + "&UM=" + MsSince1970();
}

function PrevNextComponentHTML(strPage) {
	
	return "<A id='aCompPrev' href='javascript:' style='FONT-SIZE: 8pt'> <-Prev </A>&nbsp;&nbsp;" + "<A id='aCompNext' href='javascript:' style='FONT-SIZE: 8pt'>Next -></A>";
}

function SetCookie(sName, sValue) // never expires
{
  document.cookie = sName + "=" + escape(sValue) + "; expires=Mon, 31 Dec 2100 23:59:59 UTC;";
}

function GetCookie(sName)
{
  // cookies are separated by semicolons
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    // a name/value pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) 
      return unescape(aCrumb[1]);
  }

  // a cookie with the requested name does not exist
  return null;
}

function GetQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
}

//function AddExternal(strLoc,strFile) {
	// strLoc e.g. "body" or "head"
	
	//var fileref=document.createElement('script')
	//fileref.setAttribute("type","text/javascript")
	//fileref.setAttribute("src", strFile)
	//if (typeof fileref!="undefined") {
  		//document.getElementsByTagName(strLoc)[0].appendChild(fileref)
		//return true;
	//}
	//else {
		//return false;
	//}
//}




