//javascript for the site goes here

var path = "";

function UpdateShoppingCart()
{
	var req = new Request.HTML({
		method: 'get',
		url: path + '/store/cart/ShowCartItems',
		update: $('shoppingcart')
	}).send();
}

function AddItem(cartItemID, itemSize)
{
	var req = new Request.HTML({
		method: 'get',
		url: path + '/store/cart/AddItem.rails',
		data: {
			'cartItemID': cartItemID,
			'itemSize': itemSize,
			'isAjaxCall': 'yes'
		},
		update: $('shoppingcart')
	}).send();
}

function RemoveItem(cartItemID)
{
	var req = new Request.HTML({
		method: 'get',
		url: path + '/store/cart/RemoveItem.rails',
		data: {
			'cartItemID': cartItemID,
			'isAjaxCall': 'yes'
		},
		update: $('shoppingcart')
	}).send();
}

function RemoveItemFromCheckout(cartItemID)
{
	var req = new Request.HTML({
		method: 'get',
		url: path + '/store/cart/RemoveItemFromCheckout.rails',
		data: {
			'cartItemID': cartItemID
		},
		update: $('checkout')
	}).send();
}

function updatePrices(itemPrice, input, price, itemID)
{
	var itemValue = (itemPrice * parseInt(input.value));
	if(!isNaN(itemValue))
	{
		var total = parseInt($('total').innerHTML.substring(1,$('total').innerHTML.length));
		var oldPrice = parseInt($('price_'+price).innerHTML.substring(1,$('price_'+price).innerHTML.length));

		$('price_' + price).innerHTML = '$' + CurrencyFormatted(itemValue);

		total -= oldPrice;

		total += itemValue;
		$('total').innerHTML = '$' + CurrencyFormatted(total);

		var url = path + "/store/cart/UpdateCartItemCount.rails";
		var myAjax = new Request.HTML({
			method: 'post',
			//update: $('shopCart'),
			'url': url
		}).send({data: { 'cartItemID': itemID, 'itemCount': input.value }});
	}
	else
	{
		return;
	}
}

function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i))
	{
		i = 0.00;
	}
	var minus = '';
	if(i < 0)
	{
		minus = '-';
	}
	i = Math.abs(i);
	i = parseInt((i + 0.005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0)
	{
		s += '.00';
	}
	if(s.indexOf('.') == (s.length - 2))
	{
		s += '0';
	}
	s = minus + s;
	return s;
}

function processCartResponse(response)
{
	if($chk(response))
	{
		var action = '';
		var total = '';
		response = response.replace(/<method>(\w+)<\/method>/gi, function() {
			action = arguments[1];
			return '';
		});
		response = response.replace(/<total>([\$\w\.]+)<\/total>/gi, function() {
			total = arguments[1];
			return '';
		});
		switch (action){
			case 'add':
				cartScroller.addContent(response);
				$('total').set('text', total);
				break;
			default:
				$('shopCart').set('html', response);
				break;
		}
	}
}

function RemoveItemFromCheckout(cartItemID)
{
	var url = path + "/store/cart/RemoveCartCheckout.rails";
	var myAjax = new Request.HTML({
		method: 'post',
		evalScripts: true,
		evalResponse: false,
		update: $('cartContent'),
		'url': url
	}).send({data: { 'cartItemID': cartItemID }});
}

function copyBillingAddy(checkBox)
{
	if(checkBox.checked == true)
	{
		$('shipName').value = $('billName').value;
		$('shipAddress1').value = $('billAddress1').value;
		$('shipAddress2').value = $('billAddress2').value;
		$('shipState').value = $('billState').value;
		$('shipCity').value = $('billCity').value;
		//$('shipZip').value = $('billZip').value;  // we don't want to change this, or the shipping cost might be wrong
		//$('shipCountry').getChildren().each(function(el, i) {
		//	if(el.getProperty('value') == $('billCountry').value)
		//	{
		//		el.setProperty('selected','selected');
		//	}
		//});
	}
	else
	{
		$('shipName').value = "";
		$('shipAddress1').value = "";
		$('shipAddress2').value = "";
		$('shipState').value = "";
		$('shipCity').value = "";
		$('shipState').value = "";
		//$('shipZip').value = "";  // we don't want to change this, or the shipping cost might be wrong
		//$('shipCountry').getChildren().each(function(el, i) {
		//	if(el.getProperty('value') == '')
		//	{
		//		el.setProperty('selected','selected');
		//	}
		//});
	}
}

function ResetForm()
{
	window.boxes.each(function(item){
		item.reset();
	});
	$$('input').each(function(item){
		item.value = "";
	})
	$$('.removeable').each(function(item){
		item.innerHTML = "";
	})
	$$('textarea').each(function(item){
		item.value = "";
	})
}

// don't allow a user to type non-numeric characters into a field
function checkNumericKey(e) {
	var keyCode = e.which ? e.which : e.keyCode;  // IE uses e.keyCode, others use e.which
	var digit = !isNaN(parseInt( String.fromCharCode(keyCode)))                // number
		|| ( keyCode >= 8 && keyCode <= 46 && keyCode !=16 && keyCode !=32 )   // keyboard navigation, shift, enter
		|| ( keyCode >= 96 && keyCode <=105 );                                 // numeric keyboard number keys
	return digit;
};

// find the x,y position of an object
function findPos(obj)
{
	var curleft = curtop = 0;
	if (obj.offsetParent)
	{
		do
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
		while (obj = obj.offsetParent);
		var retArray = new Array(curleft,curtop)
		return retArray;
	}
}

