$(document).ready( function() {
    $('#txt_domain').val('eg: yourdomain.com');
    $("#divDomainResult").hide();

    $('#txt_domain').focus( function () {
        if( $('#txt_domain').val() == 'eg: yourdomain.com' ) {
            $('#txt_domain').val('');
        }
    });

    $('#txt_domain').blur( function() {
        if( $(this).val() == '' ) {
            $(this).val('eg: yourdomain.com');
        }
    });


    $('#search').click( function() {
        var search_loader = '<img src="'+ base_url + 'images/ajax-bar-loader.gif" alt="Loading Results Please Wait...." />';
        $('#divDomainResult').html('<p>'+ search_loader +'</p>');
        $('#divDomainResult').slideDown('slow');
        check_domain();
        return false;
    });

});




function slideout(timer)
{
    setTimeout( function() {
        $("#divDomainResult").slideUp("slow", function () {
            });
    }, timer );
}



function check_domain()
{
    var domain = $('#txt_domain').val();
    var datastring = 'domain=' + domain;

    if( /http:\/\/www./ig.test(window.location) == false  ) {
        site_url = site_url.replace(/http:\/\/www./, 'http://');
    }

    if( ( /http:\/\/www./ig.test(site_url) == false ) && ( /http:\/\/www./ig.test(window.location) == true ) ) {
        site_url = site_url.replace(/http:\/\//ig, 'http://www.');
    }

    $.ajax({
        url : site_url + '/checkdomain',
        type : 'post',
        data : datastring,
        //            debug2 : console.log(site_url),
        success : function(result) {
            $('#divDomainResult').html(result);
            var failure_result = /(Not Available)|(Unable to get)|(valid Domain Name)/gi;
            var success_result = /(is Available)/gi;

            if (failure_result.test(result)) {
                $('#txt_domain').val('');
                setTimeout( function() {
                    $('#txt_domain').focus()
                }, 0 );
                slideout(3000);
            }

            if(success_result.test(result)) {
                Boxy.confirm("<strong>The Domain <span style=\"color: #229FD2;\">'" + domain + "'</span> is available, <br /><br /> Do you want to proceed to the order form ?</strong>", function() {
                    $.ajax({
                        url : site_url + '/checkdomain/setdomain',
                        data : 'domain='+ domain,
                        type : 'post',
                        success : function () {
                            window.location = site_url + '/order';
                        }
                    });
                }, {
                    title: 'Congratulations!!!'
                })
            }
        }
    });
}



