//----------------------------------------------------------
// 共通関数
//----------------------------------------------------------

function num2fig(Numeric){
	Numeric += '';

	//うっかり入っていたカンマを消す(=fig2num())
	var Separator = Numeric.indexOf(',',0);
	while (Separator != -1){
		Numeric = Numeric.substring(0, Separator) + Numeric.substring(Separator+1, Numeric.length);
		Separator = Numeric.indexOf(',',0);
	}

	//小数点を探し、小数点以下と整数部を分割して保持する
	var DecimalPoint = Numeric.lastIndexOf('.');
	if (DecimalPoint == -1){
		var Decimals = '';
		var Integers = Numeric + '';
	} else {
		var Decimals = Numeric.substring(DecimalPoint,Numeric.length) + '';
		var Integers = Numeric.substring(0,DecimalPoint) + '';
	}
	//整数部の文字列長を3の倍数にする。足りない分は手前に' 'を埋め込む
	Blanks = Integers.length % 3;
	if (Blanks != 0){
		for (var i = 0; 3-Blanks > i ; i++){
			Integers = ' ' + Integers;
		}
	}

	//整数文字列先頭から3文字おきにカンマを挿入する
	//先頭がマイナス符号の時は負数として処理する
	FigureInteger = Integers.substring(0,3);
	var j = 2;
	if (Integers.charAt(2) == '-'){
		FigureInteger = FigureInteger + Integers.substring(3,6);
		j=4;
	}
		for (i = j; Integers.length > i ; i++){
		if (i % 3 == 0){
			FigureInteger = FigureInteger + ',' + Integers.substring(i,i+3);
		}
	}

	//臨時に入れておいた' 'を削除する
	while (FigureInteger.charAt(0) == ' '){
		FigureInteger = FigureInteger.substring(1,FigureInteger.length);
	}

	//整形済みの整数部と、待避してあった小数部を連結。連結した文字列を返して終了！
	CommaNumber = FigureInteger + Decimals;
	return CommaNumber;
}


/* 桁区切のカンマを消す。引数FigureNumは、カンマ入りの1バイト数字文字列 */
function fig2num(FigureNum){
    var Numeric = FigureNum;

    //カンマをすべて消す
    var Separator = Numeric.indexOf(',',0);
    while (Separator != -1){
        Numeric = Numeric.substring(0, Separator) + Numeric.substring(Separator+1, Numeric.length);
        Separator = Numeric.indexOf(',',0);
    }
    //カンマ消去後の数値を返して終了！
    return Numeric;
}


//----------------------------------------------------------
// 運用後の元本額を計算するツール（普通預金・定期預金） - 終価係数
//----------------------------------------------------------

function calc1(CL){

	CL.A1.value = num2fig(CL.A1.value);	// 運用元本
	CL.B1.value = num2fig(CL.B1.value);	// 運用期間
	CL.C1.value = num2fig(CL.C1.value);	// 付利期間
	CL.D1.value = num2fig(CL.D1.value);	// 利率

	var S		= 0;		// 運用後元本金額
	var S1	= 0;		// 運用後元本金額（税引後１）
	var S2	= 0;		// 運用後元本金額（税引後2）

	var P		= 0;		// 運用元本
	var i		= 0;		// 利率
	var I		= 0;		// I = 1 + i 
	var n		= 0;		// 運用期間÷付利期間

	P		= eval( fig2num( CL.A1.value ) );

	if( eval( CL.B1.value ) <= 0 ) { CL.B1.value = 12; }

	var fukuli_flag = 0;

	if( eval( fig2num( CL.B1.value ) ) < eval( fig2num( CL.C1.value ) ) ) {
		fukuli_flag = 0;
	} else {
		fukuli_flag = 1;
	}

	switch( fukuli_flag ){

		case 0:

			i		= eval( fig2num( CL.D1.value ) ) * ( eval( fig2num( CL.B1.value ) ) / 12 ) / 100;
			I		= i + 1;

			// 運用後元本（税引前）の出力
			S  = P * I;
			S  = Math.floor( S );
			CL.E1.value = num2fig( S );

		/*
			// 運用後元本（税引後 - 満期時一括課税）の出力
			S1 = ( S - P ) * 0.8 + P;
			S1 = Math.floor( S1 );
			CL.F1.value = num2fig( S1 );
		*/

			// 運用後元本（税引後 - 利息付加時課税）の出力
			S2 = P * ( ( i * 0.8 ) + 1 );
			S2 = Math.floor(S2);
			CL.G1.value = num2fig( S2 );

			// 年平均利回り（税引前）の出力
			S3 = ( S / P ) - 1;
			S3 = S3 / ( fig2num( CL.B1.value ) / 12 ) * 100;
			S3 = S3.toFixed( 2 );
			CL.H1.value = num2fig( S3 );

			// 年平均利回り（税引後）の出力
			S4 = ( S2 / P ) - 1;
			S4 = S4 / ( fig2num( CL.B1.value ) / 12 ) * 100;
			S4 = S4.toFixed( 2 );
			CL.I1.value = num2fig( S4 );
		break;

		case 1:

			i		= eval( fig2num( CL.D1.value ) ) * ( eval( fig2num( CL.C1.value ) ) / 12 ) / 100;
			I		= i + 1;
			n		= eval( fig2num( CL.B1.value ) ) / eval( fig2num( CL.C1.value ) );

			// 運用後元本（税引前）の出力
			S  = P * Math.pow( I, n );
			S  = Math.floor( S );
			CL.E1.value = num2fig( S );

		/*
			// 運用後元本（税引後 - 満期時一括課税）の出力
			S1 = ( S - P ) * 0.8 + P;
			S1 = Math.floor( S1 );
			CL.F1.value = num2fig( S1 );
		*/

			// 運用後元本（税引後 - 利息付加時課税）の出力
			I  = i * 0.8 + 1;
			S2 = P * Math.pow( I, n );
			S2 = Math.floor(S2);
			CL.G1.value = num2fig( S2 );

			// 年平均利回り（税引前）の出力
			S3 = ( S / P ) - 1;
			S3 = S3 / ( fig2num( CL.B1.value ) / 12 ) * 100;
			S3 = S3.toFixed( 2 );
			CL.H1.value = num2fig( S3 );

			// 年平均利回り（税引後）の出力
			S4 = ( S2 / P ) - 1;
			S4 = S4 / ( fig2num( CL.B1.value ) / 12 ) * 100;
			S4 = S4.toFixed( 2 );
			CL.I1.value = num2fig( S4 );
		break;
	}

}

function reset_calc1(CL){
	CL.A1.value = 0;
	CL.B1.value = 0;
	CL.C1.value = 12;
	CL.D1.value = 0;
	CL.E1.value = 0;
//	CL.F1.value = 0;
	CL.G1.value = 0;
	CL.H1.value = 0;
	CL.I1.value = 0;
}

//----------------------------------------------------------
// 運用後の元本額を計算するツール（定期積立なし） - 終価係数
//----------------------------------------------------------

function calc2(CL){

	CL.A1.value = num2fig(CL.A1.value);	// 運用元本
	CL.B1.value = num2fig(CL.B1.value);	// 運用期間
	CL.C1.value = num2fig(CL.C1.value);	// 利回り

	var S		= 0;		// 運用後元本金額

	var P		= 0;		// 運用元本
	var i		= 0;		// 利回り
	var I		= 0;		// I = 1 + i 
	var n		= 0;

	// データ取得（算出）
	P		= eval( fig2num( CL.A1.value ) );
	i		= eval( fig2num( CL.C1.value ) ) / 100;
	I		= i + 1;
	n		= eval( fig2num( CL.B1.value ) ) / 12;

	// 運用後元本（元本）の出力
	CL.D1.value = num2fig( P );

	// 運用後元本（利益）の出力
	S  = P * Math.pow( I, n );
	S  = Math.floor( S ) - P;
	CL.E1.value = num2fig( S );

	// 運用後元本（合計）の出力
	S1  = P * Math.pow( I, n );
	S1  = Math.floor( S1 );
	CL.F1.value = num2fig( S1 );

	// 年平均利回り（税引前）の出力
	S2 = ( S1 / P ) - 1;
	S2 = S2 / ( fig2num( CL.B1.value ) / 12 ) * 100;
	S2 = S2.toFixed( 2 );
	CL.G1.value = num2fig( S2 );
}

function reset_calc2(CL){
	CL.A1.value = 0;
	CL.B1.value = 0;
	CL.C1.value = 0;
	CL.D1.value = 0;
	CL.E1.value = 0;
	CL.F1.value = 0;
	CL.G1.value = 0;
}



//----------------------------------------------------------
// 必要な積立額　- 年金終価係数
//----------------------------------------------------------
function calc3(CL){
	var R = 0
	var S = 0
	var i  = 0
	var I  = 0	/* I = 1 + i */
	var n  = 0
	R = eval(CL.A5.value)
	i  = eval(CL.D5.value) * ( eval(CL.C5.value) / 12 ) / 100
	I  = i + 1
	n = eval(CL.B5.value) / eval(CL.C5.value)
	S = R * ( ( Math.pow(I,n) - 1 ) / i )
	S = Math.ceil(S)
	CL.E5.value = S
	I  = i * 0.8 + 1
	S = R * ( ( Math.pow(I,n) - 1 ) / ( i * 0.8 ) )
	S = Math.ceil(S)
	CL.F5.value = S
	CL.G5.value = R * n
}




//----------------------------------------------------------
// 4資産ポートフォリオの期待リターンと標準偏差を計算するツール
//----------------------------------------------------------

function init(){

var base_data = parseInt(document.portfolio.base_data.options.value);
var base_data = parseInt(document.portfolio.base_data.options[document.portfolio.base_data.options.selectedIndex].value);

switch(base_data){

case 10: // 野村総研（為替ヘッジなし）

	// 期待リターンの設定
	document.portfolio.return1.value = "3.69";	// 国内株式
	document.portfolio.return2.value = "2.03";	// 国内債券
	document.portfolio.return3.value = "7.62";	// 海外株式
	document.portfolio.return4.value = "4.24";	// 海外債券

	// 標準偏差の設定
	document.portfolio.stdev1.value = "19.83";	// 国内株式
	document.portfolio.stdev2.value = "3.67";	// 国内債券
	document.portfolio.stdev3.value = "18.05";	// 海外株式
	document.portfolio.stdev4.value = "11.02";	// 海外債券

	// 相関係数の設定
	document.portfolio.corre_de1.value = "1.00";
	document.portfolio.corre_de2.value = "0.04";
	document.portfolio.corre_de3.value = "0.30";
	document.portfolio.corre_de4.value = "-0.08";

	document.portfolio.corre_db1.value = "";
	document.portfolio.corre_db2.value = "1.00";
	document.portfolio.corre_db3.value = "-0.03";
	document.portfolio.corre_db4.value = "0.04";

	document.portfolio.corre_fe1.value = "";
	document.portfolio.corre_fe2.value = "";
	document.portfolio.corre_fe3.value = "1.00";
	document.portfolio.corre_fe4.value = "0.55";

	document.portfolio.corre_fb1.value = "";
	document.portfolio.corre_fb2.value = "";
	document.portfolio.corre_fb3.value = "";
	document.portfolio.corre_fb4.value = "1.00";
break;

case 20: // 野村総研（為替ヘッジあり）

	// 期待リターンの設定
	document.portfolio.return1.value = "3.69";	// 国内株式
	document.portfolio.return2.value = "2.03";	// 国内債券
	document.portfolio.return3.value = "6.12";	// 海外株式
	document.portfolio.return4.value = "2.74";	// 海外債券

	// 標準偏差の設定
	document.portfolio.stdev1.value = "19.83";	// 国内株式
	document.portfolio.stdev2.value = "3.67";	// 国内債券
	document.portfolio.stdev3.value = "13.78";	// 海外株式
	document.portfolio.stdev4.value = "4.03";	// 海外債券

	// 相関係数の設定
	document.portfolio.corre_de1.value = "1.00";
	document.portfolio.corre_de2.value = "0.04";
	document.portfolio.corre_de3.value = "0.43";
	document.portfolio.corre_de4.value = "0.07";

	document.portfolio.corre_db1.value = "";
	document.portfolio.corre_db2.value = "1.00";
	document.portfolio.corre_db3.value = "0.10";
	document.portfolio.corre_db4.value = "0.40";

	document.portfolio.corre_fe1.value = "";
	document.portfolio.corre_fe2.value = "";
	document.portfolio.corre_fe3.value = "1.00";
	document.portfolio.corre_fe4.value = "0.11";

	document.portfolio.corre_fb1.value = "";
	document.portfolio.corre_fb2.value = "";
	document.portfolio.corre_fb3.value = "";
	document.portfolio.corre_fb4.value = "1.00";
break;

case 30: // 年金積立金管理運用独立行政法人 - 基本ポートフォリオ

	// 期待リターンの設定
	document.portfolio.return1.value = "5.14";	// 国内株式
	document.portfolio.return2.value = "1.80";	// 国内債券
	document.portfolio.return3.value = "8.10";	// 海外株式
	document.portfolio.return4.value = "2.97";	// 海外債券

	// 標準偏差の設定
	document.portfolio.stdev1.value = "22.25";	// 国内株式
	document.portfolio.stdev2.value = "5.45";	// 国内債券
	document.portfolio.stdev3.value = "19.85";	// 海外株式
	document.portfolio.stdev4.value = "13.44";	// 海外債券

	// 相関係数の設定
	document.portfolio.corre_de1.value = "1.00";
	document.portfolio.corre_de2.value = "0.15";
	document.portfolio.corre_de3.value = "0.27";
	document.portfolio.corre_de4.value = "-0.26";

	document.portfolio.corre_db1.value = "";
	document.portfolio.corre_db2.value = "1.00";
	document.portfolio.corre_db3.value = "-0.05";
	document.portfolio.corre_db4.value = "-0.06";

	document.portfolio.corre_fe1.value = "";
	document.portfolio.corre_fe2.value = "";
	document.portfolio.corre_fe3.value = "1.00";
	document.portfolio.corre_fe4.value = "0.55";

	document.portfolio.corre_fb1.value = "";
	document.portfolio.corre_fb2.value = "";
	document.portfolio.corre_fb3.value = "";
	document.portfolio.corre_fb4.value = "1.00";
break;

case 40: // 企業年金連合会

	// 期待リターンの設定
	document.portfolio.return1.value = "7.00";	// 国内株式
	document.portfolio.return2.value = "2.00";	// 国内債券
	document.portfolio.return3.value = "8.00";	// 海外株式
	document.portfolio.return4.value = "2.50";	// 海外債券

	// 標準偏差の設定
	document.portfolio.stdev1.value = "24.00";	// 国内株式
	document.portfolio.stdev2.value = "5.00";	// 国内債券
	document.portfolio.stdev3.value = "19.50";	// 海外株式
	document.portfolio.stdev4.value = "13.00";	// 海外債券

	// 相関係数の設定
	document.portfolio.corre_de1.value = "1.00";
	document.portfolio.corre_de2.value = "0.20";
	document.portfolio.corre_de3.value = "-0.08";
	document.portfolio.corre_de4.value = "-0.06";

	document.portfolio.corre_db1.value = "";
	document.portfolio.corre_db2.value = "1.00";
	document.portfolio.corre_db3.value = "-0.07";
	document.portfolio.corre_db4.value = "-0.09";

	document.portfolio.corre_fe1.value = "";
	document.portfolio.corre_fe2.value = "";
	document.portfolio.corre_fe3.value = "1.00";
	document.portfolio.corre_fe4.value = "0.63";

	document.portfolio.corre_fb1.value = "";
	document.portfolio.corre_fb2.value = "";
	document.portfolio.corre_fb3.value = "";
	document.portfolio.corre_fb4.value = "1.00";
break;

case 50: // 内藤忍本

	// 期待リターンの設定
	document.portfolio.return1.value = "6.00";	// 国内株式
	document.portfolio.return2.value = "1.00";	// 国内債券
	document.portfolio.return3.value = "7.00";	// 海外株式
	document.portfolio.return4.value = "4.00";	// 海外債券

	// 標準偏差の設定
	document.portfolio.stdev1.value = "20.30";	// 国内株式
	document.portfolio.stdev2.value = "3.40";	// 国内債券
	document.portfolio.stdev3.value = "22.40";	// 海外株式
	document.portfolio.stdev4.value = "15.00";	// 海外債券

	// 相関係数の設定
	document.portfolio.corre_de1.value = "1.00";
	document.portfolio.corre_de2.value = "-0.50";
	document.portfolio.corre_de3.value = "-0.40";
	document.portfolio.corre_de4.value = "-0.60";

	document.portfolio.corre_db1.value = "";
	document.portfolio.corre_db2.value = "1.00";
	document.portfolio.corre_db3.value = "0.80";
	document.portfolio.corre_db4.value = "0.90";

	document.portfolio.corre_fe1.value = "";
	document.portfolio.corre_fe2.value = "";
	document.portfolio.corre_fe3.value = "1.00";
	document.portfolio.corre_fe4.value = "0.80";

	document.portfolio.corre_fb1.value = "";
	document.portfolio.corre_fb2.value = "";
	document.portfolio.corre_fb3.value = "";
	document.portfolio.corre_fb4.value = "1.00";
break;

}


/*
// アセット組入比率
document.portfolio.value_de_percentage.disabled = true;
document.portfolio.value_db_percentage.disabled = true;
document.portfolio.value_fe_percentage.disabled = true;
document.portfolio.value_fb_percentage.disabled = true;

// ポートフォリオ合計
document.portfolio.portfolio_total_amount.disabled = true;
document.portfolio.portfolio_total_percentage.disabled = true;

// 結果（期待リターン & 標準偏差 & 標準偏差×2 & シャープレシオ）
document.portfolio.result_return.disabled = true;
document.portfolio.result_stdev.disabled = true;
document.portfolio.result_stdev2.disabled = true;
document.portfolio.result_sharp_ratio.disabled = true;

// リスクフリーレート
document.portfolio.rfr1.disabled = true;
*/

// アセット組入比率
document.portfolio.value_de_percentage.readOnly = true;
document.portfolio.value_db_percentage.readOnly = true;
document.portfolio.value_fe_percentage.readOnly = true;
document.portfolio.value_fb_percentage.readOnly = true;

// ポートフォリオ合計
document.portfolio.portfolio_total_amount.readOnly = true;
document.portfolio.portfolio_total_percentage.readOnly = true;

// 結果（期待リターン & 標準偏差 & 標準偏差×2 & シャープレシオ）
document.portfolio.result_return.readOnly = true;
document.portfolio.result_stdev.readOnly = true;
document.portfolio.result_stdev2.readOnly = true;
document.portfolio.result_sharp_ratio.readOnly = true;

// データセルの入力制限
document.portfolio.rfr1.disabled = true;
document.portfolio.base_data.disabled = true;

disable_corre();
disable_vari();
disable_return_and_stdev();

}

function disable_return_and_stdev(){
document.portfolio.return1.disabled = true;
document.portfolio.return2.disabled = true;
document.portfolio.return3.disabled = true;
document.portfolio.return4.disabled = true;

document.portfolio.stdev1.disabled = true;
document.portfolio.stdev2.disabled = true;
document.portfolio.stdev3.disabled = true;
document.portfolio.stdev4.disabled = true;

}

function disable_corre(){

// 入力不可 - 相関係数
document.portfolio.corre_de1.disabled = true
document.portfolio.corre_de2.disabled = true
document.portfolio.corre_de3.disabled = true
document.portfolio.corre_de4.disabled = true

document.portfolio.corre_db1.disabled = true
document.portfolio.corre_db2.disabled = true
document.portfolio.corre_db3.disabled = true
document.portfolio.corre_db4.disabled = true

document.portfolio.corre_fe1.disabled = true
document.portfolio.corre_fe2.disabled = true
document.portfolio.corre_fe3.disabled = true
document.portfolio.corre_fe4.disabled = true

document.portfolio.corre_fb1.disabled = true
document.portfolio.corre_fb2.disabled = true
document.portfolio.corre_fb3.disabled = true
document.portfolio.corre_fb4.disabled = true

}


function disable_vari(){

// 入力不可 - 分散共分散マトリクス
document.portfolio.vari_de1.disabled = true
document.portfolio.vari_de2.disabled = true
document.portfolio.vari_de3.disabled = true
document.portfolio.vari_de4.disabled = true

document.portfolio.vari_db1.disabled = true
document.portfolio.vari_db2.disabled = true
document.portfolio.vari_db3.disabled = true
document.portfolio.vari_db4.disabled = true

document.portfolio.vari_fe1.disabled = true
document.portfolio.vari_fe2.disabled = true
document.portfolio.vari_fe3.disabled = true
document.portfolio.vari_fe4.disabled = true

document.portfolio.vari_fb1.disabled = true
document.portfolio.vari_fb2.disabled = true
document.portfolio.vari_fb3.disabled = true
document.portfolio.vari_fb4.disabled = true

}



function keisan(){ 

init();

// 値（金額）の取得
var value_de = fig2num(document.portfolio.value_de.value);
var value_db = fig2num(document.portfolio.value_db.value);
var value_fe = fig2num(document.portfolio.value_fe.value);
var value_fb = fig2num(document.portfolio.value_fb.value);

if( value_de == "" ) {value_de = 0;}
if( value_db == "" ) {value_db = 0;}
if( value_fe == "" ) {value_fe = 0;}
if( value_fb == "" ) {value_fb = 0;}

//合計（金額）の計算
var sum_amount = parseInt(value_de) + parseInt(value_db) + parseInt(value_fe) + parseInt(value_fb); 


if( value_de == 0 && value_db == 0 && value_fe == 0 && value_fb == 0 ) {
	// 値（金額）の表示
	document.portfolio.value_de.value = 0;
	document.portfolio.value_db.value = 0;
	document.portfolio.value_fe.value = 0;
	document.portfolio.value_fb.value = 0;
	document.portfolio.portfolio_total_amount.value = 0; // 合計を表示

	// 値（パーセンテージ）の表示
	document.portfolio.value_de_percentage.value = 0;
	document.portfolio.value_db_percentage.value = 0;
	document.portfolio.value_fe_percentage.value = 0;
	document.portfolio.value_fb_percentage.value = 0;
	document.portfolio.portfolio_total_percentage.value = 0; // 合計を表示

	document.portfolio.result_return.value = 0;
	document.portfolio.result_stdev.value = 0;
	document.portfolio.result_stdev2.value = 0;
	document.portfolio.result_sharp_ratio.value = 0;

	return;
}


// 値（金額）の表示
document.portfolio.value_de.value = num2fig(value_de);
document.portfolio.value_db.value = num2fig(value_db);
document.portfolio.value_fe.value = num2fig(value_fe);
document.portfolio.value_fb.value = num2fig(value_fb);
document.portfolio.portfolio_total_amount.value = num2fig(sum_amount); // 合計を表示


// 値（パーセンテージ）の計算
var value_de_percentage = value_de / sum_amount;
var value_db_percentage = value_db / sum_amount;
var value_fe_percentage = value_fe / sum_amount;
var value_fb_percentage = value_fb / sum_amount;
//var sum_percentage = parseInt(value_de_percentage) + parseInt(value_db_percentage) + parseInt(value_fe_percentage) + parseInt(value_fb_percentage); //合計を計算
var sum_percentage = value_de_percentage + value_db_percentage + value_fe_percentage + value_fb_percentage; //合計を計算

// 値（パーセンテージ）の表示
document.portfolio.value_de_percentage.value = Math.round(value_de_percentage * 100 * 100) / 100;
document.portfolio.value_db_percentage.value = Math.round(value_db_percentage * 100 * 100) / 100;
document.portfolio.value_fe_percentage.value = Math.round(value_fe_percentage * 100 * 100) / 100;
document.portfolio.value_fb_percentage.value = Math.round(value_fb_percentage * 100 * 100) / 100;
document.portfolio.portfolio_total_percentage.value = Math.round(sum_percentage * 100 * 100 ) / 100; // 合計を表示


// 値（期待リターン）の取得
var return_de = document.portfolio.return1.value / 100;	// 国内株式
var return_db = document.portfolio.return2.value / 100;	// 国内債券
var return_fe = document.portfolio.return3.value / 100;	// 海外株式
var return_fb = document.portfolio.return4.value / 100;	// 海外債券

// 計算と表示
var return_output = ( 
(return_de * value_de_percentage) + 
(return_db * value_db_percentage) + 
(return_fe * value_fe_percentage) + 
(return_fb * value_fb_percentage) ) * 100;

document.portfolio.result_return.value = Math.round(return_output * 100) / 100;

// 値（標準偏差）の取得
var stdev_de = document.portfolio.stdev1.value / 100;	// 国内株式
var stdev_db = document.portfolio.stdev2.value / 100;	// 国内債券
var stdev_fe = document.portfolio.stdev3.value / 100;	// 海外株式
var stdev_fb = document.portfolio.stdev4.value / 100;	// 海外債券


// 値（相関関係）の取得
var corre_de1 = document.portfolio.corre_de1.value;
var corre_de2 = document.portfolio.corre_de2.value;
var corre_de3 = document.portfolio.corre_de3.value;
var corre_de4 = document.portfolio.corre_de4.value;

var corre_db2 = document.portfolio.corre_db2.value;
var corre_db3 = document.portfolio.corre_db3.value;
var corre_db4 = document.portfolio.corre_db4.value;

var corre_fe3 = document.portfolio.corre_fe3.value;
var corre_fe4 = document.portfolio.corre_fe4.value;

var corre_fb4 = document.portfolio.corre_fb4.value;



// 分散共分散マトリクスの計算
var vari_de1 = Math.pow(value_de_percentage,2) * Math.pow(stdev_de,2) * 100;
var vari_de2 = stdev_de * value_de_percentage * stdev_db * value_db_percentage * corre_de2 * 100;
var vari_de3 = stdev_de * value_de_percentage * stdev_fe * value_fe_percentage * corre_de3 * 100;
var vari_de4 = stdev_de * value_de_percentage * stdev_fb * value_fb_percentage * corre_de4 * 100;

var vari_db1 = vari_de2;
var vari_db2 = Math.pow(value_db_percentage,2) * Math.pow(stdev_db,2) * 100;
var vari_db3 = stdev_db * value_db_percentage * stdev_fe * value_fe_percentage * corre_db3 * 100;
var vari_db4 = stdev_db * value_db_percentage * stdev_fb * value_fb_percentage * corre_db4 * 100;

var vari_fe1 = vari_de3;
var vari_fe2 = vari_db3;
var vari_fe3 = Math.pow(value_fe_percentage,2) * Math.pow(stdev_fe,2) * 100;
var vari_fe4 = stdev_fe * value_fe_percentage * stdev_fb * value_fb_percentage * corre_fe4 * 100;

var vari_fb1 = vari_de4;
var vari_fb2 = vari_db4;
var vari_fb3 = vari_fe4;
var vari_fb4 = Math.pow(value_fb_percentage,2) * Math.pow(stdev_fb,2) * 100;

// 分散共分散マトリクスの表示
document.portfolio.vari_de1.value = Math.round(vari_de1 * 100 * 100) / 100;
document.portfolio.vari_de2.value = Math.round(vari_de2 * 100 * 100) / 100;
document.portfolio.vari_de3.value = Math.round(vari_de3 * 100 * 100) / 100;
document.portfolio.vari_de4.value = Math.round(vari_de4 * 100 * 100) / 100;

document.portfolio.vari_db1.value = Math.round(vari_db1 * 100 * 100) / 100;
document.portfolio.vari_db2.value = Math.round(vari_db2 * 100 * 100) / 100;
document.portfolio.vari_db3.value = Math.round(vari_db3 * 100 * 100) / 100;
document.portfolio.vari_db4.value = Math.round(vari_db4 * 100 * 100) / 100;

document.portfolio.vari_fe1.value = Math.round(vari_fe1 * 100 * 100) / 100;
document.portfolio.vari_fe2.value = Math.round(vari_fe2 * 100 * 100) / 100;
document.portfolio.vari_fe3.value = Math.round(vari_fe3 * 100 * 100) / 100;
document.portfolio.vari_fe4.value = Math.round(vari_fe4 * 100 * 100) / 100;

document.portfolio.vari_fb1.value = Math.round(vari_fb1 * 100 * 100) / 100;
document.portfolio.vari_fb2.value = Math.round(vari_fb2 * 100 * 100) / 100;
document.portfolio.vari_fb3.value = Math.round(vari_fb3 * 100 * 100) / 100;
document.portfolio.vari_fb4.value = Math.round(vari_fb4 * 100 * 100) / 100;


// 標準偏差の計算と表示
var vari_total = 
vari_de1 + vari_de2 + vari_de3 + vari_de4 +
vari_db1 + vari_db2 + vari_db3 + vari_db4 +
vari_fe1 + vari_fe2 + vari_fe3 + vari_fe4 +
vari_fb1 + vari_fb2 + vari_fb3 + vari_fb4;

document.portfolio.result_stdev.value = Math.round( Math.sqrt(vari_total) * 100 * 10 ) / 100;
document.portfolio.result_stdev2.value = Math.round( Math.sqrt(vari_total) * 2 * 100 * 10 ) / 100;


// 値（リスクフリーレート）の取得
var rfr = document.portfolio.rfr1.value;

// シャープレシオの計算と表示
var sharp_ratio = ( return_output - rfr ) / Math.sqrt(vari_total);
document.portfolio.result_sharp_ratio.value = Math.round( sharp_ratio * 100 ) / 100;


//var chart_type = document.portfolio.chart_type.options.value;

drawPieChart();

} 


// -------------------------------- Utilities -------------------------------------

function getRadioValue(element){
	var len;
	len = element.length;
	for (i=0;i<len;i++) {
	if (element[i].checked) return element[i].value;
	}
	return "";
}

// -------------------------------- AJAX -------------------------------------

//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest(); //Not IE
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP"); //IE
	} else {
		alert("このブラウザではグラフの描画ができません。");
	}
}

//Get our browser specific XmlHttpRequest object.
var receiveReq = getXmlHttpRequestObject();


//Initiate the asyncronous request.
function drawPieChart() {

	//If our XmlHttpRequest object is not in the middle of a request, start the new asyncronous call.
	if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
		receiveReq.open("GET", 'http://blog.ontheroad.jp/tools/portfolio01_detail.php', true);
		receiveReq.open("GET", 'http://blog.ontheroad.jp/tools/portfolio01.php', true);
	}
	receiveReq.onreadystatechange = handleDrawPieChart;
	receiveReq.send(null);
}



//Called every time our XmlHttpRequest objects state changes.

function handleDrawPieChart() {

	if (receiveReq.readyState == 4) {

		a = document.portfolio.value_de_percentage.value;
		b = document.portfolio.value_db_percentage.value;
		c = document.portfolio.value_fe_percentage.value;
		d = document.portfolio.value_fb_percentage.value;

		var view_a = "国内株式（" + a +"％）";
		var view_b = "国内債権（" + b +"％）";
		var view_c = "外国株式（" + c +"％）";
		var view_d = "外国債券（" + d +"％）";


		var obj = document.getElementById("chart_type");
		var selected = obj.selectedIndex;
		chart_type = obj.options[selected].value;

		var output;

		if( chart_type != "" ) {
			output =
			'<center><img src="http://chart.apis.google.com/chart?cht=' + chart_type + '&chd=t:' + a + ',' + b + ',' + c + ',' + d + 
			'&chs=500x180&chl=' + view_a + '|' + view_b + '|' + view_c + '|' + view_d + '&chco=0000ff"></center>';
		} else {
			output ="";
		}

//		alert( document.getElementById('chart').innerHTML );
//		document.getElementById('chart').innerHTML = receiveReq.responseText;
		document.getElementById('chart').innerHTML = output;
	}

}



