var SignUp = {
		
	setUserTimeZone : function (e) {		
		e.preventDefault();
		var $z_input = $(this).removeClass('error');
		var zip = $.trim($z_input.val());
		if (zip.length>=5) {
			if (zip.match(/^\d{5}$/)) {
	            $.post('/rules/getTimeZones', {zipcode : zip}, function (timezones) {                      
	                  $('select.tz-select').html(timezones);
	              });				
			} else {
				$z_input.addClass('error');
			}
		}
	},
	deviceModelSwitch : function (e) {
		if (!FMUtils.isEmpty($(this).val())) {
			$.post('/devices/deviceModelSwitch', {deviceModelId : $(this).val()}, function (data) {
				if (data.requirePAIRING) {
					$('#ma-device-options').fadeOut(function () {
						$('#ma-activation-form').fadeIn();
					});
				} else if (data.requirePIN) {
					$('#ma-device-options').fadeOut(function () {
						$('#ma-pin-form').fadeIn();
					});
				}
			}, 'json');
		}
	},
	getCountryCities : function (e) {
		var countryCode = $(this).val();
		if (!FMUtils.isEmpty(countryCode)) {
			if (countryCode=='US') {
				$('div.location-group').find('div.cities:visible').fadeOut(function () {
					$('div.location-group div.zip-code:hidden').fadeIn();
				});
			} else {
				$('div.location-group select').addClass('disabled').attr('disabled', 'disabled');
				$('div.location-group').find('div.zip-code:visible').fadeOut(function () {
					$('div.location-group div.cities:hidden').fadeIn();
				});
				$.post('/rules/getCountryCities', {countryCode : countryCode}, function (cities) {
					$('div.location-group div.cities select').find('option:not(:first)').remove().end().append(cities);
					$('div.location-group select').removeClass('disabled').removeAttr('disabled');
				});
			}
		}
	},
	getDeviceModels : function () {
		if (!FMUtils.isEmpty($(this).val())) {
			$('#ma-device-options select').addClass('disabled').attr('disabled', 'disabled');
			$.post('/devices/getDeviceModels', {manufacturerId : $(this).val()}, function (data) {
				$('#ma-device-options select[name="ma_device_models"]')
					.find('option:not(:first)').remove().end()
					.append(data)
					.closest('div.devicemodels').fadeIn();
				$('#ma-device-options select').removeClass('disabled').removeAttr('disabled');
			});
		}
	},
	tosAlert : function () {
		$('#tos-agree-alert').dialog({
			modal: true, resizable: false,
			buttons: {
				'OK' : function () {
					$(this).dialog('close').dialog('destroy');
					$('html,body').animate({ scrollTop: $('body').height()-300}, 750);
				}
			},
			close: function (event, ui) {
				$(this).dialog('destroy');
				$('html,body').animate({ scrollTop: $('body').height()-300}, 750);
			}
		});	
	}
};

$(document).ready(function () {
	$('div.ma-row')
		.find('select[name="ma_device_manufacturers"]').change(SignUp.getDeviceModels).end()
		.find('select[name="Countries"]').change(SignUp.getCountryCities).end()
		.find('select[name="ma_device_models"]').change(SignUp.deviceModelSwitch);
	$('#user-registration-form').submit(function (e) {
		if (!$('#ma_agree_tos').is(':checked')) {
			e.preventDefault();
			SignUp.tosAlert();
		}
	});
	$('div.ma-location-tz-form').find('div.zip-code input').keyup(SignUp.setUserTimeZone);

});