// charset="UTF-8"

/////////
/////////
/////////
/////////
var date = '';

// Prepare to show a date picker linked to three select controls 
function readLinked() { 
    jQuery('.datepicker').val(jQuery('.dp-year option:selected').val()+ jQuery('.dp-month option:selected').val() 
        +jQuery('.dp-date option:selected').val()); 
    return false; 
} 
 
// Update three select controls to match a date picker selection 
function updateLinked(date) { 
    jQuery('.dp-month').val(date.substring(0, 2)); 
    jQuery('.dp-date').val(date.substring(3, 5)); 
    jQuery('.dp-year').val(date.substring(6, 10)); 
//    jQuery('.dp-year').val(date.substring(0, 3)); 
//    jQuery('.dp-month').val(date.substring(4, 5)); 
//    jQuery('.dp-date').val(date.substring(6, 7)); 
} 
 
// Prevent selection of invalid dates through the select controls 
function checkLinkedDays() { 
    var daysInMonth = 32 - new Date(jQuery('.dp-year option:selected').val(), 
		jQuery('.dp-month option:selected').val() - 1, 32).getDate(); 
    var MINday_year = jQuery('#datepicker-startdate-year').val(); 
    var MINday_month = jQuery('#datepicker-startdate-month').val(); 
    var MINday_date = jQuery('#datepicker-startdate-date').val(); 
	jQuery('.dp-date option').each(function () {
		if(jQuery('.dp-date option:selected').val() >= daysInMonth){
			if(jQuery(this).val() == daysInMonth){
				jQuery(this).attr('selected', 'selected');
			}else{
				jQuery(this).removeAttr('selected');
			}
		}else{
			if(jQuery(this).val() == jQuery('.dp-date option:selected').val()){
				jQuery(this).attr('selected', 'selected');
			}else{
				jQuery(this).removeAttr('selected');
			}
		}
		if((jQuery(this).val() > daysInMonth) ||
			((jQuery('.dp-year option:selected').val() == MINday_year) && (jQuery('.dp-month option:selected').val() == MINday_month)
				&& (jQuery(this).val() < MINday_date)) ||
			((jQuery('.dp-year option:selected').val() == MINday_year) && (jQuery('.dp-month option:selected').val() < MINday_month)) ||
			(jQuery('.dp-year option:selected').val() < MINday_year)
		){
			if(jQuery(this).val() != ''){
				jQuery(this).attr('disabled', 'disabled');
			}
		}else{
			jQuery(this).removeAttr('disabled');
		}
	});
//	if((jQuery('.dp-year option:selected').val() == MINday_year) && (jQuery('.dp-month option:selected').val() == MINday_month)
//		&& (jQuery('.dp-month option:selected').val() > MINday_date))
//	{
//		jQuery('#error-deliverydate-box').empty();	
//	}

//    jQuery('.dp-date option').attr('disabled', ''); 
//    jQuery('.dp-date option:gt(' + (daysInMonth - 1) +')').attr('disabled', 'disabled'); 
//    if (jQuery('.dp-date option').val() > daysInMonth) { 
//        jQuery('.dp-date').val(daysInMonth); 
//    } 
}

jQuery(document).ready(function($){
//	jQuery("#content").append('datepicker exists? '+jQuery(".datepicker").length);
	jQuery = $.noConflict();
	if(jQuery(".datepicker").length != 0){
	startdate_ = jQuery('#datepicker-startdate').val();
	$(".datepicker").datepicker({
//		minDate: jQuery('#datepicker-startdate').val(),
		minDate: '+'+startdate_+'d',
		maxDate: '+365d',
	 	dateFormat: "yymmdd", 
	    showOn: "both", 
	    buttonImage: "/wp-content/plugins/store/js/ui/images/calendar.gif",
	    buttonImageOnly: true 
	//    minDate: new Date(2001, 1 - 1, 1), 
	//    maxDate: new Date(2010, 12 - 1, 31), 
//	    beforeShow: jQuery.datepicker.readLinked, 
	//    onSelect: jQuery.datepicker.updateLinked, 
	//    beforeShow: readLinked, 
	//    onSelect: updateLinked, 
	});
	jQuery(".datepicker").change(function () {
//		jQuery("body").append('<div>fdasfasfasfas</div>');
//		jQuery("body").append('<div>'+jQuery('.datepicker').val().substring(6, 8)+'</div>');
		jQuery('.dp-year option').each(function () {
			if(jQuery(this).val() == jQuery('.datepicker').val().substring(0, 4)){
				jQuery(this).attr('selected','selected');
			}else{
				jQuery(this).removeAttr('selected');
			}
		});
		jQuery('.dp-month option').each(function () {
			if(jQuery(this).val() == jQuery('.datepicker').val().substring(4, 6)){
				jQuery(this).attr('selected','selected');
			}else{
				jQuery(this).removeAttr('selected');
			}
		});
		jQuery('.dp-date option').each(function () {
			if(jQuery(this).val() == jQuery('.datepicker').val().substring(6, 8)){
				jQuery(this).attr('selected','selected');
			}else{
				jQuery(this).removeAttr('selected');
			}
		});
		checkLinkedDays();
	});
    jQuery('.datepicker').val(jQuery('.dp-year option:selected').val()+ jQuery('.dp-month option:selected').val() 
    +jQuery('.dp-date option:selected').val()); 
	jQuery(".dp-year, .dp-month, .dp-date").change(function () {
		checkLinkedDays();
	    jQuery('.datepicker').val(jQuery('.dp-year option:selected').val()+ jQuery('.dp-month option:selected').val() 
	    +jQuery('.dp-date option:selected').val()); 
	});
	}
//	jQuery(".datepicker").dpSetStartDate(jQuery('#datepicker-startdate').val());
//	jQuery("#body-end").append(jQuery('<div>fdafassa: '+jQuery('#datepicker-startdate').val()+'</div>'));
//	jQuery(".datepicker").after(new Date().getFullYear()+'-12-31');
});

var dubug = jQuery('<div id="debug">debugging..</div>');







