<?php
class PDF extends FPDF
{
//*********************************************************************************************
// Cabecera de página
function Header()
{
    $this->SetXY(0, 0);
    // Arial bold 15
    $this->SetFont('Arial','B',11);
    // Título
    $this->Cell(200,5,$_SESSION["EMPRESA"],0,0,'C');    
    // Logos
    $this->Image('../../principal/img/logoth.jpg',5,5,30,15);
    $this->Image("../../principal/img/logoth.jpg",180,5,30,15);

    // Salto de línea
    //$this->Ln(20);
    $this->SetXY(0, 25);

}

// Pie de página
function Footer()
{
    // Posición: a 1,5 cm del final
    $this->SetY(-15);
    // Arial italic 8
    $this->SetFont('Arial','I',8);
    // Número de página
    $this->Cell(0,10,'Pagina '.$this->PageNo().'/{nb}',0,0,'C');
}
//*******************************************************************************


//MYSQL
var $ProcessingTable=false;
var $aCols=array();
var $TableX;
var $HeaderColor;
var $RowColors;
var $ColorIndex;

function mysqlHeader()
{
	//Title
	$this->SetFont('Arial','',18);
	$this->Cell(0,6,'World populations',0,1,'C');
	//$this->Ln(10);
	//Ensure table header is output
	parent::mysqlHeader();
}
function amysqlHeader()
{
	//Print the table header if necessary
	if($this->ProcessingTable)
		$this->mysqlTableHeader();
		
}

function mysqlTableHeader()
{
	//$this->Ln(20);
	$this->SetFont('Arial','B',12);
	$this->SetX($this->TableX);
	$fill=!empty($this->HeaderColor);
	if($fill)
		$this->SetFillColor($this->HeaderColor[0],$this->HeaderColor[1],$this->HeaderColor[2]);
	foreach($this->aCols as $col)
		$this->Cell($col['w'],6,$col['c'],1,0,'C',$fill);
	$this->Ln();
}

function mysqlRow($data)
{
	$this->SetX($this->TableX);
	$ci=$this->ColorIndex;
	$fill=!empty($this->RowColors[$ci]);
	if($fill)
		$this->SetFillColor($this->RowColors[$ci][0],$this->RowColors[$ci][1],$this->RowColors[$ci][2]);
	foreach($this->aCols as $col)
		$this->Cell($col['w'],5,$data[$col['f']],1,0,$col['a'],$fill);
	$this->Ln();
	$this->ColorIndex=1-$ci;
}

function mysqlCalcWidths($width,$align)
{
	//Compute the widths of the columns
	$TableWidth=0;
	foreach($this->aCols as $i=>$col)
	{
		$w=$col['w'];
		if($w==-1)
			$w=$width/count($this->aCols);
		elseif(substr($w,-1)=='%')
			$w=$w/100*$width;
		$this->aCols[$i]['w']=$w;
		$TableWidth+=$w;
	}
	//Compute the abscissa of the table
	if($align=='C')
		$this->TableX=max(($this->w-$TableWidth)/2,0);
	elseif($align=='R')
		$this->TableX=max($this->w-$this->rMargin-$TableWidth,0);
	else
		$this->TableX=$this->lMargin;
}

function mysqlAddCol($field=-1,$width=-1,$caption='',$align='L')
{
	//Add a column to the table
	if($field==-1)
		$field=count($this->aCols);
	$this->aCols[]=array('f'=>$field,'c'=>$caption,'w'=>$width,'a'=>$align);
}

function mysqlTable($query,$prop=array())
{
	//Issue query
	$res=mysql_query($query) or die('Error: '.mysql_error()."<BR>Query: $query");
	//Add all columns if none was specified
	if(count($this->aCols)==0)
	{
		$nb=mysql_num_fields($res);
		for($i=0;$i<$nb;$i++)
			$this->mysqlAddCol();
	}
	//Retrieve column names when not specified
	foreach($this->aCols as $i=>$col)
	{
		if($col['c']=='')
		{
			if(is_string($col['f']))
				$this->aCols[$i]['c']=ucfirst($col['f']);
			else
				$this->aCols[$i]['c']=ucfirst(mysql_field_name($res,$col['f']));
		}
	}
	//Handle properties
	if(!isset($prop['width']))
		$prop['width']=0;
	if($prop['width']==0)
		$prop['width']=$this->w-$this->lMargin-$this->rMargin;
	if(!isset($prop['align']))
		$prop['align']='C';
	if(!isset($prop['padding']))
		$prop['padding']=$this->cMargin;
	$cMargin=$this->cMargin;
	$this->cMargin=$prop['padding'];
	if(!isset($prop['HeaderColor']))
		$prop['HeaderColor']=array();
	$this->HeaderColor=$prop['HeaderColor'];
	if(!isset($prop['color1']))
		$prop['color1']=array();
	if(!isset($prop['color2']))
		$prop['color2']=array();
	$this->RowColors=array($prop['color1'],$prop['color2']);
	//Compute column widths
	$this->mysqlCalcWidths($prop['width'],$prop['align']);
	//Print header
	$this->mysqlTableHeader();
	//Print rows
	$this->SetFont('Arial','',11);
	$this->ColorIndex=0;
	$this->ProcessingTable=true;
	while($row=mysql_fetch_array($res))
		$this->mysqlRow($row);
	$this->ProcessingTable=false;
	$this->cMargin=$cMargin;
	$this->aCols=array();
}
/*******************************************************************************
*VARILBES PDF                                                                         *
*                                                                              *                                                  *
*******************************************************************************/
var $angle=0;
var $legends;
var $wLegend;
var $sum;
var $NbVal;
var $date;
var $squareHeight;
var $squareWidth;
var $longestMonth;
var $tinySquareSize;


 /*******************************************************************************
*                                                                         *
*                                                                              *                                                  *
*******************************************************************************/
// NUEVO EXTRA FPDP
function Rotate($angle,$x=-1,$y=-1)
{
	if($x==-1)
		$x=$this->x;
	if($y==-1)
		$y=$this->y;
	if($this->angle!=0)
		$this->_out('Q');
	$this->angle=$angle;
	if($angle!=0)
	{
		$angle*=M_PI/180;
		$c=cos($angle);
		$s=sin($angle);
		$cx=$x*$this->k;
		$cy=($this->h-$y)*$this->k;
		$this->_out(sprintf('q %.5F %.5F %.5F %.5F %.2F %.2F cm 1 0 0 1 %.2F %.2F cm',$c,$s,-$s,$c,$cx,$cy,-$cx,-$cy));
	}
}
	
function PieChart($w, $h, $data, $format, $colors=null)
	{
		$this->SetFont('Courier', '', 10);
		$this->SetLegends($data,$format);

		$XPage = $this->GetX();
		$YPage = $this->GetY();
		$margin = 2;
		$hLegend = 5;
		$radius = min($w - $margin * 4 - $hLegend - $this->wLegend, $h - $margin * 2);
		$radius = floor($radius / 2);
		$XDiag = $XPage + $margin + $radius;
		$YDiag = $YPage + $margin + $radius;
		if($colors == null) {
			for($i = 0; $i < $this->NbVal; $i++) {
				$gray = $i * intval(255 / $this->NbVal);
				$colors[$i] = array($gray,$gray,$gray);
			}
		}

		//Sectors
		$this->SetLineWidth(0.2);
		$angleStart = 0;
		$angleEnd = 0;
		$i = 0;
		foreach($data as $val) {
			$angle = ($val * 360) / doubleval($this->sum);
			if ($angle != 0) {
				$angleEnd = $angleStart + $angle;
				$this->SetFillColor($colors[$i][0],$colors[$i][1],$colors[$i][2]);
				$this->Sector($XDiag, $YDiag, $radius, $angleStart, $angleEnd);
				$angleStart += $angle;
			}
			$i++;
		}

		//Legends
		$this->SetFont('Courier', '', 10);
		$x1 = $XPage + 2 * $radius + 4 * $margin;
		$x2 = $x1 + $hLegend + $margin;
		$y1 = $YDiag - $radius + (2 * $radius - $this->NbVal*($hLegend + $margin)) / 2;
		for($i=0; $i<$this->NbVal; $i++) {
			$this->SetFillColor($colors[$i][0],$colors[$i][1],$colors[$i][2]);
			$this->Rect($x1, $y1, $hLegend, $hLegend, 'DF');
			$this->SetXY($x2,$y1);
			$this->Cell(0,$hLegend,$this->legends[$i]);
			$y1+=$hLegend + $margin;
		}
	}

	function BarDiagram($w, $h, $data, $format, $color=null, $maxVal=0, $nbDiv=4)
	{
		$this->SetFont('Courier', '', 10);
		$this->SetLegends($data,$format);

		$XPage = $this->GetX();
		$YPage = $this->GetY();
		$margin = 2;
		$YDiag = $YPage + $margin;
		$hDiag = floor($h - $margin * 2);
		$XDiag = $XPage + $margin * 2 + $this->wLegend;
		$lDiag = floor($w - $margin * 3 - $this->wLegend);
		if($color == null)
			$color=array(155,155,155);
		if ($maxVal == 0) {
			$maxVal = max($data);
		}
		$valIndRepere = ceil($maxVal / $nbDiv);
		$maxVal = $valIndRepere * $nbDiv;
		$lRepere = floor($lDiag / $nbDiv);
		$lDiag = $lRepere * $nbDiv;
		$unit = $lDiag / $maxVal;
		$hBar = floor($hDiag / ($this->NbVal + 1));
		$hDiag = $hBar * ($this->NbVal + 1);
		$eBaton = floor($hBar * 80 / 100);

		$this->SetLineWidth(0.2);
		$this->Rect($XDiag, $YDiag, $lDiag, $hDiag);

		$this->SetFont('Courier', '', 10);
		$this->SetFillColor($color[0],$color[1],$color[2]);
		$i=0;
		foreach($data as $val) {
			//Bar
			$xval = $XDiag;
			$lval = (int)($val * $unit);
			$yval = $YDiag + ($i + 1) * $hBar - $eBaton / 2;
			$hval = $eBaton;
			$this->Rect($xval, $yval, $lval, $hval, 'DF');
			//Legend
			$this->SetXY(0, $yval);
			$this->Cell($xval - $margin, $hval, $this->legends[$i],0,0,'R');
			$i++;
		}

		//Scales
		for ($i = 0; $i <= $nbDiv; $i++) {
			$xpos = $XDiag + $lRepere * $i;
			$this->Line($xpos, $YDiag, $xpos, $YDiag + $hDiag);
			$val = $i * $valIndRepere;
			$xpos = $XDiag + $lRepere * $i - $this->GetStringWidth($val) / 2;
			$ypos = $YDiag + $hDiag - $margin;
			$this->Text($xpos, $ypos, $val);
		}
	}

	function SetLegends($data, $format)
	{
		$this->legends=array();
		$this->wLegend=0;
		$this->sum=array_sum($data);
		$this->NbVal=count($data);
		foreach($data as $l=>$val)
		{
			$p=sprintf('%.2f',$val/$this->sum*100).'%';
			$legend=str_replace(array('%l','%v','%p'),array($l,$val,$p),$format);
			$this->legends[]=$legend;
			$this->wLegend=max($this->GetStringWidth($legend),$this->wLegend);
		}
	}

function Sector($xc, $yc, $r, $a, $b, $style='FD', $cw=true, $o=90)
	{
		$d0 = $a - $b;
		if($cw){
			$d = $b;
			$b = $o - $a;
			$a = $o - $d;
		}else{
			$b += $o;
			$a += $o;
		}
		while($a<0)
			$a += 360;
		while($a>360)
			$a -= 360;
		while($b<0)
			$b += 360;
		while($b>360)
			$b -= 360;
		if ($a > $b)
			$b += 360;
		$b = $b/360*2*M_PI;
		$a = $a/360*2*M_PI;
		$d = $b - $a;
		if ($d == 0 && $d0 != 0)
			$d = 2*M_PI;
		$k = $this->k;
		$hp = $this->h;
		if (sin($d/2))
			$MyArc = 4/3*(1-cos($d/2))/sin($d/2)*$r;
		else
			$MyArc = 0;
		//first put the center
		$this->_out(sprintf('%.2F %.2F m',($xc)*$k,($hp-$yc)*$k));
		//put the first point
		$this->_out(sprintf('%.2F %.2F l',($xc+$r*cos($a))*$k,(($hp-($yc-$r*sin($a)))*$k)));
		//draw the arc
		if ($d < M_PI/2){
			$this->_Arc($xc+$r*cos($a)+$MyArc*cos(M_PI/2+$a),
						$yc-$r*sin($a)-$MyArc*sin(M_PI/2+$a),
						$xc+$r*cos($b)+$MyArc*cos($b-M_PI/2),
						$yc-$r*sin($b)-$MyArc*sin($b-M_PI/2),
						$xc+$r*cos($b),
						$yc-$r*sin($b)
						);
		}else{
			$b = $a + $d/4;
			$MyArc = 4/3*(1-cos($d/8))/sin($d/8)*$r;
			$this->_Arc($xc+$r*cos($a)+$MyArc*cos(M_PI/2+$a),
						$yc-$r*sin($a)-$MyArc*sin(M_PI/2+$a),
						$xc+$r*cos($b)+$MyArc*cos($b-M_PI/2),
						$yc-$r*sin($b)-$MyArc*sin($b-M_PI/2),
						$xc+$r*cos($b),
						$yc-$r*sin($b)
						);
			$a = $b;
			$b = $a + $d/4;
			$this->_Arc($xc+$r*cos($a)+$MyArc*cos(M_PI/2+$a),
						$yc-$r*sin($a)-$MyArc*sin(M_PI/2+$a),
						$xc+$r*cos($b)+$MyArc*cos($b-M_PI/2),
						$yc-$r*sin($b)-$MyArc*sin($b-M_PI/2),
						$xc+$r*cos($b),
						$yc-$r*sin($b)
						);
			$a = $b;
			$b = $a + $d/4;
			$this->_Arc($xc+$r*cos($a)+$MyArc*cos(M_PI/2+$a),
						$yc-$r*sin($a)-$MyArc*sin(M_PI/2+$a),
						$xc+$r*cos($b)+$MyArc*cos($b-M_PI/2),
						$yc-$r*sin($b)-$MyArc*sin($b-M_PI/2),
						$xc+$r*cos($b),
						$yc-$r*sin($b)
						);
			$a = $b;
			$b = $a + $d/4;
			$this->_Arc($xc+$r*cos($a)+$MyArc*cos(M_PI/2+$a),
						$yc-$r*sin($a)-$MyArc*sin(M_PI/2+$a),
						$xc+$r*cos($b)+$MyArc*cos($b-M_PI/2),
						$yc-$r*sin($b)-$MyArc*sin($b-M_PI/2),
						$xc+$r*cos($b),
						$yc-$r*sin($b)
						);
		}
		//terminate drawing
		if($style=='F')
			$op='f';
		elseif($style=='FD' || $style=='DF')
			$op='b';
		else
			$op='s';
		$this->_out($op);
	}

	function _Arc($x1, $y1, $x2, $y2, $x3, $y3 )
	{
		$h = $this->h;
		$this->_out(sprintf('%.2F %.2F %.2F %.2F %.2F %.2F c',
			$x1*$this->k,
			($h-$y1)*$this->k,
			$x2*$this->k,
			($h-$y2)*$this->k,
			$x3*$this->k,
			($h-$y3)*$this->k));
	}
function Circle($x, $y, $r, $style='D')
{
	$this->Ellipse($x,$y,$r,$r,$style);
}

function Ellipse($x, $y, $rx, $ry, $style='D')
{
	if($style=='F')
		$op='f';
	elseif($style=='FD' || $style=='DF')
		$op='B';
	else
		$op='S';
	$lx=4/3*(M_SQRT2-1)*$rx;
	$ly=4/3*(M_SQRT2-1)*$ry;
	$k=$this->k;
	$h=$this->h;
	$this->_out(sprintf('%.2F %.2F m %.2F %.2F %.2F %.2F %.2F %.2F c',
		($x+$rx)*$k,($h-$y)*$k,
		($x+$rx)*$k,($h-($y-$ly))*$k,
		($x+$lx)*$k,($h-($y-$ry))*$k,
		$x*$k,($h-($y-$ry))*$k));
	$this->_out(sprintf('%.2F %.2F %.2F %.2F %.2F %.2F c',
		($x-$lx)*$k,($h-($y-$ry))*$k,
		($x-$rx)*$k,($h-($y-$ly))*$k,
		($x-$rx)*$k,($h-$y)*$k));
	$this->_out(sprintf('%.2F %.2F %.2F %.2F %.2F %.2F c',
		($x-$rx)*$k,($h-($y+$ly))*$k,
		($x-$lx)*$k,($h-($y+$ry))*$k,
		$x*$k,($h-($y+$ry))*$k));
	$this->_out(sprintf('%.2F %.2F %.2F %.2F %.2F %.2F c %s',
		($x+$lx)*$k,($h-($y+$ry))*$k,
		($x+$rx)*$k,($h-($y+$ly))*$k,
		($x+$rx)*$k,($h-$y)*$k,
		$op));
}
function Polygon($points, $style='D')
{
	//Draw a polygon
	if($style=='F')
		$op = 'f';
	elseif($style=='FD' || $style=='DF')
		$op = 'b';
	else
		$op = 's';

	$h = $this->h;
	$k = $this->k;

	$points_string = '';
	for($i=0; $i<count($points); $i+=2){
		$points_string .= sprintf('%.2F %.2F', $points[$i]*$k, ($h-$points[$i+1])*$k);
		if($i==0)
			$points_string .= ' m ';
		else
			$points_string .= ' l ';
	}
	$this->_out($points_string . $op);
}

function PDF_USA_Calendar($orientation='L', $format="Letter")
{
	$this->FPDF($orientation, 'mm', $format);
	// compute longest month name
	$this->longestMonth = "";
	for ($month = 1; $month <= 12; ++$month)
		{
		$monthYear = gmdate("F Y", jdtounix($this->MDYtoJD($month,1,2009)));
		if (strlen($monthYear) > strlen($this->longestMonth))
			{
			$this->longestMonth = $monthYear;
			}
		}
	// compute font size
	$this->tinySquareSize = 4;
	$this->headerFontSize = 70;
	$this->SetFont("Times", "B", $this->headerFontSize);
	$width = $this->w - $this->lMargin - $this->rMargin;
	while ($this->GetStringWidth($this->longestMonth) > $width - $this->tinySquareSize * 22)
		{
		--$this->headerFontSize;
		$this->SetFont("Times", "B", $this->headerFontSize);
		}
}

// useful date manipulation routines

function JDtoYMD($date, &$year, &$month, &$day)
{
	$string = JDToGregorian($date);
	$month = strtok($string, " -/");
	$day = strtok(" -/");
	$year = strtok(" -/");
}

function MDYtoJD($month, $day, $year)
{
	if (! $month || ! $day || ! $year)
		return 0;
	$a = floor((14-$month)/12);
	$y = floor($year+4800-$a);
	$m = floor($month+12*$a-3);
	$jd = $day+floor((153*$m+2)/5)+$y*365;
	$jd += floor($y/4)-floor($y/100)+floor($y/400)-32045;
	return $jd;
}

function lastMonth($date)
{
	$this->JDtoYMD($date, $year, $month, $day);
	if (--$month == 0)
		{
		$month = 12;
		$year--;
		}
	return GregorianToJD($month, $day, $year);
}

function nextMonth($date)
{
	$this->JDtoYMD($date, $year, $month, $day);
	if (++$month > 12)
		{
		$month = 1;
		++$year;
		}
	return GregorianToJD($month, $day, $year);
}

function isHoliday($date)
{
	$this->JDtoYMD($date, $year, $month, $day);
	if ($month == 7 && $day == 4)
		return "Independence Day";
	if ($month == 1 && $day == 1)
		return "New Year's Day";
	if ($month == 12 && $day == 25)
		return "Christmas";
	if ($month == 11)
		{
		$dow = gmdate("w", jdtounix($date));
		if ($day == 11 && $dow > 0 && $dow < 6) // does the eleventh fall on a weekday?
			return "Veteran's Day";
		if ($dow == 1 && ($day == 12 || $day == 13))
			return "Veteran's Day";
		}
	if ($this->isWeekHoliday($date, 4, 4, 11)) // thursday of the fourth week of November
		return "Thanksgiving";
	if ($this->isWeekHoliday($date, 1, 3, 1))
		return "MLK, Jr. Day";
	if ($this->isWeekHoliday($date, 1, 3, 2))
		return "President's Day";
	if ($this->isWeekHoliday($date, 2, 1, 11))
		return "Election Day";
	if ($this->isWeekHoliday($date, 1, 1, 9))
		return "Labor Day";
	if ($this->isWeekHoliday($date, 1, 2, 10))
		return "Columbus Day";
	if ($this->isWeekHoliday($date, 1, 99, 5))
		return "Memorial Day";
	if ($this->isWeekHoliday($date, 0, 2, 5))
		return "Mother's Day";
	if ($this->isWeekHoliday($date, 0, 3, 6))
		return "Father's Day";
	return "";
}

function isWeekHoliday($date, $dayOfWeek, $weekOfMonth, $monthOfDate)
{
	$this->JDtoYMD($date, $year, $month, $day);
	if ($monthOfDate != $month)
		return 0;
	$jd = jdtounix($date);
	$dow = gmdate("w", $jd);
	if ($dow != $dayOfWeek)
	return 0;
	$daysInMonth = gmdate("t", $jd);
	if ($weekOfMonth > 5 && $day + 6 > $daysInMonth)
		return 1;
 	if ($day > ($weekOfMonth - 1) * 7 && $day <= ($weekOfMonth * 7))
		return 1;
	return 0;
}

function tinyCalendar($date, $square)
{
	$this->JDtoYMD($date, $year, $month, $day);
	// print numbers in boxes
	$wd=gmdate("w",jdtounix($date));
	$cur = $date - $wd;
	$this->SetFont("Helvetica", "B", 10);
	$monthStr = gmdate ("F", jdtounix($date))." $year";
	$this->JDtoYMD($date, $year, $month, $day);
	// save local copy of coordinates for future reference
	$x = $this->x;
	$y = $this->y;
	$this->Cell(7*$square, $square, $monthStr, 0, 0, "C");
	$y+=$square;
	$this->SetXY($x, $y);
	$this->SetFontSize(8);
	for ($i = 1; $i <= 7; ++$i)
		{
		$day = strtoupper(gmdate("l", jdtounix($this->MDYtoJD(2,$i,2009))));
		$this->Cell($square, $square, $day[0], 0,0,"C");
		}
	$y+=$square;
	$this->SetXY($x, $y);
	for ($k=0;$k<6;$k++)
		{
		for ($i=0;$i<7;$i++ )
			{
			$this->JDtoYMD($cur++, $curYear, $curMonth, $curDay);
			if ($curMonth != $month)
				$curDay = " ";
			$this->Cell($square, $square, $curDay, 0, 0, "R");
			}
		$y+=$square;
		$this->SetXY($x, $y);
		}
}

function printDay($date)
{
// nothing to do, can be overriden
	// add logic here to customize a day
	$this->JDtoYMD($date,$year,$month,$day);
	if ($month == 1 && $day == 10)
		{
		$this->SetXY($this->x, $this->y + $this->squareHeight / 2);
		$this->SetFont("Arial", "B", 10);
		$this->Cell($this->squareWidth,5,"Happy Birthday!", 0,0, "C");
		}
}

function printHoliday($date)
{
	$x = $this->x;
	$y = $this->y;
	$height = 5.5;
	if ($this->squareHeight < 50)
		$height = 4;
	$widthPercent = .92;
	$fontSize = 11;
	$holiday = $this->isHoliday($date);
	if (strlen($holiday))
		{
		$wd = gmdate("w",jdtounix($date));
		if ($wd != 0 && $wd != 6)
			$this->Cell($this->squareWidth, $this->squareHeight, "", 0, 0, "", true);
		$this->SetXY($x + $this->squareWidth * (1-$widthPercent)/2,$y + $this->squareHeight * 0.83);
		$this->SetFont("Helvetica", "B", $fontSize);
		$this->Cell($this->squareWidth * $widthPercent,$height,$holiday, 0, 0, "C");
		}
}

function printMonth($date)
{
	$this->date = $date;
	$this->JDtoYMD($date, $year, $month, $day);
	$this->AddPage();
	// compute size base on current settings
	$width = $this->w - $this->lMargin - $this->rMargin;
	$height = $this->h - $this->tMargin - $this->bMargin;
	// print prev and next calendars
	$this->setXY($this->lMargin,$this->tMargin);
	//$this->tinyCalendar($this->lastMonth($date), $this->tinySquareSize);
	$this->setXY($this->lMargin+$width - $this->tinySquareSize * 7,$this->tMargin);
	//$this->tinyCalendar($this->nextMonth($date), $this->tinySquareSize);
	// print header
	$firstLine = $this->tinySquareSize * 8 + $this->tMargin;
	$monthStr = strtoupper(gmdate ("F Y", jdtounix($date)));
	$this->SetXY($this->lMargin,$this->tMargin - 20);
	$this->SetFont("Times", "B", $this->headerFontSize = 30);
	$this->Cell($width, $firstLine, $monthStr, 0,0, "C");
	// compute number of weeks in month.
	$wd=gmdate("w",jdtounix($date));
	$start = $date - $wd;
	$numDays = $this->nextMonth($date) - $start;
	$numWeeks = 0;
	while ($numDays > 0)
		{
		$numDays -= 7;
		++$numWeeks;
		}
	// compute horizontal lines
	$this->squareHeight = ($height - 6 - $firstLine) / $numWeeks;
	$horizontalLines = array($firstLine,6);
	for ($i = 0; $i < $numWeeks; ++$i)
		{
		$horizontalLines[$i + 2] = $this->squareHeight;
		}
	// compute vertical lines
	$this->squareWidth = $width / 7;
	$verticalLines = array($this->lMargin, $this->squareWidth, $this->squareWidth, $this->squareWidth, $this->squareWidth, $this->squareWidth, $this->squareWidth, $this->squareWidth);
	
	// print days of week
	$x = 0;
	$this->SetFont("Times", "B", 12);
	for ($i = 1; $i <= 7; ++$i)
		{
		$x += $verticalLines[$i-1];
		$this->SetXY($x,$firstLine);
		$day = gmdate("l", jdtounix($this->MDYtoJD(2,$i,2009)));
		$this->Cell($verticalLines[$i],6,$day,0,0,"C");
		}
	
	// print numbers in boxes
	$wd=gmdate("w",jdtounix($date));
	$cur = $date - $wd;
	$this->SetFont("Times", "B", 20);
	$x = 0;
	$y = $horizontalLines[0];
	for ($k=0;$k<$numWeeks;$k++)
		{
		$y += $horizontalLines[$k+1];
		for ($i=0;$i<7;$i++ )
			{
			$this->JDtoYMD($cur, $curYear, $curMonth, $curDay);
			$x += $verticalLines[$i];
			$this->squareWidth = $verticalLines[$i+1];
			if ($curMonth == $month)
				{
				$this->setXY($x, $y);
				$this->printHoliday($cur);
				$this->setXY($x, $y);
				$this->printDay($cur);
				$this->SetFont("Times", "B", 20);
				$this->SetXY($x,$y+1);
				$this->Cell(5, 5, $curDay);
				}
			++$cur;
			}
		$x = 0;
		}
	
	// print horizontal lines
	$ly = 0;
	$ry = 0;
	foreach ($horizontalLines as $key => $value)
		{
		$ly += $value;
		$ry += $value;
		$this->Line($this->lMargin,$ly,$this->lMargin+$width,$ry);
		}
	// print vertical lines
	$lx = 0;
	$rx = 0;
	foreach ($verticalLines as $key => $value)
		{
		$lx += $value;
		$rx += $value;
		$this->Line($lx,$firstLine,$rx,$firstLine + 6 + $this->squareHeight * $numWeeks);
		}
} 

//line graph

/***********************************************************************************************************

This line graph function was developed by Anthony Master

***********************************************************************************************************/
function LineGraph($w, $h, $data, $options='', $colors=null, $maxVal=0, $nbDiv=4){
		/*******************************************
		Explain the variables:
		$w = the width of the diagram
		$h = the height of the diagram
		$data = the data for the diagram in the form of a multidimensional array
		$options = the possible formatting options which include:
			'V' = Print Vertical Divider lines
			'H' = Print Horizontal Divider Lines
			'kB' = Print bounding box around the Key (legend)
			'vB' = Print bounding box around the values under the graph
			'gB' = Print bounding box around the graph
			'dB' = Print bounding box around the entire diagram
		$colors = A multidimensional array containing RGB values
		$maxVal = The Maximum Value for the graph vertically
		$nbDiv = The number of vertical Divisions
		*******************************************/
		$this->SetDrawColor(0,0,0);
		$this->SetLineWidth(0.2);
		$keys = array_keys($data);
		$ordinateWidth = 10;
		$w -= $ordinateWidth;
		$valX = $this->getX()+$ordinateWidth;
		$valY = $this->getY();
		$margin = 1;
		$titleH = 8;
		$titleW = $w;
		$lineh = 5;
		$keyH = count($data)*$lineh;
		$keyW = $w/5;
		$graphValH = 5;
		$graphValW = $w-$keyW-3*$margin;
		$graphH = $h-(3*$margin)-$graphValH;
		$graphW = $w-(2*$margin)-($keyW+$margin);
		$graphX = $valX+$margin;
		$graphY = $valY+$margin;
		$graphValX = $valX+$margin;
		$graphValY = $valY+2*$margin+$graphH;
		$keyX = $valX+(2*$margin)+$graphW;
		$keyY = $valY+$margin+.5*($h-(2*$margin))-.5*($keyH);
		//draw graph frame border
		if(strstr($options,'gB')){
			$this->Rect($valX,$valY,$w,$h);
		}
		//draw graph diagram border
		if(strstr($options,'dB')){
			$this->Rect($valX+$margin,$valY+$margin,$graphW,$graphH);
		}
		//draw key legend border
		if(strstr($options,'kB')){
			$this->Rect($keyX,$keyY,$keyW,$keyH);
		}
		//draw graph value box
		if(strstr($options,'vB')){
			$this->Rect($graphValX,$graphValY,$graphValW,$graphValH);
		}
		//define colors
		if($colors===null){
			$safeColors = array(0,51,102,153,204,225);
			for($i=0;$i<count($data);$i++){
				$colors[$keys[$i]] = array($safeColors[array_rand($safeColors)],$safeColors[array_rand($safeColors)],$safeColors[array_rand($safeColors)]);
			}
		}
		//form an array with all data values from the multi-demensional $data array
		$ValArray = array();
		foreach($data as $key => $value){
			foreach($data[$key] as $val){
				$ValArray[]=$val;					
			}
		}
		//define max value
		if($maxVal<ceil(max($ValArray))){
			$maxVal = ceil(max($ValArray));
		}
		//draw horizontal lines
		$vertDivH = $graphH/$nbDiv;
		if(strstr($options,'H')){
			for($i=0;$i<=$nbDiv;$i++){
				if($i<$nbDiv){
					$this->Line($graphX,$graphY+$i*$vertDivH,$graphX+$graphW,$graphY+$i*$vertDivH);
				} else{
					$this->Line($graphX,$graphY+$graphH,$graphX+$graphW,$graphY+$graphH);
				}
			}
		}
		//draw vertical lines
		$horiDivW = floor($graphW/(count($data[$keys[0]])-1));
		if(strstr($options,'V')){
			for($i=0;$i<=(count($data[$keys[0]])-1);$i++){
				if($i<(count($data[$keys[0]])-1)){
					$this->Line($graphX+$i*$horiDivW,$graphY,$graphX+$i*$horiDivW,$graphY+$graphH);
				} else {
					$this->Line($graphX+$graphW,$graphY,$graphX+$graphW,$graphY+$graphH);
				}
			}
		}
		//draw graph lines
		foreach($data as $key => $value){
			$this->setDrawColor($colors[$key][0],$colors[$key][1],$colors[$key][2]);
			$this->SetLineWidth(0.8);
			$valueKeys = array_keys($value);
			for($i=0;$i<count($value);$i++){
				if($i==count($value)-2){
					$this->Line(
						$graphX+($i*$horiDivW),
						$graphY+$graphH-($value[$valueKeys[$i]]/$maxVal*$graphH),
						$graphX+$graphW,
						$graphY+$graphH-($value[$valueKeys[$i+1]]/$maxVal*$graphH)
					);
				} else if($i<(count($value)-1)) {
					$this->Line(
						$graphX+($i*$horiDivW),
						$graphY+$graphH-($value[$valueKeys[$i]]/$maxVal*$graphH),
						$graphX+($i+1)*$horiDivW,
						$graphY+$graphH-($value[$valueKeys[$i+1]]/$maxVal*$graphH)
					);
				}
			}
			//Set the Key (legend)
			$this->SetFont('Courier','',10);
			if(!isset($n))$n=0;
			$this->Line($keyX+1,$keyY+$lineh/2+$n*$lineh,$keyX+8,$keyY+$lineh/2+$n*$lineh);
			$this->SetXY($keyX+8,$keyY+$n*$lineh);
			$this->Cell($keyW,$lineh,$key,0,1,'L');
			$n++;
		}
		//print the abscissa values
		foreach($valueKeys as $key => $value){
			if($key==0){
				$this->SetXY($graphValX,$graphValY);
				$this->Cell(30,$lineh,$value,0,0,'L');
			} else if($key==count($valueKeys)-1){
				$this->SetXY($graphValX+$graphValW-30,$graphValY);
				$this->Cell(30,$lineh,$value,0,0,'R');
			} else {
				$this->SetXY($graphValX+$key*$horiDivW-15,$graphValY);
				$this->Cell(30,$lineh,$value,0,0,'C');
			}
		}
		//print the ordinate values
		for($i=0;$i<=$nbDiv;$i++){
			$this->SetXY($graphValX-10,$graphY+($nbDiv-$i)*$vertDivH-3);
			$this->Cell(8,6,sprintf('%.1f',$maxVal/$nbDiv*$i),0,0,'R');
		}
		$this->SetDrawColor(0,0,0);
		$this->SetLineWidth(0.2);
	}


}
?>