//#######################################################################################################
//###   CREATED WITHIN THE DIPLOMA THESIS <Default Inheritance for OWL-S, 2007> by Simon Ferndriger   ###
//#######################################################################################################


/* ***************************************************************************/
/* AJAX **********************************************************************/

/*
	1. AJAX Calls
		1.1 File Manipulations
			1.1.1 Delete file on server
			1.1.2 Check taxonomy of ontology
		1.2 Java Server Call via PHP
			1.2.1 Check taxonomy of ontology
			1.2.2 Check compatibility of inputs and outputs			
	2. AJAX Basis
		
*/

//////////////////////////////////////////////////////////////////////////////////////////
// 1. AJAX Calls
//////////////////////////////////////////////////////////////////////////////////////////
/* 1.1 File Manipulations****************************************************************/

/* ######################################################################### */
// [AJAX File] 1.1.1 Delete file on server
/* ######################################################################### */
function deleteFile(filename) {
	
	log_level = 1;
	
	/* Error handling~~~~~~~~~~*/
	if(!filename) {
		error_log('No filename given');
		return;
	}
	/* ~~~~~~~~~~~~~~~~~~~~~~~~*/
	
	
	// AJAX call........................................
	var a = new AJAX('delete.php', null, null, true);
	a.setParameter('filename', filename);

	a.ontransferstart = function() {
		log('Delete: ' + filename, log_level);
	}

	a.ontransferend = function() {
		log(filename + 'deleted', log_level);
	}

	var retval = a.send();

	/* Error handling~~~~~~~~~~*/
	if(!retval)
		alert('Cannot connect to server');
		
	if(retval != 'OK') {
		error_log('File could not be deleted: ' + filename);
	}
	/* ~~~~~~~~~~~~~~~~~~~~~~~~*/

	return;
}

/* ######################################################################### */
// [AJAX File] 1.1.2 Save XML file on servier
/* ######################################################################### */
function saveXML(string, filename, _groundings) {
	
	log_level = 1;
	
	// Defaut value
	if(_groundings)
		extraPath = 'Groundings';
	else
		extraPath = '';
	
	
	// AJAX call........................................
	var a = new AJAX('storage.php', null, null, true);
	a.setParameter('string', string);
	a.setParameter('path', filename);
	a.setParameter('extrapath', extraPath);

	a.ontransferstart = function() {
		log('Save filename: ' + filename, log_level);
		log('Saving XML DOM...', log_level);
	}

	a.ontransferend = function() {
		log('...file transfer complete.', log_level);
	}

	var retval = a.send();

	/* Error handling~~~~~~~~~~*/
	if(retval != 'OK') {
		error_log('File could not be saved: ' + filename);
	}
	/* ~~~~~~~~~~~~~~~~~~~~~~~~*/

	return;
}

/* ######################################################################### */
// [AJAX Calls] 1.2.1 Check taxonomy of ontology
//					
//					Calls a PHP which connects with the JAVA reasoner
/* ######################################################################### */
function AJAX_checkTaxonomy(URI) {
	
	// Parameter handling..........
	if(!validURI(URI))
		return;
	
	// Request handling............
	var url = getURLfromURI(URI);

	var a = new AJAX('reasoner.php', null, null, true);
	a.setParameter('message', 'Consistency,' + url);
	var answer = a.send();

	// Response handling............
	if(!answer)
		alert('Cannot connect to server');
	
	if(!answer || answer.length == 0) {
		return 'No response from server.';
	}
	
	return answer;
}

/* ######################################################################### */
// [AJAX Calls] 1.2.2 Check compatibility of inputs and outputs
//					
//					Calls a PHP which connects with the JAVA reasoner
/* ######################################################################### */
function AJAX_checkCompatibility(Inputs_or_Outputs, origIO_URI, replIO_URI) {
	
	// Parameter handling..........
	if(!validURI(origIO_URI))
		return;
	if(!validURI(replIO_URI))
		return;
	
	// Request handling............
	var origURL = getURLfromURI(origIO_URI);
	var origID = getIDfromURI(origIO_URI);
	
	var replURL = getURLfromURI(replIO_URI);
	var replID = getIDfromURI(replIO_URI);

	var message = new Array(Inputs_or_Outputs, origURL, origID, replURL, replID);

	var a = new AJAX('reasoner.php', null, null, true);
	a.setParameter('message', message.join(','));
	var answer = a.send();

	// Response handling............
	if(!answer)
		alert('Cannot connect to server');
		
	if(!answer || answer.length == 0)
		return 'No response from server.';
	
	return answer;
}





//////////////////////////////////////////////////////////////////////////////////////////
// 2. AJAX Basis
//
//		Enables AJAX Calls
//////////////////////////////////////////////////////////////////////////////////////////
function AJAX(url, callback, data, sync) {
	this.sync = sync || false;

	if(window.ActiveXObject)
		this.request = new ActiveXObject('Microsoft.XMLHTTP');
	else
		this.request = new XMLHttpRequest();

	if(this.sync == false) {
		this.request.open('POST', url + '?rand=' + Math.random(), true);

		var a = this;
		this.request.onreadystatechange = function() {
			var r = a.request;
			if(r.readyState == 4) {
				try { a.ontransferend(); } catch(e) { error_log(e); }

				if(r.status == 200)
					a.callback(r.responseText, a.data);
				else
					a.callback(null, a.data);
			}
		};
	}
	else
		this.request.open('POST', url + '?rand=' + Math.random(), false);

	this.request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

	if(callback)
		this.callback = callback;
	else
		this.callback = this.success;

	this.data = data;

	this.parameter = '';
}

AJAX.prototype.success = function(s, d) {
	alert(s);
}

AJAX.prototype.parseJSON = function(text) {
	if(text.length == 0)
		return null;

	var rex = /[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/
	var reprex = /"(\\.|[^"\\])*"/g

	if(rex.test(text.replace(reprex, '')) == true)
		return null;

	var obj;

	try {
		obj = eval('(' + text + ')');
	}
	catch(e) {
		obj = null;
	}

	return obj;
}

AJAX.prototype.setParameter = function(parameter, value) {
	this.parameter = this.parameter + '&' + encodeURIComponent(parameter) + '=' + encodeURIComponent(value);
}

AJAX.prototype.setForm = function(fid) {
	var f = document.getElementById(fid);
	if(!f)
		return;

	var p = '';

	for(var i = 0; i < f.elements.length; i++) {
		switch(f.elements[i].type) {
			case 'hidden':
			case 'text':
			case 'password':
			case 'textarea':
				p = p + '&' + encodeURIComponent(f.elements[i].name) + '=' + encodeURIComponent(f.elements[i].value);
				break;
			case 'checkbox':
			case 'radio':
				if(f[i].checked)
					p = p + '&' + encodeURIComponent(f.elements[i].name) + '=' + encodeURIComponent(f.elements[i].value);
				break;
			case 'select-one':
				p = p + '&' + encodeURIComponent(f.elements[i].name) + '=' + encodeURIComponent(f.elements[i].value);
				break;
			case 'select-multiple':
				for(var j = 0; j < f.elements[i].options.length; j++) {
					if(f.elements[i].options[j].selected)
						p = p + '&' + encodeURIComponent(f.elements[i].name) + '=' + encodeURIComponent(f.elements[i].options[j].value);
				}
				break;
			case 'button':
			case 'submit':
			case 'reset':
			default:
				break;
		}
	}

	this.parameter = this.parameter + p;
}

AJAX.prototype.send = function() {
	try { this.ontransferstart(); } catch(e) { error_log(e); }

	try { this.request.send('utf8=ä' + this.parameter); } catch(e) { error_log(e); }

	if(this.sync == false)
		return;

	if(this.request.status == 200) {
		try { this.ontransferend(); } catch(e) { error_log(e); }
		return this.request.responseText;
	}

	try { this.ontransferend(); } catch(e) { error_log(e); }

	return null;
}

AJAX.prototype.ontransferstart = function() {}
AJAX.prototype.ontransferend = function() {}
