var ContactUs = {
	//INIT FIELDS
	'init': function(){
		this.allFields = 
			[['first_name', 'keyup'],
			['last_name',   'keyup'],
			['address',     'keyup'],
			['address_2',   'keyup'],
			['country_id',  'change'],
			['city',        'keyup'],
			['state_id',    'change'],
			['zip',         'keyup'],
			['phone_line',  'keyup'],
			['phone_1',     'keyup'],
			['phone_2',     'keyup'],
			['phone_3',     'keyup']];
	},
	
	//BIND PAGE EVENTS
	'bindEvents' : function(){
		$('#general_form').livequery('submit', this.beforeSubmit);
		$('#user_country_id').livequery('change', this.updateBillingStates);
        $('#user_nature_of_inquiry').livequery('change', this.updateDisplayOrder);
	},
	
    //SHOW ORDER FIELDS ACCORDING TO INQUIRY
    'updateDisplayOrder': function() {
        if ( ($('#user_nature_of_inquiry').val() == 'Damaged Order') ||
             ($('#user_nature_of_inquiry').val() == 'Order Status') )
            $('#order-tr').css("visibility", "visible");
        else
            $('#order-tr').css("visibility", "hidden");
    },
    
	//SOME PREPARATIONS RIGHT AFTER LOADING
	'setDefaults': function() {
		//tabulating
		$('#user_phone_1').autotab({ target: 'user_phone_2', format: 'numeric' });
		$('#user_phone_2').autotab({ target: 'user_phone_3', format: 'numeric', previous: 'user_phone_1' });
		$('#user_phone_3').autotab({ previous: 'user_phone_2', format: 'numeric' });

		//populate phone' fields
		if ($('#user_phone_1').val() + $('#user_phone_2').val() + $('#user_phone_3').val() == '' && $('#user_phone_line').val()!='NaN' && $('#user_phone_line').val() != '--') {
                        $('#user_phone_1').attr('value' , $('#user_phone_line').val().substr(0, 3));
			$('#user_phone_2').attr('value' , $('#user_phone_line').val().substr(4, 3));
			$('#user_phone_3').attr('value' , $('#user_phone_line').val().substr(8, 4));
		}
		ContactUs.updateDisplayOrder();
	},
	
	//SOME PREPARATIONS BEFORE SUBMIT EVENT
	'beforeSubmit' : function() {
		//var time = new Date('yyyy-mm-dd hh:mm:ss');
		
		//$('#time').val(time.getDate());
		$('#time').val(Date());
		
              if ($('#user_phone_1').val() + $('#user_phone_2').val() + $('#user_phone_3').val() != '')
                 $('#user_phone_line').attr('value', $('#user_phone_1').val() +'-'+ $('#user_phone_2').val() +'-'+ $('#user_phone_3').val());
    	      else
                $('#user_phone_line').attr('value', '');
	},

	//GENERATE STATES MENU
	'updateStates' : function(fieldPrefix) {
		var country_id = $(fieldPrefix + 'country_id').val();
		var list = (country_id == 'usa' ? States.getUSA() : States.getCanada());
		
		$(fieldPrefix + 'state_id').append('<option value="">Select One</option>');
		
		for (ind in list)
			$(fieldPrefix + 'state_id').append('<option value="' + ind + '" '+(ind == defaultState ? 'selected' : '')+'>' + list[ind] + '</option>');
	},
	
	'updateBillingStates': function() {
		ContactUs.updateStates('#user_');
	}
};	

$(document).ready(function(){
	ContactUs.init();
	ContactUs.bindEvents();
	ContactUs.setDefaults();
	
	ContactUs.updateBillingStates();
});
