<?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']: 'TICS');
  $tem_query=tep_db_query("select template_type from ".TABLE_GENERAL_TEMPLATES." where template_type='".$template_type."'");
  if(tep_db_num_rows($tem_query)==0 && $action=='update_template') $action='insert_template';
  if($action!=""){
 	switch($action){
  		case "delete_template":
			tep_db_query("DELETE from " . TABLE_GENERAL_TEMPLATES . " where template_type='" . $template_type . "'");
			echo 'delete_success'; 
		break;
		case "insert_template":
		case "update_template":
			$sql_array=array(	"template_width"=>$HTTP_POST_VARS["template_width"],
								"template_height"=>$HTTP_POST_VARS["template_height"]);
			if ($template_type=="ICDS") {
				$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=="ICDS") {
				$content.="{}customer_details_content{}" . tep_db_prepare_input($HTTP_POST_VARS["customer_details_content"]);
			} else {
				$content.="{}service_details_content{}" . tep_db_prepare_input($HTTP_POST_VARS["service_details_content"]);
				$content.="{}service_details_position{}" . tep_db_prepare_input($HTTP_POST_VARS["service_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.="{}service_condition_content{}" . tep_db_prepare_input($HTTP_POST_VARS["service_condition_content"]);
			$content.="{}service_condition_position{}" . tep_db_prepare_input($HTTP_POST_VARS["service_condition_position"]);
			if ($template_type=="ICDS") {
				$content.="{}service_condition_large{}" . tep_db_prepare_input($HTTP_POST_VARS["service_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';						
		break;	
		case  "preview_TICS": // preview ticket
			$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;
			}			
			
			$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);
			//service details
			$content=$tInfo->service_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->service_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,date('d-m-Y'));
			$temp_left+=$part_width+$pdf->GetStringWidth(date('d-m-Y'));

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

			// Expiry Date
			$temp_str="Expiry Date: " . date('d-m-Y');
			$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->service_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 osCommRes
			$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->service_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";
			break;
		case "preview_ICDS": // preview Idendity card
			$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->service_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 osCommRes
			$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->service_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->service_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";
		    break;
		  case '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',
									'service_details_content'=>'%%First_Name%% %%Last_Name%%'."\n" .'%%Service_Name%%'."\n".'%%Service_Location%%' ."\n". '%%Start_Time%% %%End_Time%%',
									'service_details_position'=>'R',
									'oscomm_logo_image'=>"oscommres_micro_logo.png",
									'oscomm_logo_position'=>'R',
									'service_condition_content'=>'Card is valid for one use only.'."\n".'No refunds for unused bookings.'."\n".'Make-up any missed bookings by calling ph'."\n".' 12345678, oscommres.com',
									'service_condition_position'=>'L',
									'service_condition_large'=>'Card is valid for one use only.'."\n".'No refunds for unused bookings.'."\n".'Make-up any missed bookings by calling ph'."\n".' 12345678, oscommres.com',
									'bar_image_position'=>'R',
									'customer_details_content'=>'%%First_Name%% %%Last_Name%%'
									);
				if ($template_type=="ICDS"){
					$teDetails["service_condition_content"]="";
				}
				$template_query=tep_db_query("SELECT template_type,template_width,template_height,template_content from " . TABLE_GENERAL_TEMPLATES 
												. " where template_type='" . $template_type . "'");
				if (tep_db_num_rows($template_query)>0){
					$template_result=tep_db_fetch_array($template_query);
					$form_action="update_template";
					$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);
					$form_action="insert_template";
				}
				
				$merge_fields=array(array('id'=>'FN','text'=>TEXT_FN),
									array('id'=>'LN','text'=>TEXT_LN),
									array('id'=>'SEN','text'=>TEXT_SEN),
									array('id'=>'SEL','text'=>TEXT_SEL),
									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 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=="ICDS") { ?>
								<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" 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" 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" 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_SERVICE_CONDITION;?></b></td>
								</tr>
								<tr>
									<td>
									<table border="0" cellspacing="0" cellpadding="2">
										<tr>
											<td class="main" width="100" valign="top"><?php echo TEXT_SERVICE_CONDITION; ?></td>
											<Td  class="main"><?php echo tep_draw_textarea_field('service_condition_content','',40,6,$teInfo->service_condition_content);?></Td>
										</tr>
										<tr>
											<td class="main"><?php echo TEXT_POSITION; ?></td>
											<Td  class="main"><?php echo tep_draw_pull_down_menu('service_condition_position',$position_array,$teInfo->service_condition_position);?></Td>
										</tr>
										<?php if ($template_type=="ICDS") { ?>
										<tr>
											<td class="main" valign="top"><?php echo TEXT_CONDITION_FULL; ?></td>
											<Td  class="main"><?php echo tep_draw_textarea_field('service_condition_large','',50,10,$teInfo->service_condition_large);?></Td>
										</tr>
										<?php } ?>
									</table>
									</td>
								</tr>
								<?php if ($template_type=="ICDS") { 
										$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_SERVICE_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('service_details_content','',40,6,$teInfo->service_details_content); ?></Td>
												<Td class="main" valign="top"><?php echo tep_draw_pull_down_menu('merge_fields',$merge_fields,'','size=5 onDblClick="javascript:AddField(\'service_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('service_details_position',$position_array,$teInfo->service_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 
		  break;  
  	 }			
  	exit;
  } 	
	// array of given templates
	$template_array=array(	array("id"=>"TICS",'text'=>TEXT_TIC),
							array("id"=>"ICDS",'text'=>TEXT_ICD));
?>
<!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=yes,status=no,toolbar=no,left=280,top=180");
		}
		   
		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_SERVICES_TEMPLATES,'template_type=');?>"+template_type;
		  if((action=='preview_ICDS' || ((action=='create_template' && 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_TICS' || ((action=='create_template' && 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_ICDS' || action=='preview_TICS')
				command+="&action="+action;			
		  }
		 if(command!="") do_post_command('insert_template',command);
		 return false; 
		/*frm=document.insert_template;
		var error="";
		  if((action=='preview_ICDS' || ((action=='insert_template' && "<?php echo $template_type;?>"=='ICDS') || (action=='update_template' && "<?php echo $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_TICS' || ((action=='insert_template' && "<?php echo $template_type;?>"=='TICS') || (action=='update_template' && "<?php echo $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;
		  }else {
			frm.action="<?PHP echo FILENAME_SERVICES_TEMPLATES;?>?action="+action;
			if(action=='preview_ICDS' || action=='preview_TICS'){
			frm.target="_blank";
			} else {
			frm.target="_self";
			}
			frm.submit();*/		 
		}
	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_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><?php echo tep_draw_separator('pixel_trans.gif',1,10);?></td>
				</tr>
				<tr>
					<td valign="top">
					<?php echo tep_draw_form('insert_template',FILENAME_SERVICES_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">
					</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'); ?>