// JavaScript Document

function setSalesTax()
{
	
	var tax = document.getElementById('tax_value').value;
	var product = document.getElementById('product').value;
	var shipping = document.getElementById('shipping').value;
		
	if(document.getElementById('state_tax').checked == true)
	{
		var tax_fee = (product*tax)*.01;
		var total = parseFloat(product)+parseFloat(tax_fee)+parseFloat(shipping);
	
		// show tax fields
		document.getElementById('tax-value').innerHTML = '$'+formatCurrency(tax_fee);
		document.getElementById('tax-label').style.display = "block";
		document.getElementById('tax-value').style.display = "block";
		document.getElementById('total_price').innerHTML = '$'+formatCurrency(total);
	}
	else
	{
		var total = parseFloat(product)+parseFloat(shipping);
		
		// hide tax fields
		document.getElementById('tax-label').style.display = "none";
		document.getElementById('tax-value').style.display = "none";
		document.getElementById('total_price').innerHTML = '$'+formatCurrency(total);
	}

	// set the paypal total
	document.getElementById('amount').value = formatCurrency(total);
}

function formatCurrency(num) 
{
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
		sign = (num == (num = Math.abs(num)));
		num = Math.floor(num*100+0.50000000001);
		cents = num%100;
		num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
		num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + num + '.' + cents);
}


function showCategoryProducts(category_id)
{
	document.getElementById('category-products').style.display = "none";
	document.getElementById('throbber').style.display = "block";
	document.getElementById('featured-items').style.display = "none";
	
	req = ajaxConnect();
    req.onreadystatechange = function(event)
    {
        if (req.readyState == 4)
        {
            if (req.status != 200)
            {
                alert("There was a problem connecting to the data: "+req.statusText);
            }
			else
			{		
				document.getElementById('category-products').innerHTML = req.responseText;
				document.getElementById('throbber').style.display = "none";
				document.getElementById('category-products').style.display = "block";
			}
        }
    }	

    req.open("GET", "/store/categories.php?category_id="+escape(category_id), true);
    req.send(null);	
	
	
}


function ajaxConnect()
{
	try
	{
		result = new XMLHttpRequest();
	}
	catch(e)
	{
		try
		{
			result = new ActiveXObject("MSXML2.XMLHTTP.3.0");
		}
		catch(e2)
		{
			result = false;
			alert("Unable to connect");
		}
	}
	return result;
}

