<?php
/*
  www.oscGold.com (C) Kamelia-Net Jacek Krysiak
  modul oscGold wer 4.0
*/

require('includes/application_top.php');

if (isset($HTTP_GET_VARS['guest']) && $cart->count_contents() < 1) tep_redirect(tep_href_link(FILENAME_SHOPPING_CART));
if (ACCOUNT_VALIDATION == 'true' && ACCOUNT_CREATE_VALIDATION == 'true') {
    require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_ACCOUNT_VALIDATION);
    include_once('includes/functions/' . FILENAME_ACCOUNT_VALIDATION);
}

require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_CREATE_ACCOUNT);

$new_password = tep_create_random_value(ENTRY_PASSWORD_MIN_LENGTH);
$crypted_password = tep_encrypt_password($new_password);

$process = false;
$refresh = false;
if (isset($HTTP_POST_VARS['action']) && (($HTTP_POST_VARS['action'] == 'process') || ($HTTP_POST_VARS['action'] == 'refresh'))) {
  if ($HTTP_POST_VARS['action'] == 'process')  $process = true;
    if ($HTTP_POST_VARS['action'] == 'refresh') $refresh = true;

    // -------------------- POCZATEK
    // SPRAWDZA CZY WYBRANO FIRME
    $osoba = tep_db_prepare_input($HTTP_POST_VARS['osoba']);
    if ($osoba == 'firma') {
      // definicja zmiennych pobranych z formularza
      $company = tep_db_prepare_input($HTTP_POST_VARS['company']);								//nazwa firmy
      $nip = tep_db_prepare_input($HTTP_POST_VARS['nip']);										//NIP
      $email_address = tep_db_prepare_input($HTTP_POST_VARS['email_address']);					//mail
      $email_address_confirm = tep_db_prepare_input($HTTP_POST_VARS['email_address_confirm']);	//mail powtorzony

      $street_address = tep_db_prepare_input($HTTP_POST_VARS['street_address']);				//ulica
	  $street_nr_domu = tep_db_prepare_input($HTTP_POST_VARS['street_address_dom']);				//nr domu
      if ( tep_db_prepare_input($HTTP_POST_VARS['street_address_dom'] != '' ) ) {
        $street_address .= ' ' . tep_db_prepare_input($HTTP_POST_VARS['street_address_dom']);				//dom
      }
      if ( tep_db_prepare_input($HTTP_POST_VARS['street_address_mieszkanie'] != '' ) ) {
        $street_address .= '/' . tep_db_prepare_input($HTTP_POST_VARS['street_address_mieszkanie']);				//dom
      }

      $postcode = tep_db_prepare_input($HTTP_POST_VARS['postcode']);							//kod pocztowy
      $city = tep_db_prepare_input($HTTP_POST_VARS['city']);									//miasto
      if (ACCOUNT_SUBURB == 'true') $suburb = tep_db_prepare_input($HTTP_POST_VARS['suburb']);  //dzielnica
      if (ACCOUNT_STATE == 'true') {															//wojewodztwo - jezeli jest wlaczone w ADMIN
        $state = tep_db_prepare_input($HTTP_POST_VARS['state']);
        if (isset($HTTP_POST_VARS['zone_id'])) {
          $zone_id = tep_db_prepare_input($HTTP_POST_VARS['zone_id']);
        } else {
          $zone_id = false;
        }
      }
      $country = tep_db_prepare_input($HTTP_POST_VARS['country']);								//panstwo
      $firstname = tep_db_prepare_input($HTTP_POST_VARS['firstname']);							//imie
      $lastname = tep_db_prepare_input($HTTP_POST_VARS['lastname']);							//nazwisko
      if (NICK_VALUE == 'false') {
          $nick = '---';
         } else {
          if (isset($_GET['guest'])) {
              $nick = '---';																	//nick
            } else {
              $nick = tep_db_prepare_input($HTTP_POST_VARS['nick']);							//nick
          }
      }
      $telephone = tep_db_prepare_input($HTTP_POST_VARS['telephone']);							//telefon
      $fax = tep_db_prepare_input($HTTP_POST_VARS['fax']);										//fax
    }
    // ------------------------ KONIEC

    // -------------------- POCZATEK
    // SPRAWDZA CZY WYBRANO OSOBE FIZYCZNA
    $osoba = tep_db_prepare_input($HTTP_POST_VARS['osoba']);
    if ($osoba == 'fizyczna') {
      // definicja zmiennych pobranych z formularza
      if (ACCOUNT_GENDER == 'true') {															//plec
        if (isset($HTTP_POST_VARS['gender'])) {
          $gender = tep_db_prepare_input($HTTP_POST_VARS['gender']);
        } else {
          $gender = false;
        }
      }
      $firstname = tep_db_prepare_input($HTTP_POST_VARS['firstname']);							//imie
      $lastname = tep_db_prepare_input($HTTP_POST_VARS['lastname']);							//nazwisko
      if (NICK_VALUE == 'false') {
          $nick = '---';
         } else {
          if (isset($_GET['guest'])) {
              $nick = '---';																	//nick
            } else {
              $nick = tep_db_prepare_input($HTTP_POST_VARS['nick']);							//nick
          }
      }
      $nip = tep_db_prepare_input($HTTP_POST_VARS['nip']);			                            //NIP
      if (ACCOUNT_DOB == 'true') $dob = tep_db_prepare_input($HTTP_POST_VARS['dob']);			//data urodzenia
      $street_address = tep_db_prepare_input($HTTP_POST_VARS['street_address']);				//ulica
	  $street_nr_domu = tep_db_prepare_input($HTTP_POST_VARS['street_address_dom']);				//nr domu	  
      if ( tep_db_prepare_input($HTTP_POST_VARS['street_address_dom'] != '' ) ) {
        $street_address .= ' ' . tep_db_prepare_input($HTTP_POST_VARS['street_address_dom']);				//dom
      }
      if ( tep_db_prepare_input($HTTP_POST_VARS['street_address_mieszkanie'] != '' ) ) {
        $street_address .= '/' . tep_db_prepare_input($HTTP_POST_VARS['street_address_mieszkanie']);				//dom
      }
      if (ACCOUNT_SUBURB == 'true') $suburb = tep_db_prepare_input($HTTP_POST_VARS['suburb']);  //dzielnica
      $postcode = tep_db_prepare_input($HTTP_POST_VARS['postcode']);							//kod pocztowy
      $city = tep_db_prepare_input($HTTP_POST_VARS['city']);									//miasto
      if (ACCOUNT_STATE == 'true') {															//wojewodztwo - jezeli jest wlaczone w ADMIN
        $state = tep_db_prepare_input($HTTP_POST_VARS['state']);
        if (isset($HTTP_POST_VARS['zone_id'])) {
          $zone_id = tep_db_prepare_input($HTTP_POST_VARS['zone_id']);
        } else {
          $zone_id = false;
        }
      }
      $country = tep_db_prepare_input($HTTP_POST_VARS['country']);								//panstwo
      $email_address = tep_db_prepare_input($HTTP_POST_VARS['email_address']);					//mail
      $email_address_confirm = tep_db_prepare_input($HTTP_POST_VARS['email_address_confirm']);	//mail powtorzony
      $telephone = tep_db_prepare_input($HTTP_POST_VARS['telephone']);							//telefon
      $fax = tep_db_prepare_input($HTTP_POST_VARS['fax']);										//fax
    }
    // ------------------------ KONIEC

      $polecajacy = tep_db_prepare_input($HTTP_POST_VARS['polecajacy']);
      if (isset($HTTP_POST_VARS['newsletter'])) {
        $newsletter = tep_db_prepare_input($HTTP_POST_VARS['newsletter']);
      } else {
        $newsletter = false;
      }
      $password = $new_password;
      $confirmation = $new_password;
      $agreement = tep_db_prepare_input($HTTP_POST_VARS['agreement']);
      if (ACCOUNT_VALIDATION == 'true' && ACCOUNT_CREATE_VALIDATION == 'true') {
        $antirobotreg = tep_db_prepare_input($HTTP_POST_VARS['antirobotreg']);
      }
      $haslo1 = tep_db_prepare_input($HTTP_POST_VARS['haslo1']);
      $haslo2 = tep_db_prepare_input($HTTP_POST_VARS['haslo2']);

      // FUNKCJA WARUNKOWA JEZELI ZOSTAL WYSLANY FORMULARZ (SPRAWDZA BLEDY)
      if ($process) {
        $error = false;
        $ciag_bledu = '';

    	// ustawienia dla haslo **************
    	if (CZY_HASLO_GENERUJE_SKLEP == '0' && !isset($_GET['guest'])) {
    		//sprawdza czy haslo jest poprawne oraz czy sa zgodne
    		if (strlen($haslo1) < (int)ENTRY_PASSWORD_MIN_LENGTH) {
    			$error = true;
    			$ciag_bledu .= '* ' .  ENTRY_HASLO_ERROR.'<br>';
    		} elseif ($haslo1 != $haslo2) {
    			$error = true;
    			$ciag_bledu .= '* ' .  ENTRY_HASLO_NOT_MATCHING.'<br>';
    		} elseif (!sprawdz_haslo($haslo1)) {
    			$error = true;
    			$ciag_bledu .= '* ' .  ENTRY_ZLE_HASLO.'<br>';
    		}
    		$new_password = $haslo1;
            $password = $new_password;
            $crypted_password = tep_encrypt_password($new_password);
    	}

		// SPRAWDZA CZY WYBRANO FIRME -----------------------------------------------------------------------------------------------------------------------------------
	    $osoba = tep_db_prepare_input($HTTP_POST_VARS['osoba']);
	    if ($osoba == 'firma') {

			//sprawdza czy wypelnione pole firma
			if (strlen($company) < ENTRY_COMPANY_MIN_LENGTH) {
				$error = true;
				$ciag_bledu .= '* ' .  ENTRY_COMPANY_ERROR.'<br>';
			}
			//sprawdza czy wypelnione pole NIP
			if (strlen($nip) < ENTRY_NIP_MIN_LENGTH) {
				$error = true;
				$ciag_bledu .= '* ' .  ENTRY_NIP_ERROR.'<br>';
			}
			//sprawdza adres email oraz zgodnosc powtorzonego adresu
		    if (strlen($email_address) < ENTRY_EMAIL_ADDRESS_MIN_LENGTH) {
		    	$error = true;
			    $ciag_bledu .= '* ' .  ENTRY_EMAIL_ADDRESS_ERROR.'<br>';
		    } elseif (tep_validate_email($email_address) == false) {
			    $error = true;
			    $ciag_bledu .= '* ' .  ENTRY_EMAIL_ADDRESS_CHECK_ERROR.'<br>';
		    } elseif ($email_address != $email_address_confirm) {
			    $error = true;
			    $ciag_bledu .= '* ' .  ENTRY_EMAIL_ADDRESS_CONFIRM_NOT_MATCHING.'<br>';
		    } else {
          		$check_email_query = tep_db_query("select count(*) as total from " . TABLE_CUSTOMERS . " where customers_email_address = '" . tep_db_input($email_address) . "' and guest_account != '1'");
			    $check_email = tep_db_fetch_array($check_email_query);
			  if ($check_email['total'] > 0) {
			    $error = true;
			    $ciag_bledu .= '* ' .  ENTRY_EMAIL_ADDRESS_ERROR_EXISTS.'<br>';
			  }
		    }
			//sprawdza czy wypelnione pole ulica
	        if (strlen($street_address) < ENTRY_STREET_ADDRESS_MIN_LENGTH) {
	           $error = true;
	           $ciag_bledu .= '* ' .  ENTRY_STREET_ADDRESS_ERROR.'<br>';
	        }
			
			//sprawdza czy wypelnione nr domu
			if (empty($street_nr_domu)) {
				$error = true;
				$ciag_bledu .= '* ' .  ENTRY_STREET_ADDRESS_DOM_ERROR.'<br>';
			}
			
			//sprawdza czy wypelnione pole kod pocztowy
	        if (strlen($postcode) < ENTRY_POSTCODE_MIN_LENGTH) {
	           $error = true;
	           $ciag_bledu .= '* ' .  ENTRY_POST_CODE_ERROR.'<br>';
	        }
			//sprawdza czy wypelnione pole miasto
	        if (strlen($city) < ENTRY_CITY_MIN_LENGTH) {
	           $error = true;
	           $ciag_bledu .= '* ' .  ENTRY_CITY_ERROR.'<br>';
	        }
            //sprawdza czy wypelnione pole wojewodztwo
	        if (ACCOUNT_STATE == 'true') {
	          // +Country-State Selector
	          if ($zone_id == 0) {
	          // -Country-State Selector
	            if (strlen($state) < ENTRY_STATE_MIN_LENGTH) {
	              $error = true;
	              $ciag_bledu .= '* ' .  ENTRY_STATE_ERROR.'<br>';
	            }
	          }
	        }
			//sprawdza czy wybrane panstwo
	        if (is_numeric($country) == false) {
	          $error = true;
	          $ciag_bledu .= '* ' .  ENTRY_COUNTRY_ERROR.'<br>';
	        }
			//sprawdza czy wypelnione pole imie
            if (strlen($firstname) < ENTRY_FIRST_NAME_MIN_LENGTH) {
               $error = true;
               $ciag_bledu .= '* ' .  ENTRY_FIRST_NAME_ERROR.'<br>';
            }
			//sprawdza czy wypelnione pole nazwisko
            if (strlen($lastname) < ENTRY_LAST_NAME_MIN_LENGTH) {
               $error = true;
               $ciag_bledu .= '* ' .  ENTRY_LAST_NAME_ERROR.'<br>';
            }
			//sprawdza czy wypelnione pole telefon
			if (strlen($telephone) < ENTRY_TELEPHONE_MIN_LENGTH) {
			   $error = true;
			   $ciag_bledu .= '* ' .  ENTRY_TELEPHONE_NUMBER_ERROR.'<br>';
			}

			//sprawdza czy wypelnione pole nick
            if (NICK_VALUE == 'true') {
                if (!isset($_GET['guest'])) {
                    if (strlen($nick) < ENTRY_NICK_MIN_LENGTH) {
                       $error = true;
                       $ciag_bledu .= '* ' .  ENTRY_NICK_ERROR.'<br>';
                    }
                    if ((int)ENTRY_NICK_MIN_LENGTH != 0) {
                        $nick_email_query = tep_db_query("select count(*) as total from " . TABLE_CUSTOMERS . " where nick = '" . tep_db_input($nick) . "' and nick != ''");
                        $nick_email = tep_db_fetch_array($nick_email_query);
                        if ($nick_email['total'] > 0) {
                            $error = true;
                            $ciag_bledu .= '* ' .  ENTRY_NICK_CHECK_ERROR.'<br>';
                        }
                    }
                }
            }

		}

		// SPRAWDZA CZY WYBRANO OSOBE FIZYCZNA --------------------------------------------------------------------------------------------------------------------------
	    $osoba = tep_db_prepare_input($HTTP_POST_VARS['osoba']);
	    if ($osoba == 'fizyczna') {

			//sprawdza czy zaznaczone plec
	        if (ACCOUNT_GENDER == 'true' && ENTRY_GENDER_TEXT != '') {
	          if ( ($gender != 'm') && ($gender != 'f') ) {
	            $error = true;
	            $ciag_bledu .= '* ' .  ENTRY_GENDER_ERROR.'<br>';
	          }
	        }
			//sprawdza czy wypelnione pole imie
            if (strlen($firstname) < ENTRY_FIRST_NAME_MIN_LENGTH) {
               $error = true;
               $ciag_bledu .= '* ' .  ENTRY_FIRST_NAME_ERROR.'<br>';
            }
			//sprawdza czy wypelnione pole nazwisko
            if (strlen($lastname) < ENTRY_LAST_NAME_MIN_LENGTH) {
               $error = true;
               $ciag_bledu .= '* ' .  ENTRY_LAST_NAME_ERROR.'<br>';
            }

			//sprawdza czy wypelnione pole nick
            if (NICK_VALUE == 'true') {
                if (!isset($_GET['guest'])) {
                    if (strlen($nick) < ENTRY_NICK_MIN_LENGTH) {
                       $error = true;
                       $ciag_bledu .= '* ' .  ENTRY_NICK_ERROR.'<br>';
                    }
                    $nick_email_query = tep_db_query("select count(*) as total from " . TABLE_CUSTOMERS . " where nick = '" . tep_db_input($nick) . "' and nick != ''");
                    $nick_email = tep_db_fetch_array($nick_email_query);
                    if ($nick_email['total'] > 0) {
                        $error = true;
                        $ciag_bledu .= '* ' .  ENTRY_NICK_CHECK_ERROR.'<br>';
                    }
                }
            }

			//sprawdza czy wypelnione pole data urodzenia
	        if (ACCOUNT_DOB == 'true' && ENTRY_DATE_OF_BIRTH_TEXT != '') {
	          if (checkdate(substr(tep_date_raw($dob), 4, 2), substr(tep_date_raw($dob), 6, 2), substr(tep_date_raw($dob), 0, 4)) == false) {
	            $error = true;
	            $ciag_bledu .= '* ' .  ENTRY_DATE_OF_BIRTH_ERROR.'<br>';
	          }
	        }
			//sprawdza czy wypelnione pole ulica
	        if (strlen($street_address) < ENTRY_STREET_ADDRESS_MIN_LENGTH) {
	           $error = true;
	           $ciag_bledu .= '* ' .  ENTRY_STREET_ADDRESS_ERROR.'<br>';
	        }
			
			//sprawdza czy wypelnione nr domu
			if (empty($street_nr_domu)) {
				$error = true;
				$ciag_bledu .= '* ' .  ENTRY_STREET_ADDRESS_DOM_ERROR.'<br>';
			}
			
			//sprawdza czy wypelnione pole kod pocztowy
	        if (strlen($postcode) < ENTRY_POSTCODE_MIN_LENGTH) {
	           $error = true;
	           $ciag_bledu .= '* ' .  ENTRY_POST_CODE_ERROR.'<br>';
	        }
			//sprawdza czy wypelnione pole miasto
	        if (strlen($city) < ENTRY_CITY_MIN_LENGTH) {
	           $error = true;
	           $ciag_bledu .= '* ' .  ENTRY_CITY_ERROR.'<br>';
	        }
            //sprawdza czy wypelnione pole wojewodztwo
	        if (ACCOUNT_STATE == 'true') {
	          // +Country-State Selector
	          if ($zone_id == 0) {
	          // -Country-State Selector
	            if (strlen($state) < ENTRY_STATE_MIN_LENGTH) {
	              $error = true;
	              $ciag_bledu .= '* ' .  ENTRY_STATE_ERROR.'<br>';
	            }
	          }
	        }
			//sprawdza czy wybrane panstwo
	        if (is_numeric($country) == false) {
	          $error = true;
	          $ciag_bledu .= '* ' .  ENTRY_COUNTRY_ERROR.'<br>';
	        }
			//sprawdza adres email oraz zgodnosc powtorzonego adresu
		    if (strlen($email_address) < ENTRY_EMAIL_ADDRESS_MIN_LENGTH) {
		    	$error = true;
			    $ciag_bledu .= '* ' .  ENTRY_EMAIL_ADDRESS_ERROR.'<br>';
		    } elseif (tep_validate_email($email_address) == false) {
			    $error = true;
			    $ciag_bledu .= '* ' .  ENTRY_EMAIL_ADDRESS_CHECK_ERROR.'<br>';
		    } elseif ($email_address != $email_address_confirm) {
			    $error = true;
			    $ciag_bledu .= '* ' .  ENTRY_EMAIL_ADDRESS_CONFIRM_NOT_MATCHING.'<br>';
		    } else {
 		        $check_email_query = tep_db_query("select count(*) as total from " . TABLE_CUSTOMERS . " where customers_email_address = '" . tep_db_input($email_address) . "' and guest_account != '1'");
			    $check_email = tep_db_fetch_array($check_email_query);
			  if ($check_email['total'] > 0) {
			    $error = true;
			    $ciag_bledu .= '* ' .  ENTRY_EMAIL_ADDRESS_ERROR_EXISTS.'<br>';
			  }
		    }
			//sprawdza czy wypelnione pole telefon
			if (strlen($telephone) < ENTRY_TELEPHONE_MIN_LENGTH) {
			   $error = true;
			   $ciag_bledu .= '* ' .  ENTRY_TELEPHONE_NUMBER_ERROR.'<br>';
			}
		}
      // BOF Customers extra fields
      $extra_fields_query = tep_db_query("select ce.fields_id, ce.fields_input_type, ce.fields_required_status, cei.fields_name, ce.fields_status, ce.fields_input_type, ce.fields_size from " . TABLE_EXTRA_FIELDS . " ce, " . TABLE_EXTRA_FIELDS_INFO . " cei where ce.fields_status=1 and ce.fields_required_status=1 and cei.fields_id=ce.fields_id and cei.languages_id =" . $languages_id);
      while($extra_fields = tep_db_fetch_array($extra_fields_query)){
        if(strlen($HTTP_POST_VARS['fields_' . $extra_fields['fields_id']])<$extra_fields['fields_size']){
          $error = true;
          $ciag_bledu .= '* ' .  sprintf(ENTRY_EXTRA_FIELDS_ERROR,$extra_fields['fields_name'],$extra_fields['fields_size']).'<br>';
        }
      }
      // EOF Customers extra fields



      if (ACCOUNT_VALIDATION == 'true' && ACCOUNT_CREATE_VALIDATION == 'true') {
        $sql = "SELECT * FROM " . TABLE_ANTI_ROBOT_REGISTRATION . " WHERE session_id = '" . tep_session_id() . "' LIMIT 1";
        if( !$result = tep_db_query($sql) ) {
          $error = true;
          $entry_antirobotreg_error = true;
          $text_antirobotreg_error = ERROR_VALIDATION_1;
        } else {
          $entry_antirobotreg_error = false;
          $anti_robot_row = tep_db_fetch_array($result);
          if (( strtoupper($HTTP_POST_VARS['antirobotreg']) != $anti_robot_row['reg_key'] ) || ($anti_robot_row['reg_key'] == '') || (strlen($antirobotreg) != ENTRY_VALIDATION_LENGTH)) {
            $error = true;
            $entry_antirobotreg_error = true;
            $text_antirobotreg_error = ERROR_VALIDATION_2;
          } else {
            $sql = "DELETE FROM " . TABLE_ANTI_ROBOT_REGISTRATION . " WHERE session_id = '" . tep_session_id() . "'";
            if( !$result = tep_db_query($sql) ) {
              $error = true;
              $entry_antirobotreg_error = true;
              $text_antirobotreg_error = ERROR_VALIDATION_3;
            } else {
              $sql = "OPTIMIZE TABLE " . TABLE_ANTI_ROBOT_REGISTRATION . "";
              if( !$result = tep_db_query($sql) ) {
                $error = true;
                $entry_antirobotreg_error = true;
                $text_antirobotreg_error = ERROR_VALIDATION_4;
              } else {
                $entry_antirobotreg_error = false;
              }
            }
          }
        }
        if ($entry_antirobotreg_error == true) $ciag_bledu .= '* ' .  $text_antirobotreg_error.'<br>';
      }

      if ($agreement == false) {
        $error = true;
        $ciag_bledu .= '* ' .  ENTRY_AGREEMENT_ERROR.'<br>';
      }

      if ($polecajacy != '') {
          $polecajacy_status = '1';
          $sprawdz_email_query = tep_db_query("select customers_id from " . TABLE_CUSTOMERS . " where customers_email_address = '" . tep_db_input($polecajacy) . "'");
          $sprawdz_email = tep_db_fetch_array($sprawdz_email_query);
          $polecajacy_id = $sprawdz_email['customers_id'];

	        $customer_order = tep_db_query("select count(*) as total from " . TABLE_ORDERS . " where customers_id = '" . (int)$polecajacy_id . "' and orders_status = '".PROMO_POLECONY_ORDER_STATUS_ID."'");
          $check = tep_db_fetch_array($customer_order);
          if ($check['total'] < '1') {
  	         $error = true;
             $ciag_bledu .= '* ' .  ENTRY_POLECAJACY_ORDERS_ERROR.'<br>';
	        }

      }

      // SPRAWDZANIE DODATKOWEGO ADRESU DO WYSYLKI /////////////////////////////////////////
      $inny_adres = tep_db_prepare_input($HTTP_POST_VARS['inny_adres']);
      $ciag_bledu_dostawa = '';
      if ($inny_adres == '1') {
            $company_dostawa = tep_db_prepare_input($HTTP_POST_VARS['company_dostawa']);
            $firstname_dostawa = tep_db_prepare_input($HTTP_POST_VARS['firstname_dostawa']);
            $lastname_dostawa = tep_db_prepare_input($HTTP_POST_VARS['lastname_dostawa']);
            $street_address_dostawa = tep_db_prepare_input($HTTP_POST_VARS['street_address_dostawa']);
            $street_address_dom_dostawa = tep_db_prepare_input($HTTP_POST_VARS['street_address_dom_dostawa']);
            $street_address_mieszkanie_dostawa = tep_db_prepare_input($HTTP_POST_VARS['street_address_mieszkanie_dostawa']);
            if (ACCOUNT_SUBURB == 'true') $suburb_dostawa = tep_db_prepare_input($HTTP_POST_VARS['suburb_dostawa']);
            $postcode_dostawa = tep_db_prepare_input($HTTP_POST_VARS['postcode_dostawa']);
            $city_dostawa = tep_db_prepare_input($HTTP_POST_VARS['city_dostawa']);
            if (ACCOUNT_STATE == 'true') {
                $state_dostawa = tep_db_prepare_input($HTTP_POST_VARS['state_dostawa']);
                if (isset($HTTP_POST_VARS['zone_id_dostawa'])) {
                  $zone_id_dostawa = tep_db_prepare_input($HTTP_POST_VARS['zone_id_dostawa']);
                } else {
                  $zone_id_dostawa = false;
                }
            }
            $country_dostawa = tep_db_prepare_input($HTTP_POST_VARS['country_dostawa']);
            // sprawdzenie bledow
			//sprawdza czy wypelnione pole imie
            if (strlen($firstname_dostawa) < ENTRY_FIRST_NAME_MIN_LENGTH) { $error = true; $ciag_bledu_dostawa .= '* ' .  ENTRY_FIRST_NAME_ERROR.'<br>'; }
			//sprawdza czy wypelnione pole nazwisko
            if (strlen($lastname_dostawa) < ENTRY_LAST_NAME_MIN_LENGTH) { $error = true; $ciag_bledu_dostawa .= '* ' .  ENTRY_LAST_NAME_ERROR.'<br>'; }
			//sprawdza czy wypelnione pole ulica
	        if (strlen($street_address_dostawa) < ENTRY_STREET_ADDRESS_MIN_LENGTH) { $error = true; $ciag_bledu_dostawa .= '* ' .  ENTRY_STREET_ADDRESS_ERROR.'<br>';}
			//sprawdza czy wypelnione nr domu 
			if (empty($street_address_dom_dostawa)) { $error = true; $ciag_bledu_dostawa .= '* ' .  ENTRY_STREET_ADDRESS_DOM_ERROR.'<br>'; }			
			//sprawdza czy wypelnione pole kod pocztowy
	        if (strlen($postcode_dostawa) < ENTRY_POSTCODE_MIN_LENGTH) { $error = true; $ciag_bledu_dostawa .= '* ' .  ENTRY_POST_CODE_ERROR.'<br>'; }
			//sprawdza czy wypelnione pole miasto
	        if (strlen($city_dostawa) < ENTRY_CITY_MIN_LENGTH) { $error = true; $ciag_bledu_dostawa .= '* ' .  ENTRY_CITY_ERROR.'<br>';}
            //
            $dostawa_zaznaczona = 'checked';
          } else {
            $dostawa_zaznaczona = '';
      }
      //////////////////////////////////////////////////////////////////////////////////////
      // SPRAWDZANIE DODATKOWEGO ADRESU DO FAKTURY /////////////////////////////////////////
      $inny_adres_faktury = tep_db_prepare_input($HTTP_POST_VARS['inny_adres_faktury']);
      $ciag_bledu_faktura = '';
      if ($inny_adres_faktury == '1') {
            $company_faktura = tep_db_prepare_input($HTTP_POST_VARS['company_faktura']);
            $nip_faktura = tep_db_prepare_input($HTTP_POST_VARS['nip_faktura']);
            $street_address_faktura = tep_db_prepare_input($HTTP_POST_VARS['street_address_faktura']);
            $street_address_dom_faktura = tep_db_prepare_input($HTTP_POST_VARS['street_address_dom_faktura']);
            $street_address_mieszkanie_faktura = tep_db_prepare_input($HTTP_POST_VARS['street_address_mieszkanie_faktura']);
            if (ACCOUNT_SUBURB == 'true') $suburb_faktura = tep_db_prepare_input($HTTP_POST_VARS['suburb_faktura']);
            $postcode_faktura = tep_db_prepare_input($HTTP_POST_VARS['postcode_faktura']);
            $city_faktura = tep_db_prepare_input($HTTP_POST_VARS['city_faktura']);
            if (ACCOUNT_STATE == 'true') {
                $state_faktura = tep_db_prepare_input($HTTP_POST_VARS['state_faktura']);
                if (isset($HTTP_POST_VARS['zone_id_faktura'])) {
                  $zone_id_faktura = tep_db_prepare_input($HTTP_POST_VARS['zone_id_faktura']);
                } else {
                  $zone_id_faktura = false;
                }
            }
            $country_faktura = tep_db_prepare_input($HTTP_POST_VARS['country_faktura']);
            // sprawdzenie bledow
			//sprawdza czy wypelnione pole firma
			if (strlen($company_faktura) < ENTRY_COMPANY_MIN_LENGTH) { $error = true; $ciag_bledu_faktura .= '* ' .  ENTRY_COMPANY_ERROR.'<br>';}
			//sprawdza czy wypelnione pole NIP
			if (strlen($nip_faktura) < ENTRY_NIP_MIN_LENGTH) { $error = true; $ciag_bledu_faktura .= '* ' .  ENTRY_NIP_ERROR.'<br>'; }
			//sprawdza czy wypelnione pole ulica
	        if (strlen($street_address_faktura) < ENTRY_STREET_ADDRESS_MIN_LENGTH) { $error = true; $ciag_bledu_faktura .= '* ' .  ENTRY_STREET_ADDRESS_ERROR.'<br>';}
			//sprawdza czy wypelnione nr domu 
			if (empty($street_address_dom_faktura)) { $error = true; $ciag_bledu_faktura .= '* ' .  ENTRY_STREET_ADDRESS_DOM_ERROR.'<br>'; }				
			//sprawdza czy wypelnione pole kod pocztowy
	        if (strlen($postcode_faktura) < ENTRY_POSTCODE_MIN_LENGTH) { $error = true; $ciag_bledu_faktura .= '* ' .  ENTRY_POST_CODE_ERROR.'<br>'; }
			//sprawdza czy wypelnione pole miasto
	        if (strlen($city_faktura) < ENTRY_CITY_MIN_LENGTH) { $error = true; $ciag_bledu_faktura .= '* ' .  ENTRY_CITY_ERROR.'<br>';}
            //
            $faktura_zaznaczona = 'checked';
          } else {
            $faktura_zaznaczona = '';
      }
      //////////////////////////////////////////////////////////////////////////////////////

      // GENROWANIE OKNA BLEDU
      $szerokosc_okna = 0;
      if (tep_not_null($ciag_bledu)) {
         $ciag_bledu_wynik .= '<div style=\'width:280px;padding:5px;float:left;text-align:center;\' class=\'smallText\'>'.BLAD_REJESTRACJA_TEKST.$ciag_bledu.'</div>';
         $szerokosc_okna = $szerokosc_okna+300;
      }
      if (tep_not_null($ciag_bledu_dostawa)) {
         $ciag_bledu_wynik .= '<div style=\'width:280px;padding:5px;float:left;text-align:center;\' class=\'smallText\'>'.BLAD_REJESTRACJA_TEKST_DOSTAWA.$ciag_bledu_dostawa.'</div>';
         $szerokosc_okna = $szerokosc_okna+300;
      }
      if (tep_not_null($ciag_bledu_faktura)) {
         $ciag_bledu_wynik .= '<div style=\'width:280px;padding:5px;float:left;text-align:center;\' class=\'smallText\'>'.BLAD_REJESTRACJA_TEKST_FAKTURA.$ciag_bledu_faktura.'</div>';
         $szerokosc_okna = $szerokosc_okna+300;
      }

      if (tep_not_null($ciag_bledu) || tep_not_null($ciag_bledu_dostawa) || tep_not_null($ciag_bledu_faktura)) {
          $messageStack->add('create_account',$ciag_bledu_wynik.'<div style=\'clear:both\'></div>','',$szerokosc_okna);
      }

      // JEZELI NIE MA BLEDOW  /////////////////////////////////////////////////////////////////////////
      if ($error == false) {

        if ( NEW_CUSTOMERS_ENABLED == 'true') $cust_status = 1;
        else $cust_status = 0;
        // PWA BOF
        if (!isset($HTTP_GET_VARS['guest']) && !isset($HTTP_POST_VARS['guest']))
        {
          $dbPass = tep_encrypt_password($password);
          $guestaccount = '0';
        }else{
          $dbPass = 'null';
          $guestaccount = '1';
        }
        // PWA EOF

         $sql_data_array = array('customers_firstname' => $firstname,
                              'customers_lastname' => $lastname,
							  'nick' => $nick,
                              'customers_email_address' => $email_address,
                              'customers_telephone' => $telephone,
                              'customers_fax' => $fax,
                              'customers_newsletter' => $newsletter,
                              'customers_password' => $dbPass,
							  'customers_agreement' => $agreement,
                              'customers_polecony' => $polecajacy_status,
                              'customers_polecajacy_id' => $polecajacy_id,
                              'customers_status' => $cust_status,
                              'guest_account' => $guestaccount);

         if (ACCOUNT_GENDER == 'true') $sql_data_array['customers_gender'] = $gender;
         if (ACCOUNT_DOB == 'true') $sql_data_array['customers_dob'] = tep_date_raw($dob);

         if (isset($HTTP_GET_VARS['guest']) or isset($HTTP_POST_VARS['guest'])) {
          tep_db_query("delete from " . TABLE_CUSTOMERS . " where customers_id = '" . (int)$customer_id . "'");
          tep_db_query("delete from " . TABLE_ADDRESS_BOOK . " where customers_id = '" . (int)$customer_id . "'");
          tep_db_query("delete from " . TABLE_CUSTOMERS_INFO . " where customers_info_id = '" . (int)$customer_id . "'");
          tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "'");
          tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "'");
         }
         tep_db_perform(TABLE_CUSTOMERS, $sql_data_array);

         $customer_id = tep_db_insert_id();

          // BOF Customers extra fields
          $extra_fields_query = tep_db_query("select ce.fields_id from " . TABLE_EXTRA_FIELDS . " ce where ce.fields_status=1 ");
          while($extra_fields = tep_db_fetch_array($extra_fields_query)) {
            if(isset($HTTP_POST_VARS['fields_' . $extra_fields['fields_id']])){
              $sql_data_array = array('customers_id' => (int)$customer_id,
                                      'fields_id' => $extra_fields['fields_id'],
                                      'value' => $HTTP_POST_VARS['fields_' . $extra_fields['fields_id']]);
            } else {
              $sql_data_array = array('customers_id' => (int)$customer_id,
                                      'fields_id' => $extra_fields['fields_id'],
                                      'value' => '');
              $is_add = false;
              for($i = 1; $i <= $HTTP_POST_VARS['fields_' . $extra_fields['fields_id'] . '_total']; $i++) {
                if(isset($HTTP_POST_VARS['fields_' . $extra_fields['fields_id'] . '_' . $i])) {
                  if($is_add) {
                    $sql_data_array['value'] .= "\n";
                  } else {
                    $is_add = true;
                  }
                  $sql_data_array['value'] .= $HTTP_POST_VARS['fields_' . $extra_fields['fields_id'] . '_' . $i];
                }
              }
            }

            tep_db_perform(TABLE_CUSTOMERS_TO_EXTRA_FIELDS, $sql_data_array);
          }
          // EOF Customers extra fields

         $sql_data_array = array('customers_id' => $customer_id,
                              'entry_firstname' => $firstname,
                              'entry_lastname' => $lastname,
                              'entry_street_address' => $street_address,
                              'entry_postcode' => $postcode,
                              'entry_city' => $city,
                              'entry_country_id' => $country);

         if (ACCOUNT_GENDER == 'true') $sql_data_array['entry_gender'] = $gender;
         $sql_data_array['entry_company'] = $company;
         $sql_data_array['entry_nip'] = $nip;
         if (ACCOUNT_SUBURB == 'true') $sql_data_array['entry_suburb'] = $suburb;
         if (ACCOUNT_STATE == 'true') {
           if ($zone_id > 0) {
            $sql_data_array['entry_zone_id'] = $zone_id;
            $sql_data_array['entry_state'] = '';
           } else {
            $sql_data_array['entry_zone_id'] = '0';
            $sql_data_array['entry_state'] = $state;
           }
         }

         if (isset($HTTP_GET_VARS['guest']) or isset($HTTP_POST_VARS['guest']))
         tep_session_register('customer_is_guest');

         tep_db_perform(TABLE_ADDRESS_BOOK, $sql_data_array);
         $address_id = tep_db_insert_id();

         tep_db_query("update " . TABLE_CUSTOMERS . " set customers_default_address_id = '" . (int)$address_id . "' where customers_id = '" . (int)$customer_id . "'");
         tep_db_query("insert into " . TABLE_CUSTOMERS_INFO . " (customers_info_id, customers_info_number_of_logons, customers_info_date_account_created) values ('" . (int)$customer_id . "', '0', now())");

         // DODAWANIE ADRESU PRZESYLKI -------------------------------------------------->
         if ($inny_adres == '1') {
             $sql_data_array = array('customers_id' => $customer_id,
                          'entry_company' => $company_dostawa,
                          'entry_firstname' => $firstname_dostawa,
                          'entry_lastname' => $lastname_dostawa,
                          'entry_street_address' => $street_address_dostawa .' '.$street_address_dom_dostawa.((tep_not_null($street_address_mieszkanie_dostawa)) ? '/'.$street_address_mieszkanie_dostawa : ''),
                          'entry_postcode' => $postcode_dostawa,
                          'entry_city' => $city_dostawa,
                          'entry_country_id' => $country_dostawa);
             if (ACCOUNT_SUBURB == 'true') $sql_data_array['entry_suburb'] = $suburb_dostawa;
             if (ACCOUNT_STATE == 'true') {
               if ($zone_id > 0) {
                $sql_data_array['entry_zone_id'] = $zone_id_dostawa;
                $sql_data_array['entry_state'] = '';
               } else {
                $sql_data_array['entry_zone_id'] = '0';
                $sql_data_array['entry_state'] = $state_dostawa;
               }
             }
             tep_db_perform(TABLE_ADDRESS_BOOK, $sql_data_array);
             $address_id_dostawa = tep_db_insert_id();
             //
             if (!tep_session_is_registered('sendto')) {
                tep_session_register('sendto');
                $sendto = $address_id_dostawa;
               } else {
                $sendto = $address_id_dostawa;
             }
         }
         // DODAWANIE KONIEC ----------------------------------------------------------->
         // DODAWANIE ADRESU FAKTURY -------------------------------------------------->
         if ($inny_adres_faktury == '1') {
             $sql_data_array = array('customers_id' => $customer_id,
                          'entry_company' => $company_faktura,
                          'entry_nip' => $nip_faktura,
                          'entry_street_address' => $street_address_faktura .' '.$street_address_dom_faktura.((tep_not_null($street_address_mieszkanie_faktura)) ? '/'.$street_address_mieszkanie_faktura : ''),
                          'entry_postcode' => $postcode_faktura,
                          'entry_city' => $city_faktura,
                          'entry_country_id' => $country_faktura);
             if (ACCOUNT_SUBURB == 'true') $sql_data_array['entry_suburb'] = $suburb_faktura;
             if (ACCOUNT_STATE == 'true') {
               if ($zone_id > 0) {
                $sql_data_array['entry_zone_id'] = $zone_id_faktura;
                $sql_data_array['entry_state'] = '';
               } else {
                $sql_data_array['entry_zone_id'] = '0';
                $sql_data_array['entry_state'] = $state_faktura;
               }
             }
             tep_db_perform(TABLE_ADDRESS_BOOK, $sql_data_array);
             $address_id_faktura = tep_db_insert_id();
             //
             if (!tep_session_is_registered('billto')) {
                tep_session_register('billto');
                $billto = $address_id_faktura;
               } else {
                $billto = $address_id_faktura;
             }
         }
         // DODAWANIE KONIEC ----------------------------------------------------------->

         if (SESSION_RECREATE == 'True') {
          tep_session_recreate();
         }

         $customer_first_name = $firstname;
         $customer_default_address_id = $address_id;
         $customer_country_id = $country;
         $customer_zone_id = $zone_id;
         tep_session_register('customer_id');
         tep_session_register('customer_first_name');
         tep_session_register('customer_default_address_id');
         tep_session_register('customer_country_id');
         tep_session_register('customer_zone_id');

         if (NEW_CUSTOMERS_ENABLED=='true') {
             $check_query = tep_db_query("select session_id,products_id,products_text_attributes_id,products_text_attributes_text from customers_basket_text_attributes where session_id = '" .tep_session_id(). "'");
             while ($attr = tep_db_fetch_array($check_query)) {
                $pod_query = tep_db_query("select session_id, products_id,products_text_attributes_id,products_text_attributes_text from customers_basket_text_attributes where session_id = '" .(int)$customer_id. "' and products_id = '" . $attr['products_id']. "' and products_text_attributes_id = '".$attr['products_text_attributes_id']. "'");
                $ciag = $attr['products_text_attributes_text'];
                if ($jest = tep_db_fetch_array($pod_query)) {
                    tep_db_query("update customers_basket_text_attributes set  customqty = customqty + 1, session_id = '".(int)$customer_id."', products_text_attributes_text = '" . $jest['products_text_attributes_text'] . addslashes('<b>; </b>') . $attr['products_text_attributes_text'] ."'  where session_id = '" .(int)$customer_id. "' and products_id = '" . $attr['products_id']. "' and products_text_attributes_id = '".$attr['products_text_attributes_id']. "'");
                    tep_db_query("delete from customers_basket_text_attributes where session_id = '" .tep_session_id(). "' and products_id = '" . $attr['products_id']. "' and products_text_attributes_id = '".$attr['products_text_attributes_id']. "'");
                  } else {
                    tep_db_query("update customers_basket_text_attributes set session_id = '".(int)$customer_id."' where session_id = '" . tep_session_id(). "' and products_id = '" . $attr['products_id']. "' and products_text_attributes_id = '".$attr['products_text_attributes_id']. "'");
                }
             }
         }

         if (isset($HTTP_GET_VARS['guest']) or isset($HTTP_POST_VARS['guest'])) tep_redirect(tep_href_link(FILENAME_CHECKOUT,'', 'SSL'));
         $cart->restore_contents();
         $wishlist->restore_wishlist();
         $name = $firstname . ' ' . $lastname;

         mail_create_account($name,$new_password,$email_address,$lastname,$firstname,$customer_id,$gender);

         $customersenable = NEW_CUSTOMERS_ENABLED;
         if ($customersenable=='false') {
            tep_session_unregister('customer_id');
            tep_session_unregister('customer_default_address_id');
            tep_session_unregister('customer_first_name');
            tep_session_unregister('customer_country_id');
            tep_session_unregister('customer_zone_id');
            tep_session_unregister('comments');
            tep_session_unregister('shipping');
            tep_session_unregister('cart_sid');
         }
         if (tep_session_is_registered('customer_is_guest')) {
            tep_session_unregister('customer_is_guest');
         }

         tep_redirect(tep_href_link(FILENAME_CREATE_ACCOUNT_SUCCESS, '', 'SSL'));
        }
      }

      // +Country-State Selector
    }
    if ( isset($HTTP_POST_VARS['action']) && $HTTP_POST_VARS['action'] == 'refresh') {$state = '';}
    if (!isset($country)){$country = DEFAULT_COUNTRY;}
    // -Country-State Selector

   if (!isset($HTTP_GET_VARS['guest']) && !isset($HTTP_POST_VARS['guest'])){
     if (!isset($HTTP_GET_VARS['act'])) {
        $breadcrumb->add(NAVBAR_TITLE, tep_href_link(FILENAME_CREATE_ACCOUNT, '', 'SSL'));
       } else {
        $breadcrumb->add(NAVBAR_TITLE, tep_href_link(FILENAME_CREATE_ACCOUNT, 'act='.$HTTP_GET_VARS['act'], 'SSL'));
     }
   }else{
     $breadcrumb->add(NAVBAR_TITLE_PWA, tep_href_link(FILENAME_CREATE_ACCOUNT, 'guest=guest', 'SSL'));
    }

    $content = CONTENT_CREATE_ACCOUNT;
    $javascript = 'form_check.js.php';
    include (bts_select('main', $content)); // BTSv1.5

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