
			// ============================================================================================
			//
			//          A j a x   S u b s :   In conjunction with the zXML routines that handle XHR requests,
			//				  these subroutines handle common asynchronous Javascript calls.
			//
			//
			//    Created at the KSU Educational Communications Center
			//
			//    David Noel Pedergnana, Multimedia Designer
			//
			//    www.ksu.edu/ecc       *       dnp@ksu.edu
			//
			//    © MMVII D.N.B. Pedergnana
			//    
			// ============================================================================================

function encodedPair(param, val) {				// URI Encodes a single parameter/value pair for use in 
								//   an array: ("this param", 3) becomes "this%20param=3"
	var encParam = encodeURIComponent(param);		//   Called by the method getReqBody
	encParam += "=";
	encParam += encodeURIComponent(val);			// * Using an array instead of combining strings avoids
	return encParam;					//    memory leaks
}


function simplePair(param, val) {				// For Perl POST commands, does simple pairing: param=val

	encParam = param + "=" + val;
	return encParam;
}


function getReqFormBody(formID) {				// Accepts formID reference as input and outputs a 
								//    URI-encoded parameter string that can be sent
	var paramList = new Array();				//    via XHR post. Called by the method sendReq
	var formObj = getObjectByID(formID);
alert(formID + '->'+formObj);
	// Get selected form element types and append them to the parameter list:

	for(var i=0; i < formObj.elements.length; i++) {

		var fieldObj = formObj.elements[i];		// Get the current field object

		switch (fieldObj.type) {

			case "button":			// omit buttons
			case "submit":
			case "reset":
				break;

			case "checkbox":		// include checkboxes and radio buttons only if selected
			case "radio":
				if (!fieldObj.checked) {
					break;
				}

			case "text":			// include text, hidden, and password fields
			case "hidden":
			case "password":
				paramList.push(encodedPair(fieldObj.name, fieldObj.value));
				break;

			default:			// handle dropLists, multiline lists, and other form objects

				switch(fieldObj.tagName.toLowerCase()) {

					case "select":
						paramList.push(encodedPair(fieldObj.name, 
							fieldObj.options[fieldObj.selectedIndex].value));
						break;

					default:
						paramList.push(encodedPair(fieldObj.name, fieldObj.value));
						break;			
				}
		}
	}
	return paramList.join("&");
}


function sendReq(formID, targetID) {				// Accepts formID and a targetID as input and:

	var formObj = getObjectByID(formID);			//   (1) submits the designated form
	var params = getReqFormBody(formID);			//   (2) displays the resulting XHR response via targetID,
								//       which is usually a <span>, <div>, or form object
	var xhrObj = zXmlHttp.createRequest();			//       (assuming one is returned)
	xhrObj.open("post", formObj.action, true);
	xhrObj.setRequestHeader("Content-Type", "application/x-www-form-urelencoded");

	xhrObj.onreadystatechange = function () {
// alert(xhrObj.readyState);
		if (xhrObj.readyState == 4) {					// xhr task complete
// alert('status: '+xhrObj.status);
			if (xhrObj.status == 200 || xhrObj.status == 304) {	// Status OK: 200 = response not cached
										//            304 = reply cached from browser
// alert('status ok: '+xhrObj.responseText);
				if (targetID) {					// write result to target span, div or obj

					updatePage(targetID, xhrObj.responseText);
				}
			} else {
// alert('oops: '+xhrObj.responseText);
				updatePage(targetID, 'Error: ' + xhrObj.statusText);
			}
		}
	};
	xhrObj.send(params);
}


function updatePage(targetID, text) {				// Accepts targetID and xhrObj response text as input
								//   and displays the response through the target object
	var targetObj = getObjectByID(targetID);		//   (a div, span, or form object)

	if (targetObj) {					// Target object exists: update it

		targetObj.innerHTML = text;			// Is it a span or div?
//		location.href = '#current';			// Scroll to the anchor labeled 'current,' if it exsists

	} else {					// There is no target object: is it 'href,' 'script,' or 'do nothing'?
// alert('target obj not exist: '+targetID);
		switch (targetID) {

			case 'href':
				window.location = text;
				break;

			case 'eval':
// alert(text);
				eval(text);
				break;

			case 'refresh':
				location.reload(true);
				break;		

			default:
				return text;		
		}
	}
}
