function nfield_isIE() {
    return (document.all) ? true : false;
}

function nfield_isOpera() {
    return (navigator.userAgent.indexOf("Opera",0) != -1) ? true : false;
}

function nfield_isNN4() {
    return (document.layers) ? true : false;
}

function nfield_isNN6() {
    return (document.getElementById) ? true : false;
}

function nfield_isWindows() {
    return navigator.userAgent.match(/Win/);
}

function nfield_isMac() {
    return navigator.userAgent.match(/Mac|PPC/);
}

function nfield_isSafari() {
    return navigator.userAgent.match(/Safari/);
}

function nfield_getEvent() {
    if ( nfield_isIE() ) {
        return window.event;
    }
    else {
        for(var f=arguments.callee.caller;f!=null;f=f.caller){
            var e = f.arguments[0];
            if( e !== void 0 && typeof e == 'object' && e.toString() == '[object Event]' ) return e ;
            if( e !== void 0 && typeof e == 'object' && e.toString() == '[object KeyboardEvent]' ) return e ;
            if( f.caller == f ) break ;
        }
    }
    return null ;
}


// $B%$%Y%s%H(B $B%-!<$r2!$9(B
function nfield_form_nf_keyDown(obj) {
	return obj.fObj.onKeyDown();
}

// $B%$%Y%s%H(B $B%-!<$rN%$9(B
function nfield_form_nf_keyUp(obj) {
	return obj.fObj.onKeyUp();
}

// $B%$%Y%s%H(B $B%U%)!<%+%9(B
function nfield_form_nf_focus(obj) {
	return obj.fObj.onFocus();
}

// $B%$%Y%s%H(B $B%U%)!<%+%9(B
function nfield_form_nf_blur(obj) {
	return obj.fObj.onBlur();
}

// $B%$%Y%s%H(B $B%[%$!<%k$r2s$9(B
function nfield_form_nf_wheel(obj) {
	return obj.fObj.onWheel();
}

// $B%$%Y%s%H(B $B>e%\%?%s%/%j%C%/(B
function nfield_form_nf_up_click(obj) {
	return obj.fObj.onClickButtonUp();
}

// $B%$%Y%s%H(B $B2<%\%?%s%/%j%C%/(B
function nfield_form_nf_down_click(obj) {
	return obj.fObj.onClickButtonDown();
}

nfield_elem_id = 0;
function nfield_get_new_id() {
         nfield_elem_id++;
         return "nfield_" + nfield_elem_id;
}


function NField(arg) {
    //
    // $B=i4|2=(B
    //
    if (!arg) {
        arg = new Object();
     }
    // input $B%?%0$N(B id $BMWAG(B
    elm_id = arg.elm_id || nfield_get_new_id();
    this.elm_id = elm_id;
    // input $B%?%0$N(B name $BMWAG(B
    nf_name = arg.name || 'kosuu';
    // DocType $B$K1~$8$F??$^$?$O56$H$9$k(B
    standards = arg.std || 0;
    // $B>&IJ?tNL$N=i4|CM(B
    this.defaultValue = arg.def || 0;
    // $B>&IJ?tNL$N:G>.CM(B
    this.min = arg.min || 0;
    // $B>&IJ?tNL$N:GBgCM(B
    this.max = arg.max || 999;

    btnWidth = 5;
    nfWidth = 30;
    nfHeight = 19;
    btnMargin = '1px';
    if (standards) {
        if (!nfield_isOpera()) {
            nfWidth = 24;
            nfHeight = 13;
        }
    }
    else {
        if (nfield_isOpera()) {
            btnWidth = 12;
        }
        else if (nfield_isMac() && nfield_isIE()) {
            nfHeight = 17;
            btnWidth = 12;
        }
        else if (nfield_isWindows() && nfield_isIE()) {
            btnMargin = '4px 1px 1px 1px';
        }
    }

    // $B%F%-%9%H%(%j%"(B
    nfHtml = '<input type="text" id="' + elm_id + '" name="' + nf_name + '" value="' + this.defaultValue + '" style="vertical-align:middle; width:' + nfWidth + 'px; height:' + nfHeight + 'px;margin:0px; text-align:right;" onkeydown="return nfield_form_nf_keyDown(this);" onkeyup="return nfield_form_nf_keyUp(this);" onfocus="return nfield_form_nf_focus(this)" onmousewheel="return nfield_form_nf_wheel(this);" onblur="return nfield_form_nf_blur(this);">';

    // $B%\%?%sG[CV(B
    btnUpId = elm_id + '_btn_up';
    btnDownId = elm_id + '_btn_down';
    servername = location.host;
    protocol = location.protocol == 'https:' ? 'https:' : 'http:';
    img_plus  = protocol + '//' + servername + '/img/plus.gif';
    img_minus = protocol + '//' + servername + '/img/minus.gif';
    btnUpHtml = '<div id="' + btnUpId + '" style="display inline;cursor: pointer;width:' + btnWidth + 'px; background: #ffffff; margin: 0px; padding:1px 3px 1px 3px;border: 1px solid #000000;" onclick="return nfield_form_nf_up_click(this);"><img src="' + img_plus + '" style="display inline;border:0px; padding 0px; margin:0px;"></div>';
    btnDownHtml = '<div id="' + btnDownId + '" style="display inline;cursor: pointer;width:' + btnWidth + 'px; background: #ffffff; margin: 0px; padding:1px 3px 1px 3px;border: 1px solid #000000;" onclick="return nfield_form_nf_down_click(this);"><img src="' + img_minus + '" style="display inline;border:0px; padding 0px; margin:0px;"></div>';

    // $B=q$-9~$_(B
    document.write(
       '<table style="width: 10px; height: 20px; border-collapse:collapse; border-spacing: 0px 0px;border:0px; padding: 0px; margin: 0px;">' +
         '<tr style="border:0px;padding: 0px; margin: 0px;">' +
           '<td style="border:0px;padding: 0px; margin: 0px;">' +
              nfHtml +
           '</td>' +
           '<td style="border:0px;padding: 0px; margin: 0px;">' +
             '<table style="width: 10px; height: 20px; border-collapse:collapse; border-spacing: 0px 0px;border:0px; padding: 0px; margin:' + btnMargin + ';">' + 
               '<tr style="border:0px;padding: 0px; margin: 0px;">' +
                 '<td style="border:0px;padding: 0px; margin: 0px;">' + 
                   btnUpHtml + 
                 '</td>' +
               '</tr>' +
               '<tr style="border:0px;padding: 0px; margin: 0px;">' +
                 '<td style="border:0px;padding: 0px; margin: 0px;">' +
                   btnDownHtml +
                 '</td>' +
               '</tr>' +
             '</table>' +
           '</td>' +
         '</tr>' +
       '</table>'
       );

    // $B@_Dj(B
    this.elm       = document.getElementById(elm_id);
    this.elm.fObj = this;
    this.btnUpElm   = document.getElementById(btnUpId);
    this.btnDownElm = document.getElementById(btnDownId);
    this.btnUpElm.fObj = this;
    this.btnDownElm.fObj = this;



    this.fixVal = function(val) {
        val = parseInt(val, 10);
		if (isNaN(val)) {
			if (!isNaN(this.min)) return this.min;
			return 0;
		}
        if (!isNaN(this.min) && (val < this.min)) val = this.min;
        if (!isNaN(this.max) && (val > this.max)) val = this.max;
        return val;
    }
    this.setValue = function(val) {
        this._value = this.fixVal(val);
        this.elm.value = this._value;
    }
    this.updateByField  = function () {
        var oldVal = this.elm.value;
        if ( isNaN(oldVal) || (oldVal == '') || (oldVal.length == 0) ) {
            oldVal = this.min;
        }
        if ((this._value != oldVal)) {
            this.setValue(oldVal);
        }
        return (this._value != oldVal);
    }
	this.inc = function() {
		this.updateByField();
		this.setValue(this._value +1);
	}
	this.dec = function() {
		this.updateByField();
		this.setValue(this._value -1);
	}


    // $B%$%Y%s%H(B
	this.onFocus = function() {
		this.updateByField();
	}
	this.onBlur = function() {
		this.updateByField();
	}
    this.onKeyDown = function() {
        e = nfield_getEvent();
        keyCode = e.keyCode;

        if ((65 <= keyCode) && (keyCode <= 90)) return false;
		switch (keyCode) {
        case 109:
            this.dec();
            return false;
            break;
        case 107:
            this.inc();
            return false;
            break;
        case 40:
            this.dec();
            return false;
            break;
        case 38:
            this.inc();
            return false;
            break;
		}
		return true;
    }
	this.onKeyUp = function() {
			this.updateByField();
	}
	this.onClickButtonUp = function() {
		this.inc();
	}
	this.onClickButtonDown = function() {
		this.dec();
	}
	this.onWheel = function() {
		if (event.wheelDelta > 0) {
			this.inc();
		} else if (event.wheelDelta < 0) {
			this.dec();
		}
	}
}
