//me: add prompt for dropship items, and prompt for backorder items in the cart
function switchProductImage(pc)
{
	var l_image_link = document.getElementById("large_image_link");
	var l_image = document.getElementById("l_image");
	
	l_image.src = "http://www.wholesalechess.com/images/products/"+pc;
	l_image_link.href = "javascript: loadOverlayPopup('"+pc+"');";
	l_image_link.onclick = "";
}

function submitForm()
{
	sendRequest(document.generalForm);
}

function sendRequest(objForm)
{
	toggleLinks("disable");
	
	var vars = 'task=ajax_update';
	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.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;
	}
	
	new Ajax.Request("/inc/ajax_item_added.php", 
		{ 
			method: 'post', 
			postBody: vars,
			onComplete: showResponse 
		});
	
	return false;	//this is needed so page doesn't refresh
}

function showAjaxForm(form_name)
{
	toggleLinks("disable");
	
	objForm = document.generalForm;
	objForm.onSubmit = "return false";
	
	var vars = 'task=ajax_update';
	
	if (form_name=="wishlist")
	{
		if (document.getElementById("wishlist_name").value == "")
		{
			alert("Please enter a name for your wishlist.");
			document.getElementById("wishlist_name").focus();
			return false;
		}
		if (document.getElementById("wishlist_name").value == "~NEW~")	//protect reserved name
		{
			document.getElementById("wishlist_name").value = "NEW WISHLIST";
		}
		vars += '&wishlist_name='+document.getElementById("wishlist_name").value;
		vars += '&wishlist_id='+document.getElementById("wishlist_id").value;
	}
	
	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
	}
	
	if (form_name=="member")
	{
		new Ajax.Request("/inc/ajax_wishlist_add.php", 
			{ 
				method: 'post', 
				postBody: vars,
				onComplete: standardResponse 
			});
	}
	else if (form_name=="wishlist")
	{
		new Ajax.Request("/inc/ajax_wishlist_form.php", 
			{ 
				method: 'post', 
				postBody: vars,
				onComplete: standardResponse 
			});
	}
	else if (form_name=="pricematch")
	{
		new Ajax.Request("/inc/ajax_pricematch.php", 
			{ 
				method: 'post', 
				postBody: vars,
				onComplete: standardResponse 
			});
	}
	else if (form_name=="email")
	{
		new Ajax.Request("/inc/ajax_email_notify.php", 
			{ 
				method: 'post', 
				postBody: vars,
				onComplete: standardResponse 
			});
	}
}

function standardResponse(req)
{
	toggleLinks("enable");
	document.getElementById('show').innerHTML = req.responseText;
	
	var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body;
	//var dsocleft=document.all? iebody.scrollLeft : pageXOffset;
	var dsoctop=document.all? iebody.scrollTop : pageYOffset;
	//document.getElementById('popup').style.left = parseInt(document.getElementById('popup').style.left) + dsocleft + "px";
	document.getElementById('popup').style.top = (80 + dsoctop) + "px";
}

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	
	}
	
	new Ajax.Request("/inc/ajax_login_form.php", 
		{ 
			method: 'post', 
			postBody: vars,
			onComplete: loginResponse 
		});
}

function loginResponse(req)
{
	var login_response = req.responseText.split('*@@@*');
	if (login_response.length > 1)
	{
		if (login_response[1] == "0")	//login error
		{
			alert(login_response[2]);	
		}
		else	//login success
		{
			showAjaxForm('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");	
}

function showResponse(req)
{
	try
	{
		//split up the response by *@@@* as noted in ajax_item_added.php 
		var cart_actions = req.responseText.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 = req.responseText;
			
			var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body;
			//var dsocleft=document.all? iebody.scrollLeft : pageXOffset;
			var dsoctop=document.all? iebody.scrollTop : pageYOffset;
			//document.getElementById('popup').style.left = parseInt(document.getElementById('popup').style.left) + dsocleft + "px";
			document.getElementById('popup').style.top = (80 + dsoctop) + "px";
			
			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 = req.responseText.split('*###*');
	try
	{
		if (cart_summary.length == 3)
			document.getElementById("header_cart_button").innerHTML = cart_summary[1];
	}
	catch(e)
	{
		//nothing	
	}
}

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 addToWishlist()
{
	showAjaxForm('wishlist');
}

function priceMatch()
{
	showAjaxForm('pricematch');
}

function closePopup(popupid)
{
	document.generalForm.onSubmit = "return sendRequest(this)";
	document.getElementById(popupid).style.display = "none";	
}

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 form_place = document.getElementById("place_form_here");
	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);
		
	form_place.appendChild(myform);
	myform.submit();
}

function submitProductEmail()
{
	if (document.generalForm.email.value == "")
	{
		alert("Please enter your email.");
		return false;
	}
	showAjaxForm('email');
}

function product_image_loader()
{
	for (var i=0; i<document.generalForm.elements.length; i++)
	{
		if (document.generalForm.elements[i].name.indexOf("product_code") != -1)
		{
			pic = new Image(600,600); 
			pic.src = "http://www.wholesalechess.com/images/products/"+document.generalForm.elements[i].value+"_1L.jpg";
			//alert(pic.src);
		}
	}
}

addLoadEvent(product_image_loader);