jQuery(document).ready(function () {
    
    $('#tel').mask('89999999999');
    $('.tel').mask('8(999) 999-99-99');
    
    var Digital=new Date();
    var hours=Digital.getHours();
    if ((hours>9) & (hours<21)) {
        counter();    
    }
    
});

function validate(form) {
    result = true;
    notEntered = 'Пожалуйста введите';
    $(form).find('.required').each(function() {
        if($(this).val() == '') {
            if (!result) {div = ',';} else {div = '';}
            result = false;
            if ($(this).attr('name') == 'fio') {
                notEntered = notEntered + div + ' Ваше фио';
            } else if ($(this).attr('name') == 'tel') {
                notEntered = notEntered + div + ' контактный телефон';
            } else if ($(this).attr('name') == 'email') {
                notEntered = notEntered + div + ' e-mail';
            } else if ($(this).attr('name') == 'comment') {
                notEntered = notEntered + div + ' отзыв';
            } else if ($(this).attr('name') == 'sms') {
                notEntered = notEntered + div + ' вопрос';
            }
        }   
    });
    if(!result) {
        alert(notEntered + '.');
    }
    return result;
}

function question() {
	$.ajax ({
        type: 'POST',
        url: '/ajaxhandler/questions.php',
        data: 'fio=' + $('#fio').val() + '&tel=' + $('#tel').val() + '&sms=' + $('#sms').val() + '&town=' + $('#town').val(),
        beforeSend: function() {
            $('#sms-form').find('input:submit').attr('disabled','disabled');       
        },
        success: function(data) {
            alert(data);
            if (data == 'Ваш вопрос успешно отправлен!') {
                $('#sms-form').parent('div').html($('#sms-form').parent('div').html() + data);
                $('#sms-form').remove();
            }
            yaCounter2226040.reachGoal('online-question');
        },
        complete: function() {
            $('#sms-form').find('input:submit').removeAttr('disabled');  
        }        
    });
}

function review() {
	$.ajax ({
        type: 'POST',
        url: '/ajaxhandler/reviews.php',
        data: 'fio=' + $('#commentForm').find('.fio').val() + '&email=' + $('#commentForm').find('.email').val() + '&comment=' + $('#commentForm').find('.comment').val(),
        beforeSend: function() {
            $('#commentForm').find('input:submit').attr('disabled','disabled');       
        },
        success: function(data) {
            alert(data);
            if (data == 'Ваш отзыв успешно отправлен!') {
                $('#commentForm').parent('div').html($('#commentForm').parent('div').html() + data);
                $('#commentForm').remove();
            }
        },
        complete: function() {
            $('#commentForm').find('input:submit').removeAttr('disabled');  
        }        
    });
}

function reception() {
	$.ajax ({
        type: 'POST',
        url: '/ajaxhandler/reception.php',
        data: 'fio=' + $('#receptionForm').find('.fio').val() + '&tel=' + $('#receptionForm').find('.tel').val() + '&comment=' + $('#receptionForm').find('.comment').val() + '&date=' + $('#receptionForm').find('.date').val(),
        beforeSend: function() {
            $('#receptionForm').find('input:submit').attr('disabled','disabled');
        },
        success: function(data) {
            alert(data);
            if (data == 'Ваша заявка успешно добавлена!') {
                $('#receptionForm').parent('div').html($('#receptionForm').parent('div').html() + data);
                $('#receptionForm').remove();
            }
        },
        complete: function() {
            $('#receptionForm').find('input:submit').removeAttr('disabled');
        }
    });
}

function counter()
{
    var tooth = $('#counter a span').text();
    tooth = tooth.replace(' ', '');
    tooth = parseInt(tooth)+1;
    tooth = tooth.toString();
    str = tooth.substr(0, tooth.length-3)+' '+tooth.substr(2, 3);
    $('#counter a span').text(str);
    $('.content span#toothCounter').text(str);
    var time = Math.floor(Math.random() * (10000 - 5000 + 1)) + 750;
    t=setTimeout('counter()',time);
}

function disable() {
    
}

