function ajax_connect(url,sendMethod)	// Remote data connection (constructor)
{
	var xmlhttp, bComplete = false;
	var onComplete = false;
	
		
	// Create remote connection obj based on the currently available browser
	try 
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
	}
	catch (e)
	{ 
		try 
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
		}
		catch (e)
		{
			try 
			{
				xmlhttp = new XMLHttpRequest(); 
			}
			catch (e)
			{
				xmlhttp = false; 
			}
		}
	}
	
	if (!xmlhttp) 
		return null;
	
	this.addDefault = function(tag_id)
	{
		if(isArray(tag_id))
		{
			for(x in tag_id)
				this.addDefault(tag_id[x]);
		}
		else if(tag_id.indexOf(',') != -1)
		{
			tag_id = tag_id.split(',');
			
			for(x in tag_id)
			{
				this.addDefault(tag_id[x]);
			}
		}
		else
			this.defaults[tag_id] = 'undefined';
	};
	
	this.updateDefaults = function()
	{
		for(x in this.defaults)
		{
			this.defaults[x] = document.getElementById(x).innerHTML;
		}
	};
	
	this.setToDefault = function(tag_id)
	{
		if(tag_id == null)
		{
			for(x in this.defaults)
				this.setToDefault(x);
		}
		else
			document.getElementById(tag_id).innerHTML = this.defaults[tag_id];
	};
	
	this.tail = null;
	
	this.setTail = function(data) { this.tail = data; };
	this.clearTail = function() { this.tail = null; };
	
	this.defaults = new Array();
	this.autoClear = false;		// clear variable list everytime a new one is added (Default: false)
	this.url = url;				// url of the server script
	this.method = sendMethod.toUpperCase();	// method to send data (GET | POST)
	this.vars = Array();
	
	this.onComplete = function(func){ onComplete = func;  };
	
	this.clearVars = function() { this.vars = Array(); };
	
	this.setVars = function(vars,values)	// Set variables to be passed to the server
	{	
		if(this.autoClear)
			this.clearVars();
			
		if((!isArray(vars)) && (values == null))	// Pass one string of data
		{
			if(vars.lastIndexOf('=') == -1)	// If there is no equal (=) assume a list of tag ids 
				this.setVars(vars.split(','));
			else
			{
				vars = vars.split('=');
				this.setVars(vars[0],vars[1]);
			}
		}
		else if((isArray(vars)) && (values == null))	// Pass only an array of tag ids
		{
			for(x in vars)
			{
				if(this.tail != null)
					tag_id = String(vars[x])+String(this.tail);
				else
					tag_id = vars[x];
				
				var field = document.getElementById(tag_id);
				try
				{
					// If the element is a text input, hidden input or single select <select> box(fetch value)
					if(((field.nodeName.toUpperCase() == 'INPUT') && (field.attributes.getNamedItem('type').value.toUpperCase() == 'TEXT')) ||
						((field.nodeName.toUpperCase() == 'INPUT') && (field.attributes.getNamedItem('type').value.toUpperCase() == 'HIDDEN')) ||
						((field.nodeName.toUpperCase() == 'INPUT') && (field.attributes.getNamedItem('type').value.toUpperCase() == 'PASSWORD')) ||
						((field.nodeName.toUpperCase() == 'INPUT') && (field.attributes.getNamedItem('type').value.toUpperCase() == 'RADIO')) ||
						((field.nodeName.toUpperCase() == 'SELECT') && (field.multiple == false)) || (field.nodeName.toUpperCase() == 'TEXTAREA'))
						this.setVars(vars[x],field.value);
					
					// If the element is a checkbox input convert to a 0/1 value
					if((field.nodeName.toUpperCase() == 'INPUT') && (field.attributes.getNamedItem('type').value.toUpperCase() == 'CHECKBOX'))
					{
						String(field.checked) == 'false' ? value = '0' : value = '1';
						this.setVars(vars[x],value);
					}
						
					// If the element is a div or span
					else if((field.nodeName.toUpperCase() == 'DIV')||
						(field.nodeName.toUpperCase() == 'SPAN'))
							this.setVars(vars[x],field.innerHTML.replace(/&amp;/gi,'&'));
					
					// If the element is a multi-select select
					else if((field.nodeName.toUpperCase() == 'SELECT') && (field.multiple == true))
					{
						for(i = 0; i < field.options.length; i++)
						{
							if(field.options[i].selected == true)
								this.setVars(vars[x] + '[]',field.options[i].value);
						}
					}
				}
				catch(e){}
			}
		}
		else if((isArray(vars))&&(isArray(values)))		// Pass an array of variable names & a matching array of values
		{
		/*	for(x in vars)
				this.setVars(vars[x],values[x]);
		*/
			alert('I am Broken! PLEASE FIX ME!');
		}
		else if((vars != null)&&(values != null))	// Pass one variable and it's value
		{
			var length = this.vars.length;
			this.vars[length] = Array(vars,values);
		}
			
	};
	
	this.getVar = function(key,decode)	// Get the value of any of the set variables
	{
		key = String(key);
		if(decode == null)
			decode = false;
		
		for(x in this.vars)
		{
			if((this.vars[x][1] == 'undefined')||(this.vars[x][0] == 'undefined')||(this.vars[x][1] == null)||(this.vars[x][0] == null))
			{
				// Undefined
			}
			else
			{
				if(String(this.vars[x][0]) == key)
					if(decode)
						return this.decodeVar(this.vars[x][1]);
					else
						return this.vars[x][1];
			}
		}
		return false;
		
	};
	
	this.encodeVar = function(data)
	{
		data = encodeURIComponent(String(data));
		return data;
	}
	
	this.decodeVar = function(data)
	{
		data = decodeURIComponent(String(data));
		return data;
	}
	
	this.getVarString = function(encode)
	{
		if(encode == null)
			encode = true;
			
		var return_val = '';
		
		for(x in this.vars)
		{
			if((this.vars[x][1] == 'undefined')||(this.vars[x][0] == 'undefined')||(this.vars[x][1] == null)||(this.vars[x][0] == null))
			{
				// Undefined
			}
			else
			{
				if(encode)
					this.vars[x][1] = this.encodeVar(this.vars[x][1]);
					
				return_val += this.vars[x][0] + '=' + this.vars[x][1] + '&';
			}
		}
		
		if(encode)
			return_val += 'encode=true&';
		
		return return_val.substring(0,return_val.lastIndexOf('&'));
	}
	
	// Remote connection function (send data to server)
	this.sendData = function(sURL, sMethod)	
	{
		varStr = this.getVarString();
		
		if ((!xmlhttp) || (varStr == ''))
			return false;
			
		bComplete = false;
		
		if(sMethod != null)
			this.method = sMethod.toUpperCase();
		
		if(sURL != null)
			this.url = sURL;
		
		try 
		{
			var sPostVars = '';
			
			if (this.method == "GET")	// GET send method
					xmlhttp.open(this.method, this.url+"?"+varStr, true);
			else	// POST send method
			{
				sPostVars = varStr;
				xmlhttp.open(this.method, this.url, true);
				xmlhttp.setRequestHeader("Method", "POST "+this.url+" HTTP/1.1");
				xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			}
			
			xmlhttp.onreadystatechange = function()	
			{
			
				if (xmlhttp.readyState == 4 && !bComplete)	// When transaction is complete return results as text/xml
				{
					bComplete = true;
					
					if(onComplete != false)
					{
						var data = xmlhttp.responseXML;
						var completeData = new Array();
	
						if(data)
						{
							completeData['type'] = 'xml';
							completeData['data'] = data;
						}
						else
						{
							completeData['type'] = 'text';
							completeData['data'] = xmlhttp.responseText;
						}
						
						completeData['text'] = xmlhttp.responseText;
						onComplete(completeData);
					}
				}
			};
			
			xmlhttp.send(sPostVars);	// Pass variables to server
		}
		catch(z) 
		{
			return false;	// Failed to create/access XMLHTTP Request
		}
			
		return true;
	};
	
	return this;	// Return XMLResponse (xml/text)
}

function isArray(obj)
{
	try
	{
		if(obj.constructor == Array)
			return true;
		else
			return false;
	}
	catch(e)
	{
		return false;
	}
}

function stripAmperstand(data)
{
	return data.substring(0,data.lastIndexOf('&'));
}
