function deleteBlock(id) {
 	if (confirm("Вы действительно хотите удалить этот блок?"))
		open_url("?act=block&code=do_del&id=" + id);
}

function setRotator(num) {
	if (num < 0 || num >= rotators.length)
		num = 0;
	current_rotator = num;

	var r2 = document.getElementById('rotatorBlock');
	r2.style.background = "url('uploads/" + rotators[num][2] + "') no-repeat";
	if ((rotators[current_rotator][3] != "")&&(rotators[current_rotator][3] != "http://"))
		r2.style.cursor = "pointer";
	else
		r2.style.cursor = "default";
}

function openUrlRotator() {
	var url = rotators[current_rotator][3];
	if ((url != "")&&(url != "http://"))
		open_url(url);
}

function open_url(url) {
	var fakeLink = document.createElement('a');
	if (typeof(fakeLink.click) == 'undefined') {
		location.href = url;
	} else {
		fakeLink.href = url;
		document.body.appendChild(fakeLink);
		fakeLink.click();
	}
	return true;
}

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));
}

function is_pass(pass)
{
	var pattern = /^([a-zA-Z0-9])+$/;
    return pattern.test(trim(pass));
}

/* 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)
{
	var txt = document.getElementById('message_comment');
	if (txt) txt.value = txt.value + "[b]" + member_name + "[/b]," + " \n";
}
function insert_quote(member_id)
{
	var txt = document.getElementById('message_comment');
	if (txt) txt.value = txt.value+"[quote="+article_comments[member_id][0]+"]"+article_comments[member_id][1]+"[/quote]"+"\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_complaint2(item_id)
{
    var reason = document.getElementById('reason_' + item_id);
	if (trim(reason.value) == "") {
		alert('Введите, пожалуйста, причину жалобы.');
		return;
	}

    var item_id = document.COMPLAINT2.item_id.value;

    $.ajax({
        url: "index.php",
        type: "POST",
        data: {act: "complaint", code: "do_add2_ajax", item_id: item_id, reason: reason.value},
        success: function(){
            show_hide('complaint_' + item_id, 0);
            show_hide('complaint_success_' + item_id, 0);
            reason.value = "";
        }
	});
}

function do_rate(item_id)
{
    $.ajax({
        url: "index.php",
        type: "POST",
        dataType: "html",
        data: {act: "login", code: "do_rate_ajax", item_id: item_id},
        success: function(data){
            if (data == 0)
            {
            	document.getElementById('rates_'+item_id).style.display = "none";
                document.getElementById('already_rate_'+item_id).style.display = "";
            }
            else
            {
                document.getElementById('rating_'+item_id).innerHTML = '+' + data;
                document.getElementById('rates_'+item_id).style.display = "none";
            }
        }
       });
}

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_complaint(item_id)
{
    document.getElementById('complaint_success_'+item_id).style.display = "none";
    show_hide('complaint_'+item_id, 0);
}
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);
}
