//
//  Copyright Hansen Training Systems, Inc., 2006-2112.  All rights reserved.  
//
//  Licensed Material.  Program Property of Hansen Training Systems, Inc. 
//
//
//  Invoked from seminar.html
//

//The following variables need to be initialized by the server.
var sessionID = "";
var companyID = "";
var studentID = "";
var studentPW = "";
var newPW = "";
var firstSession = "seminar";
var secondSession = "";
var handout = "";
var prohibitNextSessionJumps = true;
var audioMaster = "on";
// End of server data


var req;
var fullScreen = false;
var logWin = null; 
//var logWin = window.open();
function log(message) {
	if (logWin == null) {
		return;
	}
	logWin.document.write(message+"<br/>");
}

function init()  {
   var thisUrl = "" + window.location;
   if (thisUrl.indexOf("seminar2") != -1) {
	   fullScreen = true;
	   prohibitNextSessionJumps = false;
   }
  //Perform configuration check and set initial cursor position.
  
  //if (!configCheck()) {
     getSessionID();
     submitToServer(companyID,studentID,studentPW,newPW);
 // }
}

function getSessionID() {
   studentPW = getParameter(window.location.hash.substr(1),"C=");   
   var inputData = "" + window.location.hash.substr(1).toUpperCase();
   studentID = getParameter(inputData,"B=");
   companyID = getParameter(inputData,"A=");
   var paidCourse = getParameter(inputData,"D=");
   if (paidCourse != "") {secondSession = paidCourse.toLowerCase();}
}

function getParameter(inString,inParm) {
// This function searches inString for inParm.  It returns subsequent characters,
// ending when '%20', ' ', '&', or the end of the line is encountered.
// If a parameter is not specified, "" is returned.
   var index1 = inString.indexOf(inParm);
   var index2;
   if (index1 !=-1) {
      index1 = index1 + inParm.length;
      index2 = inString.indexOf("%20",index1);
      if (index2 == -1) {
         index2 = inString.indexOf(" ",index1);
         if (index2 == -1) {
            index2 = inString.indexOf("&",index1);
            if (index2 == -1) {
               return inString.substring(index1);
            } else {
               return inString.substring(index1,index2);
            }
         } else {
            return inString.substring(index1,index2);
         }
      } else {
         return inString.substring(index1,index2);
      }
   } else {
      return "";
   }
}

function submitToServer(p1,p2,p3,p4) {
   //Build URL and submit data to the server for signon processing.
   var myUrl = "login.ezt";
       myUrl += "?company="     + encodeMe(p1);
       myUrl += "&student_ID="  + encodeMe(p2);
       myUrl += "&student_PW="  + encodeMe(p3);
       myUrl += "&new_PW="      + encodeMe(p4);
   log("submitToServer(): " + myUrl);
   log("secondSession: " + secondSession);
   req = createXMLHttp();
   req.onreadystatechange = myCallback;
   req.open('GET', myUrl, true);
   req.setRequestHeader("Cache-Control","nocache");
   req.send(null);
}

function  myCallback() {
  log("myCallback() req.readyState :" + req.readyState);
  //This function is called after a return from the server.
  if (req.readyState == 4) {
     log("nosignon.html:myCallback() req.getAllResponseHeaders() " + req.getAllResponseHeaders());
     log("nosignon.html:myCallback() req.status=" +req.status);
     if (req.status == 200) {
        checkResult(req.responseXML);
     } else {
        alert("Cannot contact the Manta server.\nPlease check your Internet connection and then try again.")
     }
  }
}

function encodeMe(value)  {
   //Encode parameters for placement on a URL.
   if (encodeURIComponent) return encodeURIComponent(value);
   if (escape) return escape(value);
   return value;
}

/*
 * Look for:
 * <SIGNON RETURNCODE="0" SESSIONID="abcde"/>
 */

function checkResult(returnObject)  {
  //Receives XML object returned from server.
  //Display appropriate error message or launches a course.
  var returnCodeStr = returnObject.documentElement.getAttribute("RETURNCODE");

  var returnCode = parseInt(returnObject.documentElement.getAttribute("RETURNCODE"));
  sessionID  = returnObject.documentElement.getAttribute("SESSIONID");
  switch (returnCode) {
      case(0):
   	     if (!configCheck()) {
             newWindow();
             var closeTimer = setTimeout('displayCopyright();',9000);
   	     }
         if (handoutBought()) pdfWindow();
         break;
       case(10):
     	 if (!configCheck()) {
             newWindow();
             var closeTimer = setTimeout('displayCopyright();',9000);
       	 }
         if (handoutBought()) pdfWindow();
         break;
       case(4):
         alert("Your license for this seminar has expired.  Contact Manta to renew.");
         break;
       case(1):
      	 alert("Your request could not be processed. " +
      		"If you attempted to start the seminar by clicking on the provided link in the email you received, " +
      		"your request could not be processed because your mail client does not support links with parameters. " +
      		"In this case, copy the provided link into the address bar of your browser window to start the seminar.   ");
           break;
      default:
         alert("Fatal error " + returnCode + ".  Contact Manta for assistance.");
  }
}
function newWindow() {
	var IE = false;
	if (window.navigator.appName.indexOf("Microsoft") != -1) {IE = true;}
   var popupMessage = "The Manta software is unable to run because your browser is ";
       popupMessage += "blocking pop-up windows.  To allow the Manta software to run, ";
       popupMessage += "click the OK button to close this window, open the Internet Explorer Tools ";
       popupMessage += 'pull-down menu, select the Pop-Up Blocker option, and then select "Always Allow Pop-ups from This Site...".'
   if (!courseWindow || courseWindow.closed) {
	   if (window.localStorage) {
			window.localStorage.setItem("sessionID",sessionID);
			window.localStorage.setItem("companyID",companyID);
			window.localStorage.setItem("studentID",studentID);
			window.localStorage.setItem("firstSession",firstSession);
			window.localStorage.setItem("secondSession",secondSession);
			window.localStorage.setItem("prohibitNextSessionJumps","" + prohibitNextSessionJumps);
			window.localStorage.setItem("entryViaSearch","false");
			window.localStorage.setItem("startScreen","0");
			window.localStorage.setItem("audioMaster",audioMaster);
		}
	   if (fullScreen) {
		  if (IE) {
			  var courseWindow = window.open("mantanow.html","_blank","width=1010,height=668,top=0,left=0,dependent,hotkeys,menubar=0,toolbar=0,titlebar=0,status=0,resizable=0")
		  } else {
		      var courseWindow = window.open("mantanow.html","_blank","width=1014,height=672,top=0,left=0,dependent,hotkeys,menubar=0,toolbar=0,titlebar=0,status=0,resizable=0")
		  }
	  } else {
		  if (IE) {
             var courseWindow = window.open("seminarfmt.html","_blank","titlebar,width=843px,height=597px,top=0,left=0,dependent,hotkeys,copyhistory,status=0");
		  } else {
             var courseWindow = window.open("seminarfmt.html","_blank","titlebar,width=849px,height=603px,top=0,left=0,dependent,hotkeys,copyhistory,status=0");
		  }
	  }
   } else {
       courseWindow.focus();
   }
   if (!courseWindow || courseWindow.closed) {alert(popupMessage);}
}

function pdfWindow() {
   var pdfURL = "getPDFFile.ezt;jsessionid=" + sessionID + "?file=pdf/" + handout + ".pdf";
   if (!PDFWindow || PDFWindow.closed) {
       var PDFWindow = window.open(pdfURL,"_blank");
    } else {
       PDFWindow.focus();
   }
}


function configCheck() {
   browserCheck();
   var notAllOkay = false;
   var colorDepth;
   var aHeight;
   var aWidth;
   var colorAction = "none";
   var resolutionAction = "none";
   var colorMessage = "Your screen display mode does not use enough colors to properly"
   + " display the figures in this course as they were intended."
   + "  We recommend that you change the display to use 24-bit color or higher."
   + "  To do so, perform the following steps:"
   + "<ol><li>Use your mouse to right-click any blank area of the desktop.</li>"
   + '<li>Click "Properties".</li>'
   + '<li>Click the "Settings" tab.</li>'
   + '<li>Change the "Colors" to 24-bit or higher.</li>'
   + '<li>Click "OK".</li>'
   + '</ol><p>If you are given the option, apply the new color setting without rebooting your system.</p>';
   var resolutionMessage = "Your screen display mode is not at a high enough resolution to properly"
   + " display the text, figures, and screen examples in this course."
   + "  You must change the display to use 1024 x 768 resolution or greater."
   + "  To do so, perform the following steps:"
   + "<ol><li>Use your mouse to right-click any blank area of the desktop.</li>"
   + '<li>Click "Properties".</li>'
   + '<li>Click the "Settings" tab.</li>'
   + '<li>Change the "Screen area" to 1024 x 768 pixels or greater.</li>'
   + '<li>Click "OK".</li></ol>'
   + '<p>Some browsers, including Internet Explorer, let you enlarge the browser window by reducing its '
   + 'resolution.  (This is done by holding <b>Alt</b> while turning the mouse wheel.)  '
   + 'In this case, you screen may be fine, but it is reporting a lower resolution.</p>';   
   var browserMsg = "We do not recognize the browser that you are using.  "
   +  "The Manta software has been tested with Internet Explorer, FireFox, Chrome, Opera, and Safari.  "   
   +  "If you find that the Manta software works with your browser, please contact Manta technical "
   +  "support and we will add it to the list.";
   var browserVerMsg = "You are using an older version of your browser than the version we tested.  "
   +  "As a result, not all features may work.  In particular, older browsers do not support the HTML "
   +  "audio tag that we use to play audio files.  We recommend upgrading your browser.  "
   +  "If you decide to continue, please let us know about any problems that you experience."
   var myHTML =  '<center><h1>Configuration Problem</h1>';
       myHTML += "<p>A configuration setting or the browser you are using may give you less than optimal performance.</p>"
       myHTML += '<table border="1px" cellpadding="8px" cellspacing="0px">';
       myHTML += '<tr>';
       myHTML += '<td><b>Configuration Item</b></td>';
       myHTML += '<td><b>Requirement</b></td>';
       myHTML += '<td><b>You have</b></td>';
       myHTML += '<td><b>Recommended Action</b></td>';

   // Color settings
   colorDepth = screen.colorDepth;
   if (colorDepth < 24) {
      colorAction = colorMessage;
      notAllOkay = true;
      myHTML += '</tr><tr>';
      myHTML += '<td>Display Colors</td>';
      myHTML += '<td>24-bit or greater</td>';
      myHTML += '<td>' + colorDepth + '</td>';
      myHTML += '<td>' + colorAction + '</td>';
   }

   // Screen resolution
   aHeight = screen.height;
   aWidth = screen.width;
   if ((aHeight < 768) || aWidth < 1024) {
      resolutionAction = resolutionMessage;
      notAllOkay = true;
      myHTML += '</tr><tr>';
      myHTML += '<td>Display Resolution</td>';
      myHTML += '<td>1024 x 768 or greater</td>';
      myHTML += '<td>' + aWidth +' x ' + aHeight + '</td>';
      myHTML += '<td>'+ resolutionAction + '</td>';
   }
   if ((browserName != "Internet Explorer") && 
	   (browserName != "FireFox") &&
	   (browserName != "Opera") &&
	   (browserName != "Chrome") &&
	   (browserName != "Safari") &&
	   (browserName != "Safari Mobile")) {
	   notAllOkay = true;
	   myHTML += '</tr><tr>';
	   myHTML += '<td>Browser Used</td>';
	   myHTML += '<td>Internet Explorer, FireFox, Chrome, Opera, Safari, or Sarafi Mobile</td>';
	   myHTML += '<td>' + browserName + '</td>';
	   myHTML += '<td>'+ browserMsg + '</td>';	   
   }

   if (parseInt(browserVer) < parseInt(browserMinVer)){
	   notAllOkay = true;
	   myHTML += '</tr><tr>';
	   myHTML += '<td>Browser Version</td>';
		   myHTML += '<td>' + browserName + '<br/>' + browserMinVer + '</td>';
		   myHTML += '<td>' + browserName + '<br/>' + browserVer + '</td>';
		   myHTML += '<td style="text-align:left;">'+ browserVerMsg + '</td>';	   
	   }

   myHTML += '</tr>';
   myHTML += '</table><br/><br/>';
   myHTML += '<input type="button" onclick="newWindow();" Value="Continue anyway"/>&nbsp;&nbsp;';
   myHTML += '<input type="button" onclick="window.close();" Value="Exit"/>&nbsp;&nbsp;';
   myHTML += '</center><br/><br/>';

  if (notAllOkay) {
     window.document.getElementById('pageBody').innerHTML = myHTML;
  }

  return notAllOkay;
}

function displayCopyright() {
   //Replace the signon screen.
   var htmlOut  = '<center><img id="mlogo" src="images/mantalgo.png" align="absmiddle" height="240px" width="640px" hspace="9"/>';
       htmlOut += '<br/><br/><blockquote><blockquote><p>You have successfully signed on.';
       htmlOut += 'The Manta seminar should have popped up in a second window.';
       htmlOut += '</p><p>Click the appropriate link if you experience one of these errors:</p><ul>';
       htmlOut += '<li><a href="javascript:msgPopup()">The second window never appeared.</a></li>';
       htmlOut += '<li><a href="javascript:msgTab()">The Manta seminar appeared as another tab, rather than as a second window.</a></li>';
       htmlOut += '</ul></blockquote></blockquote><br/><br/>';
   document.getElementById("pageBody").innerHTML = htmlOut;
}
function msgPopup(){
   alert("The Manta software is unable to run because your browser is " +
         "blocking pop-up windows.  Please unblock pop-ups for www.mantanow.com" +
         " or www.mantanow.net, as appropriate." +
         "\n\nIn addition to the pop-up blocker that is built into " +
         "your browser, other pop-up blockers are contained in most virus checkers " +
         "and various browser toolbar " +
         "add-ons, including those provided by Google, Yahoo, Comcast, AOL, and others. " +
         "You will need to look carefully at the " +
         "toolbar to determine which product is doing the blocking." +
         "\n\nPlease e-mail support@mantatech.com if you need further assistance."  );
}

function msgTab() {
   alert("The Manta courseware will not run as intended in a tab.  It needs to run in an " +
         "independent window.  While it will do so with the default IE7 settings, your " +
         "browser has been set up to force all pop-ups to use a tab." +
         "\n\nPerform the following steps to correct the problem in IE7." +
         "\n1.  Click the 'Tools' pull-down menu." +
         "\n2.  Select 'Internet Options.'" +
         "\n3.  On the 'General' page, click the Tabs 'Settings' button." +    
         "\n4.  Select 'Let Internet Explorer decide how pop-ups should open.'" +   
         "\n5.  Click 'OK' twice." +      
         "\n6.  Close the current browser tabs and sign-on again.");
}

function handoutBought() {
    //A handout has been purchased if char6=char13 of the companyID.
    //The handout id = courseID.pdf
    if ((companyID.length > 12) && (companyID.charAt(5) == companyID.charAt(12))) {
       var index1 = secondSession.length;
       handout = secondSession.substring(index1-6, index1);
       return true;
    } else {
       return false;
    }
}

function createXMLHttp() {
	// This function instantiates a new Ajax object.
	  if (typeof XMLHttpRequest != "undefined") {
	      return new XMLHttpRequest();
	  } else {
	     if (window.ActiveXObject) {
	        var aVersions = ["Msxml2.XMLHTTP.5.0","Msxml2.XMLHTTP.4.0",
	                         "Msxml2.XMLHTTP.3.0","Msxml2.XMLHTTP",
	                        "Microsoft.XMLHTTP"];
	        for (var i = 0; i < aVersions.length; i++) {
	           try {
	              var oXmlHttp = new ActiveXObject(aVersions[i]);
	              return oXmlHttp; 
	           } catch (e) {
	           }
	        }
	     }
	  }
	  alert('XMLHTTP is not available.  You have an obsolete or damaged browser.');
}

function browserCheck() {
	//Determines browser name and release.  Sets browserName and browserVer.
	var longName = window.navigator.userAgent;

	// Check for IE
	if (longName.indexOf("MSIE") != -1) {
		browserName = "Internet Explorer";
		browserVer  = longName.substr(longName.indexOf("MSIE")+5,4);
		browserMinVer = 7.0;
		return;
	}
		// Check for FireFox
	if (longName.indexOf("Firefox") != -1) {
		browserName = "FireFox";
		browserVer  = longName.substr(longName.indexOf("Firefox")+8,3);
		browserMinVer = 3.5;
		return;
	}
	// Check for Opera
	if (longName.indexOf("Opera") != -1) {
		browserName = "Opera";
		browserVer  = longName.substr(longName.indexOf("Version")+8,4);
		browserMinVer = 10.5;
		return;
	}
	// Check for Chrome
	if (longName.indexOf("Chrome") != -1) {
		browserName = "Chrome";
		browserVer  = longName.substr(longName.indexOf("Chrome")+7,4);
		browserMinVer = 3.0;
		return;
	}
	// Check for Safari
	if (longName.indexOf("Safari") != -1) {
		browserName = "Safari";
		if (longName.indexOf("Mobile") != -1) {
			browserName = "Safari Mobile";
		}
		browserVer  = longName.substr(longName.indexOf("Version")+8,4);
		browserMinVer = 3.0;	
		return;	
	}

	browserName = longName;
	broserVer = browserVer;
	browserMinVer = 0;
}
