function getProductId(element)
{
	return element.id.substr(element.id.indexOf('_') +1);
}

function changeButtons()
{
    //clearBasket();
    var newCurrency = $("#ccy").val();
	currSymbol = $("#ccy")[0][$("#ccy")[0].selectedIndex].text.match(/\((.+)\)/)[1];
	currCode = $("#ccy")[0][$("#ccy")[0].selectedIndex].text.match(/(.+) \(/)[1];
	$("#currencySymbol").text(currSymbol);
	var pricesFields=[".Price",".ServicePrice"];
	var i=0;
	for(var i=0;i<pricesFields.length;i++)
	{
	prices=$(pricesFields[i]);
	for (index=0;index<prices.length;index++) 
	{
                price=prices[index];
		productPrice = priceList[getProductId(price) + '_' + newCurrency];
		if(typeof(productPrice) == "undefined")
			productPriceText = "";
		else
			productPriceText= productPrice[i]
		price.innerHTML = productPriceText;
	}
		
	}
	if($("td.Buy").length!=0)
	{	
		buttonsBuy = $("td.Buy").children();
		for (index=0;index<buttonsBuy.length;index++) 
		{
			bb = buttonsBuy[index];
			bb.href = bb.href.replace(/[\\?]currency=([^&#]*)/, "?currency="+currCode );
		}
	}
}

function getPackList(productId)
{
	var result=$("#PackList_" + productId);	
	return result;
}
function updatePack(productId)
{
	var bb=$("a#BuyLink_" + productId);	
	var packList=getPackList(productId);
	currentPack=-1;
	if(packList)
	{
		currentPack=packList.val();
		if(currentPack=="")
			currentPack=0;		 
		bb.attr("href", bb.attr("href").replace(/&pack=([0-9]*)/, "&pack="+currentPack ));
	}
	return currentPack;
}

function onBuyProduct(event)
{
    	var productId=getProductId(this);	
	currentPack = updatePack(productId);	
	if(currentPack == 0)
	{
		alert("Select pack.");
		event.preventDefault();
	}

}
function onSelectPack()
{       	
	var productId=getProductId(this);	
	updatePack(productId);

}
function hasPackList(element)
{
	return getPackList(getProductId(element)).length!=0;
}

function toggleFeature() { 
	tDesc = $(this).parent().next();
	showField = !tDesc.hasClass("featureSelected");
	$('tr.featureDescription').removeClass("featureSelected"); //hideAll
	if(showField)
		tDesc.addClass('featureSelected');//showElement
}
$(document).ready(function()
{
	changeButtons();
	$('#ccy').each(function(){$(this).change(changeButtons)});
	$('th.feature').click(toggleFeature);
	$('.packBox').each(function(){ $ (this).change(onSelectPack)});
	$('.packBox').each(function(){ $ (this).click(onSelectPack)});
	$('.buyLink').each(function(){ 	if(hasPackList(this))$(this).click(onBuyProduct)});
});
