<?php 
class Controlleraccountconfirmacaocadastro extends Controller {
	private $error = array();
	      
  	public function index() {
		if ($this->customer->isLogged()) {
	  		//$this->redirect($this->url->link('account/account', '', 'SSL'));
    	}

    	$this->language->load('account/confirmacao_cadastro');
		
		$this->document->setTitle($this->language->get('heading_title'));
		$this->document->addScript('catalog/view/javascript/jquery/colorbox/jquery.colorbox-min.js');
		$this->document->addStyle('catalog/view/javascript/jquery/colorbox/colorbox.css');
					
		$this->load->model('account/customer');
		$this->load->model('account/address');
		
		$this->data['entrega']= $this->model_account_address->getAddressEntrega();
       
	   if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate() ) {
		
			$this->model_account_customer->editCustomerBrasil($this->request->post);
			$this->model_account_address->editAddressBrasil($this->request->post);
			$this->model_account_customer->editNewsletter($this->request->post['news']);
			
			$this->session->data['success'] = $this->language->get('text_success');

			$this->redirect($this->url->link('account/account', '', 'SSL'));
		}

      	$this->data['breadcrumbs'] = array();

      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home'),        	
        	'separator' => false
      	); 

      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->language->get('text_account'),
			'href'      => $this->url->link('account/account', '', 'SSL'),      	
        	'separator' => $this->language->get('text_separator')
      	);
		
      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->language->get('text_confirmacao_cadastro'),
			'href'      => $this->url->link('account/confirmacao_cadastro', '', 'SSL'),      	
        	'separator' => $this->language->get('text_separator')
      	);
		
    	$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_account_already'] = sprintf($this->language->get('text_account_already'), $this->url->link('account/login', '', 'SSL'));
		$this->data['text_your_details'] = $this->language->get('text_your_details');
    	$this->data['text_your_address'] = $this->language->get('text_your_address');
    	$this->data['text_your_password'] = $this->language->get('text_your_password');
		$this->data['text_newsletter'] = $this->language->get('text_newsletter');
		$this->data['text_yes'] = $this->language->get('text_yes');
		$this->data['text_no'] = $this->language->get('text_no');
		$this->data['text_select']   = $this->language->get('text_select');
		$this->data['text_none']     = $this->language->get('text_none');
		$this->data['text_success']  = $this->language->get('text_success');
						
    	$this->data['entry_firstname'] = $this->language->get('entry_firstname');
    	$this->data['entry_lastname'] = $this->language->get('entry_lastname');
    	$this->data['entry_local'] = $this->language->get('entry_local');
		$this->data['entry_address_1'] = $this->language->get('entry_address_1');
    	$this->data['entry_address_2'] = $this->language->get('entry_address_2');
    	$this->data['entry_postcode'] = $this->language->get('entry_postcode');
    	$this->data['entry_city'] = $this->language->get('entry_city');
    	$this->data['entry_country'] = $this->language->get('entry_country');
    	$this->data['entry_zone'] = $this->language->get('entry_zone');
		$this->data['entry_newsletter'] = $this->language->get('entry_newsletter');
    	$this->data['entry_confirm'] = $this->language->get('entry_confirm');

		$this->data['button_continue'] = $this->language->get('button_continue');
		$this->data['action'] = $this->url->link('account/confirmacao_cadastro', '', 'SSL');
    
		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		
		if (isset($this->error['firstname'])) {
			$this->data['error_firstname'] = $this->error['firstname'];
		} else {
			$this->data['error_firstname'] = '';
		}	
		
		if (isset($this->error['lastname'])) {
			$this->data['error_lastname'] = $this->error['lastname'];
		} else {
			$this->data['error_lastname'] = '';
        }		
		
		if (isset($this->error['data_nascimento'])) {
		    	$this->data['error_data_nascimento'] = $this->error['data_nascimento'];
		} else {
				$this->data['error_data_nascimento'] = '';
		}
		
		if (isset($this->error['contribuinte'])) {
		 		 $this->data['error_contribuinte'] = $this->error['contribuinte'];
		} else {
				$this->data['error_contribuinte'] = '';
				
		}
		
			if (isset($this->error['perfil'])) {
						$this->data['error_perfil'] = $this->error['perfil'];
					} else {
						$this->data['error_perfil'] = '';
					}
				
	
				
 		if (isset($this->error['confirm'])) {
			$this->data['error_confirm'] = $this->error['confirm'];
		} else {
			$this->data['error_confirm'] = '';
		}
		
  								
  		if (isset($this->error['pay_address_1'])) {
			$this->data['error_pay_address_1'] = $this->error['pay_address_1'];
		} else {
			$this->data['error_pay_address_1'] = '';
		}
		
		if (isset($this->error['ship_address_1'])) {
			$this->data['error_ship_address_1'] = $this->error['ship_address_1'];
		} else {
			$this->data['error_ship_address_1'] = '';
		}
		
		
		if (isset($this->error['pay_numero'])) {
						$this->data['error_pay_numero'] = $this->error['pay_numero'];
					} else {
						$this->data['error_pay_numero'] = '';
					}
					
		if (isset($this->error['ship_numero'])) {
						$this->data['error_ship_numero'] = $this->error['ship_numero'];
					} else {
						$this->data['error_ship_numero'] = '';
					}			
		
   		
		
		//faturamento
		if (isset($this->error['pay_city'])) {
			$this->data['error_pay_city'] = $this->error['pay_city'];
		} else {
			$this->data['error_pay_city'] = '';
		}
		
		if (isset($this->error['pay_postcode'])) {
			$this->data['error_pay_postcode'] = $this->error['pay_postcode'];
		} else {
			$this->data['error_pay_postcode'] = '';
		}

		if (isset($this->error['pay_country_id'])) {
			$this->data['error_pay_country_id'] = $this->error['pay_country_id'];
		} else {
			$this->data['error_pay_country_id'] = '';
		}

		if (isset($this->error['pay_zone_id'])) {
			$this->data['error_pay_zone_id'] = $this->error['pay_zone_id'];
		} else {
			$this->data['error_pay_zone_id'] = '';
		}
		
		//entrega
		if (isset($this->error['ship_city'])) {
			$this->data['error_ship_city'] = $this->error['ship_city'];
		} else {
			$this->data['error_ship_city'] = '';
		}
		
		if (isset($this->error['ship_postcode'])) {
			$this->data['error_ship_postcode'] = $this->error['ship_postcode'];
		} else {
			$this->data['error_ship_postcode'] = '';
		}

		if (isset($this->error['ship_country_id'])) {
			$this->data['error_ship_country_id'] = $this->error['ship_country_id'];
		} else {
			$this->data['error_ship_country_id'] = '';
		}

		if (isset($this->error['ship_zone_id'])) {
			$this->data['error_ship_zone_id'] = $this->error['ship_zone_id'];
		} else {
			$this->data['error_ship_zone_id'] = '';
		}
		
		
         
        if (isset($this->error['sexo'])) {
			$this->data['error_sexo'] = $this->error['sexo'];
		} else {
			$this->data['error_sexo'] = '';
		}		 
 		
		if (isset($this->error['data_nascimento'])) {
			$this->data['error_data_nascimento'] = $this->error['data_nascimento'];
		} else {
			$this->data['error_data_nascimento'] = '';
		}		 
		
         if (($this->request->server['REQUEST_METHOD'] != 'POST')) {
	    	$customer_info = $this->model_account_customer->getCustomer($this->customer->getId());
        }
		
		if (isset($this->request->post['firstname'])) {
			$this->data['firstname'] = $this->request->post['firstname'];
		} elseif (isset($customer_info)) {
			$this->data['firstname'] = $customer_info['firstname'];
		} else {
			$this->data['firstname'] = '';
		}

		if (isset($this->request->post['lastname'])) {
			$this->data['lastname'] = $this->request->post['lastname'];
		} elseif (isset($customer_info)) {
			$this->data['lastname'] = $customer_info['lastname'];
		} else {
			$this->data['lastname'] = '';
		}
		
		if (isset($this->request->post['sexo'])) {
						$this->data['sexo'] = $this->request->post['sexo'];
					} elseif (isset($customer_info)) {
						$this->data['sexo'] = $customer_info['sexo'];
					} else {
						$this->data['sexo'] = '';
		}
		if (isset($this->request->post['data_nascimento'])) {
						$this->data['data_nascimento'] = $this->request->post['data_nascimento'];
					} elseif (isset($customer_info)) {
						$this->data['data_nascimento'] = $customer_info['data_nascimento'];
					} else {
						$this->data['data_nascimento'] = '';
		}
		
		
		$this->data['contribuinte'] = 'S';
							
		
		
		if (isset($this->request->post['perfil'])) {
						$this->data['perfil'] = $this->request->post['perfil'];
					} elseif (isset($customer_info)) {
						$this->data['perfil'] = $customer_info['perfil'];
					} else {
						$this->data['perfil'] = '';
		}
		
		
		if (isset($this->request->post['news'])) {
						$this->data['news'] = $this->request->post['news'];
					} elseif (isset($customer_info)) {
						$this->data['news'] = $customer_info['newsletter'];
					} else {
						$this->data['news'] = '';
		}
		
		
		$this->load->model('localisation/country');
		$this->data['countries'] = $this->model_localisation_country->getCountries();
		
		
		$this->data['entrega']= $this->model_account_address->getAddressEntrega();	
     
		
	    if (($this->request->server['REQUEST_METHOD'] != 'POST')) {
		
		$this->data['addresses']=array();
		$this->data['addresses'] = $this->model_account_address->getAddressesBrasil();
        
				
		
		
		foreach($this->data['addresses'] as $address){
		
		
		 if($address['address_id'] == $customer_info['address_id'] ){
		 
		   $this->data['pay_address_id']  = $address['address_id'];
		   $this->data['pay_address_1']   = $address['address_1'];
		   $this->data['pay_address_2']   = $address['address_2'];
		   $this->data['pay_postcode']    = $address['postcode'];
		   $this->data['pay_city']        = $address['city'];
		   $this->data['pay_country_id']  = $address['country_id'];		
		   $this->data['pay_zone_id']     = $address['zone_id'];
		   $this->data['pay_local']       = $address['local'];
	       $this->data['pay_complemento'] = $address['complemento'];
		   $this->data['pay_numero']      = $address['numero'];
		
    	}else{
		  
		   $this->data['ship_address_id']  = $address['address_id'];
		   $this->data['ship_address_1']   = $address['address_1'];
		   $this->data['ship_address_2']   = $address['address_2'];
		   $this->data['ship_postcode']    = $address['postcode'];
		   $this->data['ship_city']        = $address['city'];
		   $this->data['ship_country_id']  = $address['country_id'];		
		   $this->data['ship_zone_id']     = $address['zone_id'];
		   $this->data['ship_local']       = $address['local'];
		   $this->data['ship_complemento'] = $address['complemento'];
		   $this->data['ship_numero']      = $address['numero'];
		  	  
		  
		}
	 }
   }else{
         
		// faturamento 

		
		if (isset($this->request->post['pay_address_1'])) {
			  $this->data['pay_address_1'] = $this->request->post['pay_address_1'];
        }else{
		     $this->data['pay_address_1'] = "";
		}
        
        if (isset($this->request->post['pay_address_2'])) {
			  $this->data['pay_address_2'] = $this->request->post['pay_address_2'];
        }else{
		     $this->data['pay_address_2'] = "";
		}
       
        if (isset($this->request->post['pay_numero'])) {
			  $this->data['pay_numero'] = $this->request->post['pay_numero'];
        }else{
		     $this->data['pay_numero'] = "";
		}
    	
        if (isset($this->request->post['pay_complemento'])) {
			  $this->data['pay_complemento'] = $this->request->post['pay_complemento'];
        }else{
		     $this->data['pay_complemento'] = "";
		}		
		
		if (isset($this->request->post['pay_local'])) {
			  $this->data['pay_local'] = $this->request->post['pay_local'];
        }else{
		     $this->data['pay_local'] = "";
		}	
		
		if (isset($this->request->post['pay_city'])) {
			  $this->data['pay_city'] = $this->request->post['pay_city'];
        }else{
		     $this->data['pay_city'] = "";
		}	
		
		if (isset($this->request->post['pay_zone_id'])) {
			  $this->data['pay_zone_id'] = $this->request->post['pay_zone_id'];
        }else{
		     $this->data['pay_zone_id'] = "";
		}	
		
		if (isset($this->request->post['pay_country_id'])) {
			  $this->data['pay_country_id'] = $this->request->post['pay_country_id'];
        }else{
		     $this->data['pay_country_id'] = "";
		}	
		
		if (isset($this->request->post['pay_postcode'])) {
			  $this->data['pay_postcode'] = $this->request->post['pay_postcode'];
        }else{
		     $this->data['pay_postcode'] = "";
		}	
		
		// entrega
		    
				 
        if (isset($this->request->post['ship_address_1'])) {
			  $this->data['ship_address_1'] = $this->request->post['ship_address_1'];
        }else{
		      $this->data['ship_address_1'] = "";
		}
        
        if (isset($this->request->post['ship_address_2'])) {
			  $this->data['ship_address_2'] = $this->request->post['ship_address_2'];
        }else{
		      $this->data['ship_address_2'] = "";
		}
		
       
        if (isset($this->request->post['ship_numero'])) {
			  $this->data['ship_numero'] = $this->request->post['ship_numero'];
        }else{
		      $this->data['ship_numero'] = "";
		}
    	
        if (isset($this->request->post['ship_complemento'])) {
			  $this->data['ship_complemento'] = $this->request->post['ship_complemento'];
        }else{
		      $this->data['ship_complemento'] = "";
		}		
		
		if (isset($this->request->post['ship_local'])) {
			  $this->data['ship_local'] = $this->request->post['ship_local'];
        }else{
		      $this->data['ship_local'] = "";
		}	
		
		if (isset($this->request->post['ship_city'])) {
			  $this->data['ship_city'] = $this->request->post['ship_city'];
        }else{
		      $this->data['ship_city'] = "";
		}	
		
		if (isset($this->request->post['ship_zone_id'])) {
			  $this->data['ship_zone_id'] = $this->request->post['ship_zone_id'];
        }else{
		      $this->data['ship_zone_id'] = "";
		}	
		
		if (isset($this->request->post['ship_country_id'])) {
			  $this->data['ship_country_id'] = $this->request->post['ship_country_id'];
        }else{
		      $this->data['ship_country_id'] = "";
		}	
		
		if (isset($this->request->post['ship_postcode'])) {
			  $this->data['ship_postcode'] = $this->request->post['ship_postcode'];
        }else{
		      $this->data['ship_postcode'] = "";
		 }	
   
        }
        			 
		$this->load->model('account/customer');
		
		if (isset($this->request->post['confirm'])) {
    		$this->data['confirm'] = $this->request->post['confirm'];
		} else {
			$this->data['confirm'] = '';
		}
		
		if (isset($this->request->post['newsletter'])) {
    		$this->data['newsletter'] = $this->request->post['newsletter'];
		} else {
			$this->data['newsletter'] = '';
		}	

		if ($this->config->get('config_account_id')) {
			$this->load->model('catalog/information');
			
			$information_info = $this->model_catalog_information->getInformation($this->config->get('config_account_id'));
			
			if ($information_info) {
				$this->data['text_agree'] = $this->language->get('text_agree').'<a class="politica"><b>'.$information_info['title'].'</b></a>';//sprintf($this->language->get('text_agree'), $this->url->link('information/information/info', 'information_id=' . $this->config->get('config_account_id'), 'SSL'), $information_info['title'], $information_info['title']);
			} else {
				$this->data['text_agree'] = '';
			}
		} else {
			$this->data['text_agree'] = '';
		}
		
		//criado para buscar a politica de privacidade via ajax
    $this->data['agree_Politica_privacidade'] = $this->url->link('information/information/info&information_id=' . $this->config->get('config_account_id'), '', 'SSL');
		
		if (isset($this->request->post['agree'])) {
      		$this->data['agree'] = $this->request->post['agree'];
		} else {
			$this->data['agree'] = false;
		}
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/account/confirmacao_cadastro.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/account/confirmacao_cadastro.tpl';
		} else {
			$this->template = 'default/template/account/confirmacao_cadastro.tpl';
		}
		
		$this->children = array(
			'common/column_left',
			'common/column_right',
			'common/content_top',
			'common/content_bottom',
			'common/footer',
			'common/header'	
		);
				
		$this->response->setOutput($this->render());	
  	}
	
	
  	protected function validate() {
    	if ((utf8_strlen($this->request->post['firstname']) < 1) || (utf8_strlen($this->request->post['firstname']) > 32)) {
      		$this->error['firstname'] = $this->language->get('error_firstname');
    	}

    	if ((utf8_strlen($this->request->post['lastname']) < 1) || (utf8_strlen($this->request->post['lastname']) > 32)) {
      		$this->error['lastname'] = $this->language->get('error_lastname');
    	}
        
		// Valida campo Data de Nascimento
					$dataparts = explode("/",$this->request->post['data_nascimento']);
					if (strlen(utf8_decode($this->request->post['data_nascimento'])) > 0) {
						// Se o array n�o tiver 3 posi��es a data esta incorreta
						if (sizeof($dataparts) != '3'){
							$this->error['data_nascimento'] = "Aten��o: A data informada � inv�lida";
						}else{
							// Checa validade da data
							if (!checkdate($dataparts[1],$dataparts[0],$dataparts[2])) {
								$this->error['data_nascimento'] = "Aten��o: A data informada � inv�lida";
							}
						} 
					}
		
		
		$this->load->model('localisation/country');
		
		//Valida��o para endere�o de faturamento
		
    	if ((utf8_strlen($this->request->post['pay_address_1']) < 3) || (utf8_strlen($this->request->post['pay_address_1']) > 128)) {
      		$this->error['pay_address_1'] = $this->language->get('error_address_1');
    	}
		
		$numero = preg_replace('/([^0-9])/i','',$this->request->post['pay_numero']);
					if ((utf8_strlen($numero) < 1) || (utf8_strlen($numero) > 6)) {
					$this->error['pay_numero'] = 'Aten��o: O n�mero deve ter entre 1 e 6 caracteres!';
		}
		

    	if ((utf8_strlen($this->request->post['pay_city']) < 2) || (utf8_strlen($this->request->post['pay_city']) > 128)) {
      		$this->error['pay_city'] = $this->language->get('error_city');
    	}

		$country_info = $this->model_localisation_country->getCountry($this->request->post['pay_country_id']);
		
		if ($country_info) {
			if ($country_info['postcode_required'] && (utf8_strlen($this->request->post['pay_postcode']) < 2) || (utf8_strlen($this->request->post['pay_postcode']) > 10)) {
				$this->error['pay_postcode'] = $this->language->get('error_postcode');
			}
		}
		
		if ($this->request->post['pay_country_id'] == '') {
      		$this->error['pay_country_id'] = $this->language->get('error_country_id');
    	}
		
		if (!isset($this->request->post['pay_zone_id']) || $this->request->post['pay_zone_id'] == '') {
      		$this->error['pay_zone_id'] = $this->language->get('error_zone');
    	}
		

        // Valida��o para endere�o de entrega
		//throw new exception($this->data['entrega']);
	if ($this->data['entrega']==1){
		
		if ((utf8_strlen($this->request->post['ship_address_1']) < 3) || (utf8_strlen($this->request->post['ship_address_1']) > 128)) {
      		$this->error['ship_address_1'] = $this->language->get('error_address_1');
    	}
		
		$numero = preg_replace('/([^0-9])/i','',$this->request->post['ship_numero']);
					if ((utf8_strlen($numero) < 1) || (utf8_strlen($numero) > 6)) {
						$this->error['payment_numero'] = 'Aten��o: O n�mero deve ter entre 1 e 6 caracteres!';
		}
		
		
		

    	if ((utf8_strlen($this->request->post['ship_city']) < 2) || (utf8_strlen($this->request->post['ship_city']) > 128)) {
      		$this->error['ship_city'] = $this->language->get('error_city');
    	}

        $country_info = $this->model_localisation_country->getCountry($this->request->post['ship_country_id']);
		
		if ($country_info) {
			if ($country_info['postcode_required'] && (utf8_strlen($this->request->post['ship_postcode']) < 2) || (utf8_strlen($this->request->post['ship_postcode']) > 10)) {
				$this->error['ship_postcode'] = $this->language->get('error_postcode');
			}
		}

    	if ($this->request->post['ship_country_id'] == '') {
      		$this->error['ship_country'] = $this->language->get('error_country');
    	}
		
    	if (!isset($this->request->post['ship_zone_id']) || $this->request->post['ship_zone_id'] == '') {
      		$this->error['ship_zone'] = $this->language->get('error_zone');
    	}

    }	
    	
		if ($this->config->get('config_account_id')) {
			$this->load->model('catalog/information');
			
			$information_info = $this->model_catalog_information->getInformation($this->config->get('config_account_id'));
			
			if ($information_info && !isset($this->request->post['agree'])) {
      			$this->error['warning'] = sprintf($this->language->get('error_agree'), $information_info['title']);
			}
		}
		
    	if (!$this->error) {
      		return true;
    	} else {
      		return false;
    	}
  	}
	
	public function country() {
		$json = array();
		
		$this->load->model('localisation/country');

    	$country_info = $this->model_localisation_country->getCountry($this->request->get['country_id']);
		
		if ($country_info) {
			$this->load->model('localisation/zone');

			$json = array(
				'country_id'        => $country_info['country_id'],
				'name'              => $country_info['name'],
				'iso_code_2'        => $country_info['iso_code_2'],
				'iso_code_3'        => $country_info['iso_code_3'],
				'address_format'    => $country_info['address_format'],
				'postcode_required' => $country_info['postcode_required'],
				'zone'              => $this->model_localisation_zone->getZonesByCountryId($this->request->get['country_id']),
				'status'            => $country_info['status']		
			);
		}
		
		$this->response->setOutput(json_encode($json));
	}	
}
?>