//this is the function you want to call on your page
function call(url, page_element, call_message, error_message, method, params)
{
    if (method == undefined)
        var method = 'GET';

    if (params == undefined)
        var params = null;

	target_page_elem = document.getElementById(page_element);
	
	//if we have a valid target then do the request
	if (target_page_elem != null)
	{
	    document.getElementById(page_element).innerHTML = call_message;    
	     
		//here we are creating the ajax call processing object and giving it the call back function
	    var call_processing = new CallProcessing
     						(
     							url,
							     function(response_text,output_flag)
							     {
									document.getElementById(page_element).innerHTML = response_text;
							     }
							     ,
							     error_message,
							     true
							);
	
		call_processing.perform_get_post(method,params,true);
		return true;	
	}

	//the target was invalid so we will return false
	return false;
}


/*
**   @name CallProcessing()
**   @descr This is the class that processes the ajax requests (don't call directly use call())
**   @param (string) url - url to request (if method is get then include params in normal fashion)
**   @param (function) callback - this is the callback function that will display the results on the page
**   @param (string) error_message - the message to include in responsetext when there is any kind of error
*/
function CallProcessing(url, callback, error_message, output_flag)
{
	var req = init();
	
	if (req == null)
	{
		callback("Could not create request object");
	}
	
	req.onreadystatechange = process_request;
    
	//this creates our object
     function init()
     {	     
	     try 
	     {
	     	req = new XMLHttpRequest();
	     } 
	     catch(e) 
	     {
	     	try 
	       	{
	       		req = new ActiveXObject("Msxml2.XMLHTTP");
	       	} 
	       	catch (e) 
	       	{
	        	try 
	        	{
	         		req = new ActiveXObject("Microsoft.XMLHTTP");
	         	} 
	         	catch (E) 
	         	{
	          		req = null;
	         	}
	       	}
	     }
	     
	     return req;
	 }
     
	//this checks our state and performs the callback function on success
     function process_request()
     {     
		   if(req.readyState == 4) 
		   {
		      //According to the HTTP 1.1 specification, any response that has a code between 200 and 299 inclusive is a successful response.
		      if(req.status >= 200 && req.status <= 299) 
		      {
				 callback(req.responseText,output_flag);
		      } 
		      else 
		      {
		      	//error out with the server error code and message.
		         callback (error_message+"\n"+req.status+"\n"+req.statusText,output_flag);
		      }
		   }     	
     }
 
 	//this actually sends the get or post request
	this.perform_get_post = function(method,params,asynchronous)
	{
      	req.open(method, url, asynchronous);
	    req.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	    req.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
      	req.send(params);
      	
      	/*
      		MOZ browser will never fire onreadystatechange when in synchronous mode,
      		but they will wait for req.send to complete. So we can fire the function here instead.
      		http://www.quirksmode.org/blog/archives/2005/09/xmlhttp_notes_r_2.html
      	*/
      	if (asynchronous == false)      	
      	{
			process_request();   	
      	}		
	}     
}

/*
*   @name getFormValues()
*   @descr This function creates a string of the name-value pairs for the specified form object
*   @param (object) fobj - the form object that the values are wanted for
*   @return str - the string containing the name-value pairs for the form object
*/
function getFormValues(fobj) {
    var str = "";
    var valueArr = null;
    var val = "";
    var cmd = "";

    for(var i = 0;i < fobj.elements.length;i++) {
        switch(fobj.elements[i].type) {
        	case "text":
                str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
                break;
            case "select-one":
                str += fobj.elements[i].name + "=" + fobj.elements[i].options[fobj.elements[i].selectedIndex].value + "&";
                break;
            case "hidden":
                str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
                break;
            case "radio":
                if  (fobj.elements[i].checked) {
                    str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
                }
                break;
            case "textarea":  
                str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";                
                break;
            case "checkbox": 
            	if  (fobj.elements[i].checked) { 
                	str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
                }
                break;
            case "select-multiple":
            	for(f=0;f<fobj.elements[i].length;f++) {
                   	if(fobj.elements[i].options[f].selected==true)
            			str += fobj.elements[i].name + "=" + escape(fobj.elements[i].options[f].value) + "&";       	 
               	}
            	break;            	
        }
    }

	str = str.replace(/\+/g,"%2b"); //+ are going to spaces grr!	
    str = str.substr(0,(str.length - 1));
    return str;
}

/*
*   @name call_returning_response()
*   @descr This function is similar to the above call() function, but it returns the responseText
*   @param (string) url - the url of the page that is being called
*   @param (string) params - the string of parameters to send to the url
*   @param (string) response_div - the id of the div to display the response text in
*   @param (string) method - the method to sending data to the url; optional - defaults to GET
*   @return str - the responseText from the url
*/
function call_returning_response(url,params,page_element,method,output_flag,call_message)
{    
    if (method == undefined)
        var method = 'GET';

    if (output_flag == undefined)
    	output_flag = true;
    	
   	if (call_message == undefined)
   		call_message = "Processing request, please wait...";

	var text_to_return = "";

	target_page_elem = document.getElementById(page_element);
	
	//if we have a valid target then do the request
	if (target_page_elem != null && output_flag == true)
	{
	    document.getElementById(page_element).innerHTML = call_message;    
	}
	     
	//here we are creating the ajax call processing object and giving it the call back function
    var call_processing = new CallProcessing
 						(
 							url,
						     function(response_text,output_flag)
						     {
								if (output_flag == true)
								{								
									document.getElementById(page_element).innerHTML = response_text;
								}
								
								text_to_return = response_text;
						     }
						     ,
						     'Error',
						     output_flag
						);

	call_processing.perform_get_post(method,params,false);

	return text_to_return;	
}