function createHttpRequest() {
var httpRequest;
var browser = navigator.appName;

if (browser == "Microsoft Internet Explorer") {
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
} else {
httpRequest = new XMLHttpRequest();
}

return httpRequest;
}

function sendRequest(file, _resultId, getRequestProc) {
resultId = _resultId;
document.getElementById(resultId).innerHTML = '<div style="color: #555555; padding: 20px;"><b>Подождите, идет загрузка...</b></font></div>';
httpRequest.open('get', file);
httpRequest.onreadystatechange = getRequestProc;
httpRequest.send(null);
}
function getRequest() {
if (httpRequest.readyState == 4) {
document.getElementById(resultId).innerHTML = httpRequest.responseText;
}
}
var httpRequest = createHttpRequest();
var resultId = '';
			$(function(){
				$('#map_window').dialog({
					autoOpen: false,
                    draggable: true,
					width: 630,
                    height: 520,
                    modal: true,
                    title: '',
                    resizable: false,
                    buttons: {
						"Ok": function() {
							$(this).dialog("close");
						}
					}
				});
        	});
			$(function(){
				$('#dialog').dialog({
					autoOpen: false,
                    draggable: false,
					width: 390,
                    modal: true,
                    title: '',
                    resizable: false,
                    buttons: {
						"Ok": function() {
							$(this).dialog("close");
						}
					}
				});
			});
			$(function(){
				$('#dialog2').dialog({
					autoOpen: false,
                    draggable: false,
					width: 900,
                    height: 600,
                    modal: true,
                    title: '',
                    resizable: false,
                    buttons: {
						"Ok": function() {
							$(this).dialog("close");
						}
					}
				});
			});
$(function() {
		var name = $("#login_name"),
			password = $("#login_password"),
			allFields = $([]).add(name).add(password),
			tips = $("#validateTips");

		function updateTips(t) {
			tips.text(t).effect("highlight",{},1500);
		}

		function checkLength(o,n,min,max) {

			if ( o.val().length > max || o.val().length < min ) {
				o.addClass('ui-state-error');
				updateTips("Длина " + n + " должна быть от "+min+" до "+max+" симв.");
				return false;
			} else {
				return true;
			}

		}

		function checkRegexp(o,regexp,n) {

			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass('ui-state-error');
				updateTips(n);
				return false;
			} else {
				return true;
			}

		}

		$("#login_form").dialog({
			bgiframe: true,
			autoOpen: false,
            autoOpen: false,
            resizable: false,
			height: 330,
			modal: true,
			buttons: {
            	'Отмена': function() {
					$(this).dialog('close');
				},
				'Войти': function() {
					var bValid = true;
					allFields.removeClass('ui-state-error');

					bValid = bValid && checkLength(name,"логина",3,16);
					bValid = bValid && checkLength(password,"пароля",5,16);

					bValid = bValid && checkRegexp(name,/^([0-9a-z_])+$/i,"Только латинские символы и цифры");
					bValid = bValid && checkRegexp(password,/^([0-9a-zA-Z])+$/,"Только латинские символы и цифры");

					if (bValid) {
                        $('#login_form1').submit();
					}
				}
			},
			close: function() {
				allFields.val('').removeClass('ui-state-error');
			}
		});



		$('#click_to_login').click(function() {
			$('#login_form').dialog('open');
            $('#login_name').focus();
            return false;
		})


	});
$(function() {
		$("#reason_form").dialog({
			bgiframe: true,
			autoOpen: false,
            autoOpen: false,
            resizable: false,
			height: 300,
            width: 320,
			modal: true,
			buttons: {
            	'Отмена': function() {
					$(this).dialog('close');
				},
				'Ок': function() {
                        $('#reason_form1').submit();
				}
			},
//			close: function() {				allFields.val('').removeClass('ui-state-error');			}
		});
	});


$('*').ajaxSend(function() {
   $('#loading').show();
});
$('*').ajaxComplete(function() {
   $('#loading').hide();
});
            $('#click_to_map').click(function() {
			    $('#map_window').dialog('open');
                $('#map_window').focus();
	    	})


function dialog_alert(title,text) {
    if (title=='') title='Сообщение';
    $('#dialog').dialog('option', 'title', title);
    document.getElementById('dialog_text').innerHTML=text;
    $('#dialog').dialog('open');
}
function open_map_window(title) {
    $('#map_window').dialog('option', 'title', title);
    $('#map_window').dialog('open');
    $('#map_window').focus();
}
function open_text_window(text_id, sess) {
    $.post('/text.php', {text_id: text_id, sess: sess},
        function (data) {
            $('#dialog_text2').html(data);
            $('#dialog2').dialog('option', 'title', $('#' + text_id).html());
            $('#dialog2').dialog('option', 'width', 900);
            $('#dialog2').dialog('option', 'height', 600);
            $('#dialog2').dialog('open');
        }
)};
function open_ctext_window() {
            $('#reason_form').dialog('option', 'width', 320);
            $('#reason_form').dialog('option', 'height', 550);
            $('#reason_form').dialog('open');
}
function open_text2_window(text_id, sess) {
    $.post('/spec_obj_list.php', {text_id: text_id, sess: sess},
        function (data) {
        if (data=='no_objects') {
                $('#dialog_text2').html('<div align=center class="f3 p12 firebrick">У Вас не объектов для спецразмещения</div>');
                $('#dialog2').dialog('option', 'width', 370);
                $('#dialog2').dialog('option', 'height', 180);
                $('#dialog2').dialog('option', 'title', 'Выберите объект');
            }else{
                $('#dialog_text2').html(data);
                $('#dialog2').dialog('option', 'width', 370);
                $('#dialog2').dialog('option', 'height', 500);
                $('#dialog2').dialog('option', 'title', 'Выберите объект');
            }
            $('#dialog2').dialog('open');
        }
)};


var on_load_data=false;
function change1(i) {
    if (i) {
      document.getElementById('mm1').className='mm2';
      document.getElementById('mm2').className='mm1';
      document.getElementById('otype').value='0';
    }else{
      document.getElementById('mm1').className='mm1';
      document.getElementById('mm2').className='mm2';
      document.getElementById('otype').value='1';
    }
}
function change2(id) {
    if (id=='mmid_5') {
      if (document.getElementById('mmid_5c').className=='mm21') {
        document.getElementById('mmid_1c').className='mm21';
        document.getElementById('mmid_1t').value='0';
        document.getElementById('mmid_2c').className='mm21';
        document.getElementById('mmid_2t').value='0';
        document.getElementById('mmid_3c').className='mm21';
        document.getElementById('mmid_3t').value='0';
        document.getElementById('mmid_4c').className='mm21';
        document.getElementById('mmid_4t').value='0';
      }else{
        document.getElementById('mmid_1c').className='mm22';
        document.getElementById('mmid_1t').value='1';
      }
    }else{
        document.getElementById('mmid_5c').className='mm21';
        document.getElementById('mmid_5t').value='0';
    }
    if (document.getElementById(id+'c').className=='mm22') {
      document.getElementById(id+'c').className='mm21';
      document.getElementById(id+'t').value='0';
    }else{
      document.getElementById(id+'c').className='mm22';
      document.getElementById(id+'t').value='1';
    }
}
function change_region() {
    document.location='/' + $('#m_region123 option:selected').val() + '/';
}
function check_load() {
return true;
    if (!on_load_data) alert('Дождитесь окончания загрузки страницы...');
    return on_load_data;
}
$(document).ready(function () {
    $('#m_region112233').change(function () {
        var region_id = $(this).val();
        if (region_id == '0') {
            $('#rajon_id').html('');
            $('#rajon_id').attr('disabled', true);
            return(false);
        }
        $('#rajon_id').attr('disabled', true);
        $('#rajon_id').html('<option>загрузка...</option>');
        var url = '/get_rajons.php';
        $.get(
            url,
            "region_id=" + region_id,
            function (result) {
                if (result.type == 'error') {
                    alert('error');
                    return(false);
                }
                else {
                    var options = '';
                    $(result.rajons).each(function() {
                        options += '<option value="' + $(this).attr('id') + '">' + $(this).attr('title') + '</option>';
                    });
                    $('#rajon_id').html(options);
                    $('#rajon_id').attr('disabled', false);
                    $("#rajon_id option[value=" + sel_rajon + "]").attr("selected", "selected");

                }
            },
            "json"
        );
    });
});
function ch_tariff() {
    sel = $("#t_time").val();
    objects = $("#t_objects").val();
    t_price = $("#t_price").text();
    c_balance = $("#current_balance").val();
    skidka = 0;
    if (sel>2 && sel<6) skidka = 10;
    if (sel>5 && sel<12) skidka = 20;
    if (sel == 12) skidka = 30;
    new_price = (t_price - ((t_price / 100) * skidka));
    new_price = new_price * objects;
    $("#skidka").text(skidka);
    $("#t_time2").text(sel);
    $("#t_price2").text(new_price);
    $("#t_price3").text(new_price * sel);
    $("#t_econ").text((t_price * sel * objects) - new_price * sel);
    if (c_balance - (new_price * sel)<0) {
        $("#t_nomoney").text(new_price * sel - c_balance);
        $("#t_nomoney_div").show("slow");
        $("#t_accept").hide("slow");
        $("#cont").hide("slow");
    }else{
        $("#t_nomoney_div").hide("slow");
        $("#t_accept").show("slow");
    }
}
function ch_tariff2() {
    objects = $("#t_objects").val();
    t_price = $("#t_price").text();
    c_balance = $("#current_balance").val();
    new_price = t_price * objects;
    $("#t_price2").text(new_price.toFixed(2));
    if (c_balance - new_price < 0) {
        $("#t_nomoney").text((new_price - c_balance).toFixed(2));
        $("#t_nomoney_div").show("slow");
        $("#t_accept").hide("slow");
        $("#cont").hide("slow");
    }else{
        $("#t_nomoney_div").hide("slow");
        $("#t_accept").show("slow");
    }
}
function ch_price_time() {
    sel = $("#t_time").val();
    t_price = $("#t_price").text();
    c_balance = $("#current_balance").val();
    $("#t_time2").text(sel);
    $("#t_price3").text(t_price * sel);
    if (c_balance - (t_price * sel)<0) {
        $("#t_nomoney").text(t_price * sel - c_balance);
        $("#t_nomoney_div").show("slow");
        $("#t_accept").hide("slow");
        $("#cont").hide("slow");
    }else{
        $("#t_nomoney_div").hide("slow");
        $("#t_accept").show("slow");
    }
}
function show_all_variants() {
    $("#mmid_5t").val(1);
    $("#price_from").val(600);
    $("#price_to").val(3800);
    $('#s_form').submit();
}

function recalc_price() {
    var summ = 0;
    for (i=1;i<11;i++) {
      summ += $('#spec_time_right_'+i).val() * spec_prices_right[i];
      summ += $('#spec_time_left_'+i).val() * spec_prices_left[i];
    }
    $("#t_price").text(summ);
    c_balance = $("#current_balance").val();
    if ((c_balance - summ)<0) {
        $("#t_nomoney").text(summ - c_balance);
        $("#t_nomoney_div").show("slow");
        $("#t_accept").hide("slow");
        $("#cont").hide("slow");
    }else{
        $("#t_nomoney_div").hide("slow");
        $("#t_accept").show("slow");
    }

}
function bay_spec() {
    if ($("#t_price").text()<1) {
        dialog_alert('Ошибка','Вы ничего не выбрали');
    }else{
        $("#specbay").submit()
    }
}
function change_bg(id,i) {
    if (i) {
      document.getElementById(id).className='objl2';
    }else{
      document.getElementById(id).className='objl';
    }

}
function change_bgc(id,i) {
    if (i) {
      document.getElementById(id).className='objl2_2';
    }else{
      document.getElementById(id).className='objl_2';
    }

}
function set_spec(idobj,idspec) {
    $.post('/spec_set_obj.php', {obj_id:idobj, spec_id: idspec},
        function (data) {
            data=data.split(';');
            $('#dialog2').dialog('close');
            if (data[0]=='no_objects' || data[0]=='0') {
                $('#dialog_text2').html('<div align=center class="f3 p12 firebrick">Извините, произошла ошибка</div>');
                $('#dialog2').dialog('option', 'width', 370);
                $('#dialog2').dialog('option', 'height', 180);
                $('#dialog2').dialog('option', 'title', 'Спецразмещение');
                $('#dialog2').dialog('open');
                return;
            }
            if (data[0]=='objects_use') {
                $('#dialog_text2').html('<div align=center class="f3 p12 firebrick">Этот объект уже размещён</div>');
                $('#dialog2').dialog('option', 'width', 370);
                $('#dialog2').dialog('option', 'height', 180);
                $('#dialog2').dialog('option', 'title', 'Спецразмещение');
                $('#dialog2').dialog('open');
                return;
            }
            if (data[0]=='1') {
                $('#dialog_text2').html('<div align=center class="f3 p12 firebrick">Объект выбран</div>');
                $('#dialog2').dialog('option', 'width', 370);
                $('#dialog2').dialog('option', 'height', 180);
                $('#dialog2').dialog('option', 'title', 'Спецразмещение');
            }else{
                $('#dialog_text2').html('<div align=center class="f3 p12 firebrick">Извините, произошла ошибка</div>');
                $('#dialog2').dialog('option', 'width', 370);
                $('#dialog2').dialog('option', 'height', 180);
                $('#dialog2').dialog('option', 'title', 'Спецразмещение');
                $('#dialog2').dialog('open');
                return;
            }
            idspec2=idspec.split('_');
            $('#link_'+idspec).html('Объект <a href="/'+user_city+'/'+data[1]+'/"><u>#'+data[1]+'</u></a><span id=del_link_'+idspec2[0]+'_'+idspec2[1]+'_'+idspec2[2]+' onclick="del_spec(this.id);" class="pointer del_spec_img"><img src="/img/icons/cancel.png" alt="Удалить размещение" title="Удалить размещение" /></span>');
            $('#dialog2').dialog('open');
        }
)};
function del_spec(idspec) {
    $.post('/spec_del_obj.php', {spec_id: idspec},
        function (data) {
            alert(data);
            if (!data) {
                $('#dialog_text2').html('<div align=center class="f3 p12 firebrick">Извините, произошла ошибка</div>');
                $('#dialog2').dialog('option', 'width', 370);
                $('#dialog2').dialog('option', 'height', 180);
                $('#dialog2').dialog('option', 'title', 'Спецразмещение');
                $('#dialog2').dialog('open');
                return;
            }else{
                idspec=idspec.split('_');
                $('#link_'+idspec[2]+'_'+idspec[3]+'_'+idspec[4]).html('');
            }
        }
)};