var abbr_months = new Array();
abbr_months.push('Jan');
abbr_months.push('Feb');
abbr_months.push('Mar');
abbr_months.push('Apr');
abbr_months.push('May');
abbr_months.push('Jun');
abbr_months.push('Jul');
abbr_months.push('Aug');
abbr_months.push('Sep');
abbr_months.push('Oct');
abbr_months.push('Nov');
abbr_months.push('Dec');

var numeric_months = new Array();
numeric_months.push('01');
numeric_months.push('02');
numeric_months.push('03');
numeric_months.push('04');
numeric_months.push('05');
numeric_months.push('06');
numeric_months.push('07');
numeric_months.push('08');
numeric_months.push('09');
numeric_months.push('10');
numeric_months.push('11');
numeric_months.push('12');

$(document).ready(function() {
	
	// numbers only
	$('.numbers').keyup(function () { 
		this.value = this.value.replace(/[^0-9\.]/g,'');
	});
	
	// Get day
	$('.button-day').click(function() {
		$(this).addClass('active');
		$('.table-month-wrapper, .table-year-wrapper').fadeOut('fast');
		$('.button-month, .button-year').removeClass('active');
		$('.table-day-wrapper').fadeIn('fast');			
	});
	
	$('.table-day td a').click(function() {
        var selected = $(this).text();
        $('.button-day').val(selected).removeClass('error');
		$(this).checkAll();
		$('.table-day-wrapper').fadeOut('fast');
		$('.button-day').removeClass('active');
	});	
	
	// Get month
	$('.button-month').click(function() {
		$(this).addClass('active');							  
		$('.table-day-wrapper, .table-year-wrapper').fadeOut('fast');
		$('.button-day, .button-year').removeClass('active');
		$('.table-month-wrapper').fadeIn('fast');	
	});
	
	$('.table-month td a').click(function() {
        var selected = $(this).text();
        $('.button-month').val(selected).removeClass('error');
		$(this).checkAll();
		$('.table-month-wrapper').fadeOut('fast');
		$('.button-month').removeClass('active');
	});		

	// Get year
	$('.button-year').click(function() {
		$(this).addClass('active');
		$('.table-day-wrapper, .table-month-wrapper').fadeOut('fast');
		$('.button-day, .button-month').removeClass('active');
		$('.table-year-wrapper').fadeIn('fast');
	});
	
	$('.table-year td a').click(function() {
        var selected = $(this).text();
        $('.button-year').val(selected).removeClass('error');
		$(this).checkAll();
		$('.table-year-wrapper').fadeOut('fast');
		$('.button-year').removeClass('active');
	});

	// HTML5 forms
	$('form').h5Validate();
	
	// change Submit button graphic
	jQuery.fn.checkAll = function() {	
		if ($('.numbers').hasClass('error')) {	
			  $('input[type=submit]').addClass("disabled");	
			  return false;	
		} else {
			$('input[type=submit]').removeClass('disabled');
		}
	};	

	// 21 or older popup
	jQuery.fn.popUp = function() {
		$('#popup').fadeIn(150).prepend('<a href="#" class="close"><img src="../../images/11/popup-close.png" class="img-close" title="Close Window" alt="Close" /></a>');
	
		var popMargTop = ($('#popup').height()) / 2;
		var popMargLeft = ($('#popup').width()) / 2;
	
		$('#popup').css({
			'margin-top' : -popMargTop,
			'margin-left' : -popMargLeft
		});
	
		$('body').append('<div id="fade"></div>'); 
		$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(150); 
		return false;
	};

	$('a.close, #fade').live('click', function() { 
		$('#fade, .popup-message').fadeOut(function() {
			$('#fade, a.close').remove();  
		});
		return false;
	});
	
	if(trigger) {
		$('.popup-message img').each(function(idx,val){
			$(val).hide();
		});
		$('#error'+message).show();
		$(this).popUp();
	};
	
	$("input").attr("tabindex", "-1");
	
	$.vegas({
	  src:'../../images/11/slide-01.jpg',
	  valign:'top', 
	  align:'left'
	});
	
});

function swapAbbr(){
	for(var i=0;i<abbr_months.length;i++) {
		if(abbr_months[i]==$('.button-month').val()) {
	  		$('.button-month').val(numeric_months[i]);
	  		return true;
	  	}
	}
	return false;
}

