var language;
var country;
var currencySymbol;

function formatPrice(layer, amount, star){   //star is only used for buscaprecios and it's literally a star
    if(!star){
        star="";
    }
    language = document.getElementById("language");
    country = document.getElementById("country");
    currencySymbol = getSymbol();
     if(country.value == "PT" || country.value == "DE" || country.value == "IT" || country.value == "ES"){
		 layer.innerHTML = addCommas(amount, "punto") + " "+currencySymbol+" "+star;
         return;
     }else if (country.value == "FR") {
         layer.innerHTML = addCommas(amount, "blank") + " "+currencySymbol+" "+star;
         return;
     }else if(country.value =="AR" || country.value =="BR" || country.value =="CL" || country.value =="PE" || country.value =="VE"){
         layer.innerHTML = currencySymbol+" "+addCommas(amount, "punto")+" "+star;
         return;
     }else if(country.value =="CH"){
         layer.innerHTML = addCommas(amount, "apostrofe")+" "+currencySymbol+" "+star;
         return;
     }else if (country.value =="CA" && language.value=="fr"){
         layer.innerHTML = currencySymbol+" "+addCommas(amount, "blank")+" "+star;
         return;
     }else{
        layer.innerHTML = currencySymbol+" "+englishFormat(amount, "coma")+" "+star;
        return;
    }
}
function getSymbol(){
    var symbol;
        if(country.value == "FR"||country.value == "DE"||country.value == "PT" ||country.value == "GB" || country.value == "IT"  || country.value == "ES"){
            symbol="&#8364;";
        } else if(country.value == "UK"){
            symbol= "&pound;";
        }else if(country.value == "US" || country.value == "AR" || country.value == "CL" || country.value == "CO" || country.value == "MX"){
            symbol= "$";
        }else if(country.value == "BR"){
            symbol="R$";
        }else if(country.value == "PE"){
            symbol= "S/.";
        }else if(country.value == "CA"){
            symbol= "C$";
        }else if(country.value == "CH"){
            symbol= "Fr.";
        }else if(country.value == "IN"){
            symbol= "Rs";
        }else if(country.value == "VE"){
            symbol= "Bs.F";
        }else if(country.value == "AU"){
			symbol= "AU$";
		}
   
    return symbol;
}
function addCommas(nStr, aSimb)
{
	var simb ="";
    var simb2 ="";
    switch(aSimb){
     case "coma":
        simb =",";
        simb2 =".";
        break;
     case "punto":
        simb =".";
        simb2 =",";
        break;
     case "apostrofe":
        simb ="'";
        simb2 =".";
        break;
    case "blank":
        simb =" ";
        simb2 =",";
        break;
    }
    nStr += "";
	x = nStr.split(".");
	x1 = x[0];
	x2 = x.length > 1 ? simb2 + x[1] : "";
	var rgx = /(\d+)(\d{3})/;

	while (rgx.test(x1))
	{
		x1 = x1.replace(rgx, "$1" + simb + "$2");
	}

	return x1 + x2;
}

function englishFormat(amount)
{
    var delimiter = ",";
    amount=""+amount;
    var a = amount.split('.', 2)
    var d = a[1];
    var i = parseInt(a[0]);
    if (isNaN(i)) {
        return '';
    }
    var minus = '';
    if (i < 0) {
        minus = '-';
    }
    i = Math.abs(i);
    var n = new String(i);
    var a = [];
    while (n.length > 3)
    {
        var nn = n.substr(n.length - 3);
        a.unshift(nn);
        n = n.substr(0, n.length - 3);
    }
    if (n.length > 0) {
        a.unshift(n);
    }
    n = a.join(delimiter);
    if (d.length < 1) {
        amount = n;
    }
    else {
        amount = n + '.' + d;
    }
    amount = minus + amount;
    return amount;
}
