/*
**  1 - Load this library into the page
**  2 - Write a php routine to get the information
**  3- - Invoke the script with a call onclick="doAjax(pURL, php_routine)"
**
**  Example:    
**			onclick="doAjax('getUserProperties.php?id=17, postGetUserProperties)"
**			The file getUserProperties.php must be written
*/

var tc = false;
try {
    tc = true;
} catch(f) { }


var xmlHttpError = 'XML HTTP Request: OK';  // for trouble shooting

function syncAJAX(pURL)
{
  var req = getRequestObj();
  req.open('GET', pURL, false);
  req.send(null);
  return req.responseText;
}

// pUrl is the URL to the php file and includes URL parameters
// postProcess is a function to be called for post processing of the the return --
//			no quotes in its invocation from the html
// This is the start of the AJAX stuff -- treat as a black box
function AJAX(pUrl) {
	//alert(pUrl);
	var objRequest = getRequestObj();  // create a new request object
	if (typeof(objRequest)=='object') {  // if we have an object, and
		if (objRequest.readyState>=0) {  
			// if the object supports the correct properties...proceed
			// specifiy the function to handle the results
			// the correct request object is passed for us.
			objRequest.onreadystatechange = 
				function() { 
					handleResponse(objRequest); 
				};            
			objRequest.open('POST', pUrl, true);
			objRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			//alert(pUrl);
			objRequest.send(pUrl);
			//objRequest.send(null);
		} else {
			xmlHttpError = 'XML HTTP Request Object Unavailable';
			return false;
		}
	} else {
		xmlHttpError = 'XML HTTP Request Object Not Supported';
		return false;
	}
}

function getRequestObj() {
	var objRequest;
	if (window.ActiveXObject) {
		if (tc) {
			try {
				objRequest = new ActiveXObject('Msxml2.XMLHTTP');  // IE 6+
			}
			catch(e) {
				try {
					objRequest = new ActiveXObject('Microsoft.XMLHTTP');  // IE 5.5
        }
				catch(f) { }
			}
		} else {
			objRequest = new ActiveXObject('Microsoft.XMLHTTP');  // ? IE 5.0 ?
		}
	} else if (window.XMLHttpRequest) {
		objRequest = new XMLHttpRequest();  // All other browsers, inc. IE 7 and DOM 3
	}
	return objRequest;
}

function handleResponse(pObjRequest) {
	if (pObjRequest.readyState==4) {
		if (pObjRequest.status==200) {
			resp = pObjRequest.responseText;	
			//alert(resp.length);
			//alert(resp);
			condition_code = parseMultiXML(resp);
			pObjRequest=null;  // dispose of the now un-needed object.
		}
	}
}

function loadXMLString(txt) {
	try { //Internet Explorer 
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async="false";
		xmlDoc.loadXML(txt);
		return (xmlDoc);
	} catch(e) {
		try { //Firefox, Mozilla, Opera, etc.
			parser = new DOMParser();
			xmlDoc = parser.parseFromString(txt,"text/xml");
			return (xmlDoc);
		} catch(e) {
				alert('parse error: ' + e.message)
		}
	}	
	return null;
}

function parseMultiXML(txt) {
	/**
		tags[i][0] holds the type placement -  value, innerHTML, etc.
		tags[i][1] holds the name of the html tag
		tags[i][2] holds the value to be used for the html tag
		tags[i][3] holds the index for a set of fields with the same name 
							 not radio boxex.
	**/
	//alert(txt);
	txt = '<document>' + txt + '</document>';
	xmlDoc = loadXMLString(txt);
	x = xmlDoc.documentElement.childNodes;
	//alert(txt);
	//alert('x.length=' + x.length);
	condition_code = false;
	var tags = new Array();
	for (i=0; i<x.length; i++) {
		y = x[i].childNodes;
		tags[i] = new Array();
		tags[i][3] = '';
		for (j=0; j<y.length; j++) {
			if (y[j].nodeType == 1) {
				switch (y[j].nodeName) {
					case 'name':
						tags[i][0] = y[j].childNodes[0].nodeValue;
						break;
					case 'type':
					case 'action':
						tags[i][1] = y[j].childNodes[0].nodeValue;
						break;
					case 'value':
					case 'display':
						//alert('node=' + y[j].childNodes[0].nodeValue);
						if ((y[j].childNodes[0].nodeValue == '&empty;') ||
							  (y[j].childNodes[0].nodeValue == '&amp;empty;')) 
							tags[i][2] = ' ';
						else {
							tags[i][2] = '';
							for (k=0; k<y[j].childNodes.length; k++) {
								//alert(y[j].childNodes[k].nodeValue);
								tags[i][2] += y[j].childNodes[k].nodeValue;
							}
						}
						break;
					case 'index':
						if ((y[j].childNodes[0].nodeValue == '&empty;') ||
							  (y[j].childNodes[0].nodeValue == '&amp;empty;')) 
							tags[i][3] = '';
						else
							tags[i][3] = y[j].childNodes[0].nodeValue;
						break;
					default:
						break;
				}
			}
		}
	}
	
	for (i=0; i<tags.length; i++) {
		//alert(i + ': ' + tags[i][0] + ' - ' + tags[i][1] + ' - ' + tags[i][2] + ' - ' + tags[i][3]);
		if (tags[i][3] == '') {
			obj = document.getElementById(tags[i][0]);
			switch (tags[i][1]) {
				case 'value':
				case 'v':
					//alert('value-' + tags[i][0] + '-' + tags[i][1] + '-' + tags[i][2] + '-' + tags[i][3]);
					obj.value = tags[i][2];
					break;
				case 'list':
					//alert('list-' + tags[i][0] + '-' + tags[i][1] + '-' + tags[i][2] + '-' + tags[i][3]);
					obj.selected = tags[i][2];
					break;
				case 'innerHTML':
				case 'i':
				case 'textarea':
					//alert('textarea-tags[' + i + '][1]=' + tags[i][1] + '    tags[' + i + '][2]=' + tags[i][2]);
					obj.innerHTML = tags[i][2];
					//alert('2-tags[' + i + '][1]=' + tags[i][1] + '    tags[' + i + '][2]=' + tags[i][2]);
					break;
				case 'checkbox':
				case 'check':
				case 'c':
					if (tags[i][2] == '' || tags[i][2] == ' ' || tags[i][2] == 0) {
						//alert('blanks=' + tags[i][2] + '**');
						obj.checked = false;
					} else {
						//alert('tags=' + tags[i][2] + '**');
						obj.checked = true;
					}
					//alert('1: -' + tags[i][2] + '-      checked=' + obj.checked);
					break;
				case 'radio':
				case 'radiobox':
				case 'r':
					objr = document.getElementsByName(tags[i][0]);
					var set = false;
					for (j=0; j<objr.length; j++) {
						if (objr[j].value == tags[i][2]) {
							objr[j].checked = true;
							set = true;
						}
						else objr[j].checked = false;
					}
					if (!set) objr[0].checked = true;
					break;
				case 'display':
					if (tags[i][2] == 'none') {
						obj.style.display = 'none';
					} else {
						//alert(tags[i][0] + '-' + tags[i][1] + '-' + tags[i][2] + '-' + tags[i][3]);
						obj.style.display = '';
					}
					break;
				case 'alert':
					alert(tags[i][2]);
					break;
				default:
					break;
			} 
		} else {
			index = tags[i][3];
			obj = document.getElementsByName(tags[i][0]);
			switch (tags[i][1]) {
				case 'value':
				case 'v':
					obj[index].value = tags[i][2];
					break;
				case 'innerHTML':
				case 'i':
				case 'textarea':
					obj[index].innerHTML = tags[i][2];
					break;
				case 'list':
					obj.selected = tags[i][2];
					break;
				case 'checkbox':
				case 'check':
				case 'c':
					alert('2: -' + tags[i][2] + '-');
					if (tags[i][2] == '' || tags[i][2] == ' ') 
						obj[i].checked = false;
					else
						obj[index].checked = true;
					//alert('2: -' + tags[i][2] + '-      checked=' + obj.checked);
					break;
				case 'display':
					if (tags[i][2] == 'none') {
						obj[index].style.display = 'none';
					} else {
						obj[index].style.display = '';
					}
					break;
			}
		}
	}
	return condition_code;
}


