function change_background(newColor)
{
	document.body.parentNode.style.background = newColor;
	document.body.style.background = newColor;
}

/* trim string */
function trim(str)
{
	return str.replace(/(^\s+)|(\s+$)/g, "");
}

function is_email(mail)
{
	var pattern = /^([a-zA-Z0-9_]|\-)+@([a-zA-Z0-9_]|\-)+\.[a-zA-Z]+$/;
    return pattern.test(trim(mail));
}

/* change pic src */
function change_pic(img, img_url)
{
	img.src = img_url;
}

/* switch on voters star */
function on_star(art, id)
{
	for (var i = min_vote; i <= id; i++)
		document.getElementById('star_'+art+'_'+i).src = star_on.src;
}

/* switch off voters star */
function off_star(art)
{
	for (var i = min_vote; i <= max_vote; i++)
		document.getElementById('star_'+art+'_'+i).src = star_off.src;
}

/* reference to member name  */
function insert_member_name(member_name)
{
	if (document.COMMENTFORM.message)
	{
		var input = document.COMMENTFORM.message;
		input.value = input.value + "[b]" + member_name + "[/b]," + " \n";
	}
}

/* show hide div */
function show_hide(item, id)
{
    var item_id = (id > 0) ? item + "_" + id : item;
    var f = document.getElementById(item_id);
    f.style.display = f.style.display == "none" ? "block" : "none";
}

function check_complaint(comment_id)
{
    var reason = document.getElementById('reason_' + comment_id);
	if (trim(reason.value) == "") {
		alert(FORM_COMMENT_ENTER_REASON);
		return;
	}

    var item_id = document.COMPLAINT.item_id.value;
    document.COMPLAINT.comment_id.value = comment_id;

    $.ajax({
        url: "index.php",
        type: "POST",
        data: {act: "complaint", code: "do_add_ajax",
            item_id: item_id, reason: reason.value, comment_id: comment_id},
        success: function(){
            show_hide('complaint_reason', comment_id);
            show_hide('complaint_success', comment_id);
            reason.value = "";
        }
       });
}

function check_subscribe(command_name)
{
	document.getElementById('incorrect_email').style.display = 'none';
	document.getElementById('subscribe_success').style.display = 'none';
	document.getElementById('subscribe_already').style.display = 'none';
	document.getElementById('subscribe_not_fount').style.display = 'none';
	document.getElementById('send_unsubscribe_url').style.display = 'none';
	
    var email = document.getElementById('subscribe_email');
    var svc = document.getElementById('svc');
	if (!is_email(email.value)) {
		alert("Неверно введён адрес e-mail");
		return false;
	}
	
	document.getElementById('subscribe_progress').style.display = 'block';
    $.ajax({
        url: "index.php",
        type: "POST",
        data: {act: "subscribe", code: "do_subscribe_ajax",
            email: email.value, command: command_name, svc: svc.value},
        success: function(data){
            document.getElementById('subscribe_progress').style.display = 'none';
            document.getElementById(data).style.display = 'block';
            if (data != 'incorrect_email') {
            	email.value = "";
            }
        }
       });
}

function check_username()
{
	var username = document.REGISTERFORM.name.value;
	
	document.getElementById('username_free').style.display = 'none';
	document.getElementById('username_busy').style.display = 'none';
	document.getElementById('username_empty').style.display = 'none';
	document.getElementById('username_progress').style.display = 'block';
	
    $.ajax({
        url: "index.php",
        type: "POST",
        data: {act: "register", code: "do_check_name_ajax", name: username},
        success: function(data){
            document.getElementById('username_progress').style.display = 'none';
            document.getElementById(data).style.display = 'block';
        }
       });
}

function do_vote(vote, item_id)
{
    $.ajax({
        url: "index.php",
        type: "POST",
        dataType: "html",
        data: {act: "vote", code: "do_vote_ajax",
            item_id: item_id, vote: vote},
        success: function(data){
            document.getElementById('vote_stars_'+item_id).style.display = "none";
            if (data == 0)
            {
                document.getElementById('already_vote_'+item_id).style.display = "";
            }
            else
            {
                document.getElementById('rating_'+item_id).innerHTML = data;
                document.getElementById('vote_success_'+item_id).style.display = "";
            }
        }
       });
}

function click_banner(banner_id)
{
    $.ajax({
        url: "index.php",
        type: "POST",
        data: {act: "banner", code: "do_click_ajax",
    		   id: banner_id, no_rotate: 1}
    });
}

function show_comment_complaint(comment_id)
{
    document.getElementById('comment_link_'+comment_id).style.display = "none";
    document.getElementById('complaint_success_'+comment_id).style.display = "none";
    show_hide('complaint_reason', comment_id);
}
function show_comment_link(comment_id)
{
    document.getElementById('complaint_reason_'+comment_id).style.display = "none";
    document.getElementById('complaint_success_'+comment_id).style.display = "none";
    show_hide('comment_link', comment_id);
}
