﻿$(document).ready(function () {
	// Salutation txt box changer
	$('[rel=addressBlock] [rel=salutations] :radio').click(function (ev) {
		jQuery.fn.updateAddressBlock($(this).parents('[rel=addressBlock]'), false);
	});

	jQuery.fn.updateAddressBlock = function (addressBlocks, initalCall) {
		addressBlocks.each(function (item) {
			var selectedValue = '';
			var selectedRadio = $(this).find('[rel=salutations] :checked');
			if (selectedRadio.length > 0) { selectedValue = selectedRadio.val(); }

			var toHide = null;
			var toShow = null;
			var valToDisable = null;
			var valToEnable = null;
			var txtFrom = null;
			var txtTo = null;

			if (selectedValue == '4') {
				toShow = $(this).find('[rel=company]:hidden');
				valToEnable = $(this).find('[rel=company] [rel=validator]');
				toHide = $(this).find('[rel=mrmrs]:visible');
				valToDisable = $(this).find('[rel=mrmrs] [rel=validator]');
				txtFrom = toShow.find('[rel=txtCompany]');
				txtTo = toHide.find('[rel=txtSurname]');
			} else {
				toShow = $(this).find('[rel=mrmrs]:hidden');
				valToEnable = $(this).find('[rel=mrmrs] [rel=validator]');
				toHide = $(this).find('[rel=company]:visible');
				valToDisable = $(this).find('[rel=company] [rel=validator]');
				txtFrom = toShow.find('[rel=txtSurname]');
				txtTo = toHide.find('[rel=txtCompany]');
			}

			if (toShow.length > 0) {
				if (txtFrom.val() == '') { txtFrom.val(txtTo.val()); }
				toShow.stop().slideDown();
			}
			if (toHide.length > 0) {
				toHide.stop().slideUp();
			}
			if (initalCall) {
				changeValidatorStatus($(this).find('[rel=company]:hidden [rel=validator]'), false);
				changeValidatorStatus($(this).find('[rel=mrmrs]:hidden [rel=validator]'), false);
			} else {
				changeValidatorStatus(valToEnable, true);
				changeValidatorStatus(valToDisable, false);
			}
		});
	};

	jQuery.fn.updateAddressBlock($('[rel=addressBlock]'), true);
});

function changeValidatorStatus(validators, enable) {
	validators.each(function (i) {
		this.enabled = enable != false;
	});
};

function textRequired(val, args) {
	args.IsValid = args.Value.replace(/^\s+|\s+$/g, '').length > 0;
};







