// Crossword puzzle loader
// In the call, you can either set a chapter number or "auto", in which case the 
// chapter number will be determined automatically. The path to the root is necessary, however.

function clientChecker(){
// Check for minimum requirements

	var minRequirements = false; 
	
	if (document.getElementById) {
	  minRequirements = true;
	}

	var ua = navigator.userAgent;
	var name = navigator.appName;
	var ver = navigator.appVersion;  

//Get data about the OS
	var mac = (ver.indexOf('Mac') != -1);
	var win = (ver.indexOf('Windows') != -1);

//Look for Gecko
	var gecko = (ua.indexOf('Gecko') > 1);
	if (gecko){
		var geckoVer = parseInt(ua.substring(ua.indexOf('Gecko')+6, ua.length));
		if (geckoVer < 20020000){minRequirements = false;}
	}
	
//Look for Firebird
	var firebird = (ua.indexOf('Firebird') > 1);
	
//Look for Safari
	var safari = (ua.indexOf('Safari') > 1);
	if (safari){
		gecko = false;
	}
	
//Look for IE
	var ie = (ua.indexOf('MSIE') > 0);
	if (ie){
		var ieVer = parseFloat(ua.substring(ua.indexOf('MSIE')+5, ua.length));
		if (ieVer < 5.5){minRequirements = false;}
	}
	
//Look for Opera
	var opera = (ua.indexOf('Opera') > 0);
	if (opera){
		var operaVer = parseFloat(ua.substring(ua.indexOf('Opera')+6, ua.length));
		if (operaVer < 7.04){minRequirements = false;}
	}
	if (minRequirements){
		return true;
	}
	else {
	  	return false;
	}
	
}

function crossword(chapter,toTheRoot) {
  var chapterNum = "";
  var theURL = "";
  theURL = theURL + document.location;

  var theURLArray = new Array();

  for (var i = 0; i < theURL.length; i++) {
    theURLArray[i] = theURL.charAt(i); // Load the URL into an array
  }

  theURLArray.reverse(); // Reverse the array

  if (chapter == "auto") {
	chapterNumSetter:
	for (var i = 0; i < theURLArray.length; i++) {
 	  var test = "";
	  test = test + theURLArray[i+1] + theURLArray[i];
	  if (test == "ch") {
	    if (isFinite(theURLArray[i-2])) {
		  chapterNum = chapterNum + theURLArray[i-1] + theURLArray[i-2];
		}
		else {
		  chapterNum = chapterNum + theURLArray[i-1];
		}
		break chapterNumSetter;
	  }
	}
  }
  else {
    chapterNum = chapter;
  }
  
  var openerLink;
  var openerSpecs;
  
  var meetsMinRequirements = clientChecker();
  
  if (navigator.javaEnabled()) {
    openerLink = toTheRoot + "crossword/java/ch" + chapterNum + "_puzzle.htm";
    openerSpecs = "scrollbars=yes,resizable=yes,width=700,height=430";
  }
  else {
    if (meetsMinRequirements) {
      openerLink = toTheRoot + "crossword/javascript/ch" + chapterNum + ".htm"
	  openerSpecs = "scrollbars=yes,resizable=yes,width=720,height=460";
    }
	else {
	  alert("Your browser does not support the functions necessary for the crossword puzzles.");
	  return;
	}
  }
  
  crossword_window = window.open(openerLink, "crossword", openerSpecs);
  crossword_window.focus();
}