<?php require("../replacers.inc"); 
 ?><?php //@language="VBScript"
?><?php //option explicit
?><?php require( "../custom.inc" ) ?><?php 
   //for debug: don't neglect this variable now:
   //  when converting to PHP, this is good to convert
   //  debug code also to use it if problems in PHP-coded arise.
   //dim dg   'for debug
   $ve="\r\n";
   $inter_val=5;//minutes; 
   //"overwritten" if "update interval " in present;
   define("html_header","<html><head><title>Connecting to Corporate Office</title></head><body><pre>");
   define("html_end","</pre></body></html>");
   //we apologize for inconvenience caused:
   $office_closed_message="Sorry, corporate office site is closed at this time." . $ve . "Information about open hours is not available." . $ve . "<b>This message may be cleared if to refresh it from the browser.</b>";
   //aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
   //  Auxiliary procedures
   //=======================================================


function rh($s){
   
   //response html
   echo(html_header . $s . html_end);
   exit();
   return $rh;
}


function ex($s){
   
   //exception
   rh($s);
   return $ex;
}
   //extracting <value> by parsing format: 
   //  <skey> <value> <sterminator>


function parse_key_value($ssource,$skey,$sterminator){
   global $ve;
   $i=instr_c_($ssource,$skey);
   if(0 == $i){ex("Missesd keyphrase " . $skey);
   }
   $i=$i + strlen($skey);
   $k=instr_c_($i,$ssource,$sterminator);
   if(0 == $k){ex("Missed value terminator " . $sterminator . $ve . "for keyphrase" . $skey);
   }
   $parse_key_value=substr($ssource,$i-1,$k - $i);
   return $parse_key_value;
}


function parse_key_number($ssource,$skey,$sterminator){
   global $ve;
   $ws=parse_key_value($ssource,$skey,$sterminator);
   on_error_resume_next_c_();
   $parse_key_number=(int)($ws);
   if($err_number_c_ > 0){
      ex("Value of keyphrase " . $skey . $ve . "is not a number. Value=" . $ws);
   }
   return $parse_key_number;
}
   //aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
   $redirect_subfolder=trim(request_query_string_c_("subfolder"));
   $redirect_file=read_file("link_to_corporate_office.htm");
   $ws=trim(parse_key_value($redirect_file,"update interval ",$ve));
   if("" != $ws){
      on_error_resume_next_c_();
      $wi=(int)($ws);
      if(!$err_number_c_ > 0){
         $inter_val=$wi;
      }
   }
   $redirect_url=parse_key_value($redirect_file,"a href=\"","\"");
   $update_month=parse_key_number($redirect_file,$ve . "update month ",$ve);
   $update_minutes=parse_key_number($redirect_file,$ve . "update minutes ",$ve);
   $wminute=(date("d") * 24 + date("H")) * 60 + date("i");
   //dg = dg & ve & "wminutue=" & wminute
   //dg = dg & ve & "stamped minute=" & update_minutes
   $link_is_fresh=false;
   if(date("m") != $update_month){
      //be optimistic: 
      if($wminute <= $inter_val){$link_is_fresh=true;
      }
   }else { 
      if($wminute <= $update_minutes + $inter_val){$link_is_fresh=true;
      }
   }
   if("" != $redirect_subfolder){
      $redirect_url=$redirect_url . $redirect_subfolder . "/";
   }
   if($link_is_fresh){
      //debug:
      //rh "redirecting to " & redirect_url & dg
header("Location: " . 
      $redirect_url);
   }else { 
      //debug:
      //rh office_closed_message & dg
      rh($office_closed_message);
   }
    ?>