function zeroPad(num,count) {
	var numZeropad = num + '';
	while(numZeropad.length < count) {
		numZeropad = "0" + new String(numZeropad);
	}
	return numZeropad;
}
(function($) {
	$.fn.clearIfDefault = function(opt) {
		return $(this).each(function() {
			val = $(this).val().toLowerCase();
			if(val=="dd" || val=="mm" || val=="yyyy" || val=="dd/mm/yyyy") {
				$(this).val('');	
			}
		});
	}
	$.fn.resetIfNotDate = function(opt) {
		return $(this).each(function() {
			val = $(this).val().toLowerCase();
			if(val=="") {
				$(this).val($(this).attr('originalvalue'));	
			}
		});
	}
})(jQuery);
jQuery(function($) {
	var datepickeroptions  = { 
		showOn: 'button',
		buttonImage: 'http://www.millenniumhotels.com.sg/img/img_calendar.gif',
		buttonImageOnly: true,
		buttonText:"arrival date",
		changeMonth:true,
		changeYear:true,
		dateFormat:'dd/mm/yy',
		minDate:new Date(),
		onClose: function(dateText, inst) {
			$(this).resetIfNotDate();
		}
	}
	/*SINGLE FIELD*/
	if($(".calendar-one").length > 0) {
		$("#single").each(function(){
			$(this).attr('originalvalue',$(this).val());									
		}).datepicker(datepickeroptions)
		.bind('focus',function(){
			$(this).clearIfDefault().datepicker('show');
		});
	}

	/*THREE FIELD*/
	if($(".calendar-two").length > 0) {
		$('.calendar-fields')
		.each(function(){
			$(this).attr('originalvalue',$(this).val());									
		}).bind('focus',function() {
			$(this).clearIfDefault();				   
		}).bind('blur',function() {
			$(this).resetIfNotDate();
		});
		
		$("#threefields").datepicker($.extend(datepickeroptions,{
			onSelect:function(dateText) {
				dateText = dateText.split('/');
				$('#arrivaldate_dd').val(dateText[0]);
				$('#arrivaldate_mm').val(dateText[1]);
				$('#arrivaldate_yyyy').val(dateText[2]);
			}
		}));
		
		$('#arrivaldate_dd, #arrivaldate_mm, #arrivaldate_yyyy').bind('input keypress paste change',function(){
			$("#threefields").datepicker('setDate',$('#arrivaldate_dd').val()+'/'+$('#arrivaldate_mm').val()+'/'+$('#arrivaldate_yyyy').val()).datepicker('show').datepicker('refresh');
		}).bind('focus',function() {
			$("#threefields").datepicker('refresh').datepicker('show');
		});
	}
	
	/*CALENDAR CASE THREE*/
	if($(".calendar-three").length > 0) {
		$("#mydate").datepicker($.extend(datepickeroptions,{
			onSelect:function(dateText) {
				dateText = dateText.split('/');
				$('#arrivaldate').val(dateText[0]);
				$('#selectArrivalMonth').val(dateText[1]);
				$('#arrivalYear').val(dateText[2]);
			}
		}));
		
		$('#selectArrivalMonth, #arrivalYear').change(function(){
			newDate = ((isNaN($('#arrivaldate').val()) || $('#arrivaldate').val() == 0) ? '01' : zeroPad($('#arrivaldate').val(),2)) + '/' + $('#selectArrivalMonth').val()+'/'+$('#arrivalYear').val();
			$('#mydate').datepicker('setDate',newDate);
			$('#arrivaldate').val($('#mydate').datepicker('getDate').getDate());
			$('#selectArrivalMonth').val(zeroPad($('#mydate').datepicker('getDate').getMonth()+1,2));
			$('#arrivalYear').val($('#mydate').datepicker('getDate').getFullYear());
		});
		
		$('#arrivaldate').bind('input keypress paste change',function() {
			newDate = zeroPad($('#arrivaldate').val(),2)+'/'+$('#selectArrivalMonth').val()+'/'+$('#arrivalYear').val();
			$('#mydate').datepicker('setDate',newDate);									  
		}).bind('focus',function() {
			$('#mydate').datepicker('show');									  
		});
	}

	/*For "Accordion" like Promocode selector*/

	$('.promo-code-version h4').click(function(){
		$('#promo-code-secret').val($(this).attr('class'));
		$('.promo-code-version h4').removeClass('selected');
		$('.promo-code-version .field').hide().find('input[name=code]').attr('name','code-disabled');
		$(this).addClass('selected').next('.field').show().find('input').attr('name','code');
	});
});
