<div class="Block Moveable Panel PurchaseGiftCertificate" id="PurchaseGiftCertificate">
	<h2>%%GLOBAL_CertificateTitle%%</h2>
	<div class="BlockContent">
		<p>
			%%LNG_GiftCertificateEmailedAfterPurchase%%
		</p>

		<p class="ErrorMessage" style="display: %%GLOBAL_HideErrorMessage%%">
			%%GLOBAL_ErrorMessage%%
		</p>

		<form method="post" action="%%GLOBAL_ShopPath%%/giftcertificates.php?action=do_purchase" onsubmit="return CheckGiftCertificateForm()"  
			style="display: %%GLOBAL_HideGiftCertificateForm%%;" id="frmGiftCertificate">
			<input type="hidden" name="cartitemid" value="%%GLOBAL_CartItemId%%" />

			<div class="FormContainer HorizontalFormContainer">	

				<div class="form-group">
				    <label>%%LNG_GiftCertificateTo%%:</label>
				    <input type="text" name="to_name" id="to_name" value="%%GLOBAL_CertificateTo%%" class="form-control" />
				</div>

				<div class="form-group">
				    <label>%%LNG_GiftCertificateToEmail%%:</label>
				    <input type="text" name="to_email" id="to_email" value="%%GLOBAL_CertificateToEmail%%" class="form-control" />
				</div>

				<div class="form-group">
				    <label>%%LNG_GiftCertificateFrom%%:</label>
				    <input type="text" name="from_name" id="from_name" value="%%GLOBAL_CertificateFrom%%" class="form-control" />
				</div>

				<div class="form-group">
				    <label>%%LNG_GiftCertificateFromEmail%%:</label>
				    <input type="text" name="from_email" id="from_email" value="%%GLOBAL_CertificateFromEmail%%" class="form-control" />
				</div>

				<div class="form-group">
				    <label for="exampleFormControlTextarea1">%%LNG_GiftCertificateMessage%%: <small>(%%LNG_Optional%%)</small></label>
				    <textarea name="message" id="message" rows="3" cols="40" class="form-control">%%GLOBAL_CertificateMessage%%</textarea>
				    <small class="form-text text-muted"><span id="remaining">200</span> %%LNG_CharactersRemaining%%</small>
				</div>
				
				<div class="form-group">
					<label><span class="Required">*</span> %%LNG_GiftCertificateAmount%%:</label>
					<div id="AmountSelectVisible" style="display: %%GLOBAL_HideGiftCertificateAmountSelect%%">
					<select name="selected_amount" id="selected_amount">
						%%GLOBAL_GiftCertificateAmountSelect%%
					</select>
					</div>
					<div id="CustomAmountVisible" style="display: %%GLOBAL_HideGiftCertificateCustomAmount%%">
					<input name="certificate_amount" id="certificate_amount" class="form-control" style="width: 50%;" value="%%GLOBAL_CustomCertificateAmount%%" />
					<small>(%%GLOBAL_GiftCertificateRange%%)</small>
					</div>
				</div>
				<div class="form-group form-check" id="expiryInfo" style="display: %%GLOBAL_HideExpiryInfo%%">
				    <input type="checkbox" class="form-check-input" name="agree" id="agree" value="1" %%GLOBAL_AgreeChecked%% />
				    <label>%%GLOBAL_GiftCertificateTerms%%</label>
				</div>
				<div class="form-group form-check">
					<input type="checkbox"  class="form-check-input" name="agree2" id="agree2" value="1" %%GLOBAL_AgreeChecked%% />
				    
				    <label>%%LNG_GiftCertificateTerms2%%</label>
				</div>

				<div class="form-group form-check" id="themeSelect" style="display: %%GLOBAL_HideThemeSelect%%">
					<label>%%LNG_GiftCertificateTheme%%:</label>
							
					<div style="display: %%GLOBAL_HideThemeSelect%%" class="GiftCertificateThemeList">
						%%GLOBAL_GiftCertificateThemes%%
					</div>
				</div>
				<input type="button" class="btn btn-warning" value="%%LNG_Preview%%" onclick="PreviewGiftCertificate()" />
				<input type="submit" class="btn btn-primary" value="%%GLOBAL_SaveGiftCertificateButton%%" id="SaveCertificate" />
				
					

			<dl>
			</div>
		</form>
	</div>
</div>
<script type="text/javascript">
function ToggleCertificateAmount() {
	if($('#CustomAmountVisible label').css('display') != "none" && $('#AmountSelectVisible label').css('display') != "none") {
		if($('#custom_amount').attr('checked') == true) {
			$('#SelectAmount').hide();
			$('#CustomAmount').show();
		}
		else {
			$('#SelectAmount').show();
			$('#CustomAmount').hide();
		}
	}
}

function CheckGiftCertificateForm() {
	if($('#to_name').val() == "") {
		swal('%%LNG_EnterValidCertificateToName%%');
		$('#to_name').focus();
		$('#to_name').select();
		return false;
	}

	if($('#to_email').val() == "" || $('#to_email').val().indexOf('.') == -1 || $('#to_email').val().indexOf('@') == -1) {
		swal('%%LNG_EnterValidCertificateToEmail%%');
		$('#to_email').focus();
		$('#to_email').select();
		return false;
	}

	if($('#from_name').val() == "") {
		swal('%%LNG_EnterValidCertificateFromName%%');
		$('#from_name').focus();
		$('#from_name').select();
		return false;
	}

	if($('#from_email').val() == "" || $('#from_email').val().indexOf('.') == -1 || $('#from_email').val().indexOf('@') == -1) {
		swal('%%LNG_EnterValidCertificateFromEmail%%');
		$('#from_email').focus();
		$('#from_email').select();
		return false;
	}

	if($('#message').val().length > 200) {
		swal('%%LNG_GiftCertificateMessageTooLong%%');
		$('#message').focus();
		$('#message').select();
		return false;
	}

	if($('#CustomAmountVisible').css('display') == "none") {
		if($('#selected_amount').val() == '') {
			swal('%%LNG_SelectValidGiftCertificateAmount%%');
			$('#selected_amount').focus();
			return false;
		}
	}
	else if($('#AmountSelectVisible').css('display') == "none") {
		if($('#certificate_amount').val() == 0) {
			swal('%%LNG_EnterValidGiftCertificateAmount%%');
			$('#certificate_amount').focus();
			$('#certificate_amount').select();
			return false;
		}
		else if('%%GLOBAL_GiftCertificateMinimum%%' != 0 && %%GLOBAL_GiftCertificateMinimum%% > $('#certificate_amount').val()) {
			swal('%%LNG_EnterGiftCertificateValueBetween%%');
			$('#certificate_amount').focus();
			$('#certificate_amount').select();
			return false;
		}
		else if('%%GLOBAL_GiftCertificateMaximum%%' != 0 && parseFloat($('#certificate_amount').val()) > %%GLOBAL_GiftCertificateMaximum%%) {
			swal('%%LNG_EnterGiftCertificateValueBetween%%');
			$('#certificate_amount').focus();
			$('#certificate_amount').select();
			return false;
		}
	}
	else {
		swal('%%LNG_SelectValidGiftCertificateAmount%%');
		$('#selected_amount').focus();
		return false;
	}

	/*if($("#expiryInfo").css('display') != "none" && $('#agree').attr('checked') != true) {
		swal('%%LNG_PleaseAgreeGiftCertificateTerms%%');
		$('#agree').focus();
		$('#agree').select();
		return false;
	}

	if($('#agree2').attr('checked') != true) {
		swal('%%LNG_PleaseAgreeGiftCertificateTerms%%');
		$('#agree2').focus();
		$('#agree2').select();
		return false;
	}*/


	if($('#themeSelect').css('display') != "none" && !$('.themeCheck:checked').length) {
		swal('%%LNG_SelectValidGiftCertificateTheme%%');
		return false;
	}

	return true;
}

function UpdateMessageRemaining(event) {
	var remaining = 200 - $('#message').val().length;
	if(remaining >= 0) {
		$('#remaining').html(remaining);
	}
	else {
		$('#remaining').html('0');
		if(typeof(event) != "undefined") {
			if(event.keyCode != 8) {
				event.preventDefault();
				return false;
			}
		}
	}
}

function PreviewGiftCertificate() {
	if(CheckGiftCertificateForm() == false) {
		return false;
	}
	var l = (screen.availWidth/2)-580;
	var t = (screen.availHeight/2)-220;
	var win = window.open('%%GLOBAL_ShopPath%%/giftcertificates.php?action=preview&'+$('#frmGiftCertificate').serialize(), "imagePop", "toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width=580,height=220,top="+t+",left="+l);
	win.focus();
}

$(document).ready(function() {
	ToggleCertificateAmount();
	$('#message').keyup(function(e) {
		return UpdateMessageRemaining(e);
	});
	UpdateMessageRemaining();
});

</script>