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() {

	var myDataSource = new YAHOO.util.DataSource("getPrice.ajx");
	myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;
	myDataSource.maxCacheEntries = 100;
	myDataSource.responseSchema = {
		resultsList: "Result",
	    fields: ["inStock","minOrderQty","listPrice","price","compCode"]
	};
	
	num = new NumberFormat();
	num.setInputDecimal('.');
	num.setPlaces('2', false);
	num.setCurrency(true);
	num.setCurrencyPosition(num.LEFT_OUTSIDE);
	num.setNegativeFormat(num.LEFT_DASH);
	num.setNegativeRed(false);
	num.setSeparators(true, ',', ',');
	
 	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",
			message : "Item was added to cart.",
			success : "success",
			addItemButtonName : "instantadd",
			addItemButtonNameNumber : "_instantadd_number",
			inStockName : "_inStock_number",
			minOrderQty: "_minOrderQty_number",
			spanQty: "_span_qty_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 _callBack = function (oRequest, oResponse) {
			  		if (oResponse) {
			  			var obj = oResponse.results[0];
			  			this.showPrice(obj, countCalls, savePriceClassName);
			  			this.showStockMinOrder(obj, countCalls);
			  			this.showStockValue(obj, number);
				  		this.priceGetEnd = true;
				  		countCalls++;
				  		if (countCalls < number) {
				  			myDataSource.sendRequest(queryArray[countCalls], _callBack, this);
				  		}
			  		}
		  		}
		  		
		  		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;		  		
		  		}
		  		myDataSource.sendRequest(queryArray[countCalls], _callBack, this);
		  	},

			/**
			 * 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 _callBack = function (oRequest, oResponse) {
			  		if (oResponse) {
			  			var obj = oResponse.results[0];
			  			this.showPrice(obj, number);
			  			this.showStockInnerHTML(obj, number);
				  		this.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;		  		
		  		myDataSource.sendRequest(query, _callBack, this);	
		  	},
		  	
		  	/**
		  	 * 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) {
		  		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) {
					if(storePrice.price>0) {
						if((storePrice.listPrice - storePrice.price) <= 0) {
							num.setNumber(storePrice.price);
					    	priceEl.innerHTML = num.toFormatted();
							this.setVisible(listPriceEl, false);
							this.setVisible(priceInfoEl, false);
							this.setVisible(priceEl, true);
							this.setVisible(addItemEl, true);
						} else {
							num.setNumber(storePrice.listPrice);
		  					listPriceEl.innerHTML = num.toFormatted();
							num.setNumber(storePrice.price);
		  					priceEl.innerHTML = num.toFormatted();
		  					this.setVisible(listPriceEl, true);
		  					this.setVisible(priceInfoEl, true);
							this.setVisible(priceEl, true);
							this.setVisible(addItemEl, true);
						}
					} else if(storePrice.price==0) {
						if(storePrice.listPrice==0) {
							this.callForPrice(listPriceEl, priceInfoEl, priceEl, addItemEl);
						} else if(storePrice.listPrice>0) {
							num.setNumber(storePrice.listPrice);
		  					listPriceEl.innerHTML = num.toFormatted();
							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 inStockEl = this.getElement(inStockName);
				if (inStockEl != undefined) {
					if(!this.isAllAttributesSeted(storePrice.compCode, number)) {
			  			this.hideStockInnerHTML(inStockEl);
			  		} else {
			  			Element.show(inStockEl);
						var availability="Availability";
					    var availabilityEl=this.getElement("availabilitynameid");
					    if(availabilityEl && availabilityEl.value!="")
					    	availability=availabilityEl.value;
					    
					    var invtemplate="<p>[1]:</p><span>[2]</span>";
					    
						if ((storePrice.inStock - 0) > 0) {
							inStockEl.innerHTML = invtemplate.replace("[1]",availability).replace("[2]",storePrice.inStock);
						} else {
						    var outOfStock= "Out of Stock";
							var outOfStockEl=this.getElement("outofstocknameid");
							if(outOfStockEl)
								outOfStock = outOfStockEl.value;
							
							inStockEl.innerHTML=invtemplate.replace("[1]",availability).replace("[2]",outOfStock);
						}
					}
		  		}
		  	},
		  	
		  	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) {
		  			this.setVisible(spanQtyEl, true);
		  			hidden.value = "true";
		  		} else if (outOfStockMessage != undefined) {
		  			spanQtyEl.innerHTML = outOfStockMessage;
		  			this.setVisible(spanQtyEl, true);
		  		}
		  		
		  	},		  	
		  	
		  	/**
		  	 * 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;
			}
	}
	return attribPriceAjx;
}

priceAjx = new AttribPriceAjx();

/**
 * Reset all parameters such as attributes and check boxes after
 * successfull adding product to basket.
 */
function resetAttributes() {
    var messageDest = document.getElementById(getMessageID());
    if(messageDest) {
    	messageDest.innerHTML = priceAjx.message;
    }
	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();
}

/**
 * change image source add button to adding image
 */
function animatedGif4Success(){
	var source = getSource_Element();
	var imgsrc = "";
	var addingImg = "adding.gif";
	var old_audio_img = "099addtocart.png";
	var old_video_img = "149adding.gif";
	
	if(source && source.tagName=="IMG") {
		imgsrc = source.src;
		if(imgsrc && imgsrc != "") {
		  img_index = imgsrc.lastIndexOf("/");
		  if(img_index>0)
			  source.src = source.src.substring(0,img_index+1)+addingImg;
		}
	}
	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();
}

function changeGIF4SuccessAdd(imgSrc) {
	var source = document.getElementById(imgSrc.imgSrc);
	var itemAddedImg = "itemadded.gif";
	if(source && source.tagName=="IMG") {
	    source.title='Item added'
		if(source.src && source.src != "") {
		  img_index = source.src.lastIndexOf("/");
		  if(img_index>0){
	    	 source.src = source.src.substring(0,img_index+1)+itemAddedImg;
		  }
		}
	}
}

function alreadyInCartImg() {
	var source = getSource_Element();
	var alreadyInBasketImg = "alreadyincart.gif";
	if(source && source.tagName=="IMG") {
		if(source.src && source.src != "") {
		  img_index = source.src.lastIndexOf("/");
		  if(img_index>0)
	    	 source.src = source.src.substring(0,img_index+1)+alreadyInBasketImg;
		}
	}
}

function showMiniBasket(imgSrc) {
	changeGIF4SuccessAdd(imgSrc);
	var sUrl = "minibasket.ajx" + "?t=" + new Date().getTime();
	var Check=new net.ContentLoader(sUrl,callbackShowBasket,"get","text",null);
}

function callbackShowBasket()
 { 
	       var html="";
	       var txt=this.req.responseText;
	       var res=eval('('+txt+')');
	       if(res.status == 'success'){
		  if(res.result[0].qty == '0'){
			html+="<b>You have no items</b>"; 
          }
		  else {
		    html+="<b>"+res.result[0].qty+" item";
		    if(res.result[0].qty != '1')
		       html+="s";
		    html+=" : </b>";
		    html+="<b>$"+res.result[0].pricewithdiscount+"</b>";
		  }
		  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) {
			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) {
				if(storePrice.price>0) {
					if((storePrice.listPrice - storePrice.price) <= 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 {
						num.setNumber(storePrice.listPrice);
	  					listPriceEl.innerHTML = num.toFormatted();
						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(storePrice.listPrice>0) {
						num.setNumber(storePrice.listPrice);
	  					listPriceEl.innerHTML = num.toFormatted();
						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;
}