<?php
require("../../inc/header.inc");
?>
<?php
/*****     NOTE    *****/
/*  sub_loc=sub_trunk  */
/***********************/
?>
<script language="JavaScript">
	
	function fresh(lineOnTop)
	{
		var cstatype=document.myform.csta_type.value;
		var up_date=document.myform.sel_date.value;
		var down_date_from=document.myform.sel_date_from.value;
		var down_date_to=document.myform.sel_date_to.value;
		var scrollY=document.getElementById('content_down').scrollTop;
		var trunk_n;
		var on_top_index=document.myform.on_top_select.value;
		
		var sub_sub;
		var sub_loc;
		var tru_tol;
		var tru_sub;
		var tru_tru;
		var sub_ip;
		var tru_ip;
		var inter;
		var mo;
		var mt;
		var in_tru;
		var out_tru;
		var ip_inter;
		if(cstatype==2)
			trunk_n=document.myform.trunk_num.value;
		else
			trunk_n="0,1";
				
		
		if(down_date_from<down_date_to)
			down_date_to=down_date_from;
			
		if(cstatype==0)
		{
				if(document.myform.sub_sub.checked==1)
					sub_sub=1;
				else
					sub_sub=0;
				
				if(document.myform.sub_loc.checked==1)
					sub_loc=1;
				else
					sub_loc=0;	
					
				//if(document.myform.tru_tol.checked==1)
					//tru_tol=1;
				//else
					tru_tol=0;		
					
				if(document.myform.tru_sub.checked==1)
					tru_sub=1;
				else
					tru_sub=0;
				
				if(document.myform.tru_tru.checked==1)
					tru_tru=1;
				else
					tru_tru=0;		
					
				if(document.myform.sub_ip.checked==1)
					sub_ip=1;
				else
					sub_ip=0;			
				
				if(document.myform.tru_ip.checked==1)
					tru_ip=1;
				else
					tru_ip=0;
					
				if(document.myform.inter.checked==1)
					inter=1;
				else
					inter=0;	
					
				if(document.myform.mo.checked==1)
					mo=1;
				else
					mo=0;
				
				if(document.myform.mt.checked==1)
					mt=1;
				else
					mt=0;
				
				if(document.myform.in_tru.checked==1)
					in_tru=1;
				else
					in_tru=0;
					
				if(document.myform.out_tru.checked==1)
					out_tru=1;
				else
					out_tru=0;
					
				if(document.myform.ip_inter.checked==1)
					ip_inter=1;
				else
					ip_inter=0;											
		}	
		else
		{
			sub_sub=1;
		  sub_loc=1;
		  tru_tol=1;
		  tru_sub=1;
		  tru_tru=1;
		 	sub_ip=1;
			tru_ip=1;
			inter=1;
			mo=1;
			mt=1;
			in_tru=1;
			out_tru=1;
			ip_inter=1;
		}
		var addurl="&lineTop="+lineOnTop+"&sub_sub_flag="+sub_sub+"&sub_loc_flag="+sub_loc+"&tru_tol_flag="+tru_tol+"&tru_sub_flag="+tru_sub+"&tru_tru_flag="+tru_tru;
		addurl=addurl+"&sub_ip_flag="+sub_ip+"&tru_ip_flag="+tru_ip+"&inter_flag="+inter+"&mo_flag="+mo+"&mt_flag="+mt+"&in_tru_flag="+in_tru+"&out_tru_flag="+out_tru+"&ip_inter_flag="+ip_inter;
		var urll='<?php echo"$PHP_SELF";?>?csta_type='+cstatype+'&select_date='+up_date+'&sel_date_from='+down_date_from+'&sel_date_to='+down_date_to+'&trunk_num='+trunk_n+'&on_top_select='+on_top_index+'&scroll_y='+scrollY+addurl;
		window.location.href=urll;
	}
	
	
</script>
<?php
/*
|----------------------------------------------------------------------------------------|
|             This page devide into 3 parts by the value of csta_type                    |
|              0 : service                                                               |
|              1 : E1                                                                    |
|              2 : trunk group                                                           |
|----------------------------------------------------------------------------------------|
*/
?>




<?php
	if($scroll_y > 0){
		//echo "<body topmargin=\"5\" leftmargin=\"12\" onLoad='self.scrollBy(0,$scroll_y)'>";
		echo "<body leftmargin=\"15\" rightmargin=\"10\" onload=\"javascript:scroll_div('$scroll_y');javascript:adjust();\" onresize=\"javascript:adjust();\" style=\"overflow:hidden;overflow-x:hidden;overflow-y:hidden;\">";
	}else{
		echo "<body leftmargin=\"15\" rightmargin=\"10\" onload=\"javascript:adjust();\" onresize=\"javascript:adjust();\" style=\"overflow:hidden;overflow-x:hidden;overflow-y:hidden;\">";

	}
echo "<form name=\"myform\" method=\"POST\">";
//=======================================================================================|
//========             Data Initial when run first               ========================|
//=======================================================================================|
if(!isset($csta_type))
	$csta_type=0;
if(!isset($select_date))
	$select_date=0;	
if(!isset($sel_date))
	$sel_date=0;	
if(!isset($trunk_num))
  $trunk_num="1,2";	
if(!isset($on_top_index))
	$on_top_index=0;
if(!isset($sel_date_to))
	$sel_date_to=0;
if(!isset($sel_date_from))
	$sel_date_from=0;
if(!isset($sub_sub_flag))
{
	$sub_sub_flag=0;
	$sub_loc_flag=0;
	$tru_tol_flag=0;
	$tru_sub_flag=0;
	$tru_tru_flag=0;
	$tru_ip_flag=0;
	$sub_ip_flag=0;
	$inter_flag=0;
	$mo_flag=0;
	$mt_flag=0;
	$in_tru_flag=0;
	$out_tru_flag=0;
	$ip_inter_flag=0;
}		  
	
$DEBUG=0;	
adjust_head();
adjust_title_head();
echo "<table id=\"table_up\" border=\"0\" width=\"100%\">";
echo "<tr>";
echo "<td width=\"50%\" align=\"left\">";
echo "$strTitlePerformance > MSC > $strErlang";
echo "</td>";
echo "<td width=\"50%\" align=\"right\" title=\"Help\">";
$helpId="3.2.4";
showHelp($helpId);
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td align=\"left\">";

//======up csta_type selection========
$csta_type_label=array("Service","E1","Trunk group");
echo "<select name=\"csta_type\" onChange=\"javascript:fresh()\">";
for($i=0;$i<3;$i++)
{
	//hide the E1
	if($i==1)
		continue;
	if($i==$csta_type)
		echo "<option value=$i selected>{$csta_type_label[$i]}</option>";
	else
		echo "<option value=$i>{$csta_type_label[$i]}</option>";
}
echo "</select>";

//======date selection======
echo "<select name=\"sel_date\" size=1>";			
	$day0=date("Y-m-d",mktime(0,0,0,date("m"),date("d"),date("Y")));
	$day1=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-1,date("Y")));
	$day2=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-2,date("Y")));
	$day3=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-3,date("Y")));
	$day4=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-4,date("Y")));
	$day5=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-5,date("Y")));
	$day6=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-6,date("Y")));
	for($i=0;$i<7;$i++)
	{
		if($i==$select_date)
			echo "<option selected value=$i>${day.$i}</option>";
		else
			echo "<option value=$i>${day.$i}</option>";
	}
echo "</select>";

//===search button=====
echo "<span onClick=\"javascript:fresh()\" style=\"cursor:hand\">";
	echo "<IMG align=absBottom border=0 src=\"../../images/search.gif\" >$strSearch";
echo "</span>";

echo "</td>";
echo "<td align=\"right\">";

//====printer button===
echo "<span onClick=\"window.print();\" style=\"cursor:hand\">";
	echo "<img align=absBottom border=0 src=\"../../images/printer.gif\" width=16 height=16>";
echo "</span>";
	
echo "</td>";
echo "</tr>";
echo "</table>";

adjust_title_tail();
adjust_content_head();
echo "<br>";
?>
<?php
//=======================================================================================|
//===========                csta_type==0,service  Table                 ================|
//=======================================================================================|
if($csta_type==0)	
{
	//======Data define=========
	/*
			i      					j(hour)
			0 sub to sub 			0-23
			1 sub to trunk = sub to local
			2 trunk to sub 
			3 trunk to trunk
			4 interconnection
			5 sub to IP
			6 trunk to IP
			7 mobile originated
			8 mobile terminated
		 	9 in trunk 
		 	10 out trunk
		 	11 IP terminated	
	*/
	for($i=0;$i<12;$i++)
	{
		for($j=0;$j<24;$j++)
		{
			$erlang_service_oneday[$i][$j]=0;
		}
	}
	
	//======Get Data from DB======
	//echo "sel_date:$sel_date<br>";
	$db="CSTA_DB";
	$table_name="mscSum";
	$temp=date("d",mktime(0,0,0,date("m"),date("d")-$select_date,date("Y")));
	$table_name=$table_name."_".$temp;
		
	//echo "table_name:$table_name<br>";		
	$from_time=date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d")-$select_date,date("Y")));
	$to_time=date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d")-$select_date+1,date("Y")));
	//echo "from:$from_time--to:$to_time<br>";
	$select="select key_type,sum(Duration) as duration,csta_datetime ";
	$from="from $table_name ";
	$service_where="and key_type>4 and key_type<17 ";
	$where="where csta_datetime >'$from_time' and csta_datetime <='$to_time' ".$service_where;
	$group="group by csta_datetime,key_type ";
	$order="order by key_type ";
	$sql=$select.$from.$where.$group.$order;
	//echo "$sql<br>";
	
	$key_type_relation=array(5=>0,6=>1,7=>2,8=>3,9=>4,10=>5,11=>6,12=>7,13=>8,14=>9,15=>10,16=>11);

	$result=@mysqli_query($pubConn,$sql);
	$rows=@mysqli_fetch_array($result);
	
	if($rows)
	{	
		do{
			$key_type=$rows[key_type];
			$index=$key_type_relation[$key_type]+0;
			$datetime=substr($rows[csta_datetime],11,2)+0;	
			if($datetime==23)
				continue;
			//echo "datetime=$datetime<br>";
			$erlang_service_oneday[$index][$datetime+1]+=$rows[duration];
			//echo "{$erlang_service_oneday[$index][$datetime]}<br>";
		}while($rows=mysqli_fetch_array($result));
	}
	
	//get the 0-hour of the day
	$table_name="mscSum";
	$temp=date("d",mktime(0,0,0,date("m"),date("d")-$select_date-1,date("Y")));
	$table_name=$table_name."_".$temp;
		
	//echo "table_name:$table_name<br>";		
	$from_time=date("Y-m-d H:i:s",mktime(23,0,0,date("m"),date("d")-$select_date-1,date("Y")));
	$to_time=date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d")-$select_date,date("Y")));
	//echo "from:$from_time--to:$to_time<br>";
	$select="select key_type,sum(Duration) as duration,csta_datetime ";
	$from="from $table_name ";
	$service_where="and key_type>4 and key_type<17 ";
	$where="where csta_datetime >'$from_time' and csta_datetime <='$to_time' ".$service_where;
	$group="group by csta_datetime,key_type ";
	$order="order by key_type ";
	$sql=$select.$from.$where.$group.$order;
	//echo "$sql<br>";
	
	$key_type_relation=array(5=>0,6=>1,7=>2,8=>3,9=>4,10=>5,11=>6,12=>7,13=>8,14=>9,15=>10,16=>11);

	$result=@mysqli_query($pubConn,$sql);
	$rows=@mysqli_fetch_array($result);
	
	if($rows)
	{	
		do{
			$key_type=$rows[key_type];
			$index=$key_type_relation[$key_type]+0;
			$datetime=substr($rows[csta_datetime],11,2)+0;	

			//echo "datetime=$datetime<br>";
			$erlang_service_oneday[$index][0]+=$rows[duration];
			//echo "{$erlang_service_oneday[$index][$datetime]}<br>";
		}while($rows=mysqli_fetch_array($result));
	}
	
	//======Fake Data=======
	if($DEBUG)
	{
		for($i=0;$i<13;$i++)
		{
			for($j=0;$j<24;$j++)
			{
				$erlang_service_oneday[$i][$j]=rand(10,100);
			}
		}
	}
	
	//=====Test the Data=====
	if(0)
	{	
		$test_service_type_label=array("SUB SUB","SUB LOC","TRK TOLL","TRK SUB","TRK TRK","SUB IP",
																	 "TRK IP","INTER","MO","MT","IN TRK","OUT TRK","IP");
		for($i=0;$i<13;$i++)
		{
			for($j=0;$j<24;$j++)
			{
				echo "{$test_service_type_label[$i]}--$j--{$erlang_service_oneday[$i][$j]}<br>"; 
			}
		}
	}
	
	//=====Fix the Data=====
	for($i=0;$i<12;$i++)
	{
		for($j=0;$j<24;$j++)
		{
			$erlang_service_oneday[$i][$j]=change2point($erlang_service_oneday[$i][$j]/3600);
		}
	}
	
	//====show the table-1====
	 	$table_title="Service"." Erlang Date:".date("Y-m-d",mktime(0,0,0,date("m"),date("d")-$select_date,date("Y")));	//table title
	 	$service_type_label=array("Subs to subs","Subs to trunk","Trunk to subs","Trunk to trunk","Intraconnection",
	 	                          "Sub to IP","Trunk to IP",
	 	                          "Mobile originated","Mobile terminated","In trunk","Out trunk","IP terminated");
	 	echo "<center><b>$table_title</b></center>";
	 	echo "<table width=\"100%\" border=\"1\" cellpadding=\"2\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\" bgcolor=\"#FFFFFF\">";
	 	echo "<tr bgcolor=\"#E6E6E6\">";
	 	echo "<td align=center>Type</td><td colspan=\"12\" align=\"center\">";
	 	echo "Erlang(hour)";
	 	echo "</td>";
	 	echo "</tr>";
	 	
	 	for($i=0;$i<12;$i++)
	 	{
	 		echo "<tr bgcolor=\"#E6E6E6\">";
	 			echo "<td>&nbsp</td>";
	 			for($j=0;$j<12;$j++)
	 			{
	 				echo "<td>$j</td>";
	 			}
	 		echo "</tr>";
	 		
	 		echo "<tr>";
	 			echo "<td width=\"12%\" rowspan=4>{$service_type_label[$i]}</td>";
	 			
	 					echo "<tr>";
	 						for($j=0;$j<12;$j++)
	 						{
	 							echo "<td>{$erlang_service_oneday[$i][$j]}</td>";
	 						}
	 					echo "</tr>";
	 					echo "<tr bgcolor=\"#E6E6E6\">";
	 						for($j=12;$j<24;$j++)
	 						{
	 							echo "<td>$j</td>";
	 						}
	 					echo "</tr>";
	 					echo "<tr>";
	 						for($j=12;$j<24;$j++)
	 						{
	 							echo "<td>{$erlang_service_oneday[$i][$j]}</td>";
	 						}
	 					echo "</tr>";
	 			
	 		echo "</tr>";
	 	}
	 	echo "</table>";

	
}
//=======================================================================================|
//===========                csta_type==1,E1  Table                      ================|
//=======================================================================================|
else if($csta_type==1)
{
	//=====Data define=======
	for($i=0;$i<256;$i++)
	{
		for($j=0;$j<24;$j++)
		{
			$erlang_e1_oneday[$i][$j]=0;
		}
	}
	
	//=====Get Data from DB======
	$device_name=isset($device_name)?$device_name:$DEFAULT_SYS;	//device name
	$db="CSTA_DB";
	$sys_entries=get_sys_total($device_name);
	$subsys_entries=1;
	$e1_entries=get_e1_total($device_name);
	
	$db="CSTA_DB";
	$table_name="mscSum";
	$temp=date("d",mktime(0,0,0,date("m"),date("d")-$select_date,date("Y")));
	$table_name=$table_name."_".$temp;
		
	//echo "table_name:$table_name<br>";		
	$from_time=date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d")-$select_date,date("Y")));
	$to_time=date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d")-$select_date+1,date("Y")));
	
	for($i=0;$i<$sys_entries;$i++)
	{
		$select="select key_sys_id,key_subsys_id,key_e1_tg,sum(Duration) as duration,csta_datetime ";
		$from="from $table_name ";
		$group=" GROUP BY key_e1_tg,csta_datetime";
		$e1_where="AND key_type>0 AND key_type<3 ";
		$where=" WHERE key_sys_id=$i AND key_subsys_id=0 AND csta_datetime >'$from_time' AND csta_datetime <='$to_time' ".$e1_where;
		$erl_sql=$select.$from.$where.$group;
		//echo "$erl_sql<br>";
		$result=@mysqli_query($pubConn,$erl_sql);
		$rows=@mysqli_fetch_array($result);
		if($rows)
		{
			do{
				$datetime=substr($rows[csta_datetime],11,2)+0;	
				//echo "datetime:$datetime<br>";
				$msc_no=$rows[0]+0;
				$e1_no=$rows[2]+0;
				
				$erlang_e1_oneday[$msc_no*128+$e1_no][$datetime]=change2point(($rows[3]+0)/3600);
				//echo "e1_data[$msc_no][0][$e1_no][$datetime]:{$e1_data[$msc_no][0][$e1_no][$datetime]}<br>";
			}while($rows=@mysqli_fetch_array($result));
		}
	}

	
	//====Fake Data============
	if($DEBUG)
	{
		for($i=0;$i<256;$i++)
		{
			for($j=0;$j<24;$j++)
			{
				$erlang_e1_oneday[$i][$j]=rand(10,100);
			}
		}
	}	
	
	//====Get the sys-0 and sys-1 data=====
	for($i=0;$i<256;$i++)
	{
		for($j=0;$j<24;$j++)
		{
			if($i<128)
				$e1_sys1_sum[$j]+=$erlang_e1_oneday[$i][$j];
			else
				$e1_sys2_sum[$j]+=$erlang_e1_oneday[$i][$j];
		}
	}
	
	//====Test the Data======
	if(0)
	{
		for($i=0;$i<256;$i++)
		{
			for($j=0;$j<24;$j++)
			{
				echo "E1-$i--Hour-$j--{$erlang_e1_oneday[$i][$j]}<br>";
			}
		}
	}
	
	//=====show the table-1=====
		$table_title="E1"." Erlang Date:".date("Y-m-d",mktime(0,0,0,date("m"),date("d")-$select_date,date("Y")));	//table title
		echo "<center><b>$table_title</b></center>";
		echo "<table width=100% border=\"1\" cellpadding=\"2\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\" bgcolor=\"#FFFFFF\">";
	 	echo "<tr bgcolor=\"#E6E6E6\">";
	 	echo "<td colspan=2>Equipment</td><td colspan=\"24\" align=\"center\">";
	 	echo "Erlang(hour)";
	 	echo "</td>";
	 	echo "</tr>";
	 	echo "<tr bgcolor=\"#E6E6E6\">";
	 	echo "<td align=\"center\">System</td>";
	 	echo "<td align=\"center\">E1</td>";	
		for($i=0;$i<24;$i++)
	 	{
	 		echo "<td alignt=\"center\" width=4%>$i</td>";
	 	}
	 	
	 	//msc-0
	 	echo "<tr bgcolor=\"#E6E6E6\">";
 		echo "<td>MSC-0</td><td>sys</td>";
		for($j=0;$j<24;$j++)
		{
			echo "<td>{$e1_sys1_sum[$j]}</td>";
		}
	 	echo "</tr>";
	 	
	 	for($i=0;$i<128;$i++)
	 	{
	 		echo "<tr>";
	 			echo "<td>MSC-0</td><td>$i</td>";
	 			for($j=0;$j<24;$j++)
	 			{
	 				echo "<td>{$erlang_e1_oneday[$i][$j]}</td>";
	 			}
	 		echo "</tr>";	
	 	}
	 	
	 	//msc=1
	 	echo "<tr bgcolor=\"#E6E6E6\">";
 		echo "<td>MSC-1</td><td>sys</td>";
		for($j=0;$j<24;$j++)
		{
			echo "<td>{$e1_sys2_sum[$j]}</td>";
		}
	 	echo "</tr>";
	 	
	 	for($i=128;$i<256;$i++)
	 	{
	 		echo "<tr>";
	 			$temp=$i-128;
	 			echo "<td>MSC-1</td><td>$temp</td>";
	 			for($j=0;$j<24;$j++)
	 			{
	 				echo "<td>{$erlang_e1_oneday[$i][$j]}</td>";
	 			}
	 		echo "</tr>";	
	 	}
	 	echo "</table>";
	 	
	 	
}
//=======================================================================================|
//===========                csta_type==2,trunk group   Table            ================|
//=======================================================================================|
else if($csta_type==2)
{
	//=====Data define======
	for($i=0;$i<=256;$i++)
	{
		for($j=0;$j<24;$j++)
		{
			$erlang_trunk_oneday[$i][$j]=0;
		}
	}
	
	//=====Get the Data from DB======
	$db="CSTA_DB";
	$table_name="mscSum";
	$temp=date("d",mktime(0,0,0,date("m"),date("d")-$select_date,date("Y")));
	$table_name=$table_name."_".$temp;
		
	//echo "table_name:$table_name<br>";		
	$from_time=date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d")-$select_date,date("Y")));
	$to_time=date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d")-$select_date+1,date("Y")));
	
	
	$select="select key_e1_tg,csta_datetime,sum(Duration) as duration ";
	$from="from $table_name ";
	$where=" where csta_datetime>='$from_time' and csta_datetime<'$to_time' and key_type>2 and key_type<5";
	$group=" group by key_e1_tg, csta_datetime ";
	$order=" order by csta_datetime,key_e1_tg ";
	$select_sql=$select.$from.$where.$group.$order;
	//echo "$select_sql<br>";				
	$select_result=mysqli_query($pubConn,$select_sql);
	if($select_result==TRUE)
	{
		$select_rows=mysqli_fetch_array($select_result);

		if($select_rows)
		{	
			do
			{
				//Get the hour
				$datetime=substr($select_rows[csta_datetime],11,2)+0;		
				$gro=$select_rows[0];
				if($datetime==23)
					continue;
				//echo "gro:$gro<br>";
				//echo "datetime:$datetime<br>";
				$erlang_trunk_oneday[$gro][$datetime+1]+=change2point($select_rows[duration]/3600);
				//echo "group:$gro--time:$datetime=={$erlang_trunk_oneday[$gro][$datetime]}<br>";
			}while($select_rows=mysqli_fetch_array($select_result));
		}
	}
	
	//get the 0-hour of the day
	$table_name="mscSum";
	$temp=date("d",mktime(0,0,0,date("m"),date("d")-$select_date-1,date("Y")));
	$table_name=$table_name."_".$temp;
		
	//echo "table_name:$table_name<br>";		
	$from_time=date("Y-m-d H:i:s",mktime(23,0,0,date("m"),date("d")-$select_date-1,date("Y")));
	$to_time=date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d")-$select_date,date("Y")));
	
	
	$select="select key_e1_tg,csta_datetime,sum(Duration) as duration ";
	$from="from $table_name ";
	$where=" where csta_datetime>='$from_time' and csta_datetime<'$to_time' and key_type>2 and key_type<5";
	$group=" group by key_e1_tg, csta_datetime ";
	$order=" order by csta_datetime,key_e1_tg ";
	$select_sql=$select.$from.$where.$group.$order;
	//echo "$select_sql<br>";				
	$select_result=mysqli_query($pubConn,$select_sql);
	if($select_result==TRUE)
	{
		$select_rows=mysqli_fetch_array($select_result);

		if($select_rows)
		{	
			do
			{
				//Get the hour
				$datetime=substr($select_rows[csta_datetime],11,2)+0;		
				$gro=$select_rows[0];

				//echo "gro:$gro<br>";
				//echo "datetime:$datetime<br>";
				$erlang_trunk_oneday[$gro][0]+=change2point($select_rows[duration]/3600);
				//echo "group:$gro--time:$datetime=={$erlang_trunk_oneday[$gro][$datetime]}<br>";
			}while($select_rows=mysqli_fetch_array($select_result));
		}
	}
	

	//=====Fake Data========
	if($DEBUG)
	{
		for($i=0;$i<256;$i++)
		{
			for($j=0;$j<24;$j++)
			{
				$erlang_trunk_oneday[$i][$j]=rand(10,100);
			}
		}
	}
	
	//======Test the Data=====
	if(0)
	{
		for($i=0;$i<256;$i++)
		{
			for($j=0;$j<24;$j++)
			{
				$erlang_trunk_oneday[$i][$j]=rand(10,100);
			}
		}
	}
	
	//=====Show the table-1=====
		$table_title="Trunk group Erlang Date:".date("Y-m-d",mktime(0,0,0,date("m"),date("d")-$select_date,date("Y")));
		echo "<center><b>$table_title</b></center>";
	 	echo "<table width=100% border=\"1\" cellpadding=\"2\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\" bgcolor=\"#FFFFFF\">";
	 	echo "<tr bgcolor=\"#E6E6E6\">";
	 	echo "<td>&nbsp</td><td colspan=\"24\" align=\"center\">";
	 	echo "Erlang(hour)";
	 	echo "</td>";
	 	echo "</tr>";
	 	echo "<tr bgcolor=\"#E6E6E6\">";
	 	echo "<td align=\"center\"><nobr>TG</td>";
		for($i=0;$i<24;$i++)
	 	{
	 		echo "<td>$i</td>";
	 	}
	 	echo "</tr>";
	 	
	 	for($i=1;$i<256;$i++)
	 	{
	 		echo "<tr>";
	 		$t=$i;
	 		echo "<td>$t</td>";
	 		for($j=0;$j<24;$j++)
	 		{
	 			$erlang_trunk_oneday[$i][$j]=number_format($erlang_trunk_oneday[$i][$j], 2, '.', '');
	 			if($erlang_trunk_oneday[$i][$j]==0)
	 				echo "<td><font color=#AEAEAE>{$erlang_trunk_oneday[$i][$j]}</font></td>";
	 			else
	 				echo "<td>{$erlang_trunk_oneday[$i][$j]}</td>";
	 		}
	 		echo "</tr>";
	 	}
	 	echo "</table>";
		
	
}

//=======================================================================================|
//========                     Down Selection                                ============|
//=======================================================================================|
//====date from selection=====
//echo "sel_date_from:$sel_date_from--sel_date_to:$sel_date_to<br>";
echo "<p><table>";
echo "<tr>";
echo "<td>";	
  echo "<select name=\"sel_date_from\" onChange=\"javascript:fresh()\">";
  for($i=0;$i<7;$i++)
  {
	  if($i==$sel_date_from)
   	  echo "<option selected value=$i>${day.$i}</option>";			
	  else
		  echo "<option value=$i>${day.$i}</option>";
	}
	echo "</select>";
	echo "</td>";

  echo "<td>to</td>";
	echo "<td>";
	echo "<select name=\"sel_date_to\">";			  
		for($i=0;$i<=$sel_date_from;$i++)
		{
			if($i==$sel_date_to)
		  	echo "<option selected value=$i>${day.$i}</option>";			
		  else
		  	echo "<option value=$i>${day.$i}</option>";
		}		
	echo "</select>";
	echo "</td>";
	//=====trunk number text for trunk=====
	echo "<td>&nbsp</td>";
	echo "<td>";
	if($csta_type==2)
	{
		echo "<input type=\"text\" size=6 name=\"trunk_num\" value=$trunk_num>";
	}
	//hide the trunk_num selection
	else
	{
		echo "<input type=\"hidden\" name=\"trunk_num\" value=$trunk_num>";
	}
	echo "</td>";

	//====search button=====
	echo "<td align=\"left\">";
		echo "<div onClick=\"javascript:fresh()\" style=\"cursor:hand\">";
			echo "<IMG align=absBottom border=0 src=\"../../images/search.gif\">$strSearch";
		echo "</div>";
	echo "</td>";	
	
	
	
	
	//====on top selection======
	//echo "csta_type:$csta_type<br>";
	if($csta_type==0)
	{
		echo "</tr></table>";
		echo "<table border=0><tr>";
		echo "<td>";
		if($sub_sub_flag==1)
		{
			$line_select[0]=1;
			echo "<input type=checkbox name=\"sub_sub\" checked onClick=\"javascript:fresh(0)\">";
		}
		else
		{
			$line_select[0]=0;
			$lineOnTop=0;
			echo "<input type=checkbox name=\"sub_sub\" onClick=\"javascript:fresh($lineOnTop)\">";
		}
		echo "<nobr>SUB SUB</td>";
		
		echo "<td>";
		if($sub_loc_flag==1)
		{
			$line_select[1]=1;
			echo "<input type=checkbox name=\"sub_loc\" checked onClick=\"javascript:fresh(0)\">";
		}
		else
		{
			$line_select[1]=0;
			$lineOnTop=1;
			echo "<input type=checkbox name=\"sub_loc\" onClick=\"javascript:fresh($lineOnTop)\">";
		}
		echo "<nobr>SUB TRK</td>";
		
		echo "<td>";
		/*  delete the sub to tol
		if($tru_tol_flag==1)
		{
			$line_select[2]=1;
			echo "<input type=checkbox name=\"tru_tol\" checked onClick=\"javascript:fresh()\">";
		}
		else
		{
			$line_select[2]=0;
			echo "<input type=checkbox name=\"tru_tol\" onClick=\"javascript:fresh()\">";
		}
		echo "<nobr>TRK TOLL</td>";
		*/
		echo "<td>";
		
		if($tru_sub_flag==1)
		{
			$line_select[2]=1;
			echo "<input type=checkbox name=\"tru_sub\" checked onClick=\"javascript:fresh(0)\">";
		}
		else
		{
			$line_select[2]=0;
			$lineOnTop=2;
			echo "<input type=checkbox name=\"tru_sub\" onClick=\"javascript:fresh($lineOnTop)\">";
		}
		echo "<nobr>TRK SUB</td>";
		
		echo "<td>";
		if($tru_tru_flag==1)
		{
			$line_select[3]=1;
			echo "<input type=checkbox name=\"tru_tru\" checked onClick=\"javascript:fresh(0)\">";
		}
		else
		{
			$line_select[3]=0;
			$lineOnTop=3;
			echo "<input type=checkbox name=\"tru_tru\" onClick=\"javascript:fresh($lineOnTop)\">";
		}
		echo "<nobr>TRK TRK</td>";
		
		echo "<td>";
		if($inter_flag==1)
		{
			$line_select[4]=1;
			echo "<input type=checkbox name=\"inter\" checked onClick=\"javascript:fresh(0)\">";
		}
		else
		{
			$line_select[4]=0;
			$lineOnTop=4;
			echo "<input type=checkbox name=\"inter\" onClick=\"javascript:fresh($lineOnTop)\">";
		}
		echo "INTRA</td>";
		
		echo "<td>";
		if($sub_ip_flag==1)
		{
			$line_select[5]=1;
			echo "<input type=checkbox name=\"sub_ip\" checked onClick=\"javascript:fresh(0)\">";
		}
		else
		{
			$line_select[5]=0;
			$lineOnTop=5;
			echo "<input type=checkbox name=\"sub_ip\" onClick=\"javascript:fresh($lineOnTop)\">";
		}
		echo "<nobr>SUB IP</td>";
		
		echo "<td>";
		if($tru_ip_flag==1)
		{
			$line_select[6]=1;
			echo "<input type=checkbox name=\"tru_ip\" checked onClick=\"javascript:fresh(0)\">";
		}
		else
		{
			$line_select[6]=0;
			$lineOnTop=6;
			echo "<input type=checkbox name=\"tru_ip\" onClick=\"javascript:fresh($lineOnTop)\">";
		}
		echo "<nobr>TRK IP</td>";
		
		
		echo "<td>";
		if($mo_flag==1)
		{
			$line_select[7]=1;
			echo "<input type=checkbox name=\"mo\" checked onClick=\"javascript:fresh(0)\">";
		}
		else
		{
			$line_select[7]=0;
			$lineOnTop=7;
			echo "<input type=checkbox name=\"mo\" onClick=\"javascript:fresh($lineOnTop)\">";
		}
		echo "MO</td>";
		
		echo "<td>";
		if($mt_flag==1)
		{
			$line_select[8]=1;
			echo "<input type=checkbox name=\"mt\" checked onClick=\"javascript:fresh(0)\">";
		}
		else
		{
			$line_select[8]=0;
			$lineOnTop=8;
			echo "<input type=checkbox name=\"mt\" onClick=\"javascript:fresh($lineOnTop)\">";
		}
		echo "MT</td>";
		
		echo "<td>";
		if($in_tru_flag==1)
		{
			$line_select[9]=1;
			echo "<input type=checkbox name=\"in_tru\" checked onClick=\"javascript:fresh(0)\">";
		}
		else
		{
			$line_select[9]=0;
			$lineOnTop=9;
			echo "<input type=checkbox name=\"in_tru\" onClick=\"javascript:fresh($lineOnTop)\">";
		}
		echo "<nobr>IN TRK</td>";
		
		echo "<td>";
		if($out_tru_flag==1)
		{
			$line_select[10]=1;
			echo "<input type=checkbox name=\"out_tru\" checked onClick=\"javascript:fresh(0)\">";
		}
		else
		{
			$line_select[10]=0;
			$lineOnTop=10;
			echo "<input type=checkbox name=\"out_tru\" onClick=\"javascript:fresh($lineOnTop)\">";
		}
		echo "<nobr>OUT TRK</td>";
		
		echo "<td>";
		if($ip_inter_flag==1)
		{
			$line_select[11]=1;
			echo "<input type=checkbox name=\"ip_inter\" checked onClick=\"javascript:fresh(0)\">";
		}
		else
		{
			$line_select[11]=0;
			$lineOnTop=11;
			echo "<input type=checkbox name=\"ip_inter\" onClick=\"javascript:fresh($lineOnTop)\">";
		}
		echo "IP</td>";
		
		echo "<input type=hidden name=\"on_top_select\" value=0>";
	}
	else if($csta_type==1)
	{
		echo "<td width=70%>&nbsp</td>";
		echo "<td>";
		echo "<select name=\"on_top_select\" onChange=\"javascript:fresh()\">";
		$service_on_top_label=array("In","Out","In&Out");
	
		for($i=0;$i<3;$i++)
		{
			if($i==$on_top_select)
				echo "<option value=$i selected>$service_on_top_label[$i]</option>";
			else
				echo "<option value=$i>$service_on_top_label[$i]</option>";
		}
		echo "</select><nobr>on top";
		echo "</td>";
		
		echo "<input type=hidden name=\"sub_sub\" value=1>";
		echo "<input type=hidden name=\"sub_loc\" value=1>";
		echo "<input type=hidden name=\"tru_tol\" value=1>";
		echo "<input type=hidden name=\"tru_sub\" value=1>";
		echo "<input type=hidden name=\"tru_tru\" value=1>";
		echo "<input type=hidden name=\"sub_ip\" value=1>";
		echo "<input type=hidden name=\"tru_ip\" value=1>";
		echo "<input type=hidden name=\"inter\" value=1>";
		echo "<input type=hidden name=\"mo\" value=1>";
		echo "<input type=hidden name=\"mt\" value=1>";
		echo "<input type=hidden name=\"in_tru\" value=1>";
		echo "<input type=hidden name=\"out_tru\" value=1>";
		echo "<input type=hidden name=\"ip_inter\" value=1>";
	}
	else if($csta_type==2)
	{
		echo "<td width=50%>&nbsp</td>";
		echo "<td>";
		echo "<select name=\"on_top_select\" onChange=\"javascript:fresh()\">";
		$trunk_num_select=explode(",",$trunk_num);
		$service_on_top_label=array("Trunk group ".$trunk_num_select[0],"Trunk group ".$trunk_num_select[1]);
		for($i=0;$i<2;$i++)
		{
			if($i==$on_top_select)
				echo "<option value=$i selected>$service_on_top_label[$i]</option>";
			else
				echo "<option value=$i>$service_on_top_label[$i]</option>";
		}
		echo "</select><nobr>on top";
		echo "</td>";
		
		echo "<input type=hidden name=\"sub_sub\" value=1>";
		echo "<input type=hidden name=\"sub_loc\" value=1>";
		echo "<input type=hidden name=\"tru_tol\" value=1>";
		echo "<input type=hidden name=\"tru_sub\" value=1>";
		echo "<input type=hidden name=\"tru_tru\" value=1>";
		echo "<input type=hidden name=\"sub_ip\" value=1>";
		echo "<input type=hidden name=\"tru_ip\" value=1>";
		echo "<input type=hidden name=\"inter\" value=1>";
		echo "<input type=hidden name=\"mo\" value=1>";
		echo "<input type=hidden name=\"mt\" value=1>";
		echo "<input type=hidden name=\"in_tru\" value=1>";
		echo "<input type=hidden name=\"out_tru\" value=1>";
		echo "<input type=hidden name=\"ip_inter\" value=1>";
	}	
				
	echo "</td></table>";
//=======================================================================================|
//===========                Test before draw graphics                   ================|
//=======================================================================================|
if(0)
{
	$test_csta_type_label=array("Service","E1","Trunk");
	$slot=$sel_date_from-$sel_date_to+1;
	echo "Type----{$test_csta_type_label[$csta_type]}<br>";
	echo "Time----sigle $sel_date   From $sel_date_from to $sel_date_to  slot:$slot<br>";
	echo "Service flag-----$sub_sub_flag $sub_loc_flag $tru_tol_flag $tru_sub_flag $tru_tru_flag $sub_ip_flag $tru_ip_flag $inter_flag $mo_flag $mt_flag $in_tru_flag $out_tru_flag $ip_inter_flag<br>";
	echo "Trunk num----$trunk_num<br>";
	echo "on top index----$on_top_index<br>";
}	
	
	

//=======================================================================================|
//===========                csta_type==0,service   Graphics             ================|
//=======================================================================================|
$slot=$sel_date_from-$sel_date_to+1;
if($csta_type==0)
{
	//====Data define====
	for($i=0;$i<12;$i++)
	{
		for($j=0;$j<24*$slot;$j++)
		{
			$erlang_service_mangdays[$i][$j]=0;
		}
	}
	
	//====Get the Data from DB=====
	//echo "slot:$slot<br>";
	$key_type_relation=array(5=>0,6=>1,7=>2,8=>3,9=>4,10=>5,11=>6,12=>7,13=>8,14=>9,15=>10,16=>11);
	for($day=0;$day<$slot;$day++)
	{
		$db="CSTA_DB";
		$table_name="mscSum";
		$temp=date("d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+$day,date("Y")));
		$table_name=$table_name."_".$temp;
		//echo "table_name:$table_name<br>";
		
		$from_time=date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d")-$sel_date_from+$day,date("Y")));
		$to_time=date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d")-$sel_date_from+1+$day,date("Y")));
		//echo "from:$from_time--to:$to_time<br>";
		
		$select="select key_type,sum(Duration) as duration,csta_datetime ";
		$from="from $table_name ";
		$service_where="and key_type>4 and key_type<17 ";
		$where="where csta_datetime >'$from_time' and csta_datetime <='$to_time' ".$service_where;
		$group="group by csta_datetime,key_type ";
		$order="order by key_type ";
		$sql=$select.$from.$where.$group.$order;
		//echo "$sql<p>";
		
		$result=@mysqli_query($pubConn,$sql);
		$rows=@mysqli_fetch_array($result);
		
		if($rows)
		{	
			do{
				$key_type=$rows[key_type];
				$index=$key_type_relation[$key_type]+0;
				$datetime=substr($rows[csta_datetime],11,2)+0;
				if($datetime == 23)
					continue;	
				//echo "datetime:$datetime<br>";
				$erlang_service_mangdays[$index][$datetime+1+24*$day]+=$rows[duration];
				
			}while($rows=mysqli_fetch_array($result));
		}
		
	}
	
	//get the 0-hour of the day
	for($day=0;$day<$slot;$day++)
	{
		$db="CSTA_DB";
		$table_name="mscSum";
		$temp=date("d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+$day-1,date("Y")));
		$table_name=$table_name."_".$temp;
		//echo "table_name:$table_name<br>";
		
		$from_time=date("Y-m-d H:i:s",mktime(23,0,0,date("m"),date("d")-$sel_date_from+$day-1,date("Y")));
		$to_time=date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d")-$sel_date_from+$day,date("Y")));
		//echo "from:$from_time--to:$to_time<br>";
		
		$select="select key_type,sum(Duration) as duration,csta_datetime ";
		$from="from $table_name ";
		$service_where="and key_type>4 and key_type<17 ";
		$where="where csta_datetime >'$from_time' and csta_datetime <='$to_time' ".$service_where;
		$group="group by csta_datetime,key_type ";
		$order="order by key_type ";
		$sql=$select.$from.$where.$group.$order;
		//echo "$sql<p>";
		
		$result=@mysqli_query($pubConn,$sql);
		$rows=@mysqli_fetch_array($result);
		
		if($rows)
		{	
			do{
				$key_type=$rows[key_type];
				$index=$key_type_relation[$key_type]+0;
				$datetime=substr($rows[csta_datetime],11,2)+0;

				//echo "datetime:$datetime<br>";
				$erlang_service_mangdays[$index][24*$day]+=$rows[duration];
				
			}while($rows=mysqli_fetch_array($result));
		}
		
	}
	
	//====Fix the data====
	for($i=0;$i<12;$i++)
	{
		for($day=0;$day<$slot*24;$day++)
		{
			$erlang_service_mangdays[$i][$day]=change2point($erlang_service_mangdays[$i][$day]/3600);
		}
	}	
	
	//====Fake Data=======
	if($DEBUG)
	{
		for($i=0;$i<12;$i++)
		{
			for($j=0;$j<24*$slot;$j++)
			{
				$erlang_service_mangdays[$i][$j]=rand(10,100);
			}
		}
	}
	
	//====Test Data======
	if(0)
	{
		$test_service_type_label=array("SUB SUB","SUB LOC","TRK TOLL","TRK SUB","TRK TRK","SUB IP",
																	 "TRK IP","INTER","MO","MT","IN TRK","OUT TRK","IP");
		for($i=0;$i<12;$i++)
		{
			for($j=0;$j<24*$slot;$j++)
			{
				echo "{$test_service_type_label[$i]}--$j--{$erlang_service_mangdays[$i][$j]}<br>"; 
			}
		}
	}
	
	
}
//=======================================================================================|
//===========                csta_type==1,E1        Graphics             ================|
//=======================================================================================|
else if($csta_type==1)
{
	//====Data define====
	for($i=0;$i<3;$i++)
	{
		/*
		    0  IN
		    1  OUT
		    2  IN&OUT
		*/
		for($j=0;$j<24*$slot;$j++)
		{
			$erlang_e1_mangdays[$i][$j]=0;
		}
	}
	
	//====Get the Data from DB=====
	//echo "slot:$slot<br>";
	$device_name=isset($device_name)?$device_name:$DEFAULT_SYS;	//device name
	$db="CSTA_DB";
	$sys_entries=get_sys_total($device_name);
	for($day=0;$day<$slot;$day++)
	{
		$table_name="mscSum";
		$temp=date("d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+$day,date("Y")));
		$table_name=$table_name."_".$temp;
			
		//echo "table_name:$table_name<br>";		
		$from_time=date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d")-$sel_date_from+$day,date("Y")));
		$to_time=date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d")-$sel_date_from+1+$day,date("Y")));
		//echo "from:$from_time--to:$to_time<br>";
		//echo "on_top_select:$on_top_select<br>";
		
		//===incoming e1===
		$select="select sum(Duration) as duration,csta_datetime ";
		$from="from $table_name ";
		$group=" GROUP BY csta_datetime ";
		$e1_where="AND key_type=1 ";
		$where=" WHERE key_subsys_id=0 and csta_datetime >'$from_time' and csta_datetime <='$to_time' ".$e1_where;
		$erl_sql=$select.$from.$where.$group;
		//echo "$erl_sql<br>";
		$result=@mysqli_query($pubConn,$erl_sql);
		$rows=@mysqli_fetch_array($result);
		if($rows)
		{
			do{
				$datetime=substr($rows[csta_datetime],11,2)+0;	
				//echo "datetime:$datetime<br>";
				
				$erlang_e1_mangdays[0][$datetime]=change2point(($rows[0]+0)/3600);
				
			}while($rows=@mysqli_fetch_array($result));
		}
		
		//====outgoing e1=====
		$select="select sum(Duration) as duration,csta_datetime ";
		$from="from $table_name ";
		$group=" GROUP BY csta_datetime ";
		$e1_where="AND key_type=2 ";
		$where=" WHERE key_subsys_id=0 and csta_datetime >'$from_time' and csta_datetime <='$to_time' ".$e1_where;
		$erl_sql=$select.$from.$where.$group;
		//echo "$erl_sql<br>";
		$result=@mysqli_query($pubConn,$erl_sql);
		$rows=@mysqli_fetch_array($result);
		if($rows)
		{
			do{
				$datetime=substr($rows[csta_datetime],11,2)+0;	
				//echo "datetime:$datetime<br>";
				
				$erlang_e1_mangdays[1][$datetime]=change2point(($rows[0]+0)/3600);
				
			}while($rows=@mysqli_fetch_array($result));
		}
		
		//===in-out e1
		$select="select sum(Duration) as duration,csta_datetime ";
		$from="from $table_name ";
		$group=" GROUP BY csta_datetime ";
		$e1_where="and key_type>0 and key_type<3 ";
		$where=" WHERE key_subsys_id=0 and csta_datetime >'$from_time' and csta_datetime <='$to_time' ".$e1_where;
		$erl_sql=$select.$from.$where.$group;
		//echo "$erl_sql<br>";
		$result=@mysqli_query($pubConn,$erl_sql);
		$rows=@mysqli_fetch_array($result);
		if($rows)
		{
			do{
				$datetime=substr($rows[csta_datetime],11,2)+0;	
				//echo "datetime:$datetime<br>";
				
				$erlang_e1_mangdays[2][$datetime]=change2point(($rows[0]+0)/3600);
				
			}while($rows=@mysqli_fetch_array($result));
		}
	}
	//====Fake Data=======
	if($DEBUG)
	{
		for($i=0;$i<3;$i++)
		{
			for($j=0;$j<24*$slot;$j++)
			{
				$erlang_e1_mangdays[$i][$j]=rand(10,100);
			}
		}
	}
	
	//====Test Data======
	if(0)
	{
		$test_e1_type_label=array("In","Out","In&Out");
		for($i=0;$i<3;$i++)
		{
			for($j=0;$j<24*$slot;$j++)
			{
				echo "{$test_e1_type_label[$i]}--$j--{$erlang_e1_mangdays[$i][$j]}<br>"; 
			}
		}
	}
}
//=======================================================================================|
//===========                csta_type==2,trunk     Graphics             ================|
//=======================================================================================|
else if($csta_type==2)
{
	
	$trunk_index=explode(",",$trunk_num);
	$trunk_index[0]=$trunk_index[0];
	$trunk_index[1]=$trunk_index[1];
			
	//====Data define====
	for($i=0;$i<2;$i++)
	{
		/*
		    0  The first trunk select
		    1  The second trunk select
		*/
		for($j=0;$j<24*$slot;$j++)
		{
			$erlang_trunk_mangdays[$i][$j]=0;
		}
	}
	
	//====Get the Data from DB=====
	//echo "slot:$slot<br>";
	//trunk_num_select
	for($day=0;$day<$slot;$day++)
	{
		$db="CSTA_DB";
		$table_name="mscSum";
		$temp=date("d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+$day,date("Y")));
		$table_name=$table_name."_".$temp;
			
		//echo "table_name:$table_name<br>";		
		$from_time=date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d")-$sel_date_from+$day,date("Y")));
		$to_time=date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d")-$sel_date_from+1+$day,date("Y")));
		//echo "from:$from_time--to:$to_time<br>";
		
		//tg 1
		$select="select key_e1_tg,csta_datetime,sum(Duration) as duration ";
		$from="from $table_name ";
		$where=" where csta_datetime>='$from_time' and csta_datetime<'$to_time' and key_type>2 and key_type<5 and key_e1_tg={$trunk_index[0]} ";
		$group=" group by key_e1_tg, csta_datetime ";
		$order=" order by csta_datetime,key_e1_tg ";
		$select_sql=$select.$from.$where.$group.$order;
		//echo "$select_sql<br>";				
		$select_result=mysqli_query($pubConn,$select_sql);
		if($select_result==FALSE)
			exit("<b>There is no data between ".$from_time." to ".$to_time."</b>");
		$select_rows=mysqli_fetch_array($select_result);
		if($select_rows)
		{	
			do
			{
				//Get the hour
				$datetime=substr($select_rows[csta_datetime],11,2)+0;		
				$gro=$select_rows[0];
				if($datetime==23)
					continue;
				//echo "gro:$gro<br>";
				//echo "datetime:$datetime<br>";
				$erlang_trunk_mangdays[0][$datetime+1+24*$day]+=change2point($select_rows[duration]/3600);
				//echo "group:$gro--time:$datetime=={$erlang_trunk_mangdays[$gro][$datetime]}<br>";
			}while($select_rows=mysqli_fetch_array($select_result));
		}

		//tg 2
		$select="select key_e1_tg,csta_datetime,sum(Duration) as duration ";
		$from="from $table_name ";
		$where=" where csta_datetime>='$from_time' and csta_datetime<'$to_time' and key_type>2 and key_type<5 and key_e1_tg={$trunk_index[1]} ";
		$group=" group by key_e1_tg, csta_datetime ";
		$order=" order by csta_datetime,key_e1_tg ";
		$select_sql=$select.$from.$where.$group.$order;
		//echo "<p>$select_sql<br>";				
		$select_result=mysqli_query($pubConn,$select_sql);
		if($select_result==FALSE)
			exit("<b>There is no data between ".$from_time." to ".$to_time."</b>");
		$select_rows=mysqli_fetch_array($select_result);
		if($select_rows)
		{	
			do
			{
				//Get the hour
				$datetime=substr($select_rows[csta_datetime],11,2)+0;		
				$gro=$select_rows[0];
				if($datetime==23)
					continue;
				//echo "gro:$gro<br>";
				//echo "ddatetime:$datetime<br>";
				$erlang_trunk_mangdays[1][$datetime+1+24*$day]+=change2point($select_rows[duration]/3600);
				//echo "group:$gro--time:$datetime=={$erlang_trunk_mangdays[$gro][$datetime]}<br>";
			}while($select_rows=mysqli_fetch_array($select_result));
		}

	}
	
	//get the 0-hour of the day
	for($day=0;$day<$slot;$day++)
	{
		$db="CSTA_DB";
		$table_name="mscSum";
		$temp=date("d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+$day-1,date("Y")));
		$table_name=$table_name."_".$temp;
			
		//echo "table_name:$table_name<br>";		
		$from_time=date("Y-m-d H:i:s",mktime(23,0,0,date("m"),date("d")-$sel_date_from+$day-1,date("Y")));
		$to_time=date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d")-$sel_date_from+$day,date("Y")));
		//echo "from:$from_time--to:$to_time<br>";
		
		//tg 1
		$select="select key_e1_tg,csta_datetime,sum(Duration) as duration ";
		$from="from $table_name ";
		$where=" where csta_datetime>='$from_time' and csta_datetime<'$to_time' and key_type>2 and key_type<5 and key_e1_tg={$trunk_index[0]} ";
		$group=" group by key_e1_tg, csta_datetime ";
		$order=" order by csta_datetime,key_e1_tg ";
		$select_sql=$select.$from.$where.$group.$order;
		//echo "$select_sql<br>";				
		$select_result=mysqli_query($pubConn,$select_sql);
		if($select_result==FALSE)
			exit("<b>There is no data between ".$from_time." to ".$to_time."</b>");
		$select_rows=mysqli_fetch_array($select_result);
		if($select_rows)
		{	
			do
			{
				//Get the hour
				$datetime=substr($select_rows[csta_datetime],11,2)+0;		
				$gro=$select_rows[0];
			
				//echo "gro:$gro<br>";
				//echo "datetime:$datetime<br>";
				$erlang_trunk_mangdays[0][24*$day]+=change2point($select_rows[duration]/3600);
				//echo "group:$gro--time:$datetime=={$erlang_trunk_mangdays[$gro][$datetime]}<br>";
			}while($select_rows=mysqli_fetch_array($select_result));
		}

		//tg 2
		$select="select key_e1_tg,csta_datetime,sum(Duration) as duration ";
		$from="from $table_name ";
		$where=" where csta_datetime>='$from_time' and csta_datetime<'$to_time' and key_type>2 and key_type<5 and key_e1_tg={$trunk_index[1]} ";
		$group=" group by key_e1_tg, csta_datetime ";
		$order=" order by csta_datetime,key_e1_tg ";
		$select_sql=$select.$from.$where.$group.$order;
		//echo "<p>$select_sql<br>";				
		$select_result=mysqli_query($pubConn,$select_sql);
		if($select_result==FALSE)
			exit("<b>There is no data between ".$from_time." to ".$to_time."</b>");
		$select_rows=mysqli_fetch_array($select_result);
		if($select_rows)
		{	
			do
			{
				//Get the hour
				$datetime=substr($select_rows[csta_datetime],11,2)+0;		
				$gro=$select_rows[0];
				
				//echo "gro:$gro<br>";
				//echo "ddatetime:$datetime<br>";
				$erlang_trunk_mangdays[1][24*$day]+=change2point($select_rows[duration]/3600);
				//echo "group:$gro--time:$datetime=={$erlang_trunk_mangdays[$gro][$datetime]}<br>";
			}while($select_rows=mysqli_fetch_array($select_result));
		}

	}
	
	//====Fake Data=======
	if($DEBUG)
	{
		for($i=0;$i<2;$i++)
		{
			for($j=0;$j<24*$slot;$j++)
			{
				$erlang_trunk_mangdays[$i][$j]=rand(10,100);
			}
		}
	}
	
	//====Test Data======
	if(0)
	{
		$test_trunk_type_label=array("First","Second");
		for($i=0;$i<2;$i++)
		{
			for($j=0;$j<24*$slot;$j++)
			{
				echo "{$test_trunk_type_label[$i]} {$trunk_index[$i]}--$j--{$erlang_trunk_mangdays[$i][$j]}<br>"; 
			}
		}
	}
}


//=======================================================================================|
//===========                Draw the Graphics                              =============|
//=======================================================================================|

	$sizex=800;
	if($csta_type==0)
	  $sizey=430;
	else
		$sizey=380;
	$bgcolor=F8F8F8;
	$fgcolor=000000;
	
	$im = ImageCreate ($sizex, $sizey);
	
	$background_color = ImageColorAllocate ($im, 
				hexdec(substr($bgcolor, 0, 2)), 
				hexdec(substr($bgcolor, 2, 2)),
				hexdec(substr($bgcolor, 4, 2)));
	
	$text_color = ImageColorAllocate ($im, 
				hexdec(substr($fgcolor, 0, 2)), 
				hexdec(substr($fgcolor, 2, 2)),
				hexdec(substr($fgcolor, 4, 2)));
	
	$black_color=ImageColorAllocate($im,0,0,0);
	$white_color=ImageColorAllocate($im,255,255,255);

	//===background====
	ImageFilledRectangle($im,0,0,$sizex,$sizey,ImageColorAllocate ($im, 198, 198, 198));
	ImageFilledRectangle($im,2,2,$sizex,$sizey,ImageColorAllocate ($im, 130, 130, 130));
	$pointArr=array(0,$sizey-1,3,$sizey-4,4,$sizey-1);
	Imagefilledpolygon($im, $pointArr, 3, ImageColorAllocate ($im, 130, 130, 130));
	$pointArr=array($sizex-1,0,$sizex-4,3,$sizex,4);
	Imagefilledpolygon($im, $pointArr, 3, ImageColorAllocate ($im, 130, 130, 130));
	ImageFilledRectangle($im,2,2,$sizex-3,$sizey-3,$background_color);
	
	//===border=====
	$xBgn=60;
	$yBgn=25;
	$xEnd=780;
	$yEnd=275;
	$unitXPix=30;
	ImageLine($im, $xBgn, $yBgn, $xBgn, $yEnd, $text_color);
	ImageLine($im, $xBgn, $yBgn, $xEnd, $yBgn, $text_color);
	ImageLine($im, $xBgn, $yEnd, $xEnd, $yEnd, $text_color);
	ImageLine($im, $xEnd, $yBgn, $xEnd, $yEnd, $text_color);

	//====draw the horizonal line====
	for($i=0;$i<$slot;$i++)
	{
		$datestr[$i]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+$i,date("Y")));
	}
	for($i=0;$i<24;$i++)
	{
		if($i<10)
			$timearray[$i]="0".$i."h";
		else
			$timearray[$i]=$i."h";
	}
	
	if($slot==5)
	{
		if(3>1)
		{
		$tempd[0]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from,date("Y")));
		$tempd[1]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from,date("Y")));
		$tempd[2]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from,date("Y")));
		$tempd[3]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from,date("Y")));
		$tempd[4]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from,date("Y")));
		$tempd[5]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+1,date("Y")));
		$tempd[6]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+1,date("Y")));
		$tempd[7]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+1,date("Y")));
		$tempd[8]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+1,date("Y")));
		$tempd[9]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+1,date("Y")));
		$tempd[10]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+2,date("Y")));
		$tempd[11]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+2,date("Y")));
		$tempd[12]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+2,date("Y")));
		$tempd[13]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+2,date("Y")));
		$tempd[14]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+2,date("Y")));
		$tempd[15]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+3,date("Y")));
		$tempd[16]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+3,date("Y")));
		$tempd[17]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+3,date("Y")));
		$tempd[18]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+3,date("Y")));
		$tempd[19]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+3,date("Y")));
		$tempd[20]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+4,date("Y")));
		$tempd[21]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+4,date("Y")));
		$tempd[22]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+4,date("Y")));
		$tempd[23]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+4,date("Y")));
		}
		for($i=0;$i<24;$i++){
			ImageDashedLine($im, $xBgn + $i*30, $yBgn, $xBgn + $i*30, $yEnd, $text_color);
			$xstr=$tempd[$i]." ".$timearray[($i*$slot)%24];
			Imagestringup($im, 2, $xBgn + $i*30 - 4, $yEnd+60, $xstr,  $text_color);
		}
	}
	else if($slot==7)
	{
		if(3>1)
		{
		$tempd[0]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from,date("Y")));
		$tempd[1]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from,date("Y")));
		$tempd[2]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from,date("Y")));
		$tempd[3]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from,date("Y")));
		$tempd[4]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+1,date("Y")));
		$tempd[5]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+1,date("Y")));
		$tempd[6]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+1,date("Y")));
		$tempd[7]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+2,date("Y")));
		$tempd[8]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+2,date("Y")));
		$tempd[9]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+2,date("Y")));
		$tempd[10]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+2,date("Y")));
		$tempd[11]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+3,date("Y")));
		$tempd[12]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+3,date("Y")));
		$tempd[13]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+3,date("Y")));
		$tempd[14]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+4,date("Y")));
		$tempd[15]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+4,date("Y")));
		$tempd[16]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+4,date("Y")));
		$tempd[17]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+4,date("Y")));
		$tempd[18]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+5,date("Y")));
		$tempd[19]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+5,date("Y")));
		$tempd[20]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+5,date("Y")));
		$tempd[21]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+6,date("Y")));
		$tempd[22]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+6,date("Y")));
		$tempd[23]=date("m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+6,date("Y")));
		}
		for($i=0;$i<24;$i++){
			ImageDashedLine($im, $xBgn + $i*30, $yBgn, $xBgn + $i*30, $yEnd, $text_color);
			$xstr=$tempd[$i]." ".$timearray[($i*$slot)%24];
			Imagestringup($im, 2, $xBgn + $i*30 - 4, $yEnd+60, $xstr,  $text_color);
		}
	}
	else
	{
		$tempi=0;
		
		for($i=0;$i<24;$i++){
			
			ImageDashedLine($im, $xBgn + $i*30, $yBgn, $xBgn + $i*30, $yEnd, $text_color);
			if(($i*$slot)%24==0&&$i!=0)
				$tempi++;
			$xstr=$datestr[$tempi]." ".$timearray[($i*$slot)%24];
			Imagestringup($im, 2, $xBgn + $i*30 - 4, $yEnd+60, $xstr,  $text_color);
		}
	}
	
	
	if($csta_type==0)
	{
		//====graphics title=====
		$graphics_title="Service erlang statistic graphic";
		Imagestring($im, 2, 300,5, $graphics_title,  $text_color);
		
		//===draw the label block====
		$color[0]=ImageColorAllocate($im,0x99,0x22,0x33);
		$color[1]=ImageColorAllocate($im,0x99,0x00,0xcc);
		$color[2]=ImageColorAllocate($im,0x99,0x99,0x00);
		$color[3]=ImageColorAllocate($im,0x99,0x99,0x99);
		$color[4]=ImageColorAllocate($im,0x99,0xff,0x33);
		$color[5]=ImageColorAllocate($im,0xff,0x99,0x66);
		$color[6]=ImageColorAllocate($im,0xff,0x00,0x00);
		$color[7]=ImageColorAllocate($im,0xff,0xff,0x00);
		$color[8]=ImageColorAllocate($im,0x33,0x3,0x33);
		$color[9]=ImageColorAllocate($im,0x00,0xcc,0xff);
		$color[10]=ImageColorAllocate($im,0x00,0x22,0xcc);
		$color[11]=ImageColorAllocate($im,0xEE,0x00,0xff);
	
	
		imagefilledrectangle($im,90,350,100,360,$color[0]);
		imagestring($im,2,105,350,"SUB SUB",$text_color);
		
		imagefilledrectangle($im,260,350,270,360,$color[1]);
		imagestring($im,2,275,350,"SUB TRK",$text_color);
		
		imagefilledrectangle($im,430,350,440,360,$color[2]);
		imagestring($im,2,445,350,"TRK SUB",$text_color);
		
		imagefilledrectangle($im,600,350,610,360,$color[3]);
		imagestring($im,2,615,350,"TRK TRK",$text_color);
		
		imagefilledrectangle($im,90,370,100,380,$color[4]);
		imagestring($im,2,105,370,"INTRA",$text_color);
		
		imagefilledrectangle($im,260,370,270,380,$color[5]);
		imagestring($im,2,275,370,"SUB IP",$text_color);
		
		imagefilledrectangle($im,430,370,440,380,$color[6]);
		imagestring($im,2,445,370,"TRK IP",$text_color);
		
		imagefilledrectangle($im,600,370,610,380,$color[7]);
		imagestring($im,2,615,370,"MO",$text_color);
		
		imagefilledrectangle($im,90,390,100,400,$color[8]);
		imagestring($im,2,105,390,"MT",$text_color);
		
		imagefilledrectangle($im,260,390,270,400,$color[9]);
		imagestring($im,2,275,390,"IN TRK",$text_color);
		
		imagefilledrectangle($im,430,390,440,400,$color[10]);
		imagestring($im,2,445,390,"OUT TRK",$text_color);
		
		imagefilledrectangle($im,600,390,610,400,$color[11]);
		imagestring($im,2,615,390,"IP",$text_color);
		

		//get the biggest Y-scale value
		$maxYNum=0;
		for($i=0;$i<12;$i++)
		{
			for($j=0;$j<$slot*24;$j++)
			{
				if(!isset($erlang_service_mangdays[$i][$j]))
					$erlang_service_mangdays[$i][$j]=0.0;
				if($line_select[$i])
				{	
					if($maxYNum<$erlang_service_mangdays[$i][$j])
						$maxYNum=$erlang_service_mangdays[$i][$j];
				}	
				//echo "g-$i-h-$j:{$erlang_service_mangdays[$i][$j]}<br>";	
			}
		}
		
		
			
		//====get the points scale====
		if($maxYNum<10)
			$maxYNum=20;
		$unitValue=$maxYNum/10;
		for($i=0;$i<12;$i++)
		{
			for($j=0;$j<24*$slot;$j++)
			{
				$pointscale[$i][$j]['x']=60+30*$j/$slot;
				$pointscale[$i][$j]['y']=25+(250-$erlang_service_mangdays[$i][$j]*25/$unitValue);
			}
		}
		
		
		for($type_index=0;$type_index<12;$type_index++)
		{
			if($line_select[$type_index]==1)
			{
				for($i=0;$i<24*$slot-1;$i++){
					imageline($im,$pointscale[$type_index][$i]['x'],$pointscale[$type_index][$i]['y'],$pointscale[$type_index][$i+1]['x'],$pointscale[$type_index][$i+1]['y'],$color[$type_index]);
				}
			}
		}
		
		//echo "lineTop=$lineTop<br>";
		if($line_select[$lineTop]==1)
		{
			for($i=0;$i<24*$slot-1;$i++)
			{
				imageline($im,$pointscale[$lineTop][$i]['x'],$pointscale[$lineTop][$i]['y'],$pointscale[$lineTop][$i+1]['x'],$pointscale[$lineTop][$i+1]['y'],$color[$lineTop]);
				imageline($im,$pointscale[$lineTop][$i]['x'],$pointscale[$lineTop][$i]['y']+1,$pointscale[$lineTop][$i+1]['x'],$pointscale[$lineTop][$i+1]['y']+1,$color[$lineTop]);
				imageline($im,$pointscale[$lineTop][$i]['x'],$pointscale[$lineTop][$i]['y']+2,$pointscale[$lineTop][$i+1]['x'],$pointscale[$lineTop][$i+1]['y']+2,$color[$lineTop]);

			}
		}
		
		
	}
	else if($csta_type==1)
	{
		$graphics_title="E1 erlang statistic graphic";
		Imagestring($im, 2, 300,5, $graphics_title,  $text_color);
		
		//get the block of color
		//blue
		$blockcolor[0]=ImageColorAllocate($im,0,0,255);
		//green
		$blockcolor[1]=ImageColorAllocate($im,0,255,0);
		//red
		$blockcolor[2]=ImageColorAllocate($im,255,0,0);
		imagefilledrectangle($im,90,350,100,360,$blockcolor[0]);
		imagestring($im,2,105,350,$service_on_top_label[0],$text_color);
		
		imagefilledrectangle($im,260,350,270,360,$blockcolor[1]);
		imagestring($im,2,275,350,$service_on_top_label[1],$text_color);
		
		imagefilledrectangle($im,430,350,440,360,$blockcolor[2]);
		imagestring($im,2,445,350,$service_on_top_label[2],$text_color);
		
		//==get the max num of y scale=====
		$maxYNum=0;
		for($i=0;$i<$slot*24;$i++)
		{
			$erlang_e1_mangdays[2][$i]=$erlang_e1_mangdays[0][$i]+$erlang_e1_mangdays[1][$i];
			//echo "in:{$in_data[$i]} out:{$out_data[$i]} total:{$total_data[$i]}<br>";
			if($maxYNum<$erlang_e1_mangdays[2][$i])
				$maxYNum=$erlang_e1_mangdays[2][$i];
		}
		
		if($maxYNum<10)
			$maxYNum=20;
		$unitValue=$maxYNum/10;
		
		//====draw the data line=====
		for($type=0;$type<3;$type++)
		{
			for($i=0;$i<24*$slot;$i++)
			{
				$pointscale[$i]['x']=60+30*$i/$slot;
				$pointscale[$i]['y']=25+(250-$erlang_e1_mangdays[$type][$i]*25/$unitValue);
			}
			for($i=0;$i<24*$slot-1;$i++)
				imageline($im,$pointscale[$i]['x'],$pointscale[$i]['y'],$pointscale[$i+1]['x'],$pointscale[$i+1]['y'],$blockcolor[$type]);
		}
		
		//===draw the on top line again===
		$on_top_select=$on_top_select+0;
		for($i=0;$i<24*$slot;$i++)
		{
			$pointscale[$i]['x']=60+30*$i/$slot;
			$pointscale[$i]['y']=25+(250-$erlang_e1_mangdays[$on_top_select][$i]*25/$unitValue);
		}
		for($i=0;$i<24*$slot-1;$i++)
			imageline($im,$pointscale[$i]['x'],$pointscale[$i]['y'],$pointscale[$i+1]['x'],$pointscale[$i+1]['y'],$blockcolor[$on_top_select]);
		
	}
	else if($csta_type==2)
	{
		$graphics_title="Trunk group ".$trunk_num." erlang statistic graphic";
		Imagestring($im, 2, 300,5, $graphics_title,  $text_color);
		
		//get the block color
		//blue
		$blockcolor[0]=ImageColorAllocate($im,0,0,255);
		//green
		$blockcolor[1]=ImageColorAllocate($im,255,0,0);
		imagefilledrectangle($im,90,350,100,360,$blockcolor[0]);
		imagestring($im,2,105,350,$service_on_top_label[0],$text_color);
		
		imagefilledrectangle($im,220,350,230,360,$blockcolor[1]);
		imagestring($im,2,235,350,$service_on_top_label[1],$text_color);
		
		$maxYNum=0;
		for($i=0;$i<2;$i++)
		{
			for($j=0;$j<24*$slot;$j++)
			{
				if(!isset($erlang_trunk_mangdays[$i][$j]))
					$erlang_trunk_mangdays[$i][$j]=0.0;
				if($maxYNum<$erlang_trunk_mangdays[$i][$j])
				  $maxYNum=$erlang_trunk_mangdays[$i][$j];	
				  
				//echo "g-$i-H-$j:{$erlang_trunk_mangdays[$i][$j]}<br>";
			}
		}
		
		//====draw the data line====
		if($maxYNum<10)
				$maxYNum=20;
		$unitValue=$maxYNum/10;
		
		for($type=0;$type<2;$type++)
		{	
			for($i=0;$i<24*$slot;$i++)
			{
				$pointscale[$i]['x']=60+30*$i/$slot;
				$pointscale[$i]['y']=25+(250-$erlang_trunk_mangdays[$type][$i]*25/$unitValue);
			}
			for($i=0;$i<24*$slot-1;$i++)
				imageline($im,$pointscale[$i]['x'],$pointscale[$i]['y'],$pointscale[$i+1]['x'],$pointscale[$i+1]['y'],$blockcolor[$type]);
		}	
		
		//==draw the on top line again===
		$on_top_select=$on_top_select+0;
		for($i=0;$i<24*$slot;$i++)
		{
			$pointscale[$i]['x']=60+30*$i/$slot;
			$pointscale[$i]['y']=25+(250-$erlang_trunk_mangdays[$on_top_select][$i]*25/$unitValue);
		}
		for($i=0;$i<24*$slot-1;$i++)
			imageline($im,$pointscale[$i]['x'],$pointscale[$i]['y'],$pointscale[$i+1]['x'],$pointscale[$i+1]['y'],$blockcolor[$on_top_select]);
	}
	
	//====the horizonal scale======

	if($maxYNum<=10)
	{
		$maxYNum=20;
	}
	$style = array($black_color,$black_color,$black_color,$black_color,$black_color,$white_color,$white_color,$white_color,$white_color,$white_color);
	imagesetstyle($im, $style);
	for($i=0;$i<10;$i++){
		if($i!=0)
			imageline($im, $xBgn, $yBgn + $i*25, $xEnd, $yBgn + $i*25, IMG_COLOR_STYLED);
		$scale=$maxYNum / 10 * (10 - $i);
		if($scale>1000*1000){
			$displayMaxData = round($scale/100000)/10;
			$displayMaxData = $displayMaxData."M";	
		}elseif($scale>1000){
			$displayMaxData = round($scale/100)/10;
			$displayMaxData = $displayMaxData."K";	
		}else
			$displayMaxData=$scale;
		Imagestring($im, 2, $xBgn - 50, $yBgn + $i*25 - 5, "$displayMaxData",  $text_color);	
	}
	
	//-----输出图形文件，请勿修改
	//delete *.png
	$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);
	}
	$tt=time()-4000;
	$filename=$tt.".png";
	if(1){
		ImagePng ($im,"$filename");  
		ImageDestroy($im);	
	}
	//-----显示图片
	flush();
	echo "</form>";
	echo "<img src=\"$filename\"></img>";
	

adjust_content_tail();

echo "</body>";
?>