$(document).ready(function(){
$("#calculator input.quantity").removeAttr("disabled");
$("#calculator, #calculator table, #calculator div").addClass("opaque");	
$("#calculator input.quantity").each( function() {
		$(this).keyup( function() {
			var quantity = $(this).val();
			var users = getUsersTotal();
			var index = $("#calculator input.quantity").index(this);
			var price = Number($("#calculator input.price:eq(" + index + ")").val());
			var threshold = Number($("#calculator input.threshold:eq(" + index + ")").val());
			var discount = Number($("#calculator input.discount:eq(" + index + ")").val());

			if ( (quantity != parseInt(quantity)) && ( quantity != "" ) )
			{
				alert("You can only enter whole numbers!");
				$(this).val("");
			}
			else
			{
				if ((threshold > 0) && (quantity > threshold))
				{
					var discountedPrice = price - (price * ((discount) / 100));
					var discountedTotal = (quantity * discountedPrice).toFixed(2);
					$("#calculator td.total div label:eq(" + index + ")").html('$<span class="total">' + discountedTotal + '</span>');
					$("#calculator td.total div small:eq(" + index + ")").html("You got " + discount + "% off!");
				}
				else
				{
					var total = (quantity * price).toFixed(2);
					$("#calculator td.total div label:eq(" + index + ")").html('$<span class="total">' + total + '</span>');
					$("#calculator td.total div small:eq(" + index + ")").html("");
				}
				
				var packagesTotal = getSubTotal("packages").toFixed(2);
				var addonsTotal = getSubTotal("addons").toFixed(2);
				
				if ( users < 10 )
				{
					packagesTotal = (Number(packagesTotal) + 150.00).toFixed(2);
					$("#calculator #packages label.note").html("<span>NOTE:</span> There is a $150 additional charge for less than 10 total users.");
				}
				else
				{
					$("#calculator #packages label.note").html("");
				}
				
				$("#calculator #packages span.packagesTotal").html("$" + packagesTotal);
				$("#calculator #addons span.addonsTotal").html("$" + addonsTotal);
				
				var grandTotal = (Number(packagesTotal) + Number(addonsTotal)).toFixed(2);
				
				$("#calculatorTotal span.grandTotal").html("$" + grandTotal);
				$("#calculatorTotal span").addClass("success");
			}
		});
	});
});

function getUsersTotal() {
	var total = 0;
	
	$("#calculator #packages input.quantity").each( function() {
		total += Number($(this).val());
	});
	
	return total;
}

function getSubTotal(type) {
	var total = 0;
	
	$("#calculator #" + type + " span.total").each( function() {
		total += Number($(this).html());
	});
	
	return total;
}

function validateEmail() {
	var email = $("#calculatorEmail input.email").val();
	var splitEmail = email.split("@", 2);
	var domain = splitEmail[1];
	var blockedDomains = ["gmail.com", "hotmail.com", "yahoo.com", "aol.com", "mail.com", "walla.com", "live.com", "fastmail.com"];
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	
	if ( (!(blockedDomains.Contains(domain))) && (reg.test(email)) )
	{
		$("#calculatorEmail").html("<h4>Thank You " + email + "</h4>");
		$("#calculatorEmail").height(30);
		$("#calculator input.quantity").removeAttr("disabled");
		$("#calculator, #calculator table, #calculator div").addClass("opaque");
		$("#calculatorTotal small").html("Note: The price provided is only an estimate. Does not include installation fees.");
		saveEmail(email);
	}
	else
	{
		if ( blockedDomains.Contains(domain) )
			$("#calculatorEmail h4").html('<span class="error">Error:</span> That e-mail is not allowed. Please use a business e-mail.');
		
		if ( !( reg.test(email) ) )
			$("#calculatorEmail h4").html('<span class="error">Error:</span> Please enter a valid email address.');
	}
}

Array.prototype.Contains = function(mxd,strict) {
	for(i in this) {
		if(this[i] == mxd && !strict) return true;
		else if(this[i] === mxd) return true;
	}
	return false;
}