/**
 * Utilities
 *
 * @author          Kate Bascombe
 * @modifiedby      $LastChangedBy: blundenr $
 * @copyright       Copyright Flight Centre Ltd. All rights reserved.
 * @version         $Revision: 6 $
 * @lastmodified    $Date: 2011-01-23 21:15:17 +1000 (Sun, 23 Jan 2011) $
 * @requires        
 */

;TT.UTIL =
{
	name: 'TT.UTIL',
	
	init: function()
	{
		var _this = this;
		FCL.UTIL.handleErrors(this);
		this.ezRezErrorResponseHandling();


	$(document).ready(function() {
		_this.loadActiveTab()
	});
	
	},

	serializeForm: function($form)
	{
		var obj = {},
			a = $form.serializeArray();
		
		$.each(a, function() 
		{
			obj[this.name] = this.value || '';
		});
		return obj;
	},
	
	deserializeForm: function($form, $formValues)
	{
		$.each($formValues, function(name, value)
		{ 
			if($form.find('[name="'+name+'"]'))
			{
				if (name == 'air1' || name == 'car1' || name == 'room1' ||
					$.cookie('selectedTab').indexOf('flight') > 0 
						&&  name == 'area1' 
						&& $form.attr('id').indexOf('flight') < 0 ) {
					return true; // Continue
				}
				
				var input = $form.find('[name="'+name+'"]');
				switch(input.attr('type')) {
					default:
						input.val(value);
						break;
					case 'radio':
						$form.find('[value='+value+']').attr('checked',true);
						break;
				}
			};
		});
		FCL.SIGNALS.send('TT.UTIL', 'deserializeForm', [$form, $formValues]);
	},
	
	ezRezErrorResponseHandling: function()
    {
        var _this = this
		var errorCheck = FCL.UTIL.getUrlVars();
		var errorMessage = errorCheck.ERROR_MESSAGE;

		if(errorCheck.ERROR_MESSAGE)
		{
			errorMessage = errorMessage.replace(/\+/gmi, ' ');
			
			$('<div></div>').html(errorMessage).dialog(
			{ 
				buttons: { "Ok": function() {$(this).dialog("close");}},
				title: 'Oops'
			});
		};
    },
	
		
	loadActiveTab: function()
    {
		//Load most recent active tab
		if($.cookie('selectedTab'))
		{
			$('[href=#'+($.cookie('selectedTab').replace('Form','Tab'))+']').trigger('click');
		} else
		{
			$('[href=#flightsTab').trigger('click');
		};
    }
	
};

TT.UTIL.init();
