/**
central submit method for performing ajax KVP requests. The passed handler
is a javascript method that receives one parameter: the result as string

@param url target URL of a request
@param handler function that will be invoked when response is received. The function must 
		receive one parameter containig the result of a request as a string
@param requestHeader assoziative array containing header fields (as "field") and 
		assigend values (as "value")
@param asynch if true a request will be performed asynchronoulsy (default if missing 
		or asynch == null is true)
*/
function submitGetRequest(url, handler, requestHeader, asynch) { 
	
	var req = null; 
	if ( asynch == null ) {
		asynch = true;
	}

	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {
			req = new ActiveXObject( "Msxml2.XMLHTTP" );
		} catch (e) {
			try {
				req = new ActiveXObject( "Microsoft.XMLHTTP" );
			} catch (e) {
				alert( e);
			}
		}
    }    
    
	req.onreadystatechange = function() { 
					if(req.readyState == 4) {
						if(req.status == 200) {
							handler( req.responseText );
						} else {
							loggingHandler( "Error: returned status code " + req.status + " " + req.statusText );
						}	
					} 
				} 
	
	req.open( "GET", url, asynch ); 
	// header just can be set after opening a request
	// avoid caching
	req.setRequestHeader( "If-Modified-Since", new Date( 0 ) );
	if ( requestHeader != null ) {
		setRequestHeader( req, requestHeader );
    }
	req.send( null ); 
} 

/**
central submit method for performing ajax requests with post content. The passed handler
is a javascript method that receives one parameter: the result as string

@param url target URL of a request
@param handler function that will be invoked when response is received. The function must 
		receive one parameter containig the result of a request as a string
@param content content that will be written into the post body of a request. This can be 
		either a postable string or DOM object data.
@param requestHeader assoziative array containing header fields (as "field") and 
		assigend values (as "value")
@param asynch if true a request will be performed asynchronoulsy (default if missing 
		or asynch == null is true)
*/
function submitPostRequest(url, handler, content, requestHeader, asynch) { 
	
	var req = null; 
	if ( asynch == null ) {
		asynch = true;
	}

	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {
			req = new ActiveXObject( "Msxml2.XMLHTTP" );
		} catch (e) {
			try {
				req = new ActiveXObject( "Microsoft.XMLHTTP" );
			} catch (e) {
				alert( e);
			}
		}
    }

	req.onreadystatechange = function() { 
					if(req.readyState == 4) {
						if(req.status == 200) {
							handler( req.responseText );
						} else {
							loggingHandler( "Error: returned status code " + req.status + " " + req.statusText );
						}	
					} 
				} 

	req.open( "POST", url, asynch ); 
	// header just can be set after opening a request	
	// avoid caching
	req.setRequestHeader( "If-Modified-Since", new Date( 0 ) );
	if ( requestHeader != null ) {
		setRequestHeader( req, requestHeader );
    }	
	req.send( content ); 
}

function setRequestHeader( xmlHttpRequest, requestHeader ) {
	for (var i = 0; i < requestHeader.length; i++) {
		xmlHttpRequest.setRequestHeader( requestHeader[i]["field"], requestHeader[i]["value"] );
	}
}

/**
ajax logging handler method: just alerts the result
*/
function loggingHandler(value) {
	alert( value );
}
