// Num range
function RadNumericRangeMaskPart (lower, upper, leftAlign, zeroFill)
{
    this.upperLimit = upper;
    this.lowerLimit = lower;
    this.length = Math.max(this.lowerLimit.toString().length, this.upperLimit.toString().length);
    this.leftAlign = leftAlign;
    this.zeroFill = zeroFill;
    
    this.minusIncluded = this.lowerLimit < 0 || this.upperLimit < 0;

    this.value = lower; 
    this.FlipDirection = 0;
};

RadNumericRangeMaskPart.prototype = new RadMaskPart();

RadNumericRangeMaskPart.prototype.SetController = function (controller)
{
	this.controller = controller;
	this.GetVisValue();
}

RadNumericRangeMaskPart.prototype.IsCaseSensitive = function ()
{
	return true;
}


RadNumericRangeMaskPart.prototype.CanHandle = function (value, offset)
{
	if ((value == "-" || value == "+") && this.lowerLimit < 0) 
	{
		return true;
	}
	
	if (isNaN(parseInt(value)))
	{
		this.controller.OnChunkError(this, this.GetValue(), value);	
		return false;
	}
	return true;
};

RadNumericRangeMaskPart.prototype.InsertAt = function(value, offset)
{
	return this.visValue.substr(0, offset) + 
		value.toString() + 
		this.visValue.substr(offset + 1, this.visValue.length);
}

RadNumericRangeMaskPart.prototype.ReplacePromptChar = function(value)
{
	var blankReplace = this.leftAlign ? "" : "0";
    while (value.indexOf(this.PromptChar) > -1)
    {
		value = value.replace(this.PromptChar, blankReplace);
    }
    
    return value;
}

RadNumericRangeMaskPart.prototype.SetValue = function (value, offset)
{
	if (value == "")
	{
		value = 0;
	}
    if (isNaN(parseInt(value)) && value != "+" && value != "-")
    {
		return true;
    }
	offset -= this.offset;
    
    var intent = this.InsertAt(value, offset);
    intent = this.ReplacePromptChar(intent);
    
    // special case when the minus sign gets stuck between digits
    if (intent.indexOf('-') != - 1 && intent.indexOf('-') > 0)
    {
		intent = intent.replace('-', '0');
    }
    if (isNaN(parseInt(intent))) intent = 0;
    if (this.controller.RoundNumericRanges)
    {
		intent = Math.min(this.upperLimit, intent); 
		intent = Math.max(this.lowerLimit, intent); 
		this.setInternalValue(intent);
    } 
    else
    {
		if (
			intent <= this.upperLimit
			&& intent >= this.lowerLimit
		)
		{
			this.setInternalValue(intent);
			this.GetVisValue();
		}
		else
		{
			return false;
		}
    }
    
	this.GetVisValue();
	return true;    

};

RadNumericRangeMaskPart.prototype.setInternalValue = function (value)
{
    var oldValue = this.value;
    this.value = value;
    this.controller.OnEnumChanged(this, oldValue, value);
    if (oldValue > value)
    {
		this.controller.OnMoveDown(this, oldValue, value);
	}
	else
	{
		this.controller.OnMoveUp(this, oldValue, value);
	}
	this.FlipDirection = 0; // restore flip flag
}

RadNumericRangeMaskPart.prototype.GetVisValue = function ()
{
	
    var out = "";
    var stringValue = Math.abs(this.value).toString();
    if (this.leftAlign)
    {
		if (this.value < 0)
		{
			out +=  this.PromptChar;
		} 
        out += stringValue;
        while (out.length < this.length)
        {
            out += this.controller.PromptChar;
        }
    }
    else
    {
        var filler = this.zeroFill ? '0' : this.controller.PromptChar; 
		if (this.value < 0)
		{
			stringValue =  '-' + stringValue;
		} 
        while (out.length < this.length - stringValue.length)
        {
            out += filler;
        }
        
        out += stringValue;
    }
    this.visValue = out;
    return out;
};

RadNumericRangeMaskPart.prototype.GetLength = function ()
{
	return this.length;
};


RadNumericRangeMaskPart.prototype.HandleKey = function (e)
{
	this.controller.CalculateSelection();
	var eventWrap = new MaskedEventWrap(e, this.controller.field);
	if (eventWrap.IsDownArrow())
	{
		this.MoveDown();
		this.controller.FixSelection(eventWrap);
		return true;
	}
	else if (eventWrap.IsUpArrow())
	{
		this.MoveUp();
		this.controller.FixSelection(eventWrap);
		return true;
	}
};

RadNumericRangeMaskPart.prototype.MoveUp = function ()
{
	var currentValue = this.value;
	currentValue ++;
    if (currentValue > this.upperLimit)
    {
        currentValue = this.lowerLimit;
		this.FlipDirection = 1;
    }
    
    this.setInternalValue(currentValue);
    
	this.controller.Visualise();
}

RadNumericRangeMaskPart.prototype.MoveDown = function ()
{
	var currentValue = this.value;
	currentValue --;
    if (currentValue < this.lowerLimit)
    {
        currentValue = this.upperLimit;
		this.FlipDirection = -1;
    }
    
    
    this.setInternalValue(currentValue);
    
	this.controller.Visualise();
}


RadNumericRangeMaskPart.prototype.HandleWheel = function (e)
{
	var currentValue = this.value;
	currentValue = parseInt(currentValue) + parseInt(e.wheelDelta  / 120);
	var eventWrap = new MaskedEventWrap(e, this.controller.field);
	
	if (currentValue < this.lowerLimit)
	{
		currentValue = this.upperLimit - (this.lowerLimit - currentValue - 1);
		this.FlipDirection = -1;
	}
	
	if (currentValue > this.upperLimit)
	{
		currentValue = this.lowerLimit +  (currentValue - this.upperLimit - 1);
		this.FlipDirection = 1;
	}
	
	this.setInternalValue(currentValue);
	this.controller.Visualise();
	this.controller.FixSelection(eventWrap);
	return false;
};

//BEGIN_ATLAS_NOTIFY
if (typeof(Sys) != "undefined")
{
    if (Sys.Application != null && Sys.Application.notifyScriptLoaded != null)
    {
        Sys.Application.notifyScriptLoaded();
    }
}
//END_ATLAS_NOTIFY

