<?php

// Establish Operating Variables
$html_template_name = 'hardwood';
$html_template_dir = 'themes';
$feedback_email = 'lbray@blueoxhardwoodflooring.com';

// Establish Final Operating Variables
$html_template = "$html_template_dir/$html_template_name/template.html";
$myname = $_SERVER['PHP_SELF'];

// Import the template stream
$html_template_stream = file_get_contents($html_template);

// Start the templates
$template_article=<<<EOT
<table class="maintable" border="1" cellpadding="0" cellspacing="0" style="border-width:0; border-collapse: collapse" bordercolor="#111111" width="100%">
  <tr>
    <td class="tableh1"width="100%" colspan="2"><p align="center">{TITLE}</p></td>
  </tr>
</table>
{BODY}
<table class="maintable" border="1" cellpadding="0" cellspacing="0" style="border-width:0; border-collapse: collapse" bordercolor="#111111" width="100%">
<tr>

   <td class="tableh1"width="100%" colspan="2"><p>Contact Information</p></td>
 </tr>
 <tr>

<div align="center">
  <center>
 <td class="tableb" width="13%" bordercolor="#A55727">

<h2>Blue Ox Hardwood Flooring</h2>

<p>3982 Short Street Ste:110<br>
San Luis Obispo, Ca 93401<br>
Phone (805) 781-9046 <br></p>

<h2>Electronic mail </h2>
    <p>General Information:
<a href="mailto:lbray@blueoxhardwoodflooring.com">
lbray@blueoxhardwoodflooring.com</a>
  </center>
   </td>
 <td class="tableb" width="1%" align="right">

<a href="index.php?article=1">

<img border="0" src="themes/hardwood/images/banner.jpg" width="327" height="131"></a></td>
 </tr>
</table>

EOT;

$template_map= <<<EOT
<table class="maintable" border="1" cellpadding="0" cellspacing="0" style="border-width:0; border-collapse: collapse" bordercolor="#111111" width="100%">
    <tr>
        <td class="tableb" width="1%" align="center">
        <a target="_blank" href="http://us.rd.yahoo.com/maps//maps/extmap/*-http://maps.yahoo.com//maps_result?csz=San+Luis+Obispo%2C+CA+93401-7572&state=CA&uzip=93401&ds=n&name=&desc=&ed=Eqb96Op_0TonSg0G6_bkm0C.SWBjKtsQQ2nQMUIgLMwcFjNO0RF3QZxXkRXEOZVnhMplUsRhRghVJx9HL0ZSM0MZZcewP9CWGuSww.RQ.Ukbxw--&zoomin=yes&BFKey=&mag=9">
        <img alt="Blue Ox Map" src="http://image.maps.yahoo.com/mapimage?MAPData=KetNXvhyzy1sz69NNcsRhdXEuncRS.7c3td4LlmNd1DsZybQnQ3e2SgpA9TwuQtzjTporeL.n2ieTTx2InsxfKhMq8KdvV5F2_D_nF1hAsE05GBbFP_IcbWDClbXd8jAgXw-">
        </a>
        </td>
    </tr>
</table>

EOT;

$template_thank_you= <<<EOT
<table class="maintable" border="1" cellpadding="0" cellspacing="0" style="border-width:0; border-collapse: collapse" bordercolor="#111111" width="100%">
    <tr>
        <td class="tableb" width="100%" height="150"align="center">
           <p>Thank you for your feedback.</p>
           <p>A Blue Ox Representative will be in contact with you.</p>
        </td>
    </tr>
</table>

EOT;

$template_menu = <<<EOT

                <span class="topmenu">
                        <table border="0" cellpadding="0" cellspacing="0">
                                <tr>
                                    {BUTTONS}
                                </tr>
                        </table>
                </span>

EOT;

$template_button = <<<EOT
 	<td><img name="spacer" src="images/spacer.gif" width="5" height="33" border="0" alt="" /></td>
	<td><img name="button1_r1_c1" src="themes/hardwood/images/button1_r1_c1.gif" width="10" height="33" border="0" alt="" /></td>
	<td align="center" background="themes/hardwood/images/button1_r1_c2.gif"><a href="{HREF_TGT}" title="{HREF_TITLE}">{HREF_LNK}</a></td>
	<td><img name="button1_r1_c3" src="themes/hardwood/images/button1_r1_c3.gif" width="10" height="33" border="0" alt="" /></td>
EOT;

//ranked by order of importance from the users perspective
// {HREF_LNK}{HREF_TITLE}{HREF_TGT}{_blank:y|n}
$main_menu1_buttons[]=array('Home','Home Page','/');
$main_menu1_buttons[]=array('Gallery','Picture Gallery','/cpg');
$main_menu1_buttons[]=array('Floor Guide','WFCA Flooring Guide','http://www.wfca.org/flooringguide/guide.asp',1);
$main_menu1_buttons[]=array('Carpet-Rug Guide','CRI Carpet and Rug Guide','http://www.carpet-rug.com',1);
$main_menu1_buttons[]=array('Care Guide','NOFMA Care Guide','http://www.nofma.org/careguide.htm" target',1);
$main_menu1_buttons[]=array('Feedback','Feedback','index.php?article=1');
$main_menu1_buttons[]=array('Map','Map to Showroom','index.php?article=3');

//ranked alphabetically
$main_menu2_buttons[]=array('Anderson','Anderson Floors','http://www.andersonfloors.com',1);
$main_menu2_buttons[]=array('Bois Chamois','Bois Chamois','http://www.boischamois.com',1);
$main_menu2_buttons[]=array('BR-111','Exotic Hardwood Flooring','http://www.br111.com',1);
$main_menu2_buttons[]=array('Bruce','Bruce Hardwood Flooring by Armstrong','http://www.armstrong.com/resbrucewoodna/index.asp',1);
$main_menu2_buttons[]=array('Columbia','Columbia Flooring','http://www.columbiaflooring.com',1);
$main_menu2_buttons[]=array('Flokati','Hellenic Rugs','http://www.flokati.com',1);
$main_menu2_buttons[]=array('Forbo Parquet','Timeless floors - Timeless designs','http://www.forboparquet.com',1);
$main_menu2_buttons[]=array('Mannington','The look you love. for the life you lead.','http://www.mannington.com',1);
$main_menu2_buttons[]=array('Nafco','NAFCO luxury vinyl flooring','http://www.nafco.com',1);
$main_menu2_buttons[]=array('Oshkosh','Oshkosh Floor Designs','http://www.oshkoshfloors.com',1);
$main_menu2_buttons[]=array('Royalty','Royalty Carpet Mills','http://www.royaltycarpetmills.com',1);
$main_menu2_buttons[]=array('Tarkett','Tarkett Wood Floors by Summit','http://www.harris-tarkett.com',1);
$main_menu2_buttons[]=array('Timbercreek','Timbercreek Hardwood Flooring','http://www.timbercreekhardwoodflooring.com',1);
$main_menu2_buttons[]=array('Virginia','Virginia Hardwood Company','http://www.virginiahardwood.com',1);

//{TAG}{TITLE}{INCLUDE}
add_article('{MAIN}','Blue Ox Hardwood Floors','main.htm');             // #0
add_article('{FEEDBACK}','Blue Ox Feedback','feedback.htm');            // #1
add_article('{THANKS}','Blue Ox Hardwood Floors','main.htm');           // #2 Not Being Used Anymore, need to replace.
add_article('{MAP}','Blue Ox Map','');                                  // #3

// meat and potatoes

if (isset($_REQUEST['article']) ){
    $article=$_REQUEST['article'];
    if (!is_numeric($article) || $article+1 > count($articles) || $article<0) $article=0;
} else {
    $article='0';
}

$param = array(
       '{TITLE}' => $articles[$article]['title'],
       '{META}' => '',
       '{CHARSET}' => 'ISO-8859-1',
       '{GAL_NAME}' => 'Blue Ox Hardwood Flooring',
       '{MAIN_MENU1}' => menu($main_menu1_buttons),
       '{MAIN_MENU2}' => menu($main_menu2_buttons),
       '{THEME_SELECT_LIST}' => '',
       '{LANGUAGE_SELECT_LIST}' => '',
       '{LANGUAGE_SELECT_FLAGS}' => '',
       '{LANG_DIR}' => 'ltr',
       '{ADMIN_MENU}' => '',
       '{GALLERY}' => gallery($article)
      );
echo template_eval($html_template_stream,$param);

// Begin Functions
function gallery($article){
  global $articles,$template_map;
  $output='';
// Pick off special cases, the rest are importable files.
  switch ($articles[$article]['tag']) {
    case '{FEEDBACK}':
         $output=feedback();
    break;
    case '{MAP}':
         $output=$template_map;
    break;
    default:
         $output=import_article($article);
    break;
  }
return make_article($articles[$article]['title'],$output);
}

function add_article($tag,$title,$include){
  global $articles;
  $articles[]=array('tag'=>$tag,'title'=>$title,'include'=>$include);
}

function import_article($article) {
  global $articles;
  $output='';
  $content=file_get_contents($articles[$article]['include']);
  if ($content!==FALSE) {
     $matches[]='';
     $matches2[]='';
     preg_match('/\<.*(body)/i',$content,$matches,PREG_OFFSET_CAPTURE);
     preg_match('/\>/',$content,$matches2,PREG_OFFSET_CAPTURE,$matches[0][1]);
     $start=$matches2[0][1]+1;
     $matches2[]='';
     preg_match('/\<.*(body)/i',$content,$matches2,PREG_OFFSET_CAPTURE,$start);
     $stop=$matches2[0][1];
     $output.="<!-- START Article -->\n";
     $output.=substr($content,$start,$stop-$start-1);
     $output.="<!-- STOP Article -->\n";
   }
   else {
    $output.="Article: $article not available.";
  }
return $output;
}

function buttons(&$buttons,&$template) {
// {HREF_LNK}{HREF_TITLE}{HREF_TGT}{_blank:y|n}
$output='';
$counter=0;

   foreach ($buttons as $button) {
     $counter+=1;
     if (count($button)>3) {
       if ($button[3]==1) $button[2].='" target="_blank';
     }
     $param=array(
          '{HREF_LNK}' => $button[0],
          '{HREF_TITLE}' => $button[1],
          '{HREF_TGT}' => $button[2],
     );
     $output.=template_eval($template,$param);
     if ($counter==7) {
       $output.='</tr><tr>';
       $counter=0;
     }
   }

return $output;
}

function menu(&$buttons) {
  global $template_button,$template_menu;
	$param = array('{BUTTONS}' => buttons($buttons,$template_button)
		);
	return template_eval($template_menu,$param);
}

// Eval a template (substitute vars with values)
function template_eval(&$template, &$vars)
{
        return strtr($template, $vars);
}

function feedback() {
    global $feedback_email,$template_article,$template_thank_you;
    if (isset($_POST['email'])) {
       $email = $_POST['email'];
       $mailto = $feedback_email;
       $mailsubj = "Form submission";
       $mailhead = "From: $email\n";
       reset ($_POST);
       $mailbody = "Values submitted from web site form:\n";
       while (list ($key, $val) = each ($_POST)) {
             $mailbody .= "$key : $val\n";
       }
    mail($mailto, $mailsubj, $mailbody, $mailhead);
    $output=$template_thank_you;
    } else {
      $output=import_article(1);
    }
    return $output;
}

function make_article($title,$body) {
  global $template_article;
    $param = array('{BODY}' => $body,
     '{TITLE}'=> $title
     );
  return template_eval($template_article,$param);

}

?>
