$(document).ready(function () {
    
    $('.datepick').datepick({dateFormat: 'MM d, yyyy'});
    
    /**
     * 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();
        }
    });
    
    $('.phone1').keyup(function (event) {   
	if ($(this).val().length == 0)
            $(this).prev().focus();             
        else if ($(this).val().length >= 3 )
        {
            event.preventDefault();
            $(this).next().focus();
        }
    });    
    
    $('.phone2').keyup(function (event) {   
	if ($(this).val().length == 0)
            $(this).prev().focus();             
        else if ($(this).val().length >= 4 )
        {
            event.preventDefault();
            //$(":input:eq(" + $(":input").index(this) + 1 + ")").focus();
        }
    });       


	$('.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();
                }
            }
        }
    });

   /**
    * Transfer value of dropdown to input box.
    */
   $('select[class="cs_select_to_input"]').change(function () {
   	$('input[name="' + $(this).attr('rel') + '"]').val($(this).val());
   });

   /** 
    * 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();
        }
    );
});
