var sGoodsPriceArrayName  = 'aGoodsPrice';
var sGoodsNumberArrayName = 'aGoodsNumber';

var sHighlightString = '#E2FEE2';
var sNormalString    = '#FFFFFF';

/**
 * Onload initialization
 *
 * Call all page start JavaScript functions
 *
 * @access public
 */

function onloadInit() {
	updateCartSummary();
	if(document.getElementById('PollHolder')) {
		xajax_ajaxUCard();
	}
}

/**
 * Fix png alpha in IE
 *
 * Fix png alpha in IE 5.5, 6.0
 *
 * @access public
 */

function fixPng(element) {
	if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent)) {
		var src;
		if ((element.tagName=='IMG') || (element.tagName=='INPUT')) {
			if (/\.png$/.test(element.src))	{
				src = element.src;
				element.src = "pictures/system/blank.gif";
			}
		} else {
			// src = element.currentStyle.backgroundImage.match(/url\("(.+\.png)"\)/i)
			// if (src) {
			//	src = src[1];
			//	element.runtimeStyle.backgroundImage="none";
			// }
		}
		
		if (src) {
			element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
		}
	}
}

function setCookie(sName, mValue, sExpires, sPath, sDomain, sSecure) {
	if (!mValue) {
		deleteCookie(sName);
		return;
	}
	document.cookie = sName + "=" + escape(mValue) +
		((sExpires) ? "; expires=" + sExpires : "") +
		((sPath) ? "; path=" + sPath : "") +
		((sDomain) ? "; domain=" + sDomain : "") +
		((sSecure) ? "; secure" : "");
}

function getCookie(sName) {
	sCookieName = sName + "=";
	iCookieLength = document.cookie.length;
	iCookieBegin = 0;

	while (iCookieBegin < iCookieLength) {
		iValueBegin = iCookieBegin + sCookieName.length;
		if (document.cookie.substring(iCookieBegin, iValueBegin) == sCookieName) {
			var iValueEnd = document.cookie.indexOf (";", iValueBegin);
			if (iValueEnd == -1) {
				iValueEnd = iCookieLength;
			}
			return unescape(document.cookie.substring(iValueBegin, iValueEnd));
		}
		iCookieBegin = document.cookie.indexOf(" ", iCookieBegin) + 1;
		if (iCookieBegin == 0) {
			break;
		}
	}
	return null;
}

function deleteCookie(sName, sPath, sDomain) {
	// IF cookie exists
	if (getCookie(sName)) {
		// Set Cookie in the past
		setCookie(sName, ' ', 'Mon, 01-Jan-2001 00:00:00 GMT');
	}
}

function saveArray(sArrayName, aSavedData) {
	var sCookieValue = '';
	var bFirst = true;
	if (aSavedData) {
		for (i in aSavedData) {
			if (aSavedData[i] != null) {
				if (!bFirst) {
					sCookieValue = sCookieValue + ",";
				}
				sCookieValue = sCookieValue + i + ':' + aSavedData[i];
			}
			var bFirst = false;
		}
		setCookie(sArrayName, sCookieValue);
	}
}

function getArray(sArrayName) {
	var aRestoredData = getCookie(sArrayName);
	if (aRestoredData == null) {
		aTmp = new Array();
		return aTmp;
	}
	aRestoredData = aRestoredData.split(",");
	aResultArray = new Array();
	for (i in aRestoredData) {
		aElemetArray = new Array();
		aElemetArray = aRestoredData[i].split(":");
		aResultArray[aElemetArray[0]] = aElemetArray[1];
	}

	return aResultArray;
}

function saveCartData(iGoodsId, fGoodsPrice) {
	aGoodsPrice = getArray(sGoodsPriceArrayName);
	aGoodsNumber = getArray(sGoodsNumberArrayName);

	if (aGoodsPrice[iGoodsId] != null) {
		/* Delete goods */
		var aTmpPrice = new Array();
		var aTmpNumber = new Array();
		for(iKey in aGoodsPrice) {
			if (iKey != iGoodsId) {
				aTmpPrice[iKey] = aGoodsPrice[iKey];
				aTmpNumber[iKey] = aGoodsNumber[iKey];
			}

		}

		aGoodsPrice = aTmpPrice;

		aGoodsNumber = aTmpNumber;

		pGoodsDescr = document.getElementById('GoodsDescr'+iGoodsId)
		if (pGoodsDescr) {
			pGoodsDescr.style.backgroundColor = sNormalString;
		}
		pGoodsPrice = document.getElementById('GoodsPrice'+iGoodsId)
		if (pGoodsPrice) {
			pGoodsPrice.style.backgroundColor = sNormalString;
		}

	} else {
		// Add goods
		aGoodsPrice[iGoodsId] = fGoodsPrice;
		aGoodsNumber[iGoodsId] = 1;
	}

	saveArray(sGoodsPriceArrayName, aGoodsPrice);
	saveArray(sGoodsNumberArrayName, aGoodsNumber);
	updateCartSummary();
}

function updateCartSummary() {
	aGoodsPrice = getArray(sGoodsPriceArrayName);
	aGoodsNumber = getArray(sGoodsNumberArrayName);
	iGoodsCount = 0;
	iGoodsSummaryPrice = 0;

	for(iKey in aGoodsPrice) {
		iGoodsCount += Number(aGoodsNumber[iKey]);
		iGoodsSummaryPrice = iGoodsSummaryPrice + Number(aGoodsNumber[iKey]) * Number(aGoodsPrice[iKey]);

		pGoodsDescr = document.getElementById('GoodsDescr'+iKey)
		if (pGoodsDescr) {
			pGoodsDescr.style.backgroundColor = sHighlightString;
		}
		pGoodsPrice = document.getElementById('GoodsPrice'+iKey)
		if (pGoodsPrice) {
			pGoodsPrice.style.backgroundColor = sHighlightString;
		}
	}
	if (iGoodsCount) {
		pGoodsCount = document.getElementById('GoodsCount');
		pGoodsPrice = document.getElementById('GoodsPrice');
		if ((pGoodsCount) && (pGoodsPrice)) {
			pGoodsCount.innerHTML = iGoodsCount;
			fPrice = Math.round(iGoodsSummaryPrice*100);
			pGoodsPrice.innerHTML = fPrice/100;
			if (Math.round(fPrice/100) == fPrice/100) {
				pGoodsPrice.innerHTML = pGoodsPrice.innerHTML + '.00';
			} else if (Math.round(fPrice/10) == fPrice/10) {
				pGoodsPrice.innerHTML = pGoodsPrice.innerHTML + '0';
			} 
		}

	}

	pCartSummaryBlock = document.getElementById('CartSummary');
	if (pCartSummaryBlock) {
		if (!iGoodsCount) {
			pCartSummaryBlock.style.display = 'none';
		} else {
			pCartSummaryBlock.style.display = 'block';
		}
	}
}

/* For catalog details and top position blocks */
function switchAddToCart(iGoodsId) {
	aGoods = getArray(sGoodsPriceArrayName);

	pAddToCartBlock = document.getElementById('AddToCart'+iGoodsId)
	if (pAddToCartBlock) {
		pAddToCartBlock.style.display = aGoods[iGoodsId] ? 'none' : 'block';
	}

	pAlreadyInCartBlock = document.getElementById('AlreadyInCart'+iGoodsId)
	if (pAlreadyInCartBlock) {
		pAlreadyInCartBlock.style.display = aGoods[iGoodsId] ? 'block' : 'none';
	}
}