var continueSpin=false;
var currentTarget = new Object();
currentTarget.field=null;
currentTarget.spinner=null;

 /**
 @param targetField - The target field object.
 @param btnUp - The associated 'Up' button.
 @param btnDn - The associated 'Down' button.
 @param minValue - The minimum value.
 @param maxValue - The maximum value.
 @param oneToOne - Specifies if targetField is tied-up to only one set of Up/Down buttons or not. (boolean)
 @param btnContainerId - The id container (span) where the Up/Down buttons are located.
 @param interval - The interval between succeeding numbers. Optional. (default is 1)
 @param hasLeadingZero - Specifies if single digits must have leading zero or not. Optional. (default is false)
 **/
function NumberSpinner(targetField,btnUp,btnDn,minValue,maxValue,oneToOne,btnContainerId,interval,hasLeadingZero){
   var me = this;
   this.targetField = targetField;
   this.minValue = minValue;
   this.maxValue = maxValue;
   this.oneToOne = oneToOne;
   this.btnId = btnContainerId;
   this.interval = (typeof interval!='undefined' && interval!=null && interval>0) ? interval:1; //default is 1
   this.hasLeadingZero = (typeof hasLeadingZero!='undefined' && hasLeadingZero!=null) ? hasLeadingZero:false; //default is false
   
   //attach event handlers automatically
   targetField.onfocus = function(){setTarget(targetField,me)};
   targetField.onblur = function(){if (isNaN(parseInt(this.value,10))) this.value=me.minValue;}
   if (!btnUp.onmousedown) 
     btnUp.onmousedown = function(){
   	   if (me.oneToOne) setTarget(me.targetField,me);
   	   startSpin(1,me.btnId);
     }
   if (!btnUp.onmouseup) btnUp.onmouseup = stopSpin;
   if (!btnDn.onmousedown) 
     btnDn.onmousedown = function(){
   	   if (me.oneToOne) setTarget(me.targetField,me);
   	   startSpin(0,me.btnId);
     }
   if (!btnDn.onmouseup) btnDn.onmouseup = stopSpin;
   
} 

function setTime(mode,btnId){//mode:1=up, 0=down
    //stop spinning
    if (!continueSpin || !currentTarget.field || (currentTarget.spinner && currentTarget.spinner.btnId!=btnId)) return;

    //get current value
    var spinValue=parseInt(currentTarget.field.value,10);

    //set default value if not numeric
    if (isNaN(spinValue)) spinValue=currentTarget.spinner.minValue-currentTarget.spinner.interval;

    //get next value
    spinValue = (mode==0) ? spinValue-currentTarget.spinner.interval:spinValue+currentTarget.spinner.interval;

    //out of range?
    if (spinValue>currentTarget.spinner.maxValue) spinValue=currentTarget.spinner.maxValue; 
    else if (spinValue<currentTarget.spinner.minValue) spinValue=currentTarget.spinner.minValue;

    //set leading zero
    if (currentTarget.spinner.hasLeadingZero && spinValue<10) spinValue="0"+spinValue;

    //set value to target field
    currentTarget.field.value=spinValue;	
    currentTarget.field.select();
    currentTarget.field.focus();			

    //continue spinning
    setTimeout(function(){setTime(mode,btnId)},200);
}
 
function startSpin(mode, btnId){
   continueSpin = true;
   setTime(mode, btnId);
}
 
function stopSpin(){
   	continueSpin = false;
   	if (currentTarget.field) currentTarget.field.focus();
}
 
function setTarget(focusedObj, spinnerObj){
   currentTarget.field = focusedObj;
   currentTarget.spinner = spinnerObj;
   if (focusedObj) {
   	 focusedObj.select();
   }
}
 //NOTE: if you have other window.onfocus (or '<body onfocus' in HTML) handler anywhere, you must combine them
 window.onfocus=function(){setTarget(null,null);}

var spinnerDay;

//input,upBtn,dnbtn,minvalue,maxvalue,spanId,value
function initSpinner(targetField,btnUp,btnDn,minValue,maxValue,spanId,value){
   spinnerDay = new NumberSpinner(targetField,btnUp,btnDn,minValue,maxValue,true,spanId,value,false);
   
   //NOTE: if you have other document.onmousedown (or '<body onmousedown' in HTML) handler anywhere, you must combine them
   document.onmousedown=function(e){
     var targ = (e)?e.target:event.srcElement;
     if (currentTarget.field!=null && targ.onfocus && targ.onfocus.toString().indexOf('setTarget')==-1){
       setTarget(null,null);
     }
   }
}
function spinnerKeyDown(inputName,minValue,maxValue,value){
	if(event.keyCode==38){
		var addVel = inputName.value*1 + value*1;
		if(addVel<=maxValue)inputName.value=addVel;
	}
	if(event.keyCode==40){
		var minusVel = inputName.value*1 - value*1;
		if(minusVel>=minValue)inputName.value=minusVel;
	}
}