var ac = new Object();

ac[1] = 0.5
ac[2] = 0.4
ac[3] = 0.3
ac[4] = 0.2
ac[5] = 0.1
ac[6] = 0

function numeric(w_dat){
	var W_str = "";

	for(i=0; i<w_dat.length; i++){
		W_char=w_dat.substring(i,i+1);
		if((W_char == "0")||(W_char == "1")||
		   (W_char == "2")||(W_char == "3")||
		   (W_char == "4")||(W_char == "5")||
		   (W_char == "6")||(W_char == "7")||
		   (W_char == "8")||(W_char == "9")){
			 W_str = W_str + W_char;
		}
		else{
			W_str="";
			i=w_dat.length;
		}  
	}
	if(W_str.length == 0){
		W_str="-1"
	}
	rtn = eval(W_str);
	return(rtn);
}

function rate_check(w_dat){
	var W_str = "";

	for(i=0; i<w_dat.length; i++){
		W_char=w_dat.substring(i, i+1);
		if((W_char == "0")||(W_char == "1")||
		   (W_char == "2")||(W_char == "3")||
		   (W_char == "4")||(W_char == "5")||
		   (W_char == "6")||(W_char == "7")||
		   (W_char == "8")||(W_char == "9")){
			W_str=W_str+W_char;
		}
		else{
			if((W_char!=".")||(i!=1)){ 
				W_str="";
				i = w_dat.length;
			}
			else{
				if(w_dat.length != i){
					W_str=W_str+W_char;
				}
			}
		}  
	}
	if(W_str.length == 0){
		W_str="-1"
	}
	rtn=eval(W_str);
	return(rtn);
}

function trim(w_dat){
	var W_str = "";

	start = -1;
	for(i=0; i<w_dat.length; i++){
		W_char=w_dat.substring(i, i + 1);
		if((W_char != " ")&&(start == -1)){
			start=i;
		}
	}
	end = -1;
	for(i=start; i<w_dat.length; i++){
		W_char=w_dat.substring(i, i + 1);
		if((W_char == " ")&&(end == -1)){
			end=i;
		}
	}
	if(end == -1){
		end = w_dat.length;
	}
	return(w_dat.substring(start,end));
}

function calc(form){ 
	var err_flag=0;

	form.loan.value = trim(form.loan.value);     
	form.kikan.value = trim(form.kikan.value);     
	form.rate.value = trim(form.rate.value);     

//借り入れ額のチェック
	wk_loan=numeric(form.loan.value);
	if((wk_loan<1)||(wk_loan>9999)){
		alert("お借り入れ額に誤りがあります。");
		err_flag=1;
		form.loan.focus();
		form.loan.select();
	}
	else{

//返済期間のチェック
		wk_kikan=numeric(form.kikan.value);
		if((wk_kikan<1)||(wk_kikan>40)){
			alert("ご返済期間に誤りがあります。");
			err_flag=1;
			form.kikan.focus();
			form.kikan.select();
		}
		else{
//借り入れ金利のチェック
			wk_rate=rate_check(form.rate.value);
			if((wk_rate<=0)||(wk_rate>9.999)){
				alert("お借り入れ金利に誤りがあります。");
				err_flag=1;
				form.rate.focus();
				form.rate.select();
			}
		}
	}

//エラーが発生していない場合
	if(err_flag!=1){
		n=form.bonus.selectedIndex+1;
		rt=ac[n];
		answer1 = Math.floor((wk_loan*10000)-(wk_loan * 10000 * rt)) * (wk_rate / 12 / 100) * (1 + (1 / (Math.pow((wk_rate / 12 / 100) + 1,wk_kikan * 12) - 1)));
		answer1 = Math.floor(answer1);
		if(rt != 0){

//   ボーナス月返済
			bkaisu=(wk_kikan) * 2;
			answer2 = (6 * (wk_rate / 12 / 100) * (wk_loan * 10000 * rt) * ((6 * (wk_rate / 12 / 100)) + 1)) / ((6 * (wk_rate / 12 / 100)) + 1) * ((1 / ((Math.pow(((6 * (wk_rate / 12 / 100)) + 1), ((wk_kikan) * 2)) - 1))) + 1);
			answer2 = Math.floor(answer2);
		}
		else{
			answer2 = "0";
		}

//   1年間の返済額
		answer3 = answer1 * 12 + answer2 * 2;
		wk_man=separate_man("'"+answer1);
		form.tuki1.value=wk_man;
		wk_man=separate_man("'"+answer2);
		form.bonus1.value=wk_man;
		wk_man=separate_man("'"+answer3);
		form.nen1.value=wk_man;
		wk_sen=separate_sen("'"+answer1);
		form.tuki2.value=wk_sen;
		wk_sen=separate_sen("'"+answer2);
		form.bonus2.value=wk_sen;
		wk_sen=separate_sen("'"+answer3);
		form.nen2.value=wk_sen;
	}
	else{
		form.tuki1.value=" ";
		form.bonus1.value=" ";
		form.nen1.value=" ";
		form.tuki2.value=" ";
		form.bonus2.value=" ";
		form.nen2.value=" ";
	}         
}

function separate_man(kingaku){
	var cnt = kingaku.length;

	if(cnt>5){
		w_man=kingaku.substring(0,cnt-4);
		cnt=w_man.length;
		if(cnt>4){
			j=0
			w_str="";
			p1="";
			p2="";
			for(i=1;i<cnt;i++){
				p1=i;
				p2=i+1;
				w_char=w_man.substring(p1,p2);
				w_str=w_str+w_char;
				if(i==cnt-4){ 
					w_str=w_str+",";
				}
			}
			w_man=w_str; 
		}
		else{
			w_man=w_man.substring(1,cnt);  
		}
	}    
	else{
		w_man="";
	}
	cnt=w_man.length;
	cnt=5-cnt; 
	for(i=0;i<cnt;i++){
		w_man=" "+w_man;
	}
	return(w_man);
}

function separate_sen(kingaku){
	var cnt=kingaku.length;

	w_sen="";
	if(cnt>5){
		w_sen=kingaku.substring(cnt-4,cnt);
	}
	else{
		w_sen=kingaku.substring(1,cnt);       
	}
	if(w_sen=="0000"){
		w_sen="'0";
	}
	else{
		if(w_sen.substring(0,3)=="000"){
			w_sen="'"+w_sen.substring(3,4);
		}
		else{
			if(w_sen.substring(0,2)=="00"){
				w_sen="'"+w_sen.substring(2,4);
			}
			else{
				if(w_sen.substring(0,1)=="0"){
					w_sen="'"+w_sen.substring(1,4);	
				}
				else{
					w_sen="'"+w_sen;
				}
			}
		}
	}	
	cnt=w_sen.length;   
	if(cnt>4){
		w_sen=w_sen.substring(1,2)+","+w_sen.substring(2,5); 
	}
	else{
		w_sen=w_sen.substring(1,cnt);  
	}
	cnt=w_sen.length;
	cnt=5-cnt; 
	for(i=0;i<cnt;i++){
		w_sen=" "+w_sen;
	}
	return(w_sen);
}

function clr(form){
	form.loan.value=" ";
	form.kikan.value=" ";
	form.rate.value=" ";
	form.tuki1.value=" ";
	form.tuki2.value=" ";
	form.bonus1.value=" ";
	form.bonus2.value=" ";
	form.nen1.value=" ";
	form.nen2.value=" ";
	form.bonus.selectedIndex=0;
	form.loan.focus();
	form.loan.select();
}
