function displayDeliveryAddress(theField) {

	if(theField.checked) {
		deliveryAddressObj = document.getElementById('delivery_address');
		deliveryAddressObj.style.display='block';
	} else {
		deliveryAddressObj.style.display='none';
	}
}

function changeSize(productId) {
	var sizeId = $('SizeSize').value;
	var colorId = $('ColorColor').value;
	var typeId = $('TypeTypes').value;
	new Ajax.Updater('product_info_bottom', '/orders/changeSize/'+productId+'/'+sizeId+'/'+colorId+'/'+typeId, {method:'post', parameters: 'id= ' + productId});

}

function updateCart(productId, sizeId, colorId, typeId, qtyName, action) {
	var qty = $(qtyName).value;
	
	if(action == 'remove') {
		new Ajax.Updater('checkout_item_id', '/checkouts/updateCart/'+productId+'/'+sizeId+'/'+colorId+'/'+typeId+'/0', {method:'post', parameters: 'id= ' + productId});
	} else if(action == "update") {
		new Ajax.Updater('checkout_item_id', '/checkouts/updateCart/'+productId+'/'+sizeId+'/'+colorId+'/'+typeId+'/'+qty, {method:'post', parameters: 'id= ' + productId});
	}
}

function viewRelatedProduct(theField) {

	var idx = theField.options.selectedIndex;
	var productId = theField.options[idx].value;

	if(productId) {
		new Ajax.Updater('relate_product_view_id', '/products/viewProduct/'+productId, {method:'post', parameters: 'id= ' + productId});
	}
}

function removeRelatedProduct(productId, relatedProductId) {

	var go = confirm('Are you sure?');

	if(go && productId && relatedProductId) {
		new Ajax.Updater('current_related_product_id', '/products/removeRelatedProduct/'+productId+'/'+relatedProductId, {method:'post', parameters: 'id= ' + productId});
	}
}

function removeProductSize(productId, sizeId) {

	var go = confirm('Are you sure?');

	if(go && productId && sizeId) {
		new Ajax.Updater('current_size_id', '/products_sizes/removeProductSize/'+productId+'/'+sizeId, {method:'post', parameters: 'id= ' + productId});
	}
}

function removeColorProduct(productId, colorId) {

	var go = confirm('Are you sure?');

	if(go && productId && colorId) {
		new Ajax.Updater('current_color_id', '/colors_products/removeColorProduct/'+productId+'/'+colorId, {method:'post', parameters: 'id= ' + productId});
	}
}

function removeProductType(productId, typeId) {

	var go = confirm('Are you sure?');

	if(go && productId && typeId) {
		new Ajax.Updater('current_type_id', '/products_types/removeProductType/'+productId+'/'+typeId, {method:'post', parameters: 'id= ' + productId});
	}
}

function changeFreight(freight, registeredFreight, total, totalWithFreight) {
	var registeredPost = $('registeredPost').checked;
	var freightCost = $('freight_cost');
	var totalCost = $('total_cost');
	var registered = '0';

	if(registeredPost == true) {
		registered = 1;
		freightCost.firstChild.nodeValue = registeredFreight.toFixed(2);
		totalCost.firstChild.nodeValue = totalWithFreight.toFixed(2);
	} else {
		freightCost.firstChild.nodeValue = freight.toFixed(2);
		totalCost.firstChild.nodeValue = total.toFixed(2);
	}
	new Ajax.Updater('scart_details_id', '/checkouts/registeredPost/'+registered, {method:'post', parameters: 'id= ' + registered});

}

function processOrder() {
	var url = '/checkouts/checkout';
	var pars = '';

	var myAjax = new Ajax.Request(
			url, 
			{
				method: 'post', 
				parameters: pars, 
				onComplete: showResponse
			});
	return false;
}

function trim(str) {
	return str.replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, '');
}


function showResponse(originalRequest)
{
	// put returned XML in the textarea
	// $('custom').value = originalRequest.responseText;
	var value = originalRequest.responseText;

	if(trim(value) == "intel") {
		window.location = "/payments/paymentIntel";
	} else if(isNaN(parseInt(value))) {
		window.location = "/users/login";
	} else {
		$('custom').value = value;
		$('paypal').submit();
	}
}

function addToCart(productId, direction) {
	var noItem;
	noItem = parseInt($('product_item_count_text').value);
	var sizeId = $('SizeSize').value;
	var colorId = $('ColorColor').value;
	var typeId = $('TypeTypes').value;
	var pars = '';

	if(!productId) {
		return;
	}

	if(direction == 'down') {
		if(noItem > 0) {	
			$('product_item_count_text').value = noItem - 1;
			noItem -= 1;
		} else if(noItem <= 0) {
			return;
		}
	} else if(direction == "up") {
			$('product_item_count_text').value = noItem + 1;
			noItem += 1;
	} else if(direction == 'number') {
	
	}
	var url = '/orders/addProduct/' + productId + '/' + sizeId + '/' + colorId + '/' + typeId + '/' + noItem;
	var myAjax = new Ajax.Request(
			url, 
			{
				method: 'post', 
				parameters: pars, 
				onComplete: checkResult
			});
}

function checkResult(request) {
	var value = request.responseText;

	if(value == 0 ) {
		$('product_status_id').innerHTML = '<h3><blink>Sorry, your order of this product now exceeds our stock quantity</blink></h3>';
		$('product_item_count_up').disabled = true;
		noItem = parseInt($('product_item_count_text').value);
		$('product_item_count_text').value = noItem - 1;
	} else {
		$('shopping_bag_items').innerHTML = value;
		$('product_status_id').innerHTML = '';
		$('product_item_count_up').disabled = false;
	}
}
