// html head: <script type="text/javascript" src="/xm_jscss/AHAHlib.js"></script>
// event sample: onchange="callAHAH('xmpx/documents.cgi','rm=g&id='+this.options[this.selectedIndex].value,'subtopic','message here','select')">
// perl: print "Content-type: text/plain\n\n"; $ahah =~ s/;$/~/; print $ahah;;
// jQuery: 	$(document).ready( function() {
//		$("#id_of_input_tag").change(function () {
//			callAHAH('xmpx/golfcontact.cgi','rm=g&id='+$("#id_of_input_tag option:selected").val(),'myhost','retrieving','select')";
//		});


/* ............ start of no edit ............ */
function callAHAH(url, namevaluepairs, pageElement, statusMessage, type) {
	//console.log(namevaluepairs);
	if ( statusMessage ) {
		document.getElementById( pageElement ).innerHTML = statusMessage;
	}
	
   try {
     	req = new XMLHttpRequest(); /* e.g. Firefox */
   } catch(e) {
      try {
       	req = new ActiveXObject("Msxml2.XMLHTTP");  /* some versions IE */
      } catch (e) {
         try {
        		req = new ActiveXObject("Microsoft.XMLHTTP");  /* some versions IE */
         } catch (E) {
         	req = false;
         } 
      } 
   }

	req.onreadystatechange = function() { responseAHAH( pageElement, type ); };
	req.open( "POST", url, true );
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	req.send( namevaluepairs );
}
/* ........... end of no edit ............ */

function responseAHAH( pageElement, type ) {
   var output = '';
   if ( req.readyState == 4 ) {
      if ( req.status == 200 ) {
         output = req.responseText;
         var myoutput = output.split(/~/); //peel off cookie stuff
//console.log(myoutput[0]);//+"--"+pageElement)
		   if (myoutput[0]) {
         //dispatch
				if ( type == 'select' ) {  //could add more types here
					buildSelect( myoutput[0], pageElement );
				} else if ( type == 'list' ) {  
					buildList( myoutput[0], pageElement );
				} else if (type == 'inner') {
					document.getElementById( pageElement ).innerHTML = myoutput[0];
				} else if (type == 'show' && myoutput[0] == 'pass') {
					showDiv(pageElement);
				}
			} else {
				return false;
			}
      }
   }
}

//for type = select 
function buildSelect( output, id ) {  
	var newtext, newvalue, i;
	var myoptions = output.split(/\|/); //assuming: value=string|value=string
	document.getElementById( id ).options.length = 0; //clear the list
	
	document.getElementById( id ).options[0] = new Option( 'Select...', '');

	for ( i = 0; i < myoptions.length; i++ ) {
		var newoption = myoptions[i].match(/(\d)=(.+)/);
		document.getElementById( id ).options[i+1] = new Option( newoption[2], newoption[1]);
	}
}

//for type = list
function buildList( output, id ) {  
	var newtext, newvalue, i;
	var myoptions = output.split(/\|/); //assuming: value=string|value=string
	document.getElementById( id ).options.length = 0; //clear the list
	
	for ( i = 0; i < myoptions.length; i++ ) {
		var newoption = myoptions[i].match(/([\w-]+)=(.+)/); //this could change to (\d+)=(\w+), etc.
		document.getElementById( id ).options[i] = new Option( newoption[2], newoption[1]);
	}
}

//for type = list
function showDiv(id ) {  
	document.getElementById( id ).style.display = "block";
}
