
function formatCurrency(num) {
   num = num.toString().replace(/\$|\,/g,'');
   if(isNaN(num))
      num = "0";
   sign = (num == (num = Math.abs(num)));
   num = Math.floor(num*100+0.50000000001);
   cents = num%100;
   num = Math.floor(num/100).toString();
   if(cents<10)
      cents = "0" + cents;
   for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) {
      num = num.substring(0,num.length-(4*i+3)) + ',' + num.substring(num.length-(4*i+3));
   }
   console.log(cents);
   return (((sign)?'':'-') + '$' + num + '.' + cents);
}

$(function() {
   $('.pricing').keyup(function() {
      adults = $('#adults').val();
      children = $('#children').val();
      if (children <= 0) {
         children = 0;
      }
      discount = (adults * 3);
      if (children > adults) {
         total = (adults * 39.69) + ((children - adults) * 39.69) + (adults * 22.00);
      } else {
         total = (adults * 39.69) + (children * 22.00);
      }
      
      //Add $2.90 per person for parties of 8 or more
      if ((parseInt(adults) + parseInt(children)) >= 8) {
         total += (2.90 * (parseInt(adults) + parseInt(children)));
      }

      if (total < 0) {
         total = 0;
      }
      
      $('#discount').html('-$'+discount+'.00');
      $('#total').html(formatCurrency(total));
   });

	$('#name').blur(function() {
		var v = $(this).val();
		if (v.length > 0) {
			$('#name_status').hide().html('<img src="images/tick.png" width="16" height="16" alt="Ok" />').fadeIn(300);
		} else {
			$('#name_status').hide().html('<img src="images/cross.png" width="16" height="16" alt="Error" />').fadeIn(300);
		}
	});

	$('#address').blur(function() {
		var v = $(this).val();
		if (v.length > 0) {
			$('#address_status').hide().html('<img src="images/tick.png" width="16" height="16" alt="Ok" />').fadeIn(300);
		} else {
			$('#address_status').hide().html('<img src="images/cross.png" width="16" height="16" alt="Error" />').fadeIn(300);
		}
	});

	$('#email').blur(function() {
		var email = $(this).val();
		var pattern = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (email.match(pattern)) {
			$('#email_status').hide().html('<img src="images/tick.png" width="16" height="16" alt="Ok" />').fadeIn(300);
		} else {
			$('#email_status').hide().html('<img src="images/cross.png" width="16" height="16" alt="Error" />').fadeIn(300);
		}
	});

	$('#city').blur(function() {
		var v = $(this).val();
		if (v.length > 0) {
			$('#city_status').hide().html('<img src="images/tick.png" width="16" height="16" alt="Ok" />').fadeIn(300);
		} else {
			$('#city_status').hide().html('<img src="images/cross.png" width="16" height="16" alt="Error" />').fadeIn(300);
		}
	});

   $('#state').blur(function() {
		var v = $(this).val();
		if (v.length > 0) {
			$('#state_status').hide().html('<img src="images/tick.png" width="16" height="16" alt="Ok" />').fadeIn(300);
		} else {
			$('#state_status').hide().html('<img src="images/cross.png" width="16" height="16" alt="Error" />').fadeIn(300);
		}
	});

   $('#zip').blur(function() {
		var v = $(this).val();
		if (v.length > 0) {
			$('#zip_status').hide().html('<img src="images/tick.png" width="16" height="16" alt="Ok" />').fadeIn(300);
		} else {
			$('#zip_status').hide().html('<img src="images/cross.png" width="16" height="16" alt="Error" />').fadeIn(300);
		}
	});

	$('#phone').blur(function() {
		var v = $(this).val();
		if (v.length > 0) {
			$('#phone_status').hide().html('<img src="images/tick.png" width="16" height="16" alt="Ok" />').fadeIn(300);
		} else {
			$('#phone_status').hide().html('<img src="images/cross.png" width="16" height="16" alt="Error" />').fadeIn(300);
		}
	});

	$('#cc').blur(function() {
		var v = $(this).val();
		if (v.length > 0) {
			$('#cc_status').hide().html('<img src="images/tick.png" width="16" height="16" alt="Ok" />').fadeIn(300);
		} else {
			$('#cc_status').hide().html('<img src="images/cross.png" width="16" height="16" alt="Error" />').fadeIn(300);
		}
	});

	$('#cvc').blur(function() {
		var v = $(this).val();
		if (v.length > 0) {
			$('#cvc_status').hide().html('<img src="images/tick.png" width="16" height="16" alt="Ok" />').fadeIn(300);
		} else {
			$('#cvc_status').hide().html('<img src="images/cross.png" width="16" height="16" alt="Error" />').fadeIn(300);
		}
	});

   $('#submit').click(function() {
		var name = $('#name').val();
      var address = $('#address').val();
      var city = $('#city').val();
      var zip = $('#zip').val();
      var phone = $('#phone').val();
      var email = $('#email').val();
      var cc = $('#cc').val();
      var cvc = $('#cvc').val();
		errors = 0;
      if (name.length < 2) {
         $('#name_status').hide().html('<img src="images/cross.png" width="16" height="16" alt="Error" />').fadeIn(300);
         errors++;
      }
      if (address.length < 5) {
         $('#address_status').hide().html('<img src="images/cross.png" width="16" height="16" alt="Error" />').fadeIn(300);
         errors++;
      }
      if (city.length == 0) {
         $('#city_status').hide().html('<img src="images/cross.png" width="16" height="16" alt="Error" />').fadeIn(300);
         errors++;
      }
      if (zip.length < 5) {
         $('#zip_status').hide().html('<img src="images/cross.png" width="16" height="16" alt="Error" />').fadeIn(300);
         errors++;
      }
      if (phone.length < 7) {
         $('#phone_status').hide().html('<img src="images/cross.png" width="16" height="16" alt="Error" />').fadeIn(300);
         errors++;
      }
      if (email.length == 0) {
         $('#email_status').hide().html('<img src="images/cross.png" width="16" height="16" alt="Error" />').fadeIn(300);
         errors++;
      }
      if (cc.length < 10) {
         $('#cc_status').hide().html('<img src="images/cross.png" width="16" height="16" alt="Error" />').fadeIn(300);
         errors++;
      }
      if (cvc.length == 0) {
         $('#cvc_status').hide().html('<img src="images/cross.png" width="16" height="16" alt="Error" />').fadeIn(300);
         errors++;
      }
      if (errors > 0) {
         $('#form-valid').html('There were some problems found with the form, Please correct the fields marked with the <img src="images/cross.png" width="16" height="16" alt="Error" />').fadeIn(300);
         return false;
      } else {
         return true;
      }
	});
});

