function isValidEmail(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) return false;
   return true;
}

function urlencode(str) {
	 return escape(str).replace(/\+/g,'%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
}

$(function(){
	 $inputDesignTemplate = $('input[name="customer\[design_template\]"]');
	 $inputPackage = $('input[name="customer\[package\]"]');
	 $infoBox = $('#info-box');
	 $aPreview = $('#preview-giftcard');
	 $('.required:visible').after(' *');
	 $('.float-box').click(function(){
			$this = $(this);
			$('.float-box').addClass('unselected');
			$template = $this.removeClass('unselected').find('input[type=radio]');
			$template.click().attr('checked',true);
			$inputDesignTemplate.val($template.val());
			$aPreview.attr('href','download.file.php?width=620&height=450&preview-html&template='+$template.val());
	 })
   $('.package').click(function() {
      $this = $(this);
      $('.package').removeClass('selected'); //unselect style for all first
      $this.addClass('selected');
      $this.find('input[type=radio]').attr('checked',true);
      $inputPackage.attr('value',$this.attr('id').replace("package-",""));
			$.get("presentkort.coupons.php", {package : $this.attr('id').replace("package-","")},
			function(alldata) {
				 data = alldata.split("=======");
				 $("#DiscCodeOutBox").html(data[1]);
				 $("#TotAmountPayBox").html(data[2]);
			});
   });
	 $('.package input[type="radio"]').attr('checked',false);
	 $('#go-to-paynova').click(function(){
			$allowSubmit = true;
			$('form#offline_customer .required').each(function(){
				 $field = $(this);
				 if(!$field.val() || $field.val()=='' || ($field.attr('name') == 'customer[email]' && !isValidEmail($field.val())))
				 {
						$allowSubmit = false;
						$field.addClass('invalid-input');
				 }
				 else if($field.hasClass('invalid-input')) $field.removeClass('invalid-input');
			});
			if($allowSubmit) getPaymentForm('?p=checkout&paynova-form&package='+$inputPackage.val()+'&template='+$inputDesignTemplate.val()+'&customer='+$('#offline_customer').serialize());
			return false;
	 });
	 $('#DiscCodeButton').click(function(){
			if($('input[name="package"]:checked').size()==0)
			{
				 alert('Markera vilken typ av medlemskap du vill beställa!');
				 return false;
			};
			var tmp = $('#DiscCode').val();
			tmp = tmp.replace(/^\s+|\s+$/g, '');
			tmp = urlencode(tmp);
			$.get("presentkort.coupons.php", {dcode : tmp, package : $('input[name="package"]:checked').val()},
				 function(alldata) {
						if(alldata=="0")
						{
							 alert("ERROR!");
							 $("#DiscCode_ID").val('');
							 $("#DiscCodeOutBox").html('');
							 $("#TotAmountPayBox").html('0.00');
						}
						else
						{
							 data = alldata.split("=======");
							 $("#DiscCode_ID").val(data[0]);
							 $("#DiscCodeOutBox").html(data[1]);
							 $("#TotAmountPayBox").html(data[2]);
							 $('#DiscCode').val('');
						};
			});
			return true;
	 });
});
