function get_cookie(cookie_name) {
    var results = document.cookie.match('(^|;) ?' + cookie_name + '=([^;]*)(;|$)');

    if (results)
        return (unescape(results[2]));
    else
        return null;
}

function localeNumberFormat(amount, percent, decimalPlaces) {
    // setup some default values
    var lang = get_cookie("lang");
    var delimiter = (lang == "en-US" ? '' : '');
    //var decimalPlaces = 2;
    var currencySymbol = '$';
    
    //check that the value can be recognised as an Floating point number
    try {
        amount = parseFloat(amount);
        amount = amount.toFixed(decimalPlaces);
    }
    catch (e) {
        return "";
    }

    // convert to string
    if (amount.match != 'function') { amount = amount.toString(); }
    amount = amount.replace(" ", "");
    amount = (lang == "en-US" ? amount.replace(",", "") : amount);
    // validate as numeric
    var regIsNumeric = /[^\d,\.-]+/igm;
    var results = amount.match(regIsNumeric);

    if (results != null) {
        return "";
    }

    // setup some variables for delimiting the strings with commas
    var minus = amount.indexOf('-') >= 0 ? '-' : '';
    amount = amount.replace('-', '');
    //alert(amount);

    var amtLen = amount.length;
    var decPoint = amount.indexOf('.');
    var wholeNumberEnd = decPoint > 0 ? amtLen - (amtLen - decPoint) : amtLen;

    var wholeNumber = amount.substr(0, wholeNumberEnd);
    var fraction = amount.substr(wholeNumberEnd, amtLen - wholeNumberEnd);

    var segments = (wholeNumberEnd - (wholeNumberEnd % 3)) / 3;
    var rvsNumber = wholeNumber.split('').reverse().join('');
    var output = '';

    for (k = 0; k < wholeNumberEnd; k++) {
        if (k % 3 == 0 && k != 0 && k != wholeNumberEnd) { output += delimiter; }
        output += rvsNumber.charAt(k);
    }

    if (percent == "percent") {
        if (lang == "en-US")
            output = minus + output.split('').reverse().join('') + fraction;
        else
            output = (minus + output.split('').reverse().join('') + fraction).replace(".", ",");
    } else {
        if (lang == "en-US")
            output = minus + currencySymbol + output.split('').reverse().join('') + fraction;
        else
            output = (minus + output.split('').reverse().join('') + fraction + ' ' + currencySymbol).replace(".", ",");
    }

    return output;
}

function showRecommended() {
    if (document.getElementById('recommended').className == 'hidden') {
        document.getElementById('recommended').className = 'visible';
        document.plusminus.src = "images/minus.gif";
    } else {
        document.getElementById('recommended').className = 'hidden';
        document.plusminus.src = "images/plus.gif";
    }    
}

function createAllocatorPopUp(controlx, title, key)
{
    var div = document.createElement('div');
	div.innerHTML = '<div id=popUp><table width=400px class=tableTips><tr><th width=350px colspan=2>'+title+'</th><th align=right><a href="javascript:closePopup();"><img src=images/btnRemove.gif border=0 /></a></th></tr><tr><td colspan=2>' + document.getElementById(key).innerHTML + '</td></tr></table></div>';
	div.firstChild.style.top = getTopPosition(document.getElementById(controlx)) + 'px';
	document.body.appendChild(div.firstChild);
}

function createPopUp(controlx, id, title) {
    var div = document.createElement('div');
	div.innerHTML = '<div id=popUp><table width=400px class=tableTips><tr><th width=350px colspan=2>' + title + ' ' + id + '</th><th align=right><a href="javascript:closePopup();"><img src=images/btnRemove.gif border=0 /></a></th></tr><tr><td colspan=3>' + document.getElementById('TipData' + id).innerHTML + '</td></tr></table></div>';
	div.firstChild.style.top = getTopPosition(document.getElementById(controlx)) + 'px';
	document.body.appendChild(div.firstChild);
}

function closePopup(parent) {
	divElement = document.getElementById("popUp");
	divElement.parentNode.removeChild(divElement);
}

function getTopPosition(obj){
    var topValue= 0,leftValue= 0;
    while(obj){
	//leftValue+= obj.offsetLeft;
	topValue+= obj.offsetTop;
	obj= obj.offsetParent;
    }
    return topValue;
}

function openTips(url, attribs) {
    newwindow=window.open(url,'tips',attribs);
	if (window.focus) {newwindow.focus()}
}

function popupReportAllocator(url) {
    newwindow=window.open(url, 'report', 'scrollbars=1,resizable=1,width=670,height=560,left=300,top=50');
    if (window.focus) {newwindow.focus()}
}
function popupReportIG(url) {
    newwindow = window.open(url, 'report', 'scrollbars=1,resizable=1,width=670,height=560,left=300,top=50');
    if (window.focus) { newwindow.focus() }
}
function popupReport(url, type, error) {
    var lang = get_cookie("lang");
    
    if (error != '') {
        var text = document.getElementById('over_under').innerHTML;
        if ((lang == "en-US" && text != "$0.00") || (lang == "fr-CA" && text != "0,00 $")) {
            alert(error);
            return true;
        }
    }

    if (type == 'preservation')
        url += buildPreservationCustomUrl(lang);
    else
        url += buildAccumulationCustomUrl(lang);
    newwindow = window.open(encodeURI(url), 'report', 'scrollbars=1,resizable=1,width=670,height=560,left=300,top=50');
    if (window.focus) {newwindow.focus()}
}
var subclass_p = new Array("CashEquivalent","FixedIncomeCdn","FixedIncomeCdn12Yrs","FixedIncomeCdn34Yrs","FixedIncomeCdn5Yrs","FixedIncomeHighYield","FixedIncomeInternational","EquityCdn","EquityUs","EquityInternational","EquityEmerging","EquityLinked");
var subclass_short_p = new Array("Cash","FICdn","FI12","FI34","FI5","FIHY","FIIntl","ECdn","EUs","EIntl","EE","EL");

var subclass_a = new Array("cash","fixedCdn","fixed12yrs","fixed34yrs", "fixed5yrs", "fixedHighYield", "fixedIntl", "equityCdn", "equityUS", "equityIntl", "equityEmerging", "equityLinked", "equityGlobal", "equitySmallCap", "equityOther");
var subclass_short_a = new Array("Cash","FICdn","FI12","FI34","FI5","FIHY","FIIntl","ECdn","EUs","EIntl","EE","EL","EG","ESC","EOther");

function buildAccumulationCustomUrl(lang)
{
    var url = '';
    var total = 0;
    for (j = 0; j < subclass_a.length; j++) {
        var t = parseFloatVal(document.getElementById('ctl00_maincontent_' + subclass_a[j]));
        if (t > 0)
        {
            url += "&" + subclass_short_a[j] + "=" + t;
            total += t;
        }
    }
    url += "&total=" + total;
    url += "&custom=1";
    if (lang == "fr-CA") url = url.replace(/\./g, ",");
    return url;
}

function buildPreservationCustomUrl(lang)
{
    var url = '';
   
    for (j = 0; j < subclass_p.length; j++) {
        var count = parseIntVal(document.getElementById('ctl00_maincontent_hdn' + subclass_p[j]));
        if (count > 0)
            url += "&" + subclass_short_p[j] + "_cnt=" + count;
        for (i = 0; i < count; i++) {
            var t = parseIntVal(document.getElementById('ctl00_maincontent_' + subclass_p[j] + "_" + i));
            if (t > 0)
                url += "&" + subclass_short_p[j] + "_" + i + "=" + t;
            var n = document.getElementById('ctl00_maincontent_' + subclass_p[j] + "x_" + i + "_name")
            if (n != null)
                url += "&" + subclass_short_p[j] + "_" + i + "_name" + "=" + encodeURIComponent(n.innerHTML);
            var p = document.getElementById(subclass_p[j] + "_" + i + "_percent")
            if (p != null)
                url += "&" + subclass_short_p[j] + "_" + i + "_percent" + "=" + p.innerHTML;
        }
    }
    url += "&total=" + parseIntVal(document.getElementById('ctl00_maincontent_PortfolioValue'));
    url += "&custom=1";
    if (lang == "fr-CA") url = url.replace(/\./g, ",");
    return url;
}

function adjustPortfolioDefault(isLoad)
{
    var total = parseIntVal(document.getElementById('ctl00_maincontent_PortfolioValue'));
    var totalCash = 0;
    var totalFixed = 0;
    var totalEquity = 0;
    var totalCashP = 0;
    var totalFixedP = 0;
    var totalEquityP = 0;
    
    var custom = document.getElementById('ctl00_maincontent_hdnCustom').value == "1" ? true : false;
    if (custom) {
        adjustPortfolioCustom();
        return;
    } else {
        for (j = 0; j < subclass_p.length; j++) {
            var cash = 100 * parseFloatVal(document.getElementById(subclass_p[j] + '_mix_percent'));
            var count = parseIntVal(document.getElementById('ctl00_maincontent_hdn' + subclass_p[j]));
            if (count > 0) {
                var subtotal = cash * total / 100;
                document.getElementById(subclass_p[j] + "_subtotal").innerHTML = localeNumberFormat(subtotal, "currency", 2);
            }
            for (i = 0; i < count; i++) {
                var p = (cash / count);
                if (!isLoad)
                    document.getElementById(subclass_p[j] + "_" + i + '_percent').innerHTML = localeNumberFormat(Math.round(p * 100) / 100, "percent", 2);
                var t = ((cash / 100.0 / count * total));
                if (!isLoad && document.getElementById('ctl00_maincontent_' + subclass_p[j] + "_" + i))
                    document.getElementById('ctl00_maincontent_' + subclass_p[j] + "_" + i).value = localeNumberFormat(Math.round(t * 100) / 100, "percent", 2);
                if (subclass_p[j].indexOf('Cash') == 0) {
                    totalCash += t;
                    totalCashP += p;
                } else if (subclass_p[j].indexOf('Fixed') == 0 || subclass_p[j] == "EquityLinked") {
                    totalFixed += t;
                    totalFixedP += p;
                } else if (subclass_p[j].indexOf('Equity') == 0 && subclass_p[j] != "EquityLinked") {
                    totalEquity += t;
                    totalEquityP += p;
                }
            }
        }
    }
    if (document.getElementById('cashtotal'))
        document.getElementById('cashtotal').innerHTML = localeNumberFormat(Math.round(totalCash * 100) / 100, "currency", 2);
    if (document.getElementById('fixedtotal'))
        document.getElementById('fixedtotal').innerHTML = localeNumberFormat(Math.round(totalFixed * 100) / 100, "currency", 2);
    if (document.getElementById('equitytotal'))
        document.getElementById('equitytotal').innerHTML = localeNumberFormat(Math.round(totalEquity * 100) / 100, "currency", 2);
    if (document.getElementById('cashtotalp'))
        document.getElementById('cashtotalp').innerHTML = localeNumberFormat(Math.round(totalCashP * 100) / 100, "percent", 2) + "%";
    if (document.getElementById('fixedtotalp'))
        document.getElementById('fixedtotalp').innerHTML = localeNumberFormat(Math.round(totalFixedP * 100) / 100, "percent", 2) + "%";
    if (document.getElementById('equitytotalp'))
        document.getElementById('equitytotalp').innerHTML = localeNumberFormat(Math.round(totalEquityP * 100) / 100, "percent", 2) + "%";
    //document.getElementById('grandtotal').innerHTML = localeNumberFormat(Math.round((totalEquity + totalFixed + totalCash) * 100) / 100, "currency", 2);
    document.getElementById('grandtotal').innerHTML = localeNumberFormat(total, "currency", 2);
    var diff = Math.round((total - (totalEquity + totalFixed + totalCash)) * 100) / 100;
    if (document.getElementById('over_under') != null) {
        document.getElementById('over_under').innerHTML = localeNumberFormat(diff, "currency", 2);
        if (diff < 0)
            document.getElementById('over_under').className = 'error';
        else
            document.getElementById('over_under').className = '';
    }
}

function adjustPortfolioCustom()
{

    var total = parseIntVal(document.getElementById('ctl00_maincontent_PortfolioValue'));

    document.getElementById('ctl00_maincontent_hdnCustom').value = 1;
    var totalCash = 0;
    var totalFixed = 0;
    var totalEquity = 0;
    var totalCashP = 0;
    var totalFixedP = 0;
    var totalEquityP = 0;

    for (j = 0; j < subclass_p.length; j++) {
        var count = parseIntVal(document.getElementById('ctl00_maincontent_hdn' + subclass_p[j]));
        for (i = 0; i < count; i++) {
            var t = parseFloatVal(document.getElementById('ctl00_maincontent_' + subclass_p[j] + "_" + i));
            if (subclass_p[j].indexOf('Cash') == 0) {
                totalCash += t;
            } else if (subclass_p[j].indexOf('Fixed') == 0  || subclass_p[j] == "EquityLinked") {
                totalFixed += t;
            } else if (subclass_p[j].indexOf('Equity') == 0  && subclass_p[j] != "EquityLinked") {
                totalEquity += t;
            }
        }
    }

    if (document.getElementById('cashtotal'))
        document.getElementById('cashtotal').innerHTML = localeNumberFormat(Math.round(totalCash * 100) / 100, "currency", 2);
    if (document.getElementById('fixedtotal'))
        document.getElementById('fixedtotal').innerHTML = localeNumberFormat(Math.round(totalFixed * 100) / 100, "currency", 2);
    if (document.getElementById('equitytotal'))
        document.getElementById('equitytotal').innerHTML = localeNumberFormat(Math.round(totalEquity * 100) / 100, "currency", 2);

    var new_total = totalEquity + totalFixed + totalCash;
    //document.getElementById('grandtotal').innerHTML = localeNumberFormat(Math.round(new_total * 100) / 100, "currency", 2);
    document.getElementById('grandtotal').innerHTML = localeNumberFormat(total, "currency", 2);

    // calculate percentages based on new total
    for (j = 0; j < subclass_p.length; j++) {
        var count = parseIntVal(document.getElementById('ctl00_maincontent_hdn' + subclass_p[j]));
        for (i = 0; i <= count; i++) {

            var t = parseFloatVal(document.getElementById('ctl00_maincontent_' + subclass_p[j] + '_' + i));
            var p = (t * 100 / new_total);
            if (document.getElementById(subclass_p[j] + '_' + i + '_percent'))
                document.getElementById(subclass_p[j] + '_' + i + '_percent').innerHTML = localeNumberFormat(Math.round(p * 100) / 100, "percent", 2);

            if (subclass_p[j].indexOf('Cash') == 0) {
                totalCashP += p;
            } else if (subclass_p[j].indexOf('Fixed') == 0  || subclass_p[j] == "EquityLinked") {
                totalFixedP += p;
            } else if (subclass_p[j].indexOf('Equity') == 0  && subclass_p[j] != "EquityLinked") {
                totalEquityP += p;
            }
        }
    }

    if (document.getElementById('cashtotalp'))
        document.getElementById('cashtotalp').innerHTML = localeNumberFormat(Math.round(totalCashP * 100) / 100, "percent", 2) + "%";
    if (document.getElementById('fixedtotalp'))
        document.getElementById('fixedtotalp').innerHTML = localeNumberFormat(Math.round(totalFixedP * 100) / 100, "percent", 2) + "%";
    if (document.getElementById('equitytotalp'))
        document.getElementById('equitytotalp').innerHTML = localeNumberFormat(Math.round(totalEquityP * 100) / 100, "percent", 2) + "%";

    //calculate over/under
    var diff = total - new_total;
    if (document.getElementById('over_under') != null) {
        document.getElementById('over_under').innerHTML = localeNumberFormat(Math.round(diff * 100) / 100, "currency", 2);
        if (diff != 0)
            document.getElementById('over_under').className = 'error';
        else
            document.getElementById('over_under').className = '';
    }

}

function adjustCustom(control)
{
    
    var id = control.id;
    var total = parseIntVal(document.getElementById('ctl00_maincontent_PortfolioValue'));    
    var new_percent = Math.round(parseIntVal(control) * 100 / total );
    document.getElementById(id + '_percent').innerHTML = localeNumberFormat(new_percent, "percent", 2) + "%";
}

function parseIntVal(control)
{
    var lang = get_cookie("lang");
    
    if (control != null && control.value != '')
    {
        var val = lang == "en-US" ? parseFloat(control.value.replace(",", "")) : parseFloat(control.value.replace(",", "."));
        if (isNaN(val))
            return 0;
        return val;
    }
    return 0;
}

function parseFloatVal(control)
{
    if (control != null && control.value != '') {
        var tmp = 0;
        if (get_cookie("lang") == "en-US")
            tmp = control.value.replace(/,/g, '');
        else {
            tmp = control.value.replace(',', '.');
            tmp = tmp.replace(/ /g, '');
        }
        var val = parseFloat(tmp);
        if (isNaN(val))
            return 0;
        return val;
    }
    return 0;
}
//var subclass_accumulation = new Array("Cash Equivalents","Canadian Fixed Income","Fixed Income 1,2 years","Fixed Income 3,4 years","Fixed Income 5+ years","Fixed Income High Yield","Fixed Income International","Canadian Equities","Equity US","Equity International","Equity Emerging Markets","Equity Linked");
var subclass_accumulation = new Array("cash","fixedCdn","fixed12yrs","fixed34yrs","fixed5yrs","fixedHighYield","fixedIntl","equityCdn","equityUS","equityIntl","equityEmerging","equityLinked","equityGlobal","equitySmallCap","equityOther");

function adjustAccumulation() {
    var total = parseFloatVal(document.getElementById('ctl00_maincontent_PortfolioValue'));
    //document.getElementById('ctl00_maincontent_hdnCustom').value = 1;
    var new_total = 0;
    for (i = 0; i < subclass_accumulation.length; i++) {
        var t = parseFloatVal(document.getElementById('ctl00_maincontent_' + subclass_accumulation[i]));
        new_total += t;
    }
    //document.getElementById('grandtotal').innerHTML = localeNumberFormat(Math.round(new_total * 100) / 100, "currency", 2);
    document.getElementById('grandtotal').innerHTML = localeNumberFormat(total, "currency", 2);

    for (i = 0; i < subclass_accumulation.length; i++) {
        var t = parseFloatVal(document.getElementById('ctl00_maincontent_' + subclass_accumulation[i]));
        var p = parseFloatVal(document.getElementById(subclass_accumulation[i] + '_weight'));
        if (p > 0) {
            document.getElementById(subclass_accumulation[i] + '_p').innerHTML = localeNumberFormat(Math.round(t * 100 * 100 / new_total) / 100, "percent", 2) + "%";
        }    
    }

    //calculate over/under
    var diff = total - new_total;
    if (document.getElementById('over_under') != null) {
        document.getElementById('over_under').innerHTML = localeNumberFormat(Math.round(diff * 100) / 100, "currency", 2);
    }
}


function adjustTotals() {
    var total = parseIntVal(document.getElementById('ctl00_maincontent_PortfolioValue'));
    var fixedtotal = 0;
    var equitytotal = 0;
    if (isNaN(total)) return;
    if (document.getElementById('cash_weight')) {
        document.getElementById('ctl00_maincontent_' + 'cash').value = localeNumberFormat(Math.round(parseFloat(document.getElementById('cash_weight').value.replace(",",".")) * total) / 100, "percent", 2);
    }
    if (document.getElementById('fixedCdn_weight')) {
        document.getElementById('ctl00_maincontent_' + 'fixedCdn').value = localeNumberFormat(Math.round(parseFloat(document.getElementById('fixedCdn_weight').value.replace(",", ".")) * total) / 100, "percent", 2);
        fixedtotal += Math.round(parseFloat(document.getElementById('fixedCdn_weight').value.replace(",", ".")) * total) / 100;
    }
    if (document.getElementById('fixedIntl_weight')) {
        document.getElementById('ctl00_maincontent_' + 'fixedIntl').value = localeNumberFormat(Math.round(parseFloat(document.getElementById('fixedIntl_weight').value.replace(",", ".")) * total) / 100, "percent", 2);
        fixedtotal += Math.round(parseFloat(document.getElementById('fixedIntl_weight').value.replace(",", ".")) * total) / 100;
    }
    if (document.getElementById('fixedHighYield_weight')) {
        document.getElementById('ctl00_maincontent_' + 'fixedHighYield').value = localeNumberFormat(Math.round(parseFloat(document.getElementById('fixedHighYield_weight').value.replace(",", ".")) * total) / 100, "percent", 2);
        fixedtotal += Math.round(parseFloat(document.getElementById('fixedHighYield_weight').value.replace(",", ".")) * total) / 100;
    }
    if (document.getElementById('fixed12yrs_weight')) {
        document.getElementById('ctl00_maincontent_' + 'fixed12yrs').value = localeNumberFormat(Math.round(parseFloat(document.getElementById('fixed12yrs_weight').value.replace(",", ".")) * total) / 100, "percent", 2);
        fixedtotal += Math.round(parseFloat(document.getElementById('fixed12yrs_weight').value.replace(",", ".")) * total) / 100;
    }
    if (document.getElementById('fixed34yrs_weight')) {
        document.getElementById('ctl00_maincontent_' + 'fixed34yrs').value = localeNumberFormat(Math.round(parseFloat(document.getElementById('fixed34yrs_weight').value.replace(",", ".")) * total) / 100, "percent", 2);
        fixedtotal += Math.round(parseFloat(document.getElementById('fixed34yrs_weight').value.replace(",", ".")) * total) / 100;
    }
    if (document.getElementById('fixed5yrs_weight')) {
        document.getElementById('ctl00_maincontent_' + 'fixed5yrs').value = localeNumberFormat(Math.round(parseFloat(document.getElementById('fixed5yrs_weight').value.replace(",", ".")) * total) / 100, "percent", 2);
        fixedtotal += Math.round(parseFloat(document.getElementById('fixed5yrs_weight').value.replace(",", ".")) * total) / 100;
    }
    if (document.getElementById('equityCdn_weight')) {
        document.getElementById('ctl00_maincontent_' + 'equityCdn').value = localeNumberFormat(Math.round(parseFloat(document.getElementById('equityCdn_weight').value.replace(",", ".")) * total) / 100, "percent", 2);
        equitytotal += Math.round(parseFloat(document.getElementById('equityCdn_weight').value.replace(",", ".")) * total) / 100;
    }
    if (document.getElementById('equityIntl_weight')) {
        document.getElementById('ctl00_maincontent_' + 'equityIntl').value = localeNumberFormat(Math.round(parseFloat(document.getElementById('equityIntl_weight').value.replace(",", ".")) * total) / 100, "percent", 2);
        equitytotal += Math.round(parseFloat(document.getElementById('equityIntl_weight').value.replace(",", ".")) * total) / 100;
    }
    if (document.getElementById('equityUS_weight')) {
        document.getElementById('ctl00_maincontent_' + 'equityUS').value = localeNumberFormat(Math.round(parseFloat(document.getElementById('equityUS_weight').value.replace(",", ".")) * total) / 100, "percent", 2);
        equitytotal += Math.round(parseFloat(document.getElementById('equityUS_weight').value.replace(",", ".")) * total) / 100;
    }
    if (document.getElementById('equityLinked_weight')) {
        document.getElementById('ctl00_maincontent_' + 'equityLinked').value = localeNumberFormat(Math.round(parseFloat(document.getElementById('equityLinked_weight').value.replace(",", ".")) * total) / 100, "percent", 2);
        fixedtotal += Math.round(parseFloat(document.getElementById('equityLinked_weight').value.replace(",", ".")) * total) / 100;
    }
    if (document.getElementById('equityEmerging_weight')) {
        document.getElementById('ctl00_maincontent_' + 'equityEmerging').value = localeNumberFormat(Math.round(parseFloat(document.getElementById('equityEmerging_weight').value.replace(",", ".")) * total) / 100, "percent", 2);
        equitytotal += Math.round(parseFloat(document.getElementById('equityEmerging_weight').value.replace(",", ".")) * total) / 100;
    }
    if (document.getElementById('equityGlobal_weight')) {
        document.getElementById('ctl00_maincontent_' + 'equityGlobal').value = localeNumberFormat(Math.round(parseFloat(document.getElementById('equityGlobal_weight').value.replace(",", ".")) * total) / 100, "percent", 2);
        equitytotal += Math.round(parseFloat(document.getElementById('equityGlobal_weight').value.replace(",", ".")) * total) / 100;
    }
    if (document.getElementById('equitySmallCap_weight')) {
        document.getElementById('ctl00_maincontent_' + 'equitySmallCap').value = localeNumberFormat(Math.round(parseFloat(document.getElementById('equitySmallCap_weight').value.replace(",", ".")) * total) / 100, "percent", 2);
        equitytotal += Math.round(parseFloat(document.getElementById('equitySmallCap_weight').value.replace(",", ".")) * total) / 100;
    }
    if (document.getElementById('equityOther_weight')) {
        document.getElementById('ctl00_maincontent_' + 'equityOther').value = localeNumberFormat(Math.round(parseFloat(document.getElementById('equityOther_weight').value.replace(",", ".")) * total) / 100, "percent", 2);
        equitytotal += Math.round(parseFloat(document.getElementById('equityOther_weight').value.replace(",", ".")) * total) / 100;
    }
    if (document.getElementById('cash_weight')) {
        document.getElementById('cashtotal').innerHTML = localeNumberFormat(Math.round(parseFloat(document.getElementById('cash_weight').value.replace(",",".")) * total) / 100, "currency", 2);
        cash_weight = document.getElementById('cash_weight').value.replace(",", ".");
    } else {
        cash_weight = 0;
    }
    if (document.getElementById('fixedtotal')) {
        document.getElementById('fixedtotal').innerHTML = localeNumberFormat(Math.round(fixedtotal * 100) / 100, "currency", 2);
    }
    document.getElementById('equitytotal').innerHTML = localeNumberFormat(Math.round(equitytotal * 100) / 100, "currency", 2);
    //document.getElementById('grandtotal').innerHTML = localeNumberFormat(Math.round(((Math.round(parseFloat(cash_weight) * total) / 100 + Math.round(equitytotal * 100) / 100 + Math.round(fixedtotal * 100) / 100)) * 100) / 100, "currency", 2);
    document.getElementById('grandtotal').innerHTML = localeNumberFormat(total, "currency", 2);
    if (document.getElementById('over_under') != null) {
        document.getElementById('over_under').innerHTML = localeNumberFormat(Math.round((total - (Math.round(((Math.round(parseFloat(cash_weight) * total) / 100 + Math.round(equitytotal * 100) / 100 + Math.round(fixedtotal * 100) / 100)) * 100) / 100)) * 100) / 100, "currency", 2);
    }
}

function clearSelections() {
    if (document.aspnetForm.question1) {
        document.aspnetForm.question1.selectedIndex = 0;
    }
    if (document.aspnetForm.question2) {
        document.aspnetForm.question2.selectedIndex = 0;
    }
    if (document.aspnetForm.question3) {
        document.aspnetForm.question3.selectedIndex = 0;
    }
    if (document.aspnetForm.question4) {
        document.aspnetForm.question4.selectedIndex = 0;
    }
    if (document.aspnetForm.question5) {
        document.aspnetForm.question5.selectedIndex = 0;
    }
    if (document.aspnetForm.question6) {
        document.aspnetForm.question6.selectedIndex = 0;
    }
    if (document.aspnetForm.question7) {
        document.aspnetForm.question7.selectedIndex = 0;
    }
}

function clearIG() {
    if (document.aspnetForm.ctl00_maincontent_txtTotalValue) {
        document.aspnetForm.ctl00_maincontent_txtTotalValue.value = "";
    }
    if (document.aspnetForm.ctl00_maincontent_ddlYears) {
        document.aspnetForm.ctl00_maincontent_ddlYears.selectedIndex = 0;
    }
    if (document.aspnetForm.ctl00_maincontent_txtMonthlyIncome) {
        document.aspnetForm.ctl00_maincontent_txtMonthlyIncome.value = "";
    }
    if (document.aspnetForm.ctl00_maincontent_txtIncomeOther) {
        document.aspnetForm.ctl00_maincontent_txtIncomeOther.value = "";
    }
    if (document.aspnetForm.ctl00_maincontent_txtEstate) {
        document.aspnetForm.ctl00_maincontent_txtEstate.value = "";
    }
    if (document.aspnetForm.ctl00_maincontent_ddlInflation) {
        document.aspnetForm.ctl00_maincontent_ddlInflation.selectedIndex = 0;
    }
    
}
function validateMainPageFilter(msg, warning) {
	var issel=false;
	if (document.aspnetForm.question1.selectedIndex == 0) {
	    alert(msg);
		return false;
	}
	if (document.aspnetForm.question2.selectedIndex == 0) {
	    alert(msg);
		return false;
	}
	if (document.aspnetForm.question3.selectedIndex == 0) {
	    alert(msg);
		return false;
	}
	if (document.aspnetForm.question4.selectedIndex == 0) {
	    alert(msg);
		return false;
	}
	if (document.aspnetForm.question5.selectedIndex == 0) {
	    alert(msg);
		return false;
	}
	if (document.aspnetForm.question6.selectedIndex == 0) {
	    alert(msg);
		return false;
	}
	if (warning) {
	    document.getElementById('warning').value = "1";
	} else {
	    document.getElementById('warning').value = "0";
	}
	document.aspnetForm.submit();
	return true;
}

function validatePreservationPageFilter(msg) {
	if (document.aspnetForm.question1.selectedIndex == 0) {
	    alert(msg);
		return false;
	}
	if (document.aspnetForm.question2.selectedIndex == 0) {
	    alert(msg);
		return false;
	}
	if (document.aspnetForm.question3.selectedIndex == 0) {
	    alert(msg);
		return false;
	}
	document.aspnetForm.submit();
	return false;
}

function validateAccumulationPageFilter(msg) {
    if (document.aspnetForm.question1.selectedIndex == 0) {
	    alert(msg);
		return false;
	}
	if (document.aspnetForm.question2.selectedIndex == 0) {
	    alert(msg);
		return false;
	}
	if (document.aspnetForm.question3.selectedIndex == 0) {
	    alert(msg);
		return false;
	}
	if (document.aspnetForm.question4.selectedIndex == 0) {
	    alert(msg);
		return false;
	}
	if (document.aspnetForm.question5.selectedIndex == 0) {
	    alert(msg);
		return false;
	}
	if (document.aspnetForm.question6.selectedIndex == 0) {
	    alert(msg);
		return false;
	}
	if (document.aspnetForm.question7.selectedIndex == 0) {
	    alert(msg);
		return false;
	}
	document.aspnetForm.submit();
	return false;
}

function validateIncomePageFilter(msg) {
    if (document.aspnetForm.question1.selectedIndex == 0) {
	    alert(msg);
		return false;
	}
	if (document.aspnetForm.question2.selectedIndex == 0) {
	    alert(msg);
		return false;
	}
	if (document.aspnetForm.question3.selectedIndex == 0) {
	    alert(msg);
		return false;
	}
	if (document.aspnetForm.question4.selectedIndex == 0) {
	    alert(msg);
		return false;
	}
	if (document.aspnetForm.question5.selectedIndex == 0) {
	    alert(msg);
		return false;
	}
	if (document.aspnetForm.question6.selectedIndex == 0) {
	    alert(msg);
		return false;
	}
	if (document.aspnetForm.question7.selectedIndex == 0) {
	    alert(msg);
		return false;
	}
	document.aspnetForm.submit();
	return false;
}

//Show dialog for hard stop
function showHardStop() {
    alert('hard stop');
}

//Show confirm dialog for soft stop
function showSoftStop(url) {
	if (confirm("soft stop?"))
	{
		window.location = url;
	}
}

function customPopup(url, features) {
	window.open(url, '', features);
}


//autoselect answers
function preselectAnswers() {
    var pageName = document.getElementById('hdnPageName');
    //if (pageName.value == 'Preservation' || pageName.value == 'Accumulation' || pageName.value == "Income" || ) {
    if (pageName.value == 'Needs') {
        document.getElementById('question1').selectedIndex = parseInt(gup('q1'));
        document.getElementById('question2').selectedIndex = parseInt(gup('q2'));
        document.getElementById('question3').selectedIndex = parseInt(gup('q3'));
        document.getElementById('question4').selectedIndex = parseInt(gup('q4'));
        document.getElementById('question5').selectedIndex = parseInt(gup('q5'));
        document.getElementById('question6').selectedIndex = parseInt(gup('q6'));
    }
    
        var q1 = document.getElementById('ctl00_maincontent_hdnQuestion1');
        if (q1 != null && q1.value != '') {
            document.getElementById('question1').selectedIndex  = parseInt(q1.value);
        }
        var q2 = document.getElementById('ctl00_maincontent_hdnQuestion2');
        if (q2 != null && q2.value != '') {
            document.getElementById('question2').selectedIndex  = parseInt(q2.value);
        }
        var q3 = document.getElementById('ctl00_maincontent_hdnQuestion3');
        if (q3 != null && q3.value != '') {
            document.getElementById('question3').selectedIndex  = parseInt(q3.value);
        }
        var q4 = document.getElementById('ctl00_maincontent_hdnQuestion4');
        if (q4 != null && q4.value != '') {
            document.getElementById('question4').selectedIndex  = parseInt(q4.value);
        }
        var q5 = document.getElementById('ctl00_maincontent_hdnQuestion5');
        if (q5 != null && q5.value != '') {
            document.getElementById('question5').selectedIndex  = parseInt(q5.value);
        }
        var q6 = document.getElementById('ctl00_maincontent_hdnQuestion6');
        if (q6 != null && q6.value != '') {
            document.getElementById('question6').selectedIndex  = parseInt(q6.value);
        }
        var q7 = document.getElementById('ctl00_maincontent_hdnQuestion7');
        if (q7 != null && q7.value != '') {
            document.getElementById('question7').selectedIndex  = parseInt(q7.value);
        }
    //}
}

function okAlternatives(newids)
{
    var theForm = document.forms['aspnetForm']; 
    theForm.__EVENTTARGET.value = "okAlternatives";
    document.getElementById('ctl00_maincontent_hdnCustom').value = 1;
    document.getElementById('ctl00_maincontent_hdnNewProducts').value = newids;
    document.forms['aspnetForm'].submit();
}

function deleteProduct(id)
{
    document.getElementById('hdnToDelete').value = id;
}


function gup(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}

function myChangeHandler() {
    if (this.options[this.selectedIndex].value != 0) {
        window.open(this.options[this.selectedIndex].value, '_blank');
    }
}
