amazon.js
Summary
Zapatec Amazon Shop application
Copyright (c) 2004-2009 by Zapatec, Inc.
http://www.zapatec.com
1700 MLK Way, Berkeley, California,
94709, U.S.A.
All rights reserved.
Zapatec.Shop = function() {
this.objSearchInput = null;
this.objSearchIndex = null;
this.objContainer = null;
this.itemGrid = null;
this.cartGrid = null;
this.totalResults = 0;
this.cartId = null;
this.cartHmac = null;
this.cartWin = null;
};
Zapatec.Shop.NO_IMAGE_URL = 'images/no-img-small.gif';
Zapatec.Shop.NO_IMAGE_BIG_URL = 'http://ec3.images-amazon.com/images/G/01/nav2/dp/no-image-avail-img-map.gif';
Zapatec.Shop.STARS_URL = new Array();
Zapatec.Shop.STARS_URL[0] = "images/stars/stars-0-0.gif";
Zapatec.Shop.STARS_URL[5] = "images/stars/stars-0-5.gif";
Zapatec.Shop.STARS_URL[10] = "images/stars/stars-1-0.gif";
Zapatec.Shop.STARS_URL[15] = "images/stars/stars-1-5.gif";
Zapatec.Shop.STARS_URL[20] = "images/stars/stars-2-0.gif";
Zapatec.Shop.STARS_URL[25] = "images/stars/stars-2-5.gif";
Zapatec.Shop.STARS_URL[30] = "images/stars/stars-3-0.gif";
Zapatec.Shop.STARS_URL[35] = "images/stars/stars-3-5.gif";
Zapatec.Shop.STARS_URL[40] = "images/stars/stars-4-0.gif";
Zapatec.Shop.STARS_URL[45] = "images/stars/stars-4-5.gif";
Zapatec.Shop.STARS_URL[50] = "images/stars/stars-5-0.gif";
Zapatec.Shop.MAX_SMALL_WIDTH = 50;
Zapatec.Shop.MAX_SMALL_HEIGHT = 75;
Zapatec.Shop.MAX_LARGE_WIDTH = 250;
Zapatec.Shop.MAX_LARGE_HEIGHT = 200;
Zapatec.Shop.objShowCart = null;
Zapatec.Shop.lastShoppingCartXml = null;
Zapatec.Shop.lastResponsePageIndex = 1;
Zapatec.Shop.maxSearchRequestCount = 10;
Zapatec.Shop.searchRequestCount = 0;
Zapatec.Shop.resultMaxPrice = null;
Zapatec.Shop.resultMinPrice = null;
Zapatec.Shop.FilterByPrice = function(minv,maxv){
Zapatec.Shop.PriceSliderFilterLegend(minv,maxv);
if(Zapatec.Shop.itemGrid){
Zapatec.Shop.itemGrid.limitRange({
column: 8,
min: minv,
max: maxv
});
}
}
Zapatec.Shop.PriceSliderFilterLegend = function(minv,maxv){
myminv = parseInt(minv*100)/100;
mymaxv = parseInt(maxv*100)/100;
if(document.getElementById('price_slider_legend').childNodes.length==0){
document.getElementById('price_slider_legend').appendChild(document.createTextNode(''));
}
document.getElementById('price_slider_legend').childNodes[0].nodeValue = '$'+myminv.toString()+' to '+'$'+mymaxv.toString();
}
Zapatec.Shop.FilterByACR = function(minv,maxv){
Zapatec.Shop.ACRSliderLegend(minv,maxv);
if(Zapatec.Shop.itemGrid){
Zapatec.Shop.itemGrid.limitRange({
column: 12,
min: minv,
max: maxv
});
}
}
Zapatec.Shop.ACRSliderLegend = function(minv,maxv){
if(document.getElementById('acr_slider_legend').childNodes.length==0){
document.getElementById('acr_slider_legend').appendChild(document.createTextNode(''));
}
document.getElementById('acr_slider_legend').childNodes[0].nodeValue = minv.toString()+' stars to '+maxv.toString()+' stars';
}
window.onload = function() {
document.getElementById('AmazonSearchForm').onsubmit = function(){ return Zapatec.Shop.doSearch(); };
document.getElementById('searchInput').focus();
objSearchInput = document.getElementById('searchInput');
objSearchIndex = document.getElementById('searchIndex');
objContainer = document.getElementById('searchOutput');
Zapatec.Shop.objShowCart = document.getElementById('showCart');
Zapatec.Shop.objShowCart.onclick = Zapatec.Shop.showShoppingCartWindow;
Zapatec.Utils.warnUnload('Your search and shopping results will be lost');
Zapatec.Shop.PriceSliderFilter = new Zapatec.Slider({
parent : document.getElementById('price_slider'),
range : [0,1],
length : 100,
orientation : 'H',
dual : true,
eventListeners: {
newPosition : Zapatec.Shop.FilterByPrice
}
});
Zapatec.Shop.ACRSliderFilter = new Zapatec.Slider({
parent : document.getElementById('acr_slider'),
range : [0,5],
length : 100,
orientation : 'H',
dual : true,
eventListeners: {
newPosition : Zapatec.Shop.FilterByACR
}
});
document.getElementById('dvControls').style.display = 'none';
}
Zapatec.Shop.searchCallbackHeader = function(objGrid) {
var html = [];
html.push('<div class="zpHeader">');
html.push('<div class="zpSortingText">');
html.push('Sort by: ');
var arrFields = objGrid.getFields();
for (var iCol = 1; iCol < arrFields.length; iCol++) {
var objField = arrFields[iCol];
if(objField.nosort){ continue; }
html.push('<span class="zpHeaderColumn" style="');
html.push(objGrid.getFieldStyle(objField));
html.push('" onclick="');
html.push(objGrid.getFieldOnclick(objField));
html.push('">');
html.push(objGrid.getFieldTitle(objField));
html.push('</span>');
}
html.push('</div>');
html.push('<div class="zpSearchCompare"><input type="button" value="Compare" onclick="Zapatec.Shop.compareSelectedItems()" /></div>');
html.push('</div><br/>');
objContainer.innerHTML = html.join('');
};
var CELL_INDEX_ID = 0;
var CELL_INDEX_IMAGE = 1;
var CELL_INDEX_TITLE = 2;
var CELL_INDEX_AUTHOR = 3;
var CELL_INDEX_BINDING = 4;
var CELL_INDEX_PUBLICATION_DATE = 5;
var CELL_INDEX_PUBLISHER = 6;
var CELL_INDEX_ISBN = 7;
var CELL_INDEX_LIST_PRICE = 8;
var CELL_INDEX_LIST_PRICEFORMATTED = 9;
var CELL_INDEX_AMAZON_OFFER_PRICE = 10;
var CELL_INDEX_LOWEST_USED_PRICE = 11;
var CELL_INDEX_ACR = 12;
Zapatec.Shop.searchCallbackRow = function(objGrid, objRow) {
var output = '<div class="zpRow">';
output += '<div class="zpRowData">';
var arrCells = objGrid.getRowCells(objRow);
var isNoListPrice = false;
var isNoAmazonOfferPrice = false;
var isNoLowestNewPrice = false;
var isNoLowestUsedPrice = false;
for(var iCol = 0; iCol < arrCells.length; iCol++){
var itemHTML = '';
var objCell = arrCells[iCol];
var isUseAnchor = CELL_INDEX_TITLE == iCol || CELL_INDEX_IMAGE == iCol;
if(isUseAnchor){
var params = "'" + objRow.asin + "', '" + objRow.searchIndex + "'";
itemHTML += '<a href="#" onclick="return Zapatec.Shop.onTitleClick(' + params + ');"';
}else{
itemHTML += '<span';
}
itemHTML += (objRow.cells[iCol].className) ? ' class="'+objRow.cells[iCol].className+'"' : '';
itemHTML += '>';
var data = objGrid.getCellValueString(objCell);
if (CELL_INDEX_ID == iCol || CELL_INDEX_PUBLISHER == iCol || iCol == CELL_INDEX_LIST_PRICE) {
continue;
}else if(CELL_INDEX_IMAGE == iCol ){
var imgUrl = data;
var imgHtml = Zapatec.Shop.getFitImageHtml(imgUrl,objRow.smallWidth, objRow.smallHeight,Zapatec.Shop.MAX_SMALL_WIDTH, Zapatec.Shop.MAX_SMALL_HEIGHT,'zpSearchImage');
itemHTML += imgHtml;
}else if (iCol == CELL_INDEX_AUTHOR){
if("null" != data){ itemHTML += " by "+data; }
}else if (iCol == CELL_INDEX_ISBN){
continue;
}else if(iCol == CELL_INDEX_BINDING){
var bindingAndDate = "";
if ("null" != data) {
bindingAndDate += '<span class="zpBindingAndDate">(';
bindingAndDate += '<span class="zpBinding">' + data + "</span>";
var objPublicationDateCell = arrCells[iCol+1];
var publicationDateData = objGrid.getCellValueString(objPublicationDateCell);
if("null" != publicationDateData){ bindingAndDate += " - "+publicationDateData; }
bindingAndDate += ')</span>';
itemHTML += bindingAndDate;
}
}else if(iCol == CELL_INDEX_PUBLICATION_DATE){
continue;
}else if(iCol == CELL_INDEX_LOWEST_USED_PRICE){
if ("null" != data && "undefined" != data) {
itemHTML += "Used: "+data;
}else{
isNoLowestUsedPrice = true;
if(isNoAmazonOfferPrice){ itemHTML += "Currently unavailable<br/>"; }
}
}else if(iCol == CELL_INDEX_AMAZON_OFFER_PRICE){
if("null" != data && "undefined" != data){ itemHTML += data; }
else{ isNoAmazonOfferPrice = true; }
}else if(iCol == CELL_INDEX_LIST_PRICEFORMATTED){
if ("null" != data && "undefined" != data) { itemHTML += data; }
else{ isNoListPrice = true; }
}else if(iCol == CELL_INDEX_ACR){
var indexStars = parseInt(parseFloat(data)*10);
itemHTML += Zapatec.Shop.getFitImageHtml(Zapatec.Shop.STARS_URL[indexStars],64,12,64,12,'zpStars');
}else{
if("null" != data && "undefined" != data){ itemHTML += data; }
}
itemHTML += (isUseAnchor===true) ? '</a>': '</span>';
if ("null" != data) {
if (iCol == CELL_INDEX_PUBLISHER || iCol == CELL_INDEX_ISBN || iCol == CELL_INDEX_AMAZON_OFFER_PRICE || iCol == CELL_INDEX_BINDING) {
itemHTML += '<br/>';
}
}
output += itemHTML;
}
output += '</div>';
output += '<div class="zpRowCompare"><input type="checkbox" onclick="Zapatec.Shop.selectToCompare.call(this,['+objGrid.getRowId(objRow)+'])" class="zpSearchCompareCheckbox" /></div>';
output += '</div>';
output += '<div class="clearer"></div>';
output += '<br/>';
objContainer.innerHTML = objContainer.innerHTML + output;
};
Zapatec.Shop.oGridCompareConfig = {
source: {
fields: [
{hidden: true},
{title: "Image", style:'font-weight:bold;'},
{title: "Title", style:'font-weight:bold;'},
{title: "Author", style:'font-weight:bold;'},
{title: "Binding", style:'font-weight:bold;'},
{title: "PublicationDate", style:'font-weight:bold;'},
{hidden: true},
{title: "ISBN", style:'font-weight:bold;'},
{hidden: true},
{title: "List Price", style:'font-weight:bold;'},
{title: "Lowest Price", style:'font-weight:bold;'},
{title: "Lowest Used Price", style:'font-weight:bold;'},
{title: "Average Customer Review", style:'font-weight:bold;'}
]
},
sourceType: 'json',
horizontal: true,
fixedLeft: 1,
theme: 'winxp',
headerContainer: 'gridCompareHeaderContainer',
container: 'gridCompareDataContainer',
selectCells: false,
selectRows: false,
activeCells: false,
activeRows: false,
eventListeners: {
gridModified: function(){
if(Zapatec.Shop.oWinCompare) Zapatec.Shop.oWinCompare.show();
var rows = this.getRows(), imgCell = null, titleCell = null, linkTag = '';
for(var i=0;i<rows.length;i++){
var params = "'" + rows[i].asin + "', '" + rows[i].searchIndex + "'";
linkTag += '<a href="#" onclick="return Zapatec.Shop.onTitleClick(' + params + ');">';
imgCell = this.getCellByRow(rows[i],1);
this.setCellValue(imgCell,linkTag+'<img src="'+this.getCellValue(imgCell)+'"></a>');
titleCell = this.getCellByRow(rows[i],2);
this.setCellValue(titleCell,linkTag+this.getCellValue(titleCell)+'"</a>');
this.setCellStyle(this.getCellByRow(rows[i],2),"background-color:#EAEBDB;");
}
rows = null;
cell = null;
},
gridRefreshed: function(){
var oContent = document.getElementById('gridCompareContainer');
var oContainer = oContent.parentNode;
var oGridHeader = document.getElementById('gridCompareHeaderContainer');
var oGridData = document.getElementById('gridCompareDataContainer');
oContainer.style.overflow = 'hidden';
var oOffset = Zapatec.Utils.getElementOffset(oContainer);
var iHeight = oOffset.height - oGridHeader.offsetHeight;
var iWidth = oOffset.width;
oGridData.style.height = iHeight + 'px';
oGridData.style.width = iWidth + 'px';
oGridHeader.style.width = oGridData.clientWidth + 'px';
var oDims = this.getGridDimensions();
if(oDims.width && oDims.height){
oGridData.style.overflow = 'scroll';
oGridData.style.overflowX = (oDims.width < iWidth) ? 'hidden' : 'scroll';
oGridData.style.overflowY = (oDims.height < iHeight) ? 'hidden' : 'scroll';
}else{
oGridData.style.overflow = 'scroll';
oGridData.style.overflowX = 'scroll';
oGridData.style.overflowY = 'scroll';
}
}
}
}
Zapatec.Shop.oWinCompare = Zapatec.Window.setup({
width: 470,
height: 470,
left: 200,
top: 100,
divContent: 'gridCompareContainer',
title: 'Compare Selected Rows',
initialState: 'hidden',
hideOnClose: true,
fixed: true
});
Zapatec.Shop.selectToCompare = function(rowId){
var oRow = Zapatec.Shop.itemGrid.getRowById(rowId);
if(this.checked) Zapatec.Shop.itemGrid.selectRow(oRow);
else Zapatec.Shop.itemGrid.unselectRow(oRow);
}
Zapatec.Shop.compareSelectedItems = function(){
if(Zapatec.Shop.itemGrid && Zapatec.Shop.itemGrid.getSelectedRows().length>1){
if(typeof(Zapatec.Shop.oGridCompare)==='undefined'){
Zapatec.Shop.oGridCompare = new Zapatec.Grid(Zapatec.Shop.oGridCompareConfig);
}
Zapatec.Shop.itemGrid.editSelectedRows(Zapatec.Shop.oGridCompare);
}else if(Zapatec.Shop.itemGrid.getSelectedRows().length<=1){
alert('You must choose 2 or more items to compare');
}
return;
}
Zapatec.Shop.onPaginationSliderMove = function(iValue) {
Zapatec.Shop.itemGrid.gotoPage(Math.ceil(iValue));
};
Zapatec.Shop.searchCallbackPagination = function(objGrid) {
var iPages = Zapatec.Shop.itemGrid.totalPages();
if(iPages < 2){
document.getElementById('zpShopGridPagination').style.display = 'none';
return;
}else{
document.getElementById('zpShopGridPagination').style.display = '';
}
var iCurrentPage = Zapatec.Shop.itemGrid.getCurrentPageNumber();
document.getElementById('zpShopGridPaginationContainer').innerHTML = 'Page ' + iCurrentPage + ' of ' + iPages + ' : ';
if (Zapatec.Shop.paginationSlider) {
var aSliderRange = Zapatec.Shop.paginationSlider.config.range;
if (!(aSliderRange instanceof Array) || aSliderRange[1] != iPages - 1) {
Zapatec.Shop.paginationSlider.setRange(0, iPages - 1);
}
var iSliderPos = Zapatec.Shop.paginationSlider.getPos().first;
if(!isNaN(iSliderPos)){
iCurrentPage--;
if(iCurrentPage != iSliderPos){
Zapatec.Shop.paginationSlider.setPos(iCurrentPage);
}
}
}else{
Zapatec.Shop.paginationSlider = new Zapatec.Slider({
div: 'zpShopGridPaginationSlider',
length: 99,
orientation: 'H',
step: 1,
range: [0, iPages - 1],
newPosition: Zapatec.Shop.onPaginationSliderMove
});
}
var aSelectd = ['', '', '', ''];
var oConfig = Zapatec.Shop.itemGrid.getConfiguration();
if(oConfig.rowsPerPage == 5){
aSelectd[0] = ' selected';
}else if(oConfig.rowsPerPage == 15) {
aSelectd[2] = ' selected';
}else if(oConfig.rowsPerPage == 20) {
aSelectd[3] = ' selected';
}else{
aSelectd[1] = ' selected';
}
document.getElementById('zpShopGridPaginationPerPage').innerHTML =
'<select onclick="Zapatec.Shop.setResultsPerPage(\
this[this.selectedIndex].value)">\
<option value="5"' + aSelectd[0] + '>5</option>\
<option value="10"' + aSelectd[1] + '>10</option>\
<option value="15"' + aSelectd[2] + '>15</option>\
<option value="20"' + aSelectd[3] + '>20</option>\
</select>';
};
Zapatec.Shop.setResultsPerPage = function(iRowsPerPage) {
var oConfig = Zapatec.Shop.itemGrid.getConfiguration();
if (oConfig.rowsPerPage == iRowsPerPage) { return; }
Zapatec.Shop.itemGrid.reconfigure({
rowsPerPage: iRowsPerPage
});
Zapatec.Shop.itemGrid.gotoPage(0);
};
Zapatec.Shop.next = function(iGridId) {
Zapatec.Grid.nextPage(iGridId);
this.showResults(this.totalResults,this.itemGrid.currentPage,this.itemGrid.config.rowsPerPage);
}
Zapatec.Shop.previous = function(iGridId) {
Zapatec.Grid.previousPage(iGridId);
this.showResults(this.totalResults,this.itemGrid.currentPage,this.itemGrid.config.rowsPerPage);
}
Zapatec.Shop.createItemGrid = function(objXmlSource) {
var objJsonGrid = [];
var rowsPerPage = 0;
if(objXmlSource) {
objJsonGrid = this.convertResultsXmlToGrid(objXmlSource);
rowsPerPage = objXmlSource.rowsPerPage;
Zapatec.Shop.itemGrid = new Zapatec.Grid({
source: objJsonGrid,
sourceType: 'json',
callbackHeaderDisplay: Zapatec.Shop.searchCallbackHeader,
callbackRowDisplay: Zapatec.Shop.searchCallbackRow,
callbackPaginationDisplay: Zapatec.Shop.searchCallbackPagination,
theme: "winxp",
rowsPerPage: rowsPerPage
});
document.getElementById('dvControls').style.display = 'block';
}
}
Zapatec.Shop.showLoadingMark = function(isVisible) {
document.getElementById('loading').style.display = (isVisible===true) ? 'inline' : 'none';
}
Zapatec.Shop.showShoppingCartWindow = function() {
Zapatec.Shop.objShowCart.style.display = "none";
if(this.cartWin===null || typeof(this.cartWin)=='undefined'){
Zapatec.Shop.cartWin = Zapatec.Shop.createWindow(true,function(win){
Zapatec.Shop.cartWin = null;
Zapatec.Shop.objShowCart.style.display = "inline";
});
Zapatec.Shop.cartWin.setTitle('Shopping Cart');
}
if (Zapatec.Shop.lastShoppingCartXml!==null){
Zapatec.Shop.showCartContent(Zapatec.Shop.lastShoppingCartXml);
}
return false;
}
Zapatec.Shop.getSearchDomain = function(page) {
var searchIndex = objSearchIndex.selectedIndex;
switch(objSearchIndex.selectedIndex){
case 0: return 'Books'; break;
case 1: return 'Music'; break;
case 2: return 'PCHardware'; break;
case 3: return 'Blended'; break;
}
return 'Books';
}
Zapatec.Shop.getSearchUrl = function(page) {
var searchIndex = Zapatec.Shop.getSearchDomain();
var url = 'proxy.php?Keywords=' + objSearchInput.value + '&SearchIndex=' + searchIndex;
return !isNaN(page) ? url+'&Page='+page : url;
}
Zapatec.Shop.doSearch = function() {
Zapatec.Shop.resultMaxPrice = null;
Zapatec.Shop.resultMinPrice = null;
Zapatec.Shop.PriceSliderFilterLegend(0,1);
Zapatec.Shop.ACRSliderLegend(0,5);
Zapatec.Shop.PriceSliderFilter.reset(0,1);
Zapatec.Shop.ACRSliderFilter.reset(0,5);
document.getElementById('dvControls').style.display = 'none';
Zapatec.Shop.searchRequestCount = 1;
Zapatec.Shop.fetchSearch(1);
return false;
};
Zapatec.Shop.fetchSearch = function(page) {
this.showLoadingMark(true);
if(1 == page){ Zapatec.Shop.createItemGrid(); }
var url = Zapatec.Shop.getSearchUrl(page);
Zapatec.Transport.fetchXmlDoc({
url: url,
onLoad: function(objXmlDoc) {
var objXmlSource = Zapatec.Shop.parseItemXmlResponse(objXmlDoc, false);
if(1 < page){
var objJsonGridData = Zapatec.Shop.convertResultsXmlToGrid(objXmlSource);
var pageNumber = Zapatec.Shop.itemGrid.getCurrentPageNumber();
Zapatec.Shop.itemGrid.splice({
rows: objJsonGridData.rows
});
Zapatec.Shop.itemGrid.setCurrentPage(pageNumber -1);
}else{
Zapatec.Shop.createItemGrid(objXmlSource);
}
if(Zapatec.Shop.searchRequestCount < Zapatec.Shop.maxSearchRequestCount){
Zapatec.Shop.searchRequestCount++;
setTimeout(function(){ Zapatec.Shop.fetchSearch(page +1); },1000);
if(Zapatec.Shop.resultMinPrice!==null && Zapatec.Shop.resultMaxPrice!==null){
Zapatec.Shop.PriceSliderFilter.reset(Zapatec.Shop.resultMinPrice,Zapatec.Shop.resultMaxPrice);
Zapatec.Shop.PriceSliderFilterLegend(Zapatec.Shop.resultMinPrice,Zapatec.Shop.resultMaxPrice);
}else{
document.getElementById('price_filter').style.display = 'none';
}
}
}
});
}
Zapatec.Shop.showResults = function(totalResults, currentPage, rowsPerPage ) {
var strTotalResults = totalResults;
var strFirstResPos = currentPage*rowsPerPage +1;
var iLastResPos = strFirstResPos * 1 + rowsPerPage * 1 - 1;
document.getElementById('results').innerHTML = 'Results ' + strFirstResPos + ' - ' + iLastResPos +' of about ' + strTotalResults;
}
Zapatec.Shop.convertResultsXmlToGrid = function(objXmlSource) {
var objJsonGrid = {
fields: [
{hidden: true},
{title: "Image", dataType: "string", nosort: true},
{title: "Title", dataType: "string", className: "zpTitle"},
{title: "Author", dataType: "string", className: "zpAuthor"},
{title: "Binding", dataType: "string", className: "zpBinding"},
{title: "PublicationDate", dataType: "string"},
{hidden: true},
{title: "ISBN", dataType: "string", className: "zpIsbn"},
{hidden: true},
{title: "List Price", dataType: "string", className: "zpListPrice"},
{title: "Lowest Price", dataType: "string"},
{title: "Lowest Used Price", dataType: "string"},
{title: "Average Customer Review", dataType: "string"}
],
rows: []
};
var rowId = objXmlSource.itemPage * objXmlSource.rowsPerPage;
for (var index = 0; index < objXmlSource.length; ++index) {
var item = objXmlSource[index];
var imgUrl = item.smallImageUrl;
if (!imgUrl){ imgUrl = Zapatec.Shop.NO_IMAGE_URL; }
var byString = null;
if (item.authors) {
var author = null;
for (var iAuthor = 0; iAuthor < item.authors.length; ++iAuthor) {
var currentAuthor = item.authors[iAuthor];
if(null == author){ author = currentAuthor; }
else{ author += " and " + currentAuthor; }
}
byString = author;
}else if(item.manufacturer){
byString = item.manufacturer;
}
var binding = null;
if(item.binding){ binding = item.binding; }
var publicationDate = null;
if(item.publicationDate){ publicationDate = item.publicationDate; }
var publisher = null;
if (item.publisher) {
publisher = item.publisher;
}else if (item.manufacturer) {
publisher = item.manufacturer;
}
var newPrice = null;
if (item.offers && 0 < item.offers.length) {
if (-1 != item.offers[0].merchantName.indexOf('Amazon')) {
newPrice = item.offers[0].price;
}
}
objJsonGrid.rows.push({
cells:[
{v: rowId},
{v: imgUrl},
{v: item.title, className: "zpTitle"},
{v: byString, className: "zpAuthor"},
{v: binding},
{v: publicationDate},
{v: publisher},
{v: item.isbn, className: "zpIsbn"},
{v: item.filterPrice },
{v: item.listPrice, className: "zpListPrice"},
{v: newPrice, className: "zpLowestPrice"},
{v: item.lowestUsedPrice, className: "zpLowestUsedPrice"},
{v: item.customerReviewsAverageRating }
],
asin: item.asin,
offerId: item.offerId,
searchIndex: objXmlSource.searchIndex,
productGroup: item.productGroup,
manufacturer: item.manufacturer,
smallWidth: item.smallImageWidth,
smallHeight: item.smallImageHeight
});
rowId++;
}
return objJsonGrid;
}
Zapatec.Shop.parseItemXmlResponse = function(objXmlDoc, isDetails) {
this.showLoadingMark(false);
var arrItems = [];
if(objXmlDoc && objXmlDoc.documentElement){
var items = objXmlDoc.documentElement.getElementsByTagName('Items');
var responseKeywords = null;
var itemPage = null;
var responseSearchIndex = null;
var totalResults = items[0].getElementsByTagName('TotalResults');
if(totalResults && totalResults.length){ this.totalResults = totalResults[0].firstChild.nodeValue; }
var requests = items[0].getElementsByTagName('Request');
if (requests && requests.length){
var searchRequests = requests[0].getElementsByTagName('ItemSearchRequest');
if (searchRequests && searchRequests.length) {
var itemPages = searchRequests[0].getElementsByTagName('ItemPage');
if (itemPages && itemPages.length) {
itemPage = itemPages[0].firstChild.nodeValue;
}
var keywords = searchRequests[0].getElementsByTagName('Keywords');
if (keywords && keywords.length) {
responseKeywords = keywords[0].firstChild.nodeValue;
}
var searchIndexes = searchRequests[0].getElementsByTagName('SearchIndex');
if (searchIndexes && searchIndexes.length) {
responseSearchIndex = searchIndexes[0].firstChild.nodeValue;
}
}
}
var rowsPerPage = 10;
if (null != rowsPerPage && "" != rowsPerPage) {
arrItems.rowsPerPage = parseInt(rowsPerPage);
}
arrItems.totalRows = parseInt(this.totalResults);
arrItems.displayedRows = parseInt(this.totalResults);
arrItems.itemPage = parseInt(itemPage)-1;
arrItems.keywords = responseKeywords;
arrItems.searchIndex = responseSearchIndex;
Zapatec.Shop.lastResponsePageIndex = arrItems.itemPage+1;
var arrItem = items[0].getElementsByTagName('Item');
if (false == isDetails && 1 == itemPage) {
this.showResults(this.totalResults, arrItems.itemPage, arrItems.rowsPerPage);
}
if (arrItem && arrItem.length) {
for (var iItem = 0; iItem < arrItem.length; iItem++) {
var item = arrItem[iItem];
var objItem = {};
var asins = item.getElementsByTagName('ASIN');
if (asins && asins.length) {
objItem.asin = asins[0].firstChild.nodeValue;
}
var smallImage = this.parseImageXmlResponse(item, 'SmallImage');
objItem.smallImageUrl = smallImage.url;
objItem.smallImageWidth = smallImage.width;
objItem.smallImageHeight = smallImage.height;
var mediumImage = this.parseImageXmlResponse(item, 'MediumImage');
objItem.mediumImageUrl = mediumImage.url;
objItem.mediumImageWidth = mediumImage.width;
objItem.mediumImageHeight = mediumImage.height;
var largeImage = this.parseImageXmlResponse(item, 'LargeImage');
objItem.largeImageUrl = largeImage.url;
objItem.largeImageWidth = largeImage.width;
objItem.largeImageHeight = largeImage.height;
var itemAttributes = item.getElementsByTagName('ItemAttributes');
if (itemAttributes && itemAttributes.length) {
var titles = itemAttributes[0].getElementsByTagName('Title');
if (titles && titles.length) {
objItem.title = titles[0].firstChild.nodeValue;
}
var bindings = itemAttributes[0].getElementsByTagName('Binding');
if (bindings && bindings.length) {
objItem.binding = bindings[0].firstChild.nodeValue;
}
var publicationDates = itemAttributes[0].getElementsByTagName('PublicationDate');
if (publicationDates && publicationDates.length) {
objItem.publicationDate = publicationDates[0].firstChild.nodeValue;
}
var editions = itemAttributes[0].getElementsByTagName('Edition');
if (editions && editions.length) {
objItem.edition = editions[0].firstChild.nodeValue;
}
var numberPages = itemAttributes[0].getElementsByTagName('NumberOfPages');
if (numberPages && numberPages.length) {
objItem.numberOfPages = numberPages[0].firstChild.nodeValue;
}
var authors = itemAttributes[0].getElementsByTagName('Author');
if (authors && authors.length) {
objItem.authors = [];
for (var authorIndex = 0; authorIndex < authors.length; ++authorIndex) {
var author = authors[authorIndex].firstChild.nodeValue;
objItem.authors.push(author);
}
}
var publishers = itemAttributes[0].getElementsByTagName('Publisher');
if (publishers && publishers.length) {
objItem.publisher = publishers[0].firstChild.nodeValue;
}
var manufacturers = itemAttributes[0].getElementsByTagName('Manufacturer');
if (manufacturers && manufacturers.length) {
objItem.manufacturer = manufacturers[0].firstChild.nodeValue;
}
var productGroups = itemAttributes[0].getElementsByTagName('ProductGroup');
if (productGroups && productGroups.length) {
objItem.productGroup = productGroups[0].firstChild.nodeValue;
}
var isbns = itemAttributes[0].getElementsByTagName('ISBN');
if (isbns && isbns.length) {
objItem.isbn = isbns[0].firstChild.nodeValue;
}
var listPrices = itemAttributes[0].getElementsByTagName('ListPrice');
filterPrice = null;
if (listPrices && listPrices.length) {
var formated = listPrices[0].getElementsByTagName('FormattedPrice');
if (formated && formated.length) {
objItem.listPrice = formated[0].firstChild.nodeValue;
filterPrice = parseFloat(objItem.listPrice.toString().slice(1));
}
}
if(filterPrice!==null){
objItem.filterPrice = filterPrice;
Zapatec.Shop.resultMinPrice = (Zapatec.Shop.resultMinPrice===null) ? objItem.filterPrice : (Zapatec.Shop.resultMinPrice>objItem.filterPrice ? objItem.filterPrice : Zapatec.Shop.resultMinPrice);
Zapatec.Shop.resultMaxPrice = (Zapatec.Shop.resultMaxPrice===null) ? objItem.filterPrice : (Zapatec.Shop.resultMaxPrice<objItem.filterPrice ? objItem.filterPrice : Zapatec.Shop.resultMaxPrice);
}else{
objItem.filterPrice = 0;
}
var packageDimensions = itemAttributes[0].getElementsByTagName('PackageDimensions');
if (packageDimensions && packageDimensions.length) {
var widths = packageDimensions[0].getElementsByTagName('Width');
if (widths && widths.length) {
objItem.packageWidth = widths[0].firstChild.nodeValue;
objItem.packageWidthUnits = widths[0].getAttribute('Units');
}
var heights = packageDimensions[0].getElementsByTagName('Height');
if (heights && heights.length) {
objItem.packageHeight = heights[0].firstChild.nodeValue;
objItem.packageHeightUnits = heights[0].getAttribute('Units');
}
var lengths = packageDimensions[0].getElementsByTagName('Length');
if (lengths && lengths.length) {
objItem.packageLength = lengths[0].firstChild.nodeValue;
objItem.packageLengthUnits = lengths[0].getAttribute('Units');
}
var weights = packageDimensions[0].getElementsByTagName('Weight');
if (weights && weights.length) {
objItem.packageWeight = weights[0].firstChild.nodeValue;
objItem.packageWeightUnits = weights[0].getAttribute('Units');
}
}
}
var offerSummaries = item.getElementsByTagName('OfferSummary');
if (offerSummaries && offerSummaries.length) {
var lowestNewPrices = offerSummaries[0].getElementsByTagName('LowestNewPrice');
if (lowestNewPrices && lowestNewPrices.length) {
var formated = lowestNewPrices[0].getElementsByTagName('FormattedPrice');
if (formated && formated.length) {
objItem.lowestNewPrice = formated[0].firstChild.nodeValue;
}
}
var lowestUsedPrices = offerSummaries[0].getElementsByTagName('LowestUsedPrice');
if (lowestUsedPrices && lowestUsedPrices.length) {
var formated = lowestUsedPrices[0].getElementsByTagName('FormattedPrice');
if (formated && formated.length) {
objItem.lowestUsedPrice = formated[0].firstChild.nodeValue;
}
}
}
arrItems.push(objItem);
var editorialReviewsTag = item.getElementsByTagName('EditorialReviews');
if (editorialReviewsTag && editorialReviewsTag.length) {
var editorialReviews = editorialReviewsTag[0].getElementsByTagName('EditorialReview');
if (editorialReviews && editorialReviews.length){
var arrReviews = [];
for (var iReview = 0; iReview < editorialReviews.length; iReview++) {
var review = editorialReviews[iReview];
var objReview = {};
var source = review.getElementsByTagName('Source');
if (source && source.length) {
objReview.source = source[0].firstChild.nodeValue;
}
var content = review.getElementsByTagName('Content');
if (content && content.length) {
objReview.content = content[0].firstChild.nodeValue;
}
arrReviews.push(objReview);
}
objItem.editorialReviews = arrReviews;
}
}
objItem.customerReviewsAverageRating = 0;
var customerReviewsTag = item.getElementsByTagName('CustomerReviews');
if (customerReviewsTag && customerReviewsTag.length) {
var average = customerReviewsTag[0].getElementsByTagName('AverageRating');
if (average && average.length) {
objItem.customerReviewsAverageRating = average[0].firstChild.nodeValue;
}
var reviews = customerReviewsTag[0].getElementsByTagName('Review');
if (reviews && reviews.length) {
var arrReviews = [];
for (var iReview = 0; iReview < reviews.length; iReview++) {
var review = reviews[iReview];
var objReview = {};
var summary = review.getElementsByTagName('Summary');
if (summary && summary.length) {
objReview.summary = summary[0].firstChild.nodeValue;
}
var content = review.getElementsByTagName('Content');
if (content && content.length) {
objReview.content = content[0].firstChild.nodeValue;
}
var rating = review.getElementsByTagName('Rating');
if (rating && rating.length) {
objReview.rating = rating[0].firstChild.nodeValue;
}
var helpfulVotes = review.getElementsByTagName('HelpfulVotes');
if (helpfulVotes && helpfulVotes.length) {
objReview.helpfulVotes = helpfulVotes[0].firstChild.nodeValue;
}
var totalVotes = review.getElementsByTagName('TotalVotes');
if (totalVotes && totalVotes.length) {
objReview.totalVotes = totalVotes[0].firstChild.nodeValue;
}
arrReviews.push(objReview);
}
objItem.customerReviews = arrReviews;
}
}
var similarProductsTag = item.getElementsByTagName('SimilarProducts');
if (similarProductsTag && similarProductsTag.length) {
var products = similarProductsTag[0].getElementsByTagName('SimilarProduct');
if (products && products.length) {
var arrProducts = [];
for (var iProduct = 0; iProduct < products.length; iProduct++) {
var product = products[iProduct];
var objProduct = {};
var asin = product.getElementsByTagName('ASIN');
if (asin && asin.length) {
objProduct.asin = asin[0].firstChild.nodeValue;
}
var title = product.getElementsByTagName('Title');
if (title && title.length) {
objProduct.title = title[0].firstChild.nodeValue;
}
arrProducts.push(objProduct);
}
objItem.similarProducts = arrProducts;
}
}
var offersTag = item.getElementsByTagName('Offers');
if (offersTag && offersTag.length) {
var offers = offersTag[0].getElementsByTagName('Offer');
if (offers && offers.length) {
var arrOffers = [];
for (var iOffer = 0; iOffer < offers.length; iOffer++) {
var offer = offers[iOffer];
var objOffer = {};
var offerListingId = offer.getElementsByTagName('OfferListingId');
if (offerListingId && offerListingId.length) {
objOffer.offerListingId = offerListingId[0].firstChild.nodeValue;
}
var merchants = offer.getElementsByTagName('Merchant');
if (merchants && merchants.length) {
var names = merchants[0].getElementsByTagName("Name");
if (names && names.length) {
objOffer.merchantName = names[0].firstChild.nodeValue;
}
}
var prices = offer.getElementsByTagName('Price');
if (prices && prices.length) {
var formattedPrices = prices[0].getElementsByTagName('FormattedPrice');
if (formattedPrices && formattedPrices.length) {
objOffer.price = formattedPrices[0].firstChild.nodeValue;
}
}
arrOffers.push(objOffer);
}
objItem.offers = arrOffers;
}
}
}
}
}
return arrItems;
};
Zapatec.Shop.parseImageXmlResponse = function(item, imageName) {
var result = {};
var images = item.getElementsByTagName(imageName);
if (images && images.length) {
var urls = images[0].getElementsByTagName('URL');
if (urls && urls.length) {
result.url = urls[0].firstChild.nodeValue;
}
var widths = images[0].getElementsByTagName('Width');
if (widths && widths.length) {
result.width = widths[0].firstChild.nodeValue;
}
var heights = images[0].getElementsByTagName('Width');
if (heights && heights.length) {
result.height = heights[0].firstChild.nodeValue;
}
}
return result;
}
Zapatec.Shop.parsePackageDimension = function(dimension, unit) {
var dimensionNum = parseInt(dimension);
if (null != unit && -1 != unit.indexOf("hundredths")) {
dimensionNum /= 100;
}
return dimensionNum;
}
Zapatec.Shop.onTitleClick = function(asin, searchIndex) {
var win = Zapatec.Shop.createWindow();
var title = '';
if ("Books" == searchIndex) {
title = 'Book Details';
}
else if ("Music" == searchIndex) {
title = 'Music Details';
}
else if ("PCHardware" == searchIndex) {
title = 'Hardware Details';
}
else if ("Blended" == searchIndex) {
title = 'Item Details';
}
win.setTitle(title);
Zapatec.Transport.fetchXmlDoc({
url: 'proxy.php?Asin=' + asin,
onLoad: function(objXmlDoc) {
var objXmlSource = Zapatec.Shop.parseItemXmlResponse(objXmlDoc, true);
var item = objXmlSource[0];
Zapatec.Shop.showDetailsContent(item, win, searchIndex);
}
});
return false;
}
Zapatec.Shop.getItemOverviewHtml = function(item) {
var overviewHtml;
var imageHtml = Zapatec.Shop.getFitImageHtml(item.largeImageUrl,
item.largeImageWidth, item.largeImageHeight,
Zapatec.Shop.MAX_LARGE_WIDTH, Zapatec.Shop.MAX_LARGE_HEIGHT);
overviewHtml = '<div style="background:red;"><a target="_blank" class="detailsImageHolder" href="' +
item.largeImageUrl + '">' + imageHtml+ '</a></div>';
overviewHtml += '<div class="cccccc1"><br/><span class="zpTitle zpTitleDetails">' +
item.title + '</span><br/>';
if (item.author) {
overviewHtml += '<span class="zpPopupAuthor">by ' + item.author +
'</span></div><br/><br/>';
}
else if (item.manufacturer) {
overviewHtml += '<span class="zpPopupAuthor">by ' + item.manufacturer +
'</span></div><br/><br/>';
}
if (item.listPrice) {
overviewHtml += '<span class="detailsCategory">List Price: </span>' +
'<span class="zpListPrice">' + item.listPrice + '</span><br/>';
}
if (item.lowestNewPrice) {
overviewHtml += '<span class="detailsCategory">Lowest Price: </span>' +
'<span class="zpLowestPrice">' + item.lowestNewPrice + '</span><br/>';
}
if (item.lowestUsedPrice) {
overviewHtml += '<span class="detailsCategory">Used Price: </span>' +
'<span class="zpLowestPrice">' + item.lowestUsedPrice + '</span><br/>';
}
if (item.author) {
if (item.binding) {
overviewHtml += '<span class="detailsCategory">' + item.binding + ':</span>';
}
if (item.numberOfPages) {
overviewHtml += '<span class="zpPopupSimpleText" >' + item.numberOfPages +
' pages</span>';
}
overviewHtml += '<br/>';
var publisher = item.publisher;
if (null != item.edition) {
publisher += ';' + item.edition + ' edition';
}
if (publisher) {
overviewHtml += '<span class="detailsCategory">Publisher: </span>' +
'<span class="zpPopupSimpleText" >' + publisher + '</span><br/>';
}
}
if (item.publicationDate) {
overviewHtml += '<span class="detailsCategory">Publication Date: </span>'+
'<span class="zpPopupSimpleText">' + item.publicationDate + '</span><br/>';
}
if (item.isbn) {
overviewHtml += '<span class="detailsCategory">ISBN: </span>'+
'<span class="zpPopupSimpleText" >' + item.isbn + '</span><br/>';
}
var width = Zapatec.Shop.parsePackageDimension(
item.packageWidth, item.packageWidthUnits);
var height = Zapatec.Shop.parsePackageDimension(
item.packageHeight, item.packageHeightUnits);
var length = Zapatec.Shop.parsePackageDimension(
item.packageLength, item.packageLengthUnits);
var weight = Zapatec.Shop.parsePackageDimension(
item.packageWeight, item.packageWeightUnits);
if (width && height) {
var units = "";
if (null != item || -1 != item.packageLengthUnits.indexOf("inches")) {
units = "inches";
}
var dimension = length + " x " + width + " x " + height + " " + units;
overviewHtml += '<span class="detailsCategory">Product Dimensions: </span>'+
'<span class="zpPopupSimpleText" >' + dimension + '</span><br/>';
}
var params = null;
if (item.offers && 0 < item.offers.length) {
var offerListingId = item.offers[0].offerListingId;
params += ", '" + offerListingId + "'";
overviewHtml += '<a class="addToCart" href="#" onclick='+
'"return Zapatec.Shop.addToCart(' + params + ');"></a>';
}
return overviewHtml;
}
Zapatec.Shop.getItemEditorialReviewsHtml = function(item) {
if (null == item.editorialReviews) {
return null;
}
var editorialReviews = item.editorialReviews;
var editorialHtml = '<div class="editorialHolder">';
for (var iReview = 0; iReview < item.editorialReviews.length; ++iReview) {
var review = item.editorialReviews[iReview];
editorialHtml += '<span>'+review.source+'</span>' +
'<br/><span>' + review.content + '</span><br/>';
}
editorialHtml += '</div>';
return editorialHtml;
}
Zapatec.Shop.getItemCustomerReviewsHtml = function(item) {
if (null == item.customerReviews) {
return null;
}
var averageRating = parseInt(parseFloat(item.customerReviewsAverageRating)*10);
var averageUrl = Zapatec.Shop.STARS_URL[averageRating];
var customerReviews = item.customerReviews;
var customerHtml = '<div class="customerReviewHolder">';
customerHtml += '<span class="averageCustomerReview">\
Average Customer Review:</span>'+
'<img class="zpImage" src="'+averageUrl+'"/><br/><br/>';
for (var iReview = 0; iReview < item.customerReviews.length; ++iReview) {
var review = item.customerReviews[iReview];
var rating = review.rating;
var ratingUrl = Zapatec.Shop.STARS_URL[rating];
var helpfulVotes = review.helpfulVotes;
var totalVotes = review.totalVotes;
if (totalVotes && '0' != totalVotes) {
customerHtml += '<span>'+helpfulVotes+' of '+totalVotes+
' people found the following review helpful:</span>';
}
customerHtml += '<br/>'+
'<img class="zpImage" src="'+ratingUrl+'"/>'+
'<span class="editorialSource">'+review.summary+'</span>' +
'<br/><span>' + review.content + '</span><br/><br/>';
}
customerHtml += '</div>';
return customerHtml;
}
Zapatec.Shop.getItemSimilarProductsHtml = function(item, searchIndex) {
if (null == item.similarProducts) {
return null;
}
var similarProducts = item.similarProducts;
var productsHtml = '';
for (var iProduct = 0; iProduct < similarProducts.length; ++iProduct) {
var product = item.similarProducts[iProduct];
productsHtml += '<a href="#" class="zpTitle"';
var params = "'" + product.asin + "', '" + searchIndex + "'";
productsHtml += ' onclick="return Zapatec.Shop.onTitleClick('+params+');"';
productsHtml += '>'+product.title+'</a><br/>';
}
return productsHtml;
}
Zapatec.Shop.showDetailsContent = function(item, win, searchIndex) {
if (null == item.largeImageUrl) {
item.largeImageUrl = Zapatec.Shop.NO_IMAGE_BIG_URL;
}
var overviewHtml = Zapatec.Shop.getItemOverviewHtml(item);
var editorialReviewsHtml = Zapatec.Shop.getItemEditorialReviewsHtml(item);
var customerReviewsHtml = Zapatec.Shop.getItemCustomerReviewsHtml(item);
var similarProductsHtml = Zapatec.Shop.getItemSimilarProductsHtml(item, searchIndex);
var tabsId = 'detailsTabs' + Zapatec.Shop.detailsWindowCount;
var tabs = '<div id="'+tabsId+'"' +
' style="width:584px;height:342px;">' +
'</div> ';
win.setContent(tabs);
var objTabsSource = {
tabs: [
{
id: 'overview',
linkInnerHTML: '<u>O</u>verview',
accessKey: 'o',
title: 'Overview',
content: overviewHtml,
contentType: "html/text"
}]
};
if (null != editorialReviewsHtml) {
objTabsSource.tabs.push({
id: 'editorial',
linkInnerHTML: '<u>E</u>ditorial',
accessKey: 'e',
title: 'Editorial',
content: editorialReviewsHtml,
contentType: "html/text"
});
}
if (null != customerReviewsHtml) {
objTabsSource.tabs.push({
id: 'customer',
linkInnerHTML: '<u>C</u>ustomer',
accessKey: 'c',
title: 'Customer',
content: customerReviewsHtml,
contentType: "html/text"
});
}
if (null != similarProductsHtml) {
objTabsSource.tabs.push({
id: 'similar',
linkInnerHTML: '<u>S</u>imilar',
accessKey: 's',
title: 'Similar',
content: similarProductsHtml,
contentType: "html/text"
});
}
var objTabs = new Zapatec.AccordionTabs({
tabs: tabsId,
theme: '',
source: objTabsSource,
sourceType: 'json',
overflow: 'auto'
});
win.setTitle(item.title);
}
Zapatec.Shop.detailsWindowCount = 0;
Zapatec.Shop.newWindowOffsetY = 30;
Zapatec.Shop.createWindow = function(isShoppingCart, onClose) {
var windowSize = Zapatec.Utils.getWindowSize();
var pageScrollY = Zapatec.Utils.getPageScrollY();
var width = 600;
var height;
var top = 20;
if (true == isShoppingCart) {
height = 210;
top = pageScrollY + windowSize.height - height - 50;
}
else {
height = 380;
top = pageScrollY;
var windowPositionsCount = (windowSize.height - 2*height) /
Zapatec.Shop.newWindowOffsetY;
top += (Zapatec.Shop.detailsWindowCount % windowPositionsCount) *
Zapatec.Shop.newWindowOffsetY;
++Zapatec.Shop.detailsWindowCount;
}
var left = windowSize.width - width -30;
var win = Zapatec.Window.setup({
left : left,
top : top,
width : width,
height : height,
raiseOnlyOnTitle : false,
showStatus : false,
canResize: false,
showMaxButton: false,
theme : 'winxp',
content : '<span style="margin-left: 40%; margin-top: 40%;">Loading...</span>',
onClose : onClose
});
return win;
}
Zapatec.Shop.createCartGrid = function(objXmlSource) {
var objJsonGrid = this.convertCartXmlToGrid(objXmlSource);
Zapatec.Shop.cartGrid = new Zapatec.Grid({
source: objJsonGrid,
sourceType: 'json',
callbackHeaderDisplay: Zapatec.Shop.cartCallbackHeader,
callbackRowDisplay: Zapatec.Shop.cartCallbackRow,
callbackPaginationDisplay: Zapatec.Shop.cartCallbackPagination,
theme: "winxp",
rowsPerPage: objXmlSource.rowsPerPage
});
}
Zapatec.Shop.parseCartXmlResponse = function(objXmlDoc) {
var arr = [];
if (objXmlDoc && objXmlDoc.documentElement) {
var cartId = objXmlDoc.documentElement.getAttribute('cartId');
if (null != cartId) {
arr.cartId = cartId;
}
var cartHmac = objXmlDoc.documentElement.getAttribute('hmac');
if (null != cartHmac) {
arr.cartHmac = cartHmac;
}
var purchaseURL = objXmlDoc.documentElement.getAttribute('purchaseUrl');
if (null != purchaseURL) {
arr.purchaseUrl = purchaseURL;
}
var subTotalPrice = objXmlDoc.documentElement.getAttribute('subTotalPrice');
if (null != subTotalPrice) {
arr.subTotalPrice = subTotalPrice;
}
var arrItem = objXmlDoc.documentElement.getElementsByTagName('item');
if (arrItem && arrItem.length) {
for (var iItem = 0; iItem < arrItem.length; iItem++) {
var item = arrItem[iItem];
arr.push({
itemId: item.getAttribute('itemId'),
quantity: item.getAttribute('quantity'),
title: item.getAttribute('title'),
price: item.getAttribute('price'),
itemTotal: item.getAttribute('itemtotal'),
purchaseURL: item.getAttribute('purchaseUrl'),
asin: item.getAttribute('asin')});
}
}
}
return arr;
};
Zapatec.Shop.getCartOperationUrl = function(operation, asin, offerListingId) {
var url = 'proxy.php?';
if (null != this.cartId) {
url += 'CartId='+this.cartId;
url += '&HMAC='+this.cartHmac;
}
if ('CartAdd' == operation) {
if (null == this.cartId) {
url += 'Operation=CartCreate';
}
else {
url += '&Operation=CartAdd';
}
if (offerListingId) {
url += '&OfferListingId='+offerListingId;
}
else {
url += '&Asin='+asin;
}
}
else if ('CartModify' == operation) {
url += '&Operation=CartModify';
var rows = Zapatec.Shop.cartGrid.getRows();
var rowCount = rows.length;
for (var i = 0; i < rowCount; ++i) {
var row = rows[i];
var itemIndex = i + 1;
url += '&CartItemId'+itemIndex+'='+row.cartItemId;
var value = document.getElementById('quantity.'+i).value;
url += '&Quantity'+itemIndex+'='+value;
}
}
return url;
}
Zapatec.Shop.showCartContent = function(objXmlSource) {
var html = '<div class="shoppingCartHolder">';
html += '<div class="shoppingCart">Shopping Cart</div>';
html += '<a class="checkoutAnchor" href="'+objXmlSource.purchaseUrl+
'" target="_blank" onclick="return Zapatec.Shop.purchaseCart();">'+
'<img class="zpImage" src="images/checkout.gif"/></a>';
html += '<div class="clearer"></div>';
html += '<br/>';
html += '<div class="subtotalHolder">';
var subtotal = "$0";
if (objXmlSource.subTotalPrice) {
subtotal = objXmlSource.subTotalPrice;
}
html += '<span class="zpSubtotal">Subtotal: </span>\
<span class="subtotalPrice">'+subtotal+'</span>';
html += '<a class="updateAnchor" href="#" \
onclick="return Zapatec.Shop.updateCart();">\
<img class="zpImage" src="images/update.gif"/></a>';
html += '</div>';
html += '<div class="clearer"></div>';
html += '<br/>';
html += '<div class="cartGridHolder">';
html += '<div id="cartOutput"></div><br/>';
html += '</div>';
html += '</div>';
Zapatec.Shop.lastShoppingCartXml = objXmlSource;
Zapatec.Shop.cartWin.setContent(html);
setTimeout(function() { Zapatec.Shop.createCartGrid(objXmlSource) }, 10);
}
Zapatec.Shop.addToCart = function(asin, offerListingId) {
var url = Zapatec.Shop.getCartOperationUrl('CartAdd', asin, offerListingId);
Zapatec.Shop.showShoppingCartWindow();
Zapatec.Transport.fetchXmlDoc({
url: url,
onLoad: function(objXmlDoc) {
var objXmlSource = Zapatec.Shop.parseCartXmlResponse(objXmlDoc, false);
if (null == Zapatec.Shop.cartId) {
Zapatec.Shop.cartId = objXmlSource.cartId;
Zapatec.Shop.cartHmac = objXmlSource.cartHmac;
}
Zapatec.Shop.showCartContent(objXmlSource);
}
});
return false;
}
Zapatec.Shop.updateCart = function() {
var rowCount = Zapatec.Shop.cartGrid.getRows().length;
var url = Zapatec.Shop.getCartOperationUrl('CartModify');
Zapatec.Transport.fetchXmlDoc({
url: url,
onLoad: function(objXmlDoc) {
var objXmlSource = Zapatec.Shop.parseCartXmlResponse(objXmlDoc, false);
Zapatec.Shop.showCartContent(objXmlSource);
}
});
return false;
}
Zapatec.Shop.purchaseCart = function() {
var src = Zapatec.Shop.lastShoppingCartXml.purchaseUrl;
var html = '<iframe src="'+src+'" style="width: 100%; height: 100%; border: 0;"></iframe>';
Zapatec.Shop.cartWin.setContent(html);
return false;
}
Zapatec.Shop.deleteCartItem = function(cartItemId) {
var rows = Zapatec.Shop.cartGrid.getRows();
var rowCount = rows.length;
for (var i = 0; i < rowCount; ++i) {
var row = rows[i];
var itemIndex = i + 1;
if (row.cartItemId == cartItemId) {
document.getElementById('quantity.'+i).value = 0;
break;
}
}
Zapatec.Shop.updateCart();
return false;
}
Zapatec.Shop.convertCartXmlToGrid = function(objXmlSource) {
var objJsonGrid = {
fields: [
{title: "Title", dataType: "string", className: "zpTitle"},
{title: "Price", dataType: "string"},
{title: "Qty", dataType: "string"}
],
rows: []
};
for (var index = 0; index < objXmlSource.length; ++index) {
var item = objXmlSource[index];
objJsonGrid.rows.push({cells: [
{v: item.title, className: "zpTitle"},
{v: item.price},
{v: item.quantity}
],
asin: item.asin,
cartItemId: item.itemId,
quantity: item.quantity});
}
return objJsonGrid;
}
Zapatec.Shop.cartHtml = null;
Zapatec.Shop.cartHtmlItemCount = 0;
Zapatec.Shop.cartCallbackHeader = function(objGrid) {
var cartOutput = document.getElementById("cartOutput");
Zapatec.Shop.cartHtml = '<table id="cartTable" style="width: 100%;"><thead>';
Zapatec.Shop.cartHtmlItemCount = 0;
var arrFields = objGrid.getFields();
for (var iCol = 0; iCol < arrFields.length; iCol++) {
var objField = arrFields[iCol];
var style = null;
if (0 == iCol) {
style = 'width: 80%;';
}
else {
style = 'width: 10%;';
}
Zapatec.Shop.cartHtml += '<td class="zpHeader" style="'+style;
Zapatec.Shop.cartHtml += '">';
Zapatec.Shop.cartHtml += '<a href="#" onclick="'+objGrid.getFieldOnclick(objField)+'; return false">'+objGrid.getFieldTitle(objField)+'</a>';
Zapatec.Shop.cartHtml += '</td>';
}
Zapatec.Shop.cartHtml += '</thead><tbody>';
};
Zapatec.Shop.cartCallbackRow = function(objGrid, objRow) {
Zapatec.Shop.cartHtml += '<tr>';
var arrCells = objGrid.getRowCells(objRow);
for (var iCol = 0; iCol < arrCells.length; iCol++) {
var objCell = arrCells[iCol];
var data = objGrid.getCellValueString(objCell);
Zapatec.Shop.cartHtml += '<td ';
if (objRow.cells[iCol].className) {
Zapatec.Shop.cartHtml += ' class="' + objRow.cells[iCol].className + '"';
}
if (iCol == 0) {
Zapatec.Shop.cartHtml += '><a href="#" ';
Zapatec.Shop.cartHtml += ' onclick="return Zapatec.Shop.onTitleClick(\''+
objRow.asin.toString()+'\');">';
Zapatec.Shop.cartHtml += data;
Zapatec.Shop.cartHtml += '</a';
}
Zapatec.Shop.cartHtml += '>';
if (0 == iCol) {
}
else if (2 == iCol) {
var input = '<input id="quantity.'+Zapatec.Shop.cartHtmlItemCount+
'" size="2" maxlength="3" value="'+objRow.quantity+
'" type="text">';
Zapatec.Shop.cartHtml += input;
}
else {
Zapatec.Shop.cartHtml += data;
}
Zapatec.Shop.cartHtml += '</td>';
}
Zapatec.Shop.cartHtml += '<td ';
Zapatec.Shop.cartHtml += '><a href="#" ';
Zapatec.Shop.cartHtml += ' onclick="return Zapatec.Shop.deleteCartItem(\''+
objRow.cartItemId.toString()+'\');">';
Zapatec.Shop.cartHtml += "<span class=\"deleteItem\">delete</span>";
Zapatec.Shop.cartHtml += '</a';
Zapatec.Shop.cartHtml += '</td>';
Zapatec.Shop.cartHtml += '</tr>';
++Zapatec.Shop.cartHtmlItemCount;
if (Zapatec.Shop.cartHtmlItemCount == objGrid.getRows().length) {
Zapatec.Shop.cartHtml += '</tbody></table>';
var cartOutput = document.getElementById("cartOutput");
cartOutput.innerHTML = Zapatec.Shop.cartHtml;
Zapatec.Shop.cartHtml = null;
}
};
Zapatec.Shop.cartCallbackPagination = function(objGrid) {
};
Zapatec.Shop.getFitImageHtml = function(imageUrl, origWidth, origHeight,maxWidth, maxHeight, className) {
var width = origWidth;
var height = origHeight;
if (maxWidth < width) {
var aspect = origWidth / origHeight;
var widthDiff = width - maxWidth;
width -= widthDiff;
height -= widthDiff / aspect;
}
if (maxHeight < height) {
var aspect = origWidth / origHeight;
var heightDiff = height - maxHeight;
width -= heightDiff * aspect;
height -= heightDiff;
}
var img = '<img src="' + imageUrl + '"';
img += className ? ' class="'+className+'"' : '';
if (width != origWidth || height != origHeight) {
img += ' width="'+width+'" height="'+height+'"';
}
img += ' />';
return img;
};
Documentation generated by
JSDoc on Thu May 21 12:20:07 2009