
function jajax(url, args) {
	if (typeof url == 'undefined') {
		return;
	}
	if (typeof args == 'undefined') {
		var args = {};
	}
	if (typeof args.fun == 'undefined') {
		//args.fun = null;
		args.fun = jajaxparse;
	}
	if (typeof args.datatype == 'undefined') {
		args.datatype = 'json';
	}
	if (typeof args.post == 'undefined') {
		args.post = '';
	}
	if (typeof args.async == 'undefined') {
		args.async = true;
	}
	if (typeof args.formid != 'undefined') {
		var p = $('#' + args.formid).serialize();
		if ((p != '') && (args.post != '')) {
			p += '&';
		}
		if ((typeof args.clearform == 'boolean') && (args.clearform)) {
			$('#' + args.formid).each(function() {
				this.reset();
			});
		}
		args.post = p + args.post;
	}
	$.ajax({
		type:'POST',
		url:url,
		data:args.post,
		cache:false,
		async:args.async,
		dataType:args.datatype,
		processData:false,
		scriptCharset:'UTF-8',
		beforeSend:function(xmlHttpRequest) {
                        $('.jajax-overlay').width($(window).width() + $(window).scrollLeft());
                        $('.jajax-overlay').height($(document.body).height());
                        $('.jajax-overlay .jajax-loader').css('left', ($(window).width() - $('.jajax-overlay .jajax-loader').width() )/2 + $(window).scrollLeft());
                        $('.jajax-overlay .jajax-loader').css('top', ($(window).height() - $('.jajax-overlay .jajax-loader').height() )/2 + $(window).scrollTop());
                        $('.jajax-overlay').show();
			xmlHttpRequest.setRequestHeader('X-JAJAX-Version', '0.1');
		},
		success:function(response) {
                        $('.jajax-overlay').hide();
			if (args.fun != null) {
				args.fun(response);
			}
		},
		error:function (response) {
                        $('.jajax-overlay').hide();
			if ((4 == response.readyState) && (200 == response.status) && ('OK' == response.statusText)) {
				eval('var r = ' + response.responseText);
				if (args.fun != null) {
					args.fun(r);
				}
			} else {
			
			}
		}
	});
}
function jajaxparse(result) {
	if (typeof result.innerhtml != 'undefined') {
		for (var index in result.innerhtml) {
			$('#' + result.innerhtml[index].id).html(result.innerhtml[index].html);
		}
	}
	if (typeof result.appendhtml != 'undefined') {
		for (var index in result.appendhtml) {
			$('#' + result.appendhtml[index].id).append(result.appendhtml[index].html);
		}
	}
	if (typeof result.prependhtml != 'undefined') {
		for (var index in result.prependhtml) {
			$('#' + result.prependhtml[index].id).prepend(result.prependhtml[index].html);
		}
	}
	if (typeof result.eval != 'undefined') {
		for (var index in result.eval) {
			eval(result.eval[index]);
		}
	}
	if (typeof result.addclass != 'undefined') {
		for (var index in result.addclass) {
			$('#' + result.addclass[index].id).addClass(result.addclass[index].classes);
		}
	}
	if (typeof result.removeclass != 'undefined') {
		for (var index in result.removeclass) {
			$('#' + result.removeclass[index].id).removeClass(result.removeclass[index].classes);
		}
	}

    var noteBlock = $('#noteMessage');
    var displayNoteBox = $('#noteMessageBlock');
    var errorBlock = $('#errorMessage');
    var displayErrorBox = $('#errorMessageBlock');
    
    if  (result.status == 1){
        if (typeof result.messages != 'undefined' && result.messages.length) {
            if (typeof noteBlock != 'undefined') {
                noteBlock.html(result.messages.join('<br/>'));
                if (typeof displayNoteBox != 'undefined') {
                    displayNoteBox.show();
                }
            }
        }
        if (typeof displayErrorBox != 'undefined') {
            displayErrorBox.hide();
        }
        if (typeof errorBlock != 'undefined') {
            errorBlock.html('');
        }
    } else {
        if (typeof result.messages != 'undefined' && result.messages.length) {
            if (typeof errorBlock != 'undefined') {
                errorBlock.html(result.messages.join('<br/>'));
                if (typeof displayErrorBox != 'undefined') {
                    displayErrorBox.show();
                }
            }
        }
        if (typeof displayNoteBox != 'undefined') {
            displayNoteBox.hide();
        }
        if (typeof noteBlock != 'undefined') {
            noteBlock.html('');
        }
    }
}
