<?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
	defined('_FEXEC') or die();

	require(DIR_WS_CLASSES . 'payment.php');
	include(DIR_WS_CLASSES . 'order.php');
	include(DIR_WS_CLASSES . 'ot_coupon.php');
	
	$action=isset($HTTP_GET_VARS['action'])?tep_db_prepare_input($HTTP_GET_VARS['action']):'';
	$disable_shipping=isset($HTTP_GET_VARS['disable_shipping'])?tep_db_prepare_input($HTTP_GET_VARS['disable_shipping']):'';

  $SeparateBillingFields = tep_field_exists(TABLE_ORDERS, "billing_name");
  $AddShippingTax = "0.0"; // e.g. shipping tax of 17.5% is "17.5"

//print_r($HTTP_SESSION_VARS);
//echo $HTTP_SESSION_VARS['modify_price_prefix'];
  if (tep_not_null($action)) {
    switch ($action) {
	case 'update_address':
		// Update Order Info
		$update_delivery_name = tep_db_input($HTTP_POST_VARS['update_delivery_name']);	
		$update_delivery_company = tep_db_input($HTTP_POST_VARS['update_delivery_company']);
		$update_delivery_street_address=tep_db_input($HTTP_POST_VARS['update_delivery_street_address']);	
		$update_delivery_suburb=tep_db_input($HTTP_POST_VARS['update_delivery_suburb']);
		$update_delivery_city=tep_db_input($HTTP_POST_VARS['update_delivery_city']);
		$update_delivery_state=tep_db_input($HTTP_POST_VARS['update_delivery_state']);
		$update_delivery_postcode=tep_db_input($HTTP_POST_VARS['update_delivery_postcode']);
		$update_delivery_country=tep_db_input($HTTP_POST_VARS['update_delivery_country']);
		
		
		$update_customer_telephone=tep_db_input($HTTP_POST_VARS['update_customer_telephone']);
		$update_customer_second_telephone_number=tep_db_input($HTTP_POST_VARS['update_customer_second_telephone_number']);
		$update_customer_fax=tep_db_input($HTTP_POST_VARS['update_customer_fax']);	
		$update_customer_second_email_address=tep_db_input($HTTP_POST_VARS['update_customer_second_email_address']);
		$update_customer_email_address=tep_db_input($HTTP_POST_VARS['update_customer_email_address']);
		
		$update_billing_name=tep_db_input($HTTP_POST_VARS['update_billing_name']);
		$update_billing_company=tep_db_input($HTTP_POST_VARS['update_billing_company']);
		$update_billing_street_address=tep_db_input($HTTP_POST_VARS['update_billing_street_address']);
		$update_billing_suburb=tep_db_input($HTTP_POST_VARS['update_billing_suburb']);	
		$update_billing_city=tep_db_input($HTTP_POST_VARS['update_billing_city']);
		$update_billing_state=tep_db_input($HTTP_POST_VARS['update_billing_state']);
		$update_billing_postcode=tep_db_input($HTTP_POST_VARS['update_billing_postcode']);
		$update_billing_country=tep_db_input($HTTP_POST_VARS['update_billing_country']);
		
			 
		if(is_numeric($HTTP_POST_VARS['update_customer_country'])) $update_customer_country=tep_get_country_name(tep_db_prepare_input($HTTP_POST_VARS['update_customer_country'])); 
		if(is_numeric($HTTP_POST_VARS['update_billing_country'])) $update_billing_country=tep_get_country_name(tep_db_prepare_input($HTTP_POST_VARS['update_billing_country']));
		if(is_numeric($HTTP_POST_VARS['update_delivery_country'])) $update_delivery_country=tep_get_country_name(tep_db_prepare_input($HTTP_POST_VARS['update_delivery_country']));
		if($HTTP_POST_VARS['update_billing_state1']!='')  $update_billing_state=tep_db_prepare_input($HTTP_POST_VARS['update_billing_state1']); 
		if($HTTP_POST_VARS['update_delivery_state1']!='')  $update_delivery_state=tep_db_prepare_input($HTTP_POST_VARS['update_delivery_state1']);
		if($HTTP_POST_VARS['update_customer_state1']!='')  $update_customer_state=tep_db_prepare_input($HTTP_POST_VARS['update_customer_state1']);
		// update billing and shipping details
		  $UpdateOrders = "update " . TABLE_CUSTOMERS_BASKET_ADDRESS . " set ";
		  if($SeparateBillingFields)
	   	   {
		      $UpdateOrders .= "billing_name = '" . tep_db_input(stripslashes($update_billing_name)) . "',
			billing_company = '" . tep_db_input(stripslashes($update_billing_company)) . "',
			billing_street_address = '" . tep_db_input(stripslashes($update_billing_street_address)) . "',
			billing_suburb = '" . tep_db_input(stripslashes($update_billing_suburb)) . "',
			billing_city = '" . tep_db_input(stripslashes($update_billing_city)) . "',
			billing_state = '" . tep_db_input(stripslashes($update_billing_state)) . "',
			billing_postcode = '" . tep_db_input(stripslashes($update_billing_postcode)) . "',
			billing_country = '" . tep_db_input(stripslashes($update_billing_country)) . "',";
		   }
		     $UpdateOrders .= " delivery_name = '" . tep_db_input(stripslashes($update_delivery_name)) . "',
			delivery_company = '" . tep_db_input(stripslashes($update_delivery_company)) . "',
			delivery_street_address = '" . tep_db_input(stripslashes($update_delivery_street_address)) . "',
			delivery_suburb = '" . tep_db_input(stripslashes($update_delivery_suburb)) . "',
			delivery_city = '" . tep_db_input(stripslashes($update_delivery_city)) . "',
			delivery_state = '" . tep_db_input(stripslashes($update_delivery_state)) . "',
			delivery_postcode = '" . tep_db_input(stripslashes($update_delivery_postcode)) . "',
			delivery_country = '" . tep_db_input(stripslashes($update_delivery_country)) . "',
			customers_telephone = '" . tep_db_input(stripslashes($update_customer_telephone)) . "',
		    customers_second_telephone = '" . tep_db_input(stripslashes($update_customer_second_telephone_number)) . "',
			customers_fax ='" . tep_db_input(stripslashes($update_customer_fax)) . "',
		    customers_second_email_address = '" . tep_db_input(stripslashes($update_customer_second_email_address)) . "',
			customers_email_address = '" . tep_db_input(stripslashes($update_customer_email_address)) . "'";
        if ($UpdateOrders!=''){
 		   $UpdateOrders .= " where customers_id = '" . tep_db_prepare_input($customer_id) . "';";		   
		   tep_db_query($UpdateOrders);
		}
		echo "step3||";
		exit;
	break;
    }
  }
 $billto=$customer_default_address_id;
$order_detail = new order;
?>
  <?php echo tep_draw_form('payment_address', FILENAME_EDIT_ORDERS, 'oID=' . $oID  . '&action=update_order&step=' . $step,'post','onSubmit="return validateForm()"'); ?>  
  <table border="0" width="100%" cellspacing="0" cellpadding="2">
		<tr>
        <td valign="middle"><table border="0" width="100%" cellspacing="0" cellpadding="5" align="left">
          <tr>
			<td class="infoBoxHeading" valign="middle"><font color="#003366"><b><?php echo TEXT_NAME;?></b></font>
			<?php echo tep_customers_name($customer_id); ?>&nbsp;<span  id="img_process3"></span></td>
          </tr>
        </table></td>
      </tr>
	  
	<tr><td valign=top><table width="100%" border="0" cellspacing="0" cellpadding="2">
       <?php if($SeparateBillingFields) { ?>
   		<?php tep_content_title_top(ENTRY_CUSTOMER_ADDRESS); ?>
	  <tr>
	    <td valign="top">
	    <!-- Billing Address Block -->
		<table border="0" cellspacing="1" cellpadding="2"  width=60%>
		<tr>
		    <td class="main" width="30%"><?php echo TEXT_NAME; ?></td>
		    <td class="main" nowrap="nowrap"><?php echo tep_draw_input_field('update_billing_name',tep_html_quotes($order_detail->billing['name']),'size=25',true); ?></td>
			<td class="main" nowrap="nowrap">&nbsp;&nbsp;&nbsp;<?php echo TEXT_ADDRESS; ?></td>
		    <td class="main" nowrap="nowrap">&nbsp;&nbsp;&nbsp;<?php echo tep_draw_input_field('update_billing_street_address',tep_html_quotes($order_detail->billing['street_address']),'size=25',true); ?></td>
		</tr>
		<tr>
		<?php
 			 if (ACCOUNT_COMPANY == 'true') {
		?>
		    <td class="main"><?php echo TEXT_COMPANY; ?></td>
		    <td class="main"><?php echo tep_draw_input_field('update_billing_company',tep_html_quotes($order_detail->billing['company']),'size=25'); ?></td>
		<?php } else { ?>	
			<td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif','1','10');?></td>
		<?php } ?>
		<?php 
		  if (ACCOUNT_SUBURB == 'true') {
		?>
			<td class="main" nowrap="nowrap">&nbsp;&nbsp;&nbsp;<?php echo TEXT_SUBURB; ?></td>
		    <td class="main">&nbsp;&nbsp;&nbsp;<?php echo tep_draw_input_field('update_billing_suburb',tep_html_quotes($order_detail->billing['suburb']),'size=25'); ?></td>
		<?php } else { ?>
			<td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif','1','10');?></td>
		<?php }?>		
		</tr>
		<tr>
      		  <td class="main" width="30%" nowrap><?php echo ENTRY_TELEPHONE_NUMBER; ?></td>
     		  <td class="main" nowrap="nowrap"><?php echo tep_draw_input_field('update_customer_telephone',$order_detail->customer['telephone'],'size=25',true); ?></td>
        	  <td class="main" nowrap="nowrap">&nbsp;&nbsp;&nbsp;<?php echo TEXT_CITY; ?></td>
		      <td class="main" nowrap="nowrap">&nbsp;&nbsp;&nbsp;<?php echo tep_draw_input_field('update_billing_city',tep_html_quotes($order_detail->billing['city']),'size=25',true); ?></td>
		</tr>
		<tr><?php if (ACCOUNT_SECOND_PHONE=='true') { ?>
		    <td class="main" nowrap><?php echo TEXT_SECOND_TELEPHONE; ?></td>
		    <td class="main" nowrap="nowrap"><?php echo tep_draw_input_field('update_customer_second_telephone_number',tep_html_quotes($order_detail->customer['second_telephone']),'size=25'); ?></td>
		 	<td class="main" nowrap="nowrap">&nbsp;&nbsp;&nbsp;<?php echo TEXT_POSTCODE; ?></td>
		    <td class="main" nowrap>&nbsp;&nbsp;&nbsp;<?php echo tep_draw_input_field('update_billing_postcode',tep_html_quotes($order_detail->billing['postcode']),'size=25',true); ?></td>
		  <?php } ?>
		 </tr>
		 <tr>
		    <td class="main"><?php echo TEXT_MOBILE; ?></td>
		    <td class="main"><?php echo tep_draw_input_field('update_customer_fax',tep_html_quotes($order_detail->customer['fax']),'size=25'); ?></td>
			
		    <td class="main" nowrap="nowrap">&nbsp;&nbsp;&nbsp;<?php echo TEXT_COUNTRY; ?></td>
		    <td class="main" nowrap="nowrap">&nbsp;&nbsp;&nbsp;<?php
		    $country_id=tep_html_quotes($order_detail->billing['country_id']);
		    echo sbs_get_country_list('update_billing_country', $country_id,'id="update_billing_country"  onchange="javascript:show_state_billing(this);"');
		    // echo tep_draw_input_field('update_billing_country',tep_html_quotes($order_detail->billing['country']),'size=37 id="update_billing_country" onchange="javascript:show_state();"',true); ?></td>
		</tr>
		<tr>
		  <td class="main"><?php echo ENTRY_EMAIL_ADDRESS; ?></td>
		  <td class="main" nowrap><?php echo tep_draw_input_field('update_customer_email_address',$order_detail->customer['email_address'],'size=25',true); ?></td>
		  <?php
  					if (ACCOUNT_STATE == 'true') {
		?>
			<td class="main" nowrap="nowrap">&nbsp;&nbsp;<?php echo TEXT_STATE; ?></td>
		    <td class="main" nowrap="nowrap">&nbsp;&nbsp;&nbsp;<?php 
		    $state=tep_html_quotes($order_detail->billing['state']);
		    //$zone_id=tep_get_zone_id($country_id,$state);
		    $zones_array = array(); 
          	$zones_query = tep_db_query("select zone_name from " . TABLE_ZONES . " where zone_country_id = '" . tep_db_input($country_id) . "' order by zone_name");
          	while ($zones_values = tep_db_fetch_array($zones_query)) {
	           $zones_array[] = array('id' => $zones_values['zone_name'], 'text' => $zones_values['zone_name']);
          	}
          	$sty_txt="";
          	$sty_combo='style="display:none"';
          	if(count($zones_array)>0){
          		$sty_combo='';
          		$sty_txt='style="display:none"';
          	}
		    echo tep_draw_input_field('update_billing_state1',$state,'size=25 '.$sty_txt);
    		echo tep_draw_pull_down_menu('update_billing_state', $zones_array,$state,$sty_combo);
		    //echo tep_draw_input_field('update_billing_state',tep_html_quotes($order_detail->billing['state']),'size=37',true); ?></td>
		<?php } else {?>
			<td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif','1','10');?></td>
		<?php }?>	
		</tr>
		<tr>  
	  <?php if (ACCOUNT_SECOND_EMAIL=='true') { ?>
			 <td class="main" nowrap><?php echo TEXT_SECOND_EMAIL; ?></td>
			 <td class="main" nowrap="nowrap"><?php echo tep_draw_input_field('update_customer_second_email_address',tep_html_quotes($order_detail->customer['second_email_address']),'size=25'); ?></td>
	   <?php } ?>
	   </tr>
	  </table>
	 </td>
	</tr>
	<?php tep_content_title_bottom(); ?>
<?php  } ?>
		
   		
	 <?php tep_content_title_top(ENTRY_SHIPPING_ADDRESS); ?>
	  <tr>
	    <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif','1','3'); ?></td>
	  </tr>
	  <tr>
		<td valign="top">
	   <!-- Shipping Address Block -->
		<table border="0" cellspacing="1" cellpadding="2"  width=60%>
		<tr>
		    <td class="main" width="30%"><?php echo TEXT_NAME; ?></td>
		    <td class="main" nowrap="nowrap">
				<?php echo tep_draw_separator('pixel_trans.gif', 53, 10);
				 echo tep_draw_input_field('update_delivery_name',tep_html_quotes($order_detail->delivery['name']),'size=25',true); ?>
			</td>
			<td class="main" nowrap="nowrap">&nbsp;&nbsp;&nbsp;&nbsp;<?php echo TEXT_CITY; ?></td>
		    <td class="main" nowrap="nowrap">&nbsp;&nbsp;
			<?php echo tep_draw_input_field('update_delivery_city',tep_html_quotes($order_detail->delivery['city']),'size=25',true); ?></td>
       </tr>
	<?php
	  if (ACCOUNT_COMPANY == 'true') {
	?>		<tr>
		    <td class="main" nowrap="nowrap"><?php echo TEXT_COMPANY; ?></td>
		    <td class="main" nowrap="nowrap">
				<?php  echo tep_draw_separator('pixel_trans.gif', 53, 10);
				echo tep_draw_input_field('update_delivery_company',tep_html_quotes($order_detail->delivery['company']),'size=25'); ?></td>
			<td class="main" nowrap="nowrap">&nbsp;&nbsp;&nbsp;&nbsp;<?php echo TEXT_POSTCODE; ?></td>
		    <td class="main" nowrap="nowrap">&nbsp;&nbsp;&nbsp;<?php echo tep_draw_input_field('update_delivery_postcode',tep_html_quotes($order_detail->delivery['postcode']),'size=25',true); ?></td>
		</tr>
<?php } ?>
		<tr>
		    <td class="main" nowrap="nowrap"><?php echo TEXT_ADDRESS; ?></td>
		    <td class="main" nowrap="nowrap">
				<?php echo tep_draw_separator('pixel_trans.gif', 53, 10);
				 echo tep_draw_input_field('update_delivery_street_address',tep_html_quotes($order_detail->delivery['street_address']),'size=25',true); ?></td>
			<td class="main" nowrap="nowrap">&nbsp;&nbsp;&nbsp;&nbsp;<?php echo TEXT_COUNTRY; ?></td>
		    <td class="main" nowrap="nowrap">&nbsp;&nbsp;&nbsp;<?php 
		    $country_id=tep_html_quotes($order_detail->delivery['country_id']);
		    echo sbs_get_country_list('update_delivery_country', $country_id,'id="update_delivery_country" onchange="javascript:show_state_billing(this);"');
		    //echo tep_draw_input_field('update_delivery_country',tep_html_quotes($order_detail->delivery['country']),'size=37',true); ?></td>
		</tr>
 <tr>
<?php
  if (ACCOUNT_SUBURB == 'true') {
?>		
		    <td class="main" nowrap="nowrap"><?php echo TEXT_SUBURB; ?></td>
		    <td class="main" nowrap="nowrap">
			   <?php echo tep_draw_separator('pixel_trans.gif', 53, 10);
			    echo tep_draw_input_field('update_delivery_suburb',tep_html_quotes($order_detail->delivery['suburb']),'size=25'); ?>
			</td>
	<?php } else { ?>
			<td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif','1','10');?></td>
		<?php }?>		
   <?php
     if (ACCOUNT_STATE == 'true') {
   ?>		<td class="main" nowrap="nowrap">&nbsp;&nbsp;&nbsp;<?php echo TEXT_STATE; ?></td>
		    <td class="main" nowrap="nowrap">&nbsp;&nbsp;&nbsp;<?php 
		    $state=tep_html_quotes($order_detail->delivery['state']);
		    //$zone_id=tep_get_zone_id($country_id,$state);
		    $zones_array = array(); 
          	$zones_query = tep_db_query("select zone_name from " . TABLE_ZONES . " where zone_country_id = '" . tep_db_input($country_id) . "' order by zone_name");
          	while ($zones_values = tep_db_fetch_array($zones_query)) {
	           $zones_array[] = array('id' => $zones_values['zone_name'], 'text' => $zones_values['zone_name']);
          	}
 			$sty_txt="";
          	$sty_combo='style="display:none"';
          	if(count($zones_array)>0){
          		$sty_combo='';
          		$sty_txt='style="display:none"';
          	}
		    echo tep_draw_input_field('update_delivery_state1',$state,'size=37 '.$sty_txt);
    		echo tep_draw_pull_down_menu('update_delivery_state',$zones_array,$state,$sty_combo);
		    ?></td>
	<?php } ?>	
	  </tr>
	  <tr height="10"></tr>
    <?php tep_content_title_bottom(); ?>
	<tr>
		<td class="contentTitle" height="1px"><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td>
	</tr>
	<tr>
        <td><table border="0" width="100%" cellspacing="1" cellpadding="2" >
          <tr class="main">
            <td><table border="0" width="100%" cellspacing="0" cellpadding="2">
              <tr>
                <td align="left" class="main" nowrap><?php
				echo '<a href="javascript:show_step_number(2);">' . tep_image_button('button_back.gif', IMAGE_BACK) . '</a>&nbsp;&nbsp;'; ?> </td>
				<td align="right" class="main" nowrap><?php
					echo '<a href="javascript:do_page_fetch(\'submit_payment_address\');">' . tep_image_button('button_continue.gif', IMAGE_CONTINUE,'name="button_confirm3" id="button_confirm3"') . '</a>'; ?></td>
              </tr>
            </table></td>
          </tr>
        </table></td>
      </tr>
	</table></td>
      </tr></table></td></tr>
<!-- body_text_eof //-->
</table>
</form>
<?php  ////////////////////////////////////////////////////////////////////////////////////////////////
  function tep_field_exists($table,$field) {

    $describe_query = tep_db_query("describe $table");
    while($d_row = tep_db_fetch_array($describe_query))
    {
      if ($d_row["Field"] == "$field")
      return true;
    }
    return false;
  }
  function sbs_get_country_list($name, $selected = '', $parameters = '') { 
   $countries_array = array(array('id' => '', 'text' => PULL_DOWN_DEFAULT)); 
   $countries = sbs_get_countries(); 
   $size = sizeof($countries); 
   for ($i=0; $i<$size; $i++) { 
     $countries_array[] = array('id' => $countries[$i]['countries_id'], 'text' => $countries[$i]['countries_name']); 
   } 

   return tep_draw_pull_down_menu($name, $countries_array, $selected, $parameters); 
}
  function sbs_get_countries($countries_id = '', $with_iso_codes = false) {
    $countries_array = array();
    if ($countries_id) {
      if ($with_iso_codes) {
        $countries = tep_db_query("select countries_name, countries_iso_code_2, countries_iso_code_3 from " . TABLE_COUNTRIES . " where countries_id = '" . $countries_id . "' order by countries_name");
        $countries_values = tep_db_fetch_array($countries);
        $countries_array = array('countries_name' => $countries_values['countries_name'],
                                 'countries_iso_code_2' => $countries_values['countries_iso_code_2'],
                                 'countries_iso_code_3' => $countries_values['countries_iso_code_3']);
      } else {
        $countries = tep_db_query("select countries_name from " . TABLE_COUNTRIES . " where countries_id = '" . $countries_id . "'");
        $countries_values = tep_db_fetch_array($countries);
        $countries_array = array('countries_name' => $countries_values['countries_name']);
      }
    } else {
      $countries = tep_db_query("select countries_id, countries_name from " . TABLE_COUNTRIES . " order by countries_name");
      while ($countries_values = tep_db_fetch_array($countries)) {
        $countries_array[] = array('countries_id' => $countries_values['countries_id'],
                                   'countries_name' => $countries_values['countries_name']);
      }
    }

    return $countries_array;
  } 

?>