{extends file="layout.html"}
{block name=title}LMS: {$layout.pagetitle|striphtml}{/block}
{block name=module_content}
<!--// $Id$ //-->
{$restrictive_sensitive_data_access = ConfigHelper::checkConfig('customers.restrictive_sensitive_data_access')}
{$pin_tip = ConfigHelper::getConfig('customers.pin_restriction_description', ConfigHelper::getConfig('phpui.pin_restriction_description'))}
{if !empty($unsecure_pin_validity)}
	{$pin_tip = ConfigHelper::getConfig('customers.unsecure_pin_restriction_description', $pin_tip)}
{/if}
{if !$pin_tip}
	{if empty($unsecure_pin_validity)}
		{if $pin_min_size == $pin_max_size}
			{$pin_tip = trans("Enter \$a characters PIN code", $pin_min_size)}
		{else}
			{$pin_tip = trans("Enter \$a to \$b characters PIN code", $pin_min_size, $pin_max_size)}
		{/if}
	{else}
		{if $unsecure_pin_min_size == $unsecure_pin_max_size}
			{$pin_tip = trans("Enter \$a characters PIN code", $unsecure_pin_min_size)}
		{else}
			{$pin_tip = trans("Enter \$a to \$b characters PIN code", $unsecure_pin_min_size, $unsecure_pin_max_size)}
		{/if}
	{/if}
{/if}
<H1>{$layout.pagetitle}</H1>
<script type="text/javascript" src="js/location_box_expandable.js"></script>
<style>
	@media screen and (max-width: 470px) {
		.lms-ui-address-box tr {
			display: flex;
			flex-wrap: wrap;
		}

		.cell-flex  > div textarea {
			max-width: 190px;
		}

		.lms-ui-wysiwyg-editor textarea {
			max-width: 230px;
		}
	}

	@media screen and (min-width: 470px) {
		.lms-ui-address-box {
			max-width: 500px;
		}
	}

	#paytime-cell .scombobox {
		width: 11em;
	}

	.consent-label {
		cursor: pointer;
	}

	.identity-document-box {
		padding-left: 0 !important;
	}

	.identity-document-properties {
		display: flex;
		flex-direction: column;
		flex-wrap: wrap;
	}

	.identity-document-property {
		display: flex;
		flex-direction: column;
		flex-wrap: wrap;
	}

	.identity-document-property > * {
		margin-top: 0.25em;
		margin-bottom: 0.25em;
	}

	.identity-document-property > input[type] {
		margin-left: 0;
	}

	.identity-document-property > select {
		margin-left: 0;
	}

</style>

{include file="customer/customercompanyselectiondialog.html"}

<FORM name="customeradd" id="customeradd" method="POST" action="?m=customeradd">
<p style="display: none;">
<INPUT type="submit" class="hiddenbtn">
</p>
<table class="lmsbox">
	<THEAD>
	<TR>
		<TD class="lmsbox-panels">
			<div class="lmsbox-panel">
				<table>
					<colgroup>
						<col style="width: 1%;">
						<col style="width: 99%;">
					</colgroup>
					<tr>
						<td>
							<label for="customeradd[lastname]" class="nobr">
								<strong>{trans("Name/Surname")}</strong>
							</label>
						</td>
						<td>
							<div class="valign-middle">
								<input type="text" name="customeradd[lastname]" maxlength="128"
									value="{$customeradd.lastname|escape}" required
									{tip class="lms-ui-autogrow" trigger="lastname" bold=1}>
								{hint content="Enter customer last name or company name"}
							</div>
						</td>
					</tr>
					<tr>
						<td>
							<label for="customeradd[name]" class="nobr">
								<strong>{trans("First name")}</strong>
							</label>
						</td>
						<td>
							<div class="valign-middle">
								<input type="text" name="customeradd[name]" maxlength="128" value="{$customeradd.name|escape}"
									{tip class="lms-ui-autogrow" trigger="name" bold=1}>
								{hint content="Enter customer name"}
							</div>
						</td>
					</tr>
				</table>
			</div>
			<div class="lmsbox-panel">
				<table>
					<tr>
						<td>
							<label for="extid">
								<strong>{trans("External system identifier")}</strong>
							</label>
						</td>
						<td>
							<div class="valign-middle">
								<input type="hidden" name="customeradd[extids][0][serviceproviderid]" value="0">
								<input type="text" id="extid" class="lms-ui-autogrow" name="customeradd[extids][0][extid]" value="{$customeradd.extids[0].extid|escape}"
									{tip trigger="extid"}>
								{hint content="Enter customer identifier in external system (optional)"}
							</div>
						</td>
					</tr>
					<tr>
						<td>
							<label for="altname">
								<strong>{trans("Alternative name")}</strong>
							</label>
						</td>
						<td>
							<div class="valign-middle">
								<input type="text" id="altname" class="lms-ui-autogrow" name="customeradd[altname]" maxlength="128" value="{$customeradd.altname|escape}"}>
								{hint content="Customer Alternative name (brand, shortname, casual name)"}
							</div>
						</td>
					</tr>
				</table>
			</div>
		</TD>
	</TR>
	</THEAD>
	<TBODY>
	<TR>
		<TD class="lmsbox-panels">
			<div class="lmsbox-panel">
				<TABLE>
				<COLGROUP>
					<COL style="width: 1%;">
					<COL style="width: 99%;">
				</COLGROUP>
				{block name="customeradd-leftcolumn"}
				<TR>
					<TD>
						{if $customeradd.status}{icon name="customer-status-{$_CSTATUSES[$customeradd.status].alias}"}{else}{icon name="customer-status-connected"}{/if}
					</TD>
					<TD>
						<select size="1" name="customeradd[status]" id="customer-status" {tip trigger="status"}>
							{foreach $_CSTATUSES as $statusidx => $status}
								<option value="{$statusidx}"{if $customeradd.status == $statusidx} selected{/if}>{$status.singularlabel}</option>
							{/foreach}
						</select>
					</TD>
				</TR>
				<tr>
					<td>
						{icon name="origin" class="fa-fw"}
					</td>
					<td>
						<select size="1" name="customeradd[origin]" {tip trigger="origin"}>
							<option value="">{trans("<!origin>— unspecified —")}</option>
							{foreach Localisation::arraySort($_ORIGINS) as $originidx => $origin}
								<option value="{$originidx}"{if $customeradd.origin == $originidx} selected{/if}>{$origin}</option>
							{/foreach}
						</select>
					</td>
				</tr>
				<tr>
					<td>
						{icon name="tag"}
					</td>
					<td>
						<select name="customeradd[flags][]" {tip text="Select customer flags (optional)" class="lms-ui-multiselect"}
							multiple>
							{foreach Localisation::arraySort($_CUSTOMERFLAGS, 'label') as $cflag => $flag}
							<option value="{$cflag}"
								{if is_array($customeradd.flags) && in_array($cflag, $customeradd.flags)} selected{/if}
									title="{$flag.tip}">{$flag.label}
							</option>
							{/foreach}
						</select>
					</td>
				</tr>
				{if $divisions}
				<tr>
					<td>
						{icon name="division"}
					</td>
					<td>
						<select size="1" name="customeradd[divisionid]" {tip trigger="divisionid"}>
							{foreach $divisions as $item}
								{if !$item.status}
									<option value="{$item.id}"{if $customeradd.divisionid == $item.id} selected{/if}>{$item.label|escape}</option>
								{/if}
							{/foreach}
						</select>
					</td>
				</tr>
				{/if}
				{if $groups}
					<tr>
						<td>
							{icon name="cg"}
						</td>
						<td>
							<select size="1" name="customeradd[group][]" multiple data-placeholder="{trans("… select groups …")}"
								{tip text="Select group to assign to customer" trigger="group" class="lms-ui-multiselect"}
								required>
								{foreach $groups as $group}
									<option value="{$group.id}" {if !empty($customeradd.group) && in_array($group.id, $customeradd.group)} selected{/if}
										>{$group.name|escape} (#{$group.id})</option>
								{/foreach}
							</select>
						</td>
					</tr>
				{/if}
				<TR>
					<TD>
						{icon name="legal-personality"}
					</TD>
					<TD>
						<div class="valign-middle">
							<label>
								<input type="radio" name="customeradd[type]" value="{$smarty.const.CTYPES_PRIVATE}" id="private"
									{tip trigger="type"}
									{if $customeradd.type == $smarty.const.CTYPES_PRIVATE} checked{/if}>
								{$_CTYPES[$smarty.const.CTYPES_PRIVATE]}
							</label>
							<label>
								<input type="radio" name="customeradd[type]" value="{$smarty.const.CTYPES_COMPANY}" id="company"
									{tip trigger="type"}
									{if $customeradd.type == $smarty.const.CTYPES_COMPANY} checked{/if}>
								{$_CTYPES[$smarty.const.CTYPES_COMPANY]}
							</label>
							{hint content="Select legal personality type"}
						</div>
					</TD>
				</TR>
				<tr>
					<td class="valign-top">
						{icon name="user-id-number"}
					</td>
					<td class="cell-flex identity-document-box">
						<div>
							<table class="user-id-number">
								<colgroup>
									<col style="width: 1%;">
									<col style="width: 99%;">
								</colgroup>
								{if !ConfigHelper::checkPrivilege('hide_customer_sensitive_data', !$restrictive_sensitive_data_access)}
									<tr>
										<td colspan="2">
											<fieldset>
												<legend class="bold">
													{trans("Identity document")}
												</legend>
												<div class="identity-document-properties">
													<label class="identity-document-property">
														<span>{trans("Type")}</span>
														{identitytypes elemname="customeradd[ict]" selected=$customeradd.ict
															tip="" trigger="ict"}
													</label>
													<label class="identity-document-property">
														<span>{trans("Number")}</span>
														{if $customeradd.icnwarning}
															<input type="hidden" name="customeradd[icnwarning]" value="1">
														{/if}
														{if ConfigHelper::checkPrivilege('customer_sensitive_data_view', !$restrictive_sensitive_data_access)}
															{$icn = $customeradd.icn}
															{$disabled = false}
														{else}
															{$icn = preg_replace('/[^\s]/', '*', $customeradd.icn)}
															{$disabled = true}
														{/if}
														<input type="text" value="{$icn}" name="customeradd[icn]" size="30"
															class="customer-sensitive-data" data-type="icn"
															{tip trigger="icn"}
															{if $disabled} disabled{/if}>
													</label>
													<label class="identity-document-property">
														<span>{trans("Expires")}</span>
														<input type="text" value="{if $customeradd.icexpires > 0}{$customeradd.icexpires}{/if}"
															name="customeradd[icexpires]" size="10" id="icexpires"
															class="lms-ui-date lms-ui-date-unix"
															{tip trigger="icexpires"}
															{if $customeradd.icexpires == -1}disabled{/if}>
													</label>
													<label>
														<input type="checkbox" name="customeradd[icexpires]"
															id="icexpires-indefinitely"
															value="-1"{if $customeradd.icexpires == -1} checked{/if}
															{tip trigger="icexpires-indefinitely"}>
														{trans("indefinitely")}
													</label>
												</div>
											</fieldset>
										</td>
									</tr>
									<tr>
										<td>
											{trans("SSN")}
										</td>
										<td>
											{if ConfigHelper::checkPrivilege('customer_sensitive_data_view', !$restrictive_sensitive_data_access)}
												{$ssn = $customeradd.ssn}
												{$disabled = false}
											{else}
												{$ssn = preg_replace('/[^\s]/', '*', $customeradd.ssn)}
												{$disabled = true}
											{/if}
											<input type="text" name="customeradd[ssn]" value="{$ssn}" size="30"
												data-type="ssn"
												{tip trigger="ssn" class="customer-sensitive-data"}
												{if $disabled} disabled{/if}>
											{if $customeradd.ssnwarning}
												<input type="hidden" name="customeradd[ssnwarning]" value="1">
											{/if}
											{if $customeradd.ssnexistencewarning}
												<input type="hidden" name="customeradd[ssnexistencewarning]" value="1">
											{/if}
										</td>
									</tr>
								{/if}
								<tr>
									<td>{trans("TEN")}</td>
									<td>
										<input type="text" name="customeradd[ten]" value="{$customeradd.ten}" size="30"
												{tip trigger="ten"}>
										{if $customeradd.tenwarning}
											<input type="hidden" name="customeradd[tenwarning]" value="1">
										{/if}
										{if $customeradd.tenexistencewarning}
											<input type="hidden" name="customeradd[tenexistencewarning]" value="1">
										{/if}
										{button type="link" icon="division" class="gusapi-button"
											data_type=Utils::GUS_REGON_API_SEARCH_TYPE_TEN
											tip="Click here to autocomplete company data from GUS REGON database"}
									</td>
								</tr>
								<tr>
									<td>{trans("REGON")}</td>
									<td>
										<input type="text" value="{$customeradd.regon}" name="customeradd[regon]" size="30"
											{tip trigger="regon"}>
										{button type="link" icon="division" class="gusapi-button"
											data_type=Utils::GUS_REGON_API_SEARCH_TYPE_REGON
											tip="Click here to autocomplete company data from GUS REGON database"}
									</td>
								</tr>
							</table>
						</div>
					</td>
				</tr>
				<tr>
					<td class="valign-top">
						{icon name="user-id-number"}
					</td>
					<td class="cell-flex identity-document-box">
						<div>
							<fieldset>
								<legend class="bold">
									{trans("Business register")}
								</legend>
								<div class="identity-document-properties">
									<label class="identity-document-property">
										<span>
											{trans("Name")}
											{hint content="Enter National Court Register / Register of Business Entities Name (optional)"}
										</span>
										<textarea name="customeradd[rbename]" cols="45" rows="4"
											{tip trigger="rbename"}
											>{$customeradd.rbename|escape}</textarea>
									</label>
									<label class="identity-document-property">
										<span>
											{trans("Number")}
											{hint content="Enter National Court Register / Register of Business Entities Number (optional)"}
										</span>
										<span>
											<input type="text" value="{$customeradd.rbe}" name="customeradd[rbe]" size="30"
												{tip trigger="rbe"}>
											{button type="link" icon="division" class="gusapi-button"
												data_type=Utils::GUS_REGON_API_SEARCH_TYPE_RBE
												tip="Click here to autocomplete company data from GUS REGON database"}
										</span>
									</label>
								</div>
							</fieldset>
						</div>
					</td>
				</tr>
				{$addr_counter = 0}
				<tr>
					<td class="valign-top">{icon name="home" tip="billing address"}</td>
					<td>
						{$addr_found = 0}
						{foreach $customeradd.addresses as $v}
							{if $v.location_address_type == $smarty.const.BILLING_ADDRESS}
								{$v.prefix = "customeradd[addresses]["|cat:$addr_counter|cat:"]"}
								{$addr_counter = $addr_counter + 1}
								{$addr_found = 1}
								{$v.hide_name=true}
								{location_box_expandable data=$v}
							{/if}
						{/foreach}

						{* if billing address is not set *}
						{if $addr_found == 0}
							{$tmp.location_address_type = $smarty.const.BILLING_ADDRESS}
							{$tmp.location_state_name = $default_states[$smarty.const.BILLING_ADDRESS]}
							{$tmp.prefix = "customeradd[addresses]["|cat:$addr_counter|cat:"]"}
							{$tmp.location_street_name = ConfigHelper::getConfig('phpui.default_address')}
							{$tmp.location_zip = ConfigHelper::getConfig('phpui.default_zip')}
							{$tmp.location_city_name = ConfigHelper::getConfig('phpui.default_city')}
							{$tmp.location_country_id = ConfigHelper::getConfig('phpui.default_countryid')}
							{$tmp.show = true}
							{$addr_counter = $addr_counter + 1}

							{$tmp.hide_name=true}
							{location_box_expandable data=$tmp}
							{$tmp = array()}
						{/if}
					</td>
					<script type="text/javascript">
/*
						var formid = $('form[name="customeradd"]').get(0);
						new AutoSuggest(formid, $('input[name="customeradd[addresses][0][location_street_name]"]').get(0),
							'?m=customeradd&ajax=1&mode=street&what=', 0);
						new AutoSuggest(formid, $('input[name="customeradd[addresses][0][location_city_name]"]').get(0),
							'?m=customeradd&ajax=1&mode=city&what=', 0);
						new AutoSuggest(formid, $('input[name="customeradd[addresses][0][location_zip]"]').get(0),
							'?m=customeradd&ajax=1&mode=zip&what=', 0);
*/
					</script>
				</tr>

				<tr>
					<td class="valign-top">{icon name="message" tip="postal address"}</td>
					<td>
						{$addr_found = 0}
						{foreach $customeradd.addresses as $v}
							{if $v.location_address_type == $smarty.const.POSTAL_ADDRESS}
								{$v.prefix = "customeradd[addresses]["|cat:$addr_counter|cat:"]"}
								{$addr_counter = $addr_counter + 1}
								{$addr_found = 1}
								{location_box_expandable data=$v}
							{/if}
						{/foreach}

{*						 if postal address is not set*}
						{if $addr_found == 0}
							{$tmp.location_address_type = $smarty.const.POSTAL_ADDRESS}
							{$tmp.prefix = "customeradd[addresses]["|cat:$addr_counter|cat:"]"}
							{$tmp.location_state_name = $default_states[$smarty.const.POSTAL_ADDRESS]}
							{$addr_counter = $addr_counter + 1}
							{location_box_expandable data=$tmp}
						{/if}
					</td>
					<script type="text/javascript">
/*
						new AutoSuggest(formid, $('input[name="customeradd[addresses][1][location_street_name]"]').get(0),
							'?m=customeradd&ajax=1&mode=street&what=', 0);
						new AutoSuggest(formid, $('input[name="customeradd[addresses][1][location_city_name]"]').get(0),
							'?m=customeradd&ajax=1&mode=city&what=', 0);
						new AutoSuggest(formid, $('input[name="customeradd[addresses][1][location_zip]"]').get(0),
							'?m=customeradd&ajax=1&mode=zip&what=', 0);
*/
					</script>
				</tr>

				{foreach $customeradd.addresses as $v}
				{if $v.location_address_type != $smarty.const.BILLING_ADDRESS && $v.location_address_type != $smarty.const.POSTAL_ADDRESS }
				<tr>
					<td class="valign-top">
						{if $v.location_address_type == $smarty.const.DEFAULT_LOCATION_ADDRESS}
							{icon name="default-customer-location" class="location-box-image" tip="default location address"}
						{else}
							{icon name="customer-location" class="location-box-image" tip="location/recipient address"}
						{/if}
					</td>
					<td>
						{$v.prefix = "customeradd[addresses]["|cat:$addr_counter|cat:"]"}
						{$v.location_state_name = $default_states[$smarty.const.LOCATION_ADDRESS]}
						{$address_idx = $addr_counter}
						{$v.default_type = 1}
						{$v.delete_button = 1}
						{$v.billing_address_button = 1}
						{$v.post_address_button = 1}
						{$addr_counter = $addr_counter + 1}
						{location_box_expandable data=$v}
					</td>
					<script type="text/javascript">
/*
						new AutoSuggest(formid, $('input[name="customeradd[addresses][{$address_idx}][location_street_name]"]').get(0),
							'?m=customeradd&ajax=1&mode=street&what=', 0);
						new AutoSuggest(formid, $('input[name="customeradd[addresses][{$address_idx}][location_city_name]"]').get(0),
							'?m=customeradd&ajax=1&mode=city&what=', 0);
						new AutoSuggest(formid, $('input[name="customeradd[addresses][{$address_idx}][location_zip]"]').get(0),
							'?m=customeradd&ajax=1&mode=zip&what=', 0);
*/
					</script>
				</tr>
				{/if}
				{/foreach}

				<tr>
					<td></td>
					<td>
						{button class="lms-ui-location-add-button" icon="add" label="Add address" data_prefix="customeradd"}
					</td>
				</tr>

				{include file="customer/customercontacts.html" customerinfo=$customeradd formelemprefix="customeradd"}
				<tr>
					<td>
						{icon name="customer-pin-code"}
					</td>
					<td>
						<div class="valign-middle">
							<input type="text" required size="{$pin_max_size}"
								value="{if $customeradd.pin}{$customeradd.pin}{/if}" name="customeradd[pin]"
								{tip trigger="pin"}>
							{button type="link" icon="next" id="randomize" tip="Generate random PIN"}
							{hint text=$pin_tip}
						</div>
					</td>
				</tr>
				<TR>
					<TD class="valign-top">
						{icon name="paytype"}
					</TD>
					<TD>
						<TABLE>
							<TR>
								<TD>
									{trans("Deadline")}
								</TD>
								<TD id="paytime-cell">
									<div class="valign-middle">
										<select name="customeradd[paytime]" {if isset($customeradd.paytime)}data-value="{$customeradd.paytime}"{/if}
											{tip class="lms-ui-combobox" trigger="paytime"}>
											<option value="-1"{if !isset($customeradd.paytime) || $customeradd.paytime == -1} selected{/if}>— {trans("default")} —</option>
											<option value="0"{if isset($customeradd.paytime) && $customeradd.paytime == 0} selected{/if}>0</option>
											<option value="7"{if $customeradd.paytime == 7} selected{/if}>7</option>
											<option value="10"{if $customeradd.paytime == 10} selected{/if}>10</option>
											<option value="14"{if $customeradd.paytime == 14} selected{/if}>14</option>
											<option value="21"{if $customeradd.paytime == 21} selected{/if}>21</option>
											<option value="30"{if $customeradd.paytime == 30} selected{/if}>30</option>
											<option value="60"{if $customeradd.paytime == 60} selected{/if}>60</option>
											<option value="90"{if $customeradd.paytime == 90} selected{/if}>90</option>
										</select>
										{hint content="Select default deadline in days"}
									</div>
								</TD>
							</TR>
							<TR>
								<TD>
									{trans("Payment type")}
								</TD>
								<TD>
									<div class="valign-middle">
										{paytypes elemname="customeradd[paytype]" selected=$customeradd.paytype
											tip="Select default invoices payment type" trigger="paytype"}
										{hint content="Select default invoices payment type"}
									</div>
								</TD>
							</TR>
							<TR>
								<TD>
									{trans("Cutoff suspending")}
								</TD>
								<TD>
									<div class="valign-middle nobr">
										<label>
											<input type="checkbox" name="customeradd[cutoffstopindefinitely]"
												id="cutoffstopindefinitely" value="1"
												{if $customeradd.cutoffstopindefinitely} checked{/if}>
											{trans("indefinitely")}
										</label>
									</div>
									<div class="valign-middle nobr cutoffstop">
										<input type="text" name="customeradd[cutoffstop]" id="cutoffstop"
											{if $customeradd.cutoffstopindefinitely} style="display: none;"
											{elseif $customeradd.cutoffstop} value="{$customeradd.cutoffstop}"
											{/if}
											size="10"
											{tip class="lms-ui-date" trigger="cutoffstop"}>
										{day_selection elem="#cutoffstop" days="3,7,10,14"}
									</div>
									<div class="valign-middle nobr">
										{hint content="Select date of cutoff suspending (optional)"}
									</div>
								</TD>
							</TR>
						</TABLE>
					</TD>
				</TR>
				<TR>
					<TD class="valign-top">
						{icon name="consent"}
					</TD>
					<TD>
						{include file="customer/customerconsents.html" variable_prefix="customeradd" variables=$customeradd}
					</TD>
				</TR>
				{/block}
			</TABLE>
			</div>
			<div class="lmsbox-panel">
				<TABLE>
				<COLGROUP>
					<COL style="width: 1%;">
					<COL style="width: 99%;">
				</COLGROUP>
				{block name="customeradd-rightcolumn"}
				<TR>
					<TD>{icon name="optional-info"}</TD>
					<TD>
						<div class="valign-middle">
							<textarea rows="10" cols="60" name="customeradd[info]" class="lms-ui-wysiwyg-editor"
								{if isset($customeradd.wysiwyg.info)} data-wysiwyg="{$customeradd.wysiwyg.info}"{/if}
								>{$customeradd.info|default:""|escape}</textarea>
							{hint content="Enter additional information (optional)"}
						</div>
					</TD>
				</TR>
				<TR>
					<TD>{icon name="warning"}</TD>
					<TD>
						<div class="valign-middle">
							<textarea rows="10" cols="60" name="customeradd[message]" class="lms-ui-wysiwyg-editor"
								{if isset($customeradd.wysiwyg.message)} data-wysiwyg="{$customeradd.wysiwyg.message}"{/if}
								>{$customeradd.message|default:""|escape}</textarea>
							{hint content="Enter notice contents (optional)"}
						</div>
					</TD>
				</TR>
				<TR>
					<TD>{icon name="notes"}</TD>
					<TD>
						<div class="valign-middle">
							<textarea rows="10" cols="60" name="customeradd[notes]" class="lms-ui-wysiwyg-editor"
								{if isset($customeradd.wysiwyg.notes)} data-wysiwyg="{$customeradd.wysiwyg.notes}"{/if}
								>{$customeradd.notes|default:""|escape}</textarea>
							{hint content="Enter notes (optional)"}
						</div>
					</TD>
				</TR>
				<TR>
					<TD>{icon name="memo"}</TD>
					<TD>
						<div class="valign-middle">
							<textarea name="customeradd[documentmemo]" class="lms-ui-wysiwyg-editor"
								cols="60" rows="10"
								{if isset($customeradd.wysiwyg.documentmemo)} data-wysiwyg="{$customeradd.wysiwyg.documentmemo}"{/if}
								>{$customeradd.documentmemo|default:""|escape}</textarea>
							{hint content="Enter document memo (optional)"}
						</div>
					</TD>
				</TR>
				{/block}
			</TABLE>
			</div>
		</TD>
	</TR>
	<TR>
		<TD style="width: 100%;" class="lms-ui-box-buttons">
			{button type="submit" id="save-button" icon="save" label="Submit"}
			{button id="cancel-button" icon="cancel" label="Cancel" onclick="location.href = '?m=customerlist';"}
			<br>
			<label>
				<INPUT type="checkbox" name="customeradd[reuse]" id="customeradd_reuse" value="ON" {if $customeradd.reuse}checked{/if}>
				{trans("Display this form again, when this customer is saved")}
			</label>
		</TD>
	</TR>
	</TBODY>
</TABLE>
</FORM>
<script>

	$('[name="customeradd[type]"]').change(function() {
		$('.gusapi-button').toggle(!$('#private').prop('checked'));
	}).filter(':checked').change();

	function init_email_validators() {
		$('[name^="customeradd[emails]"][value="16"]').off('click').click(function() {
			var checked = $(this).prop('checked');
			if (!$('#chkconsent{$smarty.const.CCONSENT_INVOICENOTICE}').prop('checked') && checked) {
				alertDialog($t("Customer has not checked 'electronic invoice' consent and you are trying to mark this mail contact for invoice sending!"), this);
			}
		});
	}

	$(function() {
		$('form[name="customeradd"] [name="customeradd[lastname]"]').focus();

		var legal_person_required_properties = [];
		var natural_person_required_properties = [];

		{if !empty($legal_person_required_properties)}
			{foreach $legal_person_required_properties as $index => $property}
				legal_person_required_properties.push('{$index}');
			{/foreach}
		{/if}

		{if !empty($natural_person_required_properties)}
			{foreach $natural_person_required_properties as $index => $property}
				natural_person_required_properties.push('{$index}');
			{/foreach}
		{/if}

		var legal_person_required_property_validation_error = {if empty($legal_person_required_property_validation_error)}false{else}true{/if};
		var natural_person_required_property_validation_error = {if empty($natural_person_required_property_validation_error)}false{else}true{/if};

		var legal_person_required_property_validation_customer_statuses = [];
		var natural_person_required_property_validation_customer_statuses = [];

		{if !empty($legal_person_required_property_validation_customer_statuses)}
			{foreach $legal_person_required_property_validation_customer_statuses as $index => $status}
				legal_person_required_property_validation_customer_statuses.push('{$status}');
			{/foreach}
		{/if}

		{if !empty($natural_person_required_property_validation_customer_statuses)}
			{foreach $natural_person_required_property_validation_customer_statuses as $index => $status}
				natural_person_required_property_validation_customer_statuses.push('{$status}');
			{/foreach}
		{/if}

		$('[name="customeradd[pin]"]').change(function() {
			{if empty($unsecure_pin_validity)}
				if (validate_random_string(this.value, {$pin_min_size}, {$pin_max_size}, '{str_replace("'", "\'", $pin_allowed_characters)}')) {
					this.setCustomValidity('');
				} else {
					this.setCustomValidity('{$pin_tip}');
				}
			{else}
				if (validate_random_string(this.value, {$unsecure_pin_min_size}, {$unsecure_pin_max_size}, '{str_replace("'", "\'", $unsecure_pin_allowed_characters)}')) {
					this.setCustomValidity('');
				} else {
					this.setCustomValidity('{$pin_tip}');
				}
			{/if}
		});

		init_email_validators();

		$('.contacts[data-type="email"] .newcontact a').click(function() {
			init_email_validators();
		});

		$('.gusapi-button').click(function() {
			var identifier = $(this).siblings('input[type="text"]').val().replace(/-/g, '');
			if (!identifier.length) {
				return;
			}
			var that = this;
			GusApiGetCompanyDetails(
				$(this).attr('data-type'),
				identifier,
				function(data) {
					if (data.length > 1) {
						customerCompanySelectionDialog(data, that).done(function(company) {
							GusApiFinished('customeradd', company);
						});
					} else {
						GusApiFinished('customeradd', data[0]);
					}
				});
			return;
		});

		$('[name="customeradd[type]"]').change(function() {
			var isPrivate = $('#private').prop('checked');
			var status = $('#customer-status').val();

			$('[name="customeradd[name]"]').prop('required', isPrivate);

			var required_properties = isPrivate ? natural_person_required_properties : legal_person_required_properties;
			var required_property_validation_error = isPrivate ? natural_person_required_property_validation_error : legal_person_required_property_validation_error;
			var required_property_validation_customer_statuses = status ? natural_person_required_property_validation_customer_statuses : legal_person_required_property_validation_customer_statuses;
			$.each(['ten', 'ssn', 'icn'], function(index, property) {
				$('[name="customeradd[' + property + ']"]').prop(
					'required',
					required_properties.indexOf(property) !== -1
						&& required_property_validation_error
						&& required_property_validation_customer_statuses.indexOf(status) !== -1
				);
			});
		}).change();

		$('#randomize').click(function() {
			$('[name="customeradd[pin]"]').val(
				{if empty($unsecure_pin_validity)}
					generate_random_string({$pin_min_size} + Math.floor(Math.random() * {($pin_max_size - $pin_min_size) + 1}), '{str_replace("'", "\'", $pin_allowed_characters)}')
				{else}
					generate_random_string({$pin_min_size} + Math.floor(Math.random() * {($unsecure_pin_max_size - $unsecure_pin_min_size) + 1}), '{str_replace("'", "\'", $unsecure_pin_allowed_characters)}')
				{/if}
			);
		});

		$('#icexpires-indefinitely').change(function() {
			var checked = $(this).prop('checked');
			$('#icexpires').datepicker("option", "disabled", checked).closest('.lms-ui-date-container').prev().prop('disabled', checked);
		}).change();

		if (!$('[name="customeradd[pin]"]').val().length) {
			$('#randomize').click();
		}

		$('#check-all-consents').change(function() {
			$(this).closest('.lms-ui-multi-check').find(':radio').prop('checked', $(this).prop('checked'));
		});

		$('#cutoffstopindefinitely').change(function() {
			$('.cutoffstop').toggle(!this.checked);
		});

		$("#reset-consents").click(function() {
			$("#customer-consent-table").find("[data-default-checked]").each(function() {
				if ($(this).is(":checkbox")) {
					$(this).prop("checked", $(this).attr("data-default-checked") == "1");
				} else {
					$(this).prop("checked", $(this).attr("data-default-checked") == $(this).val());
				}
			});
		});
	});

</script>
{/block}
