﻿// digit for rounding
decimalRound=2;
RoundDigit=Math.pow(10,decimalRound); //Table 1

decimalRound2=2;
RoundDigit2 =Math.pow(10,decimalRound2); //Table 2

decimalRound3=0;
RoundDigit3 =Math.pow(10,decimalRound3); //Table 2, Buy

//Bullion Values
// put -999  where "Call For Quote"
// note that the name of the coin MUST be the same (formatting inluded) of the one inserted in the first column on the table.
// the formula Will be calculated in this way (see bullion function below):
// e.g.: new bullion("U.S. Gold Eagle Bullion",1,31,81,XAU)--> Buy=1*XAuValue+31; Sell=1*XAUValue+81)
// XAUValue and others value are stored into values.js.
BullionArray = [
new bullion("U.S. Gold Eagle Bullion", 1, 20, 70, XAU),
new bullion("U.S. Gold Buffalo Bullion", 1,  20,  80, XAU),
new bullion("U.S. Gold Buffalo proof, with Box and Certificate", 1, 35, 135, XAU),
new bullion("U.S. Gold Eagle proof, with Box and Certificate", 1, 35, 230, XAU),
new bullion("Canadian Gold Maple Leaf  3(9)s, 4(9)s, 5(9)s", 1, -999, -999, XAU),
new bullion("South African Gold Bullion Krugerrand", 1, 0, 50, XAU),
new bullion("Gold Bullion Bar – PAMP Suisse with Certificate", 1, -10, +40, XAU),
new bullion("Chinese Gold Panda", 1, -999, -999, XAU),
new bullion("Austrian Vienna Philharmonic Gold Bullion", 1, 0, 50, XAU),
new bullion("Austrian Vienna Philharmonics, 0.9999 Fine", 0.1, 10, 25, XAU),
new bullion("Austrian/Hungarian 100 Corona", 0.9802, -22, 27, XAU),
new bullion("Mexican Gold 50 Peso", 1.2057, -999, -999, XAU),
new bullion("British Gold Sovereign, Circulated", 0.2354, +1, +23, XAU),
new bullion("Swiss 20 Franc Helvetia Brilliant, Uncirculated", 0.1867, +1, +23, XAU),
new bullion("French 20 Franc Brilliant, Uncirculated", 0.1867, +1, +23, XAU),

new bullion("$1000 Face 90% Circ Silver Coin Pre-1965", 715, -1613, -140, XAG),
new bullion("$1000 Face 40% Circ Silver Halves 1965-1969", 295, -989, -195, XAG),
new bullion("COMEX Bar, 0.999 Fine", 1000, -1000, +700, XAG),
new bullion("Johnson Matthey/Engelhard Bar, 0.999 Fine", 100, -70, +110, XAG),
new bullion("Generic Silver Bar, 0.999 Fine", 10, -5, +13, XAG),
new bullion("Generic Silver Round, 0.999 Fine",1, -0.5, +1.3, XAG),
new bullion("US Silver Eagles, Box of 500 Coins", 500, 500, +1400, XAG),
new bullion("Canadian Silver Maple Leaf, Box of 500 Coins", 500, 500, +1250, XAG),

new bullion("Australian Koala, 0.9995 Fine", 1, +20, -999, XPT),
new bullion("Australian Platypus, 0.9995 Fine", 1, +30, +90, XPT),
new bullion("Canadian Maple Leaf, 0.9995 Fine", 1, +20, -999, XPT),
new bullion("US Platinum Eagle, 0.9995 Fine", 1, +50, +180, XPT),


new bullion("Canadian Palladium Maple Leaf, 0.999 Fine", 1, -15, +35, XPD),
new bullion("PAMP Suisse Palladium Bar", 1, -25, -999, XPD),



];

function UpdateVal() {
    //alert('reload');
    document.getElementById("data").src = "http://albariancoins.com/wp-includes/js/values.js"
    var table = document.getElementById("table1");
    DeleteTableRows("table1");
    addRowTable1("table1", "Gold", XAU, XAU_change,XAU_change2);
    addRowTable1("table1", "Silver", XAG, XAG_change,XAG_change2);
    addRowTable1("table1", "Platinum", XPT, XPT_change,XPT_change2);
    addRowTable1("table1", "Palladium", XPD, XPD_change,XPD_change2);

    var table = document.getElementById("table2");
    if (table == null) {

    } else {
        for (i = 0; i < BullionArray.length; i++) {
            // addRowTable2("table2", BullionArray[i]);
            changeRowTable("table2", BullionArray[i])
        }
    }
  
}


function DeleteTableRows(tableID) {
    if (document.getElementById(tableID).rows.length > 0) {
        for (var i = document.getElementById(tableID).rows.length; i > 0; i--) {
            document.getElementById(tableID).deleteRow(i - 1);
        }
    }
}
function timedRefresh(timeoutPeriod) {
    var table = document.getElementById("table2");
    if (table == null) {

    } else {
   
    // setTimeout("location.reload(true);", timeoutPeriod);
        setTimeout("UpdateVal();", timeoutPeriod);
    }
}

function addRowTable1(tableID,name,val_new,change,change2) {

    var table = document.getElementById(tableID);
    

    var rowCount = table.rows.length;
    var row = table.insertRow(rowCount);

    var cell1 = row.insertCell(0); //GOLD
    cell1.innerHTML = name;

    var cell2 = row.insertCell(1); //$
    //var v=(Math.round(val_new * RoundDigit) / RoundDigit).toFixed(decimalRound);
	var v=val_new.toFixed(decimalRound);
    cell2.innerHTML = "$" + v.toString();

    var cell3 = row.insertCell(2);
    if (change > 0) {
        cell3.innerHTML = "Up";
    }
    if (change<0) {
        cell3.innerHTML = "Down";
    }
    if (Math.abs(change)<0.01) {
        cell3.innerHTML = "==";
    }
    
	if (change<0) {
        var cell4 = row.insertCell(3);
		change=(Math.abs(change)).toFixed(decimalRound);
		cell4.innerHTML = "-$ "+change.toString();
    }else {
		var cell4 = row.insertCell(3);
		change=(change).toFixed(decimalRound);
		cell4.innerHTML = "+$ "+ change.toString();
	}
   
	
	var cell5 = row.insertCell(4);
    cell5.innerHTML = " (" + change2.toString() + ")";

}

function addRowTable2(tableID,bull) {

    var table = document.getElementById(tableID);

    var rowCount = table.rows.length;
    var row = table.insertRow(rowCount);

    var cell1 = row.insertCell(0); //GOLD
    cell1.innerHTML = bull.name;

    var cell2 = row.insertCell(1); //$
    cell2.innerHTML = bull.oz;

    var cell3 = row.insertCell(2); //$
    cell3.innerHTML = bull.BuyPrice;

    var cell4 = row.insertCell(3); //$
    cell4.innerHTML = bull.SellPrice;
}

function changeRowTable(tableID, met) {
    var table = document.getElementById(tableID);
    var rowCount = table.rows.length;
    for (j = 0; j < rowCount; j++) {
        var elTableCells = table.rows[j].getElementsByTagName("td");
        name = elTableCells[0].innerHTML;
        if (met.name == name) {
            elTableCells[1].innerText = met.oz;
            if (isNaN(met.BuyPrice) == false) {
                elTableCells[2].innerHTML = "$" + met.BuyPrice;  
            } else {
                elTableCells[2].innerHTML = met.BuyPrice;  
            }
            if (isNaN(met.SellPrice)) {
                elTableCells[3].innerHTML = met.SellPrice;
            } else {
                elTableCells[3].innerHTML = "$" + met.SellPrice;
            }
                            
            return;
        }
    }
    
}

function bullion(name,oz,BuyAdd,SellAdd,BaseValue) {
    this.name = name;
    this.oz = oz;
    this.BAdd = BuyAdd;
    this.SAdd = SellAdd;

    if (this.SAdd == -999) {
        this.SellPrice="Call For Quote";
    }else {
        this.SellPrice = (Math.round((BaseValue * oz + this.SAdd) * RoundDigit2) / RoundDigit2).toFixed(2);
    };

    if (this.BAdd == -999) {
        this.BuyPrice = "Call For Quote";
    } else {
        this.BuyPrice = (Math.round((BaseValue * oz + this.BAdd) * RoundDigit3) / RoundDigit3).toFixed(2);
    }

    if (this.oz == 500) {
        this.oz = 1;
    }
}

