{{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(){
		$("form#carriers-form").validate();
		$("form#carriers-form #carrier-name").rules("add", {required: true});
		$('form#carriers-form').submit(function( e ){
			var block = false;
			$('#carriers-form input.price').each(function(){
				if( ( $(this).val().length == 0 ) || ( parseFloat( $(this).val() ) != $(this).val() && $(this).val() != '-' ) ){
					$(this).addClass( 'validation-error' );
					block = true;
				} else{
					$(this).removeClass( 'validation-error' );
				}
			});
			if( block ){
				alert( lang_validate_correct_error );
				e.preventDefault();
			}
		});
		
		var weightLimits = {{count( $aCarrier.aPrices.aWeightLimits )}};
		var summaryLimits = {{count( $aCarrier.aPrices.aSummaryLimits )}};
		
		$('a[href="#addWeightLimit"]').click(function(e){
			e.preventDefault();
			weightLimits++;
			$('#carrier_price_table .addrow').before('<tr><th rowspan="2"><input type="text" name="aWeightLimits[]" class="price" /></tr><tr class="prices"><td colspan="2" rowspan="2"><input type="text" name="aPrices[]" class="price" /></td>' + ('<td colspan="2" rowspan="2"><input type="text" name="aPrices[]" class="price" /></td>'.repeat(summaryLimits)) + '</tr>');
		});
		
		$('a[href="#removeWeightLimit"]').click(function(e){
			e.preventDefault();
			if( weightLimits > 0 ){
				weightLimits--;
				$('#carrier_price_table .addrow').prev().remove();
				$('#carrier_price_table .addrow').prev().remove();
			}
		});
		
		$('a[href="#addSummaryLimit"]').click(function(e){
			e.preventDefault();
			summaryLimits++;
			$('#carrier_price_table .addcolumn').before('<th colspan="2"><input type="text" name="aSummaryLimits[]" class="price" /></th>');
			$('#carrier_price_table .prices').append('<td colspan="2" rowspan="2"><input type="text" name="aPrices[]" class="price" /></td>');
		});
		
		$('a[href="#removeSummaryLimit"]').click(function(e){
			e.preventDefault();
			if( summaryLimits > 0 ){
				summaryLimits--;
				$('#carrier_price_table .addcolumn').prev().remove();
				$('#carrier_price_table .prices').each(function(){
					$(this).find('td').last().remove();
				});
			}
		});
	});
</script>
<h1>{{$lang.Edit_carrier}}</h1>
<form action="?p={{$_GET.p}}&amp;iCarrier={{$aCarrier.iCarrier}}" method="post" id="carriers-form" name="form">
	<fieldset>
		<input type="hidden" name="iCarrier" value="{{$aCarrier.iCarrier}}" />
		<div class="block50">
			<table class="details">
				<tr>
					<th class="name">{{$lang.Name}}</th><td class="value"><input id="carrier-name" type="text" name="sName" value="{{$aCarrier.sName}}" size="30" maxlength="30" class="input" alt="simple" /></td><td class="help"></td>
				</tr><tr>
					<th class="name">{{$lang.Active}}</th><td class="value"><input type="checkbox" name="iStatus" {{if $aCarrier.iStatus}}checked="checked"{{/if}} value="1" /></td><td class="help"></td>
				</tr><tr>
					<th class="name">{{$lang.Vat}}</th><td class="value"><select name="iVat" id="vat">{{selectOptions aOptions=$aVats mOption=$aCarrier.iVat}}</select></td><td class="help"><img class="tTip info" src="templates/images/info.png"  title="{{$lang.help_carrier_vat}}" /></td>
				</tr><tr>
					<th class="name">{{$lang.Price}}</th><td class="help" colspan="2">{{$lang.Carrier_price}}</td>
				</tr><tr>
					<td class="value" colspan="3">
						<table id="carrier_price_table">
							<tr><th colspan="2" style="text-align: right">{{$lang.Summary}}</th>{{foreach from=$aCarrier.aPrices.aSummaryLimits item=fSummaryLimit}}<th colspan="2"><input type="text" name="aSummaryLimits[]" value="{{$fSummaryLimit}}" class="price" /></th>{{/foreach}}<th class="addcolumn"><a href="#addSummaryLimit">+</a>/<a href="#removeSummaryLimit">-</a></th></tr>
							<tr class="prices"><th>{{$lang.Weight}}</th><td colspan="2" rowspan="2"><input type="text" name="aPrices[]" value="{{$aCarrier.aPrices[0]['0.00']}}" class="price" /></td>{{foreach from=$aCarrier.aPrices.aSummaryLimits item=fSummaryLimit}}<td colspan="2" rowspan="2"><input type="text" name="aPrices[]" value="{{$aCarrier.aPrices[0][$fSummaryLimit]}}" class="price" /></td>{{/foreach}}</tr>
							{{foreach from=$aCarrier.aPrices.aWeightLimits item=fWeightLimit}}
								<tr><th rowspan="2"><input type="text" name="aWeightLimits[]" value="{{$fWeightLimit}}" class="price" /></tr>
								<tr class="prices">
									<td colspan="2" rowspan="2"><input type="text" name="aPrices[]" value="{{$aCarrier.aPrices[$fWeightLimit]['0.00']}}" class="price" /></td>
									{{foreach from=$aCarrier.aPrices.aSummaryLimits item=fSummaryLimit}}
										<td colspan="2" rowspan="2"><input type="text" name="aPrices[]" value="{{$aCarrier.aPrices[$fWeightLimit][$fSummaryLimit]}}" class="price" /></td>
									{{/foreach}}
								</tr>
							{{/foreach}}
							<tr class="addrow"><th><a href="#addWeightLimit">+</a>/<a href="#removeWeightLimit">-</a></th></tr>
						</table>
					</td>
				</tr><tr>
					<th class="name">{{$lang.OuterCarrier}}</th><td class="value"><select id="carrier-outer" name="iOuterCarrier"><option value="">{{$lang.Select}}</option>{{selectOptions aOptions=$config.aOuterCarriers mOption=$aCarrier.iOuterCarrier}}</select></td><td class="help"></td>
				</tr>
			</table>
			<table class="list">
				<thead>
					<tr>
						<td class="name">{{$lang.Name_status}}</td><td>{{$lang.Summary}}</td><td></td><td>{{$lang.Delivery_free}}</td>
					</tr>
				</thead>
				{{if count( $aCarrier.aPayments ) > 0}}
					<tbody>
						{{foreach from=$aCarrier.aPayments item=aPayment}}
							<tr>
								<td class="name left"><input type="checkbox" name="aPayments[{{$aPayment.iPayment}}]" {{if $aPayment.iStatus}}checked="checked"{{/if}} value="1" />{{$aPayment.sName}}</td><td class="value"><input class="input" type="text" name="aPaymentsPrices[{{$aPayment.iPayment}}]" value="{{$aPayment.sPrice}}"  size="30" /></td><td class="help">{{$lang.example}} -10;20;100%</td><td class="value"><input class="input" type="text" name="aPaymentsDeliveries[{{$aPayment.iPayment}}]" value="{{$aPayment.fFreeDelivery}}"  size="30" /></td>
							</tr>
						{{/foreach}}
					</tbody>
				{{/if}}
			</table>
		</div>
		<div class="block50" id="carrier-countries">
			<h2>{{$lang.Countries}} <img class="tTip info" src="templates/images/info.png"  title="{{$lang.Countries_help}}" /></h2>
			{{$iContinent = -1}}
			{{foreach from=$aCountries item=aCountry}}
				{{if $aCountry.iContinent != $iContinent}}
					{{$iContinent = $aCountry.iContinent}}
					{{if $iContinent == 0}}
						<div class="continent0"> {{$lang['continent_0']}}</div>
					{{else}}
						<div class="continent"><input type="checkbox" id="cont-{{$iContinent}}" /> {{$lang['continent_'|cat:$iContinent]}}</div>
					{{/if}}
				{{/if}}
				<label {{if $iContinent == 0}}style="display: block; border-top: 2px solid #FBDFDA; width: 100%; padding: 0 5px; margin: 0;"{{/if}}><input type="checkbox" class="cont-{{$iContinent}}" name="aCountries[]" value="{{$aCountry.iCountry}}" {{if in_array( $aCountry.iCountry, $aCarrier.aCountries )}}checked="checked"{{/if}} /> {{$aCountry.sCountry}}</label>
				{{if isset( $aCountry.aRegions )}}{{foreach from=$aCountry.aRegions item=sRegion}}
					<label><input type="checkbox" name="aCountriesRegions[{{$aCountry.iCountry}}][]" value="{{$sRegion@key}}" {{if in_array( $sRegion@key, $aCarrier.aRegions )}}checked="checked"{{/if}}/> {{$sRegion}}</label>
				{{/foreach}}{{/if}}
			{{/foreach}}
		</div>

		<div class="save float"><div class="save floatcontainer">
		<input type="submit" class="submit" value="&laquo; {{$lang.return}}" onclick="location.href = '?p=carriers-list'; return false;"> <input type="submit" class="submit" value="{{$lang.Save}} &raquo;" name="sOption" />
		</div></div>

	</fieldset>
</form>