
function posty(thisUn) { 
	var elem = thisUn.elements; 
	var i = 0; 
	var postData = '';
	var nextChar = '';
	
	for (i=0; i < elem.length; i++) { 
		if (elem[i].type == 'radio' || elem[i].type == 'checkbox') {
			if (elem[i].checked == true) {
			postData += nextChar + elem[i].name + '=' + escape(elem[i].value);
			}
		}
		if (elem[i].type == 'text' || elem[i].type == 'textarea'|| elem[i].type == 'hidden' || elem[i].type == 'password') {
			postData += nextChar + elem[i].name + '=' + escape(elem[i].value);
		}
		
		if (elem[i].type == 'select-one' || elem[i].type == 'select-multiple') {
			var k = elem[i].options;
			for (a=0; a < k.length; a++) {
				if (elem[i].options[a].selected) {
				postData += nextChar + elem[i].name + '=' + escape(elem[i].options[a].value);
				nextChar = '&';
				}
			}
		}
		if (postData.length > 0) {
		nextChar = '&';
		}
	}
	
	return postData
	
}	 

function parseJs(useResponse) {

	var rxJs = /~~~[^~]+~~~/ig
	var fixJS = /~~~/ig
	
	var rxJsScript = /<script type="text\/javascript">[^¼]+?<\/script>/ig
	//var rxJsScript = /<script\b[^>]*>(.*?)<\/script>/ig
	var fixJSScript = /<script type="text\/javascript">/ig
	var fixJSScriptEnd = /<\/script>/ig
	var fixBeginComment = /<\!--/ig
	var fixEndComment = /\/\/-->/ig
	  	  
 	thisJs = useResponse.match(rxJs);
	
	//alert(useResponse);

	if (thisJs != undefined) {
	
		for (a=0; a < thisJs.length; a++) {
			jsPart = thisJs[a];
			useResponse = useResponse.replace(jsPart, "");
			jsPart = jsPart.replace(fixJS, "");
			try {	
				eval(jsPart);
				//alert(jsPart);
				} catch(x) {
				//alert(x.description);
			}
		
		}
		
	} 
	
	var thisJsScript = useResponse.match(rxJsScript);
	
	//alert(useResponse);

	if (thisJsScript != undefined && thisJsScript.length != undefined) {
	
		for (a=0; a < thisJsScript.length; a++) {
			jsPart = thisJsScript[a];
			useResponse = useResponse.replace(jsPart, "");
			jsPart = jsPart.replace(fixJSScript, "");
			jsPart = jsPart.replace(fixJSScriptEnd, "");
			jsPart = jsPart.replace(fixBeginComment, "");
			jsPart = jsPart.replace(fixEndComment, "");
			try {	
				//eval(jsPart);
				var head = document.getElementsByTagName("head");
				var script = document.createElement("script");
				head[0].appendChild(script);
				script.type = "text/javascript";
				script.text = jsPart;
				//alert(jsPart);
				} catch(x) {
				//alert(x.description);
			}
		
		}
		
	} 
	//alert(useResponse);
	return useResponse	
		
}		


function postContentLoad(useResponse) {
	// another block to do this post any content loading, for embedded js commands that can't run until after the DOM is updated
	var rxJs = /```[^~]+```/ig
	var fixJS = /```/ig
 	var thisJs = useResponse.match(rxJs);
	//alert(useResponse);
	return thisJs	
}		

function postContentLoadRun(thisJs) {
	// this one actually runs the post content load JS
	var fixJS = /```/ig

	if (thisJs != undefined) {
			for (a=0; a < thisJs.length; a++) {
			jsPart = thisJs[a];
			jsPart = jsPart.replace(fixJS, "");
			try {	
				eval(jsPart);
				//alert(jsPart);
				} catch(x) {
				//alert(x.description);
			}
		}
	} 
	
}		

function postContentLoadCleanup(useResponse) {
	// another block to do this post any content loading, for embedded js commands that can't run until after the DOM is updated
	var rxJs = /```[^~]+```/ig
	var fixJS = /```/ig
 	var thisJs = useResponse.match(rxJs);
	//alert(useResponse);

	if (thisJs != undefined) {
			for (a=0; a < thisJs.length; a++) {
			jsPart = thisJs[a];
			useResponse = useResponse.replace(jsPart, "");			
		}
	} 
	
	//alert(useResponse);
	return useResponse	
}		


function doChange(divname, useResponse, replaceOrAppend) {

	if (divname != null && document.getElementById(divname)!= undefined) {

	changeDiv = document.getElementById(divname);
	  
	  if (replaceOrAppend == 'replace') {
	  		changeDiv.innerHTML = useResponse;
   		} else	{		
			changeDiv.innerHTML += useResponse;
		}
   
	}
		
}

function badReq(postGet, dest, whichForm) {

   if (postGet == 'GET') {
   //top.location.href = dest;
   }
   else {
   //whichForm.submit();
   }

}
 
function loadurl(dest, divname, replaceOrAppend, whichForm, async) {
	
	var dest = dest + '&r=' + parseInt(Math.random()*99999999);
	var formData = null;
	var postGet = 'GET';
	var postLoadJS = null;
	if (whichForm != null) {
	
		formData = posty(whichForm);
		postGet = 'POST';
	}
	
	if (async == null) {
		async = true;
	} 
	
	if (replaceOrAppend == null) {
	
		replaceOrAppend = 'replace';
	}
	 
 try {
  var xmlhttp = window.XMLHttpRequest?new XMLHttpRequest():
  		new ActiveXObject("Microsoft.XMLHTTP");
  
 }
 catch (e) {
 	badReq(postGet, dest, whichForm);
	}
 
 xmlhttp.onreadystatechange = function() {
  
  if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200) && async == true) {
 	//alert(xmlhttp.responseText); 
  	var useResponse =  xmlhttp.responseText;
	//alert(useResponse);
	useResponse = parseJs(useResponse);
	postLoadJS = postContentLoad(useResponse);
	useResponse = postContentLoadCleanup(useResponse);
	doChange(divname, useResponse, replaceOrAppend);
	postContentLoadRun(postLoadJS);

   } else if ((xmlhttp.readyState == 4) && (xmlhttp.status != 200)) {
	
		badReq(postGet, dest, whichForm);
   	 
   }
 
}

dest = dest + '&aj=1';
 
 //alert(dest);

 
xmlhttp.open(postGet, dest, async);
	if (postGet == 'POST') {
	
 	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", formData.length);
    xmlhttp.setRequestHeader("Connection", "close");
	}
 
	//  alert(formData);
 
xmlhttp.send(formData);

if (async == false) {

	 if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
	 	
		useResponse =  xmlhttp.responseText;
		//alert(useResponse);
		useResponse = parseJs(useResponse);
		postLoadJS = postContentLoad(useResponse);
		useResponse = postContentLoadCleanup(useResponse);
		doChange(divname, useResponse, replaceOrAppend);
		postContentLoadRun(postLoadJS);
	 
	 } else if ((xmlhttp.readyState == 4) && (xmlhttp.status != 200)) {
	 
	 	badReq(postGet, dest, whichForm);
	 
	 }
	

	}
  
}



function tog(thing) { 
  
 var a =  document.getElementById(thing).style.display;
 //alert(document.getElementById(thing).style.display)
  
 if (document.getElementById(thing).style.display=='none') 
      {
      document.getElementById(thing).style.display=''
      }
 else 
      {
      document.getElementById(thing).style.display='none'
      }      
 
 }

var resultsCounter = 0;


function immediateResults(aid, divName) {

resultsCounter ++;

if (resultsCounter < 4) {

	thisURL = 'pg.cfm?aid=' + aid + '&counter=' + resultsCounter;
	
	loadurl(thisURL, divName, 'append');

	}


}