<?php


// Check to ensure this file is included in Freeway!
defined('_FEXEC') or die();
	$FOOTER_SHOPPING=true;
	$pview=isset($HTTP_GET_VARS["pview"])?(int)$HTTP_GET_VARS["pview"]:'2';
	if (!($pview>=1 && $pview<=3)){
		$pview=isset($HTTP_SESSION_VARS["service_view"])?(int)$HTTP_SESSION_VARS["service_view"]:'3';
	}
	$default_view=true;
	if ($pview>=1 && $pview<=3){
		if (!tep_session_is_registered("service_view")) tep_session_register("service_view");
		tep_set_session_value("service_view",$pview);
		$template_content=tep_load_template_content('service_listing.tpl.php');
		if ($template_content!=""){
			$default_view=false;
		}
	}
	if ($default_view){
?>

<table border="0" width="100%" cellspacing="0" cellpadding="<?php echo CELLPADDING_SUB;?>">
<?php
// BOF: Lango Added for template MOD
if (SHOW_HEADING_TITLE_ORIGINAL == 'yes') {
$header_text = '&nbsp;'
//EOF: Lango Added for template MOD
?>
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
                        <td class="pageHeading">
             <?php 
			
    // Get the category name and description
    if($current_services_category_id>0){
	$sers_category_query = tep_db_query("select secd.services_categories_name, secd.services_categories_heading_title, secd.services_categories_description, sec.services_categories_image from " . TABLE_SERVICES_CATEGORIES . " sec, " . TABLE_SERVICES_CATEGORIES_DESCRIPTION . " secd where sec.services_categories_id = '" . $current_services_category_id . "' and secd.services_categories_id = '" . $current_services_category_id . "' and secd.language_id = '" . $languages_id . "'");
	$sers_category = tep_db_fetch_array($sers_category_query);

               if ( (ALLOW_CATEGORY_DESCRIPTIONS == 'true') && (tep_not_null($sers_category['services_categories_heading_title'])) ) {
                 echo $sers_category['services_categories_heading_title'];
				 
               } else {
                 echo HEADING_TITLE;
				 
               }
			  } 
             ?>
            </td>
<?php
// Get the right image for the top-right
    $image = DIR_WS_IMAGES . 'table_background_list.gif';
    if (isset($HTTP_GET_VARS['manufacturers_id'])) {
	  
      $image = tep_db_query("select manufacturers_image from " . TABLE_MANUFACTURERS . " where manufacturers_id = '" . (int)$HTTP_GET_VARS['manufacturers_id'] . "'");
      $image = tep_db_fetch_array($image);
      $image = $image['manufacturers_image'];
    } elseif ($current_services_category_id) {
	  
      $image = tep_db_query("select services_categories_image from " . TABLE_SERVICES_CATEGORIES . " where services_categories_id = '" . (int)$current_services_category_id . "'");
      $image = tep_db_fetch_array($image);
      $image = $image['services_categories_image'];
    }
?>
            <td align="right"><?php echo tep_image(DIR_WS_IMAGES . $image, HEADING_TITLE, HEADING_IMAGE_WIDTH, HEADING_IMAGE_HEIGHT); ?></td>
          </tr>
	  <?php if ( (ALLOW_CATEGORY_DESCRIPTIONS == 'true') && (tep_not_null($sers_category['services_categories_description'])) ) { ?>
	  <tr>
            <td align="left" colspan="2" class="category_desc"><?php echo $sers_category['services_categories_description']; ?></td>
	  </tr>
	  <?php } ?>
        </table></td>
      </tr>
<?php
}else{
$header_text = HEADING_TITLE;
}
if(!isset($hide_row_top)) {
?>
     <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
      </tr>
<?php
}
// BOF: Lango Added for template MOD
if (MAIN_TABLE_BORDER == 'yes'){
table_image_border_top(false, false, $header_text);
}
// EOF: Lango Added for template MOD
?>
      <tr>
        <td>
		<?php include(DIR_WS_MODULES . FILENAME_SERVICES_LISTING_COL); ?>
		</td>
      </tr>
<?php
// BOF: Lango Added for template MOD
if (MAIN_TABLE_BORDER == 'yes'){
  table_image_border_bottom();
}
// EOF: Lango Added for template MOD
?>
    </table>
<?php 
	}else{
			// set some default values
		switch($pview){
			case 1:
				$timg_width=70;
				$timg_height=70;
				$trow_height=100;
				$tdesc_length=300;
				$tmax_rows=MAX_DISPLAY_SEARCH_RESULTS;
				$ttitle_class="title";
				$tprice_class="priceText";
				$ttotal_cols=2;
				$trow_gap=10;
				break;
			case 2:
				$timg_width=SMALL_IMAGE_WIDTH;
				$timg_height=SMALL_IMAGE_HEIGHT;
				$trow_height=60;
				$tdesc_length=300;
				$tmax_rows=(int)round(MAX_DISPLAY_SEARCH_RESULTS/2);
				$ttitle_class="title2";
				$tprice_class="priceText2";
				$ttotal_cols="1";
				$trow_gap=10;
				break;
			default:
				$timg_width=70;
				$timg_height=70;
				$trow_height=50;
				$tdesc_length=60;
				$ttitle_class="title3";
				$tprice_class="priceText3";
				$ttotal_cols="1";
				$trow_gap=20;
				$tmax_rows=MAX_DISPLAY_SEARCH_RESULTS;
		}				
		if($ttotal_cols!='') $tcols_width=sprintf(" width=%s%%",(100/$ttotal_cols));
		
		
		$template_details['VALUE_HEADING_ICON']=tep_template_image('arrow_1.gif','','align="absmiddle"') . '&nbsp;';
		$template_details['VALUE_HEADING_TITLE']=HEADING_TITLE;
		$template_details['VALUE_ARROW']=tep_template_image("arrow_1.gif",'','','align="absmiddle"');
		$template_details['VALUE_VIEWS']=TEXT_PRODUCT_VIEWS;
		$template_details["VALUE_LIST_ROW_HEIGHT"]=$trow_height;
		$template_details["VALUE_LIST_IMAGE_WIDTH"]=$timg_width;
		$template_details["VALUE_TITLE_CLASS"]=$ttitle_class;
		$template_details["VALUE_PRICE_CLASS"]=$tprice_class;
		$template_details["VALUE_NO_SERVICES"]="No Services";
		$template_details["VALUE_ROW_GAP"]=$trow_gap;
		for($icnt=1;$icnt<=3;$icnt++){
			$temp_image='ico_view' .$icnt;
			if ($pview==$icnt){
				$template_details["VALUE_VIEW" . $icnt]=tep_template_image($temp_image . "_select.gif");
			} else {
				$template_details["VALUE_VIEW" . $icnt]='<a href="' . tep_href_link(FILENAME_DEFAULT,tep_get_all_get_params(array('pview','page')) . 'pview=' . $icnt) . '">' . tep_template_image($temp_image . ".gif") . '</a>';
			}
		}
		// Get the category name and description
		$category_sql = "select scd.services_categories_name, scd.services_categories_heading_title, scd.services_categories_description, sc.services_categories_image from " . TABLE_SERVICES_CATEGORIES . " sc, " . TABLE_SERVICES_CATEGORIES_DESCRIPTION . " scd where sc.services_categories_id = '" . $current_services_category_id . "' and scd.services_categories_id = '" . $current_services_category_id . "' and scd.language_id = '" . $languages_id . "'";
		$category_query = tep_db_query($category_sql);
		$category = tep_db_fetch_array($category_query);
		$template_details["VALUE_CATEGORY_TITLE"]=$category["services_categories_heading_title"];
		$template_details["VALUE_CATEGORY_INFO"]='(' . $current_services_category_id . ')';		
		$listing_split = new splitPageResults($listing_sql, $tmax_rows, 'se.service_id');
		if ($listing_split->number_of_rows > 0){
			$template_details["VALUE_PAGE_NAVIGATION_TEXT"]=$listing_split->display_count(TEXT_DISPLAY_NUMBER_OF_SERVICES);
			$template_details["SECTION_PAGE_NAVIGATION"]=1;
			$template_details["VALUE_PAGE_NAVIGATION_LINKS"]=TEXT_RESULT_PAGE . ' ' . $listing_split->display_links(MAX_DISPLAY_PAGE_LINKS, tep_get_all_get_params(array('page', 'info', 'x', 'y')));
			$service_repeat=array();
			$icnt=0;
			$listing_query=tep_db_query($listing_split->sql_query);
			while ($listing = tep_db_fetch_array($listing_query)) {
				$service_repeat[$icnt]=array('NAME'=>'','PRICE'=>'','QUANTITY'=>'','IMAGE'=>'','BUY_NOW'=>'','DESCRIPTION'=>'');

				$service_repeat[$icnt]['NAME']='<a href="' . tep_href_link(FILENAME_SERVICES_INFO, ($secPath ? 'secPath=' . $secPath . '&' : '') . 'service_id=' . $listing['service_id']) . '">' . $listing['service_name'] . '</a>';
				if($listing['include_resource']=='N'){
					$service_repeat[$icnt]['PRICE']=$currencies->display_price($listing['service_costs'], tep_get_tax_rate($listing['service_tax_class_id']));
				}else{
					$service_repeat[$icnt]['PRICE'] = '';
				}
				if(file_exists(DIR_WS_IMAGES . $listing['service_image']) && $listing['service_image']!=''){
					$service_repeat[$icnt]['IMAGE'] = '<a href="' . tep_href_link(FILENAME_SERVICES_INFO, ($secPath ? 'secPath=' . $secPath . '&' : '') . 'service_id=' . $listing['service_id']) . '">' . tep_image(DIR_WS_IMAGES . $listing['service_image'], $listing['service_name'], $timg_width, $timg_height) . '</a>';
				}else{
					$service_repeat[$icnt]['IMAGE'] = tep_image(DIR_WS_IMAGES . 'pixel_trans.gif', $listing['service_name'], $timg_width, $timg_height);
				}
				$service_description=strip_tags($listing['service_description'],'<br>');
				$service_repeat[$icnt]['DESCRIPTION'] = substr($service_description,0,$tdesc_length);
	
				$icnt++;
			}
			$template_details["REPEAT_PRODUCT_LIST"]=$service_repeat;
			unset($product_repeat);
			mysql_free_result($listing_query);
			$template_details["SECTION_PRODUCT"]=1;
			$template_details["SECTION_NO_PRODUCT"]=0;
		} else {
			$template_details["VALUE_PRODUCTS_TOTAL"]='';
			$template_details["VALUE_PAGE_NAVIGATION_TEXT"]='';
			$template_details["SECTION_PAGE_NAVIGATION"]=0;
			$template_details["SECTION_PRODUCT"]=0;
			$template_details["SECTION_NO_PRODUCT"]=1;
		}
		$template_details["VALUE_SUB_PADDING"]=0;
		$replaced_content=$template_content;
		if(tep_session_is_registered("customer_id")){
			$template_sql = "select customers_selected_template from " . TABLE_CUSTOMERS . " where customers_id='" . $HTTP_SESSION_VARS['customer_id'] . "'";
			$template_query = tep_db_query($template_sql);
			$template_result = tep_db_fetch_array($template_query);
			$template_name = $template_result['customers_selected_template'];
		}

		while(list($key,$value)=each($template_details)){
			switch(substr($key,0,strpos($key,'_'))){
				case "VALUE":
					$replaced_content=str_replace("{{" . $key . "}}",$value,$replaced_content);
					break;
				case "SECTION":
					if ($value!=1){
						//$replaced_content=preg_replace("/\{\{" . $key ."_START\}\}((.|\n)*)\{\{" . $key . "_END\}\}/","",$replaced_content);
						$start_pos=strpos($replaced_content,"{{" . $key . "_START}}");
						$end_pos=strpos($replaced_content,"{{" . $key . "_END}}");
						$temp_content=substr($replaced_content,0,$start_pos);
						$temp_content.=substr($replaced_content,$end_pos+strlen("{{" . $key ."_END}}"));
						$replaced_content=$temp_content;
						unset($temp_content);
					} else {
						$replaced_content=str_replace(array("{{" .$key . "_START}}","{{" .$key . "_END}}"),"",$replaced_content);
					}
					break;
				case "REPEAT":
					$start_pos=strpos($replaced_content,"{{" . $key . "_START}}")+strlen("{{" . $key ."_START}}");
					$end_pos=strpos($replaced_content,"{{" . $key . "_END}}");
					$repeat_content=substr($replaced_content,$start_pos,$end_pos-$start_pos);
					$merged_total_content='<table cellpadding="0" cellspacing="0" width="100%" border="0">' . "\n";
					$col=0;
					$row=0;
					for ($icnt=0,$n=count($value);$icnt<$n;$icnt++){
						$merged_content='';
						if ($col==0 || $ttotal_cols==1) $merged_content.='<tr>';
						if(strtolower($template_name)=="carshop"){
							$merged_content.='<td class="main_bg" valign="top"' . ($row==0?$tcols_width:''). '>' . $repeat_content;
						}else{
							$merged_content.='<td valign="top"' . ($row==0?$tcols_width:''). '>' . $repeat_content;
						}

						reset($value[$icnt]);
						while(list($itemkey,)=each($value[$icnt])){
							$merged_content=str_replace("{{" . $key . "_" . $itemkey . "}}",$value[$icnt][$itemkey],$merged_content);
						}
						$col++;
						if ($col==$ttotal_cols) {
							$col=0;
							$row++;
						}
						if ($col==0 || $ttotal_cols==1) $merged_content.='</tr><tr><td class="products_bottom" colspan="2"></td></tr><tr height="10"></tr>';
						$merged_total_content.="\n" . $merged_content;
					}
					$merged_total_content.="</table>";
					//$replaced_content=preg_replace("/\{\{" . $key ."_START\}\}((.|\n)*)\{\{" . $key . "_END\}\}/",$merged_total_content,$replaced_content);
					$replaced_content=substr($replaced_content,0,$start_pos) . $merged_total_content . substr($replaced_content,$end_pos);
					unset($merged_total_content);
					$replaced_content=str_replace(array("{{" .$key . "_START}}","{{" .$key . "_END}}"),"",$replaced_content);
					break;
			}
		}
		echo $replaced_content;
		unset($replaced_content);
	}
 ?>
