/*

1. Use an associative array together with the php script name to call the ajax get function

Requirements:
 - php_script argument; ex. 'get_foo.php'
 - arg_array argument; ex. {"ref" : ref_id, "id" : users_id}

2. Javascript example function that calls ajax_get:
function get_new_tree(SN, CT)
{
    //clear the tree nodes
    clear_nodes();

    //get the new tree information
    ajax_get('get_tree.php', {"ref" : SN, "cur" : CT});
}

3. The PHP Script Should Return Values as XML.  Use the html/xhtml tag id's as the XML tag values:

In this example, 'tree' is the base XML parent tag (Can be your choice, and is required to be there.  I used 'tree' in this example
                                                    because I was sending back tree information).

Tags 'troot', 'trootid', 'node_0', 'node_0id', etc. are the html/xhtml tags I want to populate, with the data to passback
as the value of that childNode.

echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?>
	  <tree>";
echo "<troot>" . $check_name . "</troot>";
echo "<trootid>" . $check_SN . "</trootid>";
echo "<node_0>" . $nameArray[0] . "</node_0>";
echo "<node_0id>" . $nodeArray[0] . "</node_0id>";
echo "<node_1>" . $nameArray[1] . "</node_1>";
echo "<node_1id>" . $nodeArray[1] . "</node_1id>";
echo "<node_2>" . $nameArray[2] . "</node_2>";
echo "<node_2id>" . $nodeArray[2] . "</node_2id>";
echo "<node_3>" . $nameArray[3] . "</node_3>";
echo "<node_3id>" . $nodeArray[3] . "</node_3id>";
echo "<node_4>" . $nameArray[4] . "</node_4>";
echo "<node_4id>" . $nodeArray[4] . "</node_4id>";
echo "<node_5>" . $nameArray[5] . "</node_5>";
echo "<node_5id>" . $nodeArray[5] . "</node_5id>";
echo "</tree>";

You may also include attributes in the element: <troot href="http://www.google.com">Click Here To Go To Google</troot>
and those attributes will be set.

4. Make sure your output *.php file starts with:  header("Content-type: text/xml");

For Questions: contact Jason Myrup

*/

//local variable for the request
var xmlHttp;
var car_id;
var get_type;

//build the ajax url and send the request to the server
function ajax_get(php_script, arg_array, new_get_type)
{
    //set get type
    get_type = new_get_type;

    //set id
    car_id = arg_array['carid'];

    //create the object to make the request
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null)
    {
        alert("Your browser does not support AJAX!");
        return;
    }

    //build the url from the arguments
    var url = php_script;
    if (url.indexOf("?") == -1)
        url += "?";
    for (var arg in arg_array)
        url += arg + "=" + arg_array[arg] + "&";

    //set statechanged event handler
    xmlHttp.onreadystatechange = state_changed;

    //open the url for the request
    xmlHttp.open("GET", url, true);

    //send http request
    xmlHttp.send(null);
}

//function to get the http object
function GetXmlHttpObject()
{
    var xmlHttp = null;
    try
    {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e)
    {
        // Internet Explorer
        try
        {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP.6.0");
        }
        catch (e)
        {
            try
            {
                xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e)
            {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
    }
    return xmlHttp;
}

//handle the onreadystatechange event for the http request object
function state_changed()
{
    //check the request status
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
    {
        //get the doc information if the request is complete and returned a response
        if (xmlHttp.status == 200 || xmlHttp.status == 304 || xmlHttp.status == 0) {

            //set HTML response
            if (get_type == 'calendar')
                document.getElementById('car_' + car_id).innerHTML = xmlHttp.responseText;
            else
                document.getElementById('fswd_header_cart_widget').innerHTML = xmlHttp.responseText;
        }
        else
            alert("Request error for object[xmlHttp] in state_changed event handler [status: " + xmlHttp.status + "] " + xmlHttp.responseText);
    }
}



















































/******** tims mini ajax ***********/
function $(e){if(typeof e=='string')e=document.getElementById(e);return e};
function collect(a,f){var n=[];for(var i=0;i<a.length;i++){var v=f(a[i]);if(v!=null)n.push(v)}return n};

ajax={};
ajax.x=function(){try{return new ActiveXObject('Msxml2.XMLHTTP')}catch(e){try{return new ActiveXObject('Microsoft.XMLHTTP')}catch(e){return new XMLHttpRequest()}}};
ajax.serialize=function(f){var g=function(n){return f.getElementsByTagName(n)};var nv=function(e){if(e.name)return encodeURIComponent(e.name)+'='+encodeURIComponent(e.value);else return ''};var i=collect(g('input'),function(i){if((i.type!='radio'&&i.type!='checkbox')||i.checked)return nv(i)});var s=collect(g('select'),nv);var t=collect(g('textarea'),nv);return i.concat(s).concat(t).join('&');};
ajax.send=function(u,f,m,a){var x=ajax.x();x.open(m,u,true);x.onreadystatechange=function(){if(x.readyState==4)f(x.responseText)};if(m=='POST')x.setRequestHeader('Content-type','application/x-www-form-urlencoded');x.send(a)};
ajax.sendNoCache=function(u,f,m,a){u = u + (u.indexOf('?') < 0 ? '?' : '&') + new Date().getTime();var x=ajax.x();x.open(m,u,true);x.onreadystatechange=function(){if(x.readyState==4)f(x.responseText)};if(m=='POST')x.setRequestHeader('Content-type','application/x-www-form-urlencoded');x.send(a)};
ajax.get=function(url,func){ajax.send(url,func,'GET')};
ajax.getNoCache=function(url,func){url = url + (url.indexOf('?') < 0 ? '?' : '&') + new Date().getTime();ajax.send(url,func,'GET')};
ajax.gets=function(url){var x=ajax.x();x.open('GET',url,false);x.send(null);return x.responseText};
ajax.getsNoCache=function(url){url = url + (url.indexOf('?') < 0 ? '?' : '&') + new Date().getTime();var x=ajax.x();x.open('GET',url,false);x.send(null);return x.responseText};
ajax.post=function(url,func,args){ajax.send(url,func,'POST',args)};
ajax.postNoCache=function(url,func,args){ajax.sendNoCache(url,func,'POST',args)};
ajax.update=function(url,elm){var e=$(elm);var f=function(r){e.innerHTML=r};ajax.get(url,f);};
ajax.updateNoCache=function(url,elm){var e=$(elm);var f=function(r){e.innerHTML=r};ajax.getNoCache(url,f);};
ajax.submit=function(url,elm,frm){var e=$(elm);var f=function(r){e.innerHTML=r};ajax.post(url,f,ajax.serialize(frm))};
ajax.submitNoCache=function(url,elm,frm){var e=$(elm);var f=function(r){e.innerHTML=r};ajax.postNoCache(url,f,ajax.serialize(frm))};
/******** eof - tims mini ajax ***********/


/***** submit any form via ajax ******/
//use post
function ajax_post_form(strURL, thisform) {
    var xmlHttpReq = false;
    var self = this;

    if (window.XMLHttpRequest) {// Mozilla/Safari
        self.xmlHttpReq = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    } else return 'ERROR NO AJAX SUPPORT!';

    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            return self.xmlHttpReq.responseText;
        }
    }
    self.xmlHttpReq.send(getquerystring(thisform));
}


//use get
function ajax_get_form(strURL, thisform) {
    var xmlHttpReq = false;
    var self = this;

    if (window.XMLHttpRequest) {// Mozilla/Safari
        self.xmlHttpReq = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    } else return 'ERROR NO AJAX SUPPORT!';

    self.xmlHttpReq.open('GET', strURL, true);
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            return self.xmlHttpReq.responseText;
        }
    }
    self.xmlHttpReq.send(getquerystring(thisform));
}



function getquerystring(thisform) {
    //var form     = document.forms['f1'];
		var form = thisform;
		qstr = '';
		for (i = 0; i < form.elements.length; i++) {
			field = form.elements[i];
			if (field.type == 'checkbox' || field.type == 'radio') {
				if (!field.checked) continue; //skip radios and checkboxes that are not marked
			}
			qstr+= field.name + '=' + escape(field.value);  // NOTE: no '?' before querystring
		}
    return qstr;
}
/***** eof - submit a form via ajax *******/
