function ajaxSuccess(elementId, msg, tickbox) {
    $("#"+elementId).html(msg);
	if (tickbox == false) {
		
		if ($("#"+elementId).hasClass("jqmID1")) {
				$('#notice')
				.jqDrag('.jqDrag')
				.jqResize('.jqResize')
				.jqm({
					overlay: 0,
					onShow: function(h) {
						/* callback executed when a trigger click. Show notice */
						h.w.css('opacity',0.92).slideDown();
					},
					onHide: function(h) {
						/* callback executed on window hide. Hide notice, overlay. */
						h.w.slideUp("slow",function() {
							if(h.o) h.o.remove();
						});
					}
				});
			$("#"+elementId).jqmShow();
		} else {
			$("#"+elementId).css("display", "");
		}
	}
}

function ajaxBefore(elementId, tickbox, url) {

	if (tickbox) {

		$("#"+elementId).html($("#waitModal").html());
		if ($("#"+elementId).hasClass("jqmID1")) {
			$("#"+elementId).jqmShow();

		} else {
			$("#"+elementId).css("display", "");
		}
	}
}

function ajaxPostUpdate (formId, postUrl, elementId, tickbox)
{
	if (elementId == null) {
		var elementId = 'dialog';
	}

	if ( $('#ajaxfilemanager').length ) {
		$('#ajaxfilemanager').val( tinyMCE.get('ajaxfilemanager').getContent({format : 'raw'}) );
	}

	var params = $("#"+formId).serialize();


	var myAjax = $.ajax({
		type: "POST",
		url: postUrl,
		data: params,
		before: ajaxBefore(elementId, tickbox, postUrl),
		success: function(msg){
    		ajaxSuccess(elementId, msg, tickbox);
  		}
	 });
};

function ajaxCartUpdate (formId, postUrl, elementId, tickbox)
{
	if (elementId == null) {
		var elementId = 'dialog';
	}

	var params = $("#"+formId).serialize();

	var myAjax = $.ajax({
		type: "POST",
		url: postUrl,
		data: params,
		before: ajaxBefore(elementId, tickbox, postUrl),
		success: function(msg){
			$("#cart").html(msg);
			if (document.getElementById("cartDiv")){
				$("#cartDiv").load(postUrl.match(/.*\/cart\//) + "showcart?ajax=true");
			}
  		}
	 });
};

function ajaxGetUpdate (elementId, url, tickbox)
{
	var myAjax = $.ajax({
		type: "GET",
		url: url,
		before: ajaxBefore(elementId, tickbox, url),
		success: function(msg){
    		ajaxSuccess(elementId, msg, tickbox);
  		}
	 });
};

function ajaxPictureUpdate(id, elementId, url, tickbox)
{
	var myAjax = $.ajax({
		type: "GET",
		url: url,
		before: ajaxBefore(elementId, tickbox, url),
		success: function(msg){
			match = $("#" + id).attr("id").match(/_.*/);

			msg = msg.replace(/^\s+|\s+$/g,"");
			pattern = "(.*)\/" + msg;

			var tmp = new Date();
			$("#thumb" + match).attr("src", $("#thumb" + match).attr("src").match(pattern)[1] + "/" + msg + "?" + tmp.getTime() ) ;
			$("#big" + match).attr("href", $("#big" + match).attr("href").match(pattern)[1] + "/" + msg + "?" + tmp.getTime() ) ;
  		}
	 });
};
