	/*
		JavaScript Document
		
		
		Lajax object
		by Andy Gaunt and Lance Hallberg, March 2006
		Version 1.0
		
		
		
		Definition:		Lajax is a free open-source JavaScript class.  It wraps
						complex Ajax code so you can call the Lajax functions
						and rely on the class to do the work for you.  Lajax has
						been tested on most current browsers and requires JavaScript 1.2.
		
		  Abstract:  	Lajax allows you to call an external page ( .html, .php etc )
						without refreshing the current page and returns the results of
						the page back to your own Javascript function ( which can
						assign the text to any DIV or other DOM object on the page).
						It is ideal to have the called script contain PHP, make some
						database transactions, then output via echo or print which
						returns the resulting HTML to your Javascript function.
		
	 Instantiation:		Lajax instantiates itself and has no instance methods or properties;
	 					it makes no sense to create multiple Lajax instances.
	 					
	  	 Functions:		Lajax has 4 class functions
	  	  					Lajax.state()
								Returns the ready state of the HTTPRequest object.
								If less than 4 your data is not back from the server yet.
								
		 					Lajax.result()
								Returns your data that was sent back from the called page
								if Lajax.state() == 4
								
							Lajax.get(string url,function userDefined[,string params])
								Tells Lajax to invoke the external script at 'url' using the GET method.
								Lajax will call 'handlerFunction' (your user defined function) when the state
								changes (1-4); use Lajax.state() to check.  The user function may be passed
								inline. When Lajax.state()==4 your data is waiting in Lajax.result().
								NOTE: Params should be name=value[&name2=value2...] format.
								
							Lajax.post(string url,function userDefined[,string params])
								Tells Lajax to invoke the external script at 'url' using the POST method.
								Lajax will call 'handlerFunction' (your user defined function) when the state
								changes (1-4); use Lajax.state() to check.  The user function may be passed
								inline. When Lajax.state()==4 your data is waiting in Lajax.result().
								NOTE: Params should be name=value[&name2=value2...] format.
								
			   Note:	JavaScript uses a "Same as Origin" security rule which means this Lajax object
			  			can't request, include or call any page outside of it's bound domain.  If the
			  			page (url) you want to call is outside the domain of your Lajax page, it won't work.
			  			
			  			However, a work around is to have Lajax call a script in the bound domain, and have
			  			that script make a request to the outside page ( using PHP include for example )
			  			and return the result.  I haven't tested this but I'm sure it will work.
			  			
			  			For suggestion or feed back please email lhallberg@nietc.org
	*/
		
		
	
	new Lajax;														// Forces Lajax instantiation
	
	function Lajax() {												// The Lajax class constructor
	}
	
	Lajax.request = null;											// Class property refers to the HTTP request object
	
	Lajax.start = function(){										// Class method that retreives a new HTTPRequest
		if (window.XMLHttpRequest) {
			Lajax.request = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			Lajax.request = new ActiveXObject('Microsoft.XMLHTTP');
		}
	}
	
	
	// Class method to Instantiate an asynchronous request using GET method
	Lajax.get = function(url,callback,params)
	{
		var script=url;												// location of the PHP script
		var userGetFunction=callback;								// set the user GET function
		var url=url+'?'+params;
		
		Lajax.start();												// (re)create an HTTPRequest object
		Lajax.request.onreadystatechange=userGetFunction;
		Lajax.request.open('GET',url,true);
		Lajax.request.send(null);
	}
	
	
	// Class method to Instantiate an asynchronous request using POST method
	Lajax.post = function(url,callback,params)
	{
		var script = url;											// location of the PHP script
		var userPostFunction=callback;								// set the user POST function
		var parameters = params;
		
		Lajax.start();												// (re)create an HTTPRequest object
		Lajax.request.onreadystatechange=userPostFunction;
		Lajax.request.open('POST',url,true);
		Lajax.request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
		Lajax.request.send(params);
	}
	
	
	// Class method for retrieving the HttpRequest stae
	Lajax.state = function() { return Lajax.request.readyState; }
	
	
	// Class method for retrieving the script results
	Lajax.result = function() { if(Lajax.request.readyState == 4) return Lajax.request.responseText; else return null; }
	
	var paramrs = "";
var encodedInputString = "";

	// Class method to build the parameters for Lajax dynamically for a form
	
	Lajax.buildparams = function(formname)
 	{
		var theForm = formname;

   		for(i=0; i<theForm.elements.length; i++){
   			paramrs += theForm.elements[i].name + "=";

      		if(theForm.elements[i].type == "text" || theForm.elements[i].type == "textarea" || theForm.elements[i].type == "hidden")
			{
				var testparamrs = escape(theForm.elements[i].value); 
				//return2br(testparamrs);
				testparamrs = testparamrs.replace("+", "%2B");
				//Check for Interenet Explorer. If it is the browser skip the replace of returns
				var is_ie/*@cc_on = {
  // quirksmode : (document.compatMode=="BackCompat"),
  version : parseFloat(navigator.appVersion.match(/MSIE (.+?);/)[1])
}@*/;
if (is_ie && (is_ie.version < 8))
{
  // do IE specific stuff
}
else
{
  // default behavior for other browsers
				testparamrs = testparamrs.replace(/%0A/g, "<br/>");
}
				paramrs += testparamrs + "&";
				testparamrs="";
      		}

		  	else if(theForm.elements[i].type == "checkbox")
		  	{
				if (theForm.elements[i].checked)
				{
		  			paramrs += theForm.elements[i].value + "&";
				}
				paramrs += "&";
		  	}
		  	else if(theForm.elements[i].type == "select-one"){
		  		paramrs += escape(theForm.elements[i].options[theForm.elements[i].selectedIndex].text) + "&";
		  	}
   		}
	} 

function TestEncoding(paramrs)
{
var encodedInputString=escape(paramrs);
encodedInputString=encodedInputString.replace("+", "%2B");
encodedInputString=encodedInputString.replace("&", "%26"); 
}

function return2br(dataStr) {
        return dataStr.replace(/(\r\n|[\r\n])/g, "<br />");
    }