$(document).ready(function() {
	$('#plugin-errors').hide();

	$('#plugin-errors').click(function()
	{
		$('#plugin-errors').hide();
	});
});

function disablePlugin(urlwebsite, pluginName)
{
	
	$('.errors').hide();
	jQuery.ajax({
		type: "post",
		url:''+urlwebsite+'sys/backend_plugin/disableplugin/',
		data: 'pluginName=' + pluginName,
		success: function(response) {
			if(response)
			{
				var arrTmp = response.split('~~');
				pluginName = str_replace(" ","-",pluginName);
				$('#pluginstatus-'+pluginName).empty().html(arrTmp[0]);
				$('#cache-'+pluginName).empty().html(arrTmp[1]);
			}
		}
	});

	
}

$('.plugin-errors').click(function()
{
	$('#plugin-errors').hide();
});

function enablePlugin(urlwebsite, pluginName)
{
	$('.errors').hide();
	jQuery.ajax({
		type: "post",
		url:''+urlwebsite+'sys/backend_plugin/enableplugin/',
		data: 'pluginName=' + pluginName,
		success: function(response) {
			if(response =="errors in sql")
			{
				$('#plugin-errors').show();
				$('#installation-error').show();
			}else if(response =="install.sql"){
				$('#plugin-errors').show();
				$('#installsql').show();
			}else {
				var arrTmp = response.split('~~');
				pluginName = str_replace(" ","-",pluginName);
				
				$('#pluginstatus-'+pluginName).empty().html(arrTmp[0]);
				$('#cache-'+pluginName).empty().html(arrTmp[1]);
			}
		}
	});
}


function deletePlugin(urlwebsite, pluginName)
{
	$('.errors').hide();
	jQuery.ajax({
		type: "post",
		url:''+urlwebsite+'sys/backend_plugin/deleteplugin/',
		data: 'pluginName=' + pluginName,
		success: function(response) {
			if(response!='errors in sql')
			{
				$('#'+str_replace(" ","-",pluginName)+'-div').hide();
			}else
			{
				$('#plugin-errors').show();
				$('#uninstallation-error').show();
			}
		}
	});
}

function cachePlugin(pluginName)
{
	var urlwebsite = $('#hid-main-wsurl').val();
	jQuery.ajax({
		type: "post",
		url:''+urlwebsite+'sys/backend_ajax/cacheplugin/',
		data: 'plugin=' + pluginName,
		success: function(response) {
			if(response =='error')
			{
				alert('Plugin was not installed.');
			}
		}
	});


}


function str_replace (search, replace, subject)
{
	var result = "";
	var  oldi = 0;
	for (i = subject.indexOf (search); i > -1; i = subject.indexOf (search, i))
	{
		result += subject.substring (oldi, i);
		result += replace;
		i += search.length;
		oldi = i;
	}
	return result + subject.substring (oldi, subject.length);
}
