$(document).ready(function(){
	$('img[@src$=.png]').ifixpng(); 
	$('.left_menu').ifixpng();
	$('.object_list_1').ifixpng();
	$('#search_tabs').ifixpng();
	$('.ibutton').ifixpng();
});

function SelectAll() {
	
	 $(".checkbox").show();
	 $(".map_div").show();
if ($("#selectallch").is(':checked')) {
	$(".checkbox").attr('checked','checked');
}
else {
	 
	 	$(".checkbox").attr('checked','');
	 $(".map_div").hide();
}
}

function SelectAllLayers() {
	
	 //$(".checkbox").show();
	 //$(".map_div").show();
if ($("#selectall_labels").is(':checked')) {
	$(".checkbox").attr('checked','checked');
	$(".ttemp1").hide();
	 $(".ttemp2").show();
}
else {
	 
	 	$(".checkbox").attr('checked','');
	 $(".ttemp1").show();
	 $(".ttemp2").hide();
}
}
function SelectLayer(id) {
	
	 //$(".checkbox").show();
	 //$(".map_div").show();
if ($("#select_layer_"+id).is(':checked')) {
	$(".chlayer_" + id).attr('checked','checked');
	$(".llayer_"+id).hide();
	 $(".llayer2_"+id).show();
}
else {
	 
	 	$(".checkbox").attr('checked','');
	 $(".ttemp1").show();
	 $(".ttemp2").hide();
}
}



function show_submenu(id) {
	if ($("#sub_" + id).is(":hidden")) {
		$(".submenu").hide();
		$("#sub_" + id).show();
	}
	else $("#sub_" + id).hide();
}

function show_submenu2(id) {
	if ($("#sub_" + id).is(":hidden")) {
		$(".submenu2").hide();
		$("#sub_" + id).show();
	}
	else $("#sub_" + id).hide();
}


function over_country(id) {
	
	
	

	if($("#over_country_" + id).length ) {
	
	$("#over_country_" + id).show();


	}
	else {
		
		$("#country_0").after('<div id="over_country_'+id+'"  class="map_div_2"><img src="/img/maps/countries/over_'+id+'.png" width="350" height="430" border="0" usemap="#europe_Map_main"></div>');
		over_country(id);
		
	}
}
function out_country(id) {
	$("#over_country_" + id).hide();
}
function click_country(id) {
	$("#error").text("");
 	if($("#country_" + id).length) {
	
	if ($("#country_" + id).is(":hidden")) {
	$("#country_" + id).show();
	
	$("#ch_country_" + id).attr('checked','checked');
	}
	else {
		$("#country_" + id).hide();
		
		$("#ch_country_" + id).attr("checked","");
	}

	}
	check_start_cities();
}

$(document).ready(function(){
	$("#map_content").load("/inc/maps_ajax/europe.php?cel=sell&temp="+Math.floor(Math.random()*1000000000));
});


function show_map(cel){
	$("#tmp_load").show();
	$("#map_content_2").hide();
	//$("#map_content").hide();
	$("#asell").attr("src", "/img/mm/prodazha2.png");
	$("#arent").attr("src", "/img/mm/arenda2.png");
	$("#aadvs").attr("src", "/img/mm/rassh2.png");
	$("#acode").attr("src", "/img/mm/pokodu2.png");
	if (cel == 'rent') $("#arent").attr("src", "/img/mm/arenda1.png");
	if (cel == 'sell') $("#asell").attr("src", "/img/mm/prodazha1.png");
	$("#map_content").load("/inc/maps_ajax/europe.php?cel="+cel+"&temp="+Math.floor(Math.random()*1000000000), 1, function(){
  		$("#tmp_load").hide();
  		$("#map_content").show();
 });

	$('img[@src$=.png]').ifixpng();
	
}

function show_adv_search() {
	$("#tmp_load").show();
	$("#asell").attr("src", "/img/mm/prodazha2.png");
	$("#arent").attr("src", "/img/mm/arenda2.png");
	$("#aadvs").attr("src", "/img/mm/rassh2.png");
	$("#acode").attr("src", "/img/mm/pokodu2.png");
	$("#aadvs").attr("src", "/img/mm/rassh1.png");
	$("#map_content_2").load("/inc/maps_ajax/advanced.php?temp="+Math.floor(Math.random()*1000000000), 1, function(){
  		$("#tmp_load").hide();
  		$("#search_adv_button").hide();
  		$("#map_content_2").show();
 });
}

function show_code_search() {
	$("#tmp_load").show();
	$("#map_content").hide();
	$("#map_content_2").hide();
	$("#asell").attr("src", "/img/mm/prodazha2.png");
	$("#arent").attr("src", "/img/mm/arenda2.png");
	$("#aadvs").attr("src", "/img/mm/rassh2.png");
	$("#acode").attr("src", "/img/mm/pokodu2.png");
	$("#aadvs").attr("src", "/img/mm/rassh1.png");
	$("#map_content").load("/inc/maps_ajax/code.php?temp="+Math.floor(Math.random()*1000000000), 1, function(){
  		$("#tmp_load").hide();
  		$("#map_content").show();
 });
}

function show_travel() {
	$("#tmp_load").show();
	$("#map_content").hide();
	$("#map_content_2").hide();
	$("#asell").attr("src", "/img/mm/prodazha2.png");
	$("#arent").attr("src", "/img/mm/arenda2.png");
	$("#aadvs").attr("src", "/img/mm/rassh2.png");
	$("#acode").attr("src", "/img/mm/pokodu1.png");
	$("#aadvs").attr("src", "/img/mm/rassh2.png");
	$("#map_content").load("/special/index_main.php", 1, function(){
  		$("#tmp_load").hide();
  		$("#map_content").show();
 });
}

function start_search () {
	$("#tmp_load").show();
	var n = $("input:checked").length;
	if (n > 0) {
  var str = $("#start_search").serialize();
  $.post("/inc/maps_ajax/start_search.php?temp="+Math.floor(Math.random()*1000000000), str, function(data) {
    $("#map_content").html(data);
	$("#tmp_load").hide();
	

	
	
  });
 
  $('img[@src$=.png]').ifixpng();
	}
	else {
		$("#error").text("Выберите хотя бы один пункт!");

	}
}


function over_region(id, cid) {
	if($("#over_country_" + id).length ) {

	$("#over_country_" + id).show();


	}
	else {
		$("#country_0").after('<div id="over_country_'+id+'"  class="map_div_2"><img src="/img/maps/regions/over_'+id+'.png" width="350" height="430" border="0" usemap="#europe_Map_'+cid+'"></div>');
		over_country(id);
		
	}
}
function out_region(id) {
	$("#over_country_" + id).hide();
}

function show_pic(url, w, h) {
	$("#foto_div").html('<img src="/objects_fotos/'+url+'" class="object_foto">');
	
}

function show_regions(cid) {
	$("#s_regions").load("/inc/maps_ajax/advanced_ajax.php?cid="+cid);
}
function show_regions_adm(cid) {
	$("#s_regions").load("/inc/maps_ajax/advanced_ajax_adm.php?cid="+cid);
}
function show_regions_adm_2(cid, div) {
	$("#s_regions_" + div).load("/inc/maps_ajax/advanced_ajax_adm.php?cid="+cid);
}

function remember(id) {
	var str = "id=" + id;
	$("#remember_link").show();
	$.post("/inc/maps_ajax/remember.php", str, function(data) {
    	
  	});
}

function check_form_checkbox() {
	var n = $("input:checked").length;
	if (n > 0) {
	document.getElementById('form2').submit();
	}
	else {
		$("#error").text("Выберите хотя бы один пункт!");

	}
}
function check_form_checkbox2() {
	var n = $("input:checked").length;
	if (n > 0) {
	document.getElementById('start_search').submit();
	}
	else {
		$("#error").text("Выберите хотя бы один пункт!");

	}
}
function check_form_checkbox3() {
	var n = $("input:checked").length;
	if (n > 0) {
	document.getElementById('another_start_search').submit();
	}
	else {
		$("#error").text("Выберите хотя бы один пункт!");

	}
}

function openpict(pict, w, h)
{
window.open(pict,null,"top=100, left=100, width="+w+", height="+h+", toolbar=no, resizable=yes")
} 


function change_city_en()
{
	
} 



function show_cities_adv(id) {
	$("#city_rus").load("/inc/maps_ajax/cities.php?region="+id+"&type=adv_rus&"+Math.floor(Math.random()*1000000000));
	$("#city_eng").load("/inc/maps_ajax/cities.php?region="+id+"&type=adv_eng&"+Math.floor(Math.random()*1000000000));
}

function show_cities_adv_bc(id) {
	$("#city_rus").load("/inc/maps_ajax/cities.php?region="+id+"&type=advbc_rus&"+Math.floor(Math.random()*1000000000));
	$("#city_eng").load("/inc/maps_ajax/cities.php?region="+id+"&type=advbc_eng&"+Math.floor(Math.random()*1000000000));
}

function check_start_cities() {
	$("#load_cities").show();
	var str = $("#start_search").serialize();
	$("#city_rus").load("/inc/maps_ajax/cities.php?"+str+"&type=advch_rus&"+Math.floor(Math.random()*1000000000));
	$("#city_eng").load("/inc/maps_ajax/cities.php?"+str+"&type=advch_eng&"+Math.floor(Math.random()*1000000000), 1, function(){
  		$("#load_cities").hide();
 });
}


function set_end_date2() {
	$("#endDate_field").get(0).selectedIndex = $("#startDate_field").get(0).selectedIndex + 7;
	
}
function set_end_date3() {
	$("#endDate_field3").get(0).selectedIndex = $("#startDate_field3").get(0).selectedIndex + 7;
	
}

function set_date_poles(country) {
	if ($("#cel").val() == 'rent' && country == '300') {
		$("#date_poles").show();
		$("#date_poles2").show();
	}
	else {
		$("#date_poles").hide();
		$("#date_poles2").hide();
	}
}

function set_zve() {
	if ($("#cel").val() == 'rent') {
		$("#zve_pole").show();
	}
	else {
		$("#zve_pole").hide();
	}
}
var is_wid = 0;
function show_date_table(act, servid) {
	//var is_wid = $("#temp_wid").val();
	//is_numeric(is_wid);
	var new_wid = 0;
	if (act == 1 && is_wid > 0) {
		var tmp_wid = is_wid - 11;
		if (tmp_wid > 0)  {
			new_wid = tmp_wid;
			//alert ("-");
		}
		else {
			new_wid = 0;
			//alert (new_wid);
		}
		$("#dates_table_div").load("/br/dates_t_f.php?servid="+servid+"&week="+new_wid+"&"+Math.floor(Math.random()*1000000000));
	}
	if (act == 2) {
		new_wid = is_wid + 11;
		$("#dates_table_div").load("/br/dates_t_f.php?servid="+servid+"&week="+new_wid+"&"+Math.floor(Math.random()*1000000000));
		//alert (new_wid);
	}
	//$("#temp_wid").val(new_wid);
	is_wid = new_wid;
}

function ShowLayers() {
	
	if ($("#search_layers").is(":hidden")) {
		$("#search_regions").hide();
		$("#search_regions_map").hide();
		$("#search_layers").show();
		$("#search_layers_map").show();
		$("#text-val-search").html('<img src="/img/byreg.png" width="203" height="33" border="0">');
		show_points();
	}
	else {
		$("#search_regions").show();
		$("#search_regions_map").show();
		$("#search_layers").hide();
		$("#search_layers_map").hide();
		$("#text-val-search").html('<img src="/img/byraz.png" width="238" height="33" border="0">');
	}
}

function Select_marker(id) {
	if ($("#check_mark_" + id).is(':checked')) {
		$("#check_mark_" + id).attr('checked','');
		$("#lpicmarker_" + id).show();
		$("#lpicactmarker_" + id).hide();
	}
	else {
		$("#check_mark_" + id).attr('checked','checked');
		$("#lpicmarker_" + id).hide();
		$("#lpicactmarker_" + id).show();	
	}
}

function Select_marker2(id) {
	if ($("#check_mark_" + id).is(':checked')) {
		//$("#check_mark_" + id).attr('checked','');
		$("#lpicmarker_" + id).hide();
		$("#lpicactmarker_" + id).show();
	}
	else {
		//$("#check_mark_" + id).attr('checked','checked');
		$("#lpicmarker_" + id).show();
		$("#lpicactmarker_" + id).hide();	
	}
}

function show_layers_list(id) {
	$("#laylist_"+id).toggle();
}










function ReCalculate() 
{
    Fprice          = GetValue($("#price").val());
    Fdownpayment    = GetValue($("#avans").val());
    Frate           = GetValueRate($("#percent").val());
    Fterm           = GetValue($("#srok").val());

	if((Fterm==0)||(Fterm<1))
	 Fterm=10;
	if(Frate<2.5)
	 Frate=2.5;
	 
	 
	 
    iLoanAmount     = Fprice - Fdownpayment;
    iMonPayment     = GetMonthlyPayment1(Fterm, Frate, iLoanAmount);
    
    
    
    iMonPayment     = Math.round(iMonPayment * 100) / 100;
    iPaymentTotal   = ((iMonPayment * 12) * Fterm) + Fdownpayment;
    iPaymentTotal     = Math.round(iPaymentTotal * 100) / 100;
    
    
    
    Fdownpayment    = Math.round(Fdownpayment);
    Fprice          = Math.round(Fprice);
    Frate           = Math.round(Frate);
    iLoanAmount     = Math.round(iLoanAmount);
    iPaymentTotal   = Math.round(iPaymentTotal);
    iMonPayment     = Math.round(iMonPayment);

    //alert(iPaymentTotal);
    
    $("#price").val(Fprice);
    $("#avans").val(Fdownpayment);
    $("#percent").val(Frate);
    $("#kredit").val(iLoanAmount);
    $("#plata").val(iMonPayment);
    $("#aplata").val(iPaymentTotal);
    $("#srok").val(Fterm);
}

function GetValueRate(sValue)
{
    var sRaw = "";
    var sChar = "";
    var bHasDecimal = 0;
    var i = 0;

    sValue += "";
    for (i=0; i <= sValue.length; i++) {

        sChar = sValue.substring(i, i+1);

        if ((sChar >= "0") && (sChar <= "9")){
            sRaw = sRaw + sChar;
        }

        if ((sChar == ",") || (sChar == ".")){
            bHasDecimal = 1;
            sRaw = sRaw + ".";
        }

    }

    if (bHasDecimal) {
        return parseFloat(sRaw);
    }

    if (sRaw.length > 0) {
        return parseInt(sRaw);
    }
    else {
        return 0;
    }
}

function GetValue(sValue)
{
    var sRaw = "";
    var sChar = "";
    var i = 0;

    sValue += "";
    for (i=0; i <= sValue.length; i++) {

        sChar = sValue.substring(i, i+1);

	if ((sChar >= "0") && (sChar <= "9")){
            sRaw = sRaw + sChar;
        }
    }

    if (sRaw.length > 0) {
        return parseInt(sRaw);
    }
    else {
        return 0;
    }
}

function GetMonthlyPayment1(term, rate, loanAmount) 
{

 if(term==0)
  term=1;
    if (rate >= 1) {
        (rate = rate / 100);
    }

	payment=(rate*loanAmount*term+loanAmount)/(12*term);

    return(payment);
}

function FormatOutput(iValue, nDec) 
{
	return iValue;
    var bIsNegative = 0;
    var iPos = 0;
    var sChar = "";
    var sTempNumber = "";
    var sNoChars = "";
    var sDollars = "";
    var sCents = "";
    var sDollarAmount = "";
    var sFormated = "";
    var x = 0;

    if (iValue != "") {

        sTempNumber = iValue + "";
        if (sTempNumber.charAt(0) == "-") {
            bIsNegative = 1;
            sTempNumber = sTempNumber.substring(1, sTempNumber.length);
        }

        sTempNumber
        IndexOfDec = sTempNumber.indexOf(".");

        if (IndexOfDec == -1) {
            sDollars = sTempNumber;
            sCents = "00";
        }
        else if (IndexOfDec == 0) {
            sDollars = "0";
            sCents = sTempNumber.substring(IndexOfDec + 1, sTempNumber.length);
        }
        else {
            sDollars = sTempNumber.substring(0, IndexOfDec);
            if (IndexOfDec == (sTempNumber.length - 1)) {
                sCents = "00";
            }
            else {
                sCents = sTempNumber.substring(IndexOfDec + 1, sTempNumber.length)
                sCents += "0";
                sCents = sCents.charAt(0) + sCents.charAt(1);
            }
        }

        sFormated = sDollars;
        x = sDollars.length;
        iPos = 0;
        while (x > 0) {
            x--;
            sChar = sDollars.charAt(x);
            rounded = Math.round(iPos/3);
            if ( (iPos/3 == rounded ) & (iPos != 0) ) {
                sDollarAmount = "." + sDollarAmount;
            }
            sDollarAmount = sChar +  sDollarAmount;
            iPos++;
        }

        if (nDec) {
          if (bIsNegative) {
              sFormated = "-" + sDollarAmount + "," + sCents + "";
          }
          else {
              sFormated = sDollarAmount + "," + sCents  + "";
          }
	}
        else {
          if (bIsNegative) {
              sFormated = "-" + sDollarAmount + "";
          }
          else {
              sFormated = sDollarAmount + "";
          }
	}

        return (sFormated);
    }
    else {

        return("0" + "");
    }
}
