// freeTutor.js
// copyright 2010 by S&S Software
// all rights reservied

// FILE DEPENDENCIES:
// enclosing php code must include tutor.js prior to including this file

// holds an instance of XMLHttpRequestUSING_JAVA_SCRIPT
var xmlHttp = createXmlHttpRequestObject();
var g_targetId = null;
var g_oper = 0;
var g_num1 = 0;
var g_num2 = 0;
var g_num3 = 0;
var g_num4 = 0;
//var g_canvas = null;
//var g_drawCtxt = null;

// creates an XMLHttpRequest instance
function createXmlHttpRequestObject() 
{
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
    // try every prog id until one works
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) 
    {
      try 
      { 
        // try to create XMLHttpRequest object
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      } 
      catch (e) {}
    }
  }
  // return the created object or display an error message
  if (!xmlHttp) {
	// may want more usr friendly error message and control here....
    //alert("Error creating the XMLHttpRequest object.");
	USING_JAVA_SCRIPT = false;
  }	
  else 
    return xmlHttp;
}


function initFreeTutor(targetId) {
	g_targetId = targetId;

     	
	
}


function initFreeProblem(theOp) {
	

	var oInputNum1 = document.getElementById("inputNum1");	
	var oInputNum2 = document.getElementById("inputNum2");	
	var oInputNum3 = document.getElementById("inputNum3");	
	var oInputNum4 = document.getElementById("inputNum4");	
	var error = false;
	g_oper = theOp;
	
	if(oInputNum1) {
		g_num1 = Number(oInputNum1.value) ;
		if(g_num1 != oInputNum1.value) {
			alert("Please enter numbers only.");
			error = true;	
			oInputNum1.select();	
		}	
	} 
	if(oInputNum2) {
		g_num2 = Number(oInputNum2.value);
		if( ! error ) {
			if(g_num2 != oInputNum2.value) {
				alert("Please enter numbers only.");
				error = true;		
				oInputNum2.select();	
			}
		}	
	} 
	if(oInputNum3) {
		g_num3 = Number(oInputNum3.value);
		if( ! error ) {
			if(g_num3 != oInputNum3.value) {
				alert("Please enter numbers only.");
				error = true;		
				oInputNum3.select();	
			}
		}	
	} 
	if(oInputNum4) {
		g_num4 = Number(oInputNum4.value);
		if( ! error ) {
			if(g_num4 != oInputNum4.value) {
				alert("Please enter numbers only.");
				error = true;	
				oInputNum4.select();	
			}	
		}
	}

	if( ! error ) {
		if(theOp == 1) { // subtraction
			if(g_num2 > g_num1) {
				alert("Please enter a subtrahend that is smaller than your minuend.");
				error = true;		
				oInputNum2.select();				
			}
		}
		if(theOp == 3) { // division
			if(g_num2 == 0) {
				alert("Please enter a number larger than 0 for a divisor.");
				error = true;
				oInputNum2.select();	
			}
		}
	}


	if( ! error) {
		initTutor(g_targetId);
		initProblem(g_oper,g_num1,g_num2,g_num3,g_num4);
	}
}

function quitTutorShell() {
	;// empty, but necessary for this app
}
