var globalFormController = new FormController('.globalLoginForm_button', '#global_login_form');

//============= SHOW/HIDE LINKS ======================================================

/**
 * Open and close Vendor Qick Pick
 */
$('#vqp_head').click(function ()
{
    if($('#vqp_inner').is(':hidden'))
    {   
        $(this).removeClass('closed_vendor');
        $(this).addClass('open_vendor');
        $(this).next("div").show();
    }
    else
    {
        $(this).removeClass('open_vendor');
        $(this).addClass('closed_vendor');
        $(this).next("div").hide();
    }                           
});        

/**
 * Open and close search bar "Filter By:" section
 */
$('.select_search_options').click(function ()
{
    if($('#search_checks').is(':hidden'))
    {           
        $('#search_checks').removeClass('closed');
        $('#search_checks').addClass('open');
    }
    else
    {
        $('#search_checks').removeClass('open');
        $('#search_checks').addClass('closed');
    }                           
});        

//====== Search Field Text Replace ===================================================   
function TextReplace (textFieldId, defaultVal, goButton) 
{    
    // Function created for text replacement in input text fields. Created By Derek Miranda. Modified 9/6/2010 by Giovanni de la Rosa.
    // @param textFieldId -> string -> please pass the entire CSS ID selector with the "#" sign.
    // @param defaultVal -> string -> Pass the exact string in the text field's "value" parameter. <input type="text" value="pass THIS"> NOTE: Is case-sensitive
    // @param goButton -> string -> please pass the entire CSS ID selector of the button in question.
    
    // The ID selector of a text field
    this.textFieldId = textFieldId;     
    
    // The default text value of a text field 
    this.defaultVal = defaultVal; 
    
    // The ID or Class selector of the go button related to the text field
    this.goButton = goButton;
                                
    // Executes input field text replacement using the three parameters given above
    this.execute = function() 
    {    
        
    	var search_text = defaultVal;

    	$(textFieldId).focus(function()
    	{
    	    if( $(textFieldId).val() == search_text )
    	    $(textFieldId).val('');
    	});

    	$(textFieldId).blur(function()
    	{
    	    if( $(textFieldId).val() == '' )
    	    {
    	        $(textFieldId).val(search_text);
    	    }
    	});

        $(textFieldId).change(function()
        {
            var value = $(textFieldId).val();
            if( value == '' || value == search_text )
            {
                $(goButton).attr('disabled');
            }
            else
            {
                $(goButton).removeAttr('disabled');
            }
        });              
    };     
};      

var global_search_toggler = document.getElementById("global_search").value;

var mainTextReplace = new TextReplace('#global_search', global_search_toggler, '.search_button');
mainTextReplace.execute();                                                               

var zipTextReplace = new TextReplace('#zip_code', 'Enter ZIP Code', '.go_button');
zipTextReplace.execute();         

/**
 * Called for common ajax errors
 */
function ajaxError(msg)
{
    alert(msg);
}

/**
 * Enables all form elements
 * @param button  The id of the button
 */
function enableSubmitButton(button)
{
    $('#'+button).removeAttr('disabled');
}

/**
 * Disables all form elements
 * @param form  The form to submit
 * @param button The id of the button to disable
 * @param submitForm
 */
function disableSubmitButton(form, button, submitForm)
{
    $('#'+button).attr('disabled', 'disabled');

    if( submitForm !== false )
    {
       form.submit();
    }
}

/**
 * Confirms deletes
 * @param msg
 * @return boolean
 */
function confirmDelete(msg)
{
    if( msg == null )
    {
        msg = 'Are you sure you want to delete this?';
    }

    return confirm(msg);
}
