/*
    epAjax.js
	Personal Educational Press Ajax routines :)
*/

function createXHR() {
// get an xhr object in any browser.
	var xhr;
	try {
		xhr = new ActiveXObject("Msxm12.XMLHTTP");
	} catch (e) {
		try {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xhr = false;
		}
	}
	if (!xhr && typeof XMLHttpRequest != 'undefined') {
		xhr = new XMLHttpRequest();
		}
	return xhr;
}

var xhr;

function ChangeNavLevel(someone) {
// user clicked General, Languages, or MyLists.
// use slight variation of Change word list.
var NavList = new Array();
NavList["p7TBMt01"]="General";
NavList["p7TBMt02"]="Languages";
NavList["p7TBMt03"]="MyLists";

var objNavInfo= new Object();
objNavInfo.name = "strNavLevel";
objNavInfo.value=NavList[someone.id];
ChangeWordList(objNavInfo);
}

function ChangeWordList(someone)
{ // Someone- so far that would be word list, answer list, or strDelimiter - is asking for a new word list.
var str = "CartSessionID";
var CartID = document.getElementById(str).value;
var changedItem = someone.value;
//work for either button or link.
if (changedItem ==undefined) changedItem=someone.innerHTML;

var txtID = document.getElementById("txtWordsandAnswers");
var blnUseTextBox = document.getElementById("blnUseTextBox").value;
if (someone.name=="strDelimiter" && blnUseTextBox.toLowerCase() =='true') {
	var ajaxScript = "getNewDelimitedList.asp?" + someone.name + "=" + changedItem + "&txtWordsandAnswers="+encodeURI(txtID.value) + "&CartSessionID=" + CartID;
	}
else {
	var ajaxScript = "getNewWordList.asp?" + someone.name + "=" + changedItem + "&CartSessionID=" + CartID;
}
//alert('I think Ill be able to call with.' + ajaxScript);

// Set text box to blank while Ajax does its stuff.
txtID.value = "Updating... Please wait...";
//alert('opening ' + ajaxScript);
//Use ajax to bring in new list from db.
	xhr = createXHR();
	xhr.onreadystatechange=processNewDOMValues;
	xhr.open("GET",ajaxScript);
	xhr.send(null);
}

function ChangeWorksheet(WorksheetMenuItem)
{ // Change options to new worksheet.
var str = "CartSessionID";
var CartID = document.getElementById(str).value;
var msgDiv = document.getElementById("Worksheet");
var MyFileName = WorksheetMenuItem.id;
var MyName = WorksheetMenuItem.innerHTML;
// Remember our new settings.
if(document.getElementById("MyWorksheetFileName")!=null)
	document.WordListChooser.MyWorksheetFileName.value = MyFileName;
document.title = "Personal Educational Press - " + MyName;
try {
	DefaultThumbnail.src=eval(MyFileName + "Thumbnail.src"); }
catch(e) {}
msgDiv.innerHTML = "Getting new worksheet options... please wait!";
 //alert("New worksheet:" + MyFileName + ":" + MyName);

//Use ajax to bring in new list from db.
	xhr = createXHR();
	xhr.onreadystatechange=processNewDOMValues;
	xhr.open("GET","getNewWorksheetOptions.asp?Worksheet" + "=" + MyFileName + "&CartSessionID=" + CartID);
	xhr.send(null);

}

function processNewDOMValues() 
{ // User changed something; update all the new values. 
	if (xhr.readyState == 4) {
		// return JSON value from Ajax
		//alert(unescape(xhr.responseText));
		var epcart = eval('('+xhr.responseText + ')');
		//alert('eval was ok.');
		var jmsg="";
		for (var el in epcart) {
			// look at everything we got from Ajax.
			var docEl = document.getElementById(el);
			// alert('Tried to get docEl: ' + docEl + ' for ' + el);
			if (docEl != null) {
				// Was this really the same ID? (sometimes returns named element, ignore those since they're not unique.)
					// alert('Found doc element ' + el + ": " + docEl.nodeName + "\r");
				if (docEl.id == el) {
					// If there is a corresponding element in our document, change its value.
					// alert('Found doc element ' + el + ": " + docEl.nodeName + "\r");
					//jmsg = jmsg + "Found doc element " + el + ": " + docEl.nodeName + "\r";
					setNewValue(docEl,epcart[el]);
					}
				}
			else {
				// It isn't an element; might be select box string of options.
				if (el=="click") {
					P7_TBMclick(document.getElementById(epcart[el]));
				}
				else {
					tryNewSelectValue(el,epcart[el]);
				}
			}
	if (document.getElementById("msg")) {
		var msgDiv = document.getElementById("msg");
			if (msgDiv.innerHTML == "Loading new list... please wait!") 
				msgDiv.innerHTML = "";
	//			alert(jmsg);
		}
		try {
			SetNewHeading();}
		catch(e) {}
		} // End if
	} // end ready state ok.
} // end function

// Replace a document element with a new value.
// How to do this depends on the type of document element.
function setNewValue(docEl,newValue) {
	switch (docEl.nodeName)	{
		case "TEXTAREA" : {
			docEl.value = unescape(newValue); 
			break;
			}
		case "INPUT" : {
			switch (docEl.type) {
				case "hidden" : {
					docEl.value = unescape(newValue);
					break;
					}
				default :  alert("need new case for " + docEl.type);
				}
			break;
			}
		case "SELECT" : {
			for (var i =0; i< docEl.options.length; i++) {
				if (newValue == docEl.options[i].value)
					docEl.options[i].selected = true;
				}
			break;
		}
		case "DIV" : {
			docEl.innerHTML = unescape(newValue);
			break;
		}
		case "TD" : {
			docEl.innerHTML = unescape(newValue);
			break;

		}
		default: alert("need new case for " + docEl.nodeName + "(" + docEl.name + "," + docEl.id + ")");
	} 
}

// Replace a list of options with a new value.
// Just a guess, mind you. That might not be the value we care about.
function tryNewSelectValue(el,newValue) {
	newValue = unescape(newValue);
	var found = false;
	if (el.indexOf("str")==0) {
		var docEl = document.getElementById(el.replace(/str/,"int"));
		if (docEl !=null) {
			if (docEl.nodeName == "SELECT") {
				found=true;
//				alert("great, we'll replace select box contents with" + newValue);
				var arrList = eval('('+newValue + ')');
//				alert("split array into " + arrList.length + " els, starting with" + arrList[0]);
				removeOptions(docEl);
				var jmsg = "Options removed. ";
				for (var i=0;i<arrList.length;i++) {
					jmsg = jmsg + ' adding ' + arrList[i].value + " : " + arrList[i].text;
					docEl.options[i] = new Option(unescape(arrList[i].text),arrList[i].value);
					}
//				alert(jmsg);				
				}
			}
		}
// don't worry about not found items.
//	if (!found) alert("couldn't find value for " + el);
}

// remove all the options in a select menu.
function removeOptions(optionMenu) {
	var numOptions = optionMenu.options.length;
	for (var i=numOptions-1;i>=0;i--) {
		optionMenu.options[i] = null;
		}
}

