<?	
class ShippingSchedule {
	var $time_diff;
	var $schedule;

var $earliest_date;
	var $charges;

	function ShippingSchedule(){


		//Configure possible earliest delivery date for an order as follows.  This system
		//allows for complex situations where stores need variable amounts of preparation time,etc. 
		//as well as simpler schemes.

		//The schedule array uses index 0-6 for each day of the week.  0 is sunday, 1 monday, etc., 
		//'start_time' is an integer representing the hour of the day (24 hour)


		//Each day uses the previous day's 'target date' as the earliest
		//possible date to deliver on until the 'start_time' specified -- at that point 

		//it will use it's own 'target date' as the earliest delivery day.

		//ex. an order at 12:25pm on Monday (schedule[1]), the earliest possible day to deliver  
		//will be 3 (Wednesday), but after 13:00 on Monday, the earliest poss. day will be 4 (Thurs).

		//A start time of 0 would indicate to always use the current day's 'target day'.  In this example,
		//any order on Sunday would have an earliest delivery date of Wednesday.

		$this->schedule[0]=array('start_time'=>0,'target_day'=>3);
		$this->schedule[1]=array('start_time'=>13,'target_day'=>4);
		$this->schedule[2]=array('start_time'=>13,'target_day'=>5);
		$this->schedule[3]=array('start_time'=>13,'target_day'=>6);
		$this->schedule[4]=array('start_time'=>15,'target_day'=>2);
		$this->schedule[5]=array('start_time'=>0,'target_day'=>2);
		$this->schedule[6]=array('start_time'=>14,'target_day'=>3);
	}

	function EarliestArrival(){
		$current_day=date('w');
		$current_time=date('G'); //any time of the hour will be considered past the hour
		$prev_day=($current_day!=0) ? $current_day-1 : 6;
		$target=($current_time>=$this->schedule[$current_day]['start_time']) ? $this->schedule[$current_day]['target_day'] : $this->schedule[$prev_day]['target_day'];
		$days_needed=($current_day<=$target) ? $target-$current_day : (7-$current_day)+$target;
		$this->earliest_date=mktime(0,0,0,date('m'),date('d')+$days_needed,date('Y'));
	}

	function DateStillValid($shipdate){
		if(!isset($this->earliest_date))$this->EarliestArrival();
		$result=($this->earliest_date>$shipdate) ? false : true;
		return $result;	
	}

	function DateInfo($datestamp){
	//This functions returns an array containing info about whether a date is valid for delivery
	//and the CSS class for the calendar.  The classes are defined in calendar.css
		//Currently Sunday is considered invalid and Saturday is a different class, 
	//although for now its class is defined the same as other valid days.
	
	//special days (holidays) construct an array of timestamps valid for a special day   to appear "valid"
           //2007: these dates must be modified each year  (format JJJJ-MM-TT) and according to your country!
           
           // First decide whether you want use special days
            $this->use_special_dates = false; // set to true if you want to use the special days

 $xmas06 = '2006-12-25';
 $boxing06 = '2006-12-26';
 $newyear = '2007-01-01';
 $goodfriday = '2007-04-06';
 $easter = '2007-04-09';
 $mayday = '2007-05-07';
 $pentecost = '2007-05-28';
 $augustbh = '2007-08-28';
 $xmas = '2007-12-25';
 $boxingday = '2007-12-26';
  //Ende eintragen

//change date to timestamp
 $xmas06_stamp = strtotime ($xmas06);
 $boxing06_stamp = strtotime ($boxing06);
 $newyear_stamp = strtotime ($newyear);
 $goodfriday = strtotime ($goodfriday);
 $easter_stamp = strtotime ($easter);
 $mayday_stamp = strtotime ($mayday);
 $pentecost_stamp = strtotime ($pentecost);
 $augustbh_stamp = strtotime ($augustbh);
 $xmas_stamp = strtotime ($xmas);
 $boxingday = strtotime ($boxingday);

//construct an array of timestamps
 $special_date = array ("$xmas06_stamp", "$boxing06_stamp", "$newyear_stamp", "$goodfriday", "$easter_stamp", "$mayday_stamp", "$pentecost_stamp", "$augustbh_stamp", "$xmas_stamp", "$boxingday");

//now check if the day selected in the calender belongs to the array of special dates and make it vaild (true) or non-valid (false), according to your needs		
		if(!isset($this->earliest_date))$this->EarliestArrival();
		if($datestamp>=$this->earliest_date){
                    if ( ($this->use_special_dates == true) && (in_array ("$datestamp", $special_date)) ) {                   
                    $result['valid']=false;                 
                     $result['class']='invalid';                      
                  } elseif(date('w',$datestamp)==6){
				$result['valid']=true;
				$result['class']='s_valid';
			}elseif(date('w',$datestamp)==0){
				$result['valid']=false;	
				$result['class']='invalid';
			}else{
				$result['valid']=true;
				$result['class']='valid';
			}
		}else{
			$result['valid']=false;
			$result['class']='invalid';
		}
		return $result;
	}
}
?>
