function menuSwitch(a,b) 
{
	if (document.getElementById("expander"+a).style.display=="none") 
	{
		document.getElementById("expander"+a).style.display="block";
		document.getElementById("drill"+a).firstChild.nodeValue = "-";
		if(b != '')
			pageSwitch(b);
	}
	else 
	{
		document.getElementById("expander"+a).style.display="none";
		document.getElementById("drill"+a).firstChild.nodeValue = "+";
	}
}

function pageSwitch(b) 
{
	parent.mainframe.document.location.href=b;
}

function menuRefresh()
{
	parent.menuframe.document.location.href = "menu.html";
}

function InitializeMenu()
{
	var i = 2;
	var element = document.getElementById("expander" + i);

	while(element)
	{
		document.getElementById("expander" + i).style.display="none";
		i++;
		element = document.getElementById("expander" + i);
	}
}
function RealignMenu(a)
{
	parent.menuframe.document.getElementById("expander"+a).style.display="block";
}

function loadSelections(Inputs, form)
{
	for (var i = 0; i < Inputs.length; i++)
	{
		switch(form.elements[i].type)
		{
			case "text": 
			case "textarea":
			case "hidden":
				form.elements[i].value = Inputs[i];
				break;
	
			case "checkbox":
				if(Inputs[i] == 1)
				{
					form.elements[i].checked = true;
				}
				else
				{
					form.elements[i].checked = false;
				}
				break;

			case "radio":
				if(Inputs[i] == form.elements[i].value)
				{
					form.elements[i].checked = true;
				}
				else
				{
					form.elements[i].checked = false;
				}
				break;

	
			case "select-one":
				if(Inputs[i] >= 0 && Inputs[i] < form.elements[i].options.length)
				{
					form.elements[i].options[Inputs[i]].selected = true;
				}
				break;
		}
	}
}

function toggleDisabled(disabler, disablee, trigger)
{
	var form = document.getElementById(disabler[0]).form;
	for(var i = 0; i < form.elements.length; i++)
	{
		form.elements[i].disabled = false;
	}

	for(var i = 0; i < disabler.length; i++)
	{
		disableElement = document.getElementById(disabler[i]);
		if(disableElement)
		{
			if(disableElement.checked == trigger[i])
			{
				for(var j = 0; j < disablee[i].length; j++)
				{
					element = document.getElementById(disablee[i][j]);
					if(element)
					{
						element.disabled = true;
					}
				}
			}
		}
	}
}

function AlertInfo(siIndex, siType, szName, liStartTime, liHandledTime, liCurrTime, siDateMode, siTimeMode, szHandler, siMethod)
{
	var AlertWin;
	var kDateTime = new Date(liCurrTime * 1000);
    var mDateTime = new Date(liStartTime * 1000);
	var liDuration;
	var szType, szTime, szDuration;

	switch(siType)
	{
		case 0:
			szType = 'Temperature Sensor';
			break;

		case 1:
			szType = 'Humidity Sensor';
			break;

		case 2:
			szType = 'Dry Contact Sensor';
			break;

		case 3:
			szType = 'Water Sensor';
			break;
	}

	szTime = '';
	if(liHandledTime > -1)
	{
		szTime = TimeStrFromSeconds(liHandledTime, siDateMode, siTimeMode);
		szTime += '\n<tr><td><td>';
		liDuration = liHandledTime - liStartTime;
	}
	else
	{
		//getTime uses the browser time and subtracts it from the start time to determine durations on the fly
		//If the browser time is not synchronized with the system time, this duration computation will incorrect
		//based on how far off the browser time is from the system time (in most cases this error margin would be < 1 min)
		//liDuration = ((kDateTime.getTime() / 1000) - kDateTime.getTimezoneOffset()*60) - liStartTime;
        liDuration = (kDateTime.getTime() - mDateTime.getTime()) / 1000;
	}


	if(liDuration >= 86400)
	{
		szDuration = parseInt(liDuration / 86400) + ' days ';
		liDuration = liDuration % 86400;
		siHourRmdr = liDuration % 3600;
		szDuration += ((liDuration - siHourRmdr) / 3600) + ' hr ' + ((siHourRmdr - (siHourRmdr % 60)) / 60) + ' min';
	}
	else if(liDuration >= 3600)
	{
        siHourRmdr = liDuration % 3600;
		szDuration = ((liDuration - siHourRmdr) / 3600) + ' hr ' + ((siHourRmdr - (siHourRmdr % 60)) / 60) + ' min';
	}
	else if(liDuration > 60)
	{
		szDuration = parseInt(liDuration / 60) + ' min';
	}
	else
	{
		szDuration = 'Less than 1 minute';
	}

	if(siMethod == 1)
	{
		szHandler = "Alert dismissed by " + szHandler + ".";
	}
	else if(siMethod == 2)
	{
		szHandler = "Alert acknowledged by " + szHandler + ".";
	}

	AlertWin = window.open('', 'Alert_Window'+siIndex, 'width=360, height=150');
	if(AlertWin)
	{
		AlertWin.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">\n',
								'<html>\n',
								'<head>\n',
								'<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">\n',
								'<title>', szName, '</title>\n',
								'<link type="text/css" rel="stylesheet" href="content_styles.css">\n',
								'</head>\n',
								'<body><br>\n',
								'<table cellpadding="0" cellspacing="0" border = 0" width="100%">\n',
								'<tr><td class="Bold Rt">Sensor Type:&nbsp;', 
									'<td>',szType,'\n',
								'<tr><td class="Bold Rt">Action:&nbsp;\n',
									'<td>',szTime,
										   szHandler,'\n',
								'<tr><td class="Bold Rt">Duration:&nbsp;\n',
									'<td>',szDuration,'\n',
								'</table>\n',
								'</body>\n',
								'</html>\n');
		AlertWin.document.close();
	}
}

function TimeStrFromSeconds(liTimeInSec, siDateMode, siTimeMode)
{
	var kDateTime = new Date(liTimeInSec*1000);
	var szTime;

	//kDateTime.setTime(kDateTime.getTime() + kDateTime.getTimezoneOffset()*60000);
	var Year = kDateTime.getUTCFullYear(); //FindYear(kDateTime);
	var Month = LeadingZero(kDateTime.getUTCMonth()+1, false);
	var Day = LeadingZero(kDateTime.getUTCDate(), false);

	var Hours = kDateTime.getUTCHours();
	var Minutes = LeadingZero(kDateTime.getUTCMinutes(), false);
	var Seconds = LeadingZero(kDateTime.getUTCSeconds(), false);
	var szAMPM = '';

	switch(siDateMode)
	{
		case 0:
			szTime = Year + '-' + Month + '-' + Day;
			break;

		case 1:
			szTime = Month + '-' + Day + '-' + Year;
			break;
	
		case 2:
			szTime = Day + '-' + Month + '-' + Year;
			break;
	}

	if(siTimeMode != 0)
	{
		szAMPM = ' AM';
		if(Hours > 12)
		{
			Hours = Hours - 12;
			szAMPM = ' PM';
		}
		else if(Hours == 12)
		{
			szAMPM = ' PM';
		}
		else if(Hours == 0)
		{
			Hours = 12;
		}
	}

	Hours = LeadingZero(Hours, false);
	szTime += '  ' + Hours + ':' + Minutes + ':' + Seconds + szAMPM;

	return szTime;
}

/*function FindYear(theDate)
{
	rawYear 	= theDate.getYear();
	actualYear 	= rawYear % 100;
	actualYear += (actualYear < 38) ? 2000 : 1900;
	return actualYear;
}*/

function LeadingZero(num, hex)
{
	if ((num < 10 && hex != true) || (num < 16 && hex == true)) 
		num = "0" + num.toString();
	return num;
}

function CheckAll(form)
{
	var i;

	for(i = 0; i < form.elements.length;  i++)
	{
		if(form.elements[i].type == "checkbox")
		{
			form.elements[i].checked = true;
		}
	}
}

function ConfirmSubmit(form, message)
{
	if(window.confirm(message))
	{
		form.submit();
	}
}
