$(document).ready(function(){
    $("#date").datepicker();
    $("#rsvp-date").datepicker();
    
    $("#date").change(function(){
    	$(this).val($(this).val().replace(/\//g, '-'));
    });
    $("#rsvp-date").change(function(){
    	$(this).val($(this).val().replace(/\//g, '-'));
	});
			
	var totalCardsSelected = 2;
	$('input[name$="cards[]"]').change(function(){
	   if($(this).attr('checked') == true){
	   	totalCardsSelected += 1;
	   }else{
	   	totalCardsSelected -= 1;
	   }
	   
	   if(totalCardsSelected > 2){
	   	$('input[name$="cards[]"]').not(":checked").each(function(){
	   		var htmlHolder = $(this).next().html();
	   		if(htmlHolder.indexOf('<span class="red">(Additional Cost)</span>') == -1){
	   			$(this).next().html(htmlHolder + '<span class="red">(Additional Cost)</span>');
	   		}
	   	});
	   	
	   	$('input[name$="cards[]"]:checked').each(function(){
	   		var htmlHolder = $(this).next().html();
	   		if(htmlHolder.indexOf('<span class="red">(Additional Cost)</span>') != -1 && totalCardsSelected == 3){
	   			$(this).next().html(htmlHolder.replace('<span class="red">(Additional Cost)</span>', ''));
	   		}
	   	});
	   }else{
	   	$('input[name$="cards[]"]').each(function(){
	   		var htmlHolder = $(this).next().html();
	   		if(htmlHolder.indexOf('<span class="red">(Additional Cost)</span>') != -1){
	   			$(this).next().html(htmlHolder.replace('<span class="red">(Additional Cost)</span>', ''));
	   		}
	   	});
	   }
	});
	$('input[name$="cards[]"]').change();
	
	$("#product-form input, #product-form select").change(function(){
		calculateTotal();
	});
	
	
	$("#next").click(function(){
		$("#page-1").animate({
			left: -795
		});
		
		$("#page-2").animate({
			left: 0
		});
		$("#next").hide();
		$("#prev").show();
	});
	
	$("#prev").click(function(){
		$("#page-1").animate({
			left: 0
		});
		
		$("#page-2").animate({
			left: 795
		});
		$("#next").show();
		$("#prev").hide();
	});
});

function calculateTotal(){
	
	var orderTotal = 0;
	var totalQuantity = 0;
	
	//console.log($("#quantity").val());
	
	switch($("#quantity").val()){
	
		case '1':	orderTotal = 350;
					totalQuantity = 50;
					break;
					
		case '2':	orderTotal = 450;
					totalQuantity = 75;
					break;
			
		case '3':	orderTotal = 500;
					totalQuantity = 100;
					break;
				
		case '4':	orderTotal = 562.5;
					totalQuantity = 125;
					break;
		
		case '5':	orderTotal = 649.5;
					totalQuantity = 150;
					break;
					
		case '6':	orderTotal = 743.75;
					totalQuantity = 175;
					break;
		
		default:	orderTotal = 350;
					totalQuantity = 50;
					break;
		
	}
	
	
	
	var totalCardsSelected = 0;
	$('input[name$="cards[]"]:checked').each(function(){
		totalCardsSelected++;
	});
	if(totalCardsSelected == 4){
		orderTotal += (1.25 * totalQuantity);
	}
	if(totalCardsSelected == 5){
		orderTotal += (2.5 * totalQuantity);
	}
	
	$('input[name$="envelope-liners"]:checked').each(function(){
		orderTotal += (1 * totalQuantity);
	});
	
	$('input[name$="guest-address-printed"]:checked').each(function(){
		orderTotal += (1.25 * totalQuantity);
	});
	
	$('input[name$="paper-type"]:checked').each(function(){
		if($(this).val() == 'ivory-shimmer'){
			orderTotal += 50;
		}
	});
	
	
	$("#order_total").val(orderTotal + 15);
	$("#order_total_display span:first").html((orderTotal + 15).toFixed(2));
	//console.log(orderTotal);
}


function validateWordingForm(){
	
	var fullnameIsValid = phoneIsValid = emailIsValid = false;
	
	var groomNameIsValid = brideNameIsValid = groomMotherIsValid = groomFatherIsValid = brideMotherIsValid = brideFatherIsValid = false;
	var dateIsValid = timeIsValid = ceremonyLocationIsValid = receptionLocationIsValid = rsvpDateIsValid = rsvpLocationIsValid = rsvpAddressIsValid = specialInstructionsIsValid = phoneNumberIsValid = false;
	
	var formIsValid = false;
	
	if($("#full-name").val() == ''){
		$("#full-name-warning").show();
	}else{
		$("#full-name-warning").hide();
		fullnameIsValid = true;
	}
	
	var phoneNumberPattern = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;
	
	if($("#phone").val() == '' || !phoneNumberPattern.test($("#phone").val())){
		$("#phone-warning").show();
	}else{
		$("#phone-warning").hide();
		phoneIsValid = true;
	}
	
	if($("#email").val() == ''){
		$("#email-warning").show();
	}else{
		$("#email-warning").hide();
		emailIsValid = true;
	}
	
	if($("#groom-name").val() == ''){
		$("#groom-name-warning").show();
	}else{
		$("#groom-name-warning").hide();
		groomNameIsValid = true;
	}
	
	if($("#bride-name").val() == ''){
		$("#bride-name-warning").show();
	}else{
		$("#bride-name-warning").hide();
		brideNameIsValid = true;
	}
	
	/*
	if($("#groom-mother").val() == ''){
		$("#groom-mother-warning").show();
	}else{
		$("#groom-mother-warning").hide();
		groomMotherIsValid = true;
	}
	
	if($("#groom-father").val() == ''){
		$("#groom-father-warning").show();
	}else{
		$("#groom-father-warning").hide();
		groomFatherIsValid = true;
	}
	
	if($("#bride-mother").val() == ''){
		$("#bride-mother-warning").show();
	}else{
		$("#bride-mother-warning").hide();
		brideMotherIsValid = true;
	}
	
	if($("#bride-father").val() == ''){
		$("#bride-father-warning").show();
	}else{
		$("#bride-father-warning").hide();
		brideFatherIsValid = true;
	}
	*/
	
	if($("#date").val() == ''){
		$("#date-warning").show();
	}else{
		$("#date-warning").hide();
		dateIsValid = true;
	}
	
	if($("#time").val() == ''){
		$("#time-warning").show();
	}else{
		$("#time-warning").hide();
		timeIsValid = true;
	}
	
	if($("#ceremony-location").val() == ''){
		$("#ceremony-location-warning").show();
	}else{
		$("#ceremony-location-warning").hide();
		ceremonyLocationIsValid = true;
	}
	
	if($("#reception-location").val() == ''){
		$("#reception-location-warning").show();
	}else{
		$("#reception-location-warning").hide();
		receptionLocationIsValid = true;
	}
	
	if($("#rsvp-date").val() == ''){
		$("#rsvp-date-warning").show();
	}else{
		$("#rsvp-date-warning").hide();
		rsvpDateIsValid = true;
	}
	
	if($("#rsvp-location").val() == ''){
		$("#rsvp-location-warning").show();
	}else{
		$("#rsvp-location-warning").hide();
		rsvpLocationIsValid = true;
	}
	
	if($("#rsvp-address").val() == ''){
		$("#rsvp-address-warning").show();
	}else{
		$("#rsvp-address-warning").hide();
		rsvpAddressIsValid = true;
	}
	
	if($("#special-instructions").val() == ''){
		$("#special-instructions-warning").show();
	}else{
		$("#special-instructions-warning").hide();
		specialInstructionsIsValid = true;
	}
	
	if($("#phone-number").val() == ''){
		$("#phone-number-warning").show();
	}else{
		$("#phone-number-warning").hide();
		phoneNumberIsValid = true;
	}
	
	
	if(fullnameIsValid && phoneIsValid && emailIsValid && groomNameIsValid && brideNameIsValid && /*groomMotherIsValid && groomFatherIsValid && brideMotherIsValid && brideFatherIsValid &&*/ dateIsValid && timeIsValid && ceremonyLocationIsValid && receptionLocationIsValid && rsvpDateIsValid && rsvpLocationIsValid && rsvpAddressIsValid && specialInstructionsIsValid && phoneNumberIsValid){
		
		
		//$("#product-form").submit();
		
		// 	ok for whatever reason the form won't submit with .submit().
		//	so I have create a hidden submit button that I've had to
		//	manually "click" here. It's hacky, I know
		
		if(confirm('You will now be redirected to PayPal to enter payment info. You will be redirected back to Paisley Quill to confirm your order.')){
			$("#hidden-submit").click();
		}
		
		
	}else{
		if(!fullnameIsValid || !phoneIsValid || !emailIsValid)
		{
			$.scrollTo("#product-form", 300, {margin: true});
		}else{
			$.scrollTo("#groom-name", 300, {margin: true});
		}
	}
}
