/*
	id_or_class - jQuery selector: '#ddd' or '.ddd', and else
	page - page in database in table pages
*/
function send_message(id_or_class)
{
	$.jGrowl.defaults.position='center';
	var str = $(id_or_class).serialize();
	$.ajax({
		url: "/?p=ajax&module=standart&action=send_message",
		cache: false,
		type: "POST",
		data: str,
		success:function(html)
		{
			if(html == 'ok')
			{
				$.jGrowl('Message send!');
				$(id_or_class).trigger('reset');
				document.getElementById('captcha').src='/captcha/captcha.php?'+Math.random()+'.png';
			}
			else
				$.jGrowl(html,{header:'Error',theme:'error',closerTemplate:'<div>[Close all]</div>'})
			
		}
	});
}

/*----------------------------------------------------------------------------------------------------------------*/
function save_title(id_or_class, page)				//id_or_class form
{
	$.jGrowl.defaults.position='center';
	var str = $(id_or_class).serialize();
	$.ajax({
		url: "/?p=ajax&module=standart&action=save_title",
		cache: false,
		type: "POST",
		data: str+'&page='+page,
		success:function(html)
		{
			if(html == 'ok')
			{
				$.jGrowl('Title, keywords and description saved!');
			}
			else
				$.jGrowl(html,{header:'Error',theme:'error',closerTemplate:'<div>[Close all]</div>'})
			
		}
	});
}

/*----------------------------------------------------------------------------------------------------------------*/
function save_content(id_or_class, page)
{
	$.jGrowl.defaults.position='center';
	var content = $(id_or_class).find('.nicEdit-main').html();
	$.ajax({
		url: "/?p=ajax&module=standart&action=save_content",
		cache: false,
		type: "POST",
		data: 'content='+content+'&page='+page,
		success:function(html)
		{
			if(html == 'ok')
			{
				$.jGrowl('Content saved!');
			}
			else
				//$.jGrowl(html,{header:'Error',theme:'error',closerTemplate:'<div>[Close all]</div>'})
				alert(html);
		}
	});
}
/*----------------------------------------------------------------------------------------------------------------*/
function save_email(id_or_class)
{
	$.jGrowl.defaults.position='center';
	var str = $(id_or_class).serialize();
	$.ajax({
		url: "/?p=ajax&module=standart&action=save_email",
		cache: false,
		type: "POST",
		data: str,
		success:function(html)
		{
			if(html == 'ok')
			{
				$.jGrowl('Email saved!');
			}
			else
				$.jGrowl("Error. Please contact webmaster!",{header:'Error',theme:'error',closerTemplate:'<div>[Close all]</div>'})
			
		}
	});
}
/*----------------------------------------------------------------------------------------------------------------*/
function save_map(id_or_class, width, height)
{
	$.jGrowl.defaults.position='center';
	var str = $(id_or_class).serialize();
	$.ajax({
		url: "/?p=ajax&module=standart&action=save_map",
		cache: false,
		type: "POST",
		data: str+"&width="+width+"&height="+height,
		success:function(html)
		{
			if(html != 'error')
			{
				$.jGrowl('Map saved!');
				$(id_or_class).trigger('reset');
				$("#code").html(html);
			}
			else
				$.jGrowl("Error. Please contact webmaster!",{header:'Error',theme:'error',closerTemplate:'<div>[Close all]</div>'})
			
		}
	});
}

/*----------------------------------work with products---------------------------------------------------------------*/
function save_product(id_or_class, id_or_class_cont, id, link, table)		//id_or_class - for form, id_or_class_cont - for container for products, id - id product, link - for delete block, table in db
{
	$.jGrowl.defaults.position='center';
	var str = $(id_or_class).serialize();
	$.ajax({
		url: "/?p=ajax&module=standart&action=save_product",
		cache: false,
		type: "POST",
		data: str+"&table="+table,
		success:function(html)
		{
			if(id == '')
			{
				if(html != 'error')
				{
					$.jGrowl('Saved!');
					$(id_or_class).trigger('reset');
					$(id_or_class_cont).prepend(html);
				}
				else
					$.jGrowl("Error. Please contact webmaster!",{header:'Error',theme:'error',closerTemplate:'<div>[Close all]</div>'});
			}
			else
			{
				if(html != 'error')
				{
					link.remove();
					$(id_or_class_cont).prepend(html);
					$(id_or_class).trigger('reset');
					$.jGrowl('Changed!');
				}
				else
					$.jGrowl("Error. Please contact webmaster!",{header:'Error',theme:'error',closerTemplate:'<div>[Close all]</div>'});
			}
			
		}
	});
}

function delete_product(id, link, table)				//product's id and link on delete block
{
	$.jGrowl.defaults.position='center';
	if(confirm('Are you sure?'))
	{
		$.ajax({
			url: "/?p=ajax&module=standart&action=delete_product",
			cache: false,
			type: "POST",
			data: "id="+id+"&table="+table,
			success:function(html)
			{
				if(html != 'error')
				{
					$.jGrowl('Project deleted!');
					link.remove();
					//alert(html);
				}
				else
					$.jGrowl(html,{header:'Error',theme:'error',closerTemplate:'<div>[Close all]</div>'});
				
			}
		});
	}
}

function edit_product(id_or_class, id, table)			//id_or_class - for form
{
	$.ajax({
		url: "/?p=ajax&module=standart&action=edit_product",
		cache: false,
		type: "POST",
		data: "id="+id+"&table="+table,
		success:function(html)
		{
			if(html != 'error')
			{
				var l_form = $(id_or_class);
				var data = eval("("+html+")");
				l_form.find("input[name='name']").attr({'value':data.name});
				l_form.find("textarea[name='description']").attr({'value':data.description});
				l_form.find("input[name='image']").attr({'value':data.image});
				l_form.find("input[name='id']").attr({'value':data.id});
			}
			else
				$.jGrowl(html,{header:'Error',theme:'error',closerTemplate:'<div>[Close all]</div>'})
			
		}
	});
	
}

/*-----------------change password--------------------------------------*/
function change_password(id_or_class)
{
	$.jGrowl.defaults.position='center';
		var str = $(id_or_class).serialize();
		$.ajax({
			url: "/?p=ajax&module=standart&action=change_password",
			cache: false,
			type: "POST",
			data: str,
			success:function(html)
			{
				if(html == 'ok')
				{
					$.jGrowl('Changes saved!');
					$(id_or_class).trigger('reset');
					//alert(html);
				}
				else
					//alert(html);
					$.jGrowl(html,{header:'Error',theme:'error',closerTemplate:'<div>[Close all]</div>'});
				
			}
		});
}

/*----------------login------------------------------------------------*/
function log_in(id_or_class)
{
	$.jGrowl.defaults.position='center';
	var str = $(id_or_class).serialize();
	$.ajax({
		url: "/?p=ajax&module=standart&action=log_in",
		cache: false,
		type: "POST",
		data: str,
		success:function(html)
		{
			if(html == 'ok')
			{
				location.href='/adminpart.php';
			}
			else
			{
				$.jGrowl(html,{header:'Error',theme:'error',closerTemplate:'<div>[Close all]</div>'});
				$(id_or_class).trigger('reset');
			}
		}
	});
}
