/*
	MMF Namespaced Validation Hooks, Required for Proper Formatting of 
	Forms
 */

/*
    Dear Self in 6 Months,

    The error handling paradigm here isn't quite obvious, so here's 
    how it works. 
    
    Error Text is set regardless of whether there is an error or not, 
    but it gets updated when it changes. Placement is just that, it 
    just puts the error in the right place, it shouldn't do anything 
    more then that.
    
    Highlight / Unhighlight then controls the visibility of this text 
    along with the supplementary error indicators. get_validator_container, 
    toggle_validator_state are the two big functions to override when 
    necessary...
*/
jQuery(document).ready(function() {
	
	var error_count = {}
	
	MMF.validation.clear_count = function(form) {
		//console.log('Attempting to Clear error_count')
		error_count = {form : {}}
		//console.log(error_count)
	}
	
	MMF.validation.get_validator_container = function(form_name, element) {
		
		error_row = jQuery(element).closest('.form_row')
		
		//console.log(error_row)
		
		// Create Form Counter
		if(!error_count[form_name]) {
			error_count[form_name] = {}
		}
		
		// Create Row Counter
		if(!error_count[form_name][error_row.attr('id')]) {
			error_count[form_name][error_row.attr('id')] = 0
		}
		
		return error_row
    }
                
	MMF.validation.set_validator_error_text = function(form_name, element, error_text) {
					
		error_row = this.get_validator_container(form_name, element)
		//console.log('Error Count For ', error_row.attr('id'), ' : ', error_count[form_name][error_row.attr('id')])
		error_container = error_row.find('.form_inline_error')
		
		if(error_count[form_name][error_row.attr('id')] <= 1) {
			error_container.removeClass('hidden');			
			error_container.html(error_text).show();
		} else {
			
			if(error_text.text() == '') {
				
			} else {
				error_container.removeClass('hidden');		
				error_container.append('<br />'+error_text.html()).show();
			}
		}
	}
    
	MMF.validation.toggle_validator_state = function (form_name, form_error_field_class, element, type, show_success) {
		
		error_row = this.get_validator_container(form_name, element)
		
		if(show_success == undefined) {
			show_success = true;
		}
		
		if(type == 'fail') {
			error_count[form_name][error_row.attr('id')] += 1
		} else if (error_count[form_name][error_row.attr('id')] > 0) {
			error_count[form_name][error_row.attr('id')] -= 1
		}
		
        // Blank Indicator
        error_row.find('.form_field_status').addClass('hidden')
                                
        // Show Error
        if(type == 'fail' || error_count[form_name][error_row.attr('id')] > 1) {
        	
        	$('#'+form_name+'_error').removeClass('hidden')
        	        	
           //error_row.find('.formError').removeClass('hidden')            
            
            //error_row.find('form_field_status_container').addClass(form_error_field_class)
            error_row.find('.fail').removeClass('hidden')
            
            //console.log('For ', error_row.attr('id'), ' : FAIL')
        }  else if(show_success == true) {
        	
        	//console.log('SHOULD BE SUCCESS: ', error_row)
        	
            //error_row.find('.formError').addClass('hidden')
            
            error_row.find('.success').removeClass('hidden')
            //console.log('For ', error_row.attr('id'), ' : SUCCESS')
        }
    }
});
