//
// Subscribers.PublicArea.SubscriberRegistrationForm object
//

(function () {

	NI.WFI.Views['Subscribers.PublicArea.SubscriberRegistrationForm'] = {
		
		init : function () {
			for (var id in controls) {
				try {
					controls[id].init();
				} catch(e) { };
			}
		}
		
	};
	
	var controls = {
		'is_opt_in' : {
			init : function () {
				var setAddressBlockVisibility = function (isVisible) {
					var addressBlock = document.getElementById('registrationFormAddressBlock');
					if (!addressBlock) {
						throw new Error('Cannot find an address block element.');
					} else {
						addressBlock.style.display = (isVisible ? '' : 'none');
						(function swapTabIndexes() {
							var $ = function (tagName) { return addressBlock.getElementsByTagName(tagName); };
							var tags = { 
								'input' 	: $('input'), 
								'textarea' 	: $('textarea'), 
								'select' 	: $('select')
							};
							for (var name in tags) {
								var elements = tags[name];
								for (var i = 0, length = elements.length; i < length; i++) {
									elements[i].tabIndex += (isVisible ? -1000 : +1000);
								}
							}
						})();
					}
				};

				var control = document.getElementById(document.getElementById('is_opt_in_ClientID').value);

				control.onclick = function () {
					var isVisible = this.checked;
					setAddressBlockVisibility(isVisible);
					return true;
				};

				setAddressBlockVisibility.call(control);
			}
		},
		
		'privacyPolicyTrigger' : {
			init : function () {
				document.getElementById('privacyPolicyTrigger').onclick = function () {
					var params = _prepareParametersString({
						'location' : '0',
						'menubar' : '0',
						'resizable' : '1',
						'scrollbars' : '1',
						'status' : '1',
						'titlebar' : '0',
						'toolbar' : '0',
						'width' : '800',
						'height' : '800',
						'left' : '100',
						'top' : '100'
					});
					window.open(this.href, 'privacyPolicyWindow', params);
					return false;
				};
				
				var _prepareParametersString = function (data) {
					var params = [];
					for (var key in data) {
						params.push(key + '=' + data[key]);
					}
					return params.join(',');
				};
			}
		}
	};

})();