$(document).ready(function() {
    
    /** Активные счётчики **/
    function updateCounts(){
        random = getRandom(6000, 8000);
        $($("#stats-menu li").get(0)).children("strong").html(random);
        
        $($("#stats-menu li").get(1)).children("strong").html(getRandom(100000, 150000));
        $($("#stats-menu li").get(2)).children("strong").html(getRandom(100000, 150000));
        $($("#stats-menu li").get(3)).children("strong").html(getRandom(100000, 150000));
        $($("#stats-menu li").get(4)).children("strong").html(getRandom(100000, 150000));
        $($("#stats-menu li").get(5)).children("strong").html(getRandom(100000, 150000));
        
        timeout = getRandom(2000, 3000);
    	setTimeout(updateCounts, timeout);
    }
    
    setTimeout(updateCounts, 5000);

    
    
    $("#search-field").click(function(){
        if ($(this).val() == "Что искать?"){
            $(this).val('');
        }
    })
    
    
    
    $("#country_select").change(function(){
        var id = $(this).val();
        set_infos(id);
    })
    
    /** Устанавливает нужные телефоны **/
    function set_infos(id)
    {        
        $("#smsCode").html(billngSettings[id]['smsCode']);
        $("#smsPhone").html(billngSettings[id]['smsPhone']);
        $("#smsPhoneAdv").html(billngSettings[id]['smsPhoneAdv']);
        $("#steps").html(billngSettings[id]['steps']);
        
        if (billngSettings[id]['countryCode'] == "KZ")
        {
            $("#KZ").show();
        }
        else
        {
            $("#KZ").hide();
        }
    }
    
    /** Проверка телефона **/
    $("#phone_form").submit(function(){
        phone = $("input[name=phone]").val();
        $.ajax({
            url: ''+ROOT_URL+'for_scripts.php',
            type: 'POST',
            data: 'type=check_phone&phone='+phone,
            dataType: 'JSON',
            timeout: 50000,
            beforeSend: function(){
                $('.loader').show();
            },
            complete: function(){
                $('.loader').hide();
            },
            success: function(response){
                var data = eval('('+ response +')');
                if (data)
                {
                    //alert(response);
                    if (data.success)
                    {
                        if (data.payType == 1)
                        {
                            window.location = "./?type=join&countries=all";
                        }
                        else
                        {
                            if (data.payType == 2)
                            {
                                subtype="p";
                            }
                            else if (data.payType == 3)
                            {
                                subtype="s";
                            }
                            //if (data.operator == "beeline" || data.operator == "mts") subtype="s";
                            //else subtype="p";
                            window.location = "./?type=join&subtype="+subtype+"&phone="+data.phone+"&operator="+data.operator
                        }
                    }
                    else
                    {
                        alert(data.text)
                    }
                }
                return false;
            },
            error: function(){
                alert('Извините, на сервере произошла ошибка.');
                return false;
            }
        });  
        return false  
    })
    
    /** Отправка смс второй раз **/
    $("#double_send").click(function(){
        phone = $("#entered_phone").html();
        $.ajax({
            url: ''+ROOT_URL+'for_scripts.php',
            type: 'POST',
            data: 'type=double_send&phone='+phone,
            dataType: 'JSON',
            timeout: 50000,
            beforeSend: function(){
                $('.loader').show();
            },
            complete: function(){
                $('.loader').hide();
            },
            success: function(response){
                var data = eval('('+ response +')');
                if (data)
                {
                    //alert(response);
                    if (data.success)
                    {
                        $(".success_double_send").html("SMS успешно отправлено<br />");
                    }
                    else
                    {
                        alert(data.text)
                    }
                }
                return false;
            },
            error: function(){
                alert('Извините, на сервере произошла ошибка.');
                return false;
            }
        });  
        return false  
    })
    
    /** Проверка кода **/
    $("#subscribe_form, #sms_form").submit(function(){
        phone = $("input[name=phone]").val();
        code = $("input[name=code]").val();
        $.ajax({
            url: ''+ROOT_URL+'for_scripts.php',
            type: 'POST',
            data: 'type=check_code&phone='+phone+'&code='+code,
            dataType: 'JSON',
            timeout: 5000,
            beforeSend: function(){
                $('.loader').show();
            },
            complete: function(){
                $('.loader').hide();
            },
            success: function(response){
                var data = eval('('+ response +')');
                if (data)
                {
                    //alert(response);
                    if (data.success)
                    {
                        window.location = "./?q="+data.search_text;
                    }
                    else
                    {
                        alert(data.text)
                    }
                }
                return false;
            },
            error: function(){
                alert('Извините, на сервере произошла ошибка.');
                return false;
            }
        });  
        return false  
    })
    
    
    
})

function getRandom(min, max)
{
  return Math.floor(Math.random() * (max - min) + min);
}
