$(document).ready(init);
var readyToSubmit = false;
function init() {
	$("#mastertrackForm").submit(function() {
		if(readyToSubmit == false) {
			return false;
		}
	});
	$("input[name='submit']").click(function() {
		var safeToSubmit = checkFields("all");
		if(safeToSubmit == true) {
			emailResults();
		}
			return false;
	});
	$(".qty").change(calcSubTotals);
	calcSubTotals();
}
function calcSubTotals() {
	if(checkFields("prices")) {
		var allQtyInputs = $(".qty"),
			recalcDiscount = false,
			discountPrice = "895";
		$(allQtyInputs).each(function(i,thisInput) {
			var price = $(thisInput).attr("alt");
			var qty = $(thisInput).val();
			var output = $(thisInput).parents("tr:first").find(".subPrice");
			if($(thisInput).hasClass("discount")) {
				var allDiscountInputs = $(".qty.discount"),
					sessionsSelected = 0;
				$(allDiscountInputs).each(function(x,thisDiscountInput) {
					if($(thisDiscountInput).val() > 0) {
						sessionsSelected++;
					}
				});
				if(sessionsSelected > 1) {
					recalcDiscount = true;
					discountPrice = "797.50";
				} else {
					recalcDiscount = true;
					discountPrice = "895";
				}
			}
			if(output.length) {
				var subPrice = parseFloat(qty * price);
				output.attr("alt",price);
				output.val(subPrice.toFixed(2));
			}
		});
		if(recalcDiscount) {
			if(discountPrice == "797.50") {
				$("#discountMsg").fadeIn("fast");
			} else {
				$("#discountMsg").fadeOut("fast");
			}
			var allQtyDiscountInputs = $(".qty.discount");
			$(allQtyDiscountInputs).each(function(i,thisInput) {
				var price = discountPrice;
				var qty = $(thisInput).val();
				var output = $(thisInput).parents("tr:first").find(".subPrice");
				var subPrice = parseFloat(qty * price);
				output.attr("alt",price);
				output.val(subPrice.toFixed(2));
			});
		}
		var totalDue = calcTotal();
		$("#totalDue span").html(totalDue.toFixed(2));
	}
}
function checkFields(flag) {
	var allQtyInputs = $(".qty"),
		passedCalc = true,
		passedText = true,
		passedPw = true,
		hasPassed = false,
		errorMsg = "";
	if(flag == "all") {
		var allReqInputs = $(".required");
		$(allReqInputs).each(function(i,thisInput) {
			if($(thisInput).val() == "" || $(thisInput).val() == "undefined") {
				$(thisInput)
					.addClass("red");
					passedText = false;
			} else {
				$(thisInput)
					.removeClass("red");
			}
		});
		if($("#password").val() != $("#passwordVerify").val()) {
			passedPw = false;
		}
	}
	$(allQtyInputs).each(function(i,thisInput) {
		if(isNaN($(thisInput).val())) {
			$(thisInput)
				.val("0")
				.addClass("red");
					passedCalc = false;
		} else if($(thisInput).val() == "" || $(thisInput).val() == " ") {
			$(thisInput)
				.val("0")
		} else {
			$(thisInput)
				.removeClass("red");
		}
	});
	if(passedCalc && passedText && passedPw) {
		hasPassed = true;
	} else {
		if(!passedCalc) {
			errorMsg += "Please enter a valid number.\n";
		}
		if(!passedText) {
			errorMsg += "Please fill in all required fields.\n";
		}
		if(!passedPw) {
			errorMsg += "The entered passwords do not match. Please try again.\n";
		}
		alert(errorMsg);
	}
	return hasPassed;
}
function calcTotal() {
	var runningTotal = 0;
	$(".subPrice").each(function(i,e) {
		runningTotal += parseFloat($(e).val());
	});
	return runningTotal;
}
function emailResults() {
	$.ajax({
		url: "assets/php/mastertrackOnlineform.php",
		type: "post",
		data: $("#mastertrackForm").serialize(),
		success: function(results) {
results=results.replace(/\s/g,"");
			if(results == "") {
				prepForPaypal();
			} else {
				alert("There was an error processing your request. Please contact the administrator");
			}
		},
		error: function() {
			alert("There was an error processing your request.");
		}
		
	});
}
function prepForPaypal() {
	var allQtyInputs = $(".qty"),
		inputCounter = 1;
	$(allQtyInputs).each(function(i,thisInput) {
		if($(thisInput).val() > 0) {
			var itemName = $(thisInput).attr("name"),

				amount = $(thisInput).parents("tr:first").find(".subPrice").attr("alt"),
				quantity = $(thisInput).val();
			$(thisInput)
				.attr("disabled","disabled")
				
				.siblings("input[name='item_name']")
				.attr("name","item_name_" + inputCounter)
				.val(itemName)
				.removeAttr("disabled")
				
				.siblings("input[name='amount']")
				.attr("name","amount_" + inputCounter)
				.val(amount)
				.removeAttr("disabled")
				
				.siblings("input[name='quantity']")
				.attr("name","quantity_" + inputCounter)
				.val(quantity)
				.removeAttr("disabled");
			inputCounter++;
		}
	});
	readyToSubmit = true;
	$("#mastertrackForm")
		.trigger("submit");
}


