/**
 * Loads the url in the target element innerHTML
 * target = element or id in which to load data.
 * url = element to load.
 * data = post/get data to send with request.
 * method = post or get
 * onError = string message to display if request fails.
 * onComplete = function to execute if request succeeds.
 */
function loadAjaxUrl(target, url, data, method, onError, onComplete){
    // XMLHttpRequest Object:
    // readyState = number 0 - 4, 4 being ready.
    // status = error code.  404=not found, 200=okay, etc.
    // responseText    holds loaded data as a string of characters.
    // responseXml     holds an XML loaded file, DOM's method allows to extract data.
    // onreadystatechange  property that takes a function as value that is invoked when the readystatechange event is dispatched.
    // setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); for POST

    // XMLHttpRequest Methods
    // open(mode, url, boolean)    mode: type of request, GET or POST
    // url: the location of the file, with a path.
    // boolean: true (asynchronous) / false (synchronous).
    // optionally, a login and a password may be added to arguments.
    // send("string")  null for a GET command.

    var lcXmlHttpRequest; 
    if (window.XMLHttpRequest){ 
        lcXmlHttpRequest = new XMLHttpRequest();
    }else if (window.ActiveXObject){
    	// lcXmlHttpRequest = new ActiveXObject('Msxml2.XMLHTTP');
        lcXmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
    }
     
    lcXmlHttpRequest.onreadystatechange = function() {
        if(lcXmlHttpRequest.readyState  == 4) {
            if(lcXmlHttpRequest.status  == 200){
                var doc = lcXmlHttpRequest.responseText;
                
                /*
                IE ERROR: "An unknown runtime error has occurred at line 76"
                This is probably caused by the content being loaded into the
                target element.  IE complains if certain elements are placed
                under other elements.  I have seen this occur when dynamically
                putting a form under another form element.  That is the reason
                for the creation of an additional div element between the 
                content and the target element below.  If this causes problems
                later - another solution may be required.
                
                In order to debug, simply look at your content being loaded, and 
                add each different tag type, one at a time to the test below then
                refresh until you find the tag that breaks the code.  You will
                likely be able to determine which parent tag is then causing the
                problem and be able to rearrange accordingly to satisfy IE.
                
                Example:
                <form>
                  ----- dynamic content start -----
                  <form>  <-- IE will throw that error here, putting a form under a form is a nono.
                    <table...
                
                alert(doc);
                doc = "<html><head><link type='stylesheet'/></head><body>a form</body></html>";
                
                */
                
                var container = document.createElement('DIV');
                container.innerHTML = doc;
                target.innerHTML = "";
                target.appendChild(container);
                
                if( typeof(onComplete) != 'undefined' ){
                    onComplete();
                }
            }else{
                var container = document.createElement('DIV');
                if( onError ){
                    container.innerHTML = onError;
                }else{
                    container.innerHTML = "Error: "+lcXmlHttpRequest.status;
                }
                target.innerHTML="";
                target.appendChild(container);
            }
        }
    };
    
    if( typeof(method) == 'undefined' ){
        method = 'POST';
    }
    
    if( data && method.toUpperCase() == 'GET' ){
        url += '?' + data;
    }
    
    lcXmlHttpRequest.open(method, url, true);
    
    if( data && method.toUpperCase() == 'POST' ){
        lcXmlHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        lcXmlHttpRequest.send(data);
    }else{
        lcXmlHttpRequest.send(null);
    }
} 

/**
 * Gets the form for the passed in id
 * retrieves all the inputs from it
 * returns a url string from the inputs.
 */
var getFormData = function(formid){
	var urlString = "";
	
	if( formid ){
		var frm = document.getElementById(formid);
		if(typeof(getElements) != 'undefined'){
			var inputs = getElements('input', false, frm);
			for(var ii=0; ii<inputs.length; ii++){
				if(urlString!=""){
					urlString+="&";
				}
				urlString+=inputs[ii].name+"="+inputs[ii].value;
			}
			var selects = getElements('select', false, frm);
	        for(var ss=0; ss<selects.length; ss++){
	            if(urlString!=""){
	                urlString+="&";
	            }
	            urlString+=selects[ss].name+"="+selects[ss].options[selects[ss].selectedIndex].value;
	        }
			var textareas = getElements('textarea', false, frm)
	        for(var tt=0; tt<textareas.length; tt++){
	            if(urlString!=""){
	                urlString+="&";
	            }
	            urlString+=textareas[tt].name+"="+textareas[tt].innerHTML;
	        }
		}else{
			alert('requires duutils.js for getElements method.');
		}
	}
	return urlString;
};