function setRotator(num) {
	if (num < 0 || num >= rotators.length)
		num = 0;
	var r = document.getElementById('rotatorContent');
	r.innerHTML = rotators[num][1];
	r.className = rotators[num][3]+'RotatorBlock';
	if (rotators[num][3]=='left')
		r.style.marginLeft = rotators[num][5] + 'px';
	else
		r.style.marginRight = rotators[num][7] + 'px';
	r.style.marginTop = rotators[num][6] + 'px';
	
	var r2 = document.getElementById('rotatorBlock');
	r2.style.background = "url('uploads/" + rotators[num][2] + "') no-repeat";
	if ((rotators[current_rotator][4] != "")&&(rotators[current_rotator][4] != "http://"))
		r2.style.cursor = "pointer";
	else
		r2.style.cursor = "default";
}

function setCurrentRotator(step) {
	current_rotator += step;
	if (current_rotator < 0)
		current_rotator = rotators.length - 1;
	if (current_rotator >= rotators.length)
		current_rotator = 0;
	setRotator(current_rotator);
}

function openUrlRotator() {
	var url = rotators[current_rotator][4];
	if ((url != "")&&(url != "http://"))
		open_url(url);
}

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-z0-9_\.-]{1,20}@(([a-z0-9-]+\.)+(com|net|org|mil|edu|gov|arpa|info|biz|inc|name|[a-z]{2,4})|[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})$/; 
    return pattern.test(trim(mail));
}

function is_name(name) {
	var pattern = /^([а-яА-Яa-zA-Z0-9])+$/;
	return pattern.test(trim(name));
}

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";
	document.location.replace('#comments_form');
}

/* 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 send_answer(comment_id, form)
{
	form.action = 'index.php?act=consultation&code=do_answer';
	form.comment_id.value = comment_id;
	form.submit();
}

function fill_members_list(field) {
	if (trim(field.value)!="")
	{
		$.ajax({
			url: "index.php",
			type: "POST",
			dataType: "html",
			data: {act: "login", code: "do_get_members_ajax", member_name: field.value},
			success: function(resData) {
				document.getElementById('members_list').style.display = "block";
				document.getElementById('members_list').innerHTML = resData;
			}
		});
	}
}

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_subscribe');
	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_email_reg() {
	var email = document.REGISTERFORM.email.value;
	result = document.getElementById('email_result');
	result.innerHTML = '<img src="' + progress_bar.src + '" />';
	result.style.display = 'block';
	$.ajax({
		url: "index.php",
		type: "POST",
		data: {act: "register", code: "do_check_email_ajax", email: email},
		success: function(data){
			if (data == "email_empty") {
				result.style.color = '#FF0000';
				result.innerHTML = '<b>Введите e-mail.</b>';
			}
			if (data == "email_free") {
				result.style.color = '#009900';
				result.innerHTML = '<b>Введённый e-mail ранее не был зарегистрирован.<br>Вы можете его использовать.</b>';
			}
			if (data == "email_busy") {
				result.style.color = '#FF0000';
				result.innerHTML = '<b>E-mail уже зарегистрирован другим пользователем.</b>';
			}
			if (data == "email_incorrect") {
				result.style.color = '#FF0000';
				result.innerHTML = '<b>Некорректный e-mail.</b>';
			}
		}
	});
}

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_rate_consult(item_id, value)
{
	document.getElementById('rates_'+item_id+'_1').style.display = "none";
	document.getElementById('rates_'+item_id+'_2').style.display = "none";
	$.ajax({
		url: "index.php",
		type: "POST",
		dataType: "html",
		data: {act: "login", code: "do_rate_consult_ajax", item_id: item_id, value: value},
		success: function(data){
			if (data == 'error')
			{
				document.getElementById('already_rate_'+item_id).style.display = "";
			}
			else
			{
				var rating = document.getElementById('rating_'+item_id);
				if (data == 0) {
					rating.innerHTML = data;
					rating.style.color = '#3F3F3F';
				} else if (data < 0) {
					rating.innerHTML = data;
					rating.style.color = '#FF0000';
				} else {
					rating.innerHTML = '+'+data;
					rating.style.color = '#008000';
				}
            }
        }
       });
}

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 do_vote_tender(tender_id,post_id,voice)
{
	var stars=document.getElementById('vote_stars_'+post_id);
	stars.innerHTML='Рейтинг: <img src="templates/fistiblack/images/progress.gif" />';
	$.ajax({
		url: "index.php",
		type: "POST",
		dataType: "html",
		data: {act: "vote", code: "do_vote_tender_ajax",
				tender_id: tender_id, post_id: post_id, vote: voice},
		success: function(data) {
			if (data==0) {
				stars.innerHTML='Вы уже голосовали за этого участника';
			} else {
				stars.innerHTML='Рейтинг: '+data;
			}
		}
	});
}

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);
}
function show_comment_answer(comment_id)
{
    document.getElementById('comment_link_'+comment_id).style.display = "none";
    document.getElementById('complaint_success_'+comment_id).style.display = "none";
    document.getElementById('complaint_reason_'+comment_id).style.display = "none";
    show_hide('comment_answer', comment_id);
}
function get_selected_radio(buttonGroup) {
   if (buttonGroup[0]) {
      for (var i = 0; i < buttonGroup.length; i++) {
         if (buttonGroup[i].checked)
            return i;
      }
   } else {
      if (buttonGroup.checked)
    	  return 0;
   }
   return -1;
}

function addQuestion(formName) {
	var form=document.getElementById(formName);
	form.svc_consult.value=svcTrc;
	form.submit();
}

function set_symbols_count(field,field_name,maxlength) {
	var txt = document.getElementById('count_'+field_name);
	if (field.value.length <= 0 || field.value.length > maxlength)
		txt.style.color='#FF0000';
	else
		txt.style.color='#008000';
	txt.innerHTML=field.value.length;
}

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 deleteBrand(id) {
	if (confirm("Вы действительно хотите удалить этот бренд из списка любимых брендов?"))
		open_url(script_url+"act=login&code=do_del_brand&id=" + id);
}

function deleteCosmetic(id) {
	if (confirm("Вы действительно хотите удалить этот товар из косметички?"))
		open_url(script_url+"act=login&code=do_del_cosmetic&id=" + id);
}


