/**
* To be used to decode ajax responses from server. To be worked correctly HTTP responses from the server, that are about to be used for ajax
* should use the following format:
* 		When they want to reprisent a message the response should contain the following :      msg:My message\n that the user should see
*		When a result that should be used in js then the followin must be contained in the respond: result:true \n  - - true was used as an exampled. the word true
*																		        can be - and should be - replaced with anything useful
*																		       to the application, like a number, a name .... or anything at all.
*		When a result concerns a certain field then the response should contain the name of that field as: field:afm  
*
*    ------------ FEEL FREE TO ADD YOUR OWN METHODS CONCERNING AJAX --------------------
*
*  @author: gred																        
*		
*/

/**
 * @author: gred
 *startsWithMethod. It checks wheter a string starts with a cerain string. 
 * e.g. 'hamburger'.startsWith('ham') is true. while
  *        'hamburger'.startsWith('    ham') is false
* @parameter: s the string that we want to check if a certain string starts with.
* returns: true if a string stars with the string given as a parameter false otherwise.
*/
String.prototype.startsWith = function(s) { return this.indexOf(s)==0; }

	/*
	  *  @author: gred
	 *   Alerts a message contained in the response, or does nothing if no message is contained. A message is everything follows the [msg: ]  string. If no 
	 *   msg: is contained in the response the method supposes that no message is send from the server.
	 * 
	 */
	function showMessage(response) {
		if(response.indexOf('msg:')!=-1) {
			var msg = response.substring(response.indexOf('msg:') + 'msg:'.length);
			if(msg.indexOf('\n')!=-1)
				msg = msg.substring(0, msg.indexOf('\n'));
			alert(msg);
			return;
		}
	}
	
	function getAjaxAttribute(response, attr) {
	attr = attr+':';
		if(response.indexOf(attr)!=-1) {
			var msg = response.substring(response.indexOf(attr) + attr.length);
			if(msg.indexOf('\n')!=-1)
				msg = msg.substring(0, msg.indexOf('\n'));
			return msg;
		}
		return 'undefined';
	}
	
	
	/*
	 *  @author: gred
	 *
	 */
	function getResult(response) {
		if(response.indexOf('result:')!=-1) {
			var res = response.substring(response.indexOf('result:') + 'result:'.length);
			if(res.indexOf('\n')!=-1)
				res =res.substring(0, res.indexOf('\n'));
			return res;
		}
		return '';
	}
	
	/**
	 * @author: gred
	 * UNHIDES messages concerning the fields. These messages are about to be shown next to the fields or under them.
	 *
	 */
	function showAjaxMsgsInFields(response) {
			if(response.indexOf('field:')!=-1) {
				var fldResp = response;
				
				do {
					 fldResp = fldResp.substring(fldResp.indexOf('field:') + 'field:'.length);
					 var fld = fldResp.substring(0, (fldResp.indexOf('\n')==-1)?fldResp.length:fldResp.indexOf('\n'));
					 var fldMsgKey = fld+'Msg:';
					 var fldName   = fld+'Msg';
					 var msg = response.substring(response.indexOf(fldMsgKey) + fldMsgKey.length);
					 msg=msg.substring(0, (msg.indexOf('\n')==-1)?msg.length:msg.indexOf('\n'));
					 
					 $('#'+fldName).html(msg);
					 
				} while(fldResp.indexOf('field:')!=-1) ;
			}
	}
	
	function getAjaxResult(response) {
		if(response.indexOf('result:') == -1)
			return 'undefined';
		
		response = response.substring(response.indexOf('result:') + 'result:'.length);
		return response.substring(0, (response.indexOf('\n')==-1)?response.length:response.indexOf('\n'));
	}
	
	/**
	 *  removes the last \\n that usually comes from the oracla response.
	 *
	 */
	function clearResponse(response) {
		if(response.indexOf('\n')!=-1) {
			if(response.lastIndexOf('\n')== response.length-1) {
				response = response.substring(0, response.length-1);
				return response
			}
			return response;
		}
		return response;
	}
	
	
	/**
	 *  Do everything. Show my messages and give me the result.
	 *  @author: gred
	 */
	function decodeResponse(response) {
		showMessages(response);
		return getResult(response);
	}
