/**
 * Starts controllers for dynamic objects.
 * 
 * @author Anton vAsilyev <anton.vasilyev@gtmdevelopments.com>
 */
$(document).ready(function () {
	
	// Call necessary handle plugins.
	$('input.account-profile-organistionToggle').toggleOrganisation('account-profile-noAffiliations');

	$('#account-registration-saveChanges').bind('click', function () {
		var errorNumber = false;
		for($i=0; $i<20; $i++){
			if ($('#organisations-'+$i+'-organisation').attr('value') == 'ABTA' || 
				$('#organisations-'+$i+'-organisation').attr('value') == 'Global' ||
				$('#organisations-'+$i+'-organisation').attr('value') == 'IATA' ||
				$('#organisations-'+$i+'-organisation').attr('value') == 'TTA' ||
				$('#organisations-'+$i+'-organisation').attr('value') == 'Other'){
				
				if ($('#organisations-'+$i+'-number').attr('disabled') == false){
					if ($('#organisations-'+$i+'-number').attr('value') == ''){
						errorNumber = true;
						$('#organisations-'+$i+'-number').css({'border-color': 'red'});
					}else{
						$('#organisations-'+$i+'-number').css({'border-color': '#ABADB3 #DBDFE6 #DBDFE6'});
					}
				}else{
					$('#organisations-'+$i+'-number').css({'border-color': '#ABADB3 #DBDFE6 #DBDFE6'});
				}
			}
		}
		if (errorNumber == true){
			alert('Please enter membership number', 'Error');
		}else{
			var errorNumber = true;
			if($('#organisations-0-number').attr('disabled') == undefined){
				var organisationPageIs = false;
			}else{
				var organisationPageIs = true;
			}
			for($i=0; $i<20; $i++){
				if ($('#organisations-'+$i+'-number').attr('disabled') == false){
					errorNumber = false;
				}
			}
			if (organisationPageIs && errorNumber){
				if($('#account-profile-noAffiliations').attr('checked')){
					$('#account-registration-form').submit();
				}else{					
					alert('Please select at least one option before proceeding', 'Error');
				}
			}else{
				$('#account-registration-form').submit();
			}
		}
		return false;
	});
	
	if ($.isFunction($('#registration-junkBox').tooltip)) {
		$('#registration-junkBox').tooltip({
			track: true,
			delay: 0,
			showURL: false,
			opacity: 1,
			fixPNG: true,
			showBody: " - ",
			extraClass: "pretty fancy"
		});
	}
	
	$('input.thankYou-notificationEmail').radioCheckbox();
});

/**
 * JQuery plugin for remaking checkboxes to radio boxes.
 * 
 * @author Anton Vasilyev <anton.vasilyev@gtmdevelopments.com>
 */
(function ($) {
	/**
	 * Main plugin method.
	 * 	
	 * @return {void}
	 * @author Anton Vasilyev <anton.vasilyev@gtmdevelopments.com>
	 */
	$.fn.radioCheckbox = function() {
		var checkboxes = this;		 
		 
		 // Bind handlers for every found checkbox.
		 $.each(this, function () {
		 	 $(this).bind('click', function () {
		 	 	checkboxes.attr('checked', 0);
				$(this).attr('checked', 1);
			 });
		 });
	}
})(jQuery);

/**
 * JQuery plugin for toggle organisation's inputs.
 * 
 * @author Anton Vasilyev <anton.vasilyev@gtmdevelopments.com>
 */
(function($) {
	/**
	 * Main plugin method.
	 * 
	 * @param {string} noAffilations		Identifier of checkbox.			
	 * @return {void}
	 * @author Anton Vasilyev <anton.vasilyev@gtmdevelopments.com>
	 */
	$.fn.toggleOrganisation = function(noAffilations) {
		 var boxes = this;		 
		 
		 // Bind handlers for every found checkbox.
		 $.each(this, function () {
			 $(this).bind('click', function () {
				 var disabled = true;
				 if ($(this).attr('checked')) {
					 disabled = false;
				 }
					 
				 $('input', $(this).parent().prev()).attr('disabled', disabled);
				 $('input', $(this).parent().next()).attr('disabled', disabled);
				 
				 $('#' + noAffilations).attr('checked', _noAffilations());
			 });			 
		 })
		 
		// Special handler for checkbox labeled no affilations.
		$('#' + noAffilations).bind('click', function () {
			$.each(boxes, function () {
				$(this).attr('checked', false); 
				$('input', $(this).parent().prev()).attr('disabled', true);
				$('input', $(this).parent().next()).attr('disabled', true);
			});
			
			if (!$(this).attr('checked')) {
				return false;
			}
		});
		
		/**
		 * Checks no affilations.
		 * 
		 * @return {bool}
		 * @author Anton Vasilyev <anton.vasilyev@htmdevelopments.com>
		 */
		function _noAffilations()
		{
			var noAffilations = true;
			for (var i = 0; i < boxes.length; i++) {
				noAffilations &= !$(boxes[i]).attr('checked');
			}
			return noAffilations;
		}
		
		// Set default value for no affilations checkbox 
		//$('#' + noAffilations).attr('checked', _noAffilations());
	} 
})(jQuery);