////////////////////////////////////////////////////////////////////////////////////
// JavaScript Functions Library
//	Compiled from existing and new code by David DeMartini
// Created: 15Aug2011 - dad
////////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////////
// Serialize the form data,
// CAVEAT!!!  Radio inputs are not propery processed by directly calling the
//            form element. They must be identified, then processed separatly to
//            determine WHICH of the values is actually selected.  Another funtion
//            is required for this.
function serializeForm (myForm) {
	var serialized = 'ajz=1';
	for(var i = 0; i < myForm.length; i++) {
		// check element type, skip if type is radio and not selected 
		if( myForm[i].type == 'radio' && myForm[i].checked == false) {
			continue;
		}
		if( myForm[i].value && myForm[i].name ) {
			serialized += '&'+ escape(myForm[i].name) + '=' + escape(myForm[i].value);
		}
	} 
	return serialized;  // done to start string with non-& char
}


// Make Ajaxy call to submit the data into threat
// databases
function executeAjax (myForm,ajax_uri,elem_results) {
	// Trigger some sort of Ajaxy Action
        var formdata = serializeForm(myForm);  // this converts all form data into key=value pairs
        var Req = new XMLHttpRequest();
        Req.open('POST', ajax_uri, true);
        Req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        Req.onreadystatechange = function() {
                if (Req.readyState == 4) {
                        document.getElementById(elem_results).innerHTML = Req.responseText;
                }
        }
	//alert("AJAX! " + formdata);
        return Req.send(formdata);
}



