function doAjax(what){
	$.ajax({
		type: "GET",
		cache: false,
		url: what,
		dataType: "script",
		success: function(){

		},
		error: function(){
			alert("Ajax error!!!");
		}
	});
}


function doAjaxPost(where,parameters){
	$.ajax({
		type: "POST",
		cache: false,
		url: where,
		data: parameters,
		dataType: "script",
		success: function(){

		},
		error: function(){
			alert("Ajax error!!!");
		}
	});
}


function resetForm(formId){
	$("#"+formId).attr("doSend","no").find(".buttons input").show().parent().find(".loadingForm").remove();
}


function loadWindow(id,_title,_what,_width,_height,_extraAction){

	var $dialog = $('<div id="'+id+'"></div>').html("<div class='loading'></div>").dialog({
		autoOpen: false,
		title: _title,
		modal: true,
		resizable: false,
		closeOnEscape: false,

		beforeClose: function(){

			switch(_extraAction){
				case "refresh":
				window.location.reload();
				break;
			}
			$(this).find(".smallButton").die("click");
			$("#"+id).remove();
			$("#"+id).dialog("destroy");


		},

		draggable: false,
	});
	if(_width){
		$dialog.dialog("option","width",_width);
	}
	if(_height){
		$dialog.dialog("option","height",_height);
	}

	$dialog.dialog("open").load(_what);

}



function doConfirm(id,_title,_text,_action){
	var $dialog = $('<div id="'+id+'"></div>').html('<div class="formDiv"><span class="ui-icon ui-icon-notice" style="float:left; margin:0 7px 20px 0;"></span>'+_text+'</div>').dialog({
		modal: true,
		close: function(){
			$(this).parent().remove();
			$(this).remove();

		},
		resizable: false,
		draggable: false,

		buttons: {
			"Yes" : function(){
				$(this).html("<div class='loading'></div>");
				$(this).dialog({buttons:{}});
				eval(_action);
			},
			"Cancel": function(){
				$(this).dialog('close');
			}
		},
	title: _title});

}


function alertPopup(id,_title,_text,_icon,_action,_extraAction){
	switch(_icon){
		case "alert":
		_text= '<span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>' + _text;
		break;
		case "star":
		_text= '<span class="ui-icon ui-icon-star" style="float:left; margin:0 7px 20px 0;"></span>' + _text;
		break;
	}
	var $dialog = $('<div id="'+id+'""></div>').html("<div class='formDiv'>"+_text+"</div>").dialog({
		modal: true,
		close: function(){
			switch(_action){
				case "close":

				break;

				case "refresh":
				window.location.reload();
				break;

				case "redirect":
				self.location.href=_extraAction;
				break;
				case "custom":
				eval(_extraAction);
				break;
			}
			$(this).parent().remove();
			$(this).remove();

		},
		resizable: false,
		draggable: false,
		buttons: {
			OK: function(){
				$(this).dialog('close');

			}
		},
	title: _title});

}

function loadingWindow(id,_title){

	var $dialog = $('<div id="'+id+'"></div>').html("<div class='loading'></div>").dialog({

		title: _title,
		modal: true,
		resizable: false,
		closeOnEscape: false,
		open: function(event, ui) {
			// Hide close button
			$(this).parent().children().children(".ui-dialog-titlebar-close").hide();
		},
		close: function(){
			$(this).parent().remove();
			$(this).remove();

		},
		draggable: false
	});


}

function uploadFilesWindow(id){
	var $dialog = $('<div id="'+id+'""></div>').html("<div class='progressBar'><div class='progress'></div></div><div class='progressStatus'>Please wait</div>").dialog({
		modal: true,
		beforeClose: function(){
			if(uploadStarted){
				if(confirm("Are you you want to cancel upload")){
					window.location.reload();
				}
			}
		},
		close: function(){
			$(this).parent().remove();
			$(this).remove();

		},
		resizable: false,
		draggable: false,
		width: 400,
		height: 100,
	title: "Uploading files..."});

}

function closeDialog(idDialog){
	$('#'+idDialog).dialog("close");

}

function createUpload(id,_url){

	$('#'+id).fileupload({
		dataType: 'json',
		autoUpload: 'true',
		url: _url,
		start: function (e,data){
			uploadStarted=true;
			uploadFilesWindow(id+"Window");
		},
		error: function (e,data){
			uploadStarted=false;
			alert("Eroare");
		},
		progress: function (e, data) {
			var progress = parseInt(data.loaded / data.total * 100, 10);
			$("#"+id+"Window .progressBar .progress").width(progress+"%");

		},
		done: function (e,data){
			uploadStarted=false;
			var defaAction="javascript: window.location.reload();";
			if(data.result["action"]!=""){
				defaAction=data.result["action"];
			}
			if(data.result["success"]=="0"){

				$("#"+id+"Window").html("<div class='formResult formError'>"+data.result["errorMsg"]+"<div class='buttons'><input type='button' class='button' value='ok' onClick='"+defaAction+"'></div></div>");
			}
			else{
				$("#"+id+"Window").html("<div class='formResult formSuccess'>"+data.result["message"]+"<div class='buttons'><input type='button' class='button' value='ok' onClick='"+defaAction+"'></div></div>");
			}


		}

	});
}


function makeFieldEditable(id){

	$("#"+id).tinymce({
		// Location of TinyMCE script
		script_url : 'tiny_mce/tiny_mce.js',

		// General options
		theme : "advanced",
		plugins : "style,advhr,iespell,paste,table,inlinepopups,contextmenu,directionality,noneditable,visualchars,xhtmlxtras,template,advlist,advlink",

		// Theme options
		theme_advanced_buttons1 : "bold,italic,underline,strikethrough,sub,sup,|,bullist,numlist,|,outdent,indent,|,justifyleft,justifycenter,justifyright,justifyfull,|,forecolor,backcolor,fontsizeselect,link,unlink,|,hr,charmap",
		theme_advanced_buttons2 : "",
		theme_advanced_buttons3 : "",
		theme_advanced_buttons4 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : false,

		// Example content CSS (should be your site CSS)
		content_css : "editor.css",

		// Drop lists for link/image/media/template dialogs
		paste_use_dialog : false,
		paste_auto_cleanup_on_paste : true,
		paste_convert_headers_to_strong : true,
		paste_remove_spans : true,
		paste_remove_styles : true,
		paste_remove_styles_if_webkit: true


	});
};
