

function serializeCheckboxes()
{
	s = '';
	jQuery('#grid td input[@type=checkbox]').each(function(){
		if (this.checked) 
		{
			s += '&' + this.value;
		}
	})
	s = s.substr(1, s.length-1);
		
	return s;
}


function deserializeCheckboxes(str)
{
	items = str.split('&');

	return items;
}


function uncheckedCheckboxes()
{
	jQuery('#tablelist tbody td input[@type=checkbox]').each(function(){
		this.checked = '';
	})

	return true;
}


function publishItems(script, action, id_single)
{
	if (action == 'toggle')
	{
		$('#cb' + id_single).attr('checked', 'checked');
	}

	items = serializeCheckboxes();
	//startAnimation();
	$.getJSON(
		script,
		{
			action: action,
			items: items,
			rand: Math.floor(Math.random()*77877) + '' + Math.floor(Math.random()*88788) + '' + Math.floor(Math.random()*99899)
		},
		onAjaxSuccess
	);


	function onAjaxSuccess(data)
	{
		if (!data.error)
		{
			jQuery('#grid td input[@type=checkbox]').each(function(){
				if (this.checked)
				{
					if (data.action == 1)
					{
						img_src = 'green_button.png';
					}
					else
					{
						img_src = 'red_button.png';
					}
					jQuery('#publ_' + this.value).attr('src', '/img/ico/' + img_src);
					this.checked = false;
				}
			})
		}
		else
		{
			alert(data.error);
			//tb_open_new('news.php?event=alert&TB_iframe=true&height=200&width=400&modal=true&message=' + data.error||'');
		}
		//finishAnimation();
	}
}


function deleteItems(script, action, id_single, event)
{
	if (action == 'toggle')
	{
		uncheckedCheckboxes();
		$('#cb' + id_single).attr('checked', 'checked');
		items = id_single;
	}
	else
	{
		items = serializeCheckboxes();
	}

	//startAnimation();
	$.getJSON(
		script,
		{
			event: event,
			action: action||'',
			items: items||''
		},
		onAjaxSuccess
	);


	function onAjaxSuccess(data)
	{
		if (!data.error)
		{
			location = script.substring(0, script.indexOf('deleteItems'));
			/*
			var ids = items.split('&');
			var delete_status = data.report.split(',');
			for (var i=0; i<ids.length; i++)
			{
				if (delete_status[i] == 1)
				{
					$('#grid #tr_' + ids[i]).remove();
				}
				else
				{
					$('#grid #tr_' + ids[i] + ' #cb' + ids[i]).attr('checked', false);
				}
			}
			*/
		}
		else
		{
			//alert(data.error);
			//tb_open_new('news.php?event=alert&TB_iframe=true&height=200&width=500&modal=true&message=' + data.error);
		}
		//finishAnimation();
	}
}


function getRegions(country_ID)
{
	$('#region_ID').empty();
	$('#city_ID').empty();

	if (country_ID != '')
	{
		//startAnimation();
		$.post(
			'/site/regions/getList/' + country_ID,
			{},
			onAjaxSuccess
		);
	}
	function onAjaxSuccess(data)
	{
		$('#region_list').html(data);
		/*
		if (!data.error)
		{

		}
		else
		{
			//alert(data.error);
			//tb_open_new('news.php?event=alert&TB_iframe=true&height=200&width=500&modal=true&message=' + data.error);
		}
		*/
		//finishAnimation();
	}
}


function getCities(region_ID)
{
	//alert('getCities');
	if (region_ID != '')
	{
		//startAnimation();
		$.post(
		  	'/site/cities/getList/' + region_ID,
		  	{},
		  	onAjaxSuccess
		);
	}
	function onAjaxSuccess(data)
	{
		//alert(data);
		$('#city_list').html(data);
		/*
		if (!data.error)
		{

		}
		else
		{
			//alert(data.error);
			//tb_open_new('news.php?event=alert&TB_iframe=true&height=200&width=500&modal=true&message=' + data.error);
		}
		*/
		//finishAnimation();
	}
}


function toggleSpecial(is_special)
{	
	if (is_special == 1)
	{
		$('#special').show('slow');
	}
	else
	{
		$('#special').hide('slow');
	}
}


function startAnimation()
{
	//document.getElementById('ajax_act').style.display = 'block';
}


function finishAnimation()
{
	//document.getElementById('ajax_act').style.display = 'none';
}

