<?php
define(MSC_SYSTYPENO,320);
define(SMSC_SYSTYPENO,350);
define(VSS_SYSTYPENO,390);
define(AAS_SYSTYPENO,311);
define(SMEG_SYSTYPENO,355);
define(OPPS_SYSTYPENO,375);
define(PALIM_SYSTYPENO,378);
define(RLS_SYSTYPENO,379);
define(MRFC_SYSTYPENO,385);
define(MHC_SYSTYPENO,386);
define(iGRS_SYSTYPENO,387);
define(TMG_SYSTYPENO,391);
define(AMRMG_SYSTYPENO,392);
function GetCstaConf($sysTypeNo)
{
	$sql="select recordTable,sumTable,cstaType,fieldList,detailToSum,sumUp,id1Start,id1End,id1Offset,id1Field,id2Field,id2Start,id2End from CSTA_DB.cstaDefine where sysTypeNo=$sysTypeNo and isEnable=1 order by objIndex ";
	$res=mysqli_query($pubConn,$sql);
	$num=mysqli_num_rows($res);
	
	$cstaConf[DataObjectCount]=$num;
	for($i=0;$i<$num;$i++)
	{
		$row=@mysqli_fetch_array($res);
		$cstaConf[DataObject][$i][DetailDataTable]=$row[0]."Data";
		$cstaConf[DataObject][$i][SumDataTable]=$row[1]."Data";
		$cstaConf[DataObject][$i][KeyFieldCount]=$row[2]+0;
		$cstaConf[DataObject][$i][DetailFieldList]=$row[3];
		$cstaConf[DataObject][$i][DetailFieldCount]=sizeof(explode(",",$cstaConf[DataObject][$i][DetailFieldList]));
		$cstaConf[DataObject][$i][SumFlag]=$row[5]+0;
		$cstaConf[DataObject][$i][KeyOneStart]=$row[6]+0+$row[8];
		$cstaConf[DataObject][$i][KeyOneEnd]=$row[7]+0+$row[8];
		$cstaConf[DataObject][$i][KeyOneName]=$row[9];
		$cstaConf[DataObject][$i][KeyTwoName]=$row[10];
		$cstaConf[DataObject][$i][KeyTwoStart]=$row[11];
		$cstaConf[DataObject][$i][KeyTwoEnd]=$row[12];
		if(trim(strlen($row[4])) > 0)
		{
			$sqlm="select distinct sumFieldList from CSTA_DB.{$row[4]}";
			//echo "$sqlm<br>";
			$resm=mysqli_query($pubConn,$sqlm);
			$numm=mysqli_num_rows($resm);
			$cstaConf[DataObject][$i][SumFieldList]="";
			$cstaConf[DataObject][$i][SumFieldCount]=$numm;
			for($j=0;$j<$numm;$j++)
			{
				$rowm=@mysqli_fetch_array($resm);
				$cstaConf[DataObject][$i][SumFieldList].=$rowm[0];
				if($j != ($numm-1))
					$cstaConf[DataObject][$i][SumFieldList].=",";
			}
		}
		else
		{
			if($cstaConf[DataObject][$i][SumFlag])
			{
				$cstaConf[DataObject][$i][SumFieldList]=$cstaConf[DataObject][$i][DetailFieldList];
			}
		}
	}
	
	return $cstaConf;
}

/*
$cstaConf:	Return by function GetCstaConf
$startTime:	The time start to get the csta data
$endTime:		The time end to get the csta data
$dataType:	sum or detail
$keyOneCondition: specify the KeyOneValue
#condition: specil the other condition
return:			[DataObjectIndex][KeyOneIndex][KeyTwoIndex]
*/
function GetCstaDataByDay($cstaConf,$startTime,$endTime,$dataType,$keyOneCondition=-1,$condition="")
{ 
	for($i=0;$i<$cstaConf[DataObjectCount];$i++)
	{
		if($keyOneCondition != -1)
		{
			if(!($keyOneCondition >= $cstaConf[DataObject][$i][KeyOneStart] && $keyOneCondition <= $cstaConf[DataObject][$i][KeyOneEnd]))
				continue;
		}
		//sum
		if($dataType == "sum")
		{
			$dataTable=$cstaConf[DataObject][$i][SumDataTable];
			$fieldList=$cstaConf[DataObject][$i][SumFieldList];
			$fieldCount=$cstaConf[DataObject][$i][SumFieldCount];
		}
		//detail
		else
		{
			$dataTable=$cstaConf[DataObject][$i][DetailDataTable];
			$fieldList=$cstaConf[DataObject][$i][DetailFieldList];
			$fieldCount=$cstaConf[DataObject][$i][DetailFieldCount];
		}
		
		if($cstaConf[DataObject][$i][KeyFieldCount] == 1)
		{
			$fieldList.=",".$cstaConf[DataObject][$i][KeyOneName];
		}
		else if($cstaConf[DataObject][$i][KeyFieldCount] == 2)
		{
			$fieldList.=",".$cstaConf[DataObject][$i][KeyOneName];
			$fieldList.=",".$cstaConf[DataObject][$i][KeyTwoName];
		}
		
		if($keyOneCondition != -1)
		{
			$keyCondition=" and {$cstaConf[DataObject][$i][KeyOneName]} = '$keyOneCondition'";
		}
		else
		{
			if($cstaConf[DataObject][$i][KeyFieldCount] > 0)
				$keyCondition="and {$cstaConf[DataObject][$i][KeyOneName]} >= '{$cstaConf[DataObject][$i][KeyOneStart]}' and {$cstaConf[DataObject][$i][KeyOneName]} <= '{$cstaConf[DataObject][$i][KeyOneEnd]}'";
			else
				$keyCondition="";
		}
		
		$sql="select {$fieldList} from CSTA_DB.{$dataTable} where csta_datetime >= '{$startTime}' and csta_datetime <= '{$endTime}' {$keyCondition} ";
		if(strcmp($condition,""))
			$sql.=" and $condition ";
		//echo "$sql<br>";
		$res=mysqli_query($pubConn,$sql);
		$num=mysqli_num_rows($res);
		$tmpfield=explode(",",$fieldList);
		for($j=0;$j<$num;$j++)
		{
			$row=@mysqli_fetch_array($res);
			
			if($cstaConf[DataObject][$i][KeyFieldCount] == 2)
			{
				for($z=0;$z<$fieldCount;$z++)
				{
					$cstaData[$i][$row[$fieldCount]][$row[$fieldCount+1]][trim($tmpfield[$z])]+=$row[$z]+0;
				}
			}
			else if($cstaConf[DataObject][$i][KeyFieldCount] == 1)
			{
				for($z=0;$z<$fieldCount;$z++)
					$cstaData[$i][$row[$fieldCount]][trim($tmpfield[$z])]+=$row[$z]+0;
			}
			else 
			{
				for($z=0;$z<$fieldCount;$z++)
					$cstaData[$i][trim($tmpfield[$z])]+=$row[$z]+0;
			}
		}
	}
	
	return $cstaData;
}

function GetCstaDataByHour($cstaConf,$startTime,$endTime,$dataType,$keyOneCondition=-1,$keyTwoCondition=-1)
{ 
	for($i=0;$i<$cstaConf[DataObjectCount];$i++)
	{
		if($keyOneCondition != -1)
		{
			if(!($keyOneCondition >= $cstaConf[DataObject][$i][KeyOneStart] && $keyOneCondition <= $cstaConf[DataObject][$i][KeyOneEnd]))
				continue;
		}
		//sum
		if($dataType == "sum")
		{
			$dataTable=$cstaConf[DataObject][$i][SumDataTable];
			$fieldList=$cstaConf[DataObject][$i][SumFieldList];
			$fieldCount=$cstaConf[DataObject][$i][SumFieldCount];
		}
		//detail
		else
		{
			$dataTable=$cstaConf[DataObject][$i][DetailDataTable];
			$fieldList=$cstaConf[DataObject][$i][DetailFieldList];
			$fieldCount=$cstaConf[DataObject][$i][DetailFieldCount];
		}
		
		if($cstaConf[DataObject][$i][KeyFieldCount] == 1)
		{
			$fieldList.=",".$cstaConf[DataObject][$i][KeyOneName];
		}
		else if($cstaConf[DataObject][$i][KeyFieldCount] == 2)
		{
			$fieldList.=",".$cstaConf[DataObject][$i][KeyOneName];
			$fieldList.=",".$cstaConf[DataObject][$i][KeyTwoName];
		}
		
		$fieldList.=",csta_datetime";
		
		if($keyOneCondition != -1)
		{
			$keyCondition=" and {$cstaConf[DataObject][$i][KeyOneName]} = '$keyOneCondition'";
		}
		else
		{
			if($cstaConf[DataObject][$i][KeyFieldCount] > 0)
				$keyCondition="and {$cstaConf[DataObject][$i][KeyOneName]} >= '{$cstaConf[DataObject][$i][KeyOneStart]}' and {$cstaConf[DataObject][$i][KeyOneName]} <= '{$cstaConf[DataObject][$i][KeyOneEnd]}'";
			else
				$keyCondition="";
		}
		
		$keyTCondition="";
		if($keyTwoCondition != -1)
		{
			$keyTCondition="and {$cstaConf[DataObject][$i][KeyTwoName]} = '$keyTwoCondition'";
		}
		
		$sql="select {$fieldList} from CSTA_DB.{$dataTable} where csta_datetime >= '{$startTime}' and csta_datetime <= '{$endTime}' {$keyCondition} {$keyTCondition} ";
		//echo "$sql<br>";
		$res=mysqli_query($pubConn,$sql);
		$num=mysqli_num_rows($res);
		$tmpfield=explode(",",$fieldList);
		for($j=0;$j<$num;$j++)
		{
			$row=@mysqli_fetch_array($res);
			
			if($cstaConf[DataObject][$i][KeyFieldCount] == 2)
			{
				$rowtime=$row[$fieldCount+2];
				$start=strtotime($startTime);
				$rowt=strtotime($rowtime);
				$dif=$rowt-$start;
				$instance=floor($dif/(60*60));
				
				for($z=0;$z<$fieldCount;$z++)
					$cstaData[$i][$row[$fieldCount]][$row[$fieldCount+1]][trim($tmpfield[$z])][$instance]+=$row[$z]+0;
			}
			else if($cstaConf[DataObject][$i][KeyFieldCount] == 1)
			{
				$rowtime=$row[$fieldCount+1];
				$start=strtotime($startTime);
				$rowt=strtotime($rowtime);
				$dif=$rowt-$start;
				$instance=floor($dif/(60*60));
				
				for($z=0;$z<$fieldCount;$z++)
					$cstaData[$i][$row[$fieldCount]][trim($tmpfield[$z])][$instance]+=$row[$z]+0;
			}
			else 
			{
				$rowtime=$row[$fieldCount];
				$start=strtotime($startTime);
				$rowt=strtotime($rowtime);
				$dif=$rowt-$start;
				$instance=floor($dif/(60*60));
				
				for($z=0;$z<$fieldCount;$z++)
					$cstaData[$i][trim($tmpfield[$z])][$instance]+=$row[$z]+0;
			}
		}
	}
	
	return $cstaData;
}

define(X,0);
define(Y,1);
define(WIDTH,0);
define(HEIGHT,1);
define(R,0);
define(G,1);
define(B,2);
function CreateImage($width,$height)
{
	$image=imagecreate($width,$height);
	$bgcolor="F8F8F8";
	$fgcolor="000000";
	$background_color=ImageColorAllocate($image,hexdec(substr($bgcolor,0,2)),hexdec(substr($bgcolor,2,2)),hexdec(substr($bgcolor,4,2)));	
	ImageFilledRectangle($image,0,0,$width,$height,ImageColorAllocate($image,198,198,198));
	ImageFilledRectangle($image,2,2,$width,$height,ImageColorAllocate($image,130,130,130));
	$pointArr=array(0,$height-1,3,$height-4,4,$height-1);
	Imagefilledpolygon($image,$pointArr,3,ImageColorAllocate($image,130,130,130));
	$pointArr=array($width-1,0,$width-4,3,$width,4);
	Imagefilledpolygon($image,$pointArr,3,ImageColorAllocate($image, 130, 130, 130));
	ImageFilledRectangle($image,2,2,$width-3,$height-3,$background_color);
	return $image;
}
/*
$image: The image resource id return from DrawPercentHistogram function
$valueArray:{50,140,200,220}
$colorArray:{{0xff,0x00,0x00},{0x00,0xff,0x00},{0x00,0x00,0xff},{0xff,0xff,0x00}}
$startPoint:{20,20}
$chatSize: {800,600}
$leftPercent: 1
$rightPercent:1
*/
function DrawPercentHistogram($image,$valueArray,$colorArray,$startPoint,$chatSize,$leftPercent=1,$rightPercent=1)
{
	$bgcolor="F8F8F8";
	$fgcolor="000000";
	$black_color=ImageColorAllocate($image,0,0,0);
	$gray_color=ImageColorAllocate($image,0xaa,0xaa,0xaa);
	$background_color=ImageColorAllocate($image,hexdec(substr($bgcolor,0,2)),hexdec(substr($bgcolor,2,2)),hexdec(substr($bgcolor,4,2)));	
	
	$count=sizeof($valueArray);
	$horiz_gap_count=2+$count+$count-1;
	$horiz_gap_width=$chatSize[WIDTH]/$horiz_gap_count;
	$virti_gap_count=10;
	$virti_gap_height=$chatSize[HEIGHT]/$virti_gap_count;
	
	//Draw horizontal line
	$style=array($gray_color,$gray_color,$gray_color,$gray_color,$gray_color,$white_color,$white_color,$white_color,$white_color,$white_color);
	imagesetstyle($image, $style);
	for($i=0;$i<10;$i++)
	{
		$x1=$startPoint[X];
		$y1=$startPoint[Y]+$virti_gap_height*$i;
		$x2=$startPoint[X]+$chatSize[WIDTH];
		$y2=$y1;
		imageline($image,$x1,$y1,$x2,$y2,IMG_COLOR_STYLED);
		if($leftPercent)
			imagestring($image,2,$x1-22,$y2-5,((10-$i)*10)."%",$black_color);
		if($rightPercent)
			imagestring($image,2,$x2+5,$y2-5,((10-$i)*10)."%",$black_color);
	}
	
	//Create color
	for($i=0;$i<$count;$i++)
		$colorSet[$i]=ImageColorAllocate($image,$colorArray[$i][R],$colorArray[$i][G],$colorArray[$i][B]);
	
	//Draw block
	for($i=0;$i<$count;$i++)
		$totalValue+=$valueArray[$i];
	if($totalValue==0)
	{
		for($i=0;$i<$count;$i++)
		{
			$block_height[$i]=1;
			$block_percent[$i]="0%";
		}
	}
	else
	{
		for($i=0;$i<$count;$i++)
		{
			$block_height[$i]=floor($chatSize[HEIGHT]*$valueArray[$i]/$totalValue);
			$block_percent[$i]=number_format($valueArray[$i]*100/$totalValue,1,'.','')."%";
		}
	}
	for($i=0;$i<$count;$i++)
	{
		$x1=$startPoint[X]+(2*$i+1)*$horiz_gap_width;
		$y1=$startPoint[Y]+$chatSize[HEIGHT]-$block_height[$i];
		$x2=$x1+$horiz_gap_width;
		$y2=$startPoint[Y]+$chatSize[HEIGHT];
		imagefilledrectangle($image,$x1,$y1,$x2,$y2,$colorSet[$i]);
		imagestring($image,2,$x1,$startPoint[Y]+$chatSize[HEIGHT]+2,$block_percent[$i],$black_color);
	}
	
	//Draw virtical line
	for($i=0;$i<$horiz_gap_count;$i++)
	{
		$x1=$startPoint[X]+$i*$horiz_gap_width;
		$y1=$startPoint[Y];
		$x2=$x1;
		$y2=$startPoint[Y]+$chatSize[HEIGHT];
		imageline($image,$x1,$y1,$x2,$y2,IMG_COLOR_STYLED);
	}
	
	//Draw outline
	imagerectangle($image,$startPoint[X],$startPoint[Y],$startPoint[X]+$chatSize[WIDTH],$startPoint[Y]+$chatSize[HEIGHT],$black_color);
	
	return $image;
}
/*
$image: The image resource id return from DrawPercentHistogram function
$valueArray:{{50,140,200},{220,40,80}}
$colorArray:{{0xff,0x00,0x00},{0x00,0xff,0x00},{0x00,0x00,0xff}}
$startPoint:{20,20}
$chatSize: {800,600}
$leftPercent: 1
$rightPercent:1
*/
function DrawStackPercentHistogram($image,$valueArray,$colorArray,$startPoint,$chatSize,$leftPercent,$rightPercent)
{
	$bgcolor="F8F8F8";
	$fgcolor="000000";
	$black_color=ImageColorAllocate($image,0,0,0);
	$gray_color=ImageColorAllocate($image,0xaa,0xaa,0xaa);
	$background_color=ImageColorAllocate($image,hexdec(substr($bgcolor,0,2)),hexdec(substr($bgcolor,2,2)),hexdec(substr($bgcolor,4,2)));	
	
	$count=sizeof($valueArray);
	$subcount=sizeof($valueArray[0]);
	
	$horiz_gap_count=2+$count+$count-1;
	$horiz_gap_width=$chatSize[WIDTH]/$horiz_gap_count;
	$virti_gap_count=10;
	$virti_gap_height=$chatSize[HEIGHT]/$virti_gap_count;
	
	//Draw horizontal line
	$style=array($gray_color,$gray_color,$gray_color,$gray_color,$gray_color,$white_color,$white_color,$white_color,$white_color,$white_color);
	imagesetstyle($image, $style);
	for($i=0;$i<10;$i++)
	{
		$x1=$startPoint[X];
		$y1=$startPoint[Y]+$virti_gap_height*$i;
		$x2=$startPoint[X]+$chatSize[WIDTH];
		$y2=$y1;
		imageline($image,$x1,$y1,$x2,$y2,IMG_COLOR_STYLED);
		if($leftPercent)
			imagestring($image,2,$x1-22,$y2-5,((10-$i)*10)."%",$black_color);
		if($rightPercent)
			imagestring($image,2,$x2+5,$y2-5,((10-$i)*10)."%",$black_color);
	}
	
	//Create color
	for($i=0;$i<$subcount;$i++)
		$colorSet[$i]=ImageColorAllocate($image,$colorArray[$i][R],$colorArray[$i][G],$colorArray[$i][B]);
	
	//Draw block
	for($i=0;$i<$count;$i++)
	{
		for($j=0;$j<$subcount;$j++)
		{	
			$totalValue+=$valueArray[$i][$j];
		}
	}
			
	if($totalValue==0)
	{
		for($i=0;$i<$count;$i++)
		{
			for($j=0;$j<$subcount;$j++)
			{
				$block_height[$i][$j]=0;
			}
		}
	}
	else
	{
		for($i=0;$i<$count;$i++)
		{
			for($j=0;$j<$subcount;$j++)
			{
				$block_height[$i][$j]=floor($chatSize[HEIGHT]*$valueArray[$i][$j]/$totalValue);
			}
		}
	}
	for($i=0;$i<$count;$i++)
	{
		$tmpHeight=0;
		for($j=0;$j<$subcount;$j++)
		{
			$x1=$startPoint[X]+(2*$i+1)*$horiz_gap_width;
			$y1=$startPoint[Y]+$chatSize[HEIGHT]-$tmpHeight;
			$x2=$x1+$horiz_gap_width;
			$y2=$y1-$block_height[$i][$j];
			$tmpHeight+=$block_height[$i][$j];
			imagefilledrectangle($image,$x1,$y1,$x2,$y2,$colorSet[$j]);
		}		
	}
	
	//Draw virtical line
	for($i=0;$i<$horiz_gap_count;$i++)
	{
		$x1=$startPoint[X]+$i*$horiz_gap_width;
		$y1=$startPoint[Y];
		$x2=$x1;
		$y2=$startPoint[Y]+$chatSize[HEIGHT];
		imageline($image,$x1,$y1,$x2,$y2,IMG_COLOR_STYLED);
	}
	
	//Draw outline
	imagerectangle($image,$startPoint[X],$startPoint[Y],$startPoint[X]+$chatSize[WIDTH],$startPoint[Y]+$chatSize[HEIGHT],$black_color);
	
	return $image;
}

/*
$image: The image resource id return from CreateImage function
						 |section-1         |section-2
						 |column-1  |column-2
						 |blk-1|block-2
$valueArray: {{0x11,0x22}{0x33}},{{0x22,0x33}{0x55}}
$colorArray: {{0xff,0x00,0x00},{0x00,0xff,0x00},{0x00,0x00,0xff}}
$pointInterval: The distance of each point
$gapCount: The count of the gap
$startPoint: {30,30}
$chatSize: {400,300}
$leftPercent: 1
$rightPercent: 1
*/
function DrawMuliStackPercentHistogram($image,$valueArray,$colorArray,$pointInterval,$gapCount,$startPoint,$chatSize,$leftPercent,$rightPercent)
{
	$bgcolor="F8F8F8";
	$fgcolor="000000";
	$black_color=ImageColorAllocate($image,0,0,0);
	$gray_color=ImageColorAllocate($image,0xaa,0xaa,0xaa);
	$background_color=ImageColorAllocate($image,hexdec(substr($bgcolor,0,2)),hexdec(substr($bgcolor,2,2)),hexdec(substr($bgcolor,4,2)));	
	
	$type_count=count($valueArray);
	$column_count=count($valueArray[0]);
	for($i=0;$i<$column_count;$i++)
	{
		$block_count[$i]=count($valueArray[0][$i]);
	}
	
	$horiz_gap_count=$type_count*2 + ($type_count+1)*$gapCount;
	$horiz_gap_width=$chatSize[WIDTH]/$horiz_gap_count;
	$virti_gap_count=10;
	$virti_gap_height=$chatSize[HEIGHT]/$virti_gap_count;
	
	$maxValue=0;
	for($i=0;$i<$type_count;$i++)
	{
		for($j=0;$j<$column_count;$j++)
		{
			$tmpValue=array_sum($valueArray[$i][$j]);
			if($tmpValue > $maxValue)
				$maxValue=$tmpValue;
		}
	}

	$len=strlen($maxValue);
	$maxValue=pow(10,$len);
	$unitValue=$maxValue/$virti_gap_count;
	
	
	if($unitValue >= 1000000000)
	{
		$unit_devide=1000000000;
		$unit_string=" x 10";
		$unit_up="9";
	}
	else if($unitValue >= 1000000)
	{
		$unit_devide=1000000;
		$unit_string=" M";
	}
	else if($unitValue >= 1000)
	{
		$unit_devide=1000;
		$unit_string=" K";
	}
	else
	{
		$unit_devide=1;
		$unit_string="";
	}
	
	//Draw horizontal line
	$style=array($gray_color,$gray_color,$gray_color,$gray_color,$gray_color,$white_color,$white_color,$white_color,$white_color,$white_color);
	imagesetstyle($image, $style);
	for($i=0;$i<10;$i++)
	{
		$x1=$startPoint[X];
		$y1=$startPoint[Y]+$virti_gap_height*$i;
		$x2=$startPoint[X]+$chatSize[WIDTH];
		$y2=$y1;
		imageline($image,$x1,$y1,$x2,$y2,IMG_COLOR_STYLED);
		if($leftPercent)
		{
			imagestring($image,2,$x1-22,$y2-5,(((10-$i)*$unitValue)/$unit_devide).$unit_string,$black_color);
		}
		if($rightPercent)
			imagestring($image,2,$x2+5,$y2-5,(((10-$i)*$unitValue)/$unit_devide).$unit_string,$black_color);
	}
	
	//Create color
	$color_count=count($colorArray);
	for($i=0;$i<$color_count;$i++)
		$colorSet[$i]=ImageColorAllocate($image,$colorArray[$i][R],$colorArray[$i][G],$colorArray[$i][B]);
	
	//Draw block
	for($i=0;$i<$type_count;$i++)
	{
		$color_index=0;
		for($j=0;$j<$column_count;$j++)
		{
			$tmpCount=count($valueArray[$i][$j]);
			$current_height=0;
			for($z=0;$z<$tmpCount;$z++)
			{
				$x1=$startPoint[X]+(2*$i+1)*$gapCount*$horiz_gap_width+$j*$horiz_gap_width;
				$x2=$x1+$horiz_gap_width;
				$y1=$startPoint[Y]+$chatSize[HEIGHT]-$current_height;
				$y2=$y1-$valueArray[$i][$j][$z]/$maxValue*$chatSize[HEIGHT];
				$current_height+=$valueArray[$i][$j][$z]/$maxValue*$chatSize[HEIGHT];
				//if($color_index == 0)
				imagefilledrectangle($image,$x1,$y1,$x2,$y2,$colorSet[$color_index]);
				$color_index++;
			}
		}
	}
	
	
	//Draw virtical line
	for($i=0;$i<$horiz_gap_count;$i++)
	{
		$x1=$startPoint[X]+$i*$horiz_gap_width;
		$y1=$startPoint[Y];
		$x2=$x1;
		$y2=$startPoint[Y]+$chatSize[HEIGHT];
		imageline($image,$x1,$y1,$x2,$y2,IMG_COLOR_STYLED);
	}
	
	//Draw outline
	imagerectangle($image,$startPoint[X],$startPoint[Y],$startPoint[X]+$chatSize[WIDTH],$startPoint[Y]+$chatSize[HEIGHT],$black_color);
	
	
	return $image;
}

/*
$image: The image resource id return from CreateImage function
$valueArray: {50,60,20,70,100},{50,60,20,70,100}
$colorArray: {{0xff,0x00,0x00},{0x00,0xff,0x00},{0x00,0x00,0xff}}
$pointInterval: The distance of each point
$gapCount: The count of the gap
$startPoint: {30,30}
$chatSize: {400,300}
$boldline: the line need to bold
$leftPercent: 1
$rightPercent: 1
$valuepoint: 0
*/
function DrawLineHistogram($image,$valueArray,$colorArray,$pointInterval,$gapCount,$startPoint,$chatSize,$boldline=0,$leftPercent=1,$rightPercent=1,$valuepoint=0)
{
	$bgcolor="F8F8F8";
	$fgcolor="000000";
	$black_color=ImageColorAllocate($image,0,0,0);
	$gray_color=ImageColorAllocate($image,0xaa,0xaa,0xaa);
	$blue_color=ImageColorAllocate($image,0x00,0x00,0xff);
	$background_color=ImageColorAllocate($image,hexdec(substr($bgcolor,0,2)),hexdec(substr($bgcolor,2,2)),hexdec(substr($bgcolor,4,2)));	
	
	$horiz_gap_count=$gapCount;
	$horiz_gap_width=$chatSize[WIDTH]/$horiz_gap_count;
	$virti_gap_count=10;
	$virti_gap_height=$chatSize[HEIGHT]/$virti_gap_count;
	
	$line_count=sizeof($valueArray);
	
	//Create color
	for($i=0;$i<$line_count;$i++)
		$colorSet[$i]=ImageColorAllocate($image,$colorArray[$i][R],$colorArray[$i][G],$colorArray[$i][B]);
	
	$maxValue=10;
	for($l=0;$l<$line_count;$l++)
	{
		$count[$l]=sizeof($valueArray[$l]);
		for($i=0;$i<$count[$l];$i++)
		{
			if($valueArray[$l][$i]>$maxValue)
			{
				$maxValue=$valueArray[$l][$i];
			}
		}
	}
	//$maxValue+=ceil($maxValue/10);
	$unitValue=$maxValue/10;
	
	//Draw horizontal line
	$style=array($gray_color,$gray_color,$gray_color,$gray_color,$gray_color,$white_color,$white_color,$white_color,$white_color,$white_color);
	imagesetstyle($image, $style);
	for($i=0;$i<11;$i++)
	{
		$x1=$startPoint[X];
		$y1=$startPoint[Y]+$virti_gap_height*$i;
		$x2=$startPoint[X]+$chatSize[WIDTH];
		$y2=$y1;
		imageline($image,$x1,$y1,$x2,$y2,IMG_COLOR_STYLED);
		
		if($unitValue*(10-$i) >= 1000)
			$show=number_format($unitValue*(10-$i)/1000, 1, '.', '')."K";	
		else if($unitValue*(10-$i) >= 1000000)
			$show=number_format($unitValue*(10-$i)/1000000, 1, '.', '')."M";
		else
			$show=$unitValue*(10-$i);
				
		if($leftPercent)
		{
			imagestring($image,2,$x1-30,$y2-5,$show,$black_color);
		}
		if($rightPercent)
		{
			imagestring($image,2,$x2+5,$y2-5,$show,$black_color);
		}
	}
	
	//Draw virtical line
	for($i=0;$i<$horiz_gap_count;$i++)
	{
		$x1=$startPoint[X]+$i*$horiz_gap_width;
		$y1=$startPoint[Y];
		$x2=$x1;
		$y2=$startPoint[Y]+$chatSize[HEIGHT];
		imageline($image,$x1,$y1,$x2,$y2,IMG_COLOR_STYLED);
	}
	
	//Draw outline
	imagerectangle($image,$startPoint[X],$startPoint[Y],$startPoint[X]+$chatSize[WIDTH],$startPoint[Y]+$chatSize[HEIGHT],$black_color);	
	
	//Draw line
	for($l=0;$l<$line_count;$l++)
	{
		for($i=0;$i<$count[$l];$i++)
		{
			$block_height[$i]=floor($chatSize[HEIGHT]*$valueArray[$l][$i]/$maxValue);
			$pointArray[$i][X]=$startPoint[X]+$i*$pointInterval;
			$pointArray[$i][Y]=$startPoint[Y]+$chatSize[HEIGHT]-$block_height[$i];
			//echo "{$block_height[$i]},{$chatSize[HEIGHT]},{$valueArray[$i]},{$maxValue}<br>";
		}
		for($i=0;$i<$count[$l]-1;$i++)
		{
			imageline($image,$pointArray[$i][X],$pointArray[$i][Y],$pointArray[$i+1][X],$pointArray[$i+1][Y],$colorSet[$l]);
			if($valuepoint && $valueArray[$l][$i+1]>0 && $count[$l] <= 24)
			{
				imagestring($image,1,$pointArray[$i+1][X]+4,$pointArray[$i+1][Y],$valueArray[$l][$i+1],$blue_color);
				imagefilledrectangle($image,$pointArray[$i+1][X]-1,$pointArray[$i+1][Y]-1,$pointArray[$i+1][X]+1,$pointArray[$i+1][Y]+1,$black_color);
			}
		}
	}
	
	if($boldline >= 0 && $boldline < $line_count)
	{
		$l=$boldline;
		for($i=0;$i<$count[$l];$i++)
		{
			$block_height[$i]=floor($chatSize[HEIGHT]*$valueArray[$l][$i]/$maxValue);
			$pointArray[$i][X]=$startPoint[X]+$i*$pointInterval;
			$pointArray[$i][Y]=$startPoint[Y]+$chatSize[HEIGHT]-$block_height[$i];
			//echo "{$block_height[$i]},{$chatSize[HEIGHT]},{$valueArray[$i]},{$maxValue}<br>";
		}
		for($i=0;$i<$count[$l]-1;$i++)
		{
			imageline($image,$pointArray[$i][X],$pointArray[$i][Y],$pointArray[$i+1][X],$pointArray[$i+1][Y],$colorSet[$l]);
			imageline($image,$pointArray[$i][X],$pointArray[$i][Y]+1,$pointArray[$i+1][X],$pointArray[$i+1][Y]+1,$colorSet[$l]);
			imageline($image,$pointArray[$i][X],$pointArray[$i][Y]+2,$pointArray[$i+1][X],$pointArray[$i+1][Y]+2,$colorSet[$l]);
		}
	}

	
	return $image;
}
/*
$image: The image resource id return from DrawPercentHistogram function
$sel_date_from: the date to start statistic
$sel_date_to: the date to end statistic
$startPoint: the position to draw the timestamp
$gapWidth: the width of the chart
*/
function DrawTimeStamp($image,$sel_date_from,$sel_date_to,$startPoint,$gapWidth)
{
	$black_color=ImageColorAllocate($image,0,0,0);
	$slot=$sel_date_from-$sel_date_to+1;
	$total_hour=$slot*24;
	$count=0;
	for($hour=0;$hour<$total_hour;$hour++)
	{
		if($hour%$slot == 0)
		{
			$hour_title=date("Y-m-d",mktime($hour,0,0,date("m"),date("d")-$sel_date_from,date("Y")));
			//echo "hour_title=$hour_title<br>";
			imagestringup($image,2,$startPoint[X]+$count*$gapWidth-5,$startPoint[Y],$hour_title,$black_color);
			$count++;
		}
	}
}
/*
$image: The image resource id return from DrawPercentHistogram function
$pointArr: {{40,80},{56,28}}
$remarkArray:{"SUB SUB","TRK TRK"}
$colorArray:0-not have color block,$colorArray:{{0xff,0x00,0x00},{0x00,0xff,0x00},{0x00,0x00,0xff}}
$fontStyle: 0-horizontal 1-vertical
*/
function AddRemarkToImage($image,$pointArray,$remarkArray,$colorArray,$fontStyle=0,$fontsize=2)
{
	$black_color=ImageColorAllocate($image,0,0,0);
	$count=sizeof($pointArray);
	//Create color
	for($i=0;$i<$count;$i++)
		$colorSet[$i]=ImageColorAllocate($image,$colorArray[$i][R],$colorArray[$i][G],$colorArray[$i][B]);
	
	$blocksize=8;
	
	if(is_array($colorArray))
	{
		for($i=0;$i<$count;$i++)
		{
			imagefilledrectangle($image,$pointArray[$i][X],$pointArray[$i][Y],$pointArray[$i][X]+$blocksize,$pointArray[$i][Y]+$blocksize+2,$colorSet[$i]);
			if($fontStyle)
				imagestringup($image,$fontsize,2+$blocksize+$pointArray[$i][X],$pointArray[$i][Y]-1,$remarkArray[$i],$black_color);
			else
				imagestring($image,$fontsize,2+$blocksize+$pointArray[$i][X],$pointArray[$i][Y]-1,$remarkArray[$i],$black_color);
		}
	}
	else
	{
		for($i=0;$i<$count;$i++)
		{
			if($fontStyle)
				imagestringup($image,$fontsize,$pointArray[$i][X],$pointArray[$i][Y],$remarkArray[$i],$black_color);
			else
				imagestring($image,$fontsize,$pointArray[$i][X],$pointArray[$i][Y],$remarkArray[$i],$black_color);
		}
	}
	
	return $image;
}
$global_delete_flag=0;
function DrawImage($image)
{
	global $global_delete_flag;
	if($global_delete_flag==0)
	{
		$handle=opendir('.');
		while($file=readdir($handle)){
			$retval="";
			$pt=strrpos($file,".");
			if($pt) $retval=substr($file,$pt+1,strlen($file)-$pt);
			if($retval=='png') unlink($file);
		}
		$global_delete_flag=1;
	}
	
	//create image
	$imagefile=mt_rand().time().".png";
	imagepng($image,$imagefile);
	$tt=imagedestroy($image);
	echo "<img src=\"{$imagefile}\"></img>";
}

?>
<script language="JavaScript">
function select_line(g_id)
{
	switch(g_id)
	{
	case '0':
		if(document.myform.flag0.checked==1)
			document.myform.select_checkbox_id.value=g_id;
		break;
	case '1':
		if(document.myform.flag1.checked==1)
			document.myform.select_checkbox_id.value=g_id;
		break;
	case '2':
		if(document.myform.flag2.checked==1)
			document.myform.select_checkbox_id.value=g_id;
		break;
	case '3':
		if(document.myform.flag3.checked==1)
			document.myform.select_checkbox_id.value=g_id;
		break;
	case '4':
		if(document.myform.flag4.checked==1)
			document.myform.select_checkbox_id.value=g_id;
		break;
	case '5':
		if(document.myform.flag5.checked==1)
			document.myform.select_checkbox_id.value=g_id;
		break;
	case '6':
		if(document.myform.flag6.checked==1)
			document.myform.select_checkbox_id.value=g_id;
		break;
	case '7':
		if(document.myform.flag7.checked==1)
			document.myform.select_checkbox_id.value=g_id;
		break;
	case '8':
		if(document.myform.flag8.checked==1)
			document.myform.select_checkbox_id.value=g_id;
		break;
	case '9':
		if(document.myform.flag9.checked==1)
			document.myform.select_checkbox_id.value=g_id;
		break;
	case '10':
		if(document.myform.flag10.checked==1)
			document.myform.select_checkbox_id.value=g_id;
		break;
	case '11':
		if(document.myform.flag11.checked==1)
			document.myform.select_checkbox_id.value=g_id;
		break;
	case '12':
		if(document.myform.flag12.checked==1)
			document.myform.select_checkbox_id.value=g_id;
		break;
	case '13':
		if(document.myform.flag13.checked==1)
			document.myform.select_checkbox_id.value=g_id;
		break;
	case '14':
		if(document.myform.flag14.checked==1)
			document.myform.select_checkbox_id.value=g_id;
		break;
	case '15':
		if(document.myform.flag15.checked==1)
			document.myform.select_checkbox_id.value=g_id;
		break;
	case '16':
		if(document.myform.flag16.checked==1)
			document.myform.select_checkbox_id.value=g_id;
		break;
	}
	

	
	document.myform.scroll_y.value=document.getElementById('content_down').scrollTop;
	document.myform.submit();
}
</script>
<?php
$global_checkbox_id=0;
function AddCheckbox($label)
{
	global $global_checkbox_id;
	global $flag0,$flag1,$flag2,$flag3,$flag4,$flag5,$flag6,$flag7,$flag8,$flag9;
	global $flag10,$flag11,$flag12,$flag13,$flag14,$flag15,$flag16;
	
	$param="flag".$global_checkbox_id;
	echo "<input type=\"checkbox\" name=\"$param\"";
	if($$param == "on")
		echo " checked ";
	
	echo "onclick=\"javascript:select_line('$global_checkbox_id');\">$label";
	$global_checkbox_id++;
}
?>

<?php
function GetPercent($molecule,$denominator)
{
	$molecule+=0;
	if($denominator == 0 || trim($denominator)=="")
		$percent="0%";
	else
	{
		$percent=$molecule/$denominator;
		$percent=$percent*100;
		if($percent == 100 || $percent == 0)
			$percent=$percent."%";
		else
			$percent=number_format($percent, 2, '.', '')."%";	
	}
	return $percent;
}

function SecondToTimeStamp($second)
{
	$second+=0;
	if($second > 0)
	{
		$hourValue=floor($second/3600);
		$minuteValue=floor(($second%3600)/60);
		$secondValue=$second%60;
		if($hourValue > 0 || $minuteValue > 0 || $secondValue > 0)
			return "$hourValue:$minuteValue:$secondValue";
	}
	return "";
}

/*
Get the min or max, average value from a table
$type:_MIX, _MAX, _AVE
$refer_field: the field to compare or operator
$result_field:the field value to return
$tableName: the table name
$condition: condition of the sql sentence
*/
define(_MIN,0);
define(_MAX,1);
define(_AVE,2);
function GetSpecialData($type,$refer_field,$result_field,$tableName,$condition="")
{
	if($type == _AVE)
	{
		$sql="select AVG($result_field) from $tableName ";
		if(strcmp($condition,""))
			$sql.=$condition;
		$res=mysqli_query($pubConn,$sql);
		$num=mysqli_num_rows($res);
		if($num != 1)
			return 0;
		$row=@mysqli_fetch_array($res);
		return $row[0];
	}
	else
	{
		$sql="select $result_field from $tableName where $condition order by $refer_field ";
		if($type == _MAX)
			$sql.="desc ";
		else
			$sql.="asc ";
		//echo "$sql <br>";
		$res=mysqli_query($pubConn,$sql);
		$num=mysqli_num_rows($res);
		if($num <= 0)
			return 0;
		$row=@mysqli_fetch_array($res);
		return $row[0];
	}
}

function GetRowCsv($file)
{
	$row = 1;
	$handle = fopen($file,"r");
	unset($returnData);
	while ($data = fgetcsv($handle, 8192, ",")) 
	{
		$num = count($data);
		//echo "<p> $num fields in line $row: <br>\n";
		$row++;
		for ($c=0; $c < $num; $c++) 
		{
			$returnData[]=$data[$c];
		}
	}
	fclose($handle);
	return $returnData;
}

?>
