
function fetch(url){
	options = {
		method: 'get',
		onSuccess: updateElements,
		onException: function(req,exc) { showError(exc.name + ":" + exc.message); }, 
		onFailure: function(req,obj) { showError(req.status + ": " + req.statusText + " while accessing " + url);stopAjaxIcon(); },
		evalScripts: true
	}
	
	new Ajax.Request(url, options);

}

function postEntry(url, query){
	startAjaxIcon();
	
	options = {
		method: 'post',
		postBody: query,
		onSuccess: updateElements,
		onException: function(req,exc) { showError(exc.name + ": " + exc.message); }, 
		onFailure: function(req,obj) { showError(req.status + ": " + req.statusText + " while accessing " + url);stopAjaxIcon(); },
		evalScripts: true
	}
	
	new Ajax.Request(url, options);

}

function inputToQuery( enm ){
	postVars = $A();
	
	$A(enm).each(function(value, index) {
		postVars.push(Form.Element.serialize($(value)));
	});
	
	return postVars.join('&');
}

function updateElements(req, json)
{
	try{
		if (!json){
			json = eval(req.responseText);
		}
		$A(json).each(function(item){
			if (item.id == 'script')
				eval(item.value);
			else
				Element.update($(item.id), item.value);	
		});
	}catch (exc){
		showError(exc.name + ":" + exc.message);
	}

	if (Ajax.activeRequestCount == 1)
	{
		stopAjaxIcon();
	}
}

function showError(msg) {
	try {
		//Effect.Fade('container', {to: 0.5});
		$('error-container').innerHTML = msg;
	} catch (e){
		alert(msg);
	}
}

/* Starts the ajax icon */
function startAjaxIcon(){
	try{
		Element.show($('ajax-loader'));
	}catch (e) {}
}

function stopAjaxIcon(){
	try{
		Element.hide($('ajax-loader'));
	}catch (e) {}
}

/**
 * Function to format a date as YYYYMMDD
 */
function createDateString(date){
	var query = '';
	query += strpadleft(date.getFullYear(),4,'0');
	query += strpadleft(date.getMonth() + 1,2,'0');
	query += strpadleft(date.getDate(),2,'0');
	return query;
}

/**
 * Utility function to pad strings on the left
 */
function strpadleft( string, length, padding ){
	string = new String(string);
	padding = new String(padding);
	while (string.length < length){
		string = padding + string;
	}

	return string;
}

/** 
 * Our own special inplace editor for history fields.
 */

YJInPlaceEditor = Class.create();
YJInPlaceEditor.prototype = Object.extend(Ajax.InPlaceEditor.prototype, {
	initialize: function(element, url, options) {
		options = Object.extend({
			okButton: false,
			cancelLink: false
			},options || {} );
			
		if (options.cancelControl){
			$(options.cancelControl).onclick = this.onclickCancel.bind(this);
			Element.hide(options.cancelControl);
		}
		
		if (options.submitControl){
			$(options.submitControl).onclick = this.onSubmit.bind(this);
			Element.hide(options.submitControl);
		}
				
		this.baseInitialize(element, url, options);
	},
	baseInitialize: Ajax.InPlaceEditor.prototype.initialize,
	getText: function() {
      return this.element.innerHTML.unescapeHTML().replace(/^\s+|\s+$/g, '');
  },
  onEnterEditMode: function() {
    if (this.options.cancelControl)	{
			Element.show(this.options.cancelControl);
		}
		
		if (this.options.submitControl){
			Element.show(this.options.submitControl);
		}
		
		if (this.options.deleteControl){
			Element.hide(this.options.deleteControl);
		}
  },
  onLeaveEditMode: function() {
  	if (this.options.cancelControl){
			Element.hide(this.options.cancelControl);
		}
		
		if (this.options.submitControl){
			Element.hide(this.options.submitControl);
		}
		
		if (this.options.deleteControl){
			Element.show(this.options.deleteControl);
		}
  }
});
