var mainImage = "";
var mainWidth = 0;
var mainHeight = 0;
function swapMainImage(anchor, width, height){
	var imgObj = document.getElementById("MainProdImage");
	if (imgObj){
		mainImage = imgObj.src;
		mainWidth = imgObj.width;
		mainHeight = imgObj.height;
		imgObj.src = anchor.href;
		imgObj.width = width;
		imgObj.height = height;
	}
	
};

function restoreMainImage(){
	var imgObj = document.getElementById("MainProdImage");
	if (imgObj){
		imgObj.src = mainImage;
		imgObj.width = mainWidth;
		imgObj.height = mainHeight;
	}
}

function cleanUpPrice(value){
	value = value.replace(/[^-+0-9\.,]+/, '');
	var tmp = value.match(/[-+0-9\.,]+/);
	var p = 0;
	if (tmp && tmp.length > 0 && tmp[0].length > 0){
		p = parseFloat(tmp[0].replace(",", "."));
	}
	return p;
}

function extractPriceFromId(id){
	var matches = id.match(/p_\d+_(.*)/);
	var p = 0;
	if (matches && matches.length == 2){
			p = cleanUpPrice(matches[1]);
	}
	return p;
}

function updatePrice(){
	var total = 0;
	var basePrice = document.getElementById("basePrice");
	if (basePrice){
		total += cleanUpPrice(basePrice.innerHTML);	
	}
	
	var optionList = document.getElementById("Options");
	if (optionList){
		var fields = optionList.getElementsByTagName("select");
		var i = fields.length;
		while (i--){
			var selectBox = fields[i];
			total += extractPriceFromId(selectBox.options[selectBox.selectedIndex].id);
		}
	}
	var price = document.getElementById("price");
	var salePrice = document.getElementById("salePrice");
	if (salePrice && price){
		if (salePrice.innerHTML != ""){
			var prefix = salePrice.innerHTML.substr(0,1);
			salePrice.innerHTML = prefix+""+formatNumber(total);
		}
		else {
			var prefix = price.innerHTML.substr(0,1);
			price.innerHTML = prefix+""+formatNumber(total);
		}
	}
	/*
	var basePrice = document.getElementById("basePrice");
	if (basePrice){
		
	}
	
	var price = document.getElementById("price");
	if (price){
		price
	}*/
}

function formatNumber(tmp) {
    	tmp = (Math.round(tmp*100))/100;
  	  var temp = (tmp == Math.floor(tmp)) ? tmp + ',00' 
              : ( (tmp*10 == Math.floor(tmp*10)) ? 
                       tmp + '0' : tmp);
                    temp = ""+temp;
                    return temp.replace(/\./, ",");
	}

