if(document.all && !document.getElementById) {
    document.getElementById = function(id) {
         return document.all[id];
    }
}

function swc_TextBoxValidatorLabel(valClientID)
{
	this.clientID = valClientID;
		
	this.Clear = function ()
	{
		// Clear the Message Text
		control = document.getElementById(this.clientID);
		
		if ( control != null )
		{
		    control.innerHTML = "";
		}
	}
	
	this.AppendMessageEx = function (message, displayName)
	{
		var newMessage = displayName + ': ' + message;

		this.AppendMessage(newMessage);		
	}
	
	this.AppendMessage = function (message)
	{
	    var control = document.getElementById(this.clientID);
	    
	    if ( control != null )
	    {
    		var currentMessage = control.innerHTML;
	    	var newMessage = '';
		
    		if ( currentMessage.length > 0 )
	    	{
		    	newMessage = currentMessage + '<br>' + message;
    		}
	    	else
		    {
    			newMessage = message;
	    	}
		
    		control.innerHTML = newMessage;
		}
	}	
	
	this.ShowMessage = function ()
	{
	    var control = document.getElementById(this.clientID);
	    
	    if ( control != null )
	    {    
		    control.style.display = "block";
        }		    
	}
	
	this.HideMessage = function ()
	{
	    var control = document.getElementById(this.clientID);
	    
	    if ( control != null )
	    {
		    control.style.display = "none";
        }		    
	}
}

function swc_RequiredValidator(valRequired, valMessage)
{
	this.validateRequired = valRequired;
	
	this.message = valMessage;
	
	this.isValid = true;
	
	this.Validate = function (val)
	{
		this.isValid = true;
		
		if ( this.HasValidation() == true )
		{
			if ( this.validateRequired == true )
			{
				if ( val.toString().length <= 0 )
				{
					this.isValid = false;
				}
			}
		}
		
		return this.isValid;
	}
	
	this.HasValidation = function ()
	{
		return this.validateRequired;
	}
}

function swc_ComparerValidator(valExactMatch, valMatchType, valRequired, valIgnoreCase, valTextCompare, valMessage)
{
    this.exactMatch = valExactMatch;
    this.matchType = valMatchType;
    this.isRequired = valRequired;    
    this.compareToControlID = valTextCompare;
    this.message = valMessage;
    this.isValid = true;
    this.ignoreCase = valIgnoreCase;
    
    this.Validate = function (val)
    {
        this.isValid = true;
        
        if ( this.HasValidation() == true )
        {
            if ( this.isRequired == true )
            {
                var isMatch = false;
                
                if ( this.exactMatch == true )
                {
                    isMatch = this.ExactMatch(val, document.getElementById(this.compareToControlID).value);
                }
                else
                {
                    isMatch = this.PartialMatch(val, document.getElementById(this.compareToControlID).value);
                }
                  
                if ( this.matchType == 'DIFFERENT' )              
                {
                    this.isValid = !isMatch;
                }
                else 
                {
                    this.isValid = isMatch;
                }
            }
        }
        
        return this.isValid;
    }
    
    this.HasValidation = function ()
    {
        return this.isRequired;
    }
    
    this.ExactMatch = function(source1, source2)
    {
        if ( this.ignoreCase == true )
        {
            return swc_Trim(source1).toLowerCase() == swc_Trim(source2).toLowerCase();
        }
        else        
        {   return swc_Trim(source1) == swc_Trim(source2);
        }                
    }
    
    this.PartialMatch = function(source1, source2)
    {
        if ( this.ignoreCase == true )
        {
            return (swc_Trim(source2).toLowerCase().indexOf(swc_Trim(source1).toLowerCase()) >= 0 );
        }
        else        
        {
            return (swc_Trim(source2).indexOf(swc_Trim(source1)) >= 0 );
        }
    }
}

function swc_RangeValidator(valRequired, valLow, valHigh, valMessage)
{
	this.validateRange = valRequired;
	
	this.low = valLow;
	this.high = valHigh;
	this.message = valMessage;
	
	this.isValid = true;
	
	this.Validate = function (val)
	{
		this.isValid = true;	
		
		if ( this.HasValidation() == true)
		{
			if ( this.validateRange == true )
			{
			    this.isValid = true;
			}						
		}
		
		return this.isValid;
	}
	
	this.HasValidation = function ()
	{
		return this.validateRange;
	}
}

function swc_RegExValidator(valRequired, valExpression, valMessage, valMatchMode, valIgnoreCase)
{
	this.validateRegEx = valRequired;
    this.ignoreCase = valIgnoreCase;	
	this.expression = valExpression;
	this.message = valMessage;
	
	this.isValid = true;
	
	this.matchMode = valMatchMode;
	
	this.Validate = function (val)
	{
		this.isValid = true;
		
		if ( this.HasValidation() == true )
		{
			if ( this.validateRegEx == true )
			{
				this.isValid = swc_ValidateRegEx(val, this.expression, this.matchMode, this.ignoreCase);
			}	
		}
				
		return this.isValid;
	}
	
	this.HasValidation = function ()
	{
		return this.validateRegEx;
	}
}

function swc_TextBoxValidator(valClientID, valLabelID, valAppendInvalidValue, valErrorCss, valNormalCss, valDisplayName, valValidationGroup)
{
	this.clientID = valClientID;	
	this.isValid = true;
	this.appendInvalidValue = valAppendInvalidValue;
	this.errorCssClass = valErrorCss;
	this.normalCssClass = valNormalCss;
	this.displayName = valDisplayName;
	this.validationGroup = valValidationGroup;

    this.label = new swc_TextBoxValidatorLabel(valLabelID);	
	
	this.required = new swc_RequiredValidator(false, '');
	this.range = new swc_RangeValidator(false, 0,0,'');
	this.regEx = new swc_RegExValidator(false, '','');
	this.comparer = new swc_ComparerValidator(false, 'SAME', false, '', '', '');
	
	this.UpdateCssClass = function ()
	{
	    var control = document.getElementById(this.clientID);
	    
	    if ( control != null )
	    {
    		if ( this.isValid == true )
    		{
       		    control.className = this.normalCssClass;
		    }
    		else
	    	{
		    	control.className = this.errorCssClass;
    		}
		}
	}
	
	this.UpdateMessage = function ()
	{
	    var control = document.getElementById(this.clientID);
	    
	    if ( control != null && control.disabled != true && control.readOnly != true )
	    {
	        if ( !this.isValid )
	        {
	            this.label.ShowMessage();
	            
	            if ( !this.regEx.isValid && this.regEx.validateRegEx )
	            {
			        if ( this.displayName.length > 0 )
    			    {
	    			    this.label.AppendMessageEx(this.regEx.message, this.displayName);
		    	    }
			        else
    			    {
	    			    this.label.AppendMessage(this.regEx.message);		
		    	    }	            
                }
	            
	            if ( !this.range.isValid && this.range.validateRange )
	            {
    		        if ( this.displayName.length > 0 )
	    	        {
		                this.label.AppendMessageEx(this.required.message, this.displayName);
    		        }
	    	        else
		            {
			            this.label.AppendMessage(this.range.message);		
			        }
	            }
	            
	            if ( !this.required.isValid && this.required.validateRequired )
	            {
	    		    if ( this.displayName.length > 0 )
	        	    {
		                this.label.AppendMessageEx(this.required.message, this.displayName);
    		        }
	    	        else
		            {
			            this.label.AppendMessage(this.required.message);		
        			}
	            }
	            
	            if ( !this.comparer.isValid && this.comparer.isRequired )
	            {
                    if ( this.displayName.length > 0 )
	        	    {
		                this.label.AppendMessageEx(this.comparer.message, this.displayName);
    		        }
	    	        else
		            {
			            this.label.AppendMessage(this.comparer.message);		
        			}	                
	            }
	            
		        this.UpdateCssClass();		            
	        }
	    }
	}
	
	this.Validate = function () 
	{
		// Reset Page Validation
		this.isValid = true;
		
		// we only want to validate against controls that are enabled
		var control = document.getElementById(this.clientID);

		if ( control != null && control.disabled != true && control.readOnly != true )
		{
    		// Get the value of the control
	    	var value = swc_GetValue(this.clientID);

    		// Validate Required Validation
	    	if ( !this.required.Validate(value) )
		    {
    		    if ( this.displayName.length > 0 )
	    	    {
		            this.label.AppendMessageEx(this.required.message, this.displayName);
    		    }
	    	    else
		        {
			        this.label.AppendMessage(this.required.message);		
    			}
	    		    
		    	this.isValid = false;		
    		}
    		else if ( !this.range.Validate(value) ) // Validate Range
		    {
    		    if ( this.displayName.length > 0 )
	    	    {
		            this.label.AppendMessageEx(this.required.message, this.displayName);
    		    }
	    	    else
		        {
			        this.label.AppendMessage(this.range.message);		
			    }
			 
			    this.isValid = false;
		    }
		    else if ( !this.regEx.Validate(value) ) // Validate Reqular Expression
		    {
			    var msg = '';
			
    			if ( this.appendInvalidValue == true )
	    		{
		    		msg += value;
    			}
    
	    		msg += this.regEx.message;
			
			    if ( this.displayName.length > 0 )
    			{
	    			this.label.AppendMessageEx(msg, this.displayName);
		    	}
			    else
    			{
	    			this.label.AppendMessage(msg);		
		    	}
			
    			this.isValid = false;
	    	}	
	    	else if ( !this.comparer.Validate(value) ) // Validate Compare String
	    	{
			    var msg = '';
			
    			if ( this.appendInvalidValue == true )
	    		{
		    		msg += value;
    			}
    
	    		msg += this.comparer.message;
			
			    if ( this.displayName.length > 0 )
    			{
	    			this.label.AppendMessageEx(msg, this.displayName);
		    	}
			    else
    			{
	    			this.label.AppendMessage(msg);		
		    	}
			
    			this.isValid = false;
	    	}	
	    							
		    this.UpdateCssClass();				
		}
				
		return this.isValid;	
	}
}

function swc_Trim(s) {
    var m = s.match(/^\s*(\S+(\s+\S+)*)\s*$/);
    return (m == null) ? "" : m[1];
}

function swc_GetValue(id)
{
	var control = document.getElementById(id);
	
	if ( control != null )
	{
    	if ( typeof(control.value) == "string")
	    {
    		return control.value;
	    }	
	}
	
	return "";
}

function swc_ValidateRegEx(value, expression, matchMode, ignoreCase) 
{
	if ( swc_Trim(value).length == 0 )
		return true;

    var rx = new RegExp(expression);
    var matches = null;
    		
	if ( ignoreCase == true )
	{
	    rx = new RegExp(expression, "i");
	}
	 	
    var matches = rx.exec(value);
    
    if ( matchMode == 0 )
    {
        return (matches == null);
    }
    else
    {
        return (matches != null && value == matches[0]);
    }        
}

function swc_RangeValidatorEvaluateIsValid(val) {
    var value = swc_GetValue(val.controltovalidate);
    if (swc_Trim(value).length == 0) 
        return true;
    return (swc_Compare(value, val.minimumvalue, "GreaterThanEqual", val) &&
            swc_Compare(value, val.maximumvalue, "LessThanEqual", val));
}

function swc_Compare(operand1, operand2, operator, val) {
    var dataType = val.type;
    var op1, op2;
    if ((op1 = swc_ValidatorConvert(operand1, dataType, val)) == null)
        return false;    
    if (operator == "DataTypeCheck")
        return true;
    if ((op2 = swc_ValidatorConvert(operand2, dataType, val)) == null)
        return true;
    switch (operator) {
        case "NotEqual":
            return (op1 != op2);
        case "GreaterThan":
            return (op1 > op2);
        case "GreaterThanEqual":
            return (op1 >= op2);
        case "LessThan":
            return (op1 < op2);
        case "LessThanEqual":
            return (op1 <= op2);
        default:
            return (op1 == op2);            
    }
}


