var communicationArray = new Array();
var communicationArrayCounter = 0;

/** @file 
 * @brief JavaScript-Functions Communication with webserver
 * @author Stefan Rohde
 */

function filterGetParameter(inputString) {
	// TODO: Replace ' and " and & and characters that may harm Get-Communication
	inputString = inputString.replace(/\r\n/g,"\n");

	outputString = inputString.replace(/\&/g,"\\u0026");

	return outputString;
}

/**
 * Sends JSON-Encoded String through html-post to webserver
 * Retrieves HTML-Code and displays it in div-container specified by param 'destination' 
 * @param source string url that is supposed to be contacted
 * @param post string json-encoded string with data
 * @param destination string id of the div-container the retrieved HTML-Code is supposed to be displayed to  
 */ 
function LoadHTML(source, post, destination) {
  // Set XMLHttpRequest
	var r = new XMLHttpRequest;
    
    // When Communication was succesful, add retrieved data to div-container 'destination'
  	r.onreadystatechange = function() { if (r.readyState == 4) {
		document.getElementById(destination).innerHTML = r.responseText;
	
	} }
	
	// Open connection to webserver
	r.open('POST', source, true);
	
  // Set Requestheader
	r.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  
  // Send Request
  r.send('json={' + post + '}');
}

/**
 * Sends JSON-encoded string through html-post to webserver and retrieves JSON-encoded string
 * @param source string url that is supposed to be contacted
 * @param call string JS-Functions that is supposed to be executed after succesful communication
 */ 
function jsonGetData(source, call) {
	source = '/' + source;
	obj = new AjaxClass();
	obj.url = source;
	obj.call = call;
	obj.Method = "GET";
	obj.post = null;
	obj.init();
}

function jsonPostData(source, post, call) {
	source = '/' + source;
	obj = new AjaxClass();
	obj.url = source;
	obj.call = call;
	obj.Method = "POST";
	obj.post = post;
	obj.init();
}

function AjaxClass() {
	this.Async = true;
	this.request = null;
	this.init = function() {
		if (window.XMLHttpRequest) {
	    	// if Mozilla, Safari etc
	        this.request = new XMLHttpRequest();
		} else if (window.ActiveXObject) { 
	    	// if IE
			try {
				this.request = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
		        	this.request = new ActiveXObject("Microsoft.XMLHTTP");
		        } catch (e) {}
	        }
		}
	
		if(this.request) {
			var self = this; // To handle lose of focus
			this.handleResponse = function() {
				if(self.request.readyState == 4 && self.request.status == 200) {
					InputString = self.request.responseText;
					if(InputString) {
						InputString = InputString.replace(/\\u0026/g, "\&");

						eval("data=" + InputString);	
						eval(self.call+'(data)');
					}
				}
			} 
			
			this.request.onreadystatechange=this.handleResponse;
								
			this.request.open(this.Method, this.url , this.Async);

			if(this.Method == 'POST') {
				//this.request.setRequestHeader("Content-length", this.post.length);
				//this.request.setRequestHeader("Connection", "close");
			}

     		this.request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
			this.request.send("data=" + this.post);
		}
	}  
}

/** Sends Data through html-get to webserver and retrieves JSON-encoded string
 * @param source string url that is supposed to be contacted
 * @param call string JS-Functions that is supposed to be executed after succesful communication
 */ 
function ServerGetData(source, destination, call) {
  // Set XMLHttpRequest
	var r = new XMLHttpRequest;

    // When Communication was succesful, evaluate retrieved JSON-String and call JS-Function call with Object as parameter
  	r.onreadystatechange = function() { if (r.readyState == 4) {
   
	document.getElementById(destination).innerHTML = r.responseText;   

		// When there is a function to call after succesful communication
		if(call!='') {
			// Call function with Object as parameter
	      	eval(call + '()');
    	}

	} }
	
	// Open connection to webserver	
	r.open('GET', source, true);

  // Set Requestheader
	r.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

  // Send Request
  r.send('');
}

