function doPayPal(shipping)
{	

	if (shipping)
	{
		if (!$('#first_name').val()) { jAlert('You must enter a name!', 'Name Required'); return false; }
		if (!$('#address1').val()) { jAlert('You must enter a street address!', 'Address Required'); return false; }
		if (!$('#city').val()) { jAlert('You must enter a city!', 'City Required'); return false; }
		if (!$('#state').val()) { jAlert('You must select a state!', 'State Required'); return false; }
		if (!$('#zip').val()) { jAlert('You must enter a zip code!', 'Zip Code Required'); return false; }
		if (!$('#night_phone_a').val()
			|| !$('#night_phone_b').val()
			|| !$('#night_phone_c').val()) { jAlert('You must enter a phone number!', 'Phone Number Required'); return false; }
	}
	
	var postObj = {};
	
	if (shipping)
	{
		postObj = 
		{ 
			"shipFirstName": $('#first_name').val(),
			"shipLastName": $('#last_name').val(),
			"shipAddr": $('#address1').val(),
		    "shipCity": $('#city').val(),
			"shipState": $('#state').val(),
			"shipZip": $('#zip').val(),
			"shipPhone":($('#night_phone_a').val().trim() + ' ' + $('#night_phone_b').val().trim()  + ' ' + $('#night_phone_c').val().trim()),
			"shipEmail": $('#email').val(),
			"action": "setSavedOrderShipping"
		}
	}
	else
	{
		postObj = 
		{ 
			"shipEmail": $('#email').val(),
			"action": "setSavedOrderShipping"
		}
	}
	
	jQuery.post("/shop/bagproc.php", 
		postObj,
	  	function(data)
	    {			
			$('#payPalForm').submit();
	  	},
		"json");
	
	return true;
}

function validateRegisterForm()
{	
	if (document.registerForm.email.value == "" || !emailCheck(document.registerForm.email.value))
	{
		jAlert('Sorry, you must enter a valid email address', 'Invalid Email');
		return false;
	}
	

	if (document.registerForm.password.value != document.registerForm.password2.value)
	{
		jAlert('Sorry, your password was not verified. Please re-type the password in both the Password and Verify Password fields.', 'Password Not Verified');
		document.registerForm.password.focus();
		return false;
	}

	document.registerForm.submit();
	return true;
}

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }
function saveInfo()
{	
	$('#save_first_name').val($('#first_name').val());
	$('#save_last_name').val($('#last_name').val());
	$('#save_address1').val($('#address1').val());
	$('#save_city').val($('#city').val());
	$('#save_state').val($('#state').val());
	$('#save_zip').val($('#zip').val());
	$('#save_phone').val($('#night_phone_a').val().trim() + ' ' + $('#night_phone_b').val().trim()  + ' ' + $('#night_phone_c').val().trim());
	$('#saveInfoForm').submit();
}

function emailCheck(str) 
{
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){ return false }
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){ return false }
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){ return false }
		if (str.indexOf(at,(lat+1))!=-1){ return false }
		if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){ return false }
		if (str.indexOf(dot,(lat+2))==-1){ return false }
		if (str.indexOf(" ")!=-1) { return false }
 	    return true					
}

function refreshCart(data)
{
	if (data.itemCnt <= 0)
	{
		$("#shoppingBag").fadeOut();
		return;
	}
	
	$("#shoppingBag").fadeIn("slow");
	$("#shoppingBag").html('<center><a href="/shop/bag.php"><img class="noBorder" src="/shop/images/viewbag.png"></a><br/>'+ data.itemCnt + ' item'+(data.itemCnt>1?'s':'')+'<br/>(+tax & ship)<br/>'+ data.total + '<br><strong>[<a href="/shop/bag.php">checkout</a>]</strong></center>');
	$('#shoppingBag').pulse({
	    backgroundColors: ['#3399FF','#cccccc'],
	    speed: 100,
		duration: 1000
	});
}

function getBag()
{
	$("#shoppingBag").hide();
	jQuery.getJSON("/shop/bagproc.php",
				function(data)
				{
					refreshCart(data);
				}
			);
}

function addPrize(_flavor, _id)
{
	jQuery.post("/shop/bagproc.php", 
		{ 
			quantity: 0,
			product: 0,
			flavor: _flavor,
			priceAddOption: 0,
			photo: _id,
			action: "addPrize"
		},
	  	function(data){
			
			refreshCart(data);
	  	},
		"json");
}

function copyOrderToBag(orderId)
{
	if (confirm("You would like to add the items from this order to your current shopping bag? If we no longer carry a particular item, flavor, or option, it will not be added to your bag."))
	{
	jQuery.post("/shop/bagproc.php", 
		{ 
			order: orderId,
			action: "copyOrder"
		},
	  	function(data){
			
			refreshCart(data);
	  	},
		"json");
	}
}

function addToBag(productId)
{
	jQuery.post("/shop/bagproc.php", 
		{ 
			quantity: $("#"+productId+"quantity").val(),
			product: productId,
			flavor: $("#"+productId+"flavor").val(),
			priceAddOption: $("#"+productId+"priceAddOption").val(),
			action: "addItem"
		},
	  	function(data){
			
			refreshCart(data);
	  	},
		"json");
}


function updateBagCheckout()
{
	var formVals = $("#shoppingBagForm").serialize();
	jQuery.get("/shop/bagproc.php?" + formVals, 
		{ 
			action: "update"
		},
	  	function(data, textStatus){
			if (data.itemCnt > 0)
				window.location = "/shop/checkout";
			else
				window.location = "/shop/bag.php";
	  	},
		"json");
	
}

function aboutPrize()
{
	
		jAlert("We're giving you a One Penny One Pounder bag of treats for letting The Baker's Dog use your photo!","About Your Prize");
	
}

function aboutPriorityProcessing()
{
	
		jAlert("Priority processing means we'll prepare the order as soon as we receive it and will choose the fastest shipping method given the amount you've paid for shipping. There is an additional charge for this service.","About Priority Processing");
	
}
function aboutShipping()
{
		jAlert("Our shippers charge us a minimum rate. So you will notice the shipping cost for 2 items is only slightly more than 1 item alone. Take advantage of this by ordering multiple items!", "About Shipping Charges");
	
}

function aboutTax()
{
	jAlert("We realize not all online stores charge sales tax. But you would have to pay tax if you walked into our retail store and made a purchase. We would LOVE to charge less but we have to pay taxes and additional fees to process your online payment. We gotta keep the lights on around here!","About Sales Tax");
}

function aboutCustom()
{
	jAlert("This means that you are able to select a photo, either one you upload or one that has been shared to you, to be printed on this treat package. Add it to your bag and then view your shopping bag to select the photo! <a href='http://bakersdog.com/blog/custom-photo-packaging/'>More details...</a>","About Custom Photos");
}
