function fill_districts_search() {
	var city = document.SEARCHFORM.city_id.value;
	var sel = document.SEARCHFORM.district_id;
	while (sel.options.length)
		sel.options[0] = null;
	var newOpt = new Option("»»» âûáåðèòå ðàéîí «««", 0);
	sel.options.add(newOpt);
	for (d in search_districts) {
		if (city == search_districts[d][1]) {
			newOpt = new Option(search_districts[d][0], d);
			sel.options.add(newOpt);
		}
	}
	fill_streets_search();
}

function fill_streets_search() {
	var city = document.SEARCHFORM.city_id.value;
	var district = document.SEARCHFORM.district_id.value;
	sel = document.SEARCHFORM.street_id;
	while (sel.options.length)
		sel.options[0] = null;
	newOpt = new Option("»»» âûáåðèòå óëèöó «««", 0);
	sel.options.add(newOpt);
	for (s in search_streets) {
		if ( city == search_streets[s][2] ) {
			newOpt = new Option(search_streets[s][0], s);
			sel.options.add(newOpt);
		}
	}
}

function change_district_search() {
	var sel = document.SEARCHFORM.street_id;
	if (sel.value > 0) {
		district_id = search_streets[sel.value][1];
		document.SEARCHFORM.district_id.value = district_id;
	}
}

function fill_districts() {
	var city = document.EDIT_FORM.city_id.value;
	var sel = document.EDIT_FORM.district_id;
	while (sel.options.length)
		sel.options[0] = null;
	var newOpt = new Option("»»» âûáåðèòå ðàéîí «««", 0);
	sel.options.add(newOpt);
	for (d in search_districts) {
		if (city == search_districts[d][1]) {
			newOpt = new Option(search_districts[d][0], d);
			sel.options.add(newOpt);
		}
	}
	fill_streets();
}

function fill_streets() {
	var city = document.EDIT_FORM.city_id.value;
	var district = document.EDIT_FORM.district_id.value;
	sel = document.EDIT_FORM.street_id;
	while (sel.options.length)
		sel.options[0] = null;
	newOpt = new Option("»»» âûáåðèòå óëèöó «««", 0);
	sel.options.add(newOpt);
	for (s in search_streets) {
		if ( city == search_streets[s][2] ) {
			newOpt = new Option(search_streets[s][0], s);
			sel.options.add(newOpt);
		}
	}
}

function change_district() {
	var sel = document.EDIT_FORM.street_id;
	if (sel.value > 0) {
		district_id = search_streets[sel.value][1];
		document.EDIT_FORM.district_id.value = district_id;
	}
}

function change_back(obj, color)
{
	obj.style.background = color;
}

function openOrder(url, id)
{
	window.location.href = url + "order_" + id + ".html";
}

function openFirm(url, id)
{
	window.location.href = url + "firm_" + id + ".html";
}

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)
{
	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_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('bad_symbols').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(trim(data)).style.display = 'block';
        }
       });
}

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

