// Routines for generating / checking browser ID

function getReq() {
  var req;  if (window.XMLHttpRequest) {    req = new XMLHttpRequest();  }
  else {    req = new ActiveXObject("Microsoft.XMLHTTP");  }  return req;
}

function fetchURL(url) {
  var req = getReq();
  req.open("GET", url, false);
  req.send(null);
  var ret = req.responseText;
  return ret;
}

function postURL(url, args) {
  var req = getReq();
  req.open("POST", url, false);
  req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  req.send(args);
  var ret = req.responseText;
  return ret;
}


// Get or make a unique browser ID cookie, to be sent to paypal.
function getCustomID() {
  var c = readCookie("mq_userid");
  if (c) {
	return c;
  }
  else {
	var d = new Date().getTime();
	var r = Math.round(100000 * Math.random());
	var str = "" + d % 1000000 + "-" + r;
	setCustomID(str);
	return str;
  }
}

function setCustomID(cid) {
  setCookie("mq_userid", cid, 1000);
}

// Put cookie values into the custom fields
function setCustomIDs() {
  var cid = getCustomID();
  var el = document.getElementById("customid1");
  if (el) { el.value = cid; }
  var el = document.getElementById("customid2");
  if (el) { el.value = cid; }
  var el = document.getElementById("customid3");
  if (el) { el.value = cid; }
}



function runBasicTest() {
  startTests("basic");
}

function runAdvancedTest() {
  startTests("advanced");
}

function runConsultationTest() {
  startTests("consultation");
}

function numToTimes(n) {
  if (n == 1) { return "" + n +" time"; }
  else { return "" + n + " times"; }
}


// Ought to return whether the credit is available...
function useUpCredit(itemcode) {
  var cid = getCustomID();
  var txt =  fetchURL("useCredit.php?a="+cid+"&c="+itemcode);
  eraseCookie("mq_authcode");

  if (txt.substr(0,2) == "OK") {
	alert("OK - used up credit for "+itemcode+" test");

	window.location = "order.php";
  }
  else {
	alert(txt);
  }
}

// Check if any credit.
function checkCredits() {
  var cid = getCustomID();


  var txt = fetchURL("checkAccount.php?a="+cid);

  if (txt.substr(0,2) == "OK") {
	var bits = txt.split(" ");
	var bcredits = bits[1];
	var acredits = bits[2];
	var ccredits = bits[3];
	var ucredits = bits[4];
	var msg = "";
	if (bcredits > 0) {
	  //msg += "<p>You have credit to run the Basic WMQ Tester ["+numToTimes(bcredits)+"]:</p> ";
    msg += "<center><div style='padding-top:8em; padding-bottom:8em'><input type='button' onclick='runBasicTest()' style='font-size:130%' value='Take the Test now!'></div></center>";
	}
	if (acredits > 0) {
	  // msg += "<p>You have credit to run the WMQ Tester + WMQ Trainer ["+numToTimes(acredits)+"]:</p>";
    msg += "<center><div style='padding-top:8em; padding-bottom:8em'><input type='button' style='font-size:130%' onclick='runAdvancedTest()' value='Take the Test now!'></div></center>";
	}
	if (ccredits > 0) {
	  // msg += "<p>You have credit to run the Advanced WMQ Tester with personalized consultation ["+numToTimes(ccredits)+"]: </p>";
    msg += "<center><div style='padding-top:8em; padding-bottom:8em'><input type='button' style='font-size:130%' onclick='runConsultationTest()' value='Take the Test now! (with consultation)'></div></center>"; // Click To Start Test Now</a></p>"; // <a href='javascript:useUpCredit(\"consultation\");'>DONE</a> </p>";
	}

  if (!forcepurchase && (acredits + bcredits + ccredits > 0)) {
    var tabid = document.getElementById("ordertableid");
    if (tabid) { tabid.style.display='none'; }
    var infoid = document.getElementById("orderinfoid");
    if (infoid) { infoid.style.display='none'; }
  }

	if (ucredits.length > 6) {
	  msg += "<p>You can download your most recent <a href='"+ucredits+".pdf'>WMQ Trainer report (pdf)</a></p>";
	}

	if (msg != "") {
	  var el = document.getElementById("paidsection");
	  if (el) {
		el.innerHTML = msg;
	  }
	}


  // Add a gift invite link too ...
  
  var giftlinkid = document.getElementById("giftlinkid");
  if (giftlinkid && (acredits + bcredits + ccredits > 0)) {
    giftlinkid.innerHTML = "<div style='padding-top:10em'></div><h4>To run the test on a different computer...</h4><p>If you would like to transfer your license to run the test on another computer, you can copy the link below into an email message (right click on the link, and 'Copy Shortcut' or 'Copy Link Location'). The link will authorize the recipient to run the test. Note that your license may only be used to run the test one time.</p><p><a href='http://www.memoryandlearning.com/php/order.php?a=" + cid + "'>http://www.memoryandlearning.com/php/order.php?a="+ cid + "</a></p>";
  }


  }
}


