<?php

/*

  osCommRes, Services Online
  http://www.oscommres.com

  Copyright (c) 2005 osCommRes

  Released under the GNU General Public License
  
  Freeway eCommerce from ZacWare
http://www.zac-ware.com/freeway

Copyright 2007 ZacWare Pty. Ltd
*/
// Set flag that this is a parent file
	define( '_FEXEC', 1 );
  require('includes/application_top.php');
  tep_get_last_access_file();define('FPDF_FONTPATH','font/');
  require('includes/fpdf.php');
  require("barcode/image.php");
  $action = (isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : '');
  $template_type = (isset($HTTP_GET_VARS['template_type']) ? $HTTP_GET_VARS['template_type'] : '');
  $env_type = (isset($HTTP_GET_VARS['type']) ? $HTTP_GET_VARS['type'] : '');
  $tem_query=tep_db_query("select template_type from ".TABLE_GENERAL_TEMPLATES." where template_type='". tep_db_input($template_type)."'");
  if(tep_db_num_rows($tem_query)==0 && $action=='update_template') $action='insert_template';  
  if($action!=""){
  	 if($action=='create_template'){
				$teDetails=array(	'template_width'=>'9.00',
									'template_height'=>'5.50',
									'shop_logo_image'=>'oscommres_micro_logo.png',
									'shop_logo_position'=>'L',
									'member_logo_image'=>'oscommres_micro_logo.png',
									'member_logo_position'=>'L',
									'event_details_content'=>'%%First_Name%% %%Last_Name%%'."\n" .'%%Event_Name%%'."\n".'%%Events_Location%%' ."\n". '%%Start_Time%% %%End_Time%%',
									'event_details_position'=>'R',
									'oscomm_logo_image'=>"oscommres_micro_logo.png",
									'oscomm_logo_position'=>'R',
									'event_condition_content'=>'Card is valid for one until last class date.' . "\n" .'No refunds for unused' . "\n" .'classes.Make-up any missed classes'."\n".'before last class date. Renew card ph'."\n".'12345678, Freeway.com',
									'event_condition_position'=>'L',
									'event_condition_large'=>'Card is valid for one until last class date.' . "\n" .'No refunds for unused' . "\n" .'classes.Make-up any missed classes'."\n".'before last class date. Renew card ph'."\n".'12345678, Freeway.com',
									'bar_image_position'=>'R',
									'customer_details_content'=>'%%First_Name%% %%Last_Name%%'
									);
									
				if ($template_type=="ICD") $teDetails["event_condition_content"]="";
				$template_query=tep_db_query("SELECT template_type,template_width,template_height,template_content from " . TABLE_GENERAL_TEMPLATES . " where template_type='" . tep_db_input($template_type) . "'");
				if (tep_db_num_rows($template_query)>0){
					$template_result=tep_db_fetch_array($template_query);					
					$template_splt=split("{}",$template_result["template_content"]);

					for ($icnt=0;$icnt<count($template_splt);$icnt=$icnt+2){
						$key=$template_splt[$icnt];
						$teDetails[$key]=$template_splt[$icnt+1];
					}
					$teDetails["template_width"]=$template_result["template_width"];
					$teDetails["template_height"]=$template_result["template_height"];
					$teInfo=new objectInfo($teDetails);

				} else $teInfo=new objectInfo($teDetails);					
				$merge_fields=array(array('id'=>'FN','text'=>TEXT_FN),
									array('id'=>'LN','text'=>TEXT_LN),
									array('id'=>'EN','text'=>TEXT_EN),
									array('id'=>'EL','text'=>TEXT_EL),
									array('id'=>'SI','text'=>TEXT_SI),
									array('id'=>'EI','text'=>TEXT_EI)
									);
				$position_array=array(	array('id'=>'L','text'=>TEXT_ALIGN_LEFT),
										array('id'=>'R','text'=>TEXT_ALIGN_RIGHT),
								);
								
				?>
					<table border="0" cellspacing="0" cellpadding="2" width="100%">						
						<tr class="openContent_top">
							<td align="center"  onClick="javascript:do_function('close','<?php echo $template_type;?>');">
							  <table border='0' cellspacing='0' cellpadding='0' style='height:30px;display:inline;position:relative;'>
							  	 <tr class='img_move' id='img_ids'>
									 <td class='cell_bg_popm_left' width='10'>&nbsp;</td>
									 <td width='10%' onClick="javascript:doaction('delete_template');"><img alt='Delete' title='Delete' src='images/template/img_trash.gif'></td>						 
									 <td width='10%' onClick="javascript:doaction('preview_<?php echo $template_type;?>');"><img alt='Preview' title='Preview' src='images/template/img_search.gif'></td>
									 <td class='cell_bg_popm_right' width='10'>&nbsp;</td>
							  	 </tr>
				  			  </table>	
							</td><td  width="200" onClick="javascript:do_function('close','<?php echo $template_type;?>');"></td>
							<td width="80" align='right'><input type='image' src='images/template/img_savel.gif' alt='save' title='save' border='0'>&nbsp;&nbsp;<a href="javascript:do_function('close','<?php echo $template_type;?>');"><img src='images/template/img_closel.gif' alt='close' title='close' border='0'></a>&nbsp;&nbsp;</td>
						</tr>
						<tr>
							<td><?php echo tep_draw_separator('pixel_trans.gif',1,15);?></td>
						</tr>
						<tr><td class='pageHeading'><?php echo (tep_db_num_rows($tem_query)==0)?TEMPLATE_NEW:TEMPLATE_EDIT;?></td></tr>
						<tr>
							<td>
							<table border="0" cellspacing="0" cellpadding="2">
								<tr>
									<td  class="main" colspan="2"><b><?php echo TEXT_SIZE;?></b></td>
								</tr>
								<tr>
									<td>
									<table border="0" cellspacing="0" cellpadding="2">
										<tr>
											<td class="main" width="100"><?php echo TEXT_WIDTH; ?></td>
											<Td  class="main"><?php echo tep_draw_input_field('template_width',number_format($teInfo->template_width,2),'size="10"');?> cm</Td>
										</tr>
										<tr>
											<td class="main"><?php echo TEXT_HEIGHT; ?></td>
											<Td  class="main"><?php echo tep_draw_input_field('template_height',number_format($teInfo->template_height,2),'size="10"');?> cm</Td>
										</tr>
									</table>
									</td>
								</tr>
								<tr>
									<td><?php echo tep_draw_separator('pixel_trans.gif',1,10);?></td>
								</tr>
								<?php if ($template_type=="ICD") { ?>
								<tr>
									<td  class="main" colspan="2"><b><?php echo TEXT_MEMBER_LOGO;?></b></td>
								</tr>
								<tr>
									<td>
									<table border="0" cellspacing="0" cellpadding="2">
										<tr>
											<td class="main" width="100"><?php echo TEXT_IMAGE; ?></td>
											<Td  class="main"><?php echo tep_draw_input_field('member_logo_image',$teInfo->member_logo_image,'size="30"') . '&nbsp;' . tep_image(DIR_WS_IMAGES.'template/jpg.gif',IMAGE_INSERT_IMAGE,' style="cursor:hand;cursor:pointer;" onClick="javascript:image_popup(\'member_logo_image\');" align="absmiddle"');?></Td>
										</tr>
										<tr>
											<td class="main"><?php echo TEXT_POSITION; ?></td>
											<Td  class="main"><?php echo tep_draw_pull_down_menu('member_logo_position',$position_array,$teInfo->member_logo_position);?></Td>
										</tr>
									</table>
									</td>
								</tr>
								<?php }  else { ?>

								<tr>
									<td  class="main" colspan="2"><b><?php echo TEXT_SHOP_LOGO;?></b></td>
								</tr>
								<tr>
									<td>
									<table border="0" cellspacing="0" cellpadding="2">
										<tr>
											<td class="main" width="100"><?php echo TEXT_IMAGE; ?></td>
											<Td  class="main" valign="middle"><?php echo tep_draw_input_field('shop_logo_image',$teInfo->shop_logo_image,'size="30"') .'&nbsp;' . tep_image(DIR_WS_IMAGES.'template/jpg.gif',IMAGE_INSERT_IMAGE,' style="cursor:hand;cursor:pointer;" onClick="javascript:image_popup(\'shop_logo_image\');" align="absmiddle"');?></Td>
										</tr>
										<tr>
											<td class="main"><?php echo TEXT_POSITION; ?></td>
											<Td  class="main"><?php echo tep_draw_pull_down_menu('shop_logo_position',$position_array,$teInfo->shop_logo_position);?></Td>
										</tr>
									</table>
									</td>
								</tr>
								<?php } ?>
								<tr>
									<td><?php echo tep_draw_separator('pixel_trans.gif',1,10);?></td>
								</tr>
								<tr>
									<td  class="main" colspan="2"><b><?php echo TEXT_OSCOMM_LOGO;?></b></td>
								</tr>
								<tr>
									<td>
									<table border="0" cellspacing="0" cellpadding="2">
										<tr>
											<td class="main" width="100"><?php echo TEXT_IMAGE; ?></td>
											<Td  class="main"><?php echo  tep_draw_input_field('oscomm_logo_image',$teInfo->oscomm_logo_image,'size="30"') . '&nbsp;' . tep_image(DIR_WS_IMAGES.'template/jpg.gif',IMAGE_INSERT_IMAGE,' style="cursor:hand;cursor:pointer;" onClick="javascript:image_popup(\'oscomm_logo_image\');" align="absmiddle"');?></Td>
										</tr>
										<tr>
											<td class="main"><?php echo TEXT_POSITION; ?></td>
											<Td  class="main"><?php echo tep_draw_pull_down_menu('oscomm_logo_position',$position_array,$teInfo->oscomm_logo_position);?></Td>
										</tr>
									</table>
									</td>
								</tr>
								<tr>
									<td><?php echo tep_draw_separator('pixel_trans.gif',1,10);?></td>
								</tr>
								<tr>
									<td  class="main" colspan="2"><b><?php echo TEXT_BAR_CODE_IMAGE;?></b></td>
								</tr>
								<tr>
									<td>
									<table border="0" cellspacing="0" cellpadding="2">
										<tr>
											<td class="main" width="100"><?php echo TEXT_POSITION; ?></td>
											<Td  class="main"><?php echo tep_draw_pull_down_menu('bar_image_position',$position_array,$teInfo->bar_image_position);?></Td>
										</tr>
									</table>
									</td>
								</tr>
								<tr>
									<td><?php echo tep_draw_separator('pixel_trans.gif',1,10);?></td>
								</tr>
								<tr>
									<td  class="main" colspan="2"><b><?php echo TEXT_EVENT_CONDITION;?></b></td>
								</tr>
								<tr>
									<td>
									<table border="0" cellspacing="0" cellpadding="2">
										<tr>
											<td class="main" width="100" valign="top"><?php echo TEXT_EVENT_CONDITION; ?></td>
											<Td  class="main"><?php echo tep_draw_textarea_field('event_condition_content','',40,6,$teInfo->event_condition_content);?></Td>
										</tr>
										<tr>
											<td class="main"><?php echo TEXT_POSITION; ?></td>
											<Td  class="main"><?php echo tep_draw_pull_down_menu('event_condition_position',$position_array,$teInfo->event_condition_position);?></Td>
										</tr>
										<?php if ($template_type=="ICD") { ?>
										<tr>
											<td class="main" valign="top"><?php echo TEXT_CONDITION_FULL; ?></td>
											<Td  class="main"><?php echo tep_draw_textarea_field('event_condition_large','',50,10,$teInfo->event_condition_large);?></Td>
										</tr>
										<?php } ?>
									</table>
									</td>
								</tr>
								<?php if ($template_type=="ICD") { 
										$customer_fields=array(array('id'=>'FN','text'=>TEXT_FN),array('id'=>'LN','text'=>TEXT_LN));
								?>
								<tr>
									<td  class="main" colspan="2"><b><?php echo TEXT_CUSTOMER_DETAILS;?></b></td>
								</tr>
								<tr>
									<td>
									<table border="0" cellspacing="0" cellpadding="2">
										<tr>
											<Td class="main" valign="top" width="100"><?php echo TEXT_CONTENT; ?></Td>
											<Td class="main" valign="top"><?php echo tep_draw_textarea_field('customer_details_content','',40,6,$teInfo->customer_details_content); ?></Td>
											<Td class="main" valign="top"><?php echo tep_draw_pull_down_menu('merge_fields',$customer_fields,'','size=5 onDblClick="javascript:AddField(\'customer_details_content\');"'); ?></Td>
										</tr>
									</table>
									</td>
								</tr>
								<?php } else { ?>
									<tr>
										<td  class="main" colspan="2"><b><?php echo TEXT_EVENT_DETAILS;?></b></td>
									</tr>
									<tr>
										<td>
										<table border="0" cellspacing="0" cellpadding="2">
											<tr>
												<Td class="main" valign="top" width="100"><?php echo TEXT_CONTENT; ?></Td>
												<Td class="main" valign="top"><?php echo tep_draw_textarea_field('event_details_content','',40,6,$teInfo->event_details_content); ?></Td>
												<Td class="main" valign="top"><?php echo tep_draw_pull_down_menu('merge_fields',$merge_fields,'','size=5 onDblClick="javascript:AddField(\'event_details_content\');"'); ?></Td>
											</tr>
											<tr>
												<td class="main"><?php echo TEXT_POSITION;?></td>
												<td class="main" colspan="2"><?php echo tep_draw_pull_down_menu('event_details_position',$position_array,$teInfo->event_details_position);?></td>
											</tr>
										</table>
										</td>
									</tr>
								<?php } ?>
							</table>
						</td>
					</tr>
					<tr>
						<td><?php echo tep_draw_separator('pixel_trans.gif',1,10);?></td>
					</tr>
					<Tr>
						<td colspan="2" align='right'><input type='image' src='images/template/img_savel.gif' alt='save' title='save' border='0'>&nbsp;&nbsp;<a href="javascript:do_function('close','<?php echo $template_type;?>');"><img src='images/template/img_closel.gif' alt='close' title='close' border='0'></a>&nbsp;&nbsp;</td>
					</Tr>
				</table>
		<?php   		  		
  	 	}else if($action=='update_template' || $action=='insert_template'){
			$sql_array=array(	"template_width"=>$HTTP_POST_VARS["template_width"],
								"template_height"=>$HTTP_POST_VARS["template_height"]);
			 $tem_query=tep_db_query("select template_type from ".TABLE_GENERAL_TEMPLATES." where template_type='".tep_db_input($template_type)."'");
			if(!$tem_query) $action='insert_template';  									
			if ($template_type=="ICD") {
				$content.="member_logo_image{}" . tep_db_prepare_input($HTTP_POST_VARS["member_logo_image"]);
				$content.="{}member_logo_position{}" . tep_db_prepare_input($HTTP_POST_VARS["member_logo_position"]);
			} else {
				$content.="shop_logo_image{}" . tep_db_prepare_input($HTTP_POST_VARS["shop_logo_image"]);
				$content.="{}shop_logo_position{}" . $HTTP_POST_VARS["shop_logo_position"];
			}
			if ($template_type=="ICD") {
				$content.="{}customer_details_content{}" . tep_db_prepare_input($HTTP_POST_VARS["customer_details_content"]);
			} else {
				$content.="{}event_details_content{}" . tep_db_prepare_input($HTTP_POST_VARS["event_details_content"]);
				$content.="{}event_details_position{}" . tep_db_prepare_input($HTTP_POST_VARS["event_details_position"]);
			}
			$content.="{}oscomm_logo_image{}" . tep_db_prepare_input($HTTP_POST_VARS["oscomm_logo_image"]);
			$content.="{}oscomm_logo_position{}" . tep_db_prepare_input($HTTP_POST_VARS["oscomm_logo_position"]);
			$content.="{}event_condition_content{}" . tep_db_prepare_input($HTTP_POST_VARS["event_condition_content"]);
			$content.="{}event_condition_position{}" . tep_db_prepare_input($HTTP_POST_VARS["event_condition_position"]);
			if ($template_type=="ICD") {
				$content.="{}event_condition_large{}" . tep_db_prepare_input($HTTP_POST_VARS["event_condition_large"]);
			}
			$content.="{}bar_image_position{}" . tep_db_prepare_input($HTTP_POST_VARS["bar_image_position"]);

			$sql_array["template_content"]=$content;
			if ($action=="insert_template"){
				$sql_array["template_type"]=$template_type;
				tep_db_perform(TABLE_GENERAL_TEMPLATES,$sql_array);
			} else
				tep_db_perform(TABLE_GENERAL_TEMPLATES,$sql_array,"update","template_type='" . $template_type . "'");
			echo 'update_template';				
  	 }else if($action=='preview_TIC'){
  	 		$unit=72/25.4;
			$width=(int)$HTTP_POST_VARS["template_width"];
			$height=(int)$HTTP_POST_VARS["template_height"];
			if ($width<=0) $width=9.00;
			if ($height<=0) $height=5.00;

			$ticket_width=$width*10;
			$ticket_height=$height*10;
			
			$tInfo=new objectInfo($HTTP_POST_VARS);

			$tInfo->shop_logo_image=DIR_FS_CATALOG_IMAGES . $tInfo->shop_logo_image;
			$tInfo->oscomm_logo_image=DIR_FS_CATALOG_IMAGES . $tInfo->oscomm_logo_image;
			// set page setup default width and height
			$pdf=new FPDF("P","mm",array($ticket_width,$ticket_height));
	
			$pos_left=5;
			$pos_top=5;
			$pos_width=$ticket_width;
			$pos_height=$ticket_height;
			$content=array();
			
			// get info of image
			if (file_exists($tInfo->shop_logo_image)){
				$info_shop=getimagesize($tInfo->shop_logo_image);
				$info_shop[0]=($info_shop[0]/$unit)*74/100;
				$info_shop[1]=($info_shop[1]/$unit)*74/100;
			}
			
			if (file_exists($tInfo->oscomm_logo_image)){
				$info_oscomm=getimagesize($tInfo->oscomm_logo_image);
				$info_oscomm[0]=($info_oscomm[0]/$unit)*74/100;
				$info_oscomm[1]=($info_oscomm[1]/$unit)*120/100;
			}
			//require("barcode/image.php");
			$pdf->AddPage();
			$pdf->SetFont('Arial','B',10);
			$pos_top=5;
			$pos_left=5;

			// create bar image
			$bar_filename=DIR_FS_CATALOG_IMAGES . "ticket_bar_1_1.png";
			if (!file_exists($bar_filename)) create_bar_image("1-1",$bar_filename);
			// get info
			if (file_exists($bar_filename)){
				$info_bar=getimagesize($bar_filename);
				$info_bar[0]=$info_bar[0]/$unit;
				$info_bar[1]=$info_bar[1]/$unit;
				$bar_width=$info_bar[0];
			}

			// adjust for the bar image			
			if ($tInfo->bar_image_position=="L")
				$pos_left=$bar_width+2;
			else 
				$pos_width=$ticket_width-$bar_width-2;
			$content_width=$ticket_width-$bar_width-2;
			//bar image
			if (isset($info_bar)){
				if ($tInfo->bar_image_position=="L")
					$pdf->Image($bar_filename,2,2);
				else 
					$pdf->Image($bar_filename,$pos_width,2);				
			}

			// shop logo
			if (isset($info_shop)){
				if ($tInfo->shop_logo_position=="L"){
					$pdf->Image($tInfo->shop_logo_image,($tInfo->bar_image_position=="L"?$pos_left:0),0,$ticket_width/5,$ticket_height/4);
				} else  {
					$pdf->Image($tInfo->shop_logo_image,($tInfo->bar_image_position=="L"?$pos_width-$info_shop[0]:$pos_width-$info_shop[0]-2),0,$ticket_width/5,$ticket_height/4);
				}
			}
			$pdf->SetFont('Arial','B',10);
			//event details
			$content=$tInfo->event_details_content;
	
			$splt_line=split("\n",$content);
			$temp_width=0;
			for ($icnt=0;$icnt<count($splt_line);$icnt++){
				$str_width=$pdf->GetStringWidth($splt_line[$icnt]);
				if ($str_width>$temp_width)	$temp_width=$str_width;
			}

			if ($tInfo->event_details_position=="L"){
				$temp_left=$pos_left;
			} else {
				$temp_left=$pos_width-2-$temp_width;
			}
			
			for ($icnt=0;$icnt<count($splt_line);$icnt++){
				if ($icnt>=1) $pdf->SetFont('Arial','B',8);
				$pdf->Text($temp_left,$pos_top,$splt_line[$icnt]);
				$pos_top+=3;
			}
			$pos_top+=2;	
			$pdf->SetFont('Arial','B',10);
			// session dates
			$temp_top=$pos_top;
			$temp_width=$content_width-10;
			$temp_left=$pos_left;
			$part_width=($temp_width-($pdf->GetStringWidth("00-00-0000")*3))/2;
			$cnt=0;
			$pdf->Text($temp_left,$temp_top,getServerDate());
			$temp_left+=$part_width+$pdf->GetStringWidth(getServerDate());

			$pdf->SetFont('Arial','B',8);
			
			$pos_top+=10;

			// Expiry Date
			$temp_str="Expiry Date: " . getServerDate();
			$pdf->Text($ticket_width-$pdf->GetStringWidth($temp_str)-2,$pos_top,$temp_str);

			if (!isset($cond_splt)){
				$cond_height=0;
				$cond_splt=split("\n",$tInfo->event_condition_content);
				$cond_height=count($cond_splt)*3;
				$cond_width=0;
				$temp_width=0;
				for ($icnt=0;$icnt<count($cond_splt);$icnt++){
					$temp_width=$pdf->GetStringWidth($cond_splt[$icnt]);
					if ($temp_width>$cond_width) $cond_width=$temp_width;
				}
			}
			
			// print Freeway
			$temp_left=0;
			$temp_right=$ticket_width-$info_oscomm[0];
			if (isset($info_oscomm)){
				if ($tInfo->oscomm_logo_position=="L"){
					$pdf->Image($tInfo->oscomm_logo_image,$temp_left,$ticket_height-$info_oscomm[1],$ticket_width/3,$ticket_height/3);
					$temp_left+=$info_oscomm[0];
				} else  {
					$pdf->Image($tInfo->oscomm_logo_image,$temp_right,$ticket_height-$info_oscomm[1],$ticket_width/3,$ticket_height/3);
				}
			}
			
			
			// conditions
			$temp_top=$ticket_height-$cond_height;
			if ($tInfo->event_condition_position=="L"){
				$cond_left=$temp_left+2;
			} else {
				$cond_left=$ticket_width-$cond_width;
			}
			for ($icnt=0;$icnt<count($cond_splt);$icnt++){
				$pdf->Text($cond_left,$temp_top,$cond_splt[$icnt]);
				$temp_top+=3;
			}
			$pdf->output(DIR_FS_CATALOG . "images/preview_ticket.pdf");
			echo HTTP_SERVER.DIR_WS_CATALOG . "images/preview_ticket.pdf";
  	 }else if($action=='preview_ICD'){
  	 		$width=$HTTP_POST_VARS["template_width"];
			$height=$HTTP_POST_VARS["template_height"];
			if ($width<=0) $width=9.00;
			if ($height<=0) $height=5.00;
			$unit=72/25.4;

			$ticket_width=$width*10;
			$ticket_height=$height*10;

			$tInfo=new objectInfo($HTTP_POST_VARS);
			
			$tInfo->member_logo_image=DIR_FS_CATALOG_IMAGES . $tInfo->member_logo_image;
			$tInfo->oscomm_logo_image=DIR_FS_CATALOG_IMAGES . $tInfo->oscomm_logo_image;
			// set page setup default width and height
			$pdf=new FPDF("P","mm",array($ticket_width,$ticket_height));

			$pos_left=5;
			$pos_top=5;
			$pos_width=$ticket_width;
			$pos_height=$ticket_height;
			$content=array();
			if (file_exists($tInfo->oscomm_logo_image)){
				$info_oscomm=getimagesize($tInfo->oscomm_logo_image);
				$info_oscomm[0]=$info_oscomm[0]/$unit*(74/100);
				$info_oscomm[1]=$info_oscomm[1]/$unit*(120/100);
				$bottom_height=$info_oscomm[1];
			}
			if (file_exists($tInfo->member_logo_image)){
				$info_member=getimagesize($tInfo->member_logo_image);
				$info_member[0]=$info_member[0]/$unit*(74/100);
				$info_member[1]=$info_member[1]/$unit*(74/100);
			}
			$bar_width=15;			
			$pdf->AddPage();
			$pdf->SetFont('Arial','B',10);
			$pos_left=5;
			$pos_top=5;

			// create bar image
			$bar_text=1;
			$bar_filename=DIR_FS_CATALOG_IMAGES . "id_bar_1.png";
			if (!file_exists($bar_filename)){
				create_bar_image("1",$bar_filename);
			}

			if (file_exists($bar_filename)){
				$info_bar=getimagesize($bar_filename);
				$info_bar[0]=$info_bar[0]/$unit*74/100;
				$info_bar[1]=$info_bar[1]/$unit*74/100;
				$bar_width=$info_bar[0];
			}
			
			// adjust for the bar image
			if ($tInfo->bar_image_position=="L"){
				$pos_left=$bar_width+2;
			} else {	
				$pos_width=$ticket_width-$bar_width-2;
			}
			$content_width=$ticket_width-$bar_width-2;

			//bar image
			if (isset($info_bar)){
				if ($tInfo->bar_image_position=="L"){
					$pdf->Image($bar_filename,2,2);
				} else {
					$pdf->Image($bar_filename,$pos_width,2);
				}
			}
			
			// print member logo
			if (isset($info_member)){
				if ($tInfo->member_logo_position=="L"){
					$pdf->Image($tInfo->member_logo_image,($tInfo->bar_image_position=="L"?$pos_left:0),0,$ticket_width/5,$ticket_height/4);
				} else  {
					$pdf->Image($tInfo->member_logo_image,($tInfo->bar_image_position=="L"?$pos_width-$info_member[0]:$pos_width-$info_member[0]-2),0,$ticket_width/5,$ticket_height/4);
				}
			}
			
			
			// conditions
			$pdf->SetFont('Arial','B',8);
			if (!isset($cond_splt)){
				$cond_height=0;
				$cond_splt=split("\n",$tInfo->event_condition_content);
				$cond_height=count($cond_splt)*3;
				$cond_width=0;
				$temp_width=0;
				for ($icnt=0;$icnt<count($cond_splt);$icnt++){
					$temp_width=$pdf->GetStringWidth($cond_splt[$icnt]);
					if ($temp_width>$cond_width) $cond_width=$temp_width;
				}
			}
			
			// print Freeway
			$temp_left=0;
			$temp_right=$ticket_width-$info_oscomm[0];
			if (isset($info_oscomm)){
				if ($tInfo->oscomm_logo_position=="L"){
					$pdf->Image($tInfo->oscomm_logo_image,$temp_left,$ticket_height-$info_oscomm[1],$ticket_width/3,$ticket_height/3);
					$temp_left+=$info_oscomm[0];
				} else  {
					$pdf->Image($tInfo->oscomm_logo_image,$temp_right,$ticket_height-$info_oscomm[1],$ticket_width/3,$ticket_height/3);
				}
			}
			
			$temp_top=$ticket_height-2-$cond_height;
			if ($tInfo->event_condition_position=="L"){
				$cond_left=$temp_left+2;
			} else {
				$cond_left=$temp_right-2-$cond_width;
			}
			for ($icnt=0;$icnt<count($cond_splt);$icnt++){
				$pdf->Text($cond_left,$temp_top,$cond_splt[$icnt]);
				$temp_top+=3;
				$bottom_height1+=3;
			}

			$pdf->SetFont('Arial','',8);
			// full condition
			if ($bottom_height>$bottom_height1){
				$temp_height=$bottom_height;
			} else {
				$temp_height=$bottom_height1;
			}
			$temp_left=$pos_left;
			$temp_top=20+$name_height;
			$content=$tInfo->event_condition_large;
			$word="";
			$current_width=0;
			for ($icnt=0;$icnt<strlen($content);$icnt++){
				$char=substr($content,$icnt,1);
				if ($char==" " || $char=="\n"){
					if ($current_width+$pdf->GetStringWidth($word)>$content_width-8){
						$temp_left=$pos_left;
						$temp_top+=3;
						$current_width=0;
					} else if ($char!="\n") {
						$word.=$char;
					}
					$pdf->Text($temp_left,$temp_top,$word);
					if ($char!="\n") {
						$temp_left+=$pdf->GetStringWidth($word);
						$current_width+=$pdf->GetStringWidth($word);
					} else {
						$temp_left=$pos_left;
						$temp_top+=3;
						$current_width=0;
					}
					$word="";
				}  else {
					$word.=$char;
				}
			}
			if ($word!=""){
				if ($current_width+$pdf->GetStringWidth($word)>$content_width-8){
					$temp_left=$pos_left;
					$temp_top+=3;
					$current_width=0;
				}
				$pdf->Text($temp_left,$temp_top,$word);
			}
			$pdf->SetFont('Arial','B',12);

			// customer name
			$content=$tInfo->customer_details_content;
			$name_splt=split("\n",$content);
			$temp_width=0;
			$name_width=0;
			$name_height=0;
			for ($icnt=0;$icnt<count($name_splt);$icnt++){
				$temp_width=$pdf->GetStringWidth($name_splt[$icnt]);
				if ($temp_width>$name_width) $name_width=$temp_width;
			}
			$temp_top=13;
			$temp_left=$pos_left+($content_width-$temp_width)/2;
			for ($icnt=0;$icnt<count($name_splt);$icnt++){
				$pdf->Text($temp_left,$temp_top,$name_splt[$icnt]);
				$temp_top+=5;
				$name_height+=5;
			}			
			$pdf->output(DIR_FS_CATALOG . "images/preview_id_card.pdf");
			echo HTTP_SERVER.DIR_WS_CATALOG . "images/preview_id_card.pdf";
  	 }else if($action=='delete_template'){
		tep_db_query("DELETE from " . TABLE_GENERAL_TEMPLATES . " where template_type='" . tep_db_input($template_type) . "'");
		echo 'delete_success';	
  	 }
  	 exit;  	 
  	}
	// array of given templates
	$template_array=array(array("id"=>"TIC",'text'=>TEXT_TIC),
						  array("id"=>"TICS",'text'=>TEXT_TICS));
	if($env_type!='TIC')
		$template_array=array(array("id"=>"ICD",'text'=>TEXT_ICD),
						  array("id"=>"ICDS",'text'=>TEXT_ICDS));	
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<style type="text/css">
	#pup {position:absolute; visibility:hidden; z-index:200; width:130; }
</style>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF" onLoad="SetFocus();">
<script language="javascript" src="includes/menu.js"></script>
<script language="javascript" src="includes/general.js"></script>
<script language="javascript" src="includes/http.js"></script>
<script language="JavaScript">
  	var insert_image;
	function AddField(box){
		var select_field;
		var select_id;		
		if (document.insert_template.merge_fields.selectedIndex<=-1) return;
				
		select_id=document.insert_template.merge_fields.options[document.insert_template.merge_fields.selectedIndex].value;
		if (select_id.length>3) return;
				
		select_field="%%" + document.insert_template.merge_fields.options[document.insert_template.merge_fields.selectedIndex].text + "%%";
		textArea=document.insert_template.elements[box];
		
		textArea.focus();
		if (textArea.selection){
			sRange  = document.selection.createRange();
			var sText   = sRange.text;
			sRange.text = select_field;
		} else if (textArea.selectionStart || textArea.selectionStart == "0") {
			var startPos = textArea.selectionStart;
			var endPos = textArea.selectionEnd;
			textArea.value = textArea.value.substring(0, startPos)
					+ select_field + textArea.value.substring(endPos, textArea.value.length);
		} else {
			textArea.value += select_field;
		}	}

		function image_popup(type){
			insert_image=document.insert_template.elements[type];
			var wh="530px";
			window.open("insert_image.php","image_window","height="+wh+",width="+wh+",location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no,left=280,top=250");
		}
		   
		function setFileName(img)
		{ 
		  insert_image.value=img;
		}
				
	function doaction(faction){ 
		frm=document.insert_template;
		var loading=document.getElementById("loading");
		if(document.getElementById("action_type")) var action=document.getElementById("action_type").value;
		if(faction) action=faction;
		if(document.getElementById("template_type")) var template_type=document.getElementById("template_type").value;
		var error="";
		var command="<?PHP echo tep_href_link(FILENAME_GENERAL_TEMPLATES,'template_type=');?>"+template_type;
		if(template_type.length==4) command="<?PHP echo tep_href_link(FILENAME_SERVICES_TEMPLATES,'template_type=');?>"+template_type;
		  if(((action=='preview_ICD' || action=='preview_ICDS') || ((action=='create_template' && (template_type=='ICD' || template_type=='ICDS')))) && (frm.member_logo_image.value=='' || frm.oscomm_logo_image.value=='' ||(frm.oscomm_logo_image.value.indexOf('.gif')>1 || frm.member_logo_image.value.indexOf('.gif')>1))) error="Image Box Cannot Empty and Image type should not be .gif file";
		  if(((action=='preview_TIC' || action=='preview_TICS') || ((action=='create_template' && (template_type=='TIC' || template_type=='TICS')))) && (frm.shop_logo_image.value=='' || frm.oscomm_logo_image.value=='' ||(frm.shop_logo_image.value.indexOf('.gif')>1 || frm.oscomm_logo_image.value.indexOf('.gif')>1)))  error="Image Box Cannot Empty and Image type should not be .gif file";
		  if(error!='') {
			  alert(error);
			  return false;
		  }else {
			if(loading) loading.style.display="";
			if(action=='create_template')
				command+="&action=update_template";
			else if(action=='delete_template')
				command+="&action=delete_template";
			else if(action=='preview_ICD' || action=='preview_TIC' || action=='preview_ICDS' || action=='preview_TICS')
				command+="&action="+action;
			/*frm.action="<?PHP echo FILENAME_GENERAL_TEMPLATES;?>?action="+action;
			if(action=='preview_ICD' || action=='preview_TIC'){
			frm.target="_blank";
			} else {
			frm.target="_self";
			}
			frm.submit();*/
		  }
		 if(command!="") do_post_command('insert_template',command);
		 return false; 
	}
	
	function do_function(actions,type){
		var command="";
		if(actions!="" && document.getElementById("action_type")) document.getElementById("action_type").value=actions;
		var template_type=document.getElementById("template_type");
		if(document.getElementById("head_"+type).style.display=="" && actions=='create_template') return;
		if(template_type && template_type.value!='') {
			var data_id=document.getElementById("data_"+template_type.value);
			var head=document.getElementById("head_"+template_type.value);
			if(data_id) data_id.style.display="";
			if(head) {
				head.style.display="none";
				head.innerHTML="";
			}			
		}
		if(document.getElementById("template_type")) document.getElementById("template_type").value=type;
		if(actions!="close"){
			var data_id=document.getElementById("data_"+type);
			var head=document.getElementById("head_"+type);			
			if(data_id) data_id.style.display="none";
			if(head){
				head.style.display="";
				head.innerHTML="<img src='images/24-1.gif' alt='Loading...' title='Loading...'>";
			}
			command="<?php echo tep_href_link(FILENAME_GENERAL_TEMPLATES,'action=');?>"+actions+"&template_type="+type;
			if(type.length==4) command="<?php echo tep_href_link(FILENAME_SERVICES_TEMPLATES,'action=');?>"+actions+"&template_type="+type;			
		}					
		if(command!="") do_get_command(command);
	}	
	function do_result(result){
		if(document.getElementById("loading")) document.getElementById("loading").style.display="none";
		var template_type=document.getElementById("template_type");
		if(template_type) {
			var data_id=document.getElementById("data_"+template_type.value);	
			var head=document.getElementById("head_"+template_type.value);
		}
		if(result!=""){
			if(template_type!='' && (result=='update_template' || result=='delete_success') && data_id && head ){
				data_id.style.display="";												
				head.style.display="none";
			}else if(result.substr(0,4)=='http')
				window.open(result);
			else if(template_type && template_type.value!='') {
				var data_id=document.getElementById("data_"+template_type.value);
				var head=document.getElementById("head_"+template_type.value);
				if(data_id) data_id.style.display="none";
				if(head) {
					head.style.display="";
					head.innerHTML=result;	
				}		
			}			
		}				
	}		
</script>
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->
<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
	<tr> 
<!-- body_text //-->
	<td width="100%" valign="top">
	<table border="0"  cellspacing="1" cellpadding="1" width="100%">
		<tr>
			<td>
			<table border="0" width="100%" cellspacing="0" cellpadding="0">
				<tr>
				  <td valign="top">
				  	<?php echo tep_draw_form('insert_template',FILENAME_GENERAL_TEMPLATES,'','post','onsubmit="javascript:return doaction();"');?>				  		
					<table border="0" cellpadding="0" cellspacing="0" width="100%">
						<tr class="dataTableHeadingRow">
							<Td class="dataTableHeadingContent">
								<?php echo TEXT_TEMPLATE_NAME;?>
							</Td>
						</Tr>
						<tr><td valign="bottom" class="smalltext" id="loading" style="display:none"><img src='images/24-1.gif' alt='Loading...' title='Loading...'></td></tr>
						<?php for ($icnt=0;$icnt<count($template_array);$icnt++){
							 $type=$template_array[$icnt]['id'];
							?>
								<tr class="dataTableRow">
									<td><?php echo tep_draw_separator('pixel_trans.gif','100%',5);?></td>
								</tr>
								<tr class="dataTableRowOdd" style="height:25px" onMouseOver="rowOverEffect(this)" onMouseOut="rowOutEffect(this)" onClick="javascript:do_function('create_template','<?php echo $type;?>');">
									<td class="dataTableContent"><div id="data_<?php echo $type;?>"><?php echo '<b>' . $template_array[$icnt]['text'] . '</b>'; ?></div><div class="openContent" style="display:none" id="head_<?php echo $type;?>"></div></td>
								</tr>
						<?php  } // for ?>
					</table>
					<input type="hidden" name="template_type" id="template_type">
					<input type="hidden" name="action_type" id="action_type">
					</form>
				   </td>
				</tr>
			</table>			
			</td>
		</tr>
	</table>
	</td>
</table>
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>