﻿var GramsIn1TroyOunce = 31.1034768;
var TroyOuncesIn1Kilogram = 32.15074656862798;
var PennyWeightPerTroyOunce = 0.05;
var xmlhttp = false;

var xau = "";
var xag = "";

GetPriceData();
setInterval("GetPriceData()", 30 * 1000);


function GetSpotPrice(instrument) {
    if (xau == "") return 1.0;
    var prices = xau.substring(xau.indexOf("!") + 1);
    if (instrument.match("XAG_")) {
        prices = xag.substring(xag.indexOf("!") + 1);
        if (prices.indexOf(instrument) == -1) {
            return CalculateSilverPrice(instrument);
        }
    }
    xval = prices.substring(prices.indexOf(instrument));
    xval = xval.substring(0, xval.indexOf(";"));
    xval = xval.substring(xval.indexOf(",") + 1);
    var pricePerOz = new Number(xval);
    return pricePerOz;
}
function CalculateSilverPrice(instrument) {
    GoldUSD = GetSpotPrice("XAU_USD");
    SilverUSD = GetSpotPrice("XAG_USD");
    GoldInstument = GetSpotPrice(instrument.replace("XAG_", "XAU_"));
    return SilverUSD * (GoldInstument / GoldUSD);
}
function SetSpan(name, datetime) {
    var test = document.getElementById(name);
    if (test == null) return;
    if (document.getElementById) {
        document.getElementById(name).innerHTML = datetime;
    }
    else if (document.layers) {
        document.layers[name].innerHTML = datetime;
    }
}

function GetXmlHttp() {
    /*@cc_on@*/
    /*@if (@_jscript_version >= 5)
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            xmlhttp = false;
        }
    }
    @end@*/
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        try {
            xmlhttp = new XMLHttpRequest();
        } catch (e) {
            xmlhttp = false;
        }
    }
    if (!xmlhttp && window.createRequest) {
        try {
            xmlhttp = window.createRequest();
        } catch (e) {
            xmlhttp = false;
        }
    }
}
function GetPriceData() {
    var xauOrg = xau;
    var xagOrg = xag;
    try {
        if (!xmlhttp) {
            GetXmlHttp();
        }
        xmlhttp.open("GET", "charts/goldsilverpricesforcalculators.txt?" + Math.random(), true);
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4) {
                var alldata = xmlhttp.responseText;
                xau = alldata.substring(0, alldata.indexOf("@"));
                xag = alldata.substring(alldata.indexOf("@") + 1);
            }
        }
        xmlhttp.send(null);
    }
    catch (Error) {
        xau = xauOrg;
        xag = xagOrg;
    }
}
function CommaFormatted(nStr) {
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    x3 = x1 + x2;
    if (x3.indexOf('.') == -1) {
        x3 += ".00";
    }
    return x3;
}
function CommaFormattedFloatRound(flt, round) {
    if (!round) round = 2;
    flt = Math.round(flt * Math.pow(10, round)) / Math.pow(10, round);
    nStr = flt.toString();
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    if (x2.length == 2) x2 = x2 + "0";
    x3 = x1 + x2;
    if (x3.indexOf('.') == -1) {
        x3 += ".00";
    }
    return x3;
}
function RoundToDecimals(flt, round) {
    return Math.round(flt * Math.pow(10, round)) / Math.pow(10, round);
}
function ReplaceText(sId, sText) {
    var el;
    if (document.getElementById && (el = document.getElementById(sId))) {
        while (el.hasChildNodes())
            el.removeChild(el.lastChild);
        el.appendChild(document.createTextNode(sText));
    }
}
function showhide(id) {
    var allDivs = "div1;div2;div3;div4;div5;div6;div7;div8;div9;div10;div11;div12;div13;div14;div15";
    allDivs = allDivs.replace(id, "").split(";");
    for (i = 0; i < allDivs.length; i++) {
        obj = document.getElementById(allDivs[i]);
        if (obj != null) {
            obj.style.display = "none";
        }
    }
    if (document.getElementById) {
        obj = document.getElementById(id);
        if (obj != null) {
            if (obj.style.display == "none") {
                obj.style.display = "";
            } else {
                obj.style.display = "none";
            }
        }
    }
}
function showhidehelp(id) {
    if (document.getElementById) {
        obj = document.getElementById(id);
        if (obj != null) {
            if (obj.style.display == "none") {
                obj.style.display = "";
            } else {
                obj.style.display = "none";
            }
        }
    }
} 