
    //Text counter
    function textCounter (field, countfield, maxlimit)
        {
            if (field.value.length > maxlimit)
                    field.value = field.value.substring(0, maxlimit);
                else
                        countfield.value = maxlimit - field.value.length;
        }

    //Window open
    function windowOpen (mypage, w, h)
        {
            if(screen.width)
                {
                    var winl = (screen.width-w)/2;
                    var wint = (screen.height-h)/2;
                }
                else
                    {
                        winl = 0;
                        wint =0;
                    }
            if (winl < 0) winl = 0;
            if (wint < 0) wint = 0;
            var settings = 'height=' + h + ',';
            settings += 'width=' + w + ',';
            settings += 'top=' + wint + ',';
            settings += 'left=' + winl + ',';
            settings += ' scrollbars=no ';
            var myname = '';
            win = window.open(mypage,myname,settings);
            win.window.focus();
        }

    //Confirm delete
    function confirmDelete(msg)
        {
            if (!msg)
                {
                    msg="Вы уверены?";
                }
            var agree=confirm(msg);
            if (agree)
                    return true ;
                else
                        return false ;
        }

    //Show user form
    var display_tr = 'table-row';
    if ((navigator.appName == 'Microsoft Internet Explorer') && (parseInt(navigator.appVersion) <= 6 ))
            display_tr = 'block';

    function ShowServices(value_cur, value_show, tr_names)
        {
            var tr_name = new Array();
            var tr_id;
            tr_name = tr_names.split('|');
            for (var i=0; i<tr_name.length; i++)
                {
                    tr_id = document.getElementById(tr_name[i]);
                    if (tr_id)
                    {
                        if (value_cur == value_show)
                                tr_id.style.display = display_tr;
                            else
                                tr_id.style.display = 'none';
                    }
                }
        }

    function ShowRegion(city_name, tr_names)
        {
            var is_city = /^\s*чебоксары\s*$/i;
            if (city_name.search(is_city) != -1)
                    ShowServices(1, 1, tr_names);
                else
                    ShowServices(1, 0, tr_names);
        }

    $(document).ready(function()
        {
            var city = document.getElementById('city');
            if (city)
                    ShowRegion(city.value, 'region_tr');
            var catprop = document.getElementById('catprop');
            if (catprop)
                    ShowServices(catprop.value, 2, 'class_constr_tr|services_tr');
            var typeuser = document.getElementById('typeuser');
            if (typeuser)
                    ShowServices(typeuser.value, 3, 'company_name_tr|street_tr|house_tr|fax_tr|website_tr');
        });

    //Banner
    function Runflash(src, width, height)
        {
            document.write("<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,79,0\" width=\""+width+"\" height=\""+height+"\" title=\"\">");
            document.write("<param name=\"movie\" value=\""+src+"\">");
            document.write("<param name=\"quality\" value=\"high\">");
            document.write("<param name=\"wmode\" value=\"opaque\">");
            document.write("<embed src=\""+src+"\" quality=\"high\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" wmode=\"opaque\" width=\""+width+"\" height=\""+height+"\" wmode=\"transparent\">");
            document.write("</embed></object>");
        }

    //Calculator
    function ShowCalculator ( price )
        {
            document.write ( '<table class="block_calculator">' )
            document.write ( '<tr>' );
            document.write ( '<td>' );
            document.write ( '<span class="title2"><strong>Расчет кредита</strong></span>' );
            document.write ( '<form action="http://tvoyadres.ru/calculator.php" method="GET">');
            document.write ( '<table id="tooltip" class="calculatorsearch" cellspacing="0" cellpadding="0" width="100%">' );
            document.write ( '<tr>' );
            document.write ( '<td>' );
            document.write ( 'Вид платежей');
            document.write ( '<select name="scheme_payments" class="formcalculatorselect" title="Пожалуйста укажите вид платежей">' );
            document.write ( '<option value="1">Аннуитетные</option>' );
            document.write ( '<option value="2">Дифференцированные</option>' );
            document.write ( '</select>' );
            document.write ( 'Стоимость предложения, РУБ');
            document.write ( '<input type="text" size="20" name="sale_price" value="' +price+ '" class="formcalculatorinput" title="Пожалуйста укажите cтоимость предложения в формате 1000000">' );
            document.write ( 'Первоначальный взнос, %' );
            document.write ( '<input type="text" size="5" name="down_percent" value="10" class="formcalculatorinput" title="Пожалуйста укажите первоначальный взнос в формате 10">' );
            document.write ( 'Срок кредитования, лет');
            document.write ( '<input type="text" size="3" name="year_term" value="10" class="formcalculatorinput" title="Пожалуйста укажите срок кредитования в формате 10">' );
            document.write ( 'Процентная ставка, %');
            document.write ( '<input type="text" size="5" name="annual_interest_percent" value="10" class="formcalculatorinput" title="Пожалуйста укажите процентная ставка в формате 10">' );
            document.write ( '<input type="checkbox" name="show_progress" value="1" checked>' );
            document.write ( 'Показывать вычисления' );
            document.write ( '<br /><br />' );
            document.write ( '<input type="submit" name="form_complete" value="Рассчитать" class="calculate_submit">' );
            document.write ( '</td>' );
            document.write ( '</tr>' );
            document.write ( '</table>' );
            document.write ( '</form>' );
            document.write ( '</td>' );
            document.write ( '</tr>' );
            document.write ( '</table>' );
            document.write ( '<br />');
        }

    //Ajax search

    //Delay
    delayform=500;

    //When you restart the search page to check the availability of hash # and replacing it with
    if (window.location.hash)
        {
            window.location='http://tvoyadres.ru/search.php?'+window.location.hash.slice(1);
        }
    function TableRow(link)
        {
            if (typeof checkbox == "undefined")
                {
                    window.location.href = link;
                }
            if (checkbox == 1)
                {
                    window.checkbox=0;
                }
                else
                    {
                        window.location.href = link;
                    }
        }
    //console.log (window.location.href);
        
    //Submain function search
    function AjaxSearch2()
        {
            AjaxSearch();
            selectItem();
        }

    //Main function search
    function AjaxSearch(page,order_by,order_by_type)
        {
            var type_user=$("#type_user").val();
            var type2=$("#type2").val();
            var type=$("#type").val();
            var city=$("#city").val();
            var location=$("#location").val();
            var street=$("#street").val();
            var level=$("#level").val();
            var bedrooms=$("#bedrooms").val();
            if (!page)
                {
                    page='';
                }
            if ($('#image_uploaded:checkbox').is(':checked') == true)
                {
                    var image_uploaded='&image_uploaded=1';
                }
                else
                    {
                        var image_uploaded='';
                    }
            var price_min=$("#price_min").val();
            var price_max=$("#price_max").val();
            var alias=$("#alias").val();
            var favourable=$("#favourable").val();
            var property_search=$("#property_search").val();
            link='search2.php?type_user='+type_user+'&type2='+type2+'&type='+type+'&city='+city+'&location='+location+'&street='+street+'&level='+level+'&bedrooms='+bedrooms+''+image_uploaded+'&price_min='+price_min+'&price_max='+price_max+'&alias='+alias+'&favourable='+favourable+'&property_search='+property_search+'&page='+page;
            link2='type_user='+type_user+'&type2='+type2+'&type='+type+'&city='+city+'&location='+location+'&street='+street+'&level='+level+'&bedrooms='+bedrooms+''+image_uploaded+'&price_min='+price_min+'&price_max='+price_max+'&alias='+alias+'&favourable='+favourable+'&property_search='+property_search+'&page='+page;
            if (order_by)
                {
                    link=link+'&order_by='+order_by+'&order_by_type='+order_by_type;
                    link2=link2+'&order_by='+order_by+'&order_by_type='+order_by_type;
                }
            var sputnikLaunch = new Date();

            //Most of the inhabitants of the earth will be here 28 (Except for residents of non-integer time zones)
            mn = sputnikLaunch.getSeconds()
            link=link+'&tm='+mn;
            if (typeof timeout_id !=="undefined")
                {
                    clearTimeout(timeout_id)
                };

            // line which is responsible for the delay
            timeout_id=setTimeout(function()
                {
                    showContent(link);
                }, delayform);
            window.location.hash = link2;
            return false; alert ('1');
        }

    //Show content search
    function showContent(link)
        {

            // Function to show the contents, loads the file content div result_search
            var cont = document.getElementById('result_search');
            var http = createRequestObject();
            $('#loading').show();
            if ( http )
                {
                    http.open('get', link);
                    http.onreadystatechange = function ()
                        {
                            if ( http.readyState == 4 )
                                {
                                    $('#loading').hide();
                                    $('#result_search').hide();
                                    cont.innerHTML = http.responseText;
                                    $('#result_search').fadeIn('slow', function()
                                        {

                                            // Animation complete
                                        });
                                    $("body,html").animate({"scrollTop":354},($(document).scrollTop())/1.73);
                                }
                        }
                    http.send(null);
                }
                else
                    {
                        document.location = link;
                    }
        }

    //Create ajax object
    function createRequestObject()
        {
            try
                {
                    return new XMLHttpRequest()
                }
            catch (e)
                {
                    try
                        {
                            return new ActiveXObject('Msxml2.XMLHTTP')
                        }
                    catch(e)
                        {
                            try
                                {
                                    return new ActiveXObject('Microsoft.XMLHTTP')
                                }
                            catch (e)
                                {
                                    return null;
                                }
                        }
                }
        }

    //Select all checkbox
    $(document).ready(function()
        {
            var check = 0;
            $("#checkAll").click(function()
                {
                    if( check == 0 )
                        {
                            $("input.check:checkbox").attr("checked", "checked");
                            check = 1;
                        }
                        else
                            {
                                $("input.check:checkbox").removeAttr("checked");
                                check = 0;
                            }
                });
        })
       
    //More menu
    $(document).ready(function()
        {
            $('#more').hover(function() 
                {            
                    if ($(this).find('ul').length == 0) 
                        {
                            //var id = $(this).attr('id');
                            var li = $(this);
                            $.ajax(
                                {
                                    url: 'http://tvoyadres.ru/templates/default/moremenu.php',
                                    beforeSend: function()
                                        {
                                            li.addClass('loading');
                                        },
                                    success: function(data)
                                        {
                                            li.append(data);
                                            li.find('ul').stop(true, true);
                                            li.find('ul').slideDown();
                                            li.removeClass('loading');
                                        }
                                });
                        } 
                        else 
                            {
                                $(this).find('ul').stop(true, true);
                                $(this).find('ul').slideDown();
                            }            
                    $(this).addClass("active");
                },
            function() 
                {            
                    $(this).find('ul').slideUp('fast');            
                    $(this).removeClass("active");
                });
        });
