﻿window.onload = initAll;

var currIdx = 1;
var xhr = false;


if (typeof document.oncontextmenu == "object") {
   if (document.all) {
      document.onmousedown = captureMousedown;
   }
   else {
      document.oncontextmenu = captureMousedown;
   }
}
else {
   window.oncontextmenu = captureMousedown;
}


function captureMousedown(evt) {
   var mouseClick = (evt) ? evt.which : window.event.button;

   if (mouseClick == 2) {
      alert("Learn basketball at the Magic Camp!");
      return false;
   }
}


function initAll() {
	if (document.getElementById) {
		document.getElementById("back").onclick = setFree0;
		document.getElementById("next").onclick = setFree1;
	}
	else {
		alert("Sorry, your browser doesn't support this script");
	}
}

function setContent (dir) {
	var img_name = "<img src=\"dosspen" + dir + ".jpg\" border=\"0\" width=\"827\" height=\"580\">";
	document.getElementById("vowelspace").innerHTML = img_name;
	var vowel_label = "Page " + dir + ": ";
	document.getElementById("pagenumber").innerHTML = vowel_label;
}

function setFree0 () {
	if (currIdx > 1) {
		currIdx --;
		setContent (currIdx);
		makeRequest (this.href);
	}
	return false;
}

function setFree1 () {
	if (currIdx < 5) {
		currIdx ++;
		setContent (currIdx);
		makeRequest (this.href);
	}
	return false;
}

function makeRequest(url) {
	if (window.XMLHttpRequest) {
                                if (window.ActiveXObject) {
		   xhr = new ActiveXObject("Microsoft.XMLHTTP");
                                }
                                else {
		   xhr = new XMLHttpRequest();
                                }
	}
	else {
		if (window.ActiveXObject) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) { }
		}
	}

	if (xhr) {
		xhr.onreadystatechange = showContents;
		xhr.open("GET", url + currIdx + ".htm", true);
		xhr.send(null);
	}
	else {
		document.getElementById("vowelinfo").innerHTML = "Sorry, but couldn't load the info for this page from the server.";
	}
}

function showContents() {
	if (xhr.readyState == 4) {
/**
		if (xhr.status == 200) {
			var outMsg = (xhr.responseXML && xhr.responseXML.contentType=="text/xml") ? xhr.responseXML.getElementsByTagName("choices")[0].textContent : xhr.responseText;
		}
		else {
			var outMsg = "There was a problem with the request " + xhr.status;
		}
**/
		var outMsg = xhr.responseText;
		document.getElementById("vowelinfo").innerHTML = outMsg;
	}
}

