<?php
/*
	osCommerce, Open Source E-Commerce Solutions 
	http://www.oscommerce.com 
	
	Copyright (c) 2003 osCommerce 
	
	osCommRes, Services Oline 
	http://www.oscommres.com 
	Copyright (c) 2005 osCommRes 
	
	Freeway eCommerce 
	http://www.zac-ware.com/freeway 
	Copyright (c) 2007 ZacWare 
	
	Released under the GNU General Public License
*/

// Set flag that this is a parent file
	define( '_FEXEC', 1 );
	$format=array('d-m-Y'=>'dd-mm-yyyy',
				  'm-d-Y'=>'mm-dd-yyyy',
				  'Y-m-d'=>'yyyy-mm-dd');

	require('includes/application_top.php');

	if (!tep_session_is_registered('customer_id')) {
		$navigation->set_snapshot();
		tep_redirect(tep_href_link(FILENAME_LOGIN, '', 'SSL'));
	}

	$serverDate = date('Y-m-d H:i:s',getServerDate(false));
	// needs to be included earlier to set the success message in the messageStack
	require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_ACCOUNT_EDIT);

  if (isset($HTTP_POST_VARS['action']) && ($HTTP_POST_VARS['action'] == 'process')) {
  	$username_edit=tep_db_prepare_input(isset($HTTP_POST_VARS["username_edit"])?'Y':'N');
	
    if (ACCOUNT_GENDER == 'true') $gender = tep_db_prepare_input($HTTP_POST_VARS['gender']);
    $firstname = tep_db_prepare_input($HTTP_POST_VARS['firstname']);
    $lastname = tep_db_prepare_input($HTTP_POST_VARS['lastname']);
    if (ACCOUNT_DOB == 'true') $dob = tep_db_prepare_input($HTTP_POST_VARS['dob']);
    $email_address = tep_db_prepare_input($HTTP_POST_VARS['email_address']);
    $confirm_email_address = tep_db_prepare_input($HTTP_POST_VARS['confirm_email_address']);
    if (ACCOUNT_SECOND_EMAIL == 'true') $second_email_address = tep_db_prepare_input($HTTP_POST_VARS['second_email_address']);
    if($second_email_address!='')$second_confirm_email_address = tep_db_prepare_input($HTTP_POST_VARS['second_confirm_email_address']);
    if (ACCOUNT_TELEPHONE == 'true') $telephone = tep_db_prepare_input($HTTP_POST_VARS['telephone']);
    if (ACCOUNT_SECOND_PHONE == 'true') $second_telephone = tep_db_prepare_input($HTTP_POST_VARS['second_telephone']);
	 if ($username_edit=='Y') $username = tep_db_prepare_input($HTTP_POST_VARS['username']);
	 $country=tep_db_prepare_input($HTTP_POST_VARS['country']);
	 //if (ACCOUNT_STATE == 'true') $state = tep_db_prepare_input($HTTP_POST_VARS['state']);
	   $entry_state = tep_db_prepare_input($HTTP_POST_VARS['state']);
       if($entry_state=='' && $HTTP_POST_VARS['zone_id']) $entry_state=tep_db_prepare_input($HTTP_POST_VARS['zone_id']);	  
	    if ($HTTP_POST_VARS['state']=="" && isset($HTTP_POST_VARS['zone_id'])) $entry_zone_id = tep_db_prepare_input($HTTP_POST_VARS['zone_id']);
		 if (ACCOUNT_STATE == 'true') {
          if ($entry_country_error == true) {
            $entry_state_error = true; 
         } else {                        
            $zone_id = 0;
            $entry_state_error = false;
            $check_query = tep_db_query("select count(*) as total from " . TABLE_ZONES . " where zone_country_id = '" . (int)$country . "'");
            $check_value = tep_db_fetch_array($check_query);
            $entry_state_has_zones = ($check_value['total'] > 0);
            if ($entry_state_has_zones == true) {
              $zone_query = tep_db_query("select distinct zone_id from " . TABLE_ZONES . " where zone_country_id = '" . (int)$country . "' and zone_id='" . tep_db_input($entry_state) . "'");
              if (tep_db_num_rows($zone_query) == 1) {
                $zone_values = tep_db_fetch_array($zone_query);
                $entry_zone_id = $zone_values['zone_id'];
              } else {
                $error = true;
                $entry_state_error = true;
              }
            } else {
              if ($entry_state == false) {
                $error = true;
                $entry_state_error = true;
              }
            }
         }
      }
	 

	 $mobile = tep_db_prepare_input($HTTP_POST_VARS['mobile']);
    $error = false;
    if (ACCOUNT_GENDER == 'true') {
      if ( ($gender != 'm') && ($gender != 'f') ) {
        $error = true;

        $messageStack->add('account_edit', ENTRY_GENDER_ERROR);
      }
    }

    if (strlen($firstname) < ENTRY_FIRST_NAME_MIN_LENGTH) {
      $error = true;

      $messageStack->add('account_edit', ENTRY_FIRST_NAME_ERROR);
    }

    if (strlen($lastname) < ENTRY_LAST_NAME_MIN_LENGTH) {
      $error = true;

      $messageStack->add('account_edit', ENTRY_LAST_NAME_ERROR);
    }

    if (ACCOUNT_DOB == 'true') {
      if (!tep_check_date_raw($dob,EVENTS_DATE_FORMAT)) {
        $error = true;

        $messageStack->add('account_edit', ENTRY_EVENT_DATE_OF_BIRTH_ERROR);
      }
    }

	if ($username_edit=='Y') {
		if (strlen($username) < ENTRY_USERNAME_MIN_LENGTH) {
		  $error = true;
	
		  $messageStack->add('account_edit', ENTRY_USERNAME_ERROR);
		} else {
		  $check_name_query = tep_db_query("select count(*) as total from " . TABLE_CUSTOMERS . " where LOWER(customers_username) = '" . strtolower(tep_db_input($username)) . "'");
		  $check_name = tep_db_fetch_array($check_name_query);
		  if ($check_name['total'] > 0) {
			$error = true;
	
			$messageStack->add('account_edit', ENTRY_USERNAME_ERROR_EXISTS);
		  }
		}
	}

    if (strlen($email_address) < ENTRY_EMAIL_ADDRESS_MIN_LENGTH) {
      $error = true;

      $messageStack->add('account_edit', ENTRY_EMAIL_ADDRESS_ERROR);
    }
    
	 if($email_address!=$confirm_email_address){
	 	$error = true;
	   $messageStack->add('account_edit', ENTRY_CONFIRM_EMAIL_ADDRESS_ERROR);
	 }
	 
    if (!tep_validate_email($email_address)) {
      $error = true;

      $messageStack->add('account_edit', ENTRY_EMAIL_ADDRESS_CHECK_ERROR);
    }

	if($second_email_address!=''){
	 if (!tep_validate_email($second_email_address)) {
      $error = true;
      $messageStack->add('account_edit', ENTRY_SECOND_EMAIL_ADDRESS_CHECK_ERROR);
    }else  if($second_email_address!=$second_confirm_email_address){
	 		$error = true;
		   $messageStack->add('account_edit', ENTRY_SECOND_CONFIRM_EMAIL_ADDRESS_ERROR);
	 }else if($email_address==$second_email_address){
	 		$error = true;
		   $messageStack->add('account_edit', JS_SECOND_EMAIL_ADDRESS_UNIQUE);
	 }  
   }
	 
   $check_email_query = tep_db_query("select count(*) as total from " . TABLE_CUSTOMERS . " where customers_email_address = '" . tep_db_input($email_address) . "' and customers_id != '" . (int)$customer_id . "'");
   $check_email = tep_db_fetch_array($check_email_query);
   if ($check_email['total'] > 0) {
     $error = true;

      $messageStack->add('account_edit', ENTRY_EMAIL_ADDRESS_ERROR_EXISTS);
   }

	if (ACCOUNT_TELEPHONE == 'true') {
		if (strlen($telephone) < ENTRY_TELEPHONE_MIN_LENGTH) {
		  $error = true;
	
		  $messageStack->add('account_edit', ENTRY_TELEPHONE_NUMBER_ERROR);
		}
	}
    if (strpos($mobile,"+")!==false) {
      $error = true;

      $messageStack->add('create_account', ENTRY_MOBILE_NUMBER_ERROR);
    }

    if ($error == false) {
      $sql_data_array = array('customers_firstname' => $firstname,
                              'customers_lastname' => $lastname,
                              'customers_email_address' => $email_address,
                              'customers_fax' => $mobile);

      if (ACCOUNT_SECOND_EMAIL == 'true') $sql_data_array['customers_second_email_address'] = $second_email_address;
      if (ACCOUNT_SECOND_PHONE == 'true') $sql_data_array['customers_second_telephone'] = $second_telephone;
      if (ACCOUNT_GENDER == 'true') $sql_data_array['customers_gender'] = $gender;
      if (ACCOUNT_DOB == 'true') $sql_data_array['customers_dob'] = tep_convert_date_raw($dob,EVENTS_DATE_FORMAT);
	  if (ACCOUNT_TELEPHONE == 'true') $sql_data_array['customers_telephone']=$telephone;
	  if ($username_edit=='Y') $sql_data_array['customers_username']=$username;

      tep_db_perform(TABLE_CUSTOMERS, $sql_data_array, 'update', "customers_id = '" . (int)$customer_id . "'");

      tep_db_query("update " . TABLE_CUSTOMERS_INFO . " set customers_info_date_account_last_modified ='". $serverDate ."' where customers_info_id = '" . (int)$customer_id . "'");
	  if ($entry_zone_id > 0) $entry_state = '';
      $sql_data_array = array('entry_firstname' => $firstname,
                              'entry_lastname' => $lastname,
							  'entry_country_id' =>$country
							  );
							  
		 if (ACCOUNT_STATE == 'true') {
          if ($entry_zone_id > 0) {
            $sql_data_array['entry_zone_id'] = $entry_zone_id;
            $sql_data_array['entry_state'] = '';
          } else {
            $sql_data_array['entry_zone_id'] = '0';
            $sql_data_array['entry_state'] = $entry_state;
          }
        }
		//print_r($sql_data_array);

      tep_db_perform(TABLE_ADDRESS_BOOK, $sql_data_array, 'update', "customers_id = '" . (int)$customer_id . "' and address_book_id = '" . (int)$customer_default_address_id . "'");

// reset the session variables
      $customer_first_name = $firstname;

      $messageStack->add_session('account', SUCCESS_ACCOUNT_UPDATED, 'success');
	  //JOOMLA5 START
		if(isset($joomla_include) && $joomla_include){
			 $db = &JFactory::getDBO();
			 $user_obj=&JFactory::getUser();
			 if($user_obj->id>0){
	    		jimport('joomla.user.helper');				
	    		$db->Execute("update #__users set email='".tep_db_input($email_address)."' where id=$user_obj->id");
			 }
		}
		//JOOMLA5 END	
      tep_redirect(tep_href_link(FILENAME_ACCOUNT, '', 'SSL'));
    }
  }  
  $account_query=tep_db_query("select c.customers_id, c.customers_gender, LTRIM(c.customers_firstname) as customers_firstname, LTRIM(c.customers_lastname) as customers_lastname, c.customers_dob, c.customers_email_address, c.customers_second_email_address,c.customers_password,a.entry_company, a.entry_street_address, a.entry_suburb, a.entry_postcode, a.entry_city, a.entry_state, c.customers_type,c.is_blocked,
	                                 a.entry_zone_id, a.entry_country_id, c.customers_telephone, c.customers_second_telephone,c.customers_fax, c.customers_newsletter,c.customers_subscription_newsletter,c.customers_reserve_newsletter,c.customers_default_address_id,c.customers_groups_id,c.customers_discount,c.customers_username,c.customers_occupation,c.suspend_from,c.resume_from,c.customers_interest from " . TABLE_CUSTOMERS . " c left join " . TABLE_ADDRESS_BOOK . 
					 " a on c.customers_default_address_id = a.address_book_id where a.customers_id = c.customers_id and c.customers_id = '" . (int)$customer_id . "' order by customers_lastname,customers_firstname");
  $account = tep_db_fetch_array($account_query);

  $breadcrumb->add(NAVBAR_TITLE_1, tep_href_link(FILENAME_ACCOUNT, '', 'SSL'));
  $breadcrumb->add(NAVBAR_TITLE_2, tep_href_link(FILENAME_ACCOUNT_EDIT, '', 'SSL'));

  $content = CONTENT_ACCOUNT_EDIT;
  $javascript = 'form_check.js.php';

  require(DIR_WS_TEMPLATES . TEMPLATE_NAME . '/' . TEMPLATENAME_MAIN_PAGE);

  require(DIR_WS_INCLUDES . 'application_bottom.php');
?>
