
$.fn.isDialogOpen = function() {
   return $("#gadget-dialog").parents(".ui-dialog").is(":visible");
}

$.fn.closeDialog = function() {
	$("#gadget-dialog").dialog('close');
}

$.fn.openDialog = function(id, view, model, gadgetName, method) {
  var data = "view="+view;
  data += "&model=" + model;
  data += "&gadget=" + gadgetName;
  data += "&ID=" + id + "&action=dialog&method=" + method;
  $.ajax({
    url: "/ajax/dialog.php",    
    data: data,          
    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.openGMapDialog = function(id, latitudeValue, longitudeValue, title, html) {
  $("#gadget-dialog").html(html);
  $("#gadget-dialog").dialog({ modal: false });
  $("#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(html);
  $("#gadget-dialog").dialog( "option", "position", "center" );
  $(document).ready(function() {
    $("#" + id + "_dialog").gMap({ markers: [{ latitude: latitudeValue,
      longitude: longitudeValue,
      popup: true }], zoom: 14 });
  });
};

$.fn.sendDialogForm = function(ajax, dialog, infoBox, callback) {
	var _this = $(this);
	var data = $(this).getDialogFormData();
	//alert(data);
	data = replaceAll(data, "+", "%2B");
	if (typeof(data) != "undefined") {
		var postData = "dialog=" + dialog;
	    postData += "&action=sendDialog";
	    postData += data;
       // alert(postData);
		$.ajax({  
		  type: "POST",  
		  url: "/ajax/" + ajax + ".php",
		  data: postData,  
		  success: function(txt) {
			  $(this).closeWaitDialog();
			  // check for error
			  var status = ajaxGetParam(txt, "status");
			  if (status == "error") {
				  $(_this).setErrorForDialogFields(txt, infoBox);
			  }
			  if (status == "success") {
				  var id = ajaxGetParam(txt, "ID");
				  $("#ID").val(id);
				  $(_this).resetErrorForDialogFields(txt, infoBox);
				  if (typeof(callback) != "undefined") {		  
					  callback(txt);
				  }
			  }
		  }  
		});  		
	}
};

$.fn.getDialogFormData = function() {
	$.fn.getDialogFormData.data = "";
	this.each(function() {		
		var type = this.type, tag = this.tagName.toLowerCase();
		var name = this.name;
		if (tag == 'div') {
			$(':input', this).getDialogFormData();
			return;
		}
		if (tag == 'form') {
			$(':input', this).getDialogFormData();
			return;
		}
		if (type == 'text' || type == 'password' || tag == 'textarea' || type == 'hidden') {
			$.fn.getDialogFormData.data += "&" + name + "=" + this.value;
		} else if (type == 'checkbox')
			$.fn.getDialogFormData.data += "&" + name + "=" + $(this).is(':checked');
		else if (tag == 'select')
			$.fn.getDialogFormData.data += "&" + name + "=" + this.value;
		else if (type == 'radio') {
		    if ($(this).is(':checked')) {
			   $.fn.getDialogFormData.data += "&" + name + "=" + this.value;
		    }
	    }
	});
	if (typeof($.fn.getDialogFormData.data) != "undefined") {
		return $.fn.getDialogFormData.data;
	}
	return "";
};

$.fn.resetErrorForDialogFields = function(txt, infoBox) {
	this.each(function() {		
		var type = this.type, tag = this.tagName.toLowerCase();
		var name = this.name;
		if (tag == 'div') {
			$(':input', this).resetErrorForDialogFields(txt, infoBox);
			return;
		}
		if (tag == 'form') {
			$(':input', this).resetErrorForDialogFields(txt, infoBox);
			return;
		}		
		if (tag == 'tr') {
			$(':input', this).resetErrorForDialogFields(txt, infoBox);
			return;
		}		
		var errorCSS = $(this).attr("class");
		errorCSS = errorCSS.replace("errorField", "");
		$(this).attr("class",  errorCSS);
		$("#" + infoBox).html("");
		$("#" + infoBox).hide();
	});
};

$.fn.setErrorForDialogFields = function(txt, infoBox) {
	$.fn.setErrorForDialogFields.data = "";
	this.each(function() {		
		var type = this.type, tag = this.tagName.toLowerCase();
		var name = this.name;
		if (tag == 'div') {
			$(':input', this).setErrorForDialogFields(txt, infoBox);
			return;
		}
		if (tag == 'form') {
			$(':input', this).setErrorForDialogFields(txt, infoBox);
			return;
		}		
		if (tag == 'tr') {
			$(':input', this).setErrorForDialogFields(txt, infoBox);
			return;
		}		
		var error = ajaxGetParam(txt, name);
		if (error != null && error.length > 0) {			
			$(this).attr("class",  "errorField " + $(this).attr("class"));
			$.fn.setErrorForDialogFields.data += "<div>" + error + "</div>";
		}
		$("#" + infoBox).attr("class", "dialogErrorBox");
		if ($.fn.setErrorForDialogFields.data == "") {
			// check for status text
			$.fn.setErrorForDialogFields.data = ajaxGetParam(txt, "statusText");
		}
		$("#" + infoBox).html($.fn.setErrorForDialogFields.data);
		$("#" + infoBox).showAndHide(8000);
	});
};

