
//class structure, vars & functions to call
function ajaxConnection() 
{
  this.url = "";	//php page to hit
	this.next_function = "";
	this.returnObj = null;	//usually pass a class object
  this.postData = "";	//this is the post STRING
  this.process_url = process_url;	//GET function
  this.post_url = post_url;	//POST function
} 

//structure class, tracks a variety of items we might need, 
//add or take away items, whatever you end up using, i often use parentId, msgId, 
// and newId, currId... whatever)
function divProcess()
{
	this.xmlDoc = "";	//NEEDED
	this.xmlText = "";	//NEEDED
	this.subFunc = "";	//NEEDED
 	this.overallParentId = "";
 	this.grandParentId = "";
	this.parentId = "";
	this.currId = "";
	this.newId = "";
	this.msgParId = "";
	this.msgId = "";
	this.newDivLoc = "";
	this.setHeight = 0;
	this.setWidth = 0;
}

//this function gets all values from a form without actually POSTING it.
function get_form_vals(formId)
{
 	var postStr = "";
	var formDoc = null;
	var name = "";
	var value = "";
	if(formDoc = document.getElementById(formId))
	{
		for (var i = 0; i<formDoc.elements.length; i++)
		{
			name = formDoc.elements[i].name;
			value = formDoc.elements[i].value;
			if(formDoc.elements[i].type == "checkbox")
			{
			 	if(!formDoc.elements[i].checked)
			 	{
			 	 	//if the checkbox was NOT checked, it's value is NULL
					value = "";	
				}
			}	
			postStr = postStr + name + "=" + value + "&";
		}
	}
	return postStr;
}	

//GET GET GET
function process_url()
{
	//build class object into local vars to pass into function
	var nextFunction = this.next_function;
	var returnObj = this.returnObj;

	var obj=init_object();
	obj.onreadystatechange = function()	{
       	if(obj.readyState == 4 || obj.readyState == "complete")
   	    {
   		    //alert(obj.responseText);	//for debugging
    		//alert(obj.responseXML);	//for debugging
   	     	returnObj.xmlDoc = obj.responseXML;
   	 	    returnObj.xmlText = obj.responseText;
    	   	eval(nextFunction + '(returnObj)');	//go to the NEXT FUNCTION after processing
        	delete obj;
		    }
    	}
  obj.open("GET", this.url, true);
  //obj.overrideMimeType('text/xml');
  obj.send(null);
}

//POST POST POST
function post_url()
{
	var xmlHttpReq = false;
  var self = window;
	var nextFunction = this.next_function;
	var returnObj = this.returnObj;
	
  // Mozilla/Safari
  if (window.XMLHttpRequest)
	{
  	self.xmlHttpReq = new XMLHttpRequest();
  }
  // IE
  else if (window.ActiveXObject)
	{
  	try
    {
      self.xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
    	try
    	{
		   	self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
     	} 
     	catch (e)
     	{
     	}
		}
  }
	if(!self.xmlHttpReq)
	{
  	alert('Cannot create HTTP instance');
  	delete returnObj;
  	delete self;
		return false;
	}
	else
	{
  	self.xmlHttpReq.open('POST', this.url, true);
	  self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  	self.xmlHttpReq.onreadystatechange = function()
{
			if (self.xmlHttpReq.readyState == 4)
			{
				//alert(self.xmlHttpReq.responseText);	//for debugging
   	 		returnObj.xmlDoc = self.xmlHttpReq.responseXML;
   	 		returnObj.xmlText = self.xmlHttpReq.responseText;
				eval(nextFunction + '(returnObj)');
  			delete returnObj;
   			delete self;
    	}
  	}
	}
  self.xmlHttpReq.send(this.postData);
}

function init_object()
{
 	//only used in GET technique, had issues with POST for some reason, it has to do with self, objs, etc
  var x = null;
  if (window.XMLHttpRequest)
  { 
    x = new XMLHttpRequest();
  }
  else if (window.ActiveXObject)
  { 
  	// IE
    try
    {
    	x = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
    	try
    	{
      	x = new ActiveXObject("Microsoft.XMLHTTP");
      } 
      catch (e)
      {
      }
    }
  }
  if (!x)
  {
  	alert('Cannot create HTTP instance');
    return false;
  }
  else
  {
  	return x;
  }	
}
