function refreshPricesStockAutoAttach () {
	/*for (i = 0; selectx = document.getElementsByTagName("*")[i]; i++) {
		if (hasClass(selectx, 'select-attribute')) {
			selectx.onchange = function () { refreshPrices(); }
		}
	}
	refreshPrices();
	pricediv = document.getElementById('PriceDiv');
	setInterval('PriceFloat()', 100);*/
	var a = document.getElementsByTagName("form");
	for(var i = 0; i < a.length; i++) {
		if (a[i].className.match('productForm')) {
			refreshPrices(a[i].elements[0]);
		}
	}
}

function refreshPrices (self) {
	var nodeId = self.form.elements['edit[nodeId]'].value;
	var baseprice = self.form.elements['edit[basePrice]'].value;
	var surchargeUrlSafe;
	var attributes = '';
	var attribute;
	var forbidden = '';
	var categories = '';
	var surcharge = parseFloat(baseprice);
	var stock_toggle = true;
	var forbidden_toggle = false;
	for (i = 0; selectx = self.form.getElementsByTagName("*")[i]; i++) {
		//if(selectx.form.elements['edit[nodeId]'].value != nodeId) {continue;}
		if (!hasClass(selectx, 'select-attribute')) {continue;}
		if (hasClass(selectx, 'form-radio') && !selectx.checked) {continue;}
		vid=selectx.name.replace(/edit\[variations\]\[/,"");
		vid=vid.replace(/\]/,"");
		vid=vid.replace('[',"");
		//attribute=variations[vid]['attributes'][selectx.value];
		attribute=variations[nodeId][vid]['attributes'][selectx.value];
		surcharge+=parseFloat(attribute['surcharge']);
		if (forbidden && attribute['forbidden_category']){forbidden+=','+attribute['forbidden_category'];}
		else if (attribute['forbidden_category']){forbidden=attribute['forbidden_category'];}
		if (categories && attribute['category']){categories+=','+attribute['category'];}
		else if (attribute['category']){categories=attribute['category'];}
		//if (attribute['image']){document.getElementById('img_num_' + vid ).src = attribute['image'];}
		//else {document.getElementById('img_num_' + vid ).src = var_image[vid];}
		if (stock_toggle) {stock = parseInt(attribute['stock']);stock_toggle = false;}
		else {
			if (stock > parseInt(attribute['stock']) || (stock == -1 && stock < parseInt(attribute['stock'])) ) {stock = parseInt(attribute['stock']);}
		}
	}
	forbidden=forbidden.split(',');
	categories=categories.split(',');
	f_len = forbidden.length;
	c_len = categories.length;
	for (f=0; f<f_len; f++) {
		for (c=0; c<c_len; c++) {
			if (categories[c]==forbidden[f] && categories[c]!='') {
				stock= 0;
				forbidden_toggle = true;
				break;
			}
		}
	}
	if (forbidden_toggle) {self.form.elements['edit[newprice]'].value = message_forbidden;}
	else {
		if (stock != 0) {
			//<craftyspace+>
			surchargeUrlSafe = String(surcharge);
			surchargeUrlSafe = surchargeUrlSafe.replace(/\.+/,'-')
			//</craftyspace+>
			//document.getElementById('edit-newprice').value = HTTPGet(url_ajax + 'permutation_price;'+ surchargeUrlSafe, this.receive, this) ;
			self.form.elements['edit[newprice]'].value = HTTPGet(url_ajax + 'permutation_price;'+ surchargeUrlSafe, this.receive, this) ;
		}
		else {self.form.elements['edit[newprice]'].value = message_sold_out;}
	}
	for (i = 0; button = document.getElementsByName('op')[i]; i++) {
		if (hasClass(button, 'addtocart')) {button.disabled = (!stock);}
	}
}

function PriceFloat() {
	pricediv.style.width = '180px';
	pricediv.style.top = (window.pageYOffset + 200) + 'px';
	pricediv.style.left = (window.innerWidth -20- parseInt(pricediv.style.width)) + 'px';
}

if (isJsEnabled()) {
	addLoadEvent(refreshPricesStockAutoAttach);
}