﻿function DXMandatoryFormat( id )
{
	this.superClass = DXFormat;
	this.superClass( id );
}

DXMandatoryFormat.prototype = new DXFormat;

DXMandatoryFormat.prototype.Validate = function( value )
{
	if( value && value != "" )
		return new DXFormatValidationInfo( true );
	else
		return new DXFormatValidationInfo( false, this.GetErrorMessage( "Fältet måste fyllas i.", "The field must be specified." ) );
}

function DXLengthRestrictionFormat( id )
{
	this.superClass = DXFormat;
	this.superClass( id );
	this.MinLength = 0;
	this.MaxLength = 0;
}

DXLengthRestrictionFormat.prototype = new DXFormat;

DXLengthRestrictionFormat.prototype.Validate = function( value )
{
	var v = value ? value : "";
	var vLen = v.length;
	
	if( this.MinLength != 0 )
	{
		if( this.MaxLength != 0 )
		{
			if( vLen < this.MinLength || vLen > this.MaxLength )
			{
				if( this.MinLength == this.MaxLength )
				    return new DXFormatValidationInfo(false, this.GetErrorMessage("Fältet måste innehålla " + this.MinLength + " tecken.", "The field must contain " + this.MinLength + " characters."));
				else
				    return new DXFormatValidationInfo(false, this.GetErrorMessage("Fältet måste innehålla mellan " + this.MinLength + " och " + this.MaxLength + " tecken.", "The field must contain between " + this.MinLength + " and " + this.MaxLength + " characters."));
			}
		}
		else
		{
			if( vLen < this.MinLength )
			{
			    return new DXFormatValidationInfo(false, this.GetErrorMessage("Fältet måste innehålla minst " + this.MinLength + " tecken.", "The field must contain at least " + this.MinLength + " characters."));
			}
		}
	}
	else
	{
		if( this.MaxLength != 0 )
		{
			if( vLen > this.MaxLength )
			{
			    return new DXFormatValidationInfo(false, this.GetErrorMessage("Fältet får innehålla maximalt " + this.MaxLength + " tecken.", "The field must not contain more than " + this.MaxLength + " characters."));
			}
		}
	}
	return new DXFormatValidationInfo( true );
}

function DXSimpleFormat( id )
{
	this.superClass = DXFormat;
	this.superClass( id );
	this.Pattern = "";
}

DXSimpleFormat.prototype = new DXFormat;

DXSimpleFormat.prototype.Validate = function( value )
{
	if( this.IsValid( value ) )
		return new DXFormatValidationInfo( true );
	else
		return new DXFormatValidationInfo( false, this.Pattern );
}

DXSimpleFormat.prototype.IsValid = function(value) {
    var lv = value.length;
    var lp = this.Pattern.length;

    if (lv == 0)
        return true;
    if (lv != lp)
        return false;

    for (var i = 0; i < lv; i++) {
        var cv = value.charCodeAt(i);
        var cp = this.Pattern.charCodeAt(i);

        switch (cp) {
            case 48: // '0'
                // Check numeric
                if (cv >= 48 && cv <= 57)
                    break;
                else
                    return false;
                break;
            case 65:
                // Check alpha
                // TODO: Swedish chars only?
                if (cv >= 65 && cv <= 90 || cv >= 97 && cv <= 122 || cv >= 128 && cv <= 151 || cv >= 153 && cv <= 154 || cv >= 160 && cv <= 165)
                    break;
                else
                    return false;
            default:
                // Check exact
                if (cv != cp)
                    return false;
                break;
        }
    }
    return true;
}

// --

function DXRegexFormat( id )
{
	this.superClass = DXFormat;
	this.superClass( id );
	this.Pattern = "";
	this.ValidFormatText = "";
}

DXRegexFormat.prototype = new DXFormat;

DXRegexFormat.prototype.Validate = function( value )
{
	if( this.IsValid( value ) )
		return new DXFormatValidationInfo( true );
	else
		return new DXFormatValidationInfo( false, this.ValidFormatText );
}

DXRegexFormat.prototype.IsValid = function( value )
{
	var lv = value.length;
	if( lv == 0 )
		return true;

	var re = new RegExp( this.Pattern );
    return re.test( value );
}

// --

function DXFormatContainer( id )
{
	this.superClass = DXFormat;
	this.superClass( id );
	
	this.Formats = new Array();
}

DXFormatContainer.prototype = new DXFormat;

DXFormatContainer.prototype.AddFormat = function( format )
{
	this.Formats[ this.Formats.length ] = format;
	return format;
}


function DXFormatContainerOr( id )
{
	this.superClass = DXFormatContainer;
	this.superClass( id );
}

DXFormatContainerOr.prototype = new DXFormatContainer;

DXFormatContainerOr.prototype.Validate = function( value )
{
	if( this.Formats.length == 0)
		return new DXFormatValidationInfo( true );

    var totalInfo = new DXFormatValidationInfo(false, this.GetErrorMessage("Felaktigt värde, tillåtna format är:", "Invalid data, allowed formats are:"));

	for( var ff=0; ff<this.Formats.length; ff++ )
	{
		var format = this.Formats[ff];
		var info = format.Validate( value );
		if( info.Success )
		{
			return new DXFormatValidationInfo( true );
		}
		else
		{
			totalInfo.AddMessage( info.Message ); 
		}
	}
	return totalInfo;
}


function DXFormatContainerAnd( id )
{
	this.superClass = DXFormatContainer;
	this.superClass( id );
}

DXFormatContainerAnd.prototype = new DXFormatContainer;

DXFormatContainerAnd.prototype.Validate = function( value )
{
	for( var ff=0; ff<this.Formats.length; ff++ )
	{
		var format = this.Formats[ff];
		var info = format.Validate( value );
		if( !info.Success )
			return info;
	}
	return new DXFormatValidationInfo( true );
}



