/********************************************************************/
/*																	*/
/* This file defines a number of JavaScript functions used within	*/
/*	the Alarm.com customer website.									*/
/*																	*/
/********************************************************************/


// this function is used by pages that follow the standard template of creating
// 2 message-containing objects named "message" and "reminder"
// Calling this function hides the "message" object and reveals the "reminder" object
function changeMsg() {
	
	if (window.message != null) {
		message.style.visibility = 'hidden';
	}
	
	if (window.reminder != null) {
		reminder.style.visibility = 'visible';
	}
}  // changeMsg

// this function can be used to set the inner text of an HTML element to a save-reminder 
function saveMsg(objId) {
	document.getElementById(objId).innerHTML = "Click the 'Save' button when you're done!";
}  // saveMsg

// this function can be used to set the inner text of an HTML element to a save-reminder 
function saveMsgButtonName(objId, button_name) {
	document.getElementById(objId).innerHTML = "Click '" + button_name + "' when you're done!";
}  // saveMsg


// this function is called from the mini setup-window in order to log-out, close the
//  setup window, and return focus to (or open a new instance of) the parent window
function setupExit(targetUrl) {
	
	// NOTE: There is a known bug in some versions of IE when calling
	//  "window.opener.closed" and thus we cannot reliably tell if the parent window
	//  is still open or not.  The work-around is to assume that it is open, and if
	//  it is not, to trap the error that is thrown and open the window properly.
	// for details, see http://support.microsoft.com/default.aspx?scid=kb;EN-US;q241109
	
	// try to ask the parent-window whether its still open or not.  Need to wrap this
	//  call in an exception handler because an error will be thrown if the window
	//  is not open for us to call to.
	var parentWin;
	
	try {
		if (this.window.opener.askSetupWindow()) {
			parentWin = this.window.opener;
		}
		else {
			// this window exists (cause we got an answer), but it doesn't
			//  want us to re-use him like normal, so open a new window
			parentWin = this.window.open();
		}
	} catch (e) {
		// parent window either closed or was changed; open up a new window
		parentWin = this.window.open();
	}
	
	parentWin.location = targetUrl;
	parentWin.focus();
	
	this.window.close();
	
}  // setupLogout


// this function returns the length that a 1-line overlib box
//	should be, given the length of the text that will be shown
//	in the overlib.  (because of differences in the widths of
//	characters, this is just an estimate)
function sizeOfOverLib(textlen) {
	return ((textlen * 8) + 5);
} // sizeOfOverLib

// this function makes a help window with the url specified by the function argument
function makeHelpWindow(urlArg) {
	window.open(urlArg,'helpWindow','scrollbars=yes,toolbar=no,resizable=yes,location=no,width=600,height=500')
}  // makeHelpWindow

// this function opens the issue submittal page
function makeSubmitIssueWindow() {
	window.open('/submit_issue.asp','submitIssueWindow','scrollbars=yes,toolbar=no,resizable=yes,location=no,width=525,height=610')
}  // makeSubmitIssueWindow

// this function opens the contact-us page
function makeContactUsWindow() {
	var url = '/contact_us.asp';
	
	if (arguments.length >= 1) {
		url += '?category=' + arguments[0];
		
		if (arguments.length >= 2)
			url += '&' + arguments[1] + '=skip';
	}
	
	window.open(url,'contactUsWindow','scrollbars=yes,toolbar=no,resizable=yes,location=no,width=525,height=610')
}  // makeContactUsWindow

// this function opens the first page of the demo series
function makeDemoWindow() {   // always the same url, so no argument
	window.open('/demo_files/','demoWindow','scrollbars=no,toolbar=no,resizable=no,location=no,width=600,height=500')
}  // makeDemoWindow

// this series of function opens pop-up windows that give information about specific
//  panel conditions

// --- Panel Condition popups ---
function panelAlarmWindow() {
	var win = window.open('', 'sensorStateWin', 'scrollbars=no,toolbar=no,resizable=yes,location=no,width=400,height=300')
	win.document.writeln('<HTML>');
	win.document.writeln('<HEAD>');
	win.document.writeln('<TITLE>Alarm.com - Handling Panel Alarms</TITLE>');
	win.document.writeln('</HEAD>');
	win.document.writeln('<BODY bgcolor="#EFF2FF">');
	win.document.writeln('<B>What do I do if my panel is reporting an Alarm?</B>');
	win.document.writeln('<P>Try to determine if it is a true or false alarm.  If you know it\'s false, disarm your system by choosing Disarm from the change-setting drop down menu and clicking \'send\'.</P>');
	win.document.writeln('</BODY>');
	win.document.writeln('</HTML>');
	win.document.close();
}

function panelDuressWindow() {
	var win = window.open('', 'sensorStateWin', 'scrollbars=no,toolbar=no,resizable=yes,location=no,width=400,height=300')
	win.document.writeln('<HTML>');
	win.document.writeln('<HEAD>');
	win.document.writeln('<TITLE>Alarm.com - Handling Duress Alarms</TITLE>');
	win.document.writeln('</HEAD>');
	win.document.writeln('<BODY bgcolor="#EFF2FF">');
	win.document.writeln('<B>What do I do if my panel is reporting a Duress Alarm?</B>');
	win.document.writeln('<P>Try to determine the source of the duress alarm.  You may cancel the duress alarm by clicking the appropriate link.</P>');
	win.document.writeln('</BODY>');
	win.document.writeln('</HTML>');
	win.document.close();
}

function panelBatteryWindow() {
	var win = window.open('', 'sensorStateWin', 'scrollbars=no,toolbar=no,resizable=yes,location=no,width=400,height=300')
	win.document.writeln('<HTML>');
	win.document.writeln('<HEAD>');
	win.document.writeln('<TITLE>Alarm.com - Handling Panel Low Battery Events</TITLE>');
	win.document.writeln('</HEAD>');
	win.document.writeln('<BODY bgcolor="#EFF2FF">');
	win.document.writeln('<B>What do I do if my panel is reporting a low Battery?</B>');
	win.document.writeln('<P>Usually a low battery in the panel means that the panel is or was recently unplugged.  When the panel is unplugged it uses the battery for power.  Check your power cord and if it is unplugged, plug it in.  If not, click the \'submit issue\' link in the upper right of the main window to get help from customer service.</P>');
	win.document.writeln('</BODY>');
	win.document.writeln('</HTML>');
	win.document.close();
	win.focus();
}

function panelACWindow() {
	var win = window.open('', 'sensorStateWin', 'scrollbars=no,toolbar=no,resizable=yes,location=no,width=400,height=300')
	win.document.writeln('<HTML>');
	win.document.writeln('<HEAD>');
	win.document.writeln('<TITLE>Alarm.com - Handling Panel Loss of Power Events</TITLE>');
	win.document.writeln('</HEAD>');
	win.document.writeln('<BODY bgcolor="#EFF2FF">');
	win.document.writeln('<B>What do I do if my panel has lost AC Power?</B>');
	win.document.writeln('<P>Check that your panel has not become unplugged.  If it is unplugged, plug it back in.  If there is still no power, the location where the panel is installed has probably lost power; contact your power company.</P>');
	win.document.writeln('</BODY>');
	win.document.writeln('</HTML>');
	win.document.close();
	win.focus();
}

function panelTamperWindow() {
	var win = window.open('', 'sensorStateWin', 'scrollbars=no,toolbar=no,resizable=yes,location=no,width=400,height=300')
	win.document.writeln('<HTML>');
	win.document.writeln('<HEAD>');
	win.document.writeln('<TITLE>Alarm.com - Handling Panel Tamper Events</TITLE>');
	win.document.writeln('</HEAD>');
	win.document.writeln('<BODY bgcolor="#EFF2FF">');
	win.document.writeln('<B>What do I do if my panel is reporting a Tampering?</B>');
	win.document.writeln('<P>Find out if someone has been touching or playing with the panel.  Be careful to keep the panel in an out of the way, well-protected area, such as a basement closet.</P>');
	win.document.writeln('</BODY>');
	win.document.writeln('</HTML>');
	win.document.close();
	win.focus();
}