﻿$(document).ready(
    function()
    {
        $('div#callbackFeature p.buttonHolder a')
            .attr('href', '/buying-your-neo-trac/call-me-back/callback-form.aspx?width=332&amp;height=370&amp;modal=true')
            .addClass('thickbox')
            .click
            (
                function()
                {
                    pageTracker._trackPageview('/call-me-back/call-back-form');
                }
            )
        ;
    }
);

function ValidateCallbackRequest(fullName, phone, callbackWaitTime, email)
{
    var isValid = true;
    
    if (!ValidateRequiredField(fullName))
    {
        AddValidationMessage("txtFullName", "Please enter your full name");
        isValid = false;
    }
    else
    {
        RemoveValidationMessage("txtFullName");
    }
    
    if (!ValidateRequiredField(phone))
    {
        AddValidationMessage("txtPhone", "Please enter your telephone number");
        isValid = false;
    }
    else
    {
        RemoveValidationMessage("txtPhone");
    }
    
    if (!ValidateRequiredField(callbackWaitTime))
    {
        AddValidationMessage("ddlCallbackWaitTime", "Please select the best time for us to call");
        isValid = false;
    }
    else
    {
        RemoveValidationMessage("ddlCallbackWaitTime");
    }
    
    //if (!ValidateEmailField(email))
    //{
    //    AddValidationMessage("txtEmail", "Please enter a valid email address");
    //    isValid = false;
    //}
    //else
    //{
    //    RemoveValidationMessage("txtEmail");
    //}
    
    return isValid;
}

function ValidateRequiredField(value)
{
    if (value != null)
    {
        if (value.length > 0)
        {
            return true;
        }
    }
    
    return false;
}

function ValidateEmailField(value)
{
    if (value != null)
    {
        if (value.length > 0)
        {
            var regex = new RegExp('^.+@[^\.].*\.[a-z]{2,}$');
            return value.match(regex);
        }
    }
    
    return true;
}

function AddValidationMessage(id, message)
{
    var validationId = id+'Validation';
    if ($('#'+validationId).length == 0)
    {
        var validationMessage = $('<span class="validation" id="'+validationId+'">'+message+'</span>');
        $('#'+id).after(validationMessage);
    }
}

function RemoveValidationMessage(id)
{
    $('#'+id+'Validation').remove();
}

function CallbackRequest()
{
    var fullName = $('input#txtFullName').attr('value');
    fullName = (fullName == null ? "" : fullName);
    
    var phone = $('input#txtPhone').attr('value');
    phone = (phone == null ? "" : phone);
    
    var callbackWaitTime = $('select#ddlCallbackWaitTime :selected').attr('value');
    callbackWaitTime = (callbackWaitTime == null ? "" : callbackWaitTime);
    
    var email = "";//$('input#txtEmail').attr('value');
    //email = (email == null ? "" : email);
    
    var comments = $('textarea#txtComments').attr('value');
    comments = (comments == null ? "" : comments);
    
    if (ValidateCallbackRequest(fullName, phone, callbackWaitTime, email))
    {
        var VALIDATION_ERROR_MESSAGE = "VALIDATION_ERROR";
        var DATA_ERROR_MESSAGE = "DATA_ERROR";
        var REQUEST_ERROR_MESSAGE = "REQUEST_ERROR";
        var REQUEST_SUCCESS_MESSAGE = "REQUEST_SUCCESS";
    
        //Disable the submit buttons and show a timer
        DoPreRequest();

        $.ajax
        (
            {
                type: 'POST',
                url: '/buying-your-neo-trac/call-me-back/callback-form.aspx',
                data: 'fullName='+fullName+'&phone='+phone+'&callbackWaitTime='+callbackWaitTime+'&email='+email+'&comments='+comments,
                success: function(data, textStatus)
                {        
                    //Re-enable our buttons and get rid of the timer image
                    DoPostRequest();
                    
                    switch (data)
                    {
                        case VALIDATION_ERROR_MESSAGE:
                            AddMessage('warning', 'callbackForm', 'Please make sure you have completed all required fields and try again.');
                            break;
                        case DATA_ERROR_MESSAGE:
                            AddMessage('error', 'callbackForm', 'There was an error completing your request. Please try again.');
                            break;
                        case REQUEST_ERROR_MESSAGE:
                            AddMessage('error', 'callbackForm', 'There was an error completing your request. Please try again.');
                            break;
                        case REQUEST_SUCCESS_MESSAGE:
                            ShowConfirmation(callbackWaitTime);
                            break;
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown)
                {
                    AddMessage('error', 'callbackForm', 'There was an error completing your request. Please try again.');                    
        
                    //Re-enable our buttons and get rid of the timer image
                    DoPostRequest();
                }
            }
        );
    }
}

function DoPreRequest()
{
    var submitButton = $('div#callbackForm input#btnSubmit');
    var cancelButton = $('div#callbackForm input#btnCancel');        
    submitButton.attr('disabled', 'disabled');
    cancelButton.attr('disabled', 'disabled');
    submitButton.after($('<img id="ajaxLoader" src="/images/site/ajax-loader.gif" width="16" height="16" alt="" style="float: right; padding-right: 10px;" />'));
}

function DoPostRequest()
{
    var submitButton = $('div#callbackForm input#btnSubmit');
    var cancelButton = $('div#callbackForm input#btnCancel'); 
    submitButton.attr('disabled', '');
    cancelButton.attr('disabled', '');
    $('div#callbackForm img#ajaxLoader').remove();
}

function AddMessage(type, containerId, text)
{
    var message = $('<div class="message '+type+'"><p>'+text+'</p></div>');
    var container = $('#'+containerId);
    container.find('div.message').remove();
    container.prepend(message);
}

function ShowConfirmation(callbackWaitTime)
{
    var callbackForm = $('div#callbackForm');
    callbackForm.empty();    
    AddMessage('confirmation', 'callbackForm', 'Thank you for your enquiry. A member of our customer services team will be in contact '+(callbackWaitTime == 0 ? 'ASAP' : 'in '+callbackWaitTime+' minutes')+'.');
    var closeButton = $('<div class="formOptions"><input type="image" style="border-width: 0px; height: 25px; width: 70px;" onclick="javascript:tb_remove()" alt="Close" src="/images/site/Close.gif" class="image floatLeft" id="btnClose" /></div>');
    callbackForm.append(closeButton);
    pageTracker._trackPageview('/call-me-back/call-back-form-submit');
}
