// javascript for products/ajax
// 

// base URL for amazon checkout
var baseCheckoutURL = "http://www.amazon.com/gp/aws/cart/add.html?AssociateTag=homeabroad-20&SubscriptionId=0GZZPSBSE80XVDEMWH02";


// suggest products for this mainInfo		
function suggestProducts(theMainInfoId){

	//alert('suggesting products for: ' + theMainInfoId);
	
	dojo.io.bind({
		url:'/viewProductWidgetContents.ha',
		content: { 
			mainInfoId: theMainInfoId					
		},
		method:'post',
		load: function(type, data, evt){
		  displaySuggestedProducts(data, theMainInfoId);
		},
		mimetype: "text/plain"
		});

}

// suggest products for this mainInfo		
function suggestProducts_prototype(theMainInfoId){

	//alert('suggesting products for: ' + theMainInfoId);
	
	new Ajax.Updater('prodWidget', '/viewProductWidgetContents.ha?mainInfoId='+theMainInfoId, { method: 'get' });
}

function displaySuggestedProducts(data, mainInfoId){

	alert('got suggested product data:' + data);
	var prodWidgElem = document.getElementById('prodWidget');
	if (prodWidgElem) {
		prodWidgElem.innerHTML = data;
	}

/* not necessary for PRE
	// update the 'view more products' link to this site
	var searchURL = "<a href='/preProds.ha?filterId=3&amp;mainInfoId=" + mainInfoId + "'>&raquo; View more products</a>";
	var searchElem = document.getElementById('moreProdsCol');
	if (searchElem) {
		//alert('setting search URL to:' + searchURL);
		//searchElem.innerHTML = searchURL;
	}
*/

}

// javaScript methods for adding/removing an item
// from the trip shopping cart

function addToCart(productASIN){

	//alert('adding to cart:' + productASIN);
	
	new Ajax.Request('/addToCart.ha', 
		{ method:'get', parameters: {cartProdASIN: productASIN},
		 onComplete: function(transport) { 
			
			new Ajax.Updater('cartTitle', '/refreshCartSize.ha', { method: 'get' });
			
            }
		}
	
	);
	
	
			// update cartAction to remove product if they want
			var removeAction ="<a style=\"float:right;\" href=\"javascript:void(0);\" onClick=\"removeFromCart('" + productASIN + "'); \">"
							+ "<span>Remove</span>"
							+"</a>";
			var cartActionElem = document.getElementById('cartItemAction.' + productASIN);
			if (cartActionElem) {
				//alert('setting html to:' + removeAction);
				cartActionElem.innerHTML = removeAction;
			}
	
            			
}

function addToCart_dojo(productASIN){

	//alert('adding to cart:' + productASIN);
	dojo.io.bind({
		url:'/addToCart.ha',
		content: { 
			cartProdASIN: productASIN
		},
		method:'post',
		load: function(type, data, evt){
		  displayCartMessage(data, productASIN);
		},
		mimetype: "text/plain"
		});

		// update cartAction to remove product if they want
		var removeAction ="<a style=\"float:right;\" href=\"javascript:void(0);\" onClick=\"removeFromCart('" + productASIN + "'); \">"
						+ "<span>Remove</span>"
						+"</a>";
		var cartActionElem = document.getElementById('cartItemAction.' + productASIN);
		if (cartActionElem) {
			//alert('setting html to:' + removeAction);
			cartActionElem.innerHTML = removeAction;
		}
            			
}

// change the buy message as a result of the ajax transaction
function displayCartMessage( productASIN){

	var cartMessageElem = document.getElementById('cartItemMessage.' + productASIN);
	if (cartMessageElem) {
		// per Dehru, turn off for 2.6
		//cartMessageElem.innerHTML = data;
		//dojo.fx.html.colorFadeIn(cartMessageElem, 1500, "yellow");
	}
	
	// now refresh the cart size, ajaxically
	displayCartSize();
	
}

function removeFromCart(productASIN){

	//alert('removing, using add image: ' + addImageURL);


	new Ajax.Request('/removeFromCart.ha', 
		{ method:'get', parameters: {cartProdASIN: productASIN},
		 onComplete: function(transport) { 
			
			new Ajax.Updater('cartTitle', '/refreshCartSize.ha', { method: 'get' });
			
            }
		}
	
	);
	
	// update cartAction to add product if they want
		var addAction ="<a style=\"float:right;\" href=\"javascript:void(0);\" onClick=\"addToCart('" + productASIN + "'); \">"
						+ "<span>Add To Cart</span>"
						+"</a>";
		var cartActionElem = document.getElementById('cartItemAction.' + productASIN);
		if (cartActionElem) {
			//alert('setting html to:' + addAction);
			cartActionElem.innerHTML = addAction;
		}

}

function removeFromCart_dojo(productASIN){

	//alert('removing, using add image: ' + addImageURL);

	dojo.io.bind({
		url:'/removeFromCart.ha',
		content: { 
			cartProdASIN: productASIN
		},
		method:'post',
		load: function(type, data, evt){
		  displayCartMessage(data, productASIN);
		},
		mimetype: "text/plain"
		});
		
		// update cartAction to add product if they want
		var addAction ="<a style=\"float:right;\" href=\"javascript:void(0);\" onClick=\"addToCart('" + productASIN + "'); \">"
						+ "<span>Add To Cart</span>"
						+"</a>";
		var cartActionElem = document.getElementById('cartItemAction.' + productASIN);
		if (cartActionElem) {
			//alert('setting html to:' + addAction);
			cartActionElem.innerHTML = addAction;
		}

}


// update the cart size on the top of the page
function displayCartSize() {

	alert('displaying cart sizey');
	
	new Ajax.Request('/refreshCartSize.ha', 
		{ method:'post', parameters: {mainInfoId: theMainInfoId},
		 onComplete: function(transport) { 
			new Ajax.Updater('cartTitle', '/refreshCartSize.ha', { method: 'get' });
			
            }
		}
	
	);
	}
		
function displayCartSize_dojo() {

	//alert('displaying cart size');
	dojo.io.bind({
		url:'/refreshCartSize.ha',
		content: { 
		},
		method:'post',
		load: function(type, data, evt){
		  displayCartTitle(data);
		},
		mimetype: "text/plain"
		});
}

// change the cart link as a result of the change in cart size
function displayCartTitle(){
	alert('setting cart title to:' + data);
	var cartElem = document.getElementById('cartTitle');
	if (cartElem) {
		cartElem.innerHTML = data;
		// doesn't work because cartTitle is a span
		// dojo blows up the HTML
		//dojo.fx.html.fadeShow(cartElem, 1000);
	}
}

// checout

function checkout(){
	var cartCheckoutURL =  baseCheckoutURL;
	alert(baseCheckoutURL);
	checkout(cartCheckoutURL);
}

function checkout(cartCheckoutURL) {
	// start here
	//var cartCheckoutURL =  baseCheckoutURL;
	
	
	// the form is 'amazonCart'
	// collect the ASINs and quantities
	
	var cartSizeElem = document.getElementById('cartSize');
	if (cartSizeElem) {
		var cartSize = cartSizeElem.value;	
		//alert('cartsize:' + cartSize);
		var i; 
		for (i=0; i<cartSize; i++) {
			// get this ASIN and qty...
			var itemASINElem = document.getElementById('cartItems[' + i + '].ASIN');
			var itemQtyElem = document.getElementById('cartItems[' + i + '].quantity');
			if (itemASINElem && itemQtyElem) {
				var amazonItemNbr = i + 1; // az is 1-based
				var itemURL = "&ASIN." + amazonItemNbr + "=" + itemASINElem.value
								+ "&Quantity." + amazonItemNbr + "=" + itemQtyElem.value;
					//			+ "&SellerId." + amazonItemNbr + "=ATVPDKIKX0DER";
				//alert('item url:' + itemURL);
				cartCheckoutURL += itemURL;
			}
			else {
				//if (!itemASINElem) alert('no asin for:' + i);
				//if (!itemQtyElem) alert('no qty for:' + i);
			}
		}
	}

	// show the popup
	//alert('checkout url:' + cartCheckoutURL);
	checkoutWindow(cartCheckoutURL);
	
	
}

// show the checkout window
function checkoutWindow(url) {
	var mapWin = window.open(url, 'amazonPopup', 'toolbar=yes,menubar=yes,resizable=yes,scrollbars=yes,status=yes,titlebar=yes,location=yes,height=500,width=750');
	if (window.focus) { mapWin.focus() }
}

function prodImageWindow(url) {

    prodWin = window.open("", "productImageWindow", 'menubar=no,resizable=yes,scrollbars=yes,status=no,titlebar=no,height=600,width=600');
    prodWin.document.write("<table width='100%'>");
        var closeCommand = "<a href='javascript:void(0);' onClick='window.close();'>Close</a>";
        prodWin.document.write("<tr><td style='text-align:center;'>" + closeCommand + "</td></tr>");
        prodWin.document.write("<tr><td style='text-align:center;'><img onClick='window.close();' border='0' src='" + url + "'></td></tr>");
        prodWin.document.write("<tr><td style='text-align:center;'>" + closeCommand + "</td></tr>");
    prodWin.document.write("</table>");
    prodWin.document.bgColor="lightblue"

    prodWin.document.close();

}

