<?
if (!defined('IN_BANDTOPIA'))
{
	exit;
}

$accountObj = new AccountObj();

if($action == 'accountList')
{
	$template -> set_filenames(array(
		'page' => MODULE_PATH.'admin/template/accountList.htm'));

	$accountList = $accountObj->AccountList();
	for($i = 0; $i < count($accountList); $i++)
	{
		$template -> assign_block_vars('account_list', array(
			'ACCOUNT_ID' => $accountList[$i]->AccountId,
			'ACCOUNT_NAME' => $accountList[$i]->AccountName,
			'CELL_ALT' => ($i%2 == 1)?'_alt':''
			));
	}
}
elseif ($action == 'accountAdmin')
{
	$template -> set_filenames(array(
		'page' => MODULE_PATH.'admin/template/accountAdmin.htm'));
	
	if ($_SERVER['REQUEST_METHOD'] == 'POST') {
		$accountObj->AccountId = $_POST['AccountId'];
		$accountObj->AccountTypeId = $_POST['AccountTypeId'];
		$accountObj->AccountName = $_POST['AccountName'];
		$accountObj->BillingName = $_POST['BillingName'];
		$accountObj->BillingAddr = $_POST['BillingAddr'];
		$accountObj->BillingCity = $_POST['BillingCity'];
		$accountObj->BillingSt = $_POST['BillingSt'];
		$accountObj->BillingZip = $_POST['BillingZip'];
		$accountObj->BillingPhone = $_POST['BillingPhone'];
		$accountObj->BillingEmail = $_POST['BillingEmail'];
		$accountObj->StatusId = $_POST['StatusId'];
		$accountObj->StatusDetail = $_POST['StatusDetail'];
		$accountObj->Notes = $_POST['Notes'];
		
		$accountMsg = $accountObj->UpdateAccount();
		if(strpos($accountMsg, 'error'))
		{
			$template -> assign_block_vars('page_error', array());
			$template -> assign_block_vars('page_error.error_msg', array(
				'ERROR_MSG' => $accountMsg
			));
		} else {
			$template -> assign_block_vars('page_msg', array());
			$template -> assign_block_vars('page_msg.msg', array(
				'MSG' => $accountMsg
			));
		}
		
		foreach ($accountObj as $key => $value)
		{
			$accountObj->$key = stripslashes($value);
		}

	}
	else
	{
		$accountObj->GetAccount((isset($_GET['AccountId']))?$_GET['AccountId']:0);
	}
	
	$template -> assign_vars(array(
		'ACCOUNT_ID' => $accountObj->AccountId,
		'ACCOUNT_NAME' => $accountObj->AccountName,
		'BILLING_NAME' => $accountObj->BillingName,
		'BILLING_ADDR' => $accountObj->BillingAddr,
		'BILLING_CITY' => $accountObj->BillingCity,
		'BILLING_ST' => $accountObj->BillingSt,
		'BILLING_ZIP' => $accountObj->BillingZip,
		'BILLING_PHONE' => $accountObj->BillingPhone,
		'BILLING_EMAIL' => $accountObj->BillingEmail,
		'STATUS_ID' => $accountObj->StatusId,
		'STATUS_DETAIL' => $accountObj->StatusDetail,
		'NOTES' => $accountObj->Notes,
		'DATE_ADDED' => date('m/d/y h:i a',$accountObj->DateAdded),
		'ADDED_BY_USERID' => $accountObj->AddedByUserId,
		'DATE_LAST_MODIFIED' => date('m/d/y h:i a', $accountObj->DateLastModified),
		'LAST_MODIFIED_BY_USERID' => $accountObj->LastModifiedByUserId
		));
	
	$accountTypeObj = new AccountTypeObj();
	$accountTypeList = $accountTypeObj -> AccountTypeList();
	for($i=0; $i < count($accountTypeList); $i++)
	{
		$template -> assign_block_vars('account_type', array(
			'ACCOUNT_TYPE_ID' => $accountTypeList[$i]->AccountTypeId,
			'ACCOUNT_TYPE_NAME' => $accountTypeList[$i]->AccountTypeName,
			'ACCOUNT_TYPE_SELECTED' => ($accountObj->AccountTypeId == $accountTypeList[$i]->AccountTypeId)?' SELECTED':''
			));
	}

	$addressObj = new AddressObj();
	$states_list = $addressObj -> getStates();
	foreach($states_list AS $key => $value)
	{
		$template -> assign_block_vars('state', array(
			'STATE_VALUE' => $key,
			'STATE_TEXT' => $value,
			'STATE_SELECTED' => ($key==$accountObj->BillingSt)?' SELECTED':''
		));
	}
	
	$statusObj = new StatusObj();
	$statusesList = $statusObj -> getStatuses();
	foreach($statusesList AS $status)
	{
		$template -> assign_block_vars('status', array(
			'STATUS_ID' => $status->StatusId,
			'STATUS_TITLE' => $status->StatusTitle,
			'STATUS_SELECTED' => ($status->StatusId==$accountObj->StatusId)?' SELECTED':''
		));
	}
	
	$userObj = new UserObj();
	$userObj->AccountId = $accountObj->AccountId;
	$userList = $userObj -> UserList();
	foreach($userList AS $user)
	{
		$template -> assign_block_vars('user', array(
			'USER_ID' => $user->UserId,
			'USERNAME' => $user->UserName,
			'FIRST_NAME' => $user->FirstName,
			'LAST_NAME' => $user->LastName,
			'ACCOUNT_ADMIN' => ($user->AccountAdmin)?'Y':'N',
			'CELL_ALT' => ($i%2 == 1)?'_alt':''
		));
	}
}

?>