/**
 * ;FCNZ.MAILERSTORE form handler
 *
 * @author          Richard Gustin
 * @modifiedby      $LastChangedBy$
 * @copyright       Copyright Flight Centre Ltd. All rights reserved.
 * @version         $Revision$
 * @lastmodified    $Date$
 * @requires        FC, FCNZ.SETTINGS, FCL.DATETIME, FCL.UTIL, FCL.FORMS
 */

;FCNZ.SEARCH =
{

	$form: null,
    
	validatorSettings: {
		rules:
            {
                q: { 
				requiredNotDefault: true 
				}
                
            },

		messages:
		{
			q: 'Please enter some text'
		}	

	},
	
	
    init: function($form)
    {

		this.$form = $form;
        this.setupValidation();
        this.setupForm();
			
    },
    
    fieldMappings: {
        q: 'Search'
    
    },
    // default values for fields by name
    defaultValues: {
        q: 'Type here...'
    },
        
   
    /*
     *  Sets up validation using jQuery.validate
     */
    setupValidation: function()
    {
        var _this = this;
        
        $.validator.addMethod('requiredNotDefault', function(value, element, param)
        {
		
			return (value!='')  && (value != _this.defaultValues[element.name] );
        }, 'This field is required');
              
        this.$form.validate(
        {
	
            errorPlacement: function(){ },
            rules: _this.validatorSettings.rules,
            messages: _this.validatorSettings.messages,
            invalidHandler: function(e, validator)
            {
                var errorString = '';
                $.each(validator.errorMap, function(key, val)
                {
                    var fieldKey = '';
                    if(typeof _this.fieldMappings != 'undefined' && typeof _this.fieldMappings[key] != 'undefined')
                    {
                        fieldKey += _this.fieldMappings[key];
                    }
                    else
                    {
                        fieldKey += FCL.UTIL.titleCaps(key);
                    }
                    errorString +=   val +'\n';
                });
                errorString = errorString.substring(0, errorString.length-1);

                var errors = validator.numberOfInvalids();
                if(errors)
                {
                    var message = errors == 1
                        ? '1 field has errors: ' + errorString
                        : errors + 'fields have errors: ' + errorString;
                }
                
                alert(errorString);

            },

            submitHandler: function(form)
            {	
				 
				window.location.href =
                    '{1}?q={2}'.fclFormat($(form).attr('action'), $(form).find('input[name="q"]').val());						
            }
        });
    },
    
    /*
     *  Sets up default values in fields based on the defaultValues mapping (by name)
     */
    setupForm: function(){
        var _this = this;
        
        $.each(_this.defaultValues, function( key, val){
            _this.$form.find('[name="q"]').DefaultValue(val); 
        });
    }
    
};
		
				

