// Cufon Replace
Cufon.replace('h1');


$(document).ready(function() {
	
	$("select, input:checkbox, input:radio, input:file").uniform();
	
	$('#condom-form').validate();
	$('form').verification();

});

function bounce(message, container) {
			$('#bounceBox').remove();
			$(container).append(
				$('<div />')
				.attr('id', 'bounceBox')
				.append(
					$('<p />')
						.html(message)
				)
			);

			$('#bounceBox')
			.stop();
					$('#submit-btn').attr('disabled', 'disabled').addClass('disabled').css('opacity', '.5');

			
}
function unbounce() {
	$('#bounceBox').remove();
	$('#submit-btn').removeAttr('disabled').removeClass('disabled').css('opacity', '1');
}

(function($){
	$.fn.authorizeAge = function() {
		return this.each(function() {
			var rulesParsing = $(this).attr('id');
			var rulesRegExpYear = /year/;
			var rulesRegExpMonth = /month/;
			var rulesRegExpDay = /day/;
			if(rulesRegExpDay.test(rulesParsing) || rulesRegExpMonth.test(rulesParsing) || rulesRegExpYear.test(rulesParsing)) {
				var today = new Date();
					
				var year = $('#submission_date_of_birth_year').val();
				var month = $('#submission_date_of_birth_month').val();
				var day = $('#submission_date_of_birth_day').val();
				var age = 16;
				var mydate = new Date();
				mydate.setFullYear(year, month-1, day);

				var currdate = new Date();
				currdate.setFullYear(currdate.getFullYear() - age);
				if ((currdate - mydate) < 0){
					bounce("Thanks for your request, however we can't send a free condom & lube pack to anybody under 16.  You can still get free or low cost condom & lube packs from Family Planning, a Sexual Health Centre, Youth Health Centre, or your GP.", "#boxContBirthday");
				} else {
					unbounce();
				}
			}
		});
	};

	$.fn.radioMessage = function() {
		return this.each(function(){
			var rulesParsing = $(this).attr('class');
			var rulesRegExp = /bounce\[(.*)\]/;

			if(rulesRegExp.test(rulesParsing)) {
				var str = rulesRegExp.exec(rulesParsing)[1];
				var value =  str.split(',')[0];
				var rulesRegExpBis = /\"(.*)\"/;
				if(rulesRegExpBis.test(str)) {
					var message= rulesRegExpBis.exec(str)[1].replace(/_/g, ' ');
                                        var container = str.split(',')[(str.split(',').length-1)];
				}
				if($(this).val() != value) {
					bounce(message, container);

					$(this).parents('.yesNoOptions').nextAll().find('div').addClass('disabled');
					$(this).parents('.yesNoOptions').nextAll().find('input:radio:checked').removeAttr('checked');
					$(this).parents('.yesNoOptions').nextAll().find('.radio span').removeClass('checked');
					$(this).parents('.yesNoOptions').nextAll().find('input').attr('disabled', 'disabled');
					$(this).parents('.yesNoOptions').nextAll().find('label').attr('disabled', 'disabled');
					$(this).parents('.yesNoOptions').nextAll().find('select').attr('disabled', 'disabled');
					$('#submit-btn').attr('disabled', 'disabled').addClass('disabled').css('opacity', '.5');
				} else {
					$(this).parents('.yesNoOptions').nextAll().find('div').removeClass('disabled');

					$(this).parents('.yesNoOptions').nextAll().find('input').removeAttr('disabled');
					$(this).parents('.yesNoOptions').nextAll().find('label').removeAttr('disabled');
					$(this).parents('.yesNoOptions').nextAll().find('select').removeAttr('disabled');
					$('#submit-btn').removeAttr('disabled').removeClass('disabled').css('opacity', '1');

					unbounce();
				}
			}
		});
	};

	$.fn.verification = function() {
		/* each form */
		return this.each(function(){
			$('input:radio',this).change(function(){
				$(this).radioMessage();
			});
			$('select',this).change(function(){
				$(this).authorizeAge();
			});
		});

	};


})(jQuery);