//XAU_Coins_Calculator
//XAU_OldCoins_Calculator
//GPO_Converter
//Gold_Price_Main
//XAG_Coins_Calculator
//XAG_OldCoins_Calculator
//these use optionslist =form=document.FORMNMAE using the name= attribute
// historical closes and holdings with history - use a for for the calendar

Cookie = {
    get: function(key) {
        var aCookie = document.cookie.split("; ");
        for (var i = 0; i < aCookie.length; i++) {
            var aCrumb = aCookie[i].split("=");
            if (key == aCrumb[0])
                return unescape(aCrumb[1]);
        }
        return null;
    },
    set: function(key, value) {

        var expires_date = new Date();
        expires_date.setFullYear(2012, 11, 12);
        var path = window.location.pathname.replace(/(\/)[^\/]*$/, '$1');
        var domain = window.location.hostname;
        var cookieStr = key + "=" + escape(value) + ";expires=" + expires_date.toGMTString() + ";path=" + path + ";domain=" + domain;
        document.cookie = cookieStr;
        return document.cookie;
    },
    unset: function(key) {
        if (Cookie.get(key)) {
            var domain = window.location.hostname;
            document.cookie = key + "=;expires=Thu, 01-Jan-1970 00:00:01 GMT" + ";path=/" + ";domain=" + domain;
        }
    },
    hoursToExpireDate: function(ttl) {
        if (parseInt(ttl) == 'NaN') return '';
        else {
            now = new Date();
            now.setTime(now.getTime() + (parseInt(ttl) * 60 * 60 * 1000));
            return now.toGMTString();
        }
    },
    test: function() {
        Cookie.set('b49f729efde9b2578ea9f00563d06e57', 'true');
        if (Cookie.get('b49f729efde9b2578ea9f00563d06e57') == 'true') {
            Cookie.unset('b49f729efde9b2578ea9f00563d06e57');
            return true;
        }
        return false;
    }
}
var GramsIn1TroyOunce = 31.1034768;
var TroyOuncesIn1Kilogram = 32.15074656862798;
var PennyWeightPerTroyOunce = 0.05;

var xmlhttp = false;
var xau = "";
var xag = "";


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() {
    try { return new XMLHttpRequest() } catch (e) { }
    try { return new ActiveXObject("MSXML2.XMLHTTP.3.0") } catch (e) { }
    try { return new ActiveXObject("Msxml2.XMLHTTP") } catch (e) { }
    try { return new ActiveXObject("Microsoft.XMLHTTP") } catch (e) { }
    return false;
}
function GetPriceData() {
    var xauOrg = xau;
    var xagOrg = xag;
    try {
        if (!xmlhttp) {
            xmlhttp = GetXmlHttp();
        }
        xmlhttp.open("GET", "NewCharts/gold/images/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) {
    try {
        var allDivs = "div1;div2;div3;div4;div5;div6;div7;div8;div9;div10;div11;div12;div13;div14;div15;div16";
        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";
                    id = id.replace("div", "div0");
                    obj = document.getElementById(id);
                    if (obj != null) {
                        obj.style.display = "none";
                    }
                }
            }
        }
    } catch (Error) { };
}

function showhide1(id) {
    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";
            }
        }
        //hack all help has to start with div0 and match its calc div number
        id = id.replace("div0", "div");
        obj = document.getElementById(id);
        obj.style.display = "";
    }
}
GetPriceData();
setInterval("GetPriceData()", 30 * 1000);

