/**
 * General javascript routines for the dhz forms
 *
 * @author David Hazel
 * @copyright 2009 Voyager Components Inc.
 * @notes: Uses the jQuery javascript library
 */

$(document).ready(function(){
    // hide and fade in current_form_step
    $('#current_form_step').hide();
    $('#current_form_step').fadeIn('slow');

    if(! $('#notification_message').length){
        // highlight the error div if it is present
        $('#validation_errors').css('border','none');
        $('#validation_errors').css('background','none');
        $('#validation_errors').css('margin-right','0px');
        $('#validation_errors').css('margin-left','0px');
        $('#validation_errors').dialog({
            title: 'Action Required',
            width: '50%',
            draggable: true,
            modal: true,
            buttons: {
                'Okay': function() {
                    if(! $('#proceed_link').length){
                        $(this).dialog('close');
                        setFocus();
                    }else{
                        window.location = $('#proceed_link').attr('href');
                    }
                }
            }
        });

        // set focus on first form field
        setFocus();
    }
});


//-----------------------------------------------------------------------------
// functions below
//-----------------------------------------------------------------------------

/**
 * Sets the focus to the first form field on the page
 */
function setFocus(){
    // get the input paragraph that we want to focus on
    if (! $('span:contains(action required)').length){  // focus first field
        $('#current_form_step :input')[0].focus();
    }else{  // focus first errored field
        $('p:has(span:contains(action required)) :input')[0].focus();
    } 
}
