// JavaScript Document

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}


/*
Copyright (c) 2006, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
http://developer.yahoo.net/yui/license.txt
Version: 0.11.4
*/

/**
 * The YAHOO object is the single global object used by YUI Library.  It
 * contains utility function for setting up namespaces, inheritance, and
 * logging.  YAHOO.util, YAHOO.widget, and YAHOO.example are namespaces
 * created automatically for and used by the library.
 * @module YAHOO
 */

/**
 * The YAHOO global namespace object
 * @class YAHOO
 * @static
 */
if (typeof YAHOO == "undefined") {
    YAHOO = {};
}

/**
 * Returns the namespace specified and creates it if it doesn't exist
 *
 * YAHOO.namespace("property.package");
 * YAHOO.namespace("YAHOO.property.package");
 *
 * Either of the above would create YAHOO.property, then
 * YAHOO.property.package
 *
 * Be careful when naming packages. Reserved words may work in some browsers
 * and not others. For instance, the following will fail in Safari:
 *
 * YAHOO.namespace("really.long.nested.namespace");
 *
 * This fails because "long" is a future reserved word in ECMAScript
 * @method namespace
 * @static
 * @param  {String} ns The name of the namespace
 * @return {Object}    A reference to the namespace object
 */
YAHOO.namespace = function(ns) {

    if (!ns || !ns.length) {
        return null;
    }

    var levels = ns.split(".");
    var nsobj = YAHOO;

    // YAHOO is implied, so it is ignored if it is included
    for (var i=(levels[0] == "YAHOO") ? 1 : 0; i<levels.length; ++i) {
        nsobj[levels[i]] = nsobj[levels[i]] || {};
        nsobj = nsobj[levels[i]];
    }

    return nsobj;
};

/**
 * Uses YAHOO.widget.Logger to output a log message, if the widget is available.
 *
 * @method log
 * @static
 * @param  {string}  sMsg       The message to log.
 * @param  {string}  sCategory  The log category for the message.  Default
 *                              categories are "info", "warn", "error", time".
 *                              Custom categories can be used as well. (opt)
 * @param  {string}  sSource    The source of the the message (opt)
 * @return {boolean}            True if the log operation was successful.
 */
YAHOO.log = function(sMsg, sCategory, sSource) {
    var l = YAHOO.widget.Logger;
    if(l && l.log) {
        return l.log(sMsg, sCategory, sSource);
    } else {
        return false;
    }
};

/**
 * Utility to set up the prototype, constructor and superclass properties to
 * support an inheritance strategy that can chain constructors and methods.
 *
 * @method extend
 * @static
 * @param {function} subclass   the object to modify
 * @param {function} superclass the object to inherit
 */
YAHOO.extend = function(subclass, superclass) {
    var f = function() {};
    f.prototype = superclass.prototype;
    subclass.prototype = new f();
    subclass.prototype.constructor = subclass;
    subclass.superclass = superclass.prototype;
    if (superclass.prototype.constructor == Object.prototype.constructor) {
        superclass.prototype.constructor = superclass;
    }
};

YAHOO.namespace("util");
YAHOO.namespace("widget");
YAHOO.namespace("example");


YAHOO.namespace("loan_sim");

/*
function init(){
	YAHOO.loan_sim.panel = new YAHOO.widget.Panel("win", {
	width:"350px", 
	fixedcenter: true, 
	constraintoviewport: true, 
	underlay:"shadow", 
	close:true, 
	visible:false, 
	draggable:true,
	effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.25}
	} );
	YAHOO.loan_sim.panel.render();
}

YAHOO.util.Event.addListener(window, "load", init);
*/

//全角数字配列
var char1 = new Array("１","２","３","４","５","６","７","８","９","０");
//半角数字配列
var char2 = new Array(1,2,3,4,5,6,7,8,9,0);

function keisan_start(){
	var kari = chk_value(document.form.kari.value,50,99999);
	var bonus = chk_value(document.form.bonus.value,0,99999);
	var kikan = chk_value(document.form.kikan.value,3,35);
	
	var kinri = document.form.kinri.value - 0;
	
	if(isNaN(kinri)){
		kinri="err1";
	}

	if((kinri < 0.8)||(kinri > 7)){
		kinri="err2";
	}
	
	var chk_flg = 0
	if(kari=="err1"){
		document.getElementById("err_kari_1").style.display = "block";
		document.getElementById("err_kari_2").style.display = "none";
		chk_flg++;
	}else if(kari=="err2"){
		document.getElementById("err_kari_1").style.display = "none"
		document.getElementById("err_kari_2").style.display = "block";
		chk_flg++;
	}else{
		document.getElementById("err_kari_1").style.display = "none";
		document.getElementById("err_kari_2").style.display = "none";
	}
	if(bonus=="err1"){
		document.getElementById("err_bonus_1").style.display = "block";
		document.getElementById("err_bonus_2").style.display = "none";
		chk_flg++;
	}else if(bonus=="err2"){
		document.getElementById("err_bonus_1").style.display = "none";
		document.getElementById("err_bonus_2").style.display = "block";
		chk_flg++;
	}else{
		document.getElementById("err_bonus_1").style.display = "none";
		document.getElementById("err_bonus_2").style.display = "none";
	}
	if(kikan=="err1"){
		document.getElementById("err_kikan_1").style.display = "block";
		document.getElementById("err_kikan_2").style.display = "none";
		chk_flg++;
	}else if(kikan=="err2"){
		document.getElementById("err_kikan_1").style.display = "none";
		document.getElementById("err_kikan_2").style.display = "block";
		chk_flg++;
	}else{
		document.getElementById("err_kikan_1").style.display = "none";
		document.getElementById("err_kikan_2").style.display = "none";
	}
	
	if(kinri=="err1"){
		document.getElementById("err_kinri_1").style.display = "block";
		document.getElementById("err_kinri_2").style.display = "none";
		chk_flg++;
	}else if(kinri=="err2"){
		document.getElementById("err_kinri_1").style.display = "none";
		document.getElementById("err_kinri_2").style.display = "block";
		chk_flg++;
	}else{
		document.getElementById("err_kinri_1").style.display = "none";
		document.getElementById("err_kinri_2").style.display = "none";
	}
	
	if(chk_flg>0){
		document.getElementById("m_hensai").innerHTML = "<p>" + "-----" + "</p>";
		document.getElementById("b_hensai").innerHTML = "<p>" + "-----" + "</p>";
		return;
	}
	
	//計算処理
	var hensai = loan_keisan(kari,bonus,kikan,kinri);
	var b_kingaku = bonus_keisan(bonus,kikan,kinri);
	//var b_hensai = hensai + b_kingaku;
	
	//出力処理へ
	out_kekka(hensai,b_kingaku);
}

//毎月の返済額の計算
function loan_keisan(kari,bonus,kikan,kinri){
	w_kari = (kari*10000) - (bonus*10000);
	w_kinri = kinri / 100;
	w_kikan = kikan * 12;
	x = w_kari * (w_kinri / 12) * Math.pow((1 + (w_kinri / 12)),w_kikan);
	y = Math.pow((1 + (w_kinri / 12)),w_kikan) - 1;
	kingaku = Math.ceil(x / y);
	return(kingaku);
}

//ボーナス返済額の計算
function bonus_keisan(bonus,kikan,kinri){
	w_bonus = bonus * 10000;
	w_kinri = kinri / 100;
	w_kikan = kikan * 2;
	x = w_bonus * (w_kinri / 2) * Math.pow((1 + (w_kinri / 2)),w_kikan);
	y = Math.pow((1 + (w_kinri / 2)),w_kikan) - 1;
	kingaku = Math.ceil(x / y);
	return(kingaku);
}


//入力文字列のチェック
function chk_value(n,v_min,v_max){
	var count;
	//入力データに全角数字がある場合の処理
	while(n.match(/[０-９]/)){
		for(count = 0; count < char1.length; count++){
			//入力データを全角数字から半角数字に置換する
			n = n.replace(char1[count], char2[count]);
		}
	}
	//数字以外の文字である場合
	if(n.match(/[^0-9]+/)){   
		return("err1");
	}else{
		if(n<v_min || n>v_max){
			return("err2");//指定範囲を超える数値を入力している
		}
		return(n);//エラー無し
	}
}

//計算結果を画面表示
function out_kekka(kin,b_kin){
	document.getElementById("m_hensai").innerHTML = "<p>" + add_com(kin) + "<span>円</span></p>";
	document.getElementById("b_hensai").innerHTML = "<p>" + add_com(b_kin) + "<span>円</span></p>";
}

//カンマ追加処理
function add_com(n) {
	var str = String(n);
	var len = str.length - 1;
	var com = len % 3;
	var w_str = "";
	for(i=0; i < str.length; i++){
		w_str = w_str + str.substring(i,i+1);
		if(i==com && i<len){
			w_str = w_str + ",";
			com = com + 3;
		}
	}
	return(w_str);
}
