//
//-------------------------------------------------------------------
// Licensed Materials - Property of IBM
//
// WebSphere Commerce
//
// (c) Copyright IBM Corp. 2006
//
// US Government Users Restricted Rights - Use, duplication or
// disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
//-------------------------------------------------------------------
//

/**********************************************************
* Function to add a catEntry to the shopping cart. Used in conjunction
* with CatalogEntryThumbnailDisplay.jspf. 
*
*	Parameters:
*	catEntryIdentifier - the identifier of the catalog entry element
* 
* Assumptions that there are DOM elements called "formParamsStoreId_<catEntryIdentifier>",
* "formParamsCatalogId_<catEntryIdentifier>", "formParamsCatEntryId_<catEntryIdentifier>",
* "attributeValuesClass_<catEntryIdentifier>", "attrNameClass_<catEntryIdentifier>".
**********************************************************/
function Add2ShopCartAjax(catEntryIdentifier)
{
			var params = [];
			params.orderId = ".";
			params.calculationUsageId = "-1";
			params.updatePrices = "1";
			params.quantity = "1";
			
			params.URL = "SetPendingOrder?URL=OrderCalculate?URL=SuccessfulAJAXRequest";
			params.errorViewName = "FailedAJAXRequest";

			params.storeId = dojo.byId('formParamsStoreId_' + catEntryIdentifier).value;
			params.catalogId = dojo.byId('formParamsCatalogId_' + catEntryIdentifier).value;
			params.catEntryId = dojo.byId('formParamsCatEntryId_' + catEntryIdentifier).value;

			var attributeNames = dojo.html.getElementsByClass('attrNameClass_'+catEntryIdentifier);
			var attributeNamesString = "";
			var attributeName = "";
			
			var i=1;
//			dojo.lang.forEach(attributeNames, function(node){
//					attributeName = node.value;
//					if (i!=1) {
//						attributeNamesString = attributeNamesString + "&";
//					}
//					attributeNamesString = attributeNamesString + "attrName=" + attributeName;
//					i++;
//			});
			
			var attributeValues = dojo.html.getElementsByClass('attributeValuesClass_'+catEntryIdentifier);
			var attributeValuesString = "";
			var attributeValue = "";

			var i=1;
			dojo.lang.forEach(attributeValues, function(node){
					attributeValue = node.options[node.selectedIndex].value;
					if (i!=1) {
						attributeValuesString = attributeValuesString + "&";
					}
					attributeValuesString = attributeValuesString + "attrValue=" + attributeValue;
					i++;
			});
			
			var bindArgs = {
				url: 'OrderItemAdd?' + attributeNamesString + "&" + attributeValuesString,
				error: function(type, data, evt){alert("Error: \n" + data);},
				load: ShopCartAjaxCallBack,
				mimetype:'text/json',
				content: params
			};
			dojo.io.bind(bindArgs);
			cursor_wait();
}
	
/**********************************************************
* Callback function after attempting to add an item to the shopping cart.
* This function will check if the request was successful. If it failed
* then an alert message will be shown to the user.
*
*	Parameters:
*	type -
* data - the JSON response data
* evt -
**********************************************************/
function ShopCartAjaxCallBack(type, data, evt)
{
			if (data.status == 'Success') {
				// Signal that order is updated.
				order_updated();
			} else {
				alert(data.error.message);
			}
			cursor_clear();
}
		
/**********************************************************
* Function to add a catEntry to the wish list. Used in conjunction
* with CatalogEntryThumbnailDisplay.jspf. 
*
*	Parameters:
*	catEntryIdentifier - the identifier of the catalog entry element
* 
* Assumptions that there are DOM elements called "formParamsStoreId_<catEntryIdentifier>",
* "formParamsCatalogId_<catEntryIdentifier>", "formParamsCatEntryId_<catEntryIdentifier>",
* "attributeValuesClass_<catEntryIdentifier>", "attrNameClass_<catEntryIdentifier>".
**********************************************************/
function Add2WishListAjax(catEntryIdentifier)
{
			var params = [];
			params.orderId = ".";
			params.calculationUsageId = "-1";
			params.updatePrices = "1";
			params.quantity = "1";

			params.URL = "SuccessfulAJAXRequest";
			params.errorViewName = "FailedAJAXRequest";

			params.storeId = dojo.byId('formParamsStoreId_' + catEntryIdentifier).value;
			params.catalogId = dojo.byId('formParamsCatalogId_' + catEntryIdentifier).value;
			params.catEntryId = dojo.byId('formParamsCatEntryId_' + catEntryIdentifier).value;

			var attributeNames = dojo.html.getElementsByClass('attrNameClass_'+catEntryIdentifier);
			var attributeNamesString = "";
			var attributeName = "";
			
			var i=1;
			dojo.lang.forEach(attributeNames, function(node){
					attributeName = node.value;
					if (i!=1) {
						attributeNamesString = attributeNamesString + "&";
					}
					attributeNamesString = attributeNamesString + "attrName=" + attributeName;
					i++;
			});
			
			var attributeValues = dojo.html.getElementsByClass('attributeValuesClass_'+catEntryIdentifier);
			var attributeValuesString = "";
			var attributeValue = "";

			var i=1;
			dojo.lang.forEach(attributeValues, function(node){
					attributeValue = node.options[node.selectedIndex].value;
					if (i!=1) {
						attributeValuesString = attributeValuesString + "&";
					}
					attributeValuesString = attributeValuesString + "attrValue=" + attributeValue;
					i++;
			});

			var bindArgs = {
				url: 'InterestItemAdd?' + attributeNamesString + "&" + attributeValuesString,
				error: function(type, data, evt){alert("Error: \n" + data);},
				load: WishListAjaxCallBack,
				mimetype:'text/json',
				content: params
			};
			dojo.io.bind(bindArgs);
			cursor_wait();
}

/**********************************************************
* Callback function after attempting to add an item to the wish list.
* This function will check if the request was successful. If it failed
* then an alert message will be shown to the user.
*
*	Parameters:
*	type -
* data - the JSON response data
* evt -
**********************************************************/
function WishListAjaxCallBack(type, data, evt)
{
			if (data.status == 'Success') {
				// Signal that order is updated.
				WishList_Updated();
			} else {
				alert(data.error.message);
			}
			cursor_clear();
}

	//drop event handler for the compare
	//compareDrop
	//compareCounter
	//TODO: fix this function...design will not working
/**********************************************************
* Function to add a catEntry to the compare zone.
*
*	Parameters:
*	catEntryIdentifier - the identifier of the catalog entry element
**********************************************************/
function Add2CompareAjax(catEntryIdentifier) 
{
		increaseCompareCounter();	
		compareDrop.onDragOut();
		if (compareCounter <= 6) {
			var catentryAddedToCompare = document.CompareForm.catentryId[compareCounter-1].value = catEntryIdentifier;
		
			var firstItem = false;
	
			var compare = document.getElementById("compareZone");
			var childNodes = compare.childNodes;
			for (i=0; i<childNodes.length; i++) {
				if (childNodes[i].id == "compareDropZoneImg") {
					compare.removeChild(childNodes[i]);
					firstItem = true;
				}
			}
			
			var product1 = new WCProduct();
			var node1 = createProductWidget(product1, "catBrowse", "medium", false);
			var td1 = document.createElement("td");
			td1.setAttribute("id", "compareCatentry"+catentryAddedToCompare);
			td1.appendChild(node1);
	
			if (((compareCounter-1) % 3) == 0 ) {
				compareRowCounter++;
				
				var tr1 = document.createElement("tr");
				tr1.setAttribute("id", "compareRow"+compareRowCounter);
			} else {
				var tr1 = document.getElementById("compareRow"+compareRowCounter);
			}
			tr1.appendChild(td1);
			
			if (firstItem) {
				var table1 = document.createElement("table");
				
				var tbody1 = document.createElement("tbody");
				tbody1.setAttribute("id", "compareItemsTable");
				table1.appendChild(tbody1);
	
				tbody1.appendChild(tr1);
				compare.appendChild(table1);
			} else {
				var tbody1 = document.getElementById("compareItemsTable");
				tbody1.appendChild(tr1);
			}
	
			var testObj = new dojo.xml.Parse();
			var testObjects = testObj.parseElement(document.getElementById("compareZone"));
			var testParse = dojo.widget.getParser();
			testParse.createComponents(testObjects);
		}
}




function createElementWithClass(type, className) {
	// due to a error in IE's dom implimentation, use this helper method to create dynamic DOM elements with class attributes 
	var element;
	  // First try the IE way; if this fails then use the standard way
	if (document.all) {
		element =  document.createElement('<'+type+' class="'+className+'" />');
	} else {
		element = document.createElement(type);
		element.setAttribute('class', className);
	}
	return element;
}

function createElementWithClickEvent(type, clickEvent) {
	// due to a error in IE's dom implimentation, use this helper method to create dynamic DOM elements with an onclick handler
	var element;
	  // First try the IE way; if this fails then use the standard way
	if (document.all) {
		element =  document.createElement('<'+type+' onclick="' + clickEvent + '" />');
	} else {
		element = document.createElement(type);
		element.setAttribute('onclick', clickEvent);
	}
	return element;
}


function createElementWithClassAndEvents(type, className, divIndex) {
	// due to a error in IE's dom implimentation, use this helper method to create dynamic DOM elements with mouseover events
	var element;
	  // First try the IE way; if this fails then use the standard way
	if (document.all) {
		element =  document.createElement('<'+type+' class="' + className + '" onmouseover="showHelper('+divIndex+')" onmouseout="hideHelper('+divIndex+')" />');
	} else {
		element = document.createElement(type);
		element.setAttribute('class', className);
		element.setAttribute('onmouseover', "showHideLayers('quickview_img" + divIndex + "','','show')");
		element.setAttribute('onmouseout', "showHideLayers('quickview_img" + divIndex + "','','hide')");
	}
	return element;
}

function showHelper(divIndex) {
	showHideLayers('quickview_img' + divIndex,'','show');
}

function hideHelper(divIndex) {
	showHideLayers('quickview_img' + divIndex,'','hide');
}

			/**********************************************************
			* Function to create a ProductQuickView widget using JavaScript. 
			*
			*	Parameters:
			*	productObj - the WC product object
			* identifierString - the identifier of the catalog entry element
			* imageSize - the size of the image
			* showCompare - show the compare icon in the tooltip
			* imagePath - the image path
			* viewType - "image" or "detailed" view
			* storeId - the store id
			* catalogId - the catalog id
			**********************************************************/
			function createProductWidget(parentObject, buttonDiv, productObj, identifierString, pageLength, modBClass, imagePath, storeId, catalogId, outerModBClass) 
			{
					var _id = identifierString + productObj.identifierCounter;
					var widgetHTML = ""
					
					if(productObj.baseContentHTML == null || productObj.baseContentHTML == ""){
						
						widgetHTML = ""	
										+"			<div class='product_img'>\n"
										+"				<div id='catBrowse"+ productObj.identifierCounter + "' align='center'>\n"
										+"					<a href='" + productObj.productDisplayURL + "' id='imgcatBrowse"+ productObj.identifierCounter + "'>\n"
										+"						<img src='"+ productObj.mediumImageSrc +"' alt='"+ productObj.shortDescription +"' border='0' width='125' height='168'/>\n"
										+"					</a>\n"
										+"				</div>\n"
										+"			</div>\n";
					}else{
						widgetHTML = productObj.baseContentHTML;
					}
					
					if(outerModBClass != null){
					
					var outerModBDiv = createElementWithClass("div", outerModBClass);
					parentObject.appendChild(outerModBDiv);
					var modBDiv = createElementWithClass("div", modBClass);
					modBDiv.setAttribute("id","product_"+identifierString);
					outerModBDiv.appendChild(modBDiv);
					}
					else{
					var modBDiv = createElementWithClass("div", modBClass);
					modBDiv.setAttribute("id","product_"+identifierString);
					parentObject.appendChild(modBDiv);
					}
					var productContainerDiv = createElementWithClassAndEvents("div", "product_container", (productObj.identifierCounter % pageLength) + 1);
					modBDiv.appendChild(productContainerDiv);
							
					var productQuickViewDiv = createElementWithClass("div", "order_item_product_quick_view");
					productContainerDiv.appendChild(productQuickViewDiv);
									
					// create the ProductQuickView
					var productQuickViewWidget = dojo.widget.createWidget("ProductQuickView", {
																								id: "productQuickView" + productObj.identifierCounter,
																								defaultContent: false,
																								identifier: _id
																								}, productQuickViewDiv);

					var baseContentDiv = document.createElement("div");
					productQuickViewWidget.domNode.appendChild(baseContentDiv);

					var baseContentWidget = dojo.widget.createWidget("BaseContent", {
																						id: "productQuickViewBase" + productObj.identifierCounter,
																						dragSourceId: _id,
																						dragSourceType: productObj.dragSourceType,
																						catentryId: productObj.catentryId,
																						SKU: productObj.SKU,
																						partnumber: productObj.partnumber,
																						variationTheme: productObj.variationTheme,
																						userInnerHtml: widgetHTML
																						}, baseContentDiv);
																											
		var quickViewImgDiv = document.createElement("div");
		productContainerDiv.appendChild(quickViewImgDiv);
		quickViewImgDiv.setAttribute("id", "quickview_img" + ((productObj.identifierCounter % pageLength) + 1));

		
		var sneakPeekLink = createElementWithClickEvent("a", "top.sneakPeek(" + productObj.catentryId + ", '" + productObj.SKU + "');return false;");
		quickViewImgDiv.appendChild(sneakPeekLink);
		sneakPeekLink.setAttribute("href", "#");
		//sneakPeekLink.setAttribute("onclick", "top.sneakPeek(" + productObj.catentryId + ");return false;");
		var namepriceDiv = createElementWithClass("div", modBClass);
		modBDiv.appendChild(namepriceDiv);
		var namepricePar = createElementWithClass("p", "category_p");
		namepriceDiv.appendChild(namepricePar);

		var nameLink = document.createElement("a");
		namepricePar.appendChild(nameLink);
		nameLink.setAttribute("href", productObj.productDisplayURL);
		nameLink.setAttribute("text-decoration","none");
		var nameNode = document.createElement("b");
		nameLink.appendChild(nameNode);
		var productname = document.createTextNode(productObj.name);
		nameNode.appendChild(productname)
		var br1 = document.createElement("br");
		namepricePar.appendChild(br1);
		var productshortdesc = document.createTextNode(productObj.shortDescription);
		namepricePar.appendChild(productshortdesc);
		var br2 = document.createElement("br");
		namepricePar.appendChild(br2);
		var price = document.createTextNode(productObj.displayPrice);
		namepricePar.appendChild(price);
		
		//bazaar voice ratings and reviews
		//alert(productObj.ratingImageSrc);
		if(productObj.ratingImageSrc != undefined) {
			if(productObj.ratingImageSrc != "") {
				namepricePar.appendChild(document.createElement("br"));
				var ratingLink = document.createElement("a");
				namepricePar.appendChild(ratingLink);
				ratingLink.setAttribute("href", productObj.productDisplayURL);
				var ratingImg = document.createElement("img");
				ratingLink.appendChild(ratingImg);
				ratingImg.setAttribute("src",productObj.ratingImageSrc);
				ratingImg.setAttribute("border","0");
			}
		}
		if(productObj.reviews != undefined) {
			if(productObj.reviews != "") {
				namepricePar.appendChild(document.createElement("br"));
				var reviewLink = document.createElement("a");
				reviewLink.setAttribute("href", productObj.productDisplayURL);
				reviewLink.setAttribute("text-decoration","none");
				var reviewNode = document.createElement("b");
				reviewLink.appendChild(reviewNode);
				
				if(productObj.reviews > 1) {
					var reviewLine = document.createTextNode(productObj.reviews+" reviews");
					reviewNode.appendChild(reviewLine);
				} else if(productObj.reviews == 1) {
					var reviewLine = document.createTextNode(productObj.reviews+" review");
					reviewNode.appendChild(reviewLine);
	
				}
				namepricePar.appendChild(reviewLink);
			} else {
				namepricePar.appendChild(document.createElement("br"));
				var writeReviewLink = document.createElement("a");
				writeReviewLink.setAttribute("href", productObj.productDisplayURL);
				var noReviewImg = document.createElement("img");
				writeReviewLink.appendChild(noReviewImg);
				noReviewImg.setAttribute("src",productObj.noReviewImageSrc);
				noReviewImg.setAttribute("border","0");
				namepricePar.appendChild(writeReviewLink);
				
			}
		}
		
		//the new part: dropdown and add to bag button
		if(outerModBClass != null){
			var outerModBDiv = createElementWithClass("div", outerModBClass);
			buttonDiv.appendChild(outerModBDiv);
			var addToBagDiv = createElementWithClass("div", modBClass);
			outerModBDiv.appendChild(addToBagDiv);
		}
		else{
			var addToBagDiv = createElementWithClass("div", modBClass);
			buttonDiv.appendChild(addToBagDiv);
		}
		
		
		
		
		var addToBagPar = createElementWithClass("p", "category_p");
		addToBagDiv.appendChild(addToBagPar);
		/*var br5 = document.createElement("br");
		addToBagPar.appendChild(br5);
		var br3 = document.createElement("br");
		addToBagPar.appendChild(br3);*/

		var myLabel  = document.createElement("label");
		myLabel.innerHTML = "qty:"
		addToBagPar.appendChild(myLabel);		
		var se = document.createElement("select");
		se.setAttribute("id","selectcatBrowse"+productObj.identifierCounter);
		for(var i=0; i<10; i++){
			se.options[i] = new Option(i,i);
		}
		//se.options[1].setAttribute("selected","true");
		se.selectedIndex=1;
		se.setAttribute("class","textbox_with_padding");		 
		var myVar = document.createTextNode(productObj.qty);
		addToBagPar.appendChild(se);
		/*
		var br6 = document.createElement("br");
		addToBagPar.appendChild(br6);
		var br4 = document.createElement("br");
		addToBagPar.appendChild(br4);
		*/
		var myLabel  = document.createElement("label");
		myLabel.innerHTML = '&nbsp;';
		addToBagPar.appendChild(myLabel);		

		var myButton = document.createElement('input');
		myButton.setAttribute("type" , "BUTTON");
		myButton.setAttribute("value", "add");
		myButton.setAttribute("title", "click to add product to bag");
		myButton.setAttribute("name" ,"catBrowse"+productObj.identifierCounter);
		//myButton.setAttribute("onclick", "addToCart(this.name);");
		myButton.onclick=function(){
			var divName = this.name;
			var prodObj = document.getElementById(divName);
			var numOfItems = document.getElementById('select'+divName).value;
			
			if(numOfItems>0){
				var orderItemAddForm = document.getElementById('OrderItemAddForm');
				var catEntryId = document.createElement('input');
				catEntryId.type='hidden';
				catEntryId.name='catEntryId';
				catEntryId.value=prodObj.catentryId;
				orderItemAddForm.appendChild(catEntryId);
				
				var itemQuantity = document.createElement('input');
				itemQuantity.type='hidden';
				itemQuantity.name='quantity';
				itemQuantity.value=numOfItems;
				orderItemAddForm.appendChild(itemQuantity);
				
				// additional continue shopping info :: start
				var fastFinderForm = document.getElementById('FastFinderForm');
				var catId = fastFinderForm.elements['categoryId'].value;
				if(catId == null || catId.length<1){
					catId = getCookie("CategoryPage");
				}
				
				var partnumber = document.createElement('input');
				partnumber.type='hidden';
				partnumber.name='partnumber';
				partnumber.value=prodObj.partnumber;
				orderItemAddForm.appendChild(partnumber);
				
				var continueShoppingUrl = document.createElement('input');
				continueShoppingUrl.type='hidden';
				continueShoppingUrl.name='continueShoppingUrl';
				continueShoppingUrl.value=document.URL;
				orderItemAddForm.appendChild(continueShoppingUrl);
				
				var categoryId = document.createElement('input');
				categoryId.type = 'hidden';
				categoryId.name = 'cmCategoryId';
				categoryId.value = catId;
				orderItemAddForm.appendChild(categoryId);
				//alert('partnumber: '+ partnumber.value + ', url: ' + getCookie("previousCategoryUrl") + ', catId: ' + categoryId.value);
				// additional continue shopping info :: end
				
				//document.getElementById('orderItemAddFormCatEntryId').value = prodObj.catentryId;
				//document.getElementById('orderItemAddFormQuantity').value = numOfItems;
				Add2ShopCart(document.OrderItemAddForm, prodObj.catentryId ,1);
				//document.OrderItemAddForm.submit();
			}
			else
				alert('please add at least one item to your shopping bag');

		}
		myButton.className= "search_btn";
		addToBagPar.appendChild(myButton);
		var br3 = document.createElement("br");
		addToBagPar.appendChild(br3);
		var br5 = document.createElement("br");
		addToBagPar.appendChild(br5);

//		myDiv.appendChild(
//		myButton = createElementWithClass('button','search_new_btn');
//		myButton = createElementWithClass('input','ButtonDiv');
		
		
		
		
		
//		myButton = createElementWithClass("BUTTON", "search_new_btn");
	/*	
		myButton = createElementWithClickEvent("button","addToCart(this.name);");
		var buttext = document.createTextNode('add to bag');
		myButton.appendChild(buttext);
		myButton.setAttribute("className","search_new_btn");
		myButton.setAttribute("class","search_new_btn");		
		myButton.setAttribute("name","catBrowse"+productObj.identifierCounter);
		myButton.value="add to cart";
	*/	
		
		
		/*var priceDisplayLink = document.createElement("a");
		namepricePar.appendChild(priceDisplayLink);
		priceDisplayLink.setAttribute("href", "#");
		priceDisplayLink.setAttribute("id", "WC_PriceDisplay_Link_" + productObj.identifierCounter);
		
		
		
		var br7 = document.createElement("br");
		namepricePar.appendChild(br7);*/
		//var priceImg = document.createElement("img");
		//priceDisplayLink.appendChild(priceImg);
		//priceImg.setAttribute("src", imagePath + "empty.gif");
		//priceImg.setAttribute("alt", "The price is");
		//priceImg.setAttribute("width", "1");
		//priceImg.setAttribute("height", "1");
		//priceImg.setAttribute("border", "0");
			}

/**********************************************************
* Function to create a ProductQuickView widget using JavaScript. 
*
*	Parameters:
*	productObj - the WC product object
* identifierString - the identifier of the catalog entry element
* pageLength - the number of products to be shown on the page
* modBClass - the class for the top level div
* showCompare - show the compare icon in the tooltip
* imagePath - the image path
* storeId - the store id
* catalogId - the catalog id
**********************************************************/
function createProductWidget_old(productObj, identifierString, pageLength, modBClass, imagePath, storeId, catalogId) 
{

		var modBDiv = createElementWithClass("div", modBClass);

		var productContainerDiv = createElementWithClassAndEvents("div", "product_container", (productObj.identifierCounter % pageLength) + 1);
		modBDiv.appendChild(productContainerDiv);
		//productContainerDiv.setAttribute("onmouseover", "showHideLayers('quickview_img" + ((productObj.identifierCounter % pageLength) + 1) + "','','show')");
		//productContainerDiv.setAttribute("onmouseout", "showHideLayers('quickview_img" + ((productObj.identifierCounter % pageLength) + 1) + "','','hide')");
		
		var productQuickViewDiv = createElementWithClass("div", "order_item_product_quick_view");
		productContainerDiv.appendChild(productQuickViewDiv);
		productQuickViewDiv.setAttribute("dojoType", "ProductQuickView");
		productQuickViewDiv.setAttribute("dragAndDrop", "true");
		productQuickViewDiv.setAttribute("showBaseContent", "false");
		productQuickViewDiv.setAttribute("identifier", identifierString + productObj.identifierCounter);
				
		var baseContentDiv = document.createElement("div");
		productQuickViewDiv.appendChild(baseContentDiv);
		baseContentDiv.setAttribute("dojoType", "BaseContent");
		baseContentDiv.setAttribute("dragAndDrop", "true");
		baseContentDiv.setAttribute("dragSourceId", identifierString + productObj.identifierCounter);
		baseContentDiv.setAttribute("dragSourceType", productObj.dragSourceType);
		baseContentDiv.setAttribute("catentryId", productObj.catentryId);

		
		var imagediv = createElementWithClass("div", "product_img");
		baseContentDiv.appendChild(imagediv);


		var catBrowseDiv = document.createElement("div");
		imagediv.appendChild(catBrowseDiv);
		catBrowseDiv.setAttribute("id", "catBrowse" + productObj.identifierCounter);
		catBrowseDiv.setAttribute("align", "center");

		var imagelink = document.createElement("a");
		catBrowseDiv.appendChild(imagelink);
		imagelink.setAttribute("href", productObj.productDisplayURL);
		imagelink.setAttribute("id", "imgcatBrowse" + productObj.identifierCounter);

		var image1 = document.createElement("img");
		imagelink.appendChild(image1);
		image1.setAttribute("src", productObj.mediumImageSrc);
		image1.setAttribute("alt", productObj.shortDescription);
		image1.setAttribute("border", "0");
		image1.setAttribute("width", "125");
		image1.setAttribute("height", "168");

		var quickViewImgDiv = document.createElement("div");
		productContainerDiv.appendChild(quickViewImgDiv);
		quickViewImgDiv.setAttribute("id", "quickview_img" + ((productObj.identifierCounter % pageLength) + 1));

		
		var sneakPeekLink = createElementWithClickEvent("a", "top.sneakPeek(" + productObj.catentryId + ", '" + productObj.SKU + "');return false;");
		quickViewImgDiv.appendChild(sneakPeekLink);
		sneakPeekLink.setAttribute("href", "#");
		//sneakPeekLink.setAttribute("onclick", "top.sneakPeek(" + productObj.catentryId + ");return false;");
		
		var namepricePar = createElementWithClass("p", "category_p");
		modBDiv.appendChild(namepricePar);


		var nameNode = document.createElement("b");
		namepricePar.appendChild(nameNode);
		var productname = document.createTextNode(productObj.name);
		nameNode.appendChild(productname)
		var br1 = document.createElement("br");
		namepricePar.appendChild(br1);
		var productshortdesc = document.createTextNode(productObj.shortDescription);
		namepricePar.appendChild(productshortdesc);
		var br2 = document.createElement("br");
		namepricePar.appendChild(br2);
		var price = document.createTextNode(productObj.displayPrice);
		namepricePar.appendChild(price);
		var priceDisplayLink = document.createElement("a");
		namepricePar.appendChild(priceDisplayLink);
		priceDisplayLink.setAttribute("href", "#");
		priceDisplayLink.setAttribute("id", "WC_PriceDisplay_Link_" + productObj.identifierCounter);
		
		var priceImg = document.createElement("img");
		priceDisplayLink.appendChild(priceImg);
		priceImg.setAttribute("src", imagePath + "empty.gif");
		priceImg.setAttribute("alt", "The price is");
		priceImg.setAttribute("width", "1");
		priceImg.setAttribute("height", "1");
		priceImg.setAttribute("border", "0");
		
		return modBDiv;
}

/**********************************************************
* Function to create a product tool tip.
**********************************************************/
function createProductToolTipWidget(productObj, identifierString, showCompare, imagePath, storeId, catalogId) 
{
		var tooltipDiv = document.createElement("div");
		tooltipDiv.setAttribute("id", "productToolTip" + productObj.identifierCounter);
		tooltipDiv.setAttribute("dojoType", "ToolTipContent");
		tooltipDiv.setAttribute("connectId", identifierString + productObj.identifierCounter);
		tooltipDiv.setAttribute("toggle", "explode");
		tooltipDiv.setAttribute("toggleDuration", "250");
		tooltipDiv.style.display = "none";
		
		var tooltipHeaderDiv = document.createElement("div");
		tooltipHeaderDiv.setAttribute("id", "tooltipHeaderDiv" + productObj.identifierCounter);
		tooltipHeaderDiv.setAttribute("className", "popup_header");
		tooltipHeaderDiv.setAttribute("class", "popup_header");
		
		var tooltipHeaderNameDiv = document.createElement("div");
		tooltipHeaderNameDiv.setAttribute("id", "tooltipHeaderNameDiv" + productObj.identifierCounter);
		tooltipHeaderNameDiv.setAttribute("className", "popup_headerTitle");
		tooltipHeaderNameDiv.setAttribute("class", "popup_headerTitle");
		var tooltipTitle = document.createTextNode("Product Details");
		tooltipHeaderNameDiv.appendChild(tooltipTitle);
		tooltipHeaderDiv.appendChild(tooltipHeaderNameDiv);
		
		var tooltipContentDiv = document.createElement("div");
		tooltipContentDiv.setAttribute("id", "tooltipContentDiv" + productObj.identifierCounter);
		tooltipContentDiv.setAttribute("className", "popup_content");
		tooltipContentDiv.setAttribute("class", "popup_content");

		var table1 = document.createElement("table");
		table1.setAttribute("border", "0");
		table1.setAttribute("width", "285");

		var tbody1 = document.createElement("tbody");
		tbody1.setAttribute("id", identifierString + productObj.identifierCounter + "TTTable");
		table1.appendChild(tbody1);
	
		var tr1 = document.createElement("tr");
		tr1.setAttribute("id", identifierString + productObj.identifierCounter + "TTTr");	
		
		var td1 = document.createElement("td");
		td1.setAttribute("id", identifierString + productObj.identifierCounter + "TTTd");
		td1.setAttribute("align", "center");
		td1.setAttribute("valign", "top");
		td1.setAttribute("width", "90");
		td1.style.paddingTop = "4px";
		td1.style.paddingLeft = "10px";
		
		var imagelink = document.createElement("a");
		imagelink.setAttribute("href", productObj.productDisplayURL);
		imagelink.setAttribute("id", "imgcatBrowse" + productObj.identifierCounter);
		var image1 = document.createElement("img");
		image1.setAttribute("src", productObj.mediumImageSrc);
		image1.setAttribute("alt", productObj.shortDescription);
		image1.setAttribute("border", "0");
		image1.setAttribute("className", "popup_iconimages");
		image1.setAttribute("class", "popup_iconimages");
		image1.style.marginBottom = "3px";
		imagelink.appendChild(image1);

		var cartlink = document.createElement("a");
		cartlink.setAttribute("href", "javascript: Add2ShopCartAjax('" + identifierString + productObj.identifierCounter + "')");
		cartlink.setAttribute("id", "cartlink" + productObj.identifierCounter);
		var cartimage = document.createElement("img");
		cartimage.setAttribute("src", imagePath + "quickcart.gif");
		cartimage.setAttribute("alt", "add to cart");
		cartimage.setAttribute("border", "0");
		cartimage.setAttribute("className", "popup_iconimages");
		cartimage.setAttribute("class", "popup_iconimages");
		cartlink.appendChild(cartimage);
		
		var listlink = document.createElement("a");
		listlink.setAttribute("href", "javascript: Add2WishListAjax('" + identifierString + productObj.identifierCounter + "')");
		listlink.setAttribute("id", "listlink" + productObj.identifierCounter);
		var listimage = document.createElement("img");
		listimage.setAttribute("src", imagePath + "wishlist.gif");
		listimage.setAttribute("alt", "add to list");
		listimage.setAttribute("border", "0");
		listimage.setAttribute("className", "popup_iconimages");
		listimage.setAttribute("class", "popup_iconimages");
		listlink.appendChild(listimage);

		var br1 = document.createElement("br");
		td1.appendChild(imagelink);
		td1.appendChild(br1);
		td1.appendChild(cartlink);
		td1.appendChild(listlink);
				
		if (showCompare) {
			var comparelink = document.createElement("a");
			comparelink.setAttribute("href", "javascript: Add2CompareAjax('" + identifierString + productObj.identifierCounter + "')");
			comparelink.setAttribute("id", "comparelink" + productObj.identifierCounter);
			var compareimage = document.createElement("img");
			compareimage.setAttribute("src", imagePath + "compare.gif");
			compareimage.setAttribute("alt", "compare");
			compareimage.setAttribute("border", "0");
			comparelink.appendChild(compareimage);
			td1.appendChild(comparelink);
		}
		
		tr1.appendChild(td1);
		
		var td2 = document.createElement("td");
		td2.setAttribute("id", identifierString + productObj.identifierCounter + "TTTd2");
		td2.setAttribute("valign", "top");
		td2.style.paddingTop = "5px";
		td2.style.paddingLeft = "0px";
		
		var namediv = document.createElement("div");
		namediv.setAttribute("id", identifierString + productObj.identifierCounter + "NameDiv");
		namediv.setAttribute("className", "popup_productname");
		namediv.setAttribute("class", "popup_productname");
		var productname = document.createTextNode(productObj.name);
		namediv.appendChild(productname);
		
		var pricediv = document.createElement("div");
		pricediv.setAttribute("id", identifierString + productObj.identifierCounter + "PriceDiv");
		pricediv.setAttribute("className", "popup_productprice");
		pricediv.setAttribute("class", "popup_productprice");
		var price = document.createTextNode(productObj.displayPrice);
		pricediv.appendChild(price);

		var sdescdiv = document.createElement("div");
		sdescdiv.setAttribute("id", identifierString + productObj.identifierCounter + "SDescDiv");
		sdescdiv.setAttribute("className", "popup_productother");
		sdescdiv.setAttribute("class", "popup_productother");
		var productdesc = document.createTextNode(productObj.shortDescription);
		sdescdiv.appendChild(productdesc);
		
		var input1 = document.createElement("input");
		input1.setAttribute("id", "formParamsStoreId_" + identifierString + productObj.identifierCounter);
		input1.setAttribute("name", "formParamsStoreId_" + identifierString + productObj.identifierCounter);
		input1.setAttribute("type", "hidden");
		input1.setAttribute("value", storeId);

		var input2 = document.createElement("input");
		input2.setAttribute("id", "formParamsCatalogId_" + identifierString + productObj.identifierCounter);
		input2.setAttribute("name", "formParamsCatalogId_" + identifierString + productObj.identifierCounter);
		input2.setAttribute("type", "hidden");
		input2.setAttribute("value", catalogId);

		var input3 = document.createElement("input");
		input3.setAttribute("id", "formParamsCatEntryId_" + identifierString + productObj.identifierCounter);
		input3.setAttribute("name", "formParamsCatEntryId_" + identifierString + productObj.identifierCounter);
		input3.setAttribute("type", "hidden");
		input3.setAttribute("value", productObj.catentryId);
		
		td2.appendChild(namediv);
		td2.appendChild(pricediv);
		td2.appendChild(sdescdiv);
		td2.appendChild(input1);
		td2.appendChild(input2);
		td2.appendChild(input3);
		tr1.appendChild(td2);
		tbody1.appendChild(tr1);
		tooltipContentDiv.appendChild(table1);

		var tooltipFooterDiv = document.createElement("div");
		tooltipFooterDiv.setAttribute("id", "tooltipFooterDiv" + productObj.identifierCounter);
		tooltipFooterDiv.setAttribute("className", "popup_footer");
		tooltipFooterDiv.setAttribute("class", "popup_footer");

		tooltipDiv.appendChild(tooltipHeaderDiv);
		tooltipDiv.appendChild(tooltipContentDiv);
		tooltipDiv.appendChild(tooltipFooterDiv);
		
		return tooltipDiv;
}

/**********************************************************
* Function to create a WC product object with useful parameters for the Web 2.0 store.
**********************************************************/	
function WCProduct() 
{
		this.catentryId = "";
		this.SKU = "";
		this.name = "";
		this.identifierCounter = "";
		this.shortDescription = "";
		this.smallImageSrc = "";
		this.mediumImageSrc = "";
		this.price = "";
		this.offerPrice = "";
		this.listPrice = "";
		this.brand = "";
		this.features = new Array();
		this.productDisplayURL = "";
		this.dragSourceType = "";
		this.baseContentHTML = "";
		this.catentryRank = "";
		this.categorySequence = "";
		//needed for coremetrics
		this.partnumber = "";
		this.variationTheme = "";
		//needed for ratings
		this.ratingImageSrc = "";
		this.reviews = "";
		this.parentId = "";
		this.noReviewImageSrc = "/images/randrstars/noreview.gif";
		//for product display
		this.longDescription = "";
		this.swatchImageSrc = "";
		this.largeImageSrc = "";
		this.buyable="";
		this.attribute = "";
		this.extraLargeImageSrc = "";
}
/**********************************************************
* Function to retrieve a cookie.
**********************************************************/	
function getCookie(c_name){
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) { 
		    c_start=c_start + c_name.length+1; 
		    c_end=document.cookie.indexOf(";",c_start);
		    if (c_end==-1) c_end=document.cookie.length;
				return unescape(document.cookie.substring(c_start,c_end));
		}
  	}
  	return "";
}


/**********************************************************
			* Function to create a ProductQuickView widget using JavaScript. 
			*
			*	Parameters:
			*	productObj - the WC product object
			* identifierString - the identifier of the catalog entry element
			* imageSize - the size of the image
			* showCompare - show the compare icon in the tooltip
			* imagePath - the image path
			* viewType - "image" or "detailed" view
			* storeId - the store id
			* catalogId - the catalog id
			**********************************************************/
			function createProductWidgetForPromotionDiscount(parentObject, buttonDiv, productObj, identifierString, pageLength, modBClass, imagePath, storeId, catalogId, outerModBClass) 
			{
					var _id = identifierString + productObj.identifierCounter;
					var widgetHTML = ""
					
					if(productObj.baseContentHTML == null || productObj.baseContentHTML == ""){
						widgetHTML = ""	
										+"			<div class='product_img'>\n"
										+"				<div id='catBrowse"+ productObj.identifierCounter + "' align='center'>\n"
										+"					<a href='javascript:void(0);' onClick='Add2ShopCartFromDiscount("+productObj.catentryId+" ,1);' id='imgcatBrowse"+ productObj.identifierCounter + "'>\n"
										+"						<img src='"+ productObj.mediumImageSrc +"' alt='"+ productObj.shortDescription +"' border='0' width='125' height='168'/>\n"
										+"					</a>\n"
										+"				</div>\n"
										+"			</div>\n";
					}else{
						widgetHTML = productObj.baseContentHTML;
					}
					
					if(outerModBClass != null){
					
					var outerModBDiv = createElementWithClass("div", outerModBClass);
					parentObject.appendChild(outerModBDiv);
					var modBDiv = createElementWithClass("div", modBClass);
					modBDiv.setAttribute("id","product_"+identifierString);
					outerModBDiv.appendChild(modBDiv);
					}
					else{
					var modBDiv = createElementWithClass("div", modBClass);
					modBDiv.setAttribute("id","product_"+identifierString);
					parentObject.appendChild(modBDiv);
					}
					var productContainerDiv = createElementWithClassAndEvents("div", "product_container", (productObj.identifierCounter % pageLength) + 1);
					modBDiv.appendChild(productContainerDiv);
							
					var productQuickViewDiv = createElementWithClass("div", "order_item_product_quick_view");
					productContainerDiv.appendChild(productQuickViewDiv);
									
					// create the ProductQuickView
					var productQuickViewWidget = dojo.widget.createWidget("ProductQuickView", {
																								id: "productQuickView" + productObj.identifierCounter,
																								defaultContent: false,
																								identifier: _id
																								}, productQuickViewDiv);

					var baseContentDiv = document.createElement("div");
					productQuickViewWidget.domNode.appendChild(baseContentDiv);

					var baseContentWidget = dojo.widget.createWidget("BaseContent", {
																						id: "productQuickViewBase" + productObj.identifierCounter,
																						dragSourceId: _id,
																						dragSourceType: productObj.dragSourceType,
																						catentryId: productObj.catentryId,
																						SKU: productObj.SKU,
																						partnumber: productObj.partnumber,
																						variationTheme: productObj.variationTheme,
																						userInnerHtml: widgetHTML
																						}, baseContentDiv);
																											
		var quickViewImgDiv = document.createElement("div");
		productContainerDiv.appendChild(quickViewImgDiv);
		quickViewImgDiv.setAttribute("id", "quickview_img" + ((productObj.identifierCounter % pageLength) + 1));

		
		var sneakPeekLink = createElementWithClickEvent("a", "top.sneakPeek(" + productObj.catentryId + ", '" + productObj.SKU + "');return false;");
		quickViewImgDiv.appendChild(sneakPeekLink);
		sneakPeekLink.setAttribute("href", "#");
		//sneakPeekLink.setAttribute("onclick", "top.sneakPeek(" + productObj.catentryId + ");return false;");
		var namepriceDiv = createElementWithClass("div", modBClass);
		modBDiv.appendChild(namepriceDiv);
		var namepricePar = createElementWithClass("p", "category_p");
		namepriceDiv.appendChild(namepricePar);

		//var nameLink = document.createElement("a");
		var nameLink = createElementWithClickEvent("a","Add2ShopCartFromDiscount("+productObj.catentryId+" ,1);");
		namepricePar.appendChild(nameLink);
		nameLink.setAttribute("href", "javascript:void(0);");
		nameLink.setAttribute("text-decoration","none");
		var nameNode = document.createElement("b");
		nameLink.appendChild(nameNode);
		var productname = document.createTextNode(productObj.name);
		nameNode.appendChild(productname)
		var br1 = document.createElement("br");
		namepricePar.appendChild(br1);
		var productshortdesc = document.createTextNode(productObj.shortDescription);
		namepricePar.appendChild(productshortdesc);
		var br2 = document.createElement("br");
		namepricePar.appendChild(br2);
		var price = document.createTextNode(productObj.displayPrice);
		namepricePar.appendChild(price);
		
		
		//bazaar voice ratings and reviews
		//alert(productObj.ratingImageSrc);
		if(productObj.ratingImageSrc != undefined) {
			if(productObj.ratingImageSrc != "") {
				namepricePar.appendChild(document.createElement("br"));
				var ratingLink = createElementWithClickEvent("a","DisplayProduct('"+ productObj.productDisplayURL+"');");
				//var ratingLink = document.createElement("a");
				ratingLink.setAttribute("href", "javascript:void(0);");
				namepricePar.appendChild(ratingLink);
				//ratingLink.setAttribute("href", productObj.productDisplayURL);
				var ratingImg = document.createElement("img");
				ratingLink.appendChild(ratingImg);
				ratingImg.setAttribute("src",productObj.ratingImageSrc);
				ratingImg.setAttribute("border","0");
			}
		}
		if(productObj.reviews != undefined) {
			if(productObj.reviews != "") {
				namepricePar.appendChild(document.createElement("br"));
				var reviewLink = createElementWithClickEvent("a","DisplayProduct('"+ productObj.productDisplayURL+"');");
				reviewLink.setAttribute("href", "javascript:void(0);");
				//var reviewLink = document.createElement("a");
				//reviewLink.setAttribute("href", productObj.productDisplayURL);
				//reviewLink.setAttribute("text-decoration","none");
				var reviewNode = document.createElement("b");
				reviewLink.appendChild(reviewNode);
				
				if(productObj.reviews > 1) {
					var reviewLine = document.createTextNode(productObj.reviews+" reviews");
					reviewNode.appendChild(reviewLine);
				} else if(productObj.reviews == 1) {
					var reviewLine = document.createTextNode(productObj.reviews+" review");
					reviewNode.appendChild(reviewLine);
	
				}
				namepricePar.appendChild(reviewLink);
			} else {
				namepricePar.appendChild(document.createElement("br"));
				var writeReviewLink = createElementWithClickEvent("a","DisplayProduct('"+ productObj.productDisplayURL+"');");
				writeReviewLink.setAttribute("href","javascript:void(0);");
				var noReviewImg = document.createElement("img");
				writeReviewLink.appendChild(noReviewImg);
				noReviewImg.setAttribute("src",productObj.noReviewImageSrc);
				noReviewImg.setAttribute("border","0");
				namepricePar.appendChild(writeReviewLink);
			}
		}
		
		//the new part: dropdown and add to bag button
		if(outerModBClass != null){
			var outerModBDiv = createElementWithClass("div", outerModBClass);
			buttonDiv.appendChild(outerModBDiv);
			var addToBagDiv = createElementWithClass("div", modBClass);
			outerModBDiv.appendChild(addToBagDiv);
		}
		else{
			var addToBagDiv = createElementWithClass("div", modBClass);
			buttonDiv.appendChild(addToBagDiv);
		}
		
		
		
		
		var addToBagPar = createElementWithClass("p", "category_p");
		addToBagDiv.appendChild(addToBagPar);
		/*var br5 = document.createElement("br");
		addToBagPar.appendChild(br5);
		var br3 = document.createElement("br");
		addToBagPar.appendChild(br3);*/

		var myLabel  = document.createElement("label");
		myLabel.innerHTML = "qty:"
		addToBagPar.appendChild(myLabel);		
		var se = document.createElement("select");
		se.setAttribute("id","selectcatBrowse"+productObj.identifierCounter);
		for(var i=0; i<10; i++){
			se.options[i] = new Option(i,i);
		}
		//se.options[1].setAttribute("selected","true");
		se.selectedIndex=1;
		se.setAttribute("class","textbox_with_padding");		 
		var myVar = document.createTextNode(productObj.qty);
		addToBagPar.appendChild(se);
		/*
		var br6 = document.createElement("br");
		addToBagPar.appendChild(br6);
		var br4 = document.createElement("br");
		addToBagPar.appendChild(br4);
		*/
		var myLabel  = document.createElement("label");
		myLabel.innerHTML = '&nbsp;';
		addToBagPar.appendChild(myLabel);		

		var myButton = document.createElement('input');
		myButton.setAttribute("type" , "BUTTON");
		myButton.setAttribute("value", "add");
		myButton.setAttribute("title", "click to add product to bag");
		myButton.setAttribute("name" ,"catBrowse"+productObj.identifierCounter);
		//myButton.setAttribute("onclick", "addToCart(this.name);");
		myButton.onclick=function(){
			var divName = this.name;
			var prodObj = document.getElementById(divName);
			var numOfItems = document.getElementById('select'+divName).value;
			
			if(numOfItems>0){
			
				
				
				//alert('partnumber: '+ prodObj.partnumber + ', url: ' + getCookie("previousCategoryUrl"));
				// additional continue shopping info :: end
				
				//document.getElementById('orderItemAddFormCatEntryId').value = prodObj.catentryId;
				//document.getElementById('orderItemAddFormQuantity').value = numOfItems;
				Add2ShopCartFromDiscount(prodObj.catentryId ,numOfItems);
				//document.OrderItemAddForm.submit();
			}
			else
				alert('please add at least one item to your shopping bag');

		}
		myButton.className= "search_btn";
		addToBagPar.appendChild(myButton);
		var br3 = document.createElement("br");
		addToBagPar.appendChild(br3);
		var br5 = document.createElement("br");
		addToBagPar.appendChild(br5);


			}
