// This function changes the main image BOO YA
//imageChange added 9-24-08/JMB
function imageChange(image, color_id, name, inventory)
{
	//image is the filepath, filename and extension of the imageChange
	if (inventory < 1)
		alert('Sorry, out of stock!');
	else
	{
		document.getElementById('image').src = image;
		document.getElementById('larger').href = image.replace('_220', '_500');
		document.getElementById('color').value = color_id;
		document.getElementById('name').innerHTML = 'Select a Color: ' + name;
		document.getElementById('inventory').value = inventory;
	}
}

function dropdownChange(color_id)
{
	var inv = document.getElementById('hideinv' + color_id).value;
	if (inv < 1)
	{
		alert('Sorry, out of stock!');
	}
}

function checkInventory()
{
	var inventory = document.getElementById('inventory').value;
	var quantity = document.getElementById('quantity').value;

	if (Number(quantity) > Number(inventory))
	{
		alert('Sorry, there are only ' + inventory + ' of this item left. Please lower your quantity!');
		return false;
	}
	else if (Number(inventory) < 1)
	{
		alert('Sorry, out of stock!');
		return false;
	}
	else
		return true;
}

function checkDropdownInventory()
{
	var len = document.addtoCart.color.length;
	var chosen = '';
	
	for (var i = 0; i < len; i++)
	{
		if (document.addtoCart.color[i].selected)
			chosen = document.addtoCart.color[i].value;
	}
	
	var inv = document.getElementById('hideinv' + chosen);
	var quantity = document.getElementById('quantity').value;
	
	if (Number(quantity) > Number(inv.value))
	{
		alert('Sorry, there are only ' + inv.value + ' of this item left. Please lower your quantity!');
		return false;
	}
	else if (Number(inv.value) > 0)
		return true;
	else
	{
		alert('Sorry, out of stock!');
		return false;
	}
}

var quantities = new Array();

//integers_only, update_total, addCommas, and deleteItem added 8-20-07/JMB
function integers_only(value, inputId)
{
	//This function makes sure there are only integers in 'value' and if not, strips out un-integers and puts it back into 'inputId' which is the quantity field. It will also round any floating double
	var newvalue = '';
	for (i = 0; i < value.length; i++)
	{
		var char = value.charAt(i);
		var reg = new RegExp("[0-9.]");
		if (reg.test(char))
			newvalue = newvalue + char;
	}
	newvalue = Math.round(newvalue*Math.pow(10,2))/Math.pow(10,2);
	if (inputId != null)
		document.getElementById(inputId).value = newvalue;
	else
		return newvalue;
}

//alnumOnly added 10-15-08/JMB
function alnumOnly(value, inputId)
{
	//This function makes sure there are only alpha numeric values in 'value' and if not, strips out un-integers and puts it back into 'inputId' which is the quantity field. It will also round any floating double
	var newvalue = '';
	for (i = 0; i < value.length; i++)
	{
		var char = value.charAt(i);
		var reg = new RegExp("[0-9a-zA-Z\s]");
		if (reg.test(char))
			newvalue = newvalue + char;
	}
	if (inputId != null)
		document.getElementById(inputId).value = newvalue;
	else
		return newvalue;
}

function update_total(count)
{
	var quantity = document.getElementById('quantity' + count).value; //0
	var price = document.getElementById('money' + count).innerHTML;
	price = integers_only(price, null);
	var update_price = quantity * price;
	update_price = addCommas(update_price);
	document.getElementById('subtotal' + count).innerHTML = '$' + update_price;
	
	//var grand_total = document.getElementById('grandtotal').innerHTML;
	//grand_total = integers_only(grand_total);
	var i = 0;
	var grand_total = 0;
	do {
		var cart_item = document.getElementById('subtotal' + i).innerHTML;
		cart_item = integers_only(cart_item);
		grand_total += cart_item;
		i++;
	}
	while (isNaN(document.getElementById('subtotal' + i)));
	
	grand_total = Math.round(grand_total*Math.pow(10,2))/Math.pow(10,2);
	grand_total = addCommas(grand_total);
	document.getElementById('grandtotal').innerHTML = '$' + grand_total;
	
	return update_price;
}

//inventory_count added 9-16-08/JMB
function inventory_count(count)
{
	var inventory = parseInt(document.getElementById('inventory' + count).value);
	var quantity = parseInt(document.getElementById('quantity' + count).value);
	if (quantity > inventory)
	{
		alert('There are only ' + inventory + ' of this item left in stock!');
		document.getElementById('quantity' + count).value = inventory;
		update_total(count);
	}
	
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function shipping()
{
	var zip = document.getElementById('zip').value;
	var total = document.getElementById('grandtotal').value;
	var wa = new Array('99205');
	
	for (var i = 0; wa.length; i++)
	{
		if (zip == wa[i])
			document.getElementById('grandtotal').innerHTML = '$' + integers_only(document.getElementById('grandtotal').innerHTML, null) * 8.06;
	}
	
	document.getElementById('shipping').value = '$10.16';
}

//
// Ajax Stuff
//
 
function createRequestObject()
{
	var ro;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer")
	{
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		ro = new XMLHttpRequest();
	}
	return ro;
}

var http = createRequestObject();

function updateDb()
{
	http.open('get', 'update_db.php?first=' + document.getElementById('first').value + '&last=' + document.getElementById('last').value + '&address1=' + document.getElementById('address1').value + '&address2=' + document.getElementById('address2').value + '&city=' + document.getElementById('city').value + '&state=' + document.getElementById('state').value + '&zip=' + document.getElementById('zip').value + '&phone=' + document.getElementById('phone').value + '&email=' + document.getElementById('email').value);
	//alert(page + '&notnew=1');
	http.send(null);
}