﻿///-------------------------------GOLD-------------------------------
var XAU_CookieTestRun = false;
var XAU_UserHasCookie = false;
var XAU_LastCookieValue = "";
function XAU_CookieTest() {
    try {
        var t = document.getElementById("XAU_Holdings_CookieOk");
        if (t == null) return;
        XAU_CookieTestRun = true;
        if (Cookie.test()) {
            XAU_Holdings_ReadCookie();
            if (!XAU_UserHasCookie)
                document.getElementById("XAU_Holdings_CookieOk").style.display = "";
        }
        else {
            document.getElementById("XAU_Holdings_NoCookie").style.display = "";
        }
    }
    catch (err) {
    }
}

//XAU_Holdings_Update();
//setInterval("XAU_Holdings_Update()", UPDATEINTERVAL);

function XAU_Holdings_Update() {
    try {
        if (xau == "") return;

        var t = document.getElementById("XAU_Holdings_Amount");

        if (t == null) return;

        //        if (XAU_CookieTestRun == false) {
        //            XAU_CookieTest();
        //        }
        var amount = new Number(t.value);
        if (isNaN(amount)) {
            amount = 0;
            t.value = "0";
        }
        //Current Value
        var instrument = document.getElementById("XAU_Holdings_Currency_Select").value;
        var price = GetSpotPriceHistorical(instrument);
        var pricePerOz = Math.round(price * 100) / 100;

        var weightType = document.getElementById("XAU_Holdings_WeightType_Select").value;
        var priceHeader = "Current Gold Price/Ounce";
        var purchaseHeader = "Purchase Gold Price/Ounce";
        if (weightType == "G") {
            price = price / GramsIn1TroyOunce;
            priceHeader = "Current Gold Price/Gram";
            purchaseHeader = "Purchase Gold Price/Gram";
        }
        else if (weightType == "K") {
            price = price * TroyOuncesIn1Kilogram;
            priceHeader = "Current Gold Price/Kilogram";
            purchaseHeader = "Purchase Gold Price/Kilogram";
        }
        if (IsHoldingsInRTMode() == false) {
            priceHeader = priceHeader.replace("Current", "Closing");
        }
        var pricePerUnit = Math.round(price * 100) / 100;
        ReplaceText("XAU_Holdings_Current_Price_Header", priceHeader);
        ReplaceText("XAU_Holdings_Purchase_Price_Header", purchaseHeader);
        price = price * amount;
        document.getElementById("XAU_Holdings_CurrentPrice").value = CommaFormatted(pricePerUnit.toString());
        var priceR = Math.round(price * 100) / 100;
        document.getElementById("XAU_Holdings_CurrentValue").value = CommaFormatted(priceR.toString());
        var PurchasePrice = new Number(document.getElementById("XAU_Holdings_PurchasePrice").value);
        document.getElementById("XAU_Holdings_CurrentGainLoss").value = "";
        if (!isNaN(PurchasePrice)) {
            if (PurchasePrice > 0) {
                PurchasePrice = PurchasePrice * amount;
                var gain = (price - PurchasePrice) / PurchasePrice;
                gain = gain * 100.00;
                gain = Math.round(gain * 100) / 100;
                document.getElementById("XAU_Holdings_CurrentGainLoss").value = CommaFormatted(gain.toString()) + "%";
            }
        }
        //Future Value
        var FuturePrice = new Number(document.getElementById("XAU_Holdings_FuturePrice").value);
        document.getElementById("XAU_Holdings_FutureGainLoss").value = "";
        if (!isNaN(FuturePrice)) {
            if (FuturePrice > 0) {

                var weightTypeF = document.getElementById("XAU_Holdings_WeightType_Future_Select").value;
                var futureHeader = "Future Gold Price/Ounce";
                if (weightTypeF == "G") {
                    futureHeader = "Future Gold Price/Gram";
                }
                else if (weightTypeF == "K") {
                    futureHeader = "Future Gold Price/Kilogram";
                }
                ReplaceText("XAU_Holdings_Future_Price_Header", futureHeader);


                var priceOz = GetSpotPriceHistorical(instrument);
                var instrument = document.getElementById("XAU_Holdings_Currency_Future_Select").value;
                var priceFutureOz = GetSpotPriceHistorical(instrument);
                var cAmount = XAU_ConvertAmount(weightType, weightTypeF, amount);
                var priceF = FuturePrice * cAmount;
                priceF = priceF * (priceOz / priceFutureOz)
                document.getElementById("XAU_Holdings_FutureValue").value = CommaFormattedFloatRound(priceF, 2);
                if (!isNaN(PurchasePrice)) {
                    if (PurchasePrice > 0) {
                        var gain = (priceF - PurchasePrice) / PurchasePrice;
                        gain = gain * 100.00;
                        gain = Math.round(gain * 100) / 100;
                        document.getElementById("XAU_Holdings_FutureGainLoss").value = CommaFormatted(gain.toString()) + "%";
                    }
                }
            }
        }
        //        if (XAU_UserHasCookie)
        //            XAU_Holdings_SaveCookie();
    }
    catch (err) {
    }
}

function XAU_ConvertAmount(fromWt, toWt, amount) {
    if (fromWt == toWt) return amount;
    if (fromWt == "K" && toWt == "G") {
        return amount * 1000;
    }
    if (fromWt == "G" && toWt == "K") {
        return amount / 1000;
    }
    if (fromWt == "G" && toWt == "O") {
        return amount / GramsIn1TroyOunce;
    }
    if (fromWt == "K" && toWt == "O") {
        return amount * TroyOuncesIn1Kilogram;
    }
    if (fromWt == "O" && toWt == "G") {
        return GramsIn1TroyOunce * amount;
    }
    if (fromWt == "O" && toWt == "K") {
        return amount / TroyOuncesIn1Kilogram;
    }
}

function XAU_Holdings_SaveCookie() {
    try {
        //Amount,WeightType,Currency,PurchasePrice,FuturePrice
        var cval = document.getElementById("XAU_Holdings_Amount").value + ",";
        cval += document.getElementById("XAU_Holdings_WeightType_Select").value + ",";
        cval += document.getElementById("XAU_Holdings_Currency_Select").value + ",";
        cval += document.getElementById("XAU_Holdings_PurchasePrice").value + ",";
        cval += document.getElementById("XAU_Holdings_FuturePrice").value + ",";
        cval += document.getElementById("XAU_Holdings_WeightType_Future_Select").value + ",";
        cval += document.getElementById("XAU_Holdings_Currency_Future_Select").value;
        if (XAU_LastCookieValue == cval) {
            return;
        }
        XAU_LastCookieValue = cval;
        Cookie.set("au.goldprice.org", cval);
        XAU_UserHasCookie = true;
        var o = document.getElementById("XAU_Holdings_CookieOk");
        if (o == null) return;
        document.getElementById("XAU_Holdings_CookieOk").style.display = "none";
    }
    catch (err) {
    }
}
function XAU_Holdings_ReadCookie() {
    XAU_LastCookieValue = Cookie.get("au.goldprice.org");
    if (XAU_LastCookieValue == null) {
        XAU_UserHasCookie = false;
        return false;
    }
    XAU_UserHasCookie = true;
    //var temp="10,K,XAU_AUD,,";
    var values = XAU_LastCookieValue.split(",");
    document.getElementById("XAU_Holdings_Amount").value = values[0];
    document.getElementById("XAU_Holdings_WeightType_Select").value = values[1];
    document.getElementById("XAU_Holdings_Currency_Select").value = values[2];
    document.getElementById("XAU_Holdings_PurchasePrice").value = values[3];
    document.getElementById("XAU_Holdings_FuturePrice").value = values[4];
    document.getElementById("XAU_Holdings_WeightType_Future_Select").value = values[5];
    document.getElementById("XAU_Holdings_Currency_Future_Select").value = values[6];
    return true;
}

function XAU_Holdings_DeleteCookie() {
    Cookie.unset("au.goldprice.org");
}
///-------------------------------Silver------------------------------
var XAG_CookieTestRun = false;
var XAG_UserHasCookie = false;
var XAG_LastCookieValue = "";
function XAG_CookieTest() {
    try {
        var t = document.getElementById("XAG_Holdings_CookieOk");
        if (t == null) return;
        XAG_CookieTestRun = true;
        if (Cookie.test()) {
            XAG_Holdings_ReadCookie();
            if (!XAG_UserHasCookie)
                document.getElementById("XAG_Holdings_CookieOk").style.display = "";
        }
        else {
            document.getElementById("XAG_Holdings_NoCookie").style.display = "";
        }
    }
    catch (err) {
    }
}
var UPDATEINTERVAL = 5 * 1000;
//XAG_Holdings_Update();
//setInterval("XAG_Holdings_Update()", UPDATEINTERVAL);

function XAG_Holdings_Update() {
    try {
        if (xag == "") {
            return;
        }
        var t = document.getElementById("XAG_Holdings_Amount");
        if (t == null) return;

        //        if (XAG_CookieTestRun == false) {
        //            XAG_CookieTest();
        //        }
        var amount = new Number(document.getElementById("XAG_Holdings_Amount").value);
        if (isNaN(amount)) {
            amount = 0;
            document.getElementById("XAG_Holdings_Amount").value = "0";
        }
        //Current Value
        var instrument = document.getElementById("XAG_Holdings_Currency_Select").value;
        var price = GetSpotPriceHistorical(instrument);
        var pricePerOz = Math.round(price * 100) / 100;

        var weightType = document.getElementById("XAG_Holdings_WeightType_Select").value;
        var priceHeader = "Current Silver Price/Ounce";
        var purchaseHeader = "Purchase Silver Price/Ounce";
        if (weightType == "G") {
            price = price / GramsIn1TroyOunce;
            priceHeader = "Current Silver Price/Gram";
            purchaseHeader = "Purchase Silver Price/Gram";
        }
        else if (weightType == "K") {
            price = price * TroyOuncesIn1Kilogram;
            priceHeader = "Current Silver Price/Kilogram";
            purchaseHeader = "Purchase Silver Price/Kilogram";
        }
        if (IsHoldingsInRTMode() == false) {
            priceHeader = priceHeader.replace("Current", "Closing");
        }
        var pricePerUnit = Math.round(price * 100) / 100;
        ReplaceText("XAG_Holdings_Current_Price_Header", priceHeader);
        ReplaceText("XAG_Holdings_Purchase_Price_Header", purchaseHeader);
        price = price * amount;
        document.getElementById("XAG_Holdings_CurrentPrice").value = CommaFormatted(pricePerUnit.toString());
        var priceR = Math.round(price * 100) / 100;
        document.getElementById("XAG_Holdings_CurrentValue").value = CommaFormatted(priceR.toString());
        var PurchasePrice = new Number(document.getElementById("XAG_Holdings_PurchasePrice").value);
        document.getElementById("XAG_Holdings_CurrentGainLoss").value = "";
        if (!isNaN(PurchasePrice)) {
            if (PurchasePrice > 0) {
                PurchasePrice = PurchasePrice * amount;
                var gain = (price - PurchasePrice) / PurchasePrice;
                gain = gain * 100.00;
                gain = Math.round(gain * 100) / 100;
                document.getElementById("XAG_Holdings_CurrentGainLoss").value = CommaFormatted(gain.toString()) + "%";
            }
        }
        //Future Value
        var FuturePrice = new Number(document.getElementById("XAG_Holdings_FuturePrice").value);
        document.getElementById("XAG_Holdings_FutureGainLoss").value = "";
        if (!isNaN(FuturePrice)) {
            if (FuturePrice > 0) {

                var weightTypeF = document.getElementById("XAG_Holdings_WeightType_Future_Select").value;
                var futureHeader = "Future Silver Price/Ounce";
                if (weightTypeF == "G") {
                    futureHeader = "Future Silver Price/Gram";
                }
                else if (weightTypeF == "K") {
                    futureHeader = "Future Silver Price/Kilogram";
                }
                ReplaceText("XAG_Holdings_Future_Price_Header", futureHeader);


                var priceOz = GetSpotPriceHistorical(instrument);
                var instrument = document.getElementById("XAG_Holdings_Currency_Future_Select").value;
                var priceFutureOz = GetSpotPriceHistorical(instrument);
                var cAmount = XAG_ConvertAmount(weightType, weightTypeF, amount);
                var priceF = FuturePrice * cAmount;
                priceF = priceF * (priceOz / priceFutureOz)
                document.getElementById("XAG_Holdings_FutureValue").value = CommaFormattedFloatRound(priceF, 2);
                if (!isNaN(PurchasePrice)) {
                    if (PurchasePrice > 0) {
                        var gain = (priceF - PurchasePrice) / PurchasePrice;
                        gain = gain * 100.00;
                        gain = Math.round(gain * 100) / 100;
                        document.getElementById("XAG_Holdings_FutureGainLoss").value = CommaFormatted(gain.toString()) + "%";
                    }
                }
            }
        }
        //        if (XAG_UserHasCookie)
        //            XAG_Holdings_SaveCookie();
    }
    catch (err) {
    }
}

function XAG_ConvertAmount(fromWt, toWt, amount) {
    if (fromWt == toWt) return amount;
    if (fromWt == "K" && toWt == "G") {
        return amount * 1000;
    }
    if (fromWt == "G" && toWt == "K") {
        return amount / 1000;
    }
    if (fromWt == "G" && toWt == "O") {
        return amount / GramsIn1TroyOunce;
    }
    if (fromWt == "K" && toWt == "O") {
        return amount * TroyOuncesIn1Kilogram;
    }
    if (fromWt == "O" && toWt == "G") {
        return GramsIn1TroyOunce * amount;
    }
    if (fromWt == "O" && toWt == "K") {
        return amount / TroyOuncesIn1Kilogram;
    }
}

function XAG_Holdings_SaveCookie() {
    try {
        //Amount,WeightType,Currency,PurchasePrice,FuturePrice
        var cval = document.getElementById("XAG_Holdings_Amount").value + ",";
        cval += document.getElementById("XAG_Holdings_WeightType_Select").value + ",";
        cval += document.getElementById("XAG_Holdings_Currency_Select").value + ",";
        cval += document.getElementById("XAG_Holdings_PurchasePrice").value + ",";
        cval += document.getElementById("XAG_Holdings_FuturePrice").value + ",";
        cval += document.getElementById("XAG_Holdings_WeightType_Future_Select").value + ",";
        cval += document.getElementById("XAG_Holdings_Currency_Future_Select").value;
        if (XAG_LastCookieValue == cval) {
            return;
        }
        XAG_LastCookieValue = cval;
        Cookie.set("ag.goldprice.org", cval);
        XAG_UserHasCookie = true;
        var o = document.getElementById("XAG_Holdings_CookieOk");
        if (o == null) return;
        document.getElementById("XAG_Holdings_CookieOk").style.display = "none";
    }
    catch (err) {
    }
}
function XAG_Holdings_ReadCookie() {
    XAG_LastCookieValue = Cookie.get("ag.goldprice.org");
    if (XAG_LastCookieValue == null) {
        XAG_UserHasCookie = false;
        return false;
    }
    XAG_UserHasCookie = true;
    //var temp="10,K,XAG_AUD,,";
    var values = XAG_LastCookieValue.split(",");
    document.getElementById("XAG_Holdings_Amount").value = values[0];
    document.getElementById("XAG_Holdings_WeightType_Select").value = values[1];
    document.getElementById("XAG_Holdings_Currency_Select").value = values[2];
    document.getElementById("XAG_Holdings_PurchasePrice").value = values[3];
    document.getElementById("XAG_Holdings_FuturePrice").value = values[4];
    document.getElementById("XAG_Holdings_WeightType_Future_Select").value = values[5];
    document.getElementById("XAG_Holdings_Currency_Future_Select").value = values[6];
    return true;
}

function XAG_Holdings_DeleteCookie() {
    Cookie.unset("ag.goldprice.org");
}
//XAU_XAG_Holdings_Update();

var XAU_XAG_CookieTestRun = false;
var XAU_XAG_UserHasCookie = false;

function XAU_XAG_CookieTest() {
    try {
        var t = document.getElementById("XAU_XAG_Holdings_HasCookie");
        if (t == null) return;
        if (Cookie.test() == false) {
            document.getElementById("XAU_XAG_Holdings_Can_NOT_HaveCookie").style.display = "";
            return;
        }
        XAU_XAG_Holdings_ReadCookie();
        if (XAU_XAG_UserHasCookie) {
            document.getElementById("XAU_XAG_Holdings_HasCookie").style.display = "";
        }
        else {
            document.getElementById("XAU_XAG_Holdings_CanHaveCookie").style.display = "";
        }
        XAU_XAG_CookieTestRun = true;
    }
    catch (err) {
    }
}
var XAU_XAG_Holdings_Update_TimerRunning = false;
function XAU_XAG_Holdings_Update() {
    try {
        if (XAU_XAG_Holdings_Update_TimerRunning == false) {
            XAU_XAG_Holdings_Update_TimerRunning = true;
            setInterval("XAU_XAG_Holdings_Update()", UPDATEINTERVAL);
        }
        if (XAU_XAG_CookieTestRun == false) {
            XAU_XAG_CookieTest();
        }
        if (xau == "") {
            return;
        }
        XAU_Holdings_Update();
        XAG_Holdings_Update();

        XAU_XAG_PlaceFooter();
        var i1 = document.getElementById("XAU_Holdings_Currency_Select").value;
        i1 = i1.substring(4);

        var i2 = document.getElementById("XAG_Holdings_Currency_Select").value;
        i2 = i2.substring(4);

        if (i1 != i2) {
            document.getElementById("XAU_XAG_Holdings_Current").value = "";
            document.getElementById("XAU_XAG_Holdings_CurrentGainLoss").value = "";
            document.getElementById("XAU_XAG_Holdings_CurrentRatio").value = "";
            document.getElementById("XAU_XAG_Holdings_Future").value = "";
            document.getElementById("XAU_XAG_Holdings_FutureGainLoss").value = "";
            document.getElementById("XAU_XAG_Holdings_FutureRatio").value = "";
            return;
        }
        var agval = new Number(CleanStr(document.getElementById("XAG_Holdings_CurrentValue").value));
        var auval = new Number(CleanStr(document.getElementById("XAU_Holdings_CurrentValue").value));
        document.getElementById("XAU_XAG_Holdings_Current").value = "";
        var val = 0;
        if (!isNaN(agval)) {
            val = agval;
        }
        else {
            agval = 0;
        }
        if (!isNaN(auval)) {
            val += auval;
        }
        else {
            auval = 0;
        }
        if (val > 0) {
            val = Math.round(val * 100) / 100;
            document.getElementById("XAU_XAG_Holdings_Current").value = CommaFormatted(val.toString());
            var aup = new Number(CleanStr(document.getElementById("XAU_Holdings_CurrentGainLoss").value));
            var agp = new Number(CleanStr(document.getElementById("XAG_Holdings_CurrentGainLoss").value));
            document.getElementById("XAU_XAG_Holdings_CurrentGainLoss").value = CalcPercent(auval, aup, agval, agp);

            document.getElementById("XAU_XAG_Holdings_CurrentRatio").value = CalcRatio(auval, agval);
        }
        agval = new Number(CleanStr(document.getElementById("XAG_Holdings_FutureValue").value));
        auval = new Number(CleanStr(document.getElementById("XAU_Holdings_FutureValue").value));
        document.getElementById("XAU_XAG_Holdings_Future").value = "";
        val = 0;
        if (!isNaN(agval)) {
            val = agval;
        }
        else {
            agval = 0;
        }
        if (!isNaN(auval)) {
            val += auval;
        }
        else {
            auval = 0;
        }
        if (val > 0) {
            val = Math.round(val * 100) / 100;
            document.getElementById("XAU_XAG_Holdings_Future").value = CommaFormatted(val.toString());

            var aup = new Number(CleanStr(document.getElementById("XAU_Holdings_FutureGainLoss").value));
            var agp = new Number(CleanStr(document.getElementById("XAG_Holdings_FutureGainLoss").value));
            document.getElementById("XAU_XAG_Holdings_FutureGainLoss").value = CalcPercent(auval, aup, agval, agp);
            document.getElementById("XAU_XAG_Holdings_FutureRatio").value = CalcRatio(auval, agval);
        }
        if (XAU_XAG_UserHasCookie) {
            XAU_XAG_Holdings_SaveCookie();
        }
    }
    catch (err) {
    }
}
function XAU_XAG_PlaceFooter() {
    try {
        useRT = IsHoldingsInRTMode();

        lable1 = "Closeing Gold Price: ";
        if (useRT) {
            lable1 = "Spot Gold Price: ";
        }
        lable2 = "Closeing Silver Price: ";
        if (useRT) {
            lable2 = "Spot Silver Price: ";
        }
        var selObj = document.getElementById("XAU_Holdings_WeightType_Select");
        spot = lable1 +
               document.getElementById("XAU_Holdings_Currency_Select").value.replace("XAU_", "") + " " +
               document.getElementById("XAU_Holdings_CurrentPrice").value + " / " +
               selObj.options[selObj.selectedIndex].text;

        var selObj = document.getElementById("XAG_Holdings_WeightType_Select");
        spot2 = lable2 +
               document.getElementById("XAG_Holdings_Currency_Select").value.replace("XAG_", "") + " " +
               document.getElementById("XAG_Holdings_CurrentPrice").value + " / " +
               selObj.options[selObj.selectedIndex].text;

        SetSpan("XAU_XAG_Holdings_Spot", spot + " | " + spot2);
        var theHistoryDateStr = "";
        if (useRT) {
            SetSpan("XAU_XAG_Holdings_DateTimeEST", xau.substring(0, xau.indexOf("!")));
        }
        else {
            if (HISTORICALPRICES != "") {
                hdata = HISTORICALPRICES.split(",");
                adate = hdata[0];
                datedata = adate.split("-");
                theHistoryDate = new Date();
                theHistoryDate.setFullYear(Number(datedata[0]));
                month = Number(datedata[1]) - 1;
                day = Number(datedata[2]);
                theHistoryDate.setMonth(month, day);
                theHistoryDateStr = theHistoryDate.toLocaleDateString();
                SetSpan("XAU_XAG_Holdings_DateTimeEST", theHistoryDateStr);
            }
        }
        if (useRT) {
            SetSpan("XAU_XAG_Holdings_CurrentLabel", "Current Value");
        }
        else {

            SetSpan("XAU_XAG_Holdings_CurrentLabel", "Value On: " + theHistoryDateStr);
        }
    }
    catch (err) {
    }
}
function XAU_XAG_Holdings_ReadCookie() {
    try {
        XAU_XAG_UserHasCookie = false;
        if (XAU_Holdings_ReadCookie()) {
            XAU_XAG_UserHasCookie = true;
            XAG_Holdings_ReadCookie();
        }
    }
    catch (err) {
    }
}
function XAU_XAG_Holdings_SaveCookie() {
    XAU_Holdings_SaveCookie();
    XAG_Holdings_SaveCookie();
    XAU_XAG_UserHasCookie = true;
    //show delete button
    document.getElementById("XAU_XAG_Holdings_HasCookie").style.display = "";
    document.getElementById("XAU_XAG_Holdings_CanHaveCookie").style.display = "none";
}
function XAU_XAG_Holdings_DeleteCookie() {
    XAU_Holdings_DeleteCookie();
    XAG_Holdings_DeleteCookie();
    XAU_XAG_UserHasCookie = false;
    //show save button
    document.getElementById("XAU_XAG_Holdings_HasCookie").style.display = "none";
    document.getElementById("XAU_XAG_Holdings_CanHaveCookie").style.display = "";
}
function XAU_XAG_Synchronize(thisSelect) {
    try {
        var selIndex = document.getElementById(thisSelect).selectedIndex;
        if (thisSelect == "XAU_Holdings_Currency_Select") {
            document.getElementById('XAG_Holdings_Currency_Select').selectedIndex = selIndex;
        }
        else {
            document.getElementById('XAU_Holdings_Currency_Select').selectedIndex = selIndex;
        }
        XAU_XAG_Holdings_Update();
    } catch (Error) { }
}

function CleanStr(str) {
    if (str == null) return null;
    if (str.indexOf(",") > -1)
        str = str.replace(/,/g, "");
    if (str.indexOf("%") > -1)
        str = str.replace(/%/g, "");
    return str;
}
function CalcPercent(v1, p1, v2, p2) {
    try {
        if (isNaN(v1) && isNaN(v2)) return "";
        if (v1 <= 0 && v2 <= 0) return "";
        if (p1 == 0 && p2 == 0) return "";
        if (v1 <= 0) {
            if (p2 == 0) {
                return "";
            }
            else {
                p2 = Math.round(p2 * 100) / 100;
                return CommaFormatted(p2.toString()) + "%";
            }
        }
        if (v2 <= 0) {
            if (p1 == 0) {
                return "";
            }
            else {
                p1 = Math.round(p1 * 100) / 100;
                return CommaFormatted(p1.toString()) + "%";
            }
        }
        if (p1 == 0) {
            p2 = Math.round(p2 * 100) / 100;
            return CommaFormatted(p2.toString()) + "%";
        }
        if (p2 == 0) {
            p1 = Math.round(p1 * 100) / 100;
            return CommaFormatted(p1.toString()) + "%";
        }
        var tp = (v1 * p1) / (v1 + v2) + (v2 * p2) / (v1 + v2);
        if (tp == 0) return "";
        tp = Math.round(tp * 100) / 100;
        return CommaFormatted(tp.toString()) + "%";
    }
    catch (Error) {
        return "";
    }
}
function CalcRatio(v1, v2) {
    try {
        var r1 = (v1 / (v1 + v2)) * 100.0;
        r1 = Math.round(r1 * 100) / 100;
        var r2 = (v2 / (v1 + v2)) * 100.0;
        r2 = Math.round(r2 * 100) / 100;
        return CommaFormatted(r1.toString()) + "%/" + CommaFormatted(r2.toString()) + "%";
    }
    catch (Error) {
        return "";
    }
}
//-- -- -- -- -- -- -- --
//history data
if (document.location.hostname=="localhost") {
    var HISTORYURL = "http://localhost:8080//GPOTest/GPOGetHistoricalData.aspx?d=";
}
else {
    var HISTORYURL = "http://goldprice.org/Calculators/gethistorydata.php?gpo_data=";
    HISTORYURL = HISTORYURL.replace("goldprice.org", document.location.hostname);
}

var xmlhttp2 = false;
//YYYYMMDD,CUR,XAU,XAG
var HISTORICALPRICES = "";
function GetHistoricalPriceData(YYYYMMDD, instrument) {
    myurl = HISTORYURL + YYYYMMDD + instrument;
    //alert("myurl =" + myurl);
    try {
        if (!xmlhttp2) {
            xmlhttp2 = GetXmlHttp();
        }
        xmlhttp2.open("GET", myurl, true);
        xmlhttp2.onreadystatechange = function() {
            if (xmlhttp2.readyState == 4) {
                HISTORICALPRICES = xmlhttp2.responseText;
                hdata = HISTORICALPRICES.split(",");
                adate = hdata[0];
                datedata = adate.split("-");
                thedate = new Date();
                thedate.setFullYear(Number(datedata[0]));
                month = Number(datedata[1]) - 1;
                day = Number(datedata[2]);
                thedate.setMonth(month, day);
                GPOHCDate_Object.setToDate(thedate);
                XAU_XAG_Holdings_Update();
                WaitCursorOff();
            }
        }
    }
    catch (err) {
        WaitCursorOff();
        alert("xmlhttp2.open() failed.\n" + err.message + " " + err.number);
        GPOHCHReset();
        return;
    }
    xmlhttp2.send(null);
}
function GPOHCHReset() {
    HISTORICALPRICES = "";
    GPOHCDate_Object.resetToToday();
    rb = document.getElementById("GPOHCRadioUseHist");
    rb.checked = true;
    rb = document.getElementById("GPOHCRadioUseRTP");
    rb.checked = false;
    XAU_XAG_Holdings_Update();
}
function GPOHCHUpdate() {
    try {
        //CEH set cal to date picked
        Today = new Date();
        if (GPOHCDate_Object.picked.date > Today) {
            GPOHCDate_Object.resetToToday();
            return;
        }
        rb = document.getElementById("GPOHCRadioUseHist");
        rb.checked = true;
        rb = document.getElementById("GPOHCRadioUseRTP");
        rb.checked = false;
        datestr = GPOHCDate_Object.picked.formatted;
        instrument = document.getElementById("XAU_Holdings_Currency_Select").value;
        GetHistoricalPriceData(datestr, instrument);
        XAU_XAG_Holdings_Update();
    }
    catch (Error) {
    }
}
function IsHoldingsInRTMode() {
    rb = document.getElementById("GPOHCRadioUseRTP");
    if (rb.checked) {
        return true;
    }
    else {
        return false;
    }
}
function GetSpotPriceHistorical(instrument) {
    if (IsHoldingsInRTMode()) {
        return GetSpotPrice(instrument);
    }
    else {
        if (HISTORICALPRICES == "") return GetSpotPrice(instrument);
        hdata = HISTORICALPRICES.split(",");
        if (instrument.match("XAU_")) {
            return new Number(hdata[2]);
        }
        else {
            return new Number(hdata[3]);
        }
    }
}
function WaitCursorOn() {
    document.body.style.cursor = "wait";
}

function WaitCursorOff() {
    document.body.style.cursor = "default";
}
XAU_XAG_Holdings_Update();

