/**
 * employerContact.js - Controller and renderer for the Contact Us (employer) input page.
 * 
 * @module YAHOO.storefront.pages.message.employerContactUsForm
 * @requires YAHOO.storefront.base
 */
YAHOO.namespace("storefront.pages.message.employerContactUsForm");

/**
 * The widget page renderer.
 * 
 * @class YAHOO.storefront.pages.message.employerContactUsForm.ReferralFormRenderer
 * @extends YAHOO.storefront.base.BaseRenderer
 */
YAHOO.storefront.pages.message.employerContactUsForm.ContactUsFormRenderer = function( messages )
{
	this.inheritFrom = YAHOO.storefront.base.BaseRenderer;
	this.inheritFrom( messages );
	
    /**
     * Disables the add widget button and shows a loading icon.
     * 
     * @method disableSubmitButton
     */
    this.disableSubmitButton = function()
    {
        document.getElementById("employerContactUsFormSubmit").setAttribute("disabled","true");
    };
    
    /**
     * Enables the add widget button and hides the loading icon.
     * 
     * @method enableSubmitButton
     */
    this.enableSubmitButton = function()
    {
        document.getElementById("employerContactUsFormSubmit").removeAttribute("disabled");
    };
    
    /**
     * Inits auto-tab
     */
    this.init = function()
    {
    	this.baseInit();
        this.bindAutoTab( ["phoneSegment1","phoneSegment2"] );
    };
    
};

/**
 * The widget page controller.
 * 
 * @class YAHOO.storefront.pages.message.employerContactUsForm.ContactUsFormController
 * @extends YAHOO.storefront.base.BaseController
 */
YAHOO.storefront.pages.message.employerContactUsForm.ContactUsFormController = function( renderer, formId, fieldConfigs )
{
	this.inheritFrom = YAHOO.storefront.base.BaseController;
	this.inheritFrom( renderer );
		
	/**
	 * The form ID.
	 * 
	 * @property formId
	 */
	this.formId = formId;
	
	/**
	 * The validation field configs
	 */
	this.fieldConfigs = fieldConfigs;
	
	/**
	 * The validator
	 */
	this.validator = null;
	
	/**
	 * Handles updating the employer's personal info to the server.
	 * 
	 * @method submitForm
	 */
	this.submitForm = function()
	{
		this.log("submitForm() - begin");
		
        this.renderer.disableSubmitButton();
        
		// use commons validator
		this.validator.validateForm(
        {
        	failure: function(controller)
            {
                return function()
                {
                	YAHOO.log("submitForm() failure");
                    controller.renderer.enableSubmitButton();
                };
            }(this),
            success: function(controller)
            {
                return function()
                {
                	YAHOO.log("submitForm() - success");
                    var form = document.getElementById( controller.formId );
                    form.submit();
                    controller.renderer.enableSubmitButton();
                };
            }(this)
        });
		
		this.log("submitForm() - end");
	},
	
	/**
	 * Initializes the controller by setting event bindings.
	 * 
	 * @method init
	 */
	this.init = function()
	{
	    this.baseInit();
	    
		// configure validator
		var callbacks = this.getValidationCallbacks();
		this.validator = new YAHOO.storefront.validation.Validator( this.formId, this.fieldConfigs, callbacks );
		
		this.bindCityStateLookupEvents( "zip", "city", "state" );
		
		// bind controller events
		this.event.onAvailable( this.formId, function()
		{
			this.validator.bindToType();
			this.renderer.disableFormSubmit( this.formId );
			this.event.addListener( "employerContactUsFormSubmit", "click", function( evt )
			{
				this.submitForm();
			}, this, true );
		}, this, true );
	};
};

	var messages = YAHOO.storefront.pages.message.employerContactUsForm.messages;
	var fieldConfigs = YAHOO.storefront.validation.forms.employerContactUsForm.fieldConfigs;
	var renderer = new YAHOO.storefront.pages.message.employerContactUsForm.ContactUsFormRenderer( messages );
	var controller = new YAHOO.storefront.pages.message.employerContactUsForm.ContactUsFormController( renderer, "employerContactUsForm", fieldConfigs );
	controller.init();
	renderer.init();
	
