
/**
 * Author the request for adding quantity of product instances to the cart
 */
function getAddToCartString(prefix, ids) { 
	var currentField = null;
	var addToCartString = '';
	var quantity = null;
	var fieldId = null;
	for(var index = 0; index < ids.length; index++) {
		fieldId = prefix + ids[index];
		currentField = document.getElementById(prefix + ids[index]);
		if(currentField != undefined) { 
			quantity = currentField.value;
			if(quantity > 0) { 
				addToCartString += '&' + prefix + '[' + ids[index] + ']=' + quantity; 
			}
		}
	}
	return addToCartString;
}

/**
 * Validate that there is at least one product instance with a positive quantity
 */
function addToCartValidate(prefix, ids) {
    var currentField = null;
	var quantity = null;
	var fieldId = null;
	var quantityEntered = false;
	for(var index = 0; index < ids.length; index++) {
		fieldId = prefix + ids[index];
		currentField = document.getElementById(prefix + ids[index]);
		if(currentField != undefined) { 
			quantity = currentField.value;
			if(quantity > 0) { 
				quantityEntered = true; 
			}
		}
	}
	return quantityEntered;
}


function removeFromMyFavourites(id) { 
	updateMyFavouritesAction = 'index.php?do=popupMyFavouritesUpdate';
	window.open(updateMyFavouritesAction + '&removeProduct=' + id,'added','status=0,toolbar=0,location=0,directories=0');
}

function addToMyFavourites(id) { 
	updateMyFavouritesAction = 'index.php?do=popupMyFavouritesUpdate';
	window.open(updateMyFavouritesAction + '&addProduct=' + id,'added','status=0,toolbar=0,location=0,directories=0');
}

/**
 * Update the cart contents
 */
 /**
* Added thickbox instead of popup
* Added by paulm 200608
* Updated 20090217 by paulm
* altered resume if statement to solve IE problem
* added var declaration to resume statement
* fixed if statement for resume != undefined to work correctly in IE
*/
function addToCart(prefix, ids) {
    if(addToCartValidate(prefix, ids)) {
    	updateCartAction = 'index.php?do=popupPOProductUpdate';
    	var resume = document.getElementById('resume');
    	if(resume != undefined) {
			updateCartAction += '&resume=' + resume.value;
		}
		tb_show('', updateCartAction + getAddToCartString(prefix, ids) + '&KeepThis=true&TB_iframe=true&height=230&width=620&modal=true');
    } else {
        alert('You have not entered a valid quantity for this product');
    }
    
}

/**
 * Update the quantity for a product
 */
function updateQuantity(id, suffix) {
	var quantityInput = document.getElementById('updateProducts' + id + suffix);
	var removeCheckbox = document.getElementById('remove' + id + suffix);
	if(quantityInput != undefined && removeCheckbox != undefined) {
		if(quantityInput.value > 0) {
			removeCheckbox.checked = false;
		} else {
			removeCheckbox.checked = true;
		}
	}
}

/**
 * Remove the quantity for a product
 */
function removeQuantity(prefix, id, suffix) {
	var quantityInput = document.getElementById(prefix + id + suffix);
	var removeCheckbox = document.getElementById('remove' + id + suffix);
	if(quantityInput == undefined) alert('unable to find the quantityInput');
	if(removeCheckbox == undefined) alert('unable to find the removeCheckbox');
	if(quantityInput != undefined && removeCheckbox != undefined) {
		if(removeCheckbox.checked) {
			quantityInput.value = 0;
		}
	}
}


function removeFromMyFavourites(id) { 
	updateMyFavouritesAction = 'index.php?do=popupMyFavouritesUpdate';
	//window.open(updateMyFavouritesAction + '&removeProduct=' + id,'added','status=0,toolbar=0,location=0,directories=0');
	var faveRem = 1;
	tb_show('', updateMyFavouritesAction + ('&removeProduct=' + id) + '&KeepThis=true&TB_iframe=true&height=230&width=620&modal=true');
}

function addToMyFavourites(id) { 
	updateMyFavouritesAction = 'index.php?do=popupMyFavouritesUpdate';
	//window.open(updateMyFavouritesAction + '&addProduct=' + id,'added','status=0,toolbar=0,location=0,directories=0');
	var faveRem = 0;
	tb_show('', updateMyFavouritesAction + ('&addProduct=' + id) + '&KeepThis=true&TB_iframe=true&height=230&width=620&modal=true');
}



	