$.fn.clearForm = function() {
	return this.each(function() {		
		var type = this.type, tag = this.tagName.toLowerCase();
		if (tag == 'div')
			return $(':input', this).clearForm();
		if (tag == 'form')
			return $(':input', this).clearForm();
		if (type == 'text' || type == 'password' || tag == 'textarea')
			this.value = '';
		else if (type == 'checkbox' || type == 'radio')
			this.checked = false;
		else if (tag == 'select')
			//this.selectedIndex = -1;
			$(this).find("option:first").attr("selected", true);
	});
};

$.fn.showAndHide = function(delay) {
	$(this).show();
	var _this = $(this);
	setTimeout(function() {
	  $(_this).fadeOut("slow");
    }, delay);	
}

$.fn.openWaitDialog = function(title, text) {
	var dialog = '<div class="waitDialog">';
	dialog += text;
	dialog += "<p/>";
	dialog += "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\"><tr><td align=\"center\">";
	dialog += "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" style=\"border-radius:5px; border:0.1em solid green;\">";
	dialog += "<tr>";
	for (var i = 0; i < 20; i++) {
		dialog += "<td width=\"8px\" id=\"waitDialogProgressBar_" + i + "\">&#160;</td>"; 
	}
	dialog += "</tr>";
	dialog += "</table>";
	dialog += "</td></tr></table>";
	dialog += '</div>';
    $("#wait-dialog").html(dialog);
    $("#wait-dialog").dialog({ modal: true });
    $("#wait-dialog").dialog( "option", "title", title );
    $("#wait-dialog").dialog( "option", "closeText", "hide" );
    $("#wait-dialog").dialog( "option", "closeOnEscape", true );
//    $("#wait-dialog").dialog( "option", "height", "auto" );
//    $("#wait-dialog").dialog( "option", "width", "auto" );
    $("#wait-dialog").html(dialog);
    $("#wait-dialog").dialog( "option", "position", "center" );	
    $("#wait-dialog").attr("progress", 0);
    waitDialogProgressbar();
} 

function waitDialogProgressbar() {
    var progress = $("#wait-dialog").attr("progress");
    if (progress >= 20) {
    	if( $('#wait-dialog').dialog('isOpen') ) {
    		for (var i = 20; i > 0; i--) {
    	       $("#waitDialogProgressBar_" + i).css("background-color", "");
    		}
    	    $("#wait-dialog").attr("progress", 1);
    		setTimeout("waitDialogProgressbar()", 100);
    		return;
    	} else {
    	   return;
    	}
    }
    $("#waitDialogProgressBar_" + progress).css("background-color", "green");
//    alert($("#progressBar_" + progress));
    $("#wait-dialog").attr("progress", ++progress);
	setTimeout("waitDialogProgressbar()", 100);
}


$.fn.openLoadingDialog = function(title, text) {
	var dialog = '<div class="loadingDialog">';
	dialog += text; 
	dialog += "<p/>";
	dialog += "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\"><tr><td align=\"center\">";
	dialog += "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" style=\"border-radius:5px; border:0.1em solid green;\">";
	dialog += "<tr>";
	for (var i = 0; i < 20; i++) {
		dialog += "<td width=\"8px\" id=\"loadingDialogProgressBar_" + i + "\">&#160;</td>"; 
	}
	dialog += "</tr>";
	dialog += "</table>";
	dialog += "</td></tr></table>";
	dialog += '</div>';
    $("#loading-dialog").html(dialog);
    $("#loading-dialog").dialog({ modal: true });
    $("#loading-dialog").dialog( "option", "title", title );
    $("#loading-dialog").dialog( "option", "closeText", "hide" );
    $("#loading-dialog").dialog( "option", "closeOnEscape", true );
    $("#loading-dialog").html(dialog);
    $("#loading-dialog").dialog( "option", "position", "center" );
    $("#loading-dialog").attr("progress", 0);
    loadingDialogProgressbar();
}

function loadingDialogProgressbar() {
    var progress = $("#loading-dialog").attr("progress");
    if (progress >= 20) {
    	if( $('#loading-dialog').dialog('isOpen') ) {
    		for (var i = 20; i > 0; i--) {    			
    	       $("#loadingDialogProgressBar_" + i).css("background-color", "");
    		}
    	    $("#loading-dialog").attr("progress", 1);
    		setTimeout("loadingDialogProgressbar()", 100);    		
    		return;
    	} else {
    	   return;
    	}
    }
    $("#loadingDialogProgressBar_" + progress).css("background-color", "green");
//    alert($("#progressBar_" + progress));
    $("#loading-dialog").attr("progress", ++progress);
	setTimeout("loadingDialogProgressbar()", 100);
}

$.fn.closeWaitDialog = function() {
    $("#wait-dialog").dialog( "close" );	
} 

