function roundCurrency(price){
	var roundPrice = Math.ceil(price);
	var priceString = new String(roundPrice);
	var priceLen = priceString.length;
	var getLastVal = priceString.substring(priceLen-1);
	if(getLastVal > 0){
		getDiff = (10 - parseInt(getLastVal));
		priceString = new String((parseInt(priceString)+ getDiff));
		priceLen = priceString.length;
	}
	switch(priceLen){
		case 4:
			subString = 1;
		break;
		case 5:
			subString = 2;
		break;
		case 6:
			subString = 3;
		break;
		case 7:
			subString = 4;
		break;
		default:
			subString = 0;
		break;
	}
	var start = priceString.substring(0,subString);
	var middle = '';
	if(subString == 4){
		start = priceString.substring(0,1);
		var middle = priceString.substring(1,subString) + ',';
	}
	var end = priceString.substring(subString, priceLen);
	if(priceLen >= 4){
		start += ',';
	}
	return start + middle + end;
	//return (Math.round(price * 100) / 100).toFixed(2)
}

function scDoCurrency(){
	if(scCookieValue('exchangeRate') == '' || scCookieValue('exchangeRate') == '0'){
		var scriptTag = document.createElement('script');
		scriptTag.type = 'text/javascript';
		scriptTag.src = 'http://www.noospheracr.com/lib/vinum/convertor.htm';
		document.getElementsByTagName('head')[0].appendChild(scriptTag);
	}
}

function scCookieValue(cookie){
	var scCookie = '';
	if (document.cookie.length>0){
		var cookieName = cookie;
		var cookieNameStart=document.cookie.indexOf(cookieName+"=")
		if (cookieNameStart!=-1){
			cookieNameStart=cookieNameStart + cookieName.length+1 
			var cookieNameEnd=document.cookie.indexOf(";",cookieNameStart)
			if (cookieNameEnd==-1){
				cookieNameEnd=document.cookie.length
			}
			scCookie = document.cookie.substring(cookieNameStart,cookieNameEnd);
		}
	}
	return scCookie;
}

function displayCurrency(){
	var displayExchange = 0;
	if(scCookieValue('exchangeRate') && (scCookieValue('exchangeRate') != '' || scCookieValue('exchangeRate') != '0')){
		displayExchange = scCookieValue('exchangeRate');
	}else if(window.exchangeRate){
		displayExchange = exchangeRate
	}
	return displayExchange;
}

function writePagingColons(price){
	var displayExchange = 0;
	if(displayCurrency() != 0){
		displayExchange = '&cent;' + roundCurrency((displayCurrency() * parseFloat(price)));
	}
	return displayExchange;
}

function writeColons(){
	if(window.currencyIds && displayCurrency() != 0){
		var oDivsLen = window.currencyIds.oDivs.length;
		var pos = 0;
		var price = 0;
		var saleprice = 0;
		var parseCurrency = parseFloat(displayCurrency());
		while(pos < oDivsLen){
			if(currencyIds.oDivs[pos]){
				var divId = document.getElementById(currencyIds.oDivs[pos].divid);
				if(divId){
					divId.innerHTML = '&cent; ' + roundCurrency((parseCurrency  *  parseFloat(currencyIds.oDivs[pos].price)));
				}
				if(currencyIds.oDivs[pos].divid == 'pinfoPrice'){
					price = parseFloat(currencyIds.oDivs[pos].price);
					priceEx = (parseCurrency  *  price);
				}
				if(currencyIds.oDivs[pos].divid == 'pinfoSalePrice'){
					saleprice = parseFloat(currencyIds.oDivs[pos].price);
					salepriceEx = (parseCurrency  *  saleprice);
				}
			}
			pos++;
		}
		if(price != 0 && saleprice != 0){
			var youSaveDiv = document.getElementById('pinfoYouSave');
			if(youSaveDiv){
				youSaveDiv.innerHTML = '&cent;' + roundCurrency((priceEx - salepriceEx));
			}
		}
		var CrcDiv = document.getElementById('CrcDiv');
		if(CrcDiv){
			var CrcInput = document.createElement('input');
			CrcInput.name = 'Costa Rica Colon:';
			CrcInput.type = 'hidden';
			if(saleprice != 0){
				CrcInput.value = '&cent;' + roundCurrency((parseCurrency * saleprice));
			}else{
				CrcInput.value = '&cent;' + roundCurrency((parseCurrency * price));
			}
			CrcDiv.appendChild(CrcInput);
		}
	}
}
