{{function name="selectOptions" aOptions=0 mOption=0}}
	{{foreach from=$aOptions item=sOption}}
		<option value="{{$sOption@key}}" {{if ( is_array( $mOption ) && in_array( $sOption@key, $mOption ) ) || ( $sOption@key == $mOption )}}selected="selected"{{/if}}>{{$sOption}}</option>
	{{/foreach}}
{{/function}}

<script type="text/javascript">
	$(document).ready(function(){
		{{if isset( $smarty.get.activeTabId )}}$('ul.tab-headers li a[href="#{{$smarty.get.activeTabId}}"]').trigger('click');{{/if}}
	});
</script>

<form action="?p={{$_GET.p}}&amp;iOrder={{$aOrder.iOrder}}" method="post" enctype="multipart/form-data" id="order-form" name="form">
	<fieldset class="tabbed">
		<input type="hidden" name="iOrder" value="{{$aOrder.iOrder}}" />
		<input type="hidden" name="fOrderSummary" value="{{$aOrder.fOrderSummary}}" />

		<ul class="tab-headers">
			<li><a href="#order-info">{{$lang.OrderInfo}}</a></li>
			<li><a href="#order-products">{{$lang.Products_and_payment}}</a></li>
			<li><a href="#order-email">{{$lang.Send_email}}</a></li>
			<li><a href="#order-advance">{{$lang.Advance_order}}</a></li>
		</ul>

		<h1>{{$lang.Orders_form}} - {{$aOrder.iOrder}}</h1>
		
		<div class="tab" id="order-info">
			<script type="text/javascript">
				$(document).ready(function(){
					var aRegions = {{$sRegions}};
					
					$('#oCountry').change(function(){
						$('#oRegion').html('<option value="">{{$lang.Choose_region}}</option>');
						var iCountry = $(this).val();
						$.each(aRegions, function( key, region ){
							if( region.iCountry == iCountry ){
								$('#oRegion').append('<option value="' + region.iRegion + '"'+ ( region.iRegion == {{intval( $aOrder.iRegion )}} ? ' selected="selected"' : '' ) +'>' + region.sRegion + '</option>');
							}
						});
						if( $('#oRegion').html() == '<option value="">{{$lang.Choose_region}}</option>'){
							$('#oRegionTR').hide();
						} else{
							$('#oRegionTR').show();
						}
					});
					$('#oCountry').trigger('change');
					
					$('#oPostalCountry').change(function(){
						$('#oPostalRegion').html('<option value="">{{$lang.Choose_region}}</option>');
						var iCountry = $(this).val();
						$.each(aRegions, function( key, region ){
							if( region.iCountry == iCountry ){
								$('#oPostalRegion').append('<option value="' + region.iRegion + '"'+ ( region.iRegion == {{intval( $aOrder.iPostalRegion )}} ? ' selected="selected"' : '' ) +'>' + region.sRegion + '</option>');
							}
						});
						if( $('#oPostalRegion').html() == '<option value="">{{$lang.Choose_region}}</option>'){
							$('#oPostalRegionTR').hide();
						} else{
							$('#oPostalRegionTR').show();
						}
					});
					$('#oPostalCountry').trigger('change');
				});
			</script>
			<div class="block30">
				<h2>{{$lang.OrderInfo}}</h2>
				<table class="details">
					<tbody>
						<tr><th class="name">{{$lang.Date}}</th><td class="value">{{$aOrder.sDate}}</td><td class="help"></td></tr>
						<tr><th class="name">{{$lang.Carrier}}</th><td class="value">{{$aOrder.sCarrierName}}</td><td class="help"></td></tr>
						<tr><th class="name">{{$lang.Carrier_info}}</th><td class="value">{{$aOrder.mCarrierInfo}}</td><td class="help"></td></tr>
						<tr><th class="name">{{$lang.Payment}}</th><td class="value">{{$aOrder.sPaymentName}}</td><td class="help"></td></tr>
						<tr><th class="name">{{$lang.Payment_channel}}</th><td class="value">{{$aOrder.sPaymentChannel}}</td><td class="help"></td></tr>
						<tr><th class="name">{{$lang.Invoice}}</th><td class="value"><select name="iInvoice">{{selectOptions aOptions=$aGeneralInfo.aInvoices mOption=$aOrder.iInvoice}}</select></td><td class="help"></td></tr>
						<tr><th class="name">{{$lang.Language}}</th><td class="value">{{$aOrder.sLanguage}}</td><td class="help"></td></tr>
						<tr><th class="name">IP</th><td class="value"><a href="http://www.ripe.net/perl/whois?form_type=simple&amp;full_query_string=&amp;searchtext={{$aOrder.sIp}}&amp;do_search=Search" target="_blank">{{$aOrder.sIp}}</a></td><td class="help"></td></tr>
						<tr><th colspan="3"><h2>{{$lang.OrderRealization}}</h2></th></tr>
						<tr><th class="name">{{$lang.Package_number}}</th><td class="value"><input name="sPackageNumber2" value="{{$aOrder.aPackageNumber.1}}" /></td><td class="help"></td></tr>
						<tr><th class="name">{{$lang.Carrier}}</th><td class="value"><select name="iPackageNumber1">{{selectOptions aOptions=$aGeneralInfo.aPackageCarriers mOption=$aOrder.aPackageNumber.0}}</select></td><td class="help"></td></tr>
						<tr><th class="name">{{$lang.InvoiceId}}</th><td class="value"><input type="text" value="{{$aOrder.sInvoiceId}}" name="sInvoiceId" class="input" /></td><td class="help"></td></tr>
						<tr><th class="name">{{$lang.Status}}</th><td class="value"><select name="iStatus">{{selectOptions aOptions=$aGeneralInfo.aStatuses mOption=$aOrder.iStatus}}</select> </td><td class="help"></td></tr>
						<tr>
							<th class="name">{{$lang.Status_change_history}}</th>
							<td class="value">
								{{if count( $aOrder.aStatusHistory ) > 0}}
									<ul id="status">
										{{foreach from=$aOrder.aStatusHistory item=aStatus}}
											<li><span class="date">{{$aStatus.sDate}}</span> - <span class="value">{{$aStatus.sStatus}}</span></li>
										{{/foreach}}
									</ul>
								{{/if}}
							</td>
							<td class="help"></td>
						</tr>
					</tbody>
				</table>
			</div>

			<div class="block30">
				<h2>{{$lang.ClientInfo}}</h2>
				<table class="details">
					<thead>
						<tr>
						</tr>
					</thead>
					<tbody>
						<tr><th class="name">{{$lang.First_name}}</th><td class="value"><input type="text" name="sFirstName" value="{{$aOrder.sFirstName}}" /></td><td class="help"></td></tr>
						<tr><th class="name">{{$lang.Last_name}}</th><td class="value"><input type="text" name="sLastName" value="{{$aOrder.sLastName}}" /></td><td class="help"></td></tr>
						<tr><th class="name">{{$lang.Pesel}}</th><td class="value"><input type="text" name="sPesel" value="{{$aOrder.sPesel}}" /></td><td class="help"></td></tr>
						<tr><th class="name">{{$lang.Street}}</th><td class="value"><input type="text" name="sStreet" value="{{$aOrder.sStreet}}" class="input" /></td><td class="help"></td></tr>
						<tr><th class="name">{{$lang.Zip_code}}</th><td class="value"><input type="text" name="sZipCode" value="{{$aOrder.sZipCode}}" class="input" /></td><td class="help"></td></tr>
						<tr><th class="name">{{$lang.City}}</th><td class="value"><input type="text" name="sCity" value="{{$aOrder.sCity}}" class="input" /></td><td class="help"></td></tr>
						<tr><th class="name">{{$lang.Country}}</th><td class="value"><select name="iCountry" id="oCountry">{{selectOptions aOptions=$aGeneralInfo.aCountries mOption=$aOrder.iCountry}}</select></td><td class="help"></td></tr>
						<tr id="oRegionTR"><th class="name"><label for="oRegion">{{$lang.Region}}</label></th><td class="value"><select name="iRegion" id="oRegion"></select></td><td class="help"></td></tr>
						<tr><th class="name">{{$lang.Telephone}}</th><td class="value"><input type="phone" name="sPhone" value="{{$aOrder.sPhone}}" class="input" /></td><td class="help"></td></tr>
						<tr><th class="name">{{$lang.Email}}</th><td class="value"><input type="text" name="sEmail" value="{{$aOrder.sEmail}}" class="input" /></td><td class="help"></td></tr>
						<th colspan="3"><h2>{{$lang.Company}}</h2></th>
						<tr><th class="name">{{$lang.Company}}</th><td class="value"><input type="text" name="sCompanyName" value="{{$aOrder.sCompanyName}}" class="input" /></td><td class="help"></td></tr>
						<tr><th class="name">{{$lang.Nip}}</th><td class="value"><input type="text" name="sNip" value="{{$aOrder.sNip}}" class="input" /></td><td class="help"></td></tr>
						<tr><th colspan="3"><h2>{{$lang.PostalData}}</h2></th></tr>
						<tr><th class="name">{{$lang.Name}}</th><td class="value"><input type="text" name="sPostalName" value="{{$aOrder.sPostalName}}" class="input" /></td><td class="help"></td></tr>
						<tr><th class="name">{{$lang.Street}}</th><td class="value"><input type="text" name="sPostalStreet" value="{{$aOrder.sPostalStreet}}" size="50" maxlength="40" class="input" /></td><td class="help"></td></tr>
						<tr><th class="name">{{$lang.Zip_code}}</th><td class="value"><input type="text" name="sPostalZipCode" value="{{$aOrder.sPostalZipCode}}" class="input" /></td><td class="help"></td></tr>
						<tr><th class="name">{{$lang.City}}</th><td class="value"><input type="text" name="sPostalCity" value="{{$aOrder.sPostalCity}}" class="input" /></td><td class="help"></td></tr>
						<tr><th class="name">{{$lang.Country}}</th><td class="value"><select name="iPostalCountry" id="oPostalCountry"><option value="0">{{$lang.Select}}</option>{{selectOptions aOptions=$aGeneralInfo.aCountries mOption=$aOrder.iPostalCountry}}</select></td><td class="help"></td></tr>
						<tr><th class="name">{{$lang.Telephone}}</th><td class="value"><input type="phone" name="sPostalPhone" value="{{$aOrder.sPostalPhone}}" class="input" /></td><td class="help"></td></tr>
						<tr id="oPostalRegionTR"><th class="name"><label for="oPostalRegion">{{$lang.Region}}</label></th><td class="value"><select name="iPostalRegion" id="oPostalRegion"></select></td></tr>
					</tbody>
				</table>
			</div>
			<div class="block30">
				<h2>{{$lang.Comments}}</h2>
				<table id="order" class="details">
					<tbody>
						<tr {{if $aOrder.sComment != ""}}class="highlight"{{/if}}><th class="name">{{$lang.Order_comment}}</th><td class="value"><textarea name="sComment" cols="60" rows="5" >{{$aOrder.sComment}}</textarea></td><td class="help"></td></tr>
						<tr {{if $aOrder.sCommentAdmin != ""}}class="highlight"{{/if}}><th class="name">{{$lang.Admin_comment}}</th><td class="value"><textarea name="sCommentAdmin" cols="60" rows="5">{{$aOrder.sCommentAdmin}}</textarea></td><td class="help"></td></tr>
						<tr><th colspan="3"><h2>{{$lang.Pack_as_gift}}</h2></th></tr>
						<tr {{if $aOrder.iGift}}class="highlight"{{/if}}><th class="name">{{$lang.Pack_as_gift}}</th><td class="value"><input type="checkbox" name="iGift" {{if $aOrder.iGift}}checked="checked"{{/if}} value="1" /></td><td class="help"></td></tr>
						<tr {{if $aOrder.iGift}}class="highlight"{{/if}}><th class="name">{{$lang.Gift_Text}}</th><td class="value"><textarea name="sGiftText" cols="60" rows="5">{{$aOrder.sGiftText}}</textarea></td><td class="help"></td></tr>
						<tr {{if $aOrder.iGift}}class="highlight"{{/if}}><th class="name">{{$lang.Show_Client}}</th><td class="value"><input type="checkbox" name="iShowClient" {{if $aOrder.iShowClient}}checked="checked"{{/if}} value="1" /></td><td class="help"></td></tr>
					</tbody>
				</table>
			</div>
		</div><!-- end of tab -->
		<div class="tab" id="order-products">
			<h2>{{$lang.Products_and_payment}}</h2>
			<table class="details">
				<tr>
					<td colspan="3" id="ordered-products">
						<!-- ordered products table start -->
						{{if count( $aOrder.aProducts.aProducts ) > 0}}
							<table class="list">
								<thead>
									<tr>
										<th class="id">{{$lang.Id}}</th>
										<th class="photo">{{$lang.Photo}}</th>
										<th class="name">{{$lang.Name}}</th>
										<th class="price">{{$lang.Price}} [{{$aGeneralInfo.currency_symbol}}]</th>
										<th class="quantity">{{$lang.Quantity}}</th>
										<th class="summary">{{$lang.Summary}} [{{$aGeneralInfo.currency_symbol}}]</th>
									</tr>
								</thead>
								<tfoot>
									<tr class="summary-product">
										<th colspan="5"><a id="new-order-item-button" href="#">{{$lang.Add_new_order_item}}</a> {{$lang.Order_summary}}:</th>
										<td id="summary-product-cost">{{$aOrder.aProducts.sProductsSummary}}</td>
									</tr>
									{{if $aOrder.aProducts.bDiscountPanelActive}}
										<tr class="summary-product">
											<th colspan="5" class="info">{{$lang.Discount_coupon_value}} ({{$aOrder.aProducts.sDiscountCode}})</th>
											<td>{{$aOrder.aProducts.sDiscountCouponValue}}</td>
										</tr>
										<tr class="summary-product">
											<th colspan="5" class="info">{{$lang.Summary_after_discount}}:</th>
											<td>{{$aOrder.aProducts.sProductsSummaryDiscount}}</td>
										</tr>
									{{/if}}
									<tr class="summary-delivery">
										<th colspan="5">{{$aOrder.aCarrier.sName}} (id: {{$aOrder.aCarrier.iCarrier}})</th>
										<td class="price"><input type="text" name="fCarrierPrice" value="{{$aOrder.aProducts.fCarrierPrice}}" readonly="readonly" /></td>
									</tr>
									<tr class="summary-delivery">
										<th colspan="5">{{$aOrder.aPayment.sName}} (id: {{$aOrder.aPayment.iPayment}})</th>
										<td class="price"><input type="text" name="sPaymentPrice" value="{{$aOrder.aProducts.sPaymentPrice}}" readonly="readonly" /></td>
									</tr>
									{{if $aOrder.iGift}}
										<tr class="summary-gift"><th colspan="5" class="info">{{$lang.Packing_as_gift}}:</th><td id="summary-gift-cost">{{$aOrder.aProducts.sGiftCost}}</td></tr>
									{{/if}}
									<tr class="summary-order">
										<th colspan="5" class="info">{{$lang.Summary_cost}}:</th>
										<td>{{$aOrder.aProducts.sOrderSummary}}</td>
									</tr>
								</tfoot>
								<tbody>
									{{foreach from=$aOrder.aProducts.aProducts item=aProduct}}
										<tr>
											<td class="id"><a href="{{$aProduct.sLinkName}}" target="_blank">{{$aProduct.iProduct}}</a> <a href="?p=products-form&amp;iProduct={{$aProduct.iProduct}}"><img src="{{$config.dir_templates}}admin/images/ico/edit.png" alt="[edit]" /></a></td>
											<td class="photo">{{if isset( $aProduct.aDataImage )}}<a href="{{$config.dir_files}}{{$aProduct.aDataImage.sFileName}}" class="photo"><img src="{{$config.dir_files}}30/{{$aProduct.aDataImage.sFileName}}" alt="(f)"/></a>{{/if}}</td>
											<td class="name"><input type="text" name="aProducts[{{$aProduct.iElement}}][sName]" value="{{$aProduct.sName}}" class="input" size="40" readonly="readonly" /></td>
											<td class="price"><input type="text" name="aProducts[{{$aProduct.iElement}}][fPrice]" value="{{$aProduct.fPrice}}" class="input" size="7" readonly="readonly" /></td>
											<td class="quantity"><input type="text" name="aProducts[{{$aProduct.iElement}}][iQuantity]" value="{{$aProduct.iQuantity}}" class="input" size="2" readonly="readonly" /></td>
											<td class="summary">{{$aProduct.sSummary}} {{if isset( $aProduct.bDiscount )}}<img class="tTip discount_{{if $aProduct.bDiscount}}true{{else}}false{{/if}}" title="{{if $aProduct.bDiscount}}{{$lang.Discount_affected}}{{else}}{{$lang.Discount_not_affected}}{{/if}}" src="{{$config.dir_templates}}images/discount.png" />{{/if}}</td>
										</tr>
										{{if is_array( $aProduct.aStores )}}
											{{foreach from=$aProduct.aStores item=aStore}}
												<tr class="store">
													<td colspan="4" class="store_head">{{$aStore.sName}}</td>
													<td class="quantity">{{$aStore.iQuantity}}</td>
													<td colspan="2"></td>
												</tr>
											{{/foreach}}
										{{/if}}
									{{/foreach}}
									<tr id="new-order-item" style="display: none;" >
										<td class="id"><input type="text" name="aNewProduct[%ItemId%][iProduct]" value="" class="input" id="newProductId" disabled="true" /></td>
										<td class="name" colspan="2"><input type="text" name="aNewProduct[%ItemId%][sName]" value="" class="input" /></td>
										<td class="price"><input type="text" name="aNewProduct[%ItemId%][fPrice]" value="" class="input" /></td>
										<td class="quantity"><input type="text" name="aNewProduct[%ItemId%][iQuantity]" value="" class="input" /></td>
										<td class="summary"></td>
										<td class="del"></td>
									</tr>
								</tbody>
							</table>
						{{/if}}
						<!-- ordered products table end -->
					</td>
				</tr>
			</table>
		</div>
		<div class="tab" id="order-email">
			<div class="block50">
				<h2>{{$lang.Send_email}}</h2>
				<table class="details">
					<tbody>
						<tr><th class="name">{{$lang.Email_title}}</th><td class="value"><input type="text" name="sSendEmailTitle" /></td><td class="help"></td></tr>
						<tr><th class="name">{{$lang.Email_content}}</th><td class="value"><textarea name="sSendEmailContent" cols="60" rows="5"></textarea></td><td class="help"></td></tr>
						<tr><th class="name">{{$lang.Attachments}}</th><td class="value"><input type="file" name="aAttachments[]" value="" class="finput" multiple="multiple" /></td><td class="help"></td>
					</tbody>
				</table>
			</div>
			<div class="block50">
				<h2>{{$lang.Email_history}}</h2>
				{{if !empty( $aOrder.aEmails )}}
					<table id="email_history">
						<thead>
						</thead>
						{{foreach from=$aOrder.aEmails item=aEmail}}
							<tbody>
								<tr>
									<th class="title">{{$aEmail.sTitle}}</th>
									<td rowspan="2" class="text">
										{{$aEmail.sText}}
										{{if !empty( $aEmail.aAttachments )}}
											<p>{{$lang.Attachments}}:</p>
											<ul>
												{{foreach from=$aEmail.aAttachments item=aAttachment}}
													<li><a href="upload/orders_emails/{{$aAttachment.iElement}}_{{$aAttachment.sFileName}}">{{$aAttachment.sFileName}}</a></li>
												{{/foreach}}
											</ul>
										{{/if}}
									</td>
									</tr>
								<tr><td class="date">{{date( 'Y-m-d H:i:s', $aEmail.iTime )}}</td></tr>
							</tbody>
						{{/foreach}}
					</table>
				{{/if}}
			</div>
		</div>
		
		<div class="tab" id="order-advance">
			<div class="block100">
				<script type="text/javascript">
				$(document).ready(function(){
					$( 'form#order-form' ).validate();
					$( 'form#order-form #order-advance-value' ).rules( "add", {pattern: '^[0-9]+(\.[0-9]{0,2})?%?$' });
					$( 'form#order-form #order-advance-payment' ).rules( "add", {required: function( element ){return $( '#order-advance-value' ).val().length > 0;} });
					
					$( 'form#order-form input#order-advance-value' ).on( 'change', function( e ){
						e.preventDefault();
						var sAdvance = $( this ).val();
						var sOrderSummary = '{{$aOrder.aProducts.sOrderSummary}}';
						if( sAdvance.length > 0 ){
							var fAdvance = sAdvance.replace( '%', '' );
							var fOrderSummary = '{{$aOrder.aProducts.sOrderSummary}}';
							if( sAdvance.match( /^[0-9]+(\.[0-9]{0,2})?%$/ ) ){
								fAdvance = fAdvance * fOrderSummary / 100;
							}
							fAdvance = parseFloat( fAdvance );
							fAdvance = fAdvance.toFixed( 2 );
							if( fAdvance < fOrderSummary ){
								$( 'form#order-form input#order-advance-amount' ).val( fAdvance );
							}else{
								alert( '{{$lang.Advance_order_too_high}}' );
							}
						}
					});
				});
				</script>
				<h2>{{$lang.Advance_order}}</h2>
				<table class="details">
					<tbody>
						{{*if isset( $aOrder.iOrder ) && $aOrder.iPaymentSystem == $const_PAID_ON_DELIVERY*}}
							<tr>
								<th class="name">{{$lang.Advance_order}}</th>
								<td class="value">
									<input id="order-advance-value" type="text" class="input short" name="sOrderAdvance" value="{{$aOrder.sOrderAdvance}}" {{if $aOrder.sOrderAdvance != ''}}readonly="readonly"{{/if}}/>
								</td>
								<td class="help"><img class="tTip info" src="templates/images/info.png"  title="{{$lang.help_orders_advance_format}}" /></td>
							</tr>
							<tr>
								<th class="name">{{$lang.Advance_order_summary}}</th>
								<td class="value">
									<input id="order-advance-amount" type="text" class="input short" name="fOrderAdvance" value="{{$aOrder.fOrderAdvance}}" readonly="readonly"/>
								</td>
								<td class="help"><img class="tTip info" src="templates/images/info.png"  title="{{$lang.help_orders_advance_amount}}" /></td>
							</tr>
							<tr>
								<th class="name">{{$lang.Advance_order_payment}}</th>
								<td class="value">
									<select id="order-advance-payment" name="iOrderAdvancePayment">
										<option value=""></option>
										{{foreach from = $aAdvanceOrderPayments item = aAdvanceOrderPayment}}
											<option value="{{$aAdvanceOrderPayment.iPayment}}" {{if $aAdvanceOrderPayment.iPayment == $aOrder.iOrderAdvancePayment}}selected="selected"{{/if}}>{{$aAdvanceOrderPayment.sName}}</option>
										{{/foreach}}
									</select>
								</td>
								<td class="help"><img class="tTip info" src="templates/images/info.png"  title="{{$lang.help_orders_advance_payment}}" /></td>
							</tr>
						{{*/if*}}
					</tbody>
				</table>
			</div>
		</div>
		
		<div class="save float">
			<div class="save floatcontainer"><input type="submit" class="submit" value="&laquo; {{$lang.return}}" onclick="location.href = '?p=orders-list'; return false;"> <input type="submit" class="submit" value="{{$lang.User}} &raquo;" onclick="location.href = '?p=users-form&amp;iUser={{$aOrder.iUser}}'; return false;" /> <input type="submit" class="submit" value="{{$lang.Save}}" name="sOption" /></div>
		</div>
	</fieldset>
</form>
