<?php
if (isset($_POST['append_ajax']) && $_POST['append_ajax']){	
	$action = 'reload';
	$next = 1;
	if (isset($_POST['links_data'])) {
		foreach ($_POST['links_data'] AS $key => $value){
			$next++;
		}
	}
	print "
<div class='form-row'>
	<label for='links_link_".$next."' class='form-label'>".LABEL_LINK." ".$next."</label>
	<div class='form-cols'>
		<div class='form-col form-full'>
			<div class='form-controls'>
				<input type='text' class='field' id='links_link_".$next."' name='links_data[".$next."][link]' value=''>
			</div><!-- /.form-controls -->
		</div><!-- /.form-col -->
	</div><!-- /.form-cols -->
</div><!-- /.form-row -->

<div class='form-row'>
	<label for='links_linktext_".$next."' class='form-label'>".LABEL_LINKTEXT." ".$next."</label>
	<div class='form-cols'>
		<div class='form-col form-full'>
			<div class='form-controls'>
				<input type='text' class='field' id='links_linktext_".$next."' name='links_data[".$next."][linktext]' value=''>
			</div><!-- /.form-controls -->
		</div><!-- /.form-col -->
	</div><!-- /.form-cols -->
</div><!-- /.form-row -->

<hr/>";
} else if (isset($_POST['reload_ajax']) && $_POST['reload_ajax']){	
	$action = 'reload';
	$next = 1;
	if (isset($_POST['links_data'])) {
		foreach ($_POST['links_data'] AS $key => $value){
			$links_data[$next] = $value;
			$next++;
		}
	}
} else {
	print "
<div class='form ";
	if ($tab != 'links') print "hidden";
	print "' data-id='links'>
	<div id='box_links'>";
}
if (!isset($_POST['append_ajax']) OR !$_POST['append_ajax']){
	print "
		<div class='form-content'>";
	$next = 0;
	if (isset($links_data) && is_array($links_data)){
		foreach ($links_data AS $key => $value){
			print "
			<div class='form-row'>
				<label for='links_link_".$key."' class='form-label'>".LABEL_LINK." ".$key."</label>
				<div class='form-cols'>
					<div class='form-col form-full'>
						<div class='form-controls'>
							<input type='text' class='field' id='links_link_".$key."' name='links_data[".$key."][link]' value='".$value['link']."'>
						</div><!-- /.form-controls -->
					</div><!-- /.form-col -->
				</div><!-- /.form-cols -->
			</div><!-- /.form-row -->
	
			<div class='form-row'>
				<label for='links_linktext_".$key."' class='form-label'>".LABEL_LINKTEXT." ".$key."</label>
				<div class='form-cols'>
					<div class='form-col form-full'>
						<div class='form-controls'>
							<input type='text' class='field' id='links_linktext_".$key."' name='links_data[".$key."][linktext]' value='".$value['linktext']."'>
						</div><!-- /.form-controls -->
					</div><!-- /.form-col -->
				</div><!-- /.form-cols -->
			</div><!-- /.form-row -->
			
			<hr/>";
			$next = $key;
		}
	}
	$next++;
	print "
			<div class='form-row'>
				<label for='links_link_".$next."' class='form-label'>".LABEL_LINK." ".$next."</label>
				<div class='form-cols'>
					<div class='form-col form-full'>
						<div class='form-controls'>
							<input type='text' class='field' id='links_link_".$next."' name='links_data[".$next."][link]' value=''>
						</div><!-- /.form-controls -->
					</div><!-- /.form-col -->
				</div><!-- /.form-cols -->
			</div><!-- /.form-row -->
			
			<div class='form-row'>
				<label for='links_linktext_".$next."' class='form-label'>".LABEL_LINKTEXT." ".$next."</label>
				<div class='form-cols'>
					<div class='form-col form-full'>
						<div class='form-controls'>
							<input type='text' class='field' id='links_linktext_".$next."' name='links_data[".$next."][linktext]' value=''>
						</div><!-- /.form-controls -->
					</div><!-- /.form-col -->
				</div><!-- /.form-cols -->
			</div><!-- /.form-row -->
			
			<hr/>
			
			<div id='box_append_links'></div>
			
			<div class='form-row'>
				<span class='form-label'>".LABEL_OPTIONS_ADD."</span>
				<div class='form-cols'>
					<div class='form-col form-full'>
						<div class='form-controls'>
							<span class='form-label-after'><a href='#' class='append_ajax' data-file='links'><i class='material-icons md-24'>add</i></a></span>
						</div><!-- /.form-controls -->
					</div><!-- /.form-col -->
				</div><!-- /.form-cols -->
			</div><!-- /.form-row -->
			
			<div class='form-action'>
				<button type='submit' class='btn save' name='submit' title='".LABEL_SAVE."'><i class='material-icons md-24'>save_alt</i>".LABEL_SAVE."</button>
			</div><!-- /.form-action -->
			
		</div><!-- /.form-content -->";
}

if (!isset($_POST['reload_ajax']) OR !$_POST['reload_ajax']){
	print "
	</div>
</div>";
}