GLOBAL_MESSAGES_OUTPUT = 'success';

Item = function(_number, _itemCode) {
	var item = {
		number : _number,
		itemCode : _itemCode
	}
	return item;
}
var Url = {

    // public method for url encoding
    encode : function (string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode : function (string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
       // string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {
            var c = string.charCodeAt(n);
            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            } else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;
        while ( i < utftext.length ) {
            c = utftext.charCodeAt(i);
            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            } else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            } else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }
}

AttribPriceAjx = function() {

	function createGetPriceAjaxAction() {
		var action = new SimpleAjaxAction('getPrice.ajx', 'GET');
		return action; 
	}
	
	function executeGetPriceAjaxAction(params, callback) {
		var action = createGetPriceAjaxAction();
		action.execute('getPrice', params, callback);
	}
	
	if(typeof NumberFormat != "undefined" && NumberFormat) {
		num = new NumberFormat();
		num.setInputDecimal('.');
		num.setPlaces('2', false);
		num.setCurrency(false);
		num.setCurrencyPosition(num.LEFT_OUTSIDE);
		num.setNegativeFormat(num.LEFT_DASH);
		num.setNegativeRed(false);
		num.setSeparators(true, ',', ',');
	} else {
		num = {};
	}
	
 	var attribPriceAjx = {
 	
 			listPriceName : "_listPrice_number",
 			priceName : "_price_number",
			attributeCountName : "_attributeCount_number",
			vendorIdName : "vendorId",
			currencyIdName : "c",
			qtyName : "basketItems[number].quantity",
			qtyNameId: "qty_number",
			attributesNames : "basketItems[number].attributes[$].optionId",
			calcedItems: [],
			priceGetEnd: false,
			mainClass : "STYLE6",
			adClass: "STYLE7",
			priceInfoName: "_priceInfo_number",
			itemAccessoriseName : "basketItems[$].process",
			itemToProcessName : "basketItems[$].itemToProcess",
			checkAll : "basketItemsAllProcess",
			itemsCountName : "itemscount",
			success : "success",
			addItemButtonName : "instantadd",
			addItemButtonNameNumber : "_instantadd_number",
			inStockName : "_inStock_number",
			inStockMsg:"_instock_number_msg",
			inStock:"stock_number",
			minOrderQty: "_minOrderQty_number",
			spanQty: "_span_qty_number",
			spanMinOrder: "_span_min_order_number",
			attributedesc:"_atts_number",
			/**
			 * add parameters to query for AJAX request
			 * @param query - returned query
			 * @param itemCode - item code
			 * @param number - item number on form
			*/
			formAjaxQuery: function(query, itemCode, number) {
		  		if(query && query != "")
		  			this.push(new Item(number, itemCode));
		  		var vendorId = document.getElementById(this.vendorIdName);
		  		query = this.appendParamTo(query, vendorId, "vendorId");
				query = this.appendParamTo(query, itemCode, "itemCode");
		  		var currencyId = document.getElementById(this.currencyIdName);
		  		query = this.appendParamTo(query, currencyId, "currencyId");
		  		var qtyRealName = this.qtyName.replace("number", number+"");
	  			var qty = document.getElementsByName(qtyRealName)[0];
		  		query = this.appendParamTo(query, qty, "qty");
		  		query = this.appendParamTo(query, number, "number");
		  		return query;
			},

			/**
			 * @param attributesArray - array of attributes, 
			 * for example [[1Y, 21], [1Y, 22], [2Y, 21],[2Y, 22]]
			 * @param itemCode - item code
			 */
			getStockInfoArray: function(attributesArray, itemCode, savePriceClassName) {
				//Function as resolve a response for AJAX XHTMLRequest
				var oSelf = this;
		  		var _callBack = function (result) {
			  		if (result) {
			  			oSelf.showPrice(result, countCalls, savePriceClassName);
			  			oSelf.showStockMinOrder(result, countCalls);
			  			oSelf.showStockValue(result, countCalls);
			  			oSelf.priceGetEnd = true;
				  		countCalls++;
				  		if (countCalls < number) {
				  			executeGetPriceAjaxAction(queryArray[countCalls], _callBack);
				  		}
			  		}
		  		}
		  		
		  		var count = 2;
		  		var query = "";
		  		var queryArray = new Array();
		  		var countCalls = 0;
		  		for(var number= 0; number<attributesArray.length;number++) {
			  		query = "?"+this.getAttributeFromArray(count,attributesArray[number]);
			  		query = this.formAjaxQuery(query, itemCode, number);
					this.priceGetEnd = false;
					queryArray[number] = query;		  		
		  		}
		  		executeGetPriceAjaxAction(queryArray[countCalls], _callBack);
		  	},

			/**
			 * Get price for item by itemCode and others parameters
			 * on form by ajax request.
			 * @param number - item number on form
			 * @param itemCode - item code
			 */			 
			getPriceByAttributes : function (number, itemCode) {
				//Function as resolve a response for AJAX XHTMLRequest
					  		
		  		var oSelf = this;
		  		var _callBack = function (result) {
			  		if (result) {
			  			oSelf.showPrice(result, number);
			  			oSelf.showStockInnerHTML(result, number);
			  			oSelf.priceGetEnd = true;
			  		}
		  		}
		  		var count = this.getElementValueDef(
		  			this.attributeCountName.replace("number", number+""), 1);
		  		var query = "?";
		  		query += this.getAttributeFrom(count,
		  					this.attributesNames.replace("number", number+""));
		  		query = this.formAjaxQuery(query, itemCode, number);
				this.priceGetEnd = false;		  		
				executeGetPriceAjaxAction(query, _callBack);
		  	},
		  	
		  	/**
		  	 * Show price by price object storePrce[listPrice, price].
		  	 * We define four rules for showing price (P - price, LP - listPrice):
		  	 * 1. P > 0, LP <= P: show only price.
		  	 * 2. P > 0, LP > P: show listPrice and price.
		  	 * 3. P = 0, LP = 0: show call for pricing information.
		  	 * 4. P = 0, LP > 0: show listPrice and call for pricing info.
		  	 * @param storePrice - price object[listPrice, price]
		  	 * @param number - number item on form
		  	 */
		  	showPrice : function (storePrice, number) {
		  		if(num.setCurrencyValue)
		  			num.setCurrencyValue('$');
		  		var listPriceName = this.listPriceName.replace("number", number);
			  	var priceName = this.priceName.replace("number",  number);
			  	var priceInfoName = this.priceInfoName.replace("number", number);
			  	var listPriceEl = document.getElementById(listPriceName);
			  	var priceEl = document.getElementById(priceName);
			  	var priceInfoEl = document.getElementById(priceInfoName);
			  	var addItemEl = this.getElement(this.addItemButtonName);
				var price1 = storePrice.price;
				var listPrice1 = storePrice.listPrice;
			  	if(storePrice.price && storePrice.listPrice) {
			  		var digitPrice = storePrice.price.replace(",", "");
			  		digitPrice = digitPrice.replace(" ", "");
                    for(var i = 0;i<digitPrice.length;i++){
                        if(digitPrice.charAt(i)<'0'||digitPrice.charAt(i)>'9'){   
                        	digitPrice = digitPrice.substring(1);
                        }else{
                            break;
                        }
                    }
			  		var digitListPrice = storePrice.listPrice.replace(",", "");
			  		digitListPrice = digitListPrice.replace(" ", "");
			  		if(digitPrice>0) {
						if((digitListPrice - digitPrice) <= 0) {
							priceEl.innerHTML = storePrice.price;
							this.setVisible(listPriceEl, false);
							this.setVisible(priceInfoEl, false);
							this.setVisible(priceEl, true);
							this.setVisible(addItemEl, true);
						} else {
		  					listPriceEl.innerHTML = storePrice.listPrice
							priceEl.innerHTML = storePrice.price;
		  					this.setVisible(listPriceEl, true);
		  					this.setVisible(priceInfoEl, true);
							this.setVisible(priceEl, true);
							this.setVisible(addItemEl, true);
						}
					} else if(digitPrice == 0) {
						if(digitListPrice == 0) {
							this.callForPrice(listPriceEl, priceInfoEl, priceEl, addItemEl);
						} else if(digitListPrice > 0) {
		  					listPriceEl.innerHTML = storePrice.listPrice;
							priceEl.innerHTML = "Call for pricing";
							this.setVisible(listPriceEl, true);
							this.setVisible(priceInfoEl, false);
							this.setVisible(priceEl, true);
							this.setVisible(addItemEl, false);
						} else {
							this.callForPrice(listPriceEl, priceInfoEl, priceEl, addItemEl);
						}
					} else {
						
						this.callForPrice(listPriceEl, priceInfoEl, priceEl, addItemEl);
					}		  			
	  			} else {
	  				this.callForPrice(listPriceEl, priceInfoEl, priceEl, addItemEl);
	  			}
		  	},
		  	showStockInnerHTML : function (storePrice, number) {
		  		var inStockName = this.inStockName.replace("number", number);
				var inStockPriceEl = this.getElement(inStockName);
				var inStockMsg = this.inStockMsg.replace("number", number);
				var inStockMsgEl = this.getElement(inStockMsg);
				var inStock = this.inStock.replace("number", number);
				var inStockEl = this.getElement(inStock);
				if(storePrice.hide==false){
					if (inStockPriceEl != undefined && inStockMsgEl != undefined && inStockEl != undefined) {
			  			Element.show(inStockPriceEl);
			  			Element.show(inStockEl);
			  			var qtyRealName = this.qtyName.replace("number", number);
			  			var qtyEl = this.getElement(qtyRealName);
			  			if(qtyEl!=null && qtyEl != undefined && storePrice.minOrderQty > 0)
			  				qtyEl.value = storePrice.minOrderQty;
						var availability="Availability";
					    var availabilityEl=this.getElement("availabilitynameid");
					    if(availabilityEl && availabilityEl.value!="")
					    	availability=availabilityEl.value;
					    inStockMsgEl.innerHTML=availability;
						if ((storePrice.inStock - 0) > 0) {
							inStockPriceEl.innerHTML = storePrice.inStock;
						} else if (inStockPriceEl != undefined){
						    var outOfStock= "Out of Stock";
							var outOfStockEl=this.getElement("outofstocknameid");
							if(outOfStockEl)
								outOfStock = outOfStockEl.value;
							inStockPriceEl.innerHTML=outOfStock;
						}
			  		}
				}else if (inStockEl != undefined){
					Element.hide(inStockEl);
				}
		  	},
		  	
		  	hideStockInnerHTML : function(el) {
		  		Element.hide(el);
		  	},
		  	isAllAttributesSeted : function(compCode, number) {
		  		var count = 0;
		  		if(compCode.indexOf(".") > 0) {
		  			count = compCode.split("-").length;
		  		}
		  		return count == this.getElementValueDef(
			  			this.attributeCountName.replace("number", number+""), 1) - 1;
		  	},
		  	showStockValue : function (storePrice, number) {
		  		var inStockName = this.inStockName.replace("number", number);
				var inStockEl = this.getElement(inStockName);
				if (inStockEl != undefined) {
					if ((storePrice.inStock - 0) > 0) {
						inStockEl.value = storePrice.inStock;
					} else {
						inStockEl.value = "OUT OF STOCK";
					}
				}
		  	},
		  	
		  	
		  	/**
		  	 * Show  quantity products in stock and minimal order of products
		  	 * @param storePrice - price object[listPrice, price, minOrder, inStock]
		  	 * @param number - number item on form
		  	 */
		  	showStockMinOrder : function (storePrice, number) {
			  	var inStockName = this.inStockName.replace("number", number);
			  	var minOrderQtyName = this.minOrderQty.replace("number", number);	  
			  	var inStockEl = this.getElement(inStockName);
			  	var minOrderQtyEl = this.getElement(minOrderQtyName);
			  	var addItemByttonRealName = this.addItemButtonNameNumber.replace("number", number);
			  	var addItemButton = this.getElement(addItemByttonRealName);
				
				minOrderQtyEl.value = storePrice.minOrderQty;
				inStockEl.value = storePrice.inStock;
				//set qty equals minimal order
				var qtyRealId = this.qtyNameId.replace("number", number);
		  		var qtyEl = this.getElement(qtyRealId);
		  		qtyEl.value = storePrice.minOrderQty;
			  	var spanQtyName = this.spanQty.replace("number", number);		  		
		  		var spanQtyEl = this.getElement(spanQtyName);
	  			var currParam = priceAjx.itemToProcessName.replace("\$", number+"");
				var hidden = document.getElementsByName(currParam)[0];

		  		if ((inStockEl.value -0) > 0) {
		  			if(AvaliableMessage != undefined)
		  				spanQtyEl.innerHTML = AvaliableMessage+" : "+inStockEl.value;
		  			this.setVisible(spanQtyEl, true);
		  			hidden.value = "true";
		  		} else if (outOfStockMessage != undefined) {
		  			spanQtyEl.innerHTML = outOfStockMessage;
		  			this.setVisible(spanQtyEl, true);
		  		}
		  		
		  		if ((storePrice.minOrderQty - 0) < 0) {
					var minOrderQtyDivName = this.spanMinOrder.replace("number", number);
					minOrderQtyDiv = this.getElement(minOrderQtyDivName);
					this.setVisible(minOrderQtyDiv, false);
				}
		  		
		  	},		  	
		  	
		  	/**
		  	 * Call for pricing variant in show price.
		  	 * @param listPriceEl - listPrice element. 
		  	 * @param priceInfoEl - information elemnt.
		  	 * @param priceEl - price element.
		  	 */
		  	callForPrice : function (listPriceEl, priceInfoEl, priceEl, addItemEl) {
		  		priceEl.innerHTML = "Call for pricing";
				this.setVisible(listPriceEl, false);
				this.setVisible(priceInfoEl, false);
				this.setVisible(priceEl, true);
				this.setVisible(addItemEl, false);
		  	},
		  	
		  	isVisible : function (element) {
		  		return "none" == element.style.display;
		  	},
		  	
		  	setVisible : function (element, value) {
		  		if(element) {
		  			var valueVis = (value == true)? "inline": "none";
		  			element.style.display = valueVis;
		  		}
		  	},
		  	
		   /**
		  	* For append parameters to request that handle StoreAttributeAjxController
		  	* @param query - query in request for StoreAttributeAjxController
		  	* @param param - object in javascript
		  	* @param paramName - name in request
		  	*/
		  	appendParamTo : function (query, param, paramName) {
		  		try {
		  			query += "&";
			  		query += paramName + "=" + ((param.value)? Url.encode(param.value) : Url.encode(param));
			  	} catch (err) {
			  	
			  	}
		  		return query;	  		
		  	},
		  	
		  	/**
		  	 * Push Item(number, itemcode) into stack by number
		  	 * @param item Item
		  	 */
		  	push : function (item) {
		  		for(var i = 0; i < this.calcedItems.length; i++) {
		  			var curItem = this.calcedItems.pop();
		  			if(curItem.number == item.number) {
		  				this.calcedItems.push(item);
		  				return;
		  			}
		  			this.calcedItems.push(curItem);
		  		}
		  		this.calcedItems.push(item);
		  	},
		  	
		  	/**
		  	 * Get value from query by it name.
		  	 * @param query - url query
		  	 * @param paramName - name of parameter in query
		  	 * @return param value
		  	 */
		  	getParamValueFrom : function (query, paramName) {
		  		var posParamName = query.indexOf(paramName) + paramName.length + 1;
		  		if(posParamName < 0) return;
		  		var endParamValue = query.indexOf("&", posParamName);
		  		if(endParamValue < 0)
		  			endParamValue = query.length;
		  		var paramValue = query.substring(posParamName, endParamValue);
		  		return paramValue		
		  	},

		  	/**
		  	* This function begin to form string query, that
		  	* transferred in request
		  	* @param count - count of attributes
		  	* @param paramName - string (usually "basketItems[0].attributes[$].optionId") 
		  	*in wich symbol '&' replace to number of attrbute
		  	*/
			getAttributeFrom : function (count, paramName) {
				var query = "";
				for(var i = 0; i < count; i++) {
					var currParam = paramName.replace("\$", i+"");
					var selectAttr = document.getElementById(currParam);
					if(selectAttr) {
						var attribute = selectAttr.options[selectAttr.selectedIndex].title;
						if(attribute) {
							query += "attribute" + "=" + attribute;
							query += "&"; 
						}
					}
				}
				query = query.substring(0, query.length-1);
				return query;
			},

		  	/**
		  	* This function begin to form string query, that
		  	* transferred in request
		  	* @param count - count of attributes
		  	* @param smallAttributeArray - array ["attribute1", "attribute2"]
		  	*in wich symbol '&' replace to number of attrbute
		  	*/
			getAttributeFromArray : function (count, smallAttributeArray) {
				var query = "";
				for(var i = 0; i < count; i++) {
					var attribute = smallAttributeArray[i];
					if(attribute) {
						query += "attribute" + "=" + attribute;
						query += "&"; 
					}
				}
				query = query.substring(0, query.length-1);
				return query;
			},			
			
			
			getElement : function (name) {
				var el = document.getElementById(name);
				if(!el) {
					var arr = document.getElementsByName(name);
					if(arr.length > 0)
						el = arr[0];
				}
				return el;
			},
			
			/**
			 * Get value from element by name. Get element from form
			 * by get element by id or get element by name or return null.
			 * @param name - name of parameter
			 * @return paramter value
			 */
			getElementValue : function (name) {
				var el = this.getElement(name);
				if(el && el.value)
					return el.value
				else 
					return null;
			},
			
			/**
			 * Get element value or return default value
			 * @param name - name of parameter
			 * @param defaultValue - default value if value null
			 * @return value of element or default value
			 */
			getElementValueDef : function (name, defaultValue) {
				var result = this.getElementValue(name);
				if(result == null)
					result = defaultValue
				return result;
			},
			
			
			/**
			* function for check minimal order quantity and in stock quantity 
			* after change in quantity products field
			* @number - number of form element
			* 	that has information about quantity in stock and minimal order
			*	quantity for currenct product
			*/
			checkStockAndMinOrder : function(number) {
				var inStockName = this.inStockName.replace("number", number);
			  	var minOrderQtyName = this.minOrderQty.replace("number", number);	  
			  	var inStockEl = this.getElement(inStockName);
			  	var minOrderQtyEl = this.getElement(minOrderQtyName);
			  	var qtyRealId = this.qtyNameId.replace("number", number);
		  		var qtyEl = this.getElement(qtyRealId);
			
				if ((qtyEl.value - inStockEl.value) > 0) {
					alert ('OUT OF STOCK /n in storehouse are ' + inStockEl.value+ ' products');
					qtyEl.value = inStockEl.value;
					qtyEl.focus();
					return;
				}
				if ((qtyEl.value - minOrderQtyEl.value) < 0) {
					alert('minimal quantity order for this product is: ' + minOrderQtyEl.value);
					qtyEl.value = minOrderQtyEl.value;
					qtyEl.focus();
					return;
				}
			},
			
			/**
			* if quantity of products less or equals zero - set itemToProcess = false.
			* if no items with quantity not equals 0 - return exception
			* @count - number of variant main item with attributes
			* @return true if quantity > 0 though for one item with attributes, 
			*/
			checkForQtyBeforeAdd: function(count) {
				var statusQty = false;
				for(var number = 0;number<count; number++ ) {
					var minOrderQtyName = this.minOrderQty.replace("number", number);
					var minOrderQtyEl = this.getElement(minOrderQtyName);
				  	var qtyRealId = this.qtyNameId.replace("number", number);
			  		var qtyEl = this.getElement(qtyRealId);
					var currParam = priceAjx.itemToProcessName.replace("\$", number+"");
					var hidden = document.getElementsByName(currParam)[0];
					if ((qtyEl == undefined) || ((qtyEl.value - 0) <= 0)) {
						hidden.value = "false";
					} else {
						hidden.value = "true";
						statusQty = true;
					}
				}
				return statusQty;
			},
			
			/**
			* if quantity of products less or equals zero - return false.
			* if no items with quantity not equals 0 - return exception
			* @count - number of variant main item with attributes
			* @return true if quantity > miniorderqty though for one item with attributes, 
			*/
			validateQtyBeforeAdd: function(count) {
				var statusQty = true;
				for(var number = 0;number<count; number++ ) {
					var minOrderQtyName = this.minOrderQty.replace("number", number);
					var minOrderQtyEl = this.getElement(minOrderQtyName);
				  	var qtyRealId = this.qtyNameId.replace("number", number);
			  		var qtyEl = this.getElement(qtyRealId);
			  		var attrsdescId = this.attributedesc.replace("number", number);
			  		var attrsdescEl = this.getElement(attrsdescId);
					var currParam = priceAjx.itemToProcessName.replace("\$", number+"");
					var hidden = document.getElementsByName(currParam)[0];
					if ((qtyEl == undefined) || ((qtyEl.value - 0) <= 0)) {
						hidden.value = "false";
					} else {
						hidden.value = "true";
					}
					if ((qtyEl == undefined) || ((qtyEl.value - 0) < 0) || minOrderQtyEl && (qtyEl.value-minOrderQtyEl.value)<0) {						
						statusQty = false;
						if(errors!=undefined){
							errors.push("Quantity of the item with attributes "+attrsdescEl.value+" must be more than "+minOrderQtyEl.value);
						}else							
							break;
					}
				}
				return statusQty;
			}
	}
	return attribPriceAjx;
}

priceAjx = new AttribPriceAjx();

/**
 * Reset all parameters such as attributes and check boxes after
 * successfull adding product to basket.
 */
function resetAttributes() {
	var attributesNames = priceAjx.attributesNames;
	var itemsCount = getElement("itemscount").value;
	for(var ii = 0; ii < itemsCount; ii++) {
		var currParamItem = attributesNames.replace("number", ii+"");
		try {
			var count = document.getElementById(
				priceAjx.attributeCountName.replace("number", ii+"")).value;
			for(var i = 0; i < count; i++) {
				currParamAttribute = currParamItem.replace("\$", i+"");
				var selectAttr = document.getElementById(currParamAttribute);
				if(selectAttr) {
					selectAttr.selectedIndex = 0;
				}
			}
		} catch(err) {
			
		}
	}
	var qtyEl = document.getElementById(priceAjx.qtyName);
	if(qtyEl) 
		qtyEl.value = 1;
	var checkAllEl = document.getElementById(priceAjx.checkAll);
	if(checkAllEl) 
		checkAllEl.checked = "";
	var countAccessItems = document.getElementsByName(priceAjx.itemsCountName)[0].value - 1;
	for(var i = 0; i < countAccessItems; i++) {
		var currParam = priceAjx.itemAccessoriseName.replace("\$", i+"");
		var checkBoxEl = document.getElementById(currParam);
		if(checkBoxEl && checkBoxEl.checked != ""
					  && checkBoxEl.checked != "false") {
			checkBoxEl.click();
		}
	}
	calcDefaultPrices();
	showMiniBasket();
}

function showMiniBasket() {
	var sUrl = "minibasket.ajx";
	if(typeof net != "undefined" && net &&
			typeof net.ContentLoader != "undefined" && net.ContentLoader) {
		var Check=new net.ContentLoader(sUrl,callbackShowBasket,"get","text",null);
	}
}

function callbackShowBasket()
 { 
	var html="<li class=\"cart\">";
	       var txt=this.req.responseText; 
	       var res=eval('('+txt+')');
	       if(res.status == 'success'){
		  if(res.result[0].qty == '0'){
			html+="Your Shopping Cart is empty"; 
			html+="</li>";
                  }
		  else {
			html+=res.result[0].qty;
                        html+="&nbsp;items in <a href=\"basket.html\">Cart</a>";
                        html+="&nbsp; |&nbsp; Subtotal:";
                        html+="&nbsp;$"+res.result[0].pricewithdiscount; 
			html+="</li>";
			html+="<li>"; 
			html+="<a class=\"checkout-button\" href=\"basket.html\" />";
			html+="<span>Checkout</span>";
			html+="</a></li>";
		  }
		  var minibasket = document.getElementById("minibasket");
		  if(minibasket) 
    			minibasket.innerHTML = html;
	       }    
}


/**
 * Getting default price for all product with non default pricing.
 * This function need synchronisation with ajax requests.
 */
function calcDefaultPrices() {
	try {
		item = this.priceAjx.calcedItems.pop();
		if(item) {
			interval = window.setInterval(priceAjxCalcAndWait, 100);
			startTime = new Date().getTime();
		} else {
			priceAjx.calcedItems = [];
		}
	} catch(err) {
		alert(err);
	}
}

var interval;
var item;
var timeLimit = 3000;
var startTime;

/**
 * Helper function for calcDefaultPrices
 *
 */
function priceAjxCalcAndWait() {
	if(item.number != "toJSONString") {
		if(priceAjx.priceGetEnd) {
			priceAjx.getPriceByAttributes(item.number, item.itemCode);
			clearWaiting();
		}
	}  else {
		clearWaiting();
	}
	if(new Date().getTime() - startTime > timeLimit) {
		clearWaiting();
	}
}

/**
 * If we do not need wait even.
 */
function clearWaiting() {
	window.clearInterval(interval);
	interval = null;
	setTimeout(calcDefaultPrices, 5);
}

function getElement(name) {
	var el = document.getElementById(name);
	if(!el)
		el = document.getElementsByName(name)[0];
	return el;
}

/**
 * Clear element by id (innerHTML).
 * @param elementId - id of element on form.
 */  
function clearMessage(elementId) {
	var element = document.getElementById(elementId);
	if(element) {
		element.innerHTML = "";
	}	
}

/**
 * Need for all checker. If check is true check all elements by class name
 * className or uncheck.
 * @param className - name of class of element.
 * @param check - check or not - boolean.
 */
function selectCheckBoxes(className, check) {
	var elements = document.getElementsByTagName('input');
	var count = 0;
	for(var i = 0; i < elements.length; i++) {
		var input = elements[i];
		if(input.type == 'checkbox') {
			if(input.className == className) {
				input.checked = check;
				count++;
			}
		}
	}
	for(var i = 1; i <= count; i++) {
		var currParam = priceAjx.itemToProcessName.replace("\$", i+"");
		var hidden = document.getElementsByName(currParam)[0];
		if(hidden) {
			hidden.value = check;
		}
	}
}

//check Prototype Class
if (typeof Class != "undefined" && Class) {

	var AttribPriceAjxCents = Class.create();
	AttribPriceAjxCents.prototype = Object.extend(AttribPriceAjx(), {
		priceCentName : "_priceCent_number",
		className: "AttribPriceAjxCents",
		initialize : function() {
			this.priceCentName = "_priceCent_number";
			this.className = "AttribPriceAjxCents";
		},
	  	/**
	  	 * Show price by price object storePrce[listPrice, price].
	  	 * We define four rules for showing price (P - price, LP - listPrice):
	  	 * 1. P > 0, LP <= P: show only price.
	  	 * 2. P > 0, LP > P: show listPrice and price.
	  	 * 3. P = 0, LP = 0: show call for pricing information.
	  	 * 4. P = 0, LP > 0: show listPrice and call for pricing info.
	  	 * @param storePrice - price object[listPrice, price]
	  	 * @param number - number item on form
	  	 */
	  	showPrice : function (storePrice, number, savePriceClassName) {
			if(num.setCurrencyValue) {
				num.setCurrencyValue('');
			}
	  		var listPriceName = this.listPriceName.replace("number", number);
		  	var priceName = this.priceName.replace("number",  number);
		  	var priceCentName = this.priceCentName.replace("number", number);
		  	var priceInfoName = this.priceInfoName.replace("number", number);
		  	var listPriceEl = document.getElementById(listPriceName);
		  	var priceEl = document.getElementById(priceName);
		  	var priceCentElement = document.getElementById(priceCentName);
		  	var priceInfoEl = document.getElementById(priceInfoName);
		  	var addItemEl = this.getElement(this.addItemButtonName);
			var price1 = storePrice.price;
			var listPrice1 = storePrice.listPrice;
		  	if(storePrice.price && storePrice.listPrice) {
		  		var digitPrice = storePrice.price.replace(",", "");
		  		digitPrice = digitPrice.replace(" ", "");
		  		var digitListPrice = storePrice.listPrice.replace(",", "");
		  		digitListPrice = digitListPrice.replace(" ", "");
				if(digitPrice > 0) {
					if((digitListPrice - digitPrice) <= 0) {
						num.setNumber(storePrice.price);
				    	priceEl.innerHTML = num.integerPart();
				    	priceCentElement.innerHTML = num.fractionalPart();
						this.setVisible(listPriceEl, false);
						this.setVisible(priceInfoEl, false);
						this.setVisible(priceEl, true);
						this.setVisible(addItemEl, true);
						this.setVisible(priceCentElement, true);
					} else {
	  					listPriceEl.innerHTML = storePrice.listPrice;
						num.setNumber(storePrice.price);
	  					priceEl.innerHTML = num.integerPart();
	  					priceCentElement.innerHTML = num.fractionalPart();
	  					this.setVisible(listPriceEl, true);
	  					this.setVisible(priceInfoEl, true);
						this.setVisible(priceEl, true);
						this.setVisible(priceCentElement, true);
						this.setVisible(addItemEl, true);
					}
					if (savePriceClassName != undefined) {
						var saveElements = document.getElementsByClassName(savePriceClassName);
						if (saveElements != null) {
							var saveElement = saveElements[number];
							num.setNumber(storePrice.listPrice - storePrice.price);
							saveElement.innerHTML = num.toFormatted();
						}
					}
				} else if(storePrice.price==0) {
					if(storePrice.listPrice==0) {
						this.callForPrice(listPriceEl, priceInfoEl, priceEl, addItemEl);
					} else if(digitListPrice > 0) {
						listPriceEl.innerHTML = storePrice.listPrice;
						priceEl.innerHTML = "Call for pricing";
						this.setVisible(listPriceEl, true);
						this.setVisible(priceInfoEl, false);
						this.setVisible(priceEl, true);
						this.setVisible(priceCentElement, true);
						this.setVisible(addItemEl, false);
					} else {
						this.callForPrice(listPriceEl, priceInfoEl, priceEl, addItemEl);
					}
				} else {
					this.callForPrice(listPriceEl, priceInfoEl, priceEl, addItemEl);
				}		  			
	 			} else {
	 				this.callForPrice(listPriceEl, priceInfoEl, priceEl, addItemEl);
	 			}
	  	}		
	});
	attribPriceAjxCents = new AttribPriceAjxCents();
} else {
	attribPriceAjxCents = null;
}

//stub for pervious version of itemTemplates
XT = {
	doAjaxSubmit : function(eventId, sourceElement, url, form, jsonObject, callback) {
		saveItem(url, form);
	}
};

function saveItem(url, form) {
	var ajaxAction = new FullFormSubmitAjaxAction(url, form, null);
	clearAllMessages();
	ajaxAction.execute(getMode(), handleAddItemResponse);
}

var handleAddItemResponse = function(result) {
	var basketItemId = result.basketItemId;
	if(getRedirectMode() == "true") {
		if(result.redirectUrl) {
			window.location = getBaseLink() + result.redirectUrl;
		} else {
			//debugAlert("Redirect problem. Please try again");
		}
	} else {
		resetAttributes();
	}
}

function getValueByElementName(name) {
	var list = document.getElementsByName(name);
	return list.length > 0? list[0].value: null;
}

function getRedirectMode() {
	var list = document.getElementsByName('redirectMode');
	return list.length > 0? list[0].value: false;
}

function getMode() {
	var modes = document.getElementsByName('mode');
	return modes.length > 0? modes[0].value: null;
}

function getBaseLink() {
	return (typeof baseLink != "undefined" && baseLink)? baseLink: "";
}
