// AJSHP in action, example file from devpro.it
// (C) Andrea Giammarchi

var service = false;
var last_call = null;

function call(method) {
	if(service && service[method] && last_call !== method) {
		last_call = method;
		service[method]();
	};
};

function sendPersonalString() {
	if(last_call !== 'getPersonalString') {
		var str = window.prompt('Write something');
		if(str && str.length > 0) {
			last_call = 'getPersonalString';
			if(str.length > 1000)
				str = str.substr(0,5000) + '...';
			service[last_call](str.substr(0, 1000));
		};
	};
};

function example() {
	var linkresult = true;
	if(document.getElementById) {
		function write(where, what) {
			document.getElementById(where).innerHTML = what;
		};
		var uri = document.location.href.split('index.php')[0];
		uri = uri.split('#')[0];
		NetServices.setDefaultGatewayUrl(uri + 'gateway/Gateway.class.php');
		var connection = NetServices.createGatewayConnection();
		var listener = new Object();
		listener.getPersonalString_Result = function(s) {
			write('results', s);
		};
		listener.getArray_Result = function(s) {
			for(var a = 0; a < s.length; a++)
				s[a] = '<strong>array[' + a + ']</strong> => ' + s[a] + '<br />';
			write('results', s.join(''));
		};
		listener.getInternalVar_Result = function(s) {
			var str = '<strong>Returned variable:</strong> ' + s +
			'<br /><strong>type:</strong> ' + typeof(s);
			write('results', str);
		};
		listener.getExampleClass_Result = function(s) {
			write('results', s);
		};
		listener.getPersonalString_Progress =
		listener.getArray_Progress =
		listener.getInternalVar_Progress =
		listener.getExampleClass_Progress = function(p) {
			document.getElementById('progress').innerHTML = p;
		};
		listener.onService = function(s) {
			var a = 0;
			if(s) {
				s = [];
				s[a++] = ('<h1>AJSHP Project :: EXAMPLES</h1>');
				s[a++] = ('<h2 id="methods"><a href="#methods">Public methods loaded from ' + 
				       '<strong>Example.class.php</strong></a></h2><p>');
				s[a++] = ('Click each link to view an interaction example.<br />');
				s[a++] = ('Here there is the progress of interaction: ' +
				       '<strong><span id="progress">0</span>%</strong><br />');
				s[a++] = ('<ul>');
				s[a++] = ('<li><a href="#getPersonalString" onclick="sendPersonalString();" ' +
				       'title="Gets sent sring modified from server">getPersonalString</a><br />' +
				       'Sends your string to server and recieves server responce</li>');
				s[a++] = ('<li><a href="#getArray" onclick="call(\'getArray\');" title="Gets a ' +
				       'runtime created array">getArray</a><br />' +
				       'Recieves and show a runtime array created with PHP and 1000 indexes, 10 chars string for each index. ' +
				       'Note that returned value is the native array, the output is created by listener dedicated method.</li>');
				s[a++] = ('<li><a href="#getInternalVar" onclick="call(\'getInternalVar\');" ' +
				       'title="Gets internal Example class var">getInternalVar</a><br />' +
				       'Recieves internal boolean variable as native JavaScript boolean var.</li>');
				s[a++] = ('<li><a href="#getExampleClass" onclick="call(\'getExampleClass\');" ' +
				       'title="Gets highlighted Example.class.php file">getExampleClass</a><br />' +
				       'Recieves output created from php highlight_file function, to show class used for theese tests</li>');
				s[a++] = ('</ul>');
				s[a++] = ('</p>');
				s[a++] = ('<p id="results">');
				s[a++] = ('</p>');
				s[a++] = ('<p> &nbsp; <br /> &nbsp; <br />');
				s[a++] = ('JavaScript example file used to show AJSHP in action is this one:<br />');
				s[a++] = ('<a href="'+uri+'javascript/example.js" target="_blank">example.js</a>');
				s[a++] = ('</p>');
				s[a++] = ('<h2 id="externals"><a href="#externals">external resource examples</a></h2><p>');
				s[a++] = ('<p>');
				s[a++] = ('If you want to add your example page, please send me the link at andrea' +
				       '@3site' + '.it<br />');
				s[a++] = ('<ul>');
				s[a++] = ('<li><a href="http://www.3site.it/blog/" target="_blank"><strong>my blog</strong> [italian language]</a></li>');
				s[a++] = ('</ul>');
				s[a++] = ('</p>');
				write('main', s.join(''));
			};
		};
		service = connection.getService('Example', listener);
		if(service !== false)
			linkresult = false;
	};
	return linkresult;
};
