$(document).ready(function()
{
    $('#submitForm').bind('click', function()
    {
        $('#lblScheduleACall').text('');
        $('#submitForm').attr('disabled', 'disabled');
        if(Validate())
        {
            $.ajax(
            {
                url: '/legal/ajaxScheduleACall.php',
                cache: false,
                type: 'POST',
                data: $('#frmScheduleACall').serialize(),
                success: function(msg)
                {
                    $('#submitForm').removeAttr('disabled');
                    $('#lblScheduleACall').text('Thank you for contacting First Page Sage. You will be called back shortly.');
                    $('#lblScheduleACall').css(
                    {
                        color: 'green',
                        display: 'block',
                        fontWeight: 'bold',
                        padding: '5px'
                    });
                },
                error: function()
                {
                    alert('omg');
                    $('#frmScheduleACall').submit();
                }
            });
        }
        else
            $('#submitForm').removeAttr('disabled');

        return false;
    });
});

function Validate()
{
    $('#lblScheduleACall').css(
    {
        color: 'red',
        display: 'block',
        fontWeight: 'normal',
        padding: '5px'
    });

    if($('#name').val() == '')
    {
        $('#lblScheduleACall').text('Please enter your name.');
        return false;
    }

    if($('#number').val() == '')
    {
        $('#lblScheduleACall').text('Please enter your contact number.');
        return false;
    }

    //if($('#alt_number').val() == '')
    //{
    //    $('#lblScheduleACall').text('Please enter an alternative contact number.');
    //    return false;
    //}

    if($('#date').val() == '')
    {
        $('#lblScheduleACall').text('Please enter a date for the call.');
        return false;
    }

    if($('#hour').val() == '' || $('#minute').val() == '')
    {
        $('#lblScheduleACall').text('Please enter the time for the call.');
        return false;
    }

    if(isNaN(parseInt($('#hour').val(), 10)) || isNaN(parseInt($('#minute').val(), 10)) || parseInt($('#hour').val(), 10) < 1|| parseInt($('#hour').val(), 10) > 12 || parseInt($('#minute').val(), 10) < 0 || parseInt($('#minute').val(), 10) > 59)
    {
        $('#lblScheduleACall').text('Please enter a valid time.');
        return false;
    }
    
    if($('#DropDownTimezone option:selected').val() == '')
    {
        $('#lblScheduleACall').text('Please select your timezone.');
        return false;
    }

    return true;
}