function postsShowCategories() {
	new Ajax.Request(
		'/cms/posts/getcategories/',
		{
			onSuccess: function(json) {
				$('categories').update(json.responseText);
			},
			parameters: null,
			method: 'post'
		}
	);
}

function postsShowPosts(category) {
	var params = 'category=' + category;

	new Ajax.Request(
		'/cms/posts/getposts/',
		{
			onSuccess: function(json) {
				$('posts').update(json.responseText);
			},
			parameters: params,
			method: 'post'
		}
	);
}

function postsShowPost(alias) {
	var params = 'alias=' + alias;

	new Ajax.Request(
		'/cms/posts/getpost/',
		{
			onSuccess: function(json) {
				$('post').update(json.responseText);
			},
			parameters: params,
			method: 'post'
		}
	);
}

function postsShowSuggests(page) {
	if ($('amazon').value != '') {
		var params = 'index=' + $('amazon').value;
		var endpoint = 'amazon';
	} else {
		var params = 'index=' + $('edelight').value;
		var endpoint = 'edelight';
	}
	
	params += '&query=' + $('query').value + '&page=' + page;
	
	$('page').value = page;
	
	new Ajax.Request(
		'/cms/posts/get' + endpoint + '/',
		{
			onSuccess: function(json) {
				$('suggests').update(json.responseText);
			},
			parameters: params,
			method: 'post'
		}
	);
}

function postsSuggestsApply(id) {
	$('textfield').value = $('description_' + id).innerHTML;
	$('image').value = $('image_' + id).innerHTML;
	$('link').value = $('link_' + id).innerHTML; 
	$('title').value = $('title_' + id).innerHTML;
	$('alias').value = makeAlias($('title_' + id).innerHTML); 
}

function postsSave() {
	var params = $('post_form').serialize();
		
	new Ajax.Request(
		'/cms/posts/setpost/',
		{
			onSuccess: function(json) {
				$('status').update(json.responseText);
			},
			parameters: params,
			method: 'post'
		}
	);
}

function postsDel(id) {
	var params = 'id=' + id;
		
	new Ajax.Request(
		'/cms/posts/delpost/',
		{
			onSuccess: function(json) {
				$('status').update(json.responseText);
			},
			parameters: params,
			method: 'post'
		}
	);
}

function makeAlias(string) {
	string = string.toLowerCase();
	string = string.replace(/[äåæàáâã]/g, 'a');
	string = string.replace(/[ç]/g, 'c');
	string = string.replace(/[Ð]/g, 'd');
	string = string.replace(/[èéêë]/g, 'e');
	string = string.replace(/[ìíîï]/g, 'i');
	string = string.replace(/[ñ]/g, 'n');
	string = string.replace(/[ðòóôõöøŒ]/g, 'o');
	string = string.replace(/[ßŠš]/g, 's');
	string = string.replace(/[ùúûüµ]/g, 'u');
	string = string.replace(/[ýÿ¥]/g, 'y');
	string = string.replace(/[ ]/g, '-');
	string = string.replace(/[-,]/g, '-');
	string = string.replace(/[&]/g, '-');
	string = string.replace(/(\n|\r|' '|[^a-zA-Z0-9-])/g, '');
	string = string.replace(/[-]{2,}/g, '-');
	
	if (string.substring(0, 1) == '-') {
	        string = string.substring(1);
	}
	
	if (string.substring(string.length - 1, string.length) == '-') {
	        string = string.substring(0, string.length - 1);
	}
	
	return string;
}

function baseShowSuggests(page) {
	var params = $('amazon').serialize();
	
	new Ajax.Request(
		'/cms/base/getproducts/',
		{
			onSuccess: function(json) {
				$('products').update(json.responseText);
			},
			parameters: params,
			method: 'post'
		}
	);
}

function baseSubmitProducts(page) {
	var params = $('base_products').serialize();
	
	new Ajax.Request(
		'/cms/base/submitproducts/',
		{
			onSuccess: function(json) {
				$('status').update(json.responseText);
			},
			parameters: params,
			method: 'post'
		}
	);
}
