$.fn.refreshGadget = function(view, model, gadgetName, search) {
	var _gadgetName = gadgetName;
	var data = "view="+view;
	data += "&model=" + model;
	data += "&name=" + gadgetName;
	data += "&search=" + search;
	$.ajax({
          url: "/ajax/gadget.php",
          data: data,          
	      cache: false,
	         success: function(txt){
               $("#gadgetContent_" + _gadgetName).html(txt);
               $("#gadget_search_" + gadgetName).focus().val(search);
	         }
	  });
};

$.fn.refreshGadgetData = function(view, model, gadgetName, search) {
	var _gadgetName = gadgetName;
	var data = "view="+view;
	data += "&model=" + model;
	data += "&name=" + gadgetName;
	data += "&search=" + search;
	data += "&mode=data"
	$.ajax({
          url: "/ajax/gadget.php",
          data: data,          
	      cache: false,
	         success: function(txt){
               $("#gadgetContentData_" + _gadgetName).html(txt);
	         }
	  });
};

$.fn.clearEvent = function(ID, view, model, gadgetName) {
	if (ID > 0) {
		$(this).openForm(ID, view, model, gadgetName);
	} else {
	   $(this).clearForm();	
	}	
};

$.fn.openForm = function(id, view, model, gadgetName, gadgetLayout, method, infoBox, callback) {
//  alert("ID=" + id + "&view=" + view + "&model=" + model + "&name=" + gadgetName + "&action=dialog&method=" + method);
  $.ajax({
    url: "/ajax/gadget.php",
    data: "ID=" + id + "&view=" + view + "&model=" + model + "&name=" + gadgetName + "&action=dialog&method=" + method,          
    cache: false,
    success: function(txt){
       var action = ajaxGetParam(txt, "action");
       var width = ajaxGetParam(txt, "width");
       var height = ajaxGetParam(txt, "height");
       var title = ajaxGetParam(txt, "title");
       if (action == "dialog") {
          //setter
          $("#gadget-dialog").html(txt);
          $("#gadget-dialog").dialog({ modal: true });
          $("#gadget-dialog").dialog( "option", "title", title );
          $("#gadget-dialog").dialog( "option", "closeText", "hide" );
          $("#gadget-dialog").dialog( "option", "closeOnEscape", true );
          $("#gadget-dialog").dialog( "option", "height", "auto" );
          $("#gadget-dialog").dialog( "option", "width", "auto" );
          $("#gadget-dialog").html(txt);
          $("#gadget-dialog").dialog( "option", "position", "center" );
       } else {    	   
       }
       if (callback != null) {
    	   callback();
       }
    }
   });
};

$.fn.newEvent = function(view, model, gadgetName, gadgetLayout, infoBox, callback) {
  $.ajax({
    url: "/ajax/gadget.php",
    data: "view=" + view + "&model=" + model + "&name=" + gadgetName + "&action=dialog&method=new",          
    cache: false,
    success: function(txt){
       var action = ajaxGetParam(txt, "action");
       var width = ajaxGetParam(txt, "width");
       var height = ajaxGetParam(txt, "height");
       var title = ajaxGetParam(txt, "title");
       if (action == "dialog") {
          //setter
          $("#gadget-dialog").html(txt);
          $("#gadget-dialog").dialog({ modal: true });
          $("#gadget-dialog").dialog( "option", "title", title );
          $("#gadget-dialog").dialog( "option", "closeText", "hide" );
          $("#gadget-dialog").dialog( "option", "closeOnEscape", true );
          $("#gadget-dialog").dialog( "option", "height", "auto" );
          $("#gadget-dialog").dialog( "option", "width", "auto" );
          $("#gadget-dialog").html(txt);
          $("#gadget-dialog").dialog( "option", "position", "center" );
       } else {
       }
    }
   });
};

$.fn.uploadFile = function(file, ajax, view, model, gadgetName, gadgetLayout, infoBox, callback) {
	var xhr = new XMLHttpRequest();
	this.xhr = xhr;
	 
	var prozent;
	this.xhr.upload.addEventListener("progress", function(e) {
	   if (e.lengthComputable) {	       
	      prozent = Math.round((e.loaded * 100) / e.total);
	   }
	}, false);
	xhr.upload.addEventListener("load", function(e){
	   prozent  = 100;
	}, false);
	
	var fd = new FormData;
	fd.append("File", file);
	 
	xhr.open("POST", "/ajax/" + ajax + ".php", true);
	xhr.overrideMimeType('text/plain; charset=x-user-defined-binary');
	xhr.send(fd);  	 
}

$.fn.saveForm = function(ajax, view, model, gadgetName, gadgetLayout, infoBox, callback) {
	var _this = $(this);
	var data = $(this).getSaveFormData();
	data = replaceAll(data, "+", "%2B");
//	alert(data);
	if (typeof(data) != "undefined") {
		// submit using ajax post call to address /ajax/events.php
		var postData = "view=" + view + "&model=" + model + "&gadget=" + gadgetName;
	    postData += "&layout=" + gadgetLayout;
	    postData += "&action=saveOrUpdate";
	    postData += data;
        //alert(postData);
		$.ajax({  
		  type: "POST",  
		  url: "/ajax/" + ajax + ".php",
		  data: postData,  
		  success: function(txt) { 
			  // check for error
			  var status = ajaxGetParam(txt, "status");
			  if (status == "error") {
				  $(_this).setErrorForFields(txt, infoBox);
			  }
			  if (status == "success") {
				  var id = ajaxGetParam(txt, "ID");
				  $("#ID").val(id);
				  $(_this).resetErrorForFields(txt, infoBox);
				  if (typeof(callback) != "undefined") {		  
					  callback(txt);
				  }
			  }
		  }  
		});  		
	}
	$(_this).refreshGadget(view, model, gadgetName, $("#gadget_search_"+gadgetName).val());
};

$.fn.getSaveFormData = function() {
	$.fn.getSaveFormData.data = "";
	this.each(function() {		
		var type = this.type, tag = this.tagName.toLowerCase();
		var name = this.name;
//		alert("type = " + type + ", name =" + name + ", data = " + $.fn.getSaveFormData.data);
		if (tag == 'div') {
			$(':input', this).getSaveFormData();
			return;
		}
		if (tag == 'form') {
			$(':input', this).getSaveFormData();
			return;
		}
		if (type == 'text' || type == 'password' || tag == 'textarea' || type == 'hidden') {
			var localValue = replaceAll(this.value, "&", "%26");
			$.fn.getSaveFormData.data += "&" + name + "=" + localValue;
		} else if (type == 'checkbox' || type == 'radio') {
			$.fn.getSaveFormData.data += "&" + name + "=" + $(this).is(':checked');
		} else if (tag == 'select') {
			var localValue = replaceAll(this.value, "&", "%26");
			$.fn.getSaveFormData.data += "&" + name + "=" + localValue;
		}		
	});
	if (typeof($.fn.getSaveFormData.data) != "undefined") {
		return $.fn.getSaveFormData.data;
	}
	return "";
};

$.fn.setErrorForFields = function(txt, infoBox) {
	$.fn.setErrorForFields.data = "";
	this.each(function() {		
		var type = this.type, tag = this.tagName.toLowerCase();
		var name = this.name;
		if (tag == 'div') {
			$(':input', this).setErrorForFields(txt, infoBox);
			return;
		}
		if (tag == 'form') {
			$(':input', this).setErrorForFields(txt, infoBox);
			return;
		}		
		if (tag == 'tr') {
			$(':input', this).setErrorForFields(txt, infoBox);
			return;
		}		
		var error = ajaxGetParam(txt, name);
		if (error != null && error.length > 0) {			
			$(this).attr("class",  "errorField " + $(this).attr("class"));
			$.fn.setErrorForFields.data += "<div>" + name + ":" + error + "</div>";
		}
		$("#" + infoBox).attr("class", "dialogErrorBox");
		if ($.fn.setErrorForFields.data == "") {
			// check for status text
			$.fn.setErrorForFields.data = ajaxGetParam(txt, "statusText");
		}
		$("#" + infoBox).html($.fn.setErrorForFields.data);
		$("#" + infoBox).showAndHide(8000);
	});
};

$.fn.resetErrorForFields = function(txt, infoBox) {
	this.each(function() {		
		var type = this.type, tag = this.tagName.toLowerCase();
		var name = this.name;
		if (tag == 'div') {
			$(':input', this).resetErrorForFields(txt, infoBox);
			return;
		}
		if (tag == 'form') {
			$(':input', this).resetErrorForFields(txt, infoBox);
			return;
		}		
		if (tag == 'tr') {
			$(':input', this).resetErrorForFields(txt, infoBox);
			return;
		}		
		var errorCSS = $(this).attr("class");
		errorCSS = errorCSS.replace("errorField", "");
		$(this).attr("class",  errorCSS);
		$("#" + infoBox).html("");
		$("#" + infoBox).hide();
	});
};

$.fn.confirmFormAction = function(eventID, title, text, yes, no, callback) {
    $("#confirm-dialog").html(text);
    $("#confirm-dialog").dialog({ modal: true });
    $("#confirm-dialog").dialog( "option", "buttons", [
     {
       text: yes,
       click: function() {
    	   $(this).dialog("close"); 
           callback(true);
       }
     },
     {
         text: no,
         click: function() { 
           $(this).dialog("close"); 
           callback(false);
         }
       }
    ]);
    $("#confirm-dialog").dialog( "option", "title", title );
    $("#confirm-dialog").dialog( "option", "closeText", "hide" );
    $("#confirm-dialog").dialog( "option", "closeOnEscape", true );
    $("#confirm-dialog").dialog( "option", "height", "auto" );
    $("#confirm-dialog").dialog( "option", "width", "auto" );
    $("#confirm-dialog").dialog( "option", "position", "center" );
}

$.fn.deleteForm = function(ajax, view, model, gadgetName, gadgetLayout, infoBox, callback) {
	var _this = $(this);
	var data = $(this).getSaveFormData();
	if (typeof(data) != "undefined") {
		// submit using ajax post call to address /ajax/events.php
		var postData = "view=" + view + "&model=" + model + "&gadget=" + gadgetName;
	    postData += "&layout=" + gadgetLayout;
	    postData += "&action=delete";
	    postData += data;
		$.ajax({  
		  type: "POST",  
		  url: "/ajax/" + ajax + ".php",  
		  data: postData,  
		  success: function(txt) { 
			  // check for error
			  var status = ajaxGetParam(txt, "status");
			  if (status == "error") {
				  $(_this).setErrorForFields(txt, infoBox);
			  }
			  if (status == "success") {
				  $("#ID").val("");
				  $(_this).resetErrorForFields(txt, infoBox);
				  if (typeof(callback) != "undefined") {		  
					  callback(txt);
				  }
			  }
		  }  
		});  		
	}
	$(_this).refreshGadget(view, model, gadgetName, $("#gadget_search_"+gadgetName).val());
};

