// general ajax library: function processXML or processText must be defined
// makes an http request, data must already be READY to be send via POST
function makeRequest(url, data) {
	var http_request = false;
	
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
	    http_request.overrideMimeType('text/xml');
	    }
		} else if (window.ActiveXObject) { // IE
	try {
		http_request = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
    try {
      http_request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
    }
	}
	
	if (!http_request) {
    alert('Error in trying to send data to the server...\n\tGiving up :( Cannot create an XMLHTTP instance');
    return false;
	}
	http_request.onreadystatechange = function() {
		processResponse(http_request);
	}
	
	if(data){
		http_request.open('POST', url, true);
		http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
		http_request.setRequestHeader("Content-length", data.length);
		http_request.setRequestHeader("Connection", "close");
		http_request.send(data);
	}else{
		http_request.open('GET', url, true);
		http_request.send(null);
	}
}

// process the response that comes back (does NOT process the XML, but hands it off to another function)
function processResponse(request){
	if (request.readyState != 4) {
		return false;
	}
	if (request.status != 200) {
		alert('The HTTP status was NOT OK!');
		return false;
	}
	// go on to process the xml/text
	if(request.responseXML){
		var xmldoc = request.responseXML;
		processXML(xmldoc);
	}else	if(request.responseText){
		var textdoc = request.responseText;
		processText(textdoc);
	}else{
		alert('you must define a function processXML or processText!:: \r\n' + request.responseText);
		return false;
	}
}

// this function taken from the ajax.js library,
// edited to skip restoring the button values (deemed unnecessary);
function restoreFormParams(form, params) {   
	//construct values
	if(!form){return;}
	childList = form.getElementsByTagName('*');
	for( var e = 0; e < childList.length; e++ ) {
		thisInput = childList[e];
		//only get form elements
		if( thisInput.nodeName.toLowerCase() == 'input' ) {
		 thisElmType = thisInput.getAttribute('type');
		 thisElmType = ( thisElmType == null ) ? 'text' : thisElmType.toLowerCase();
		}
		else if( thisInput.nodeName.toLowerCase() == 'button' ) {
		 thisElmType = 'button';           
		}
		else if( thisInput.nodeName.toLowerCase() == 'textarea' ) {
		 thisElmType = 'textarea';
		}
		else if( thisInput.nodeName.toLowerCase() == 'select' ) {
		 thisElmType = 'select';
		}	else {
		 continue;
		}
		// skipping over the 'button' element type: unlikely to be changed by an end user
		//do not handle elements with no names
		if(thisElmType == 'button' || thisElmType == 'submit' || thisElmType == 'reset' || thisInput.name == '' || thisInput.name == 'undefined' || thisInput.name == undefined) {
		 continue;
		}
		
	  var controlName = thisInput.name;
	  if(controlName.match(/\[\]/,controlName)){
		  continue;
	  }
	  var controlIsArray = false;
	  var reg = /[a-z0-9_]\[\]/im;
	  if (reg.test(controlName)) {
			controlBaseName = controlName.slice(0, controlName.length-2);
			controlIsArray = true;
	  }
	  if (controlName) {
			var controlValue = params[controlName];
	  }
		/*
			Account for different element types
		*/
		//file upload
		if( thisElmType == "file" ) {
			userFuncVal = userFunc( null , AJForm.STATUS['FILE_UPLOAD_FAILED'] , "Unable to handle file uploads." );
			return userFuncVal;
		} else if( thisElmType == "checkbox" || thisElmType == "radio" ) {
			if (params[controlName]!=undefined){
				if (params[controlName]==thisInput.value) {
					thisInput.checked = true;
				} else {
					thisInput.checked = false;
				}
				if (controlIsArray) {
					if (array_contains(params[controlBaseName], thisInput.value)) {
						thisInput.checked = true;
					} else {
						thisInput.checked = false;
					}
				}
			}
			continue;
		}
		if (thisElmType == 'select') {
			if (!thisInput.multiple) {
				for (var sIndex=0; sIndex<thisInput.length; sIndex++) {
					if (controlValue) {
						if (thisInput.options[sIndex].value == controlValue) {
							thisInput.options[sIndex].selected = true;
						} else {
							thisInput.options[sIndex].selected = false;
						}
					} else {
						thisInput.options[sIndex].selected = false;
					}
				}
			} else {
				for (var sIndex=0; sIndex<thisInput.options.length; sIndex++) {
					thisInput.options[sIndex].selected = false;
					if (controlValue && controlValue.length) {
						for (var kx=0;kx<controlValue.length;kx++) {
							if (controlValue[kx] == thisInput.options[sIndex].value) {
								thisInput.options[sIndex].selected = true;
								break;
							}
						}
					}
				}
			}
		} else {
			//JMM 11/18 fix 1 = switched location of this and dataStr code below
			//argument separator
			//encode the data
			if (controlValue!=undefined) {
				thisInput.value=controlValue;
			}
		}
	}
	return true;
}
