<?php 
/**
 * Система управления сайтом HostCMS v. 5.xx
 * Copyright © 2005-2010 ООО "Хостмэйк" (Hostmake LLC), http://www.hostcms.ru
 *
 * Форма добаления/редактирования оформленных закзов.
 * 
 * Файл: /admin/shop/action/form_edit_add_coupon.php
 * 
 * @author Hostmake LLC
 * @version 5.x
 */
if (isset($id))
{
	$edit_order = $id;
}
else
{
	$edit_order = 0;
}

$datetime = & singleton('DateClass');

$form_params = array();

$form_params['title'] = $edit_order == 0
? $GLOBALS['MSG_shops']['order_add_form_title']
: $GLOBALS['MSG_shops']['order_edit_form_title'];

$form_params['form_attribs']['method'] = 'post';
$form_params['form_attribs']['action'] = '/admin/shop/shop.php';
$form_params['form_attribs']['name'] = 'EditAddOrder';

$admin_forms_fields = new admin_forms_fields();

$shop_shops_id = to_int($_REQUEST['shop_shops_id']);

$shop = & singleton('shop');

$shop_groups_parent_id = to_int($_REQUEST['shop_groups_parent_id']);

$shop_row = $shop->GetShop($shop_shops_id);

$path_array = $shop->GetShopPathArray($shop_groups_parent_id, false);

// Преобразуем идентификаторы групп в ссылки на группы.
$new_path_array = array();
$new_path_array[0]['name'] = $GLOBALS['MSG_shops']['header_admin__list_forms'];
$new_path_array[0]['link'] = $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', '', 54, 'load_data');
$new_path_array[0]['onclick'] = $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', '', 54, 'load_data');

$new_path_array[1]['name'] = $shop_row['shop_shops_name'];
$new_path_array[1]['link'] = $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', "&shop_groups_parent_id=0&shop_shops_id=$shop_shops_id", 65, 'load_data');
$new_path_array[1]['onclick'] = $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', "&shop_groups_parent_id=0&shop_shops_id=$shop_shops_id", 65, 'load_data');

if ($path_array)
{
	foreach ($path_array as $key => $value)
	{
		$new_path_array[] = array (
		'link' => $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', "&shop_groups_parent_id=$key&shop_shops_id=$shop_shops_id", 65, 'load_data'),
		'onclick' => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', "&shop_groups_parent_id=$key&shop_shops_id=$shop_shops_id", 65, 'load_data'),
		'name' => $value);
	}
}
$new_path_array[] = array("name" => $GLOBALS['MSG_shops']['shops_add_form_link_orders'],
"link" => $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', "&shop_shops_id=$shop_shops_id&shop_groups_parent_id=$shop_groups_parent_id", 75, 'load_data'),
"onclick" => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', "&shop_shops_id=$shop_shops_id&shop_groups_parent_id=$shop_groups_parent_id", 75, 'load_data')
);

$form_params['path_array'] = $new_path_array;

$admin_forms_fields->admin_forms_fields($form_params);

// Редактировние оформленного заказа
if ($edit_order)
{
	$type = 1;
	$result = $shop->GetOrder($edit_order);
	$shop_location_id = to_int($result['shop_location_id']);
	$shop_country_id = to_int($result['shop_country_id']);
	$shop_city_id = to_int($result['shop_city_id']);
	$shop_city_area_id = to_int($result['shop_city_area_id']);
	$shop_cond_of_delivery_id = to_int($result['shop_cond_of_delivery_id']);
	$shop_order_delivery_price = to_float($result['shop_order_delivery_price']);
	$site_users_id = to_int($result['site_users_id']);
	$shop_order_users_name = to_str($result['shop_order_users_name']);
	$shop_order_users_surname = to_str($result['shop_order_users_surname']);
	$shop_order_users_patronymic = to_str($result['shop_order_users_patronymic']);
	$shop_order_users_email = to_str($result['shop_order_users_email']);
	$shop_order_users_company = to_str($result['shop_order_users_company']);
	$shop_order_users_fax = to_str($result['shop_order_users_fax']);
	$shop_order_status_id = to_int($result['shop_order_status_id']);
	$shop_order_change_status_date_time =  $datetime->datetime_format(date($result['shop_order_change_status_datetime']));
	$shop_currency_id = to_int($result['shop_currency_id']);
	$shop_system_of_pay_id = to_int($result['shop_system_of_pay_id']);
	$shop_order_date_time =  $datetime->datetime_format(date($result['shop_order_date_time']));
	$shop_order_sum = $shop->Round($shop->GetOrderSum($edit_order));
	$shop_order_status_of_pay = to_int($result['shop_order_status_of_pay']);
	$shop_order_date_of_pay = $datetime->datetime_format(date($result['shop_order_date_of_pay']));
	$shop_order_address = to_str($result['shop_order_address']);
	$shop_order_index = $result['shop_order_index'];
	$shop_order_phone = to_str($result['shop_order_phone']);
	$shop_order_description = to_str($result['shop_order_description']);
	$shop_order_system_information = to_str($result['shop_order_system_information']);
	$shop_order_cancel = to_int($result['shop_order_cancel']);
	$users_id = to_int($result['users_id']);
	$order_cancel_status = to_int($result['shop_order_cancel']);
	$shop_order_account_number = to_str($result['shop_order_account_number']);
	$shop_order_sending_info = $result['shop_order_sending_info'];
	$shop_order_ip = $result['shop_order_ip'];
}
// Добавление оформленного заказа
else
{
	$shop_location_id = 0;
	$shop_country_id = 0;
	$shop_city_id = 0;
	$shop_city_area_id = 0;
	$shop_cond_of_delivery_id = 0;
	$shop_order_delivery_price = '';
	$site_users_id = 0;
	$shop_order_users_name = '';
	$shop_order_users_surname = '';
	$shop_order_users_patronymic = '';
	$shop_order_users_email = '';
	$shop_order_users_company = '';
	$shop_order_users_fax = '';
	// Статус заказа по умолчанию берется из магазина
	$shop_order_status_id = $shop_row['shop_order_status_id'];
	$shop_order_change_status_date_time = '00.00.0000 00:00:00';
	$shop_currency_id = 0;
	$shop_system_of_pay_id = 0;
	$shop_order_date_time = date("d.m.Y H:i:s");
	$shop_order_sum = '';
	$shop_order_status_of_pay = 0;
	$shop_order_date_of_pay = '00.00.0000 00:00:00';
	$shop_order_address = '';
	$shop_order_index = '';
	$shop_order_phone = '';
	$shop_order_description = '';
	$shop_order_system_information = '';
	$shop_order_cancel = 0;
	$users_id = false;
	$order_cancel_status = 0;
	$shop_order_account_number = '';
	$shop_order_sending_info = '';
	$shop_order_ip = $_SERVER['REMOTE_ADDR'];
}

// Параметры.
$tab_id = $admin_forms_fields->AddTab($GLOBALS['MSG_shops']['tab']);

// Номер оформленного заказа
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'shop_order_account_number';
$param['caption'] = $GLOBALS['MSG_shops']['show_order_number'];
$param['type'] = 0; // Поле ввода
$param['value'] = $shop_order_account_number;
$param['attributes']['class'] = 'large';
$param['format']['minlen']['value'] = 0;
$param['format']['maxlen']['value'] = 255;
$param['div_attributes']['style'] = 'float: left; margin-right: 10px; width: 150px;';
$admin_forms_fields->AddField($param);

// Дата и время когда был сделан заказ
$param = array();
$param['tab_id'] = $tab_id;
$param['html_id'] = 'order_date_time';
$param['name'] = 'order_date_time';
$param['caption'] = $GLOBALS['MSG_shops']['order_date_time'];
$param['type'] = 11; // Дата-время
$param['value'] = $shop_order_date_time;
$param['attributes']['class'] = 'calendar_field_large';
$param['div_attributes']['style'] = 'float: left; margin-right: 10px; width: 200px;';
$admin_forms_fields->AddField($param);

// Идентификатор пользователя (заказчика)
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'site_users_id';
$param['caption'] = $GLOBALS['MSG_shops']['add_site_users'];
$param['type'] = 0; // Поле ввода
$param['value'] = $site_users_id;
$param['format']['lib']['value'] = 'integer';
$param['format']['lib']['message'] = $GLOBALS['MSG_shops']['show_invalid_characters_integer'];
$param['format']['minlen']['value'] = 1;
$param['format']['maxlen']['value'] = 11;
$param['attributes']['class'] = 'large';
$param['div_attributes']['style'] = 'float: left; margin-right: 10px; width: 150px;';
$admin_forms_fields->AddField($param);

// Если указан ID пользователя и есть модуль "Пользователи сайта"
if (!empty($site_users_id) && class_exists('SiteUsers'))
{
	$SiteUsers = & singleton('SiteUsers');
	$site_user_row = $SiteUsers->GetSiteUser($site_users_id);

	if ($site_user_row)
	{
		ob_start();
		?><div class="large item_div item_div_as_is"" style="width: 150px"><img src="/admin/images/new_window.gif" /> <a href="<?php echo "/admin/Site_users/Site_users.php?admin_forms_id=30&operation=edit_user&check_0_{$site_user_row['site_users_id']}=1"?>" target="_blank"><?php echo $site_user_row['site_users_login']?></a></div><?php
		$site_users_link = ob_get_clean();

		// Ссылка на пользователя
		$param = array();
		$param['tab_id'] = $tab_id;
		$param['name'] = 'site_users_id_link';
		$param['caption'] = '&nbsp;';
		$param['type'] = 15; // Как есть
		$param['value'] = $site_users_link;
		$admin_forms_fields->AddField($param);
	}
}

// Разделитель.
$admin_forms_fields->AddSeparator($tab_id);

// Сумма заказа
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'order_sum';
$param['caption'] = $GLOBALS['MSG_shops']['cond_of_delivery_add_form_price_order'];
$param['attributes']['readonly'] = '';
$param['type'] = 0; // Поле ввода
$param['value'] = $shop_order_sum;
$param['attributes']['class'] = 'large';
$param['attributes']['style'] = 'width: 150px;';
$param['div_attributes']['style'] = 'float: left;';
$admin_forms_fields->AddField($param);

// Заполняем данными поле "Валюта"
$arr = array();
$arr[0] = ' ... ';
$row = $shop->GetAllCurrency();

while ($res = mysql_fetch_assoc($row))
{
	$arr[$res['shop_currency_id']] = $res['shop_currency_name'];
}

// Валюта, в которой будет оплачиваться заказ
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'currency_id';
$param['caption'] = $GLOBALS['MSG_shops']['order_currency'];
$param['type'] = 2; // Выпадающий список
$param['items'] = $arr;
$param['value'] = $shop_currency_id;
$param['attributes']['class'] = 'large';
$param['attributes']['style'] = 'width: 150px;';
$param['div_attributes']['style'] = 'float: left;';
$admin_forms_fields->AddField($param);

// Отобразим ссылку на товар
if ($edit_order)
{
	ob_start();
	?><div class="large item_div item_div_as_is" style="width: 250px"><img src="/admin/images/page.gif" /> <a href="<?php echo "/admin/shop/shop.php?admin_forms_id=76&operation=load_data&limit=0&shop_shops_id=$shop_shops_id&shop_groups_parent_id=$shop_groups_parent_id&view_order_id=$edit_order"?>" onclick="<?php echo "DoLoadAjax('/admin/shop/shop.php', '&shop_shops_id=$shop_shops_id&shop_groups_parent_id=$shop_groups_parent_id&view_order_id=$edit_order', 76, 'load_data', '', '', '', ''); return false;"?>">
	<?php echo $GLOBALS['MSG_shops']['order_items_link']?></a></div><?php
	$item_link = ob_get_clean();
	
	// Ссылка на товар
	$param = array();
	$param['tab_id'] = $tab_id;
	$param['name'] = 'item_link';
	$param['caption'] = '&nbsp;';
	$param['type'] = 15; // Как есть
	$param['value'] = $item_link;
	$admin_forms_fields->AddField($param);
}

// Разделитель.
$admin_forms_fields->AddSeparator($tab_id);

// Статус оплаты
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'order_status_of_pay';
$param['caption'] = $GLOBALS['MSG_shops']['order_status_of_pay'];
$param['type'] = 1; // Флажок
$param['value'] = $shop_order_status_of_pay;
$param['div_attributes']['style'] = 'float: left;';
$admin_forms_fields->AddField($param);

// Статус отмены заказа
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'order_cancel_status';
$param['caption'] = $GLOBALS['MSG_shops']['cancel_status'];
$param['type'] = 1; // Флажок
$param['value'] = $order_cancel_status;
$param['div_attributes']['style'] = 'float: left;';
$admin_forms_fields->AddField($param);

// Разделитель.
$admin_forms_fields->AddSeparator($tab_id);

// Дата и время когда был оплачен заказ
$param = array();
$param['tab_id'] = $tab_id;
$param['html_id'] = 'order_date_of_pay';
$param['name'] = 'order_date_of_pay';
$param['caption'] = $GLOBALS['MSG_shops']['order_date_of_pay'];
$param['type'] = 11; // Дата-время
$param['value'] = $shop_order_date_of_pay;
$param['div_attributes']['style'] = 'float: left;';
$admin_forms_fields->AddField($param);

// Заполняем поле "Платежная система"
$arr = array();
$arr[0] = ' ... ';
$row = $shop->GetAllSystemOfPay($shop_shops_id);

while ($res = mysql_fetch_assoc($row))
{
	$arr[$res['shop_system_of_pay_id']] = $res['shop_system_of_pay_name'];
}

// Платежная система
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'system_of_pay';
$param['caption'] = $GLOBALS['MSG_shops']['system_of_pay'];
$param['type'] = 2; // Выпадающий список
$param['items'] = $arr;
$param['value'] = $shop_system_of_pay_id;
$param['div_attributes']['style'] = 'float: left; width: 300px;';
$admin_forms_fields->AddField($param);

if ($edit_order)
{
	// ссылка на печать
	$param = array();
	$param['tab_id'] = $tab_id;
	$param['type'] = 15; // Как есть
	$param['caption'] = '&nbsp;';
	$param['value'] = '<div class="large item_div item_div_as_is" style="width: 100px"><img src="/admin/images/printer.gif" alt="'.$GLOBALS['MSG_shops']['print'].'" title="'.$GLOBALS['MSG_shops']['print'].'"/> <a href="/admin/shop/shop.php?print_order='.$edit_order.'" target="_blank" title="'.$GLOBALS['MSG_shops']['print'].'">'.$GLOBALS['MSG_shops']['print'].'</a></div>';
	$admin_forms_fields->AddField($param);
}
	
// Карточка заказа
if ($edit_order)
{
	// Формируем ссылку на "карточку заказа"
	ob_start();
	?><div class="large item_div item_div_as_is" style="width: 250px"><img src="/admin/images/new_window.gif" /> <a href="<?php echo "/admin/shop/shop.php?show_order_card=$edit_order"?>" target="_blank"><?php echo $GLOBALS['MSG_shops']['order_card']?></a></div><?php
	$order_card_link = ob_get_clean();
	
	$param = array();
	$param['tab_id'] = $tab_id;
	$param['name'] = 'order_card_link';
	$param['caption'] = '&nbsp;';
	$param['type'] = 15; // Как есть
	$param['value'] = $order_card_link;
	$admin_forms_fields->AddField($param);
}

// Разделитель.
$admin_forms_fields->AddSeparator($tab_id);

// Заполнение поля "Статус заказа"
$arr = array();
$arr[0] = ' ... ';
$row = $shop->GetAllOrderStatus();

while ($res = mysql_fetch_assoc($row))
{
	$arr[$res['shop_order_status_id']] = $res['shop_order_status_name'];
}

// Статус заказа
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'shop_order_status_id';
$param['caption'] = $GLOBALS['MSG_shops']['show_order_status'];
$param['type'] = 2; // Выпадающий список
$param['items'] = $arr;
$param['value'] = $shop_order_status_id;
$param['div_attributes']['style'] = 'float: left;';
$param['attributes']['onchange'] = 'ChangeOrderStatus();';
$admin_forms_fields->AddField($param);

// Дата изменеия статуса заказа
$param = array();
$param['tab_id'] = $tab_id;
$param['html_id'] = 'order_change_status_date_time_id';
$param['name'] = 'order_change_status_date_time';
$param['caption'] = $GLOBALS['MSG_shops']['show_order_change_status_date'];
$param['type'] = 11; // Дата-время
$param['value'] = $shop_order_change_status_date_time;
$param['div_attributes']['style'] = 'float: left;';
$admin_forms_fields->AddField($param);

// Разделитель.
//$admin_forms_fields->AddSeparator($tab_id);

// IP-адрес заказчика
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'shop_order_ip';
$param['caption'] = $GLOBALS['MSG_shops']['show_order_ip'];
$param['type'] = 0; // Текстовое поле
$param['attributes']['style'] = 'width: 150px;';
$param['format']['maxlen']['value'] = 15;
$param['value'] = $shop_order_ip;

$admin_forms_fields->AddField($param);

// Разделитель.
$admin_forms_fields->AddSeparator($tab_id);

// Заполняем форму "Типы доставок"
$arr = array();
$arr[0] = ' ... ';
$row = $shop->GetAllTypeOfDelivery($shop_shops_id);

while ($res = mysql_fetch_assoc($row))
{
	$arr[$res['shop_type_of_delivery_id']] = $res['shop_type_of_delivery_name'];
}

// Тип доставки явно не хранится в базе, поэтому извлечем его ID из условия доставки
$cond_of_delivery_row = $shop->GetCondOfDelivery($shop_cond_of_delivery_id);

// типы доставки
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'shop_type_of_delivery_id';
$param['caption'] = $GLOBALS['MSG_shops']['type_of_delivery'];
$param['type'] = 2; // Выпадающий список
$param['items'] = $arr;
$param['value'] = to_int($cond_of_delivery_row['shop_type_of_delivery_id']);
$param['attributes']['onchange'] = 'setCondition(this)';
$param['div_attributes']['style'] = 'float: left; margin-right: 10px; width: 300px;';
$admin_forms_fields->AddField($param);

// Разделитель.
//$admin_forms_fields->AddSeparator($tab_id);

// Заполняем форму "Условия доставки"
$arr = array();
$sysp = array();
$arr[0] = ' ... ';
$sysp['type_of_delivery_id'] = 0;
$row = $shop->GetAllCondOfDelivery();

// Условия доставки
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'shop_cond_of_delivery_id';
$param['caption'] = $GLOBALS['MSG_shops']['cond_of_delivery'];
$param['type'] = 2; // Выпадающий список
$param['value'] = $shop_cond_of_delivery_id;
$param['div_attributes']['style'] = 'float: left; margin-right: 10px; width: 300px;';
$type_of_delivery_data = $shop->GetAllTypeOfDelivery($shop_shops_id);

// Количество типов доставки
$count_of = mysql_num_rows($type_of_delivery_data);

$itog = '';

// Получаем условие доставки для каждого типа доставки
for ($i = 0; $i < $count_of; $i++)
{
	$row = mysql_fetch_assoc($type_of_delivery_data);

	$id = $row['shop_type_of_delivery_id'];

	// Получаем список условий доставки для текущего типа доставки
	$param_delivery['type_of_delivery_id'] = $id;

	$cond_of_delivery_data = $shop->GetAllCondOfDelivery($param_delivery);

	$count_fo = mysql_num_rows($cond_of_delivery_data);

	$extend = "";

	$atemp = array();
	
	// Дописываем информацию в скрипт:
	for ($j = 0; $j < $count_fo; $j++)
	{
		$row2 = mysql_fetch_assoc($cond_of_delivery_data);

		// Если тип доставки текущий - пишем в список, который выводится сразу при редактировании. Дальше этот список меняется
		if (to_int($cond_of_delivery_row['shop_type_of_delivery_id']) == $row2['shop_type_of_delivery_id'])
		{
			$param['items'][$row2['shop_cond_of_delivery_id']] = $row2['shop_cond_of_delivery_name'];
		}

		$atemp[] = "{value:\"{$row2['shop_cond_of_delivery_id']}\", text:\"{$row2['shop_cond_of_delivery_name']}\"}";
	}
	
	$itog .= "\n type_id[\"$id\"] = [" . implode(',', $atemp) . '];';
}

// Формируем JavaScript код для выборки элементов
$js_code = '<script>
var type_id = Object(); '.$itog.'

function setCondition(chooser)
{
    var contChooser = chooser.form.elements["shop_cond_of_delivery_id"];
    // обнуляем список
    contChooser.options.length = 0;
    // получаем value для массива type_id
    var choice = chooser.options[chooser.selectedIndex].value;
    var db = type_id[choice];
    // вставляем первый элемент
    contChooser.options[0] = new Option(" ... ", "", true, false);
    if (choice != "" && choice != 0)
    {
        for (var i = 0; i < db.length; i++)
        {
            contChooser.options[i + 1] = new Option(db[i].text, db[i].value);
        }
    }
}
</script>';

$admin_forms_fields->AddField($param);

// Разделитель.
$admin_forms_fields->AddSeparator($tab_id);

// Запоняем поле "Страна"
$arr = array();
$arr[0] = ' ... ';

$res = $shop->GetAllCountries();

if ($res)
{
	while ($row = mysql_fetch_assoc($res))
	{
		$arr[$row['shop_country_id']] = $row['shop_country_name'];
	}
}

if($edit_order)
{
	// Формируем ссылку "Пересчитать стоимость доставки"
	ob_start();
			
			?><div class="large"><img src="/admin/images/coins.gif" /> <a href="<?php echo $admin_forms_fields->GetHtmlCallTrigerSingleAction('recalc_delivery', 'check_0_' . $edit_order, 75, 0, 0);?>" onclick="<?php echo $admin_forms_fields->GetOnClickCallTrigerSingleAction('recalc_delivery', 'check_0_' . $edit_order, 75, 0, 0);?>"><?php echo $GLOBALS['MSG_shops']['recalc_order_delivery_sum']?></a></div><?php
	$recalc_order_delivery_sum = ob_get_clean();

	// Пересчитать стоимость доставки
	$param = array();
	$param['tab_id'] = $tab_id;
	$param['name'] = 'recalc_order_delivery_sum';
	$param['caption'] = '&nbsp;';
	$param['type'] = 15; // Как есть
	$param['value'] = $recalc_order_delivery_sum;
	$param['div_attributes']['style'] = 'float: left; margin-right: 10px; width: 450px;';
	$admin_forms_fields->AddField($param);

	// Разделитель.
	$admin_forms_fields->AddSeparator($tab_id);
}

// Контактные данные.
$tab2_id = $admin_forms_fields->AddTab($GLOBALS['MSG_shops']['tab2']);

// Страна
$param = array();
$param['tab_id'] = $tab2_id;
$param['name'] = 'country_id';
$param['caption'] = $GLOBALS['MSG_shops']['order_add_form_country'];
$param['type'] = 2; // Выпадающий список
$param['items'] = $arr;
$param['attributes']['onchange'] = "doSetLocation(this.options[this.selectedIndex].value)";
$param['value'] = $shop_country_id;
$param['html_id'] = 'country_id';
$param['div_attributes']['style'] = 'float: left; width: 200px;';
$admin_forms_fields->AddField($param);

// Разделитель.
//$admin_forms_fields->AddSeparator($tab_id);

// Заполняем поле "Область"
$arr = array();
$arr[0] = ' ... ';

// Расположения заполняем только тогда, когда страна указана
if ($shop_country_id)
{
	$res = $shop->GetAllLocation($shop_country_id);

	if ($res)
	{
		while ($row = mysql_fetch_assoc($res))
		{
			$arr[$row['shop_location_id']] = $row['shop_location_name'];
		}
	}
}

// Область
$param = array();
$param['tab_id'] = $tab2_id;
$param['name'] = 'location_id';
$param['caption'] = $GLOBALS['MSG_shops']['order_add_form_location'];
$param['type'] = 2; // Выпадающий список
$param['items'] = $arr;
$param['attributes']['onchange'] = "doSetCity(this.options[this.selectedIndex].value)";
$param['value'] = $shop_location_id;
$param['div_attributes']['style'] = 'float: left; width: 200px;';
$param['html_id'] = 'location_id';
$admin_forms_fields->AddField($param);

// Заполняем поле "Город"
$arr = array(' ... ');

// Города заполняем только тогда, когда расположение указано
if ($shop_location_id)
{
	$res = $shop->GetAllCity($shop_location_id);

	if ($res)
	{
		while ($row = mysql_fetch_assoc($res))
		{
			$arr[$row['shop_city_id']] = $row['shop_city_name'];
		}
	}
}

// Город
$param = array();
$param['tab_id'] = $tab2_id;
$param['name'] = 'city_id';
$param['caption'] = $GLOBALS['MSG_shops']['order_add_form_city'];
$param['type'] = 2; // Выпадающий список
$param['items'] = $arr;
$param['attributes']['onchange'] = "doSetCityArea(this.options[this.selectedIndex].value)";
$param['value'] = $shop_city_id;
$param['div_attributes']['style'] = 'float: left; width: 200px;';
$param['html_id'] = 'city_id';
$admin_forms_fields->AddField($param);

// Заполняем поле "Район"
$arr = array();
$arr[0] = ' ... ';

// Города заполняем только тогда, когда расположение указано
if ($shop_city_id)
{
	$res = $shop->GetAllCityArea($shop_city_id);

	if ($res)
	{
		while ($row = mysql_fetch_assoc($res))
		{
			$arr[$row['shop_city_area_id']] = $row['shop_city_area_name'];
		}
	}
}

// Район города
$param = array();
$param['tab_id'] = $tab2_id;
$param['name'] = 'city_area_id';
$param['caption'] = $GLOBALS['MSG_shops']['order_add_form_city_area'];
$param['type'] = 2; // Выпадающий список
$param['items'] = $arr;
$param['value'] = $shop_city_area_id;
$param['div_attributes']['style'] = 'float: left; width: 200px;';
$param['html_id'] = 'city_area_id';
$admin_forms_fields->AddField($param);

// Разделитель.
$admin_forms_fields->AddSeparator($tab2_id);

// Индекс
$param = array();
$param['tab_id'] = $tab2_id;
$param['name'] = 'order_index';
$param['caption'] = $GLOBALS['MSG_shops']['order_index'];
$param['type'] = 0; // Поле ввода
$param['value'] = $shop_order_index;
$param['format']['lib']['message'] = $GLOBALS['MSG_shops']['show_invalid_characters_integer'];
$param['format']['maxlen']['value'] = 11;
$param['div_attributes']['style'] = 'float: left; margin-right: 10px; width: 100px;';
$admin_forms_fields->AddField($param);

// Адрес
$param = array();
$param['tab_id'] = $tab2_id;
$param['name'] = 'order_address';
$param['caption'] = $GLOBALS['MSG_shops']['order_address'];
$param['type'] = 0; // Поле ввода
$param['value'] = $shop_order_address;
$param['format']['maxlen']['value'] = 255;
$param['div_attributes']['style'] = 'float: left; margin-right: 10px; width: 490px';
$admin_forms_fields->AddField($param);

// Разделитель.
$admin_forms_fields->AddSeparator($tab2_id);

// Фамилия заказчика
$param = array();
$param['tab_id'] = $tab2_id;
$param['name'] = 'shop_order_users_surname';
$param['caption'] = $GLOBALS['MSG_shops']['form_order_surname'];
$param['type'] = 0; // Поле ввода
$param['value'] = $shop_order_users_surname;
$param['format']['maxlen']['value'] = 255;
$param['div_attributes']['style'] = 'float: left; margin-right: 10px; width: 190px';
$admin_forms_fields->AddField($param);

// Имя заказчика
$param = array();
$param['tab_id'] = $tab2_id;
$param['name'] = 'shop_order_users_name';
$param['caption'] = $GLOBALS['MSG_shops']['form_order_name'];
$param['type'] = 0; // Поле ввода
$param['value'] = $shop_order_users_name;
$param['format']['maxlen']['value'] = 255;
$param['div_attributes']['style'] = 'float: left; margin-right: 10px; width: 190px';
$admin_forms_fields->AddField($param);

// Отчество заказчика
$param = array();
$param['tab_id'] = $tab2_id;
$param['name'] = 'shop_order_users_patronymic';
$param['caption'] = $GLOBALS['MSG_shops']['form_order_patronymic'];
$param['type'] = 0; // Поле ввода
$param['value'] = $shop_order_users_patronymic;
$param['format']['maxlen']['value'] = 255;
$param['div_attributes']['style'] = 'float: left; margin-right: 10px; width: 190px';
$admin_forms_fields->AddField($param);

// Разделитель.
//$admin_forms_fields->AddSeparator($tab_id);

// Компания
$param = array();
$param['tab_id'] = $tab2_id;
$param['name'] = 'shop_order_users_company';
$param['caption'] = $GLOBALS['MSG_shops']['form_order_company'];
$param['type'] = 0; // Поле ввода
$param['value'] = $shop_order_users_company;
$param['format']['maxlen']['value'] = 255;
$param['div_attributes']['style'] = 'float: left; margin-right: 10px; width: 190px';
$admin_forms_fields->AddField($param);

// Разделитель.
$admin_forms_fields->AddSeparator($tab2_id);

// Телефон
$param = array();
$param['tab_id'] = $tab2_id;
$param['name'] = 'order_phone';
$param['caption'] = $GLOBALS['MSG_shops']['order_phone'];
$param['type'] = 0; // Поле ввода
$param['value'] = $shop_order_phone;
$param['format']['maxlen']['value'] = 255;
$param['div_attributes']['style'] = 'float: left; margin-right: 10px; width: 190px';
$admin_forms_fields->AddField($param);

// Факс
$param = array();
$param['tab_id'] = $tab2_id;
$param['name'] = 'shop_order_users_fax';
$param['caption'] = $GLOBALS['MSG_shops']['form_order_fax'];
$param['type'] = 0; // Поле ввода
$param['value'] = $shop_order_users_fax;
$param['format']['maxlen']['value'] = 255;
$param['div_attributes']['style'] = 'float: left; margin-right: 10px; width: 190px';
$admin_forms_fields->AddField($param);

// E-mail заказчика
$param = array();
$param['tab_id'] = $tab2_id;
$param['name'] = 'shop_order_users_email';
$param['caption'] = $GLOBALS['MSG_shops']['form_order_email'];
$param['type'] = 0; // Поле ввода
$param['value'] = $shop_order_users_email;
$param['format']['lib']['value'] = 'email';
$param['div_attributes']['style'] = 'float: left; margin-right: 10px; width: 190px';
$admin_forms_fields->AddField($param);

// Разделитель.
$admin_forms_fields->AddSeparator($tab2_id);

// Параметры.
$tab3_id = $admin_forms_fields->AddTab($GLOBALS['MSG_shops']['tab3']);

// Описание заказа
$param = array();
$param['tab_id'] = $tab3_id;
$param['name'] = 'shop_order_description';
$param['caption'] = $GLOBALS['MSG_shops']['form_order_description'];
$param['type'] = 5; // Текстовое поле
$param['value'] = $shop_order_description;
$param['attributes']['rows'] = 7;

$admin_forms_fields->AddField($param);

// Разделитель.
$admin_forms_fields->AddSeparator($tab3_id);

// Информация о заказе
$param = array();
$param['tab_id'] = $tab3_id;
$param['name'] = 'shop_order_system_information';
$param['caption'] = $GLOBALS['MSG_shops']['form_order_system_information'];
$param['type'] = 5; // Текстовое поле
$param['value'] = $shop_order_system_information;
$param['attributes']['rows'] = 7;

$admin_forms_fields->AddField($param);

// Разделитель.
$admin_forms_fields->AddSeparator($tab3_id);

// Информация об отправлении
$param = array();
$param['tab_id'] = $tab3_id;
$param['name'] = 'shop_order_sending_info';
$param['caption'] = $GLOBALS['MSG_shops']['form_order_sending_data'];
$param['type'] = 5; // Текстовое поле
$param['value'] = $shop_order_sending_info;
$param['attributes']['rows'] = 7;

$admin_forms_fields->AddField($param);

// Разделитель.
$admin_forms_fields->AddSeparator($tab3_id);

// передеам скрытое поле с идентификатором адреса если он есть
if ($edit_order)
{
	$param = array();
	$param['tab_id'] = $tab_id;
	$param['name'] = 'edit_order';
	$param['caption'] = '';
	$param['type'] = 4; // hidden.
	$param['value'] = $edit_order;
	$admin_forms_fields->AddField($param);
}

$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'action';
$param['caption'] = '';
$param['type'] = 4; // hidden.
$param['value'] = 'show_order';
$admin_forms_fields->AddField($param);

$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'shop_shops_id';
$param['caption'] = '';
$param['type'] = 4; // hidden.
$param['value'] = $shop_shops_id;
$admin_forms_fields->AddField($param);

$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'shop_groups_parent_id';
$param['caption'] = '';
$param['type'] = 4; // hidden.
$param['value'] = $shop_groups_parent_id;
$admin_forms_fields->AddField($param);

// Кнопка "Сохранить".
$param = array();
$param['name'] = 'save_order';
$param['caption'] = $GLOBALS['MSG_shops']['save'];
$param['attributes']['onclick'] = "return SendForm('{$admin_forms_fields->GetWindowId()}', '{$admin_forms_fields->AAction}', '{$admin_forms_fields->AAdditionalParams}', this, ".to_int($_REQUEST['admin_forms_id']).", 'load_data', " . to_int($admin_forms_fields->form_params['current_page']) . ", " . to_int($admin_forms_fields->form_params['on_page']) . " )";
$param['attributes']['type'] = 'button';
$admin_forms_fields->AddButton($param);

// Кнопка "Применить".
$param = array();
$param['name'] = 'add_order';
$param['caption'] = $GLOBALS['MSG_shops']['apply'];
$param['image'] = '/admin/images/add.gif';
$param['attributes']['onclick'] = "return SendForm('{$admin_forms_fields->GetWindowId()}', '{$admin_forms_fields->AAction}', '{$admin_forms_fields->AAdditionalParams}', this, ".to_int($_REQUEST['admin_forms_id']).", 'load_data', " . to_int($admin_forms_fields->form_params['current_page']) . ", " . to_int($admin_forms_fields->form_params['on_page']) . ")";
$param['attributes']['type'] = 'submit';
$admin_forms_fields->AddButton($param);

ob_start();
?>
<script type="text/javascript">
location_select_id = "location_id";
city_select_id = "city_id";
cityarea_select_id = "city_area_id";
</script>
<?php

$js_code .= ob_get_clean();

$admin_forms_fields->SetExternalHtml($js_code);

// Отображение формы.
$admin_forms_fields->ShowForm();
?>