window.onerror = handleJSError;

var bIsClientCodeLoaded = false;
var bExecuteStarted = false;

var bIsMSIE = false;
var bIsWINXP = false;
var bIsWINXP_SP2 = false;
var bIsMSIE_55_SP2_Or_Above = false;

var bIsNetscape = false;
var bIsOpera = false;
var bIsFireFox = false;

getBrowserInfo();

function getBrowserInfo() {
	var bIsSP2 = false;
	var iMajorVersion = 0;
	var iMinorVersion = 0;
	if (document.getElementById) {
		var userAgent = navigator.userAgent;
		var matchResults = navigator.appVersion.match(/(MSIE) (\d+).(\d+)/i);
		if (matchResults && (/OPERA|SPOOFER|WEBTV/i).test(userAgent) == false) {
			bIsMSIE = true;
			bIsWINXP = (/WINDOWS NT 5.1|WINDOWS XP/i).test(userAgent);
			if (matchResults.length == 4) {
				iMajorVersion = parseInt(matchResults[2]); iMajorVersion = (isNaN(iMajorVersion) ? 0 : iMajorVersion);
				iMinorVersion = parseInt(matchResults[3]); iMinorVersion = (isNaN(iMinorVersion) ? 0 : iMinorVersion);
				bIsSP2 = (navigator.appMinorVersion && (/SP2/i).test(navigator.appMinorVersion));
				bIsWINXP_SP2 = (bIsWINXP && (userAgent.indexOf("SV1") > userAgent.indexOf("MSIE")));
				bIsMSIE_55_SP2_Or_Above = (iMajorVersion >= 6 || (iMajorVersion == 5 && (iMinorVersion >= 5 || (iMinorVersion == 5 && bIsSP2))));
			}
		} else {
			bIsNetscape = (/netscape/i).test(userAgent);
			bIsOpera = (/opera/i).test(userAgent);
			bIsFireFox = (/firefox/i).test(userAgent);
		}
	}
}

function doCheckWebInstaller() {
	var oInstructionsDiv, oHideDiv, oShowDiv, oShowHowToDiv, oHideHowToDiv;
	if (bIsMSIE && document.getElementById) { //'get ActiveX Instructions DIV in case we need to show it
		if (bIsWINXP_SP2) {
			oInstructionsDiv = document.getElementById('IESP2Instructions');
		} else {
			oInstructionsDiv = document.getElementById('IEInstructions');
		}
		oHideDiv = document.getElementById('DownloadBtn');
		oShowDiv = document.getElementById('BeforeContinue');

		oHideHowToDiv = document.getElementById('IENonActiveXHowTo');
		oShowHowToDiv = document.getElementById('IEActiveXHowTo');
	}

	if (bdownload && bdownload == "true" && dlcount && dlcount > 0) { //'they clicked on the "DOWNLOAD NOW" button at least once
		if (document.WebInstallerAvailable) { //'simulate download button click
			downloadnow();
		} else {
			if (bIsMSIE) {
				if (bIsWINXP_SP2 && dlcount >= 1) {
					//'alert('SP2, no WebInstaller and first click or later\n\nshow box');
					if (oHideDiv && oHideDiv.style && oShowDiv && oShowDiv.style) {
						oHideDiv.style.display = 'none';
						oShowDiv.style.display = 'block';
					}
				} else if (dlcount > 1) {
					//'alert('non-SP2, no WebInstaller and second click or later\n\ndo not show box');
				}
			} else {
				//'alert('do nothing, not IE so no WebInstaller');
			}
		}
	} else { //'this is the first page load or SP2 refresh after accepting WebInstaller
		if (bdownload && bdownload == "true" && document.WebInstallerAvailable) {
			downloadnow();
		} else {
			//'alert('will be showing instructions');
		}
	}
//'DEBUG
//'alert('doCheckWebInstaller()\n\nbIsMSIE:'+(bIsMSIE ? "yes" : "NO")+'\nbIsWINXP_SP2:'+(bIsWINXP_SP2 ? "yes" : "NO")+'\ndocument.WebInstallerAvailable:'+(document.WebInstallerAvailable ? document.WebInstallerAvailable : "false")+'\ndlcount:'+(dlcount ? dlcount : "0")+'\nbdownload:'+(bdownload ? bdownload : "NO")+'\noHideHowToDiv:'+(oHideHowToDiv ? "yes" : "NO")+'\noHideHowToDiv.style:'+(oHideHowToDiv.style ? "yes" : "NO")+'\noShowHowToDiv:'+(oShowHowToDiv ? "yes" : "NO")+'\noShowHowToDiv.style:'+(oShowHowToDiv.style ? "yes" : "NO")+'\noInstructionsDiv:'+(oInstructionsDiv ? "yes" : "NO")+'\noInstructionsDiv.style:'+(oInstructionsDiv.style ? "yes" : "NO"));
//'DEBUG
	if (document.WebInstallerAvailable) {
		if (oHideHowToDiv && oHideHowToDiv.style && oShowHowToDiv && oShowHowToDiv.style) {
			oHideHowToDiv.style.display = 'none';
			oShowHowToDiv.style.display = 'block';
		}
	} else {
		if (bIsWINXP_SP2 || (bIsMSIE && dlcount && dlcount > 0)) { //'SP2 or clicked on download button
			if (oInstructionsDiv && oInstructionsDiv.style) {
				oInstructionsDiv.style.display = 'block';
			}
		}
	}
}

function doRetryDownload() {
	document.activate.action = "Activate_Info.asp";
	document.activate.submit();
}

function doNonActiveXDownload() { //'also called if non-popup blocker error
	window.open('http://www.peoplepc.com/connect/downloadcw.asp?bIsMSIE=false', 'download');
}

function launchICWTutorial(){
	if (!document.WebInstallerAvailable && bIsWINXP_SP2) {
		window.open('http://home.peoplepc.com/helpinfo/tutorial/icw','ICWTutorial','height=536,width=711,status=yes,toolbar=no,menubar=no,location=no,resizable=1');
	} else {
		window.open('http://home.peoplepc.com/helpinfo/tutorial/icw','ICWTutorial','height=556,width=711,status=yes,toolbar=no,menubar=no,location=no,resizable=1');
	}
}

function showHowToInstructions()
{
	if (bIsMSIE) {
		if (bIsWINXP_SP2) {
			//'Profile #3: IE XP Service Pack 2  - PPC ActiveX not installed
			document.writeln('<div id="IENonActiveXHowTo" style="display:block">');
				document.writeln('To use any service level of Internet Call Waiting, you will need to download and activate additional software by CallWave.  It only takes three minutes.');
			document.writeln('</div>');
		} else {
			//'Profile #4: IE all configurations outside XP Service Pack 2 - PPC ActiveX not installed
			document.writeln('<div id="IENonActiveXHowTo" style="display:block">');
				document.writeln('To use any service level of Internet Call Waiting, you will need to download and activate additional software by CallWave.  It only takes three minutes.');
			document.writeln('</div>');
		}
		//'Profile #1: IE ActiveX enabled
		document.writeln('<div id="IEActiveXHowTo" style="display:none">');
			document.writeln('Click the "Download Now" button to your right to begin downloading the Internet Call Waiting software.<br>It only takes three minutes.');
		document.writeln('</div>');
	} else {
		//'Profile #2 Browser other than IE
		document.writeln('<div id="NonIEHowTo" style="display:block">');
			document.writeln('Click the "Download Now" button to your right.  You will be forwarded to the CallWave site to complete the download.  It only takes three minutes.');
		document.writeln('</div>');
	}
}

function showSpecialInstructions() {
	if (bIsWINXP_SP2) {
		document.writeln('<tr><td><div id="IESP2Instructions" style="display:none;position:relative;width:560px;height:220px;border:1px solid silver;background-color:#ffffd5;">');
			document.writeln('<table border="0">');
				document.writeln('<tr><td><img src="/i/icw/red_exclamation.gif" width="14" height="14" border="0"/></td><td style="font-size:14px;color:red;">Important Note</td></tr>');
				document.writeln('<tr><td></td><td style="color:red;">To download the Internet Call Waiting software you will need ActiveX, a plug-in for Internet Explorer.  Installing ActiveX is easy, simply follow the steps below:</td></tr>');
				document.writeln('<tr><td colspan="2">');
					document.writeln('<table border="0">');
						document.writeln('<tr valign="top"><td><img src="/i/icw/roundedarrow.gif" width="68" height="91" border="0"/></td><td>1.</td><td>A yellow bar like the one below appears at the top of your browser.  Click the yellow bar and then click "Install ActiveX Control" in the drop-down menu.  The ActiveX plug-in will automatically install.<br><img src="/i/utils/spacer.gif" width="1" height="22" border="0"/><br><img src="/i/icw/Activexyellowbar.gif" width="500" height="81" border="0"/></td></tr>');
						document.writeln('<tr><td colspan="3"><img src="/i/utils/spacer.gif" width="1" height="4" border="0"/></td></tr>');
						document.writeln('<tr valign="top"><td>&nbsp;</td><td>2.</td><td>Then, click the "DOWNLOAD NOW" button to the right to download the Internet Call Waiting software.</td></tr>');
					document.writeln('</table>');
				document.writeln('</td></tr>');
			document.writeln('</table>');
		document.writeln('</div></td><td align="right"></td></tr>');
		document.writeln('<tr><td><img src="i/utils/spacer.gif" width="1" height="12" border="0"/></td></tr>');
	} else if (bIsMSIE) {
		document.writeln('<tr><td><div id="IEInstructions" style="display:none;position:relative;width:560px;height:220px;border:1px solid silver;background-color:#ffffd5;">');
			document.writeln('<table border="0">');
				document.writeln('<tr><td><img src="/i/icw/red_exclamation.gif" width="14" height="14" border="0"/></td><td>Important Note</td></tr>');
				document.writeln('<tr><td></td><td style="color:red;">To download the Internet Call Waiting software you will need ActiveX, a plug-in for Internet Explorer.  Installing ActiveX is easy, simply follow the steps below:</td></tr>');
				document.writeln('<tr><td colspan="2">');
					document.writeln('<table border="0">');
						document.writeln('<tr><td>&nbsp;</td><td>1.</td><td>Click the "DOWNLOAD NOW" button on your right.<br><img src="/i/utils/spacer.gif" width="1" height="4" border="0"/></td></tr>');
						document.writeln('<tr valign="top"><td><img src="/i/icw/roundedarrow.gif" width="68" height="91" border="0"/></td><td>2.</td><td>A dialogue box like the one below will appear on top of your screen.  Click "Yes" and follow the instruction prompts to get your Internet Call Waiting software.<br><img src="/i/utils/spacer.gif" width="1" height="6" border="0"/><br><img src="http://www.peoplepc.com/i/register/non_xp_dialogue.jpg" width="265" height="193" border="0"/></td></tr>');
					document.writeln('</table>');
				document.writeln('</td></tr>');
			document.writeln('</table>');
		document.writeln('</div></td><td align="right"></td></tr>');
		document.writeln('<tr><td><img src="i/utils/spacer.gif" width="1" height="12" border="0"/></td></tr>');
	}
}

function handleJSError(msg, url, linenumber) {
	if ((msg.indexOf("Access is denied")>=0) ||
			(msg.indexOf("Permission denied")>=0) ||
			(msg.indexOf("Unspecified error")>=0) ||
			(msg.indexOf("Server execution failed")>=0) ||
			(msg.indexOf("Call was rejected by callee")>=0) ||
			(msg.indexOf("The RPC server is unavailable")>=0) ||
			(msg.indexOf("The remote procedure call failed")>=0) ||
			(msg.indexOf("The object invoked has disconnected from its clients")>=0) ||
			(msg.indexOf("The callee (server [not server application]) is not available and disappeared; all connections are invalid.  The call did not execute")>=0)) {
			alert("Your pop-up blocker is preventing one of our windows from being displayed.\nTo see this window, you must enable pop-ups for this site.");
			return true;
	} else {
//'DEBUG
//'alert('msg='+msg+'\nurl='+url+',\nlinenumber='+linenumber+'\n');
//'DEBUG
		doNonActiveXDownload();
//'DEBUG
		return true;
	}
}

