<?php
///////////////////////////////////////////////////////////////////////////////////////////
///Script PHP/MYSQL of management of classifieds ads developed by Script PAG
///Script PAG all rights reserved. Use under license. http://www.script-pag.com
///////////////////////////////////////////////////////////////////////////////////////////

############################################################

require_once __DIR__.'/includes/all_fns.php';

///////////////////////////////////
//Check if the member or PRO is logged in
//////////////////////////////////

if(!isset($_SESSION['connect_id']))
redirect('acc_connexion.php');

///////////////////////////////////
//Initializing variable $error
//////////////////////////////////

$error = array();

///////////////////////////////////
//Validation of the region
//////////////////////////////////
	
if (isset($_POST['reg']))
$reg = (int) $_POST['reg'];
	
else $reg = $member['id_reg'];

///////////////////////////////////
//Validation of the county
//////////////////////////////////

if (isset($_POST['county']))
$county = (int) $_POST['county'];
	
else $county = $member['id_county'];

///////////////////////////////////
//Validation of the category
//////////////////////////////////

if(isset($_POST['cat']))
$cat = (int) $_POST['cat'];

else $cat = $member['id_cat'];

///////////////////////////////////
//Validation of data
//////////////////////////////////

if(isset($_POST['name']))
{
	if($member['type'] == 2)
	{
		if(empty($_POST['comp_name']))
		$error['comp_name'] = 1;
		
		if($cache_param_fields['act_comp_num'] == 1)
		{
			if(empty($_POST['comp_num']))
			$error['comp_num'] = 1;
		}
	}
	
	if(empty($_POST['name']))
	$error['name'] = 1;
	
	if(empty($_POST['first_name']))
	$error['first_name'] = 1;
	
	if($member['type'] == 1 && !empty($_POST['pseudo_display']) && empty($_POST['pseudo']))
	$error['pseudo'] = 1;
	
	if($reg == 0)
	$error['reg'] = 1;
	
	if(isset($_POST['county']) && empty($_POST['county']))
	$error['county'] = 1;
	
	if(isset($_POST['cat']) && empty($_POST['cat']))
	$error['cat'] = 1;
	
	if(empty($_POST['address']))
	$error['address'] = 1;
	
	if($cache_param_fields['act_postcode'] == 1)
	{
		if(isset($_POST['postcode']) && empty($_POST['postcode']))
		$error['postcode'] = 1;
	}
	
	if($cache_param_fields['act_city'] == 1)
	{
		if(empty($_POST['city']))
		$error['city'] = 1;
	}
	
	if(empty($_POST['phone']))
	$error['phone'] = 1;
	
	if(!valid_email($_POST['email']))
	$error['email'] = 1;
	
	if(check_email_acc($_POST['email'], $member['id_acc']))
	$error['email_duplicate'] = 1;
	
	if((!empty($_POST['pas']) || !empty($_POST['new_pas']) || !empty($_POST['new_pas2'])) && check_account($member['email'], $_POST['pas']))
	{
		if(preg_match('#^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).{8,}#', $_POST['new_pas']) == false)
		$error['str_pas'] = 1;
		
		if(empty($_POST['new_pas']) || $_POST['new_pas'] != $_POST['new_pas2'])
		$error['pas'] = 2;
	}
	elseif(!empty($_POST['pas']) || !empty($_POST['new_pas']) || !empty($_POST['new_pas2']))
	$error['pas'] = 1;
}

///////////////////////////////////
//Variables of elements
//////////////////////////////////

$index = 0;
$account = 1;

///////////////////////////////////
//Update the account
//////////////////////////////////

if(isset($_POST['name']) && empty($error))
{
	update_account($_POST, $member['id_acc']);
	$conn = null;
	
	redirect('acc_data.php?action=SuccesUpdate');
}
///////////////////////////////////
//Confirmation of account updated
//////////////////////////////////

elseif(isset($_GET['action']) && $_GET['action'] == 'SuccesUpdate')
{
	///////////////////////////////////
	//Title and Meta
	//////////////////////////////////

	$title = $language['account_conf_update_title'];
	$description = $language['account_conf_update_desc'];
	$words = $language['account_conf_update_key'];
	$info_page = $language['account_dashboard_pers_info'];
	
	///////////////////////////////////
	//Text of the page
	//////////////////////////////////
	
	$text = $language['account_conf_update_msg'];
	
	///////////////////////////////////
	//Display view
	//////////////////////////////////
	
	pool::execute('beforestart');
	
	$view = array();
	$view[] = 'template/tpl_txt.php';
	
	require_once __DIR__.'/template/tpl_layout.php';
}
else
{
	///////////////////////////////////
	//Title and Meta
	//////////////////////////////////
	
	$title = $language['account_dashboard_pers_title'];
	$description = $language['account_dashboard_pers_desc'];
	$words = $language['account_dashboard_pers_key'];
	$info_page = $language['account_dashboard_pers_info'];
		
	///////////////////////////////////
	//Display view
	//////////////////////////////////
	
	pool::execute('beforestart');
	
	$view = array();
	$view[] = 'template/tpl_form_acc_data.php';
	
	require_once __DIR__.'/template/tpl_layout.php';
}