// JavaScript Document
$(document).ready(function(){
	$("a.popLink").fancybox({"titleShow":false});
	
	$(".whyBox").click(function(){
		$(".productContainer").append("<div id='popupContainer'></div>");
		$("#popupContainer").load("/savings_and_promises.php"+($(".allow_pricematch").length>0?"?product_id="+$("#this_product_id").val():''),function(){
			//center popup
			$("#popupContainer").css({"position":"absolute","display":"block","top":"25px", "left":($(window).width()/2-$(".popup_frame").width()/2)+"px","z-index":"99999999"});
			$(".sandp_close").click(function(){
				$("#popupContainer").remove();
			});
			$(document).keyup(function(e) {
				if (e.keyCode == 27) { $("#popupContainer").remove(); }   // esc
			});
		});
	});
	
	$(".detail_tab").not("#tab1").hide();
	
	$("ul.detail_tabs li").click(function(){
		var tabi = $(this).attr("index");
		$("ul.detail_tabs li.selected").removeClass("selected");
		$(".detail_tab").hide();
		$("#tab"+tabi).show();
		$(this).addClass("selected");
	});
	
	$("#read_reviews").click(function(){
		$("#detail_tab_4").click();
	});
	
	$("#watch_a_video").click(function(){
		$("#detail_tab_3").click();
	});
	
	var qtext = "";
	$(".quantity_link").click(function(){
		//see if the price table is visible or not
		if ($("#quantity_prices:visible").length>0)
		{
			$(".quantity_link").html(qtext);
		}
		else
		{
			qtext = $(".quantity_link").html();
			$(".quantity_link").html("Hide Quantity Discounts");
		}
		$("#quantity_prices").toggle();
	});
});

function removePopup(nothing)
{
	$("#popupContainer").remove();
}

function addToWishlist()
{
	productForm('wishlist');
}

function checkWishlistValue(selectObj)
{
	if (selectObj.value == '~NEW~')
	{
		document.getElementById("wishlist_span").innerHTML = "<input type='hidden' id='wishlist_id' name='wishlist_id' value='0' /><input type='text' name='wishlist_name' id='wishlist_name' value='' class='input_large' />";
		document.getElementById("wishlist_name").focus();
	}
}

function productForm(formName)
{
	objForm = document.generalForm;
	
	var vars = '?task=ajax_update';
	
	try
	{
		for (var i=0; i<objForm.elements.length; i++)
		{
			if (objForm.elements[i].name)
			{	
				vars += '&'+objForm.elements[i].name+'='+objForm.elements[i].value;
			}
		}
	}
	catch (e)
	{
		//nothing
	}
	
	url = "";
	if (formName == "email")
	{
		if ($("#oos_email_me").val() == "")
		{
			alert("Please enter a valid email.");
			$("#oos_email_me").focus();
			return false;
		}
		url = "/inc/ajax_email_notify.php";
	}
	else if (formName == "pricematch")
	{
		url = "/inc/ajax_pricematch.php";
	}
	else if (formName=="wishlist")
	{
		url = "/inc/ajax_wishlist_form.php";
		var wn = document.getElementById("wishlist_name") ? document.getElementById("wishlist_name").value : ' ';
		
		if (wn == "")
		{
			//remove the why box popup
			$("#popupContainer").remove();
			alert("Please enter a name for your wishlist.");
			document.getElementById("wishlist_name").focus();
			return false;
		}
		if (wn == "~NEW~")	//protect reserved name
		{
			document.getElementById("wishlist_name").value = "NEW WISHLIST";
		}
		vars += '&wishlist_name='+wn;
		vars += '&wishlist_id='+document.getElementById("wishlist_id").value;
		
		if (document.generalForm.sale_type.value == "single")
		{
			vars += '&product_id='+document.generalForm.this_product_id.value;
			if (!document.generalForm.quantity)
				vars += "&quantity=1";
			if (!document.generalForm.quantity_in_stock)
				vars += "&quantity_in_stock=0";
		}
	}
	else if (formName == "show_wishlist")
	{
		url = "/inc/ajax_wishlist_add.php";
		if (document.generalForm.sale_type.value == "single")
		{
			vars += '&product_id='+document.generalForm.this_product_id.value;
			if (!document.generalForm.quantity)
				vars += "&quantity=1";
			if (!document.generalForm.quantity_in_stock)
				vars += "&quantity_in_stock=0";
		}
		else
		{
			var q = false;
			var temp = $(".quantity_field");
			for (var t=0; t<temp.length; t++)
			{
				if (!isNaN(parseInt(temp[t].value)))
				{
					q = true;
					break;
				}
			}
			if (!q)
			{
				
				//remove the why box popup
				$("#popupContainer").remove();
				alert("Please enter a quantity of at least 1.");	
				toggleLinks("enable");
				return;
			}
		}
	}
	else if (formName=="member")
	{
		url = "/inc/ajax_wishlist_add.php";
		if (document.generalForm.sale_type.value == "single")
		{
			vars += '&product_id='+document.generalForm.this_product_id.value;
			if (!document.generalForm.quantity)
				vars += "&quantity=1";
			if (!document.generalForm.quantity_in_stock)
				vars += "&quantity_in_stock=0";
		}
		else
		{
			var q = false;
			var temp = $(".quantity_field");
			for (var t=0; t<temp.length; t++)
			{
				if (!isNaN(parseInt(temp[t].value)))
				{
					q = true;
					break;
				}
			}
			if (!q)
			{
				
				//remove the why box popup
				$("#popupContainer").remove();
				alert("Please enter a quantity of at least 1.");	
				toggleLinks("enable");
				return;
			}
		}
	}
		
	
	//remove the why box popup
	$("#popupContainer").remove();
		
	$.get(url+vars, function(data){
		var popupContainer = document.createElement("div");
		popupContainer.id = "popupContainer";
		popupContainer.innerHTML = data;
		$("body").prepend(popupContainer);
		//center popup
		$("#popupContainer").css({"position":"absolute","display":"block","top":"25px", "left":($(window).width()/2-$(".popup_frame").width()/2)+"px","z-index":"99999999"});
	});
}

function validatePriceMatch()
{
	var price_match_store = document.getElementById("price_match_store").value;
	var price_match_website = document.getElementById("price_match_website").value;
	var price_match_price = document.getElementById("price_match_price").value;
	
	if (price_match_store == "")
	{
		alert("Please enter the name of the store where you found the item.");
		document.getElementById("price_match_store").focus();
		return;
	}
	if (price_match_website == "")
	{
		alert("Please enter the exact website (URL) where you found the item.");
		document.getElementById("price_match_website").focus();
		return;
	}
	
	price_match_price = parseFloat(price_match_price.replace(/[^0-9.]/ig, ""));
	if (price_match_price == "0" || isNaN(price_match_price))
	{
		alert(price_match_price+"\nPlease enter a valid price.");
		document.getElementById("price_match_price").focus();
		return;
	}
	
	var order_value = 0;
	var myform = document.createElement("form");
	myform.setAttribute("action","/inc/ajax_pricematch_form.php");
	myform.setAttribute("name","priceMatchForm");
	myform.setAttribute("method","post");
	
	var pm_product_id = document.createElement("input");
	pm_product_id.setAttribute("type","hidden");
	pm_product_id.setAttribute("name","product_id");
	pm_product_id.setAttribute("value",document.generalForm.this_product_id.value);
	myform.appendChild(pm_product_id);
	
	var url_name = document.createElement("input");
	url_name.setAttribute("type","hidden");
	url_name.setAttribute("name","url_name");
	url_name.setAttribute("value",document.generalForm.url_name.value);
	myform.appendChild(url_name);
	
	var pm_store = document.createElement("input");
	pm_store.setAttribute("type","hidden");
	pm_store.setAttribute("name","price_match_store");
	pm_store.setAttribute("value",price_match_store);
	myform.appendChild(pm_store);
	
	var pm_website = document.createElement("input");
	pm_website.setAttribute("type","hidden");
	pm_website.setAttribute("name","price_match_website");
	pm_website.setAttribute("value",price_match_website);
	myform.appendChild(pm_website);
	
	var pm_price = document.createElement("input");
	pm_price.setAttribute("type","hidden");
	pm_price.setAttribute("name","price_match_price");
	pm_price.setAttribute("value",price_match_price);
	myform.appendChild(pm_price);
		
	$(".productContainer").append(myform);
	myform.submit();
}

function addToCart()
{
	var objForm = document.generalForm;
	
	toggleLinks("disable");
	
	var vars = 'test=0';
	
	var sale_type = objForm.sale_type.value;
	var item_added = false;
	
	if (objForm.availability.value == "drop_ship")
	{
		if (!confirm("I understand that I am placing an order for a drop ship item that ships separately within 1-2 business days.\nSorry, we cannot ship this item outside of the contiguous United States!"))
		{
			toggleLinks("enable");
			return false;	
		}
	}
	
	try
	{
		for (var i=0; i<objForm.elements.length; i++)
		{
			if (objForm.elements[i].name)
			{	
				if (objForm.elements[i].name == "task")
				{
					if ($("#product_code").val().substr(0, 2) == "GC")
						objForm.elements[i].value = "add_gift_card";
				}
				else if (objForm.elements[i].name.indexOf("quantity") != -1 && objForm.elements[i].name.indexOf("stock") == -1)
				{
					objForm.elements[i].value = parseInt(objForm.elements[i].value);
					if (!isNaN(objForm.elements[i].value) && objForm.elements[i].value > 0)
					{
						item_added = true;
					}
					else
					{
						objForm.elements[i].value = "";	
					}
				}
				vars += '&'+objForm.elements[i].name+'='+objForm.elements[i].value;
			}
		}
	}
	catch (e)
	{
		//nothing
	}
	
	if (!item_added)
	{
		alert("Please enter a quantity of at least 1.");
		toggleLinks("enable");
		return false;
	}
	
	$.post("/inc/ajax_item_added.php", vars, function(data){
		try
		{
			//split up the response by *@@@* as noted in ajax_item_added.php 
			var cart_actions = data.split('*@@@*');
			if (cart_actions.length > 1)
			{
				//alert(cart_actions[1]+" "+cart_actions[2]);
				if (cart_actions[1] == "reduce_quantity")
				{
					if (cart_actions[2]==0)
					{
						document.generalForm.elements[cart_actions[3]].value = "";
						alert("We're sorry: This item is no longer in stock.");
					}
					else
					{
						document.generalForm.elements[cart_actions[3]].value = cart_actions[2];
						alert("There is not enough of this item in stock.  Your quantity has been reduced to "+cart_actions[2]);
					}
				}
			}
			else
			{
				//document.getElementById('show').innerHTML = data;
				$("#popupContainer").remove();
				var popupContainer = document.createElement("div");
				popupContainer.id = "popupContainer";
				popupContainer.innerHTML = data;
				popupContainer.style.width = "auto";
				$("body").prepend(popupContainer);
				
				//center popup
				$("#popupContainer").css({"position":"absolute","display":"block","top":"25px", "left":($(window).width()/2-$(".popup_frame").width()/2)+"px","z-index":"99999999"});
				
				document.getElementById('hidden_cart_button').style.display = "";
				document.getElementById('view_cart_link').focus();
			}
			toggleLinks("enable");
		}
		catch(e)
		{
			alert(e);	
		}
		
		//split up the response by *###* as noted in ajax_item_added.php 
		var cart_summary = data.split('*###*');
		try
		{
			if (cart_summary.length == 3)
				document.getElementById("header_cart_button").innerHTML = cart_summary[1];
		}
		catch(e)
		{
			alert(e);	
		}
	});
	
	return false;	//this is needed so page doesn't refresh	
}

function memberLogin()
{
	toggleLinks("disable");
	try
	{
		var vars = 'task=ajax_login';
		vars += '&email='+document.getElementById("member_email").value;
		vars += '&password='+document.getElementById("member_password").value;
	}
	catch(e)
	{
		//nothing	
	}
	
	$.post("/inc/ajax_login_form.php", vars, function(data)
	{
		var login_response = data.split('*@@@*');
		if (login_response.length > 1)
		{
			if (login_response[1] == "0")	//login error
			{
				alert(login_response[2]);	
			}
			else	//login success
			{
				productForm('member');	//show the popup again now that they've successfully logged in
			}
		}
		else
		{
			alert("An error occured while trying to login to your member account. Please contact us if the problem persists.");	
		}
		toggleLinks("enable");	
	});
}
