if($_POST['save']){
		if($_POST['recurrence'] == 'Monthly'){
			$start_ts = getTSFromInput($_POST['start_ts']);
			$end_ts = getTSFromInput($_POST['end_ts']);
			$created_ps = SI_PaymentSchedule::generateScheduledPayments($_POST['project_id'], $_POST['task_id'], $_POST['recurrence'], $start_ts, $end_ts, $_POST['item_code_id'], $_POST['description'], $_POST['amount']);
			if($created_ps === FALSE){
				$error_msg .= "Error creating scheduled payments!\n";
			}else{
				//var_dump($created_ps);
				goBack();
			}
		}else{
			$_POST['due_ts'] = getTSFromInput($_POST['due_ts']);
			$ps->updateFromAssocArray($_POST);
			if($ps->add()){
				goBack();
			}else{
				$error_msg .= "Error adding Payment Schedule!\n";
				debug_message($ps->getLastError());
			}
		}
	}
}else if($_REQUEST['mode'] == 'edit'){
	$title = "Edit Payment Schedule";
	if(empty($_REQUEST['id'])){
		$error_msg .= "Error: No ID specified!\n";
	}else{
		if(!$ps->get($_REQUEST['id'])){
			$error_msg .= "Error retrieving payment information!\n";
			debug_message($ps->getLastError());
	function generateScheduledPayments($project_id, $task_id, $frequency, $start_ts, $end_ts, $item_code, $description_format, $amount){
		$months = array( 1 => 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec');
		$scheduled_payments = array();
		if($frequency == 'Monthly'){
			$dom = (int)date('d', $start_ts);
			$start_month = (int)date('m', $start_ts);
			$start_year = (int)date('Y', $start_ts);
			$end_month = (int)date('m', $end_ts);
			$end_year = (int)date('Y', $end_ts);
			$current_month = $start_month;
			$current_year = $start_year;
			//print("From $start_month/$start_year to $end_month/$end_year on the $dom\n");
			while($current_year < $end_year || 
			$current_month <= $end_month && $current_year == $end_year){
				//print("Creating payment for $current_month/$dom/$current_year\n");
				$ps = new SI_PaymentSchedule();
				if($task_id > 0){
					$ps->task_id = $task_id;
					$task = $ps->getTask();
					$ps->project_id = $task->project_id;
				}else{
					$ps->project_id = $project_id;
				}
				$ps->amount = $amount;
				$ps->due_ts = strtotime($current_year.'-'.$current_month.'-'.$dom);
				if(stristr($description_format, '|MONTH|')){
					$ps->description = str_replace('|MONTH|', $months[(int)$current_month], $description_format);
				}else{
					$ps->description = $description_format;
				}
				$ps->item_code_id = $item_code;
				if($ps->add()){
					$scheduled_payments[] = $ps;
				}else{
					return FALSE;
				}
				if($current_month == 12){
					$current_month = 1;
					$current_year++;
				}else{
					$current_month++;
				}
				//print("Next date $current_month/$dom/$current_year\n");
			}
		}
		
		return $scheduled_payments;
	}