$(document).ready(function() {
	$('.field-row label').hide();
	
	$('a.general-conditions').click(function() {
		window.open($(this).attr('href'));
		return false;
	});
	
	$('input[type=checkbox]').each(function() {
		var $checkbox = $(this);
		var $customCheckbox = $('<a href="#" class="custom-checkbox"><span>Check</span></a>');
		if($checkbox.is(':checked')) {
			$customCheckbox.addClass('on');
		}
		$checkbox.after($customCheckbox);
		$checkbox.hide();
		
		$checkbox.change(function() {
			doCustomCheckboxClick($customCheckbox);
		});
		
		$customCheckbox.click(function() {
			if($checkbox.is(':checked')) {
				$checkbox.removeAttr('checked');
			} else {
				$checkbox.attr('checked', 'checked');
			}
			$checkbox.trigger('change');
			return false;
		});
	});
})


/**
 * A custom checkbox click
 */
function doCustomCheckboxClick($customCheckbox) {
	// If selected
	if($customCheckbox.hasClass('on')) {
		// Then, deselect now:
		setNewCustomCheckboxState($customCheckbox, false);
	}
	// If not selected
	else {
		// Then, select now:
		setNewCustomCheckboxState($customCheckbox, true);
	}
}

/**
 * Set new state of checkbox
 */
function setNewCustomCheckboxState($customCheckbox, newStateFlag) {
	// If selected
	if(! newStateFlag) {
		// Then, deselect now:
		$customCheckbox.removeClass('on');
		//$customCheckbox.siblings('input[type=checkbox]').removeAttr('checked');
	}
	// If not selected
	else {
		// Then, select now:
		$customCheckbox.removeClass('on').addClass('on');
		//$customCheckbox.siblings('input[type=checkbox]').attr('checked', 'checked');
	}
}
