
//this is a modified version of ajaxFunction() designed to be used anywhere on the site
//theScript is just the script name (ie. index.php?m=xyz&a=xyz&param=xyz)
//doWait is optional, defaults to false
//targetLayer is the optional target layer's id
//  if you don't specify targetLayer and the response is plain text, not xml, it just returns the response text
//  if you don't specify targetLayer and the response is xml, it returns the 
//callbackFuncName, if it is not null, calls the specified function with the resultText (escaped) as the first argument, if it is not null
function callAjax(theScript, doWait, targetLayerID, callbackFuncName, optionalPostText ) {
	var xmlHttp;

	//alert(theScript);
	
	if (!(doWait == true)) { doWait = true; } //permanently on true to fix the Firefox bug, otherwise false
	
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
		  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	//if (xmlHttp.overrideMimeType) {
	//	xmlHttp.overrideMimeType('text/xml');
	//}
	//alert(xmlHttp);
	
	//SOME AJAX PROBLEMS, TO FIX THEM, REQUIRE THE dowait FLAG TO BE true
	//AN EXAMPLE OF THIS IS THE AJAX CALL IN items.add_edit.js:getAvailableUnits() which calls back modifyUnitsSelectList(), only works with doWait
	//ALTHOUGH SOME CALLS DO SEEM TO WORK WITHOUT THE doWait FLAG, SO AT SOME POINT IN THE FUTURE I WOULD LIKE THIS INVESTIGATED....
	xmlHttp.onreadystatechange=function() {
		//alert('NOT QUITE!');
		if(xmlHttp.readyState==4) {
			var response_xml;
			var response_txt;
			var response_javascript;
			var response_body_text;
			//alert('HERE!' + response_txt);
			//alert('FIRST: \n-' + xmlHttp.responseXML +'-'); 
			//alert(dump(response_xml, 1));
			
			//TO DO: I'D LIKE SOMEONE TO GO THROUGH AND VERIFY THE COMPATIBILITY OF ALL OF THIS FUNCTION'S AJAX/JAVASCRIPT CODE....
			if (callbackFuncName != null && callbackFuncName != '') {
				response_txt = xmlHttp.responseText;
				eval(callbackFuncName + '(escape(response_txt))' );
				//alert('called ' + callbackFuncName);
				return;
			}
			response_xml = xmlHttp.responseXML;
			if (response_xml == null) {
				//alert('1');
				if (targetLayerID != null && document.getElementById(targetLayerID)) {
					//alert(targetLayerID + ':' + response_txt);
					document.getElementById(targetLayerID).innerHTML = response_txt;
				} else {
					//alert('no targetLayerID:' + response_txt);
					return response_txt;
				}
			} else {
				//THIS SECTION DOESN'T WORK IN IE IF THE RETURNED TAGS FOR
				//		ajax_javascript OR ajax_body_text ARE BLANK, SO
				//		functions.php HAS BEEN ALTERED TO NEVER THROW A BLANK TAG
				//alert(response_txt);
				//response_xml_root = response_xml.getElementsByTagName('root');
				if (response_xml.getElementsByTagName('ajax_javascript')
					&& response_xml.getElementsByTagName('ajax_javascript').item(0)
					&& response_xml.getElementsByTagName('ajax_javascript').item(0).firstChild
					&& response_xml.getElementsByTagName('ajax_javascript').item(0).firstChild.data) {
						response_javascript = response_xml.getElementsByTagName('ajax_javascript').item(0).firstChild.data;
						
						response_body_text  = response_xml.getElementsByTagName('ajax_body_text').item(0).firstChild.data;

						if (response_javascript != null && response_javascript != '') {
							eval(response_javascript);
						}
						if (response_body_text != null && response_body_text != '') {
							if (targetLayerID != null && document.getElementById(targetLayerID) != null) {
								document.getElementById(targetLayerID).innerHTML = response_body_text;
							} else {
								return response_body_text;
							}
						}
					} else {
						//alert('Error retrieving profile information.')
						return response_xml;
					}
			}
		}
	}
	//alert(theScript + '::' + doWait)
	if (optionalPostText != null && optionalPostText != "") {
		xmlHttp.open("POST",theScript,doWait);
		xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		//xmlHttp.setRequestHeader('Content-Length',optionalPostText.length);
		xmlHttp.send(optionalPostText);
	} else {
		xmlHttp.open("GET",theScript,doWait);
		xmlHttp.send("");
	}
}

//strips newlines, deal with it
//mealwell uses ajah
function getMWAJTagContents(str, tagId) {
	r_begin = new RegExp('\\[\\[' + tagId + '\\]\\]', "gi");
	r_end   = new RegExp('\\[\\[\\/' + tagId + '\\]\\]', "gi");
	startTag = r_begin.exec(str);
	endTag   = r_end.exec(str);
	if (startTag && endTag 
	    && startTag.index >= 0 && endTag.index > 0) {
	//	alert('FOUND ' + startTag.index + ' to ' + endTag.index + '\n--------------------------\n'
	//			+ str.substring((startTag.index + tagId.length + 4), endTag.index));
			return str.substring((startTag.index + tagId.length + 4), endTag.index);
	}
	return null;

	//str = str.replace(/\\n/g, '');
	mystr = str;
	//mystr = mystr.replace(/[\n\r]/g, '');
	//overlib('<pre>' + mystr + '</pre>', STICKY);
	//rStr = ;

	//after an intense amount of debugging, the above solution is the only one that works for multiple line results
	//be very careful when editing this regexp, here is the original in case something happens: '\\[\\[' + tagId + '\\]\\](.*?)\\[\\[\\/' + tagId + '\\]\\]'
	regexp = new RegExp('\\[\\[' + tagId + '\\]\\](.*)\\[\\[\\/' + tagId + '\\]\\]', "gi");
	if (str.search(regexp) > -1) {
		//alert('value: ' + RegExp.$1);
		return RegExp.$1;
	} else {
		//alert('failed to get ' + tagId + ' within \n' + str)
	}
}

