
function ajax(action, data){
	var request = new Request({
		method : 'post',
		url : '/ajax_control.php',
		async : false,
		onSuccess : function(responseTree){
			result = responseTree;
		}
	});
	request.send('action='+action+(data?'&'+data:''));
	return(result);
}

function purify(data){
	data = data.replace(/&/g, 'amp;');
	data = data.replace(/%/g, 'perc;');
	return data;
}
function is_numeric(value){
	if ((isNaN(value)) || (value.length == 0))
		return 0;
	else
		return 1;
}
function imposeMaxLength(Object, MaxLen){
  return (Object.value.length <= MaxLen);
}

function default_check(str, def){
	if( str == def )
		return '';
	else
		return str;
}

function blank_check(str, def){
	if( str == '' )
		return def;
	else
		return str;
}

function head_panel(){
	var disable = false;
	items = $$('.panel_item');
	$each(items, function(i){
		i.addEvent('mouseenter', function(){
			if( this.className == 'panel_item active' )
				return false;
			//if( disable == true )
			//	return false;
			disable = true;
			$each(items, function(x){
				x.className = 'panel_item';
			});
			this.className = 'panel_item active';
			var id = this.id;
			$('panel').store('id', id);
			$('panel_container').className = 'loading';
			var fader = new Fx.Tween($('panel'));
			fader.start('opacity', 0).chain(function(){
				var request = new Request({
					method : 'post',
					url : '/ajax_control.php',
					async : true,
					onSuccess : function(responseTree){
						var current_id = $('panel').retrieve('id');
						if( current_id == id ){
							result = responseTree;
							$('panel').innerHTML = result;
							disable = false;
							fader.start('opacity', 1).chain(function(){
								$('panel_container').className = '';
							});
						}
					}
				});
				request.send('action=load_panel&id=' + id);
			});
		});
	});
}

function switchBox(num){
	for(x=1; x<=3; x++){
		$('switch_' + x).style.display = 'none';
		$('switch_toggle_' + x).className = '';
	}
	$('switch_' + num).style.display = 'block';
	$('switch_toggle_' + num).className = 'active';
}

function switchFeature(num){
	for(x=1; x<=4; x++){
		$('home_featured_' + x).style.display = 'none';
		$('featured_sub_' + x).className = 'featured_sub';
	}
	$('home_featured_' + num).style.display = 'block';
	$('featured_sub_' + num).className = 'featured_sub active';
}

function rateComment(contentID, commentID, rating){
	var response = ajax('rate_comment', 'contentID=' + contentID + '&commentID=' + commentID + '&rating=' + rating);
	if( !response )
		alert('An error has occurred');
	else{
		if( is_numeric(response) ){
			var comment = $('CommentRating_' + commentID);
			if( comment ){
				comment.innerHTML = response;
				if( response < 0 )
					comment.style.color = '#990000';
				else if( response > 0 )
					comment.style.color = '#08b73d';
				else
					comment.style.color = '#333333';
				if( response <= -5 ){
					var comment = $('comment_' + commentID);
					if( comment )
						comment.innerHTML = '<div class="comment_spam">Comment marked as spam</div>';
				}
			}
		}
		else
			alert(response);
	}
	return false;
}
