/*CRIOSSpinEdit 0.7 - advanced javascript SpinEdit visual control
Copyright (c) 2005-2007 Lucian SABO (luciansabo@gmail.com);
http://luci.criosweb.ro

Licensed under CreativeCommons LGPL http://creativecommons.org/licenses/LGPL/2.1/
For commercial use please contact me.
=================================
*/

function CriosSpinEdit(EditId)
{
//properties
this.EditId=EditId;
this.DefaultValue=0;
this.MinValue=0;
this.MaxValue=10;
this.Step=1;
this.Size=2;
this.Style='vert';
this.ReadOnly=false;

//public methods
CriosSpinEdit.prototype.up=up;
CriosSpinEdit.prototype.down=down;
CriosSpinEdit.prototype.WriteControl=WriteControl;
CriosSpinEdit.prototype.GetValue=GetValue;
CriosSpinEdit.prototype.SetValue=SetValue;
CriosSpinEdit.prototype.Validate=Validate;


//internal event handlers; there is not need to call them directly
CriosSpinEdit.prototype.HandleChange=HandleChange;

//private function members
var FixJSFloatBug=function FixJSFloatBug(n){return Math.round(n*1000)/1000;}

//events
CriosSpinEdit.prototype.OnChange=function OnChange(){};

//-----------------------------------------------------------------
function Validate(theValue)
{
if(theValue==='' || theValue==null || theValue>this.MaxValue || theValue<this.MinValue || isFinite(theValue)==0 || /*theValue%this.Step!=0*/ String(FixJSFloatBug(theValue/this.Step)).indexOf('.')!=-1) return false;
else {
	//prevent bug when user enters '07' for example in the edit field
	document.getElementById(this.EditId).value=FixJSFloatBug(theValue*1); //little trick to convert to numeric and prevent JS Floating point operation bugs
	return true;
	}
}
//-----------------------------------------------------------------
	function up()
	{
	if(this.n<this.MaxValue)
		{
		this.n=FixJSFloatBug(this.n+this.Step);
		document.getElementById(this.EditId).value=this.n;
		this.OnChange();
		}
	}
//-----------------------------------------------------------------	
	function down()
	{
	if(this.n>this.MinValue)
		{
		this.n=FixJSFloatBug(this.n-this.Step);
		document.getElementById(this.EditId).value=this.n;
		this.OnChange();
		}
	}
//-----------------------------------------------------------------
	function WriteControl(SpinName)
	{
	this.n=this.DefaultValue;
	var readonly_str='';
	if(this.ReadOnly)	readonly_str='readonly="readonly"';
	document.write('<table border="0" summary="" cellspacing="0" cellpadding="0">');
	document.write('<tr>');
if(this.Style=="vert")
{
		document.write('<td style="width:10px;height:24px;" rowspan="2"><input size="'+this.Size+'" id="'+this.EditId+'" name="'+this.EditId+'" type="text" value="'+this.DefaultValue+'" onchange="'+SpinName+'.HandleChange()" '+readonly_str+' /></td>');
		document.write('<td><input type="button" style="font-size:8px;color:black;width:16px;height:14px;vertical-align:bottom;padding:0px;" value="&#9650;" onclick="this.blur();'+SpinName+'.up()" /></td>');
	document.write('</tr>');
	document.write('<tr>');
		document.write('<td><input type="button" style="font-size:8px;color:black;width:16px;height:14px;vertical-align:top;padding:0px;" value="&#9660;" onclick="this.blur();'+SpinName+'.down()" /></td>');
}
else	//horizontal
{
		document.write('<td style="width:10px;height:24px;" rowspan="2"><input size="'+this.Size+'" id="'+this.EditId+'" name="'+this.EditId+'" type="text" value="'+this.DefaultValue+'"  onchange="'+SpinName+'.HandleChange()" '+readonly_str+' /></td>');
		document.write('<td><input type="button" style="font-size:11px;color:black;width:18px;height:24px;padding:0px;" value="&#9650;" onclick="this.blur();'+SpinName+'.up()" /></td>');
		document.write('<td><input type="button" style="font-size:11px;color:black;width:18px;height:24px;padding:0px;" value="&#9660;" onclick="this.blur();'+SpinName+'.down()" /></td>');
}
	document.write('</tr>');
	document.write('</table>');
	}
	
//-----------------------------------------------------------------
	//GetValue
	function GetValue()
	{
	return this.n;
	}
//-----------------------------------------------------------------
	//SetValue
	function SetValue(newValue)
	{
	//validation
	if(!this.Validate(newValue)) return false;
	
	this.n=newValue*1;
	document.getElementById(this.EditId).value=this.n;
	}	

//-----------------------------------------------------------------
function HandleChange()
{
document.getElementById(this.EditId).style.border='';
if(!this.Validate(document.getElementById(this.EditId).value)) 
	{
	document.getElementById(this.EditId).value=this.n;
	document.getElementById(this.EditId).style.border='1px solid red';
	document.getElementById(this.EditId).focus();	//set focus warn user
	return false;
	}
	else
		this.n=FixJSFloatBug(document.getElementById(this.EditId).value*1);	//little trick to convert to numeric and prevent JS Floating point operation bugs

this.OnChange();

	return true;
}

}

