/**
 * Add a specified vehicle to the wishlist
 *
 * If a correctly formatted object is passed, it will (attempt to) add the
 * save & compare box thumbnail without reloading the page.
 *
 * @param car auto_car_detail.id or object
 */
function addToWishlist( car ) {
	if (scIsInList) {
		alert ('This vehicle is already in your wishlist.');
		return false;
	}
	var reloadRequired = true;
	var carId = '';
	if (car.id) {
		carId = car.id;

		// Attempt to add the save & compare box thumbnail without a reload
		var nextBoxId = 'scBox'+ scNextBox;
		if (($(nextBoxId) == undefined) || ($(nextBoxId) == null)) {
			reloadRequired = true;
		} else {
			var eLink = $$('#'+ nextBoxId +' a')[0];
			eLink.href = car.link;
			eLink.setStyle({ cursor: 'pointer' });

			var eImg = $$('#'+ nextBoxId +' a img')[0];
			eImg.src = car.image;
			eImg.setStyle({ width: car.imgWidth +'px', height: car.imgHeight +'px' });
			eImg.width = car.imgWidth;
			eImg.height = car.imgHeight;

			scIsInList = true;
			reloadRequired = false;
		}
	} else {
		carId = car;
		reloadRequired = true;
	}

	var target = baseHref + 'ajax.php';
	var params = 'type=addToWishList&carId=' + carId;
	var myAjax = new Ajax.Request(target,
	{
		method: 'post',
		parameters: params,
		onSuccess: function (request) {
			alert( request.responseText );
			scIsInList = true;
			if (reloadRequired) {
				location.reload(true);
			}
		},
		onFailure: function (request) {alert( request.responseText )}
	});
	return false;
}

/**
 * Remove vehicles from the wishlist.
 *
 * Pass 'all' as the carId to remove all vehicles from the wishlist.
 * @param carId auto_car._detail.id or 'all'
 */
function removeFromWishlist( carId ) {
	var target = baseHref + 'ajax.php';
	var params = 'type=removeFromWishList&carId=' + carId;

	var myAjax = new Ajax.Request(target,
	{
		method: 'post',
		parameters: params,
		onSuccess: function (request) {
			location.reload(true);
		},
		onFailure: function (request) {alert( request.responseText )}
	});
	return false;
}

