var answerPage = "question.asp";  // Global constants and variables
var brname;                       // Variables for fullscreen switching
var brver;                        // Variables for fullscreen switching
var contentTop = null;            // Variables for fullscreen switching
var contentLeft = null;           // Variables for fullscreen switching
var fixedW;                       // Variables for fullscreen switching
var fixedH;                       // Variables for fullscreen switching
var initialWidth = null;          // Variables for fullscreen switching
var initialHeight = null;         // Variables for fullscreen switching
var initialTop = null;            // Variables for fullscreen switching
var initialLeft = null;           // Variables for fullscreen switching
var initialBrowserLeft = null;    // Variables for fullscreen switching
var initialBrowserTop = null;     // Variables for fullscreen switching
var qdsWindow = null;             // QDS Window variable
var repeatTimeout;                // Variables for fullscreen switching
var session_notify;               // Variables for fullscreen switching
var staffDonorMode = "DonorMode"; // Global constants and variables
var testMode = 0;                 // Variables for fullscreen switching
var timeout;                      // Variables for fullscreen switching

//===B===button================================================
var NSbuttonRef = "document.button"

function buttonUp(bid, textLeft, textTop) {
}

function buttonUnClick(bid, textLeft, textTop) {
	if (document.all)	{
		document[bid].src = eval(bid + "up.src");
		ref = "document.all.button" + bid + "Text.style"
		eval(ref).pixelTop = textTop
		eval(ref).pixelLeft = textLeft}
	else	{
		eval(NSbuttonRef + bid + "Pic").document[bid].src = eval(bid + "up.src");
		eval(NSbuttonRef + bid + "Text").top = textTop
		eval(NSbuttonRef + bid + "Text").left = textLeft}
}

function buttonOver(bid, textLeft, textTop){
}

function buttonDisable(bid) {
	if (document.all)	{
		ref = "document.all.button" + bid + "Zone.style"
		eval(ref).visibility = "visible"}
	else	{
		eval(NSbuttonRef + bid + "Zone").visibility = "show"}
}

function buttonReleased(bid, textLeft, textTop) {
	buttonUnClick(bid, textLeft, textTop)	//May need a delay function for about 50 mSec.
	delayFlag = 0
	buttonDisable(bid)
	buttonClick(bid)
}

//===B===browserCheck==========================================

function browserCheck() { // Check for a compatible browser n Remove hard coded return value.
  var brName = null;
  var brVersion = 0;
    	brVersion = parseFloat(navigator.appVersion);
  	if (document.all) {brName = "IE";} // Internet Explorer Detected
  	else if (document.layers)	{brName = "Netscape";} // Netscape Navigator Detected
      else if (document.getElementById) {
    		  brName = "Netscape";
    		  brVersion = 6;} // Netscape 6 Detected
    else {brName = "XX";} // Unrecognized Browser Detected
  	      
 if (brName != "IE") {return false;}
 else	{return true;} 
}

//===C===checkKey==============================================

function checkKey(k) {
  checkKeyPress(window.event);
}

function checkKey() {
  if (window.parent != window.self) {parent.checkKeyPress(window.event);}
  else { checkKeyPress(window.event);}
}

//===C===checkKeyPress=========================================

function checkKeyPress(eventObj) {
  var keyName = null;
  var returnCode = false;
  var altKeyCode = 18;
  var shiftKeyCode = 16;
  var ctrlKeyCode = 17;
  var AKeyCode = 65;
  var DKeyCode = 68;
  var PKeyCode = 80;
  var QKeyCode = 81;
  var RKeyCode = 82;
  var TKeyCode = 84;
  
  // Decode the key data
  if (!eventObj.altKey && eventObj.type == "keydown" && eventObj.keyCode != shiftKeyCode && eventObj.keyCode != altKeyCode && eventObj.keyCode != ctrlKeyCode)
  {    // This key was not a Control, Shift or Alt key and the Alt key is not down
	  if (eventObj.ctrlKey && eventObj.shiftKey) {
	    if (eventObj.keyCode == AKeyCode) keyName = "CSA";
	    else if (eventObj.keyCode == DKeyCode) keyName = "CSD";
	    else if (eventObj.keyCode == PKeyCode) keyName = "CSP";
	    else if (eventObj.keyCode == QKeyCode) keyName = "CSQ";
	    else if (eventObj.keyCode == RKeyCode) keyName = "CSR";
	    else if (eventObj.keyCode == TKeyCode) keyName = "CST";
	  }
  }
	if (keyName != null)
	{	  // Handle the event at this level
	  eventObj.cancelBubble = true;
	  eventObj.returnValue = false;
	  if (keyName == "CSD") {returnCode = NavStaff(eventObj);}
	  else if (keyName == "CSP") {returnCode = printPage();}
	  else if (keyName == "CSA") {returnCode = goStopQDS();}
	  else if (keyName == "CSR") {returnCode = ResetDonorSession();}
	  else if (keyName == "CSQ") {returnCode = goStopQDS();}
	  else if (keyName == "CST") {returnCode = ToggleTestMode();}
	}
}

//===C===CurrentDonorLocation==================================

function CurrentDonorLocation() {// Return the curent location if currently in the donor mode
  if (staffDonorMode != "DonorMode" ) {return "";}
  else {return  document.location.href;}
}

//===F===FocusQDS==============================================

function FocusQDS() {
  if (qdsWindow != null) { // Set the focus on this window
    try {
      window.focus();
      qdsWindow.close();
      qdsWindow = null;
    }
    catch(e) {}// Ignore error.  The window may have been closed already.
    qdsWindow = null;
  }
}

//===F===Forms Buttons==================================

function FormCredits() {
  document.location = "creditsOld.asp"
}

function FormHome() {
  document.location = "default.asp"
}

function FormPrivacy() {
  document.location = "privacyP.asp"
}

//===F===fullScreen============================================

function fullScreen() {
  var hdiff;
  if (initialWidth == null) {  // Save the initial screen parmeters
    initialWidth = document.body.offsetWidth; // Get the size of the content area 
    initialHeight = document.body.offsetHeight;
    
    initialTop = window.screenTop;    // Get the position of the content area relative to the screen boundary.
    initialLeft = window.screenLeft;

    window.moveTo(0, 0);  // Get the offset of the content area relative to the boundary of the browser window
    contentTop = window.screenTop;
    contentLeft = window.screenLeft;
    initialBrowserTop = initialTop - contentTop;
    initialBrowserLeft =  initialLeft - contentLeft;
    
    window.resizeTo(screen.width/2,screen.height/2)
    window.moveTo(0,10)

    var hdiff=window.screenTop;
    window.moveTo(-6,-hdiff+6);
    window.resizeTo(screen.width+13,screen.height+hdiff+26)    
    //window.resizeTo(window.screen.availWidth+40,window.screen.availHeight+90)    
  }
}

//===G===getNow=================================================

function getNow() {
	var dateobj = new Date();
	var d = dateobj.getDate();
	  if (d < 10) {	d = "0" + d;}
	var m = dateobj.getMonth() + 1;
	  if (m < 10)	{m = "0" + m;}
	var y = dateobj.getFullYear();
	var h = dateobj.getHours();
	  if (h < 10)	{h = "0" + h;}
	var mn = dateobj.getMinutes()
	  if (mn < 10){	mn = "0" + mn;}
	var s = dateobj.getSeconds();
	  if (s < 10)	{s = "0" + s;}
	var strDate = m + "/" + d + "/" + y + "%20" + h + ":" + mn + ":" + s;
	return strDate;
}

//===G===goDonor=======Switch back to the donor mode.===========

function goDonor() { 
  document.location = "goDonor.asp";
}

//===G===goStopQDS=================================================

function goStopQDS() {
		OldDHC = null;
		ViewDHC = null;
		OldDHC = window.open('blank.htm','OldDHC','directories=no,status=no,location=no,toolbar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=10,height=10');
		OldDHC.blur();
		OldDHC.close();
		ViewDHC = window.open('blank.htm','ViewDHC','directories=no,status=no,location=no,toolbar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=10,height=10');
		ViewDHC.blur();
		ViewDHC.close();
  if (window.parent == window.self) { stopQDS(); }
  else { window.parent.stopQDS(); }
}

//===H===HandleTimeout===========================================

function HandleTimeout() {
  alert("Your session has expired due to inactivity for more than 20 minutes.");
  goDonor();
}

//===I===Initialize==============================================

function Initialize() {
  session_notify = 1000 * 60 * 18;
  timeout = 3000;
  repeatTimeout = 2000;
  fixedW = 642;
  fixedH = 482;
  testMode = 0;
  brname = null;
  brver = 0;
  
  // Variables for fullscreen switching
  initialWidth = null;
  initialHeight = null;
  initialTop = null;
  initialLeft = null;
  initialBrowserLeft = null;
  initialBrowserTop = null;
  contentTop = null;
  contentLeft = null;
}

//===I===InitializeCommon========================================

function InitializeCommon() {
  // Initialize common functions
  // Hook up common event handlers
  document.onkeydown = checkKey;
  //document.onmousedown = click;
}

//===N===NavStaff==============================================

function NavStaff(eventObj) {
  if ( staffDonorMode != "StaffMode" ) {
    var currentDonorLocation = CurrentDonorLocation();
    var index = currentDonorLocation.indexOf(answerPage);
    if (index >= 0) {
      alert("Please finish all questions or cancel the interview before going to the staff mode.")}
    else {
      staffDonorMode = "StaffMode";
      document.location = "login.asp?WhichUser=Nurse&staffPopUp=true";
    }
  }
  return true;
}

//===O===OnBeforeUnload========================================

function OnBeforeUnload() {
  FocusQDS();
}

//===O===OnClick==============================================

function OnClick() {
  var thisRightClick = "The right click is disabled.";
	if (event.button == 2 && testMode == 0) {
		alert(thisRightClick);
		return false;
	}
}

function click(mclick) {
  var thisRightClick = "The right click is disabled.";
	if (document.all)	{
		if (event.button == 2 && parent.testMode == 0) {
			alert(thisRightClick);
			return false;	}
	}
}

//===O===OnLoad============================================

function OnLoad() {
  if (!browserCheck()) {
    location.href = "WrongBrowser.asp";
  }
  else {
    Initialize();
    document.onmousedown = OnClick;
    fullScreen();
    display.document.location.href = "copyright.asp";
  }
}

function OnLoads() {
  if (!browserCheck()) {
    location.href = "WrongBrowser.asp";
  }
  else {
    Initialize();
    document.onmousedown = OnClick;
    fullScreen();
    display.document.location.href = "language.asp";
  }
}

//===P===PrintPage==========================================
  
function PrintPage() {
  window.print()
}

//===R===realjump===========================================

function realjump(URL) {
	clearTimeout(intervalID)
	document.location = URL;
}

//===R===ResetDonorSession===================================

function ResetDonorSession() {
  if (staffDonorMode != "StaffMode") {
    var answer = confirm("Do you want to cancel the current donor interview?");}
  else {
    answer = true;
  }
  if (answer) {
    document.location = "goDonor.asp";
  }
  return answer;
}

//===R===Restore==============================================

function restore() { // Restore the window to the initial size and location
  if (initialBrowserLeft != null) { // Do nothing if the screen was not moved.
    window.moveTo(initialBrowserLeft, initialBrowserTop); // Move the window to the initial location
    
    document.body.style.width = initialWidth; // Adjust the content size
    document.body.style.height = initialHeight;
    var borderSize = 3;    // Add an adjustment for the window border
    // Adjust the window size
    window.resizeTo(contentLeft + initialWidth + borderSize, contentTop + initialHeight + borderSize);
    window.document.location.href = "restart.htm";
  }
}

//===S===showButtons=============================================Audio=====

function showButtons() {
	if (document.all)	{
		document.all.buttonContinuePic.style.visibility = "visible"
		document.all.buttonContinueText.style.visibility = "visible"
		document.all.buttonContinueZone.style.visibility = "visible"
	}
	else 	{
		document.buttonContinuePic.visibility = "show"
		document.buttonContinueText.visibility = "show"
		document.buttonContinueZone.visibility = "show"
	}
}

//===S===StartQDS==================================================

function StartupQDS() {
  FocusQDS();
  qdsWindow = null;
  if (!browserCheck()) {
    return false;
  }
  else {
    var URL = "MainPage2.asp";
      try {
        qdsWindow = window.open(URL,'QDS','directories=no,status=no,location=no,toolbar=no,scrollbars=yes,resizable=no,copyhistory=no');
      }
      catch(e) {
        qdsWindow = null;
        alert("An error has occured that prevents QDS from starting.  Please click the 'Start QDS' button to try again.  If the error persists, please contact QDS support.");
        return false;
      }
    return true;
  }
}
//=====S========StartupCheck==============================================
  
function StartupCheck() {
  FocusQDS();
  qdsWindow = null;
  InitializeCommon();
  if (!browserCheck()) {
    return false;
  }
}

//===S===StopQDS================================================

function stopQDS()  {
  var answer = confirm("Do you want to stop the QDS application?");
  if (answer) {
    var opener = window.opener;
    if (opener == null || opener.StopQDS == null) {
      void(window.close());
      void(opener.close());
    }
    else  {
      try {
        void(opener.close());
        //opener.StopQDS();
      }
      catch(errObject)  {
        void(opener.close());
      }
    }
    return true;
  }
  else  {
    return false;
  }
}

//===S===StopSound==============================================

function StopSound() {
  //ClearAudioRepeatTimer();
  var audio = document.frames["auIEContainer"];  // Verify that the audio frame has been loaded
  if (audio != undefined && audio != null) {
    if (audio.stopSound != undefined && audio.stopSound != null) {
      audio.stopSound();
    }
  }
}

//===T===timeShow==========================================

function timeShow() {
	clearTimeout(intervalID)
  ClearAudioRepeatTimer();
  //var delay = GetRepeatTimeout();
  delay = 500;
  timeMsg = "Interval:  " + delay + "Time= " + getNow();
	buttonIntervalID = setTimeout("showButtons()", delay);
}

//===T===ToggleTestMode==========================================

function ToggleTestMode() {
  if (testMode == 0) {
    alert("Test mode is now ON.");
    testMode = 1;}
  else {
    alert("Test mode is now OFF.");
    testMode = 0;}
  return true;
}

//===W===waitjump==============================================

function waitjump(jumpURL, timeout) {
	cmd = "setTimeout(\"realjump('" + jumpURL + "')\", " + timeout + ")"
	intervalID = eval(cmd)
}
