$(document).ready(function () {
    
    if (ROOT_PAGE != '')
    {
        var list_item = $('#' + ROOT_PAGE);

        var nav_index = $('.mainnav').not('ul .subnavs li').index(list_item);

        /**
         * Hide subnav when not selected.
         */
        if (nav_index >= 0)
        {
            ddaccordion.expandone('mainnav', nav_index);
        }
    }
    /**
     * Clear the content of search box on set focus.
     */
    $('input[name="search"]').focus(function () {
        $(this).val('');
    });

    $('input[name="search"]').blur(function () {
        if ($(this).val() == '')
        {
            $(this).val('Search the web');
        }
    });

    $('input[name="inches"]').keyup(function(event)
    {
        if ($(this).val().length > 1)
        {
            event.preventDefault();

            if ($('input[name="pounds"]').size() > 0)
            {
                $('input[name="pounds"]').focus();
            }
        }
    });

    $('input[name="feet"]').keyup(function () {
        if ($('input[name="feet"]').val().length > 0) 
        {
            $('input[name="inches"]').focus();
        }
    });

    /**
     * Auto-move cursor for phone number fields.
     */
    $('.phone1').keydown(function (event) {
   		// Allow only backspace and delete
        if ( event.keyCode == 46 || event.keyCode == 8 )
        {
            if ($(this).val().length == 0)
            {
                $(this).prev().focus();
            }
        }
        else if ($(this).val().length > 2 )
        {
            event.preventDefault();
            $(this).next().focus();
        }
    });

    $('.phone2').keydown(function (event) {
        if ( event.keyCode == 46 || event.keyCode == 8 )
        {
        
        }
        else if ($(this).val().length > 3)
        {
            event.preventDefault();
        }
    });

	$('.require_numeric').keydown(function(event) {
		// Allow only backspace and delete
        if ( event.keyCode == 46 || event.keyCode == 8 )
        {
        
        }
        else {
            if (event.keyCode < 95) 
            {
                if (event.keyCode < 48 || event.keyCode > 57 ) 
                {
                    event.preventDefault();
                }
            } 
            else 
            {
                if (event.keyCode < 96 || event.keyCode > 105 ) 
                {
                    event.preventDefault();
                }
            }
        }
    });

   /** 
    * Alert when clicking nav message.
    */
    $('.back-button').live('click', function () {
        var x = confirm('Are you sure you want to leave this page? All your typed entries will be lost.');

        if (x)
        {
            window.location = BASE_URL + ROOT_PAGE;
        }
    });     

    $('input[name="referral_name"]').keyup(function ()
    {         
          $('span[name="referral-name"]').empty().text($(this).val());
    });

    $('input[name="patient_name"]').keyup(function ()
    {         
          $('span[name="patient-name"]').empty().text($(this).val());
    });

    if ($('#referral-message').size() > 0)
    {
        var referral_message = $('#referral-message').html();

        $('#referral-message').html(referral_message.replace(/%%REFERRAL_NAME%%/g, '<span name="referral-name"></span>'));

        referral_message = $('#referral-message').html();

        $('#referral-message').html(referral_message.replace(/%%PATIENT_NAME%%/g, '<span name="patient-name"></span>'));

        referral_message = $('#referral-message').html();

        $('#referral-message').html(referral_message.replace(/%%CLOSING%%/g, 'Alleghenies Surgical'));

        $('#referral-message').hide();
    }

    $('#referral-message-preview').toggle(
        function() {
            $('#referral-message').show();
        }, 
        function() {
            $('#referral-message').hide();
        }
    );
});

