// JavaScript Document
$(document).ready(function(e) {
// SEND / PROCESS CONTACT FORM
var error = 0;
var specialCont = [];
var theText;

function confirmSelection(){
	var selection = $("#special").val();	
	if(selection == "0"){
		alert("Please select a special.");
		$("#special").focus();
		return false;	
	} else {
		return true;
	}
}
	
function updateCart() {
	$.ajax({
		url:"includes/cartContents.php",
		type:"POST",
		data:"fetch=yes",
		success:function(cart){
			$("#cartContents").text(cart);	
		}
	})
}

function addCheckoutButton(){
	$("#MixMatchToCart").fadeIn('fast');	
}

function updateSpecial(){
	var checkCount = $("#ItemsLeft").text();
	if(checkCount > 0) {
		$("#MMItemList h3").text("");
		$.each(specialCont,function(i,v){
			$("#MMItemList h3").append("<p>"+v.name+"</p>");
		})
		var currentNum = $("#ItemsLeft").text();
		var newCount = currentNum - 1;
		$("#ItemsLeft").text(newCount);
		if(newCount == 0) addCheckoutButton();
	}
}

$("#special").change(function(e) {
    var spCnt = $("#special").val();
	$("#ItemsLeft").text(spCnt);
});

$("#RHC").click(function(e) {
	if(confirmSelection() == true){
	theText = "Rough Hand Cream";
	specialCont.push({'name':theText}); 
	updateSpecial();
	}
});

$("#LVR").click(function(e) {
	if(confirmSelection() == true){
	theText = "Vanilla-Rose Lotion";
	specialCont.push({'name':theText}); 
	updateSpecial();
	}
});

$("#LP").click(function(e) {
	if(confirmSelection() == true){
	theText = "Plumeria Lotion";
	specialCont.push({'name':theText}); 
	updateSpecial();
	}
});

$("#LCW").click(function(e) {
	if(confirmSelection() == true){
	theText = "Calming Waters Lotion";
	specialCont.push({'name':theText}); 
	updateSpecial();
	}
});

$("#LLG").click(function(e) {
	if(confirmSelection() == true){
	theText = "Lemon-Grass Sage Lotion";
	specialCont.push({'name':theText}); 
	updateSpecial();
	}
});

updateCart();

var prodIMG = $("#productImage");
var newIMG;

$("#top").click(function(e) {
	newIMG = "images/oil_top.jpg";
	prodIMG.hide().attr("src",newIMG).fadeIn(400);
});

$("#front").click(function(e) {
	newIMG = "images/oil_front.jpg";
	prodIMG.hide().attr("src",newIMG).fadeIn(400);
});

$("#HCBack").click(function(e) {
	newIMG = "images/hlotion_back.jpg";
	prodIMG.hide().attr("src",newIMG).fadeIn(400);
});

$("#HCFront").click(function(e) {
	newIMG = "images/hlotion_front.jpg";
	prodIMG.hide().attr("src",newIMG).fadeIn(400);
});

$("#lotionCW").click(function(e) {
	newIMG = "images/Lfront1.jpg";
	prodIMG.hide().attr("src",newIMG).fadeIn(400);
});

$("#lotionPul").click(function(e) {
	newIMG = "images/Lfront2.jpg";
	prodIMG.hide().attr("src",newIMG).fadeIn(400);
});

$("#lotionRose").click(function(e) {
	newIMG = "images/Lfront4.jpg";
	prodIMG.hide().attr("src",newIMG).fadeIn(400);
});

$("#lotionLG").click(function(e) {
	newIMG = "images/Lfront3.jpg";
	prodIMG.hide().attr("src",newIMG).fadeIn(400);
});
	
$("#cartContents").click(function(e) {
	document.location.href="shopping-cart.php";
});

$("#sendContact").click(function(e) {
	error = 0;
	$("input, textarea").css("background-color","#FFFFFF");
	$("p span").text("");
    var fname = $("#fname");
	var lname = $("#lname");
	var phone = $("#phone");
	var email = $("#email");
	var msg = $("#message");
	var formData = "submit=yes" +
				   "&fname=" + fname.val() +
				   "&lname=" + lname.val() +
				   "&phone=" + phone.val() +
				   "&email=" + email.val() +
				   "&msg=" + msg.val();
	//Validate text
	var errorMsg = "Field Invalid or Empty";
	if(fname.val().length == 0) {
		fname.css("background-color","#FFFFCC");
		$(".fname").text(errorMsg);
		error = 1;
	}
	if(lname.val().length == 0) {
		lname.css("background-color","#FFFFCC");
		$(".lname").text(errorMsg);
		error = 1;
	}
	if(email.val().length == 0) {
		email.css("background-color","#FFFFCC");
		$(".email").text(errorMsg);
		error = 1;
	}
	if(msg.val().length == 0) {
		msg.css("background-color","#FFFFCC");
		$(".msg").text(errorMsg);
		error = 1;
	}
	if(error != 0) {	
		return false;
	} else {
		$.ajax({
			url:"includes/sendMessage.php",
			data:formData,
			type:"POST",
			success:function(done){			
				$("#contactTable").hide('fast');
				$("#contactMessage").text(done).delay(1000).show('slow');
			}
		})
	}
});
	
$("#addToCart").click(function(e) {
	var desc = $("#description").text();
	var scent = $("#scent").val();
	var quantity = $("#quantity").val();
	var price = $("#price").text();
	var addCartData = "add=yes" +
					  "&desc=" + desc +
					  "&scent=" + scent +
					  "&quantity=" + quantity +
					  "&price=" + price;
	$.ajax({
		url:"includes/addItem.php",
		type:"POST",
		data:addCartData,
		success:function(response) {
			alert(response);
			document.location.href="shopping-cart.php";
		}			
	})

updateCart();										

});
	
$("#theShoppingCart tr:even").css("background-color","#CCCCCC");

$("#MixMatchToCart").click(function(e) {
	var desc = $("#special").val() + " Item Mix / Match: ";
		$.each(specialCont,function(i,v){
			desc = desc + v.name + ", ";
		})
	var scent = "Variety";
	var quantity = 1;
	var price = 0;
	var number = $("#special").val();
		if(number == 3){
			price = "30.00";
		}else if(number == 4){
			price = "38.00";		
		}else if(number == 5){
			price = "45.00";		
		}

	var addCartData = "add=yes" +
					  "&desc=" + desc +
					  "&scent=" + scent +
					  "&quantity=" + quantity +
					  "&price=" + price;
	$.ajax({
		url:"includes/addItem.php",
		type:"POST",
		data:addCartData,
		success:function(response) {
			alert(response);
			document.location.href="shopping-cart.php";
		}			
	})

updateCart();										

});


})
