/**********************************************************
* validation.js
*
* Author:       Jamie Nordmeyer
* Created:      October 24, 2006
* Description:  Provides the client side functionality for
*               DefaultN.Web.Validation.Validator control.
**********************************************************/

// Shortcut function for document.getElementById;
var $;
if (document.getElementById)
    $ = function(id) {return document.getElementById(id);}
else if (document.all)
    $ = function(id) {return document.all[id];}
else if (document.layers)
    $ = function(id) {return document.layers[id];}
else
    alert('Cannot locate a suitable control retrieval function for $');
    
// String prototype additions
String.prototype.trim = function()
{
	return this.replace(/^\s*(\b.*\b|)\s*$/, "$1");
}

String.prototype.trimEnd = function()
{
    return this.replace(/(\b.*\b|)\s*$/, "$1");
}

String.prototype.trimStart = function()
{
    return this.replace(/^\s*(\b.*\b|)/, "$1");
}

String.prototype.isDate = function()
{
	//in mm/dd/yyyy format without leap year check
	//var re=/^((0?\d)|(1[0-2]))(\\|\/|-)((0?\d)|([1-2]\d)|(3[0-1]))(\\|\/|-)(\d{2}|\d{4})$/; 
    
    //in dd/mm/yyyy format with leap year check
	//message - 'Please enter valid date as month, day, and four digit year.\nYou may use a slash to separate the values.\nThe date must be a real date. 30/2/2000 would not be accepted.\nFormay dd/mm/yyyy.';	
    var re=/^((((0?[1-9]|[12]\d|3[01])[\/](0?[13578]|1[02])[\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\/](0?[13456789]|1[012])[\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\/]0?2[\/]((1[6-9]|[2-9]\d)?\d{2}))|(29[\/]0?2[\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$/;
	return re.test(this);
}

String.prototype.isTime = function()
{
	var re=/^(((0?[1-9]|1[0-2])(:|\.)[0-5]\d((:|\.)[0-5]\d)?( )?(([aA]|[pP])[mM]))|((0?\d|1\d|2[0-3])(:|\.)[0-5]\d((:|\.)[0-5]\d)?))$/;
	return re.test(this);
}

String.prototype.isDateTime = function()
{
	//in mm/dd/yyyy format without leap year check
	//var re=/^((0?\d)|(1[0-2]))(\\|\/|-)((0?\d)|([1-2]\d)|(3[0-1]))(\\|\/|-)(\d{2}|\d{4})( )(((0?[1-9]|1[0-2])(:|\.)[0-5]\d((:|\.)[0-5]\d)?( )?(([aA]|[pP])[mM]))|((0?\d|1\d|2[0-3])(:|\.)[0-5]\d((:|\.)[0-5]\d)?))$/;

    //in dd/mm/yyyy format with leap year check
	//message - 'Please enter valid date as month, day, and four digit year.\nYou may use a slash to separate the values.\nThe date must be a real date. 30/2/2000 would not be accepted.\nFormay dd/mm/yyyy.';	
	var re=/^((((0?[1-9]|[12]\d|3[01])[\/](0?[13578]|1[02])[\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\/](0?[13456789]|1[012])[\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\/]0?2[\/]((1[6-9]|[2-9]\d)?\d{2}))|(29[\/]0?2[\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))( )(((0?[1-9]|1[0-2])(:|\.)[0-5]\d((:|\.)[0-5]\d)?( )?(([aA]|[pP])[mM]))|((0?\d|1\d|2[0-3])(:|\.)[0-5]\d((:|\.)[0-5]\d)?))$/;
	
	if (this.isDate()) return true;
	if (this.isTime()) return true;
	if (re.test(this)) return true;
	return false;
}

String.prototype.isInteger = function()
{
    var re=/^\d+$/;
    return re.test(this);
}

String.prototype.isFloat = function()
{
    var re=/^\d+(\.\d+)?$/;
    return re.test(this);
}

String.prototype.toDateTime = function()
{
    if (!this.isDateTime())
        return null;
    
    return new Date(this);
}

String.prototype.toInteger = function()
{
    if (!this.isInteger())
        return null;
        
    return parseInt(this);
}

String.prototype.toFloat = function()
{
    if (!this.isFloat())
        return null;
        
    return parseFloat(this);
}

// Array prototype additions
Array.prototype.indexOf = function(val)
{
	for (var i=0; i<this.length; i++)
		if (this[i]==val) return i;
	return -1;
}

// Helper Objects
function EventArgs()
{
    this.isValid = true;
}
//This function is designed for Autocomplete extender TextBoxes.
//Hide  the ListControls in the Application.
function ListInvisible(lstBoxId)
{
    if($get(lstBoxId))
    {
      $get(lstBoxId).style.display = "none";
    }
}
function validator()
{
    this.version = "2.0";
    this.div = null;
    this.properties = null;
    this.conditions = null;
    
    // Main Validation Function
    this.validateConditions = function()
    {
        var valDiv = $(this.div);
        var errors = new Array();
        var errorSources = new Array();
        var validationSources = new Array();
        var eventArgs;
                
        for (var i = 0; i < this.conditions.length; i++)
        {
            var condition = this.conditions[i];
            var errorCtrl = null;

            var ctrl = $(condition.controlToValidate);
            if (ctrl == null || ctrl == 'undefined' || ((ctrl.disabled || ctrl.readOnly) && this.properties.ignoreDisabled == 'true'))
            {
                continue;
            }

            if(validationSources.indexOf(ctrl) == -1) { validationSources.push(ctrl); }
                    
            eventArgs = new EventArgs();
            eval(condition.clientSideFunction + '(condition, eventArgs);');
            
            if (condition.errorMessageTarget.length > 0)
            {
                errorCtrl = $(condition.errorMessageTarget);
                if (errorCtrl == null || errorCtrl == 'undefined')
                        alert(condition.errorMessageTarget + ' is not a known control.');
            }
            
            if (!eventArgs.isValid)
            {
                if (condition.showInSummary)
                {
                    errors.push(condition.errorMessage);
                    if(errorSources.indexOf(ctrl) == -1) { errorSources.push(ctrl); }
                }

                if(this.properties.displayStyle == 'Popup' && this.properties.enableClientScript == 'true')
                {
                    alert(condition.errorMessage);
                    var ctrl = $(condition.controlToValidate);
                    ctrl.focus();
                    return false;
                }
                
                if (errorCtrl != null && errorCtrl != 'undefined')
                {
                    if (errorCtrl.value)
                        errorCtrl.value = condition.errorMessage;
                    else
                        errorCtrl.innerHTML = condition.errorMessage;
                }
            }
            else
            {
                if (errorCtrl != null && errorCtrl != 'undefined')
                {
                    if (errorCtrl.value)
                        errorCtrl.value = '';
                    else
                        errorCtrl.innerHTML = '';
                }
            }
        }
        
        if (errors.length > 0)
        {
            this.displaySummary(errors, errorSources, validationSources);
            return false;
        }
        return true;
    };
    
    // Condition Validation Functions
    this.validateChangeRequiredCondition = function(condition, eventArgs)
    {
        var ctrl = $(condition.controlToValidate);
        if (ctrl == null || ctrl == 'undefined')
            alert('validateChangeRequiredCondition was passed an unknown control to validate.');
        
        // Are we validating a radio group?
        if (!ctrl.type && typeof(ctrl.firstChild) != 'undefined')
        {
            if (this.recurseRadioGroup(ctrl) == condition.initialValue)
                eventArgs.isValid = false;
        }
        else
        {
            if (ctrl.value.trim() == condition.initialValue)
                eventArgs.isValid = false;
        }
    };
    
    this.validateRegularExpressionCondition = function(condition, eventArgs)
    {
        var ctrl = $(condition.controlToValidate);
        if (ctrl == null || ctrl == 'undefined')
            alert('validateRegularExpressionCondition was passed an unknown control to validate.');
        
        // If the length is 0, we don't need to validate, as the 
        // ChangeRequiredCondition is used to force the existence of content.
        if (ctrl.value.length == 0) return;
        
        // Set up regular expression options.
        var reOptions = '';
        reOptions += condition.multiline == 'True' ? 'm' : '';
        reOptions += condition.caseInsensitive == 'True' ? 'i' : '';
        reOptions += condition.ignoreWhiteSpace == 'True' ? 'x' : '';
        
        // Create the RegExp object and test.
        var re = new RegExp(condition.regularExpression, reOptions);
        if(!re.test(ctrl.value))
        {
            eventArgs.isValid = false;
        }
    };
    
    this.validateRangeCondition = function(condition, eventArgs)
    {
        var ctrl = $(condition.controlToValidate);
        if (ctrl == null || ctrl == 'undefined')
            alert('validateRangeCondition was passed an unknown control to validate.');
        
        // If the length is 0, we don't need to validate, as the 
        // ChangeRequiredCondition is used to force the existence of content.
        if (ctrl.value.length == 0) return;
        
        switch(condition.dataType)
        {
            case 'String':
                if (condition.caseInsensitive == true)
                    eventArgs.isValid = condition.minimum.toUpperCase() <= ctrl.value.toUpperCase() && condition.maximum.toUpperCase() >= ctrl.value.toUpperCase();
                else
                    eventArgs.isValid = condition.minimum <= ctrl.value && condition.maximum >= ctrl.value;
                break;
            case 'Integer':
                if (!ctrl.value.isInteger())
                    eventArgs.isValid = false;
                else
                    eventArgs.isValid = condition.minimum.toInteger() <= ctrl.value.toInteger() && condition.maximum.toInteger() >= ctrl.value.toInteger();
                break;
            case 'Float':
                if (!ctrl.value.isFloat())
                    eventArgs.isValid = false;
                else
                    eventArgs.isValid = condition.minimum.toFloat() <= ctrl.value.toFloat() && condition.maximum.toFloat() >= ctrl.value.toFloat();
                break;
            case 'DateTime':
                if (!ctrl.value.isDateTime())
                    eventArgs.isValid = false;
                else
                    eventArgs.isValid = condition.minimum.toDateTime() <= ctrl.value.toDateTime() && condition.maximum.toDateTime() >= ctrl.value.toDateTime();
                break;
        }
    };    
    
    this.validateCompareCondition = function(condition, eventArgs)
    {
        var ctrl = $(condition.controlToValidate);
        
        if (ctrl == null || ctrl == 'undefined')
            alert('validateCompareCondition was passed an unknown control to validate.');
        
        // If the length is 0, we don't need to validate, as the 
        // ChangeRequiredCondition is used to force the existence of content.
        if (ctrl.value.length == 0) return;
        
        var ctrlToCompare = $(condition.controlToCompare);
        switch(condition.comparisonOperator)
        {
            case 'Equal':
                switch (condition.dataType)
                {
                    case 'String':
                        if (condition.caseInsensitive == 'True')
                            eventArgs.isValid = condition.compareToValue == 'True' ? ctrl.value.toUpperCase() == condition.valueToCompare.toUpperCase() : ctrl.value.toUpperCase() == ctrlToCompare.value.toUpperCase();
                        else
                            eventArgs.isValid = condition.compareToValue == 'True' ? ctrl.value == condition.valueToCompare : ctrl.value == ctrlToCompare.value;
                        break;
                        alert(eventArgs.isValid);
                    case 'Integer':
                        eventArgs.isValid = condition.compareToValue == 'True' ? ctrl.value.toInteger() == condition.valueToCompare.toInteger() : ctrl.value.toInteger() == ctrlToCompare.value.toInteger();
                        break;
                    case 'Float':
                        eventArgs.isValid = condition.compareToValue == 'True' ? ctrl.value.toFloat() == condition.valueToCompare.toFloat() : ctrl.value.toFloat() == ctrlToCompare.value.toFloat();
                        break;
                    case 'DateTime':
                        eventArgs.isValid = condition.compareToValue == 'True' ? ctrl.value.toDateTime() == condition.valueToCompare.toDateTime() : ctrl.value.toDateTime() == ctrlToCompare.value.toDateTime();
                        break;
                }
                break;
            case 'NotEqual':
                switch (condition.dataType)
                {
                    case 'String':
                        if (condition.caseInsensitive == 'True')
                            eventArgs.isValid = condition.compareToValue == 'True' ? ctrl.value.toUpperCase() != condition.valueToCompare.toUpperCase() : ctrl.value.toUpperCase() != ctrlToCompare.value.toUpperCase();
                        else
                            eventArgs.isValid = condition.compareToValue == 'True' ? ctrl.value != condition.valueToCompare : ctrl.value != ctrlToCompare.value;
                        break;
                    case 'Integer':
                        eventArgs.isValid = condition.compareToValue == 'True' ? ctrl.value.toInteger() != condition.valueToCompare.toInteger() : ctrl.value.toInteger() != ctrlToCompare.value.toInteger();
                        break;
                    case 'Float':
                        eventArgs.isValid = condition.compareToValue == 'True' ? ctrl.value.toFloat() != condition.valueToCompare.toFloat() : ctrl.value.toFloat() != ctrlToCompare.value.toFloat();
                        break;
                    case 'DateTime':
                        eventArgs.isValid = condition.compareToValue == 'True' ? ctrl.value.toDateTime() != condition.valueToCompare.toDateTime() : ctrl.value.toDateTime() != ctrlToCompare.value.toDateTime();
                        break;
                }
                break;
            case 'LessThan':
                switch (condition.dataType)
                {
                    case 'String':
                        if (condition.caseInsensitive == 'True')
                            eventArgs.isValid = condition.compareToValue == 'True' ? ctrl.value.toUpperCase() < condition.valueToCompare.toUpperCase() : ctrl.value.toUpperCase() < ctrlToCompare.value.toUpperCase();
                        else
                            eventArgs.isValid = condition.compareToValue == 'True' ? ctrl.value < condition.valueToCompare : ctrl.value < ctrlToCompare.value;
                        break;
                    case 'Integer':
                        eventArgs.isValid = condition.compareToValue == 'True' ? ctrl.value.toInteger() < condition.valueToCompare.toInteger() : ctrl.value.toInteger() < ctrlToCompare.value.toInteger();
                        break;
                    case 'Float':
                        eventArgs.isValid = condition.compareToValue == 'True' ? ctrl.value.toFloat() < condition.valueToCompare.toFloat() : ctrl.value.toFloat() < ctrlToCompare.value.toFloat();
                        break;
                    case 'DateTime':
                        eventArgs.isValid = condition.compareToValue == 'True' ? ctrl.value.toDateTime() < condition.valueToCompare.toDateTime() : ctrl.value.toDateTime() < ctrlToCompare.value.toDateTime();
                        break;
                }
                break;
            case 'LessThanOrEqual':
                switch (condition.dataType)
                {
                    case 'String':
                        if (condition.caseInsensitive == 'True')
                            eventArgs.isValid = condition.compareToValue == 'True' ? ctrl.value.toUpperCase() <= condition.valueToCompare.toUpperCase() : ctrl.value.toUpperCase() <= ctrlToCompare.value.toUpperCase();
                        else
                            eventArgs.isValid = condition.compareToValue == 'True' ? ctrl.value <= condition.valueToCompare : ctrl.value <= ctrlToCompare.value;
                        break;
                    case 'Integer':
                        eventArgs.isValid = condition.compareToValue == 'True' ? ctrl.value.toInteger() <= condition.valueToCompare.toInteger() : ctrl.value.toInteger() <= ctrlToCompare.value.toInteger();
                        break;
                    case 'Float':
                        eventArgs.isValid = condition.compareToValue == 'True' ? ctrl.value.toFloat() <= condition.valueToCompare.toFloat() : ctrl.value.toFloat() <= ctrlToCompare.value.toFloat();
                        break;
                    case 'DateTime':
                        eventArgs.isValid = condition.compareToValue == 'True' ? ctrl.value.toDateTime() <= condition.valueToCompare.toDateTime() : ctrl.value.toDateTime() <= ctrlToCompare.value.toDateTime();
                        break;
                }
                break;
            case 'GreaterThan':
                switch (condition.dataType)
                {
                    case 'String':
                        if (condition.caseInsensitive == 'True')
                            eventArgs.isValid = condition.compareToValue == 'True' ? ctrl.value.toUpperCase() > condition.valueToCompare.toUpperCase() : ctrl.value.toUpperCase() > ctrlToCompare.value.toUpperCase();
                        else
                            eventArgs.isValid = condition.compareToValue == 'True' ? ctrl.value > condition.valueToCompare : ctrl.value > ctrlToCompare.value;
                        break;
                    case 'Integer':
                        eventArgs.isValid = condition.compareToValue == 'True' ? ctrl.value.toInteger() > condition.valueToCompare.toInteger() : ctrl.value.toInteger() > ctrlToCompare.value.toInteger();
                        break;
                    case 'Float':
                        eventArgs.isValid = condition.compareToValue == 'True' ? ctrl.value.toFloat() > condition.valueToCompare.toFloat() : ctrl.value.toFloat() > ctrlToCompare.value.toFloat();
                        break;
                    case 'DateTime':
                        eventArgs.isValid = condition.compareToValue == 'True' ? ctrl.value.toDateTime() > condition.valueToCompare.toDateTime() : ctrl.value.toDateTime() > ctrlToCompare.value.toDateTime();
                        break;
                }
                break;
            case 'GreaterThanOrEqual':
                switch (condition.dataType)
                {
                    case 'String':
                        if (condition.caseInsensitive == 'True')
                            eventArgs.isValid = condition.compareToValue == 'True' ? ctrl.value.toUpperCase() >= condition.valueToCompare.toUpperCase() : ctrl.value.toUpperCase() >= ctrlToCompare.value.toUpperCase();
                        else
                            eventArgs.isValid = condition.compareToValue == 'True' ? ctrl.value >= condition.valueToCompare : ctrl.value >= ctrlToCompare.value;
                        break;
                    case 'Integer':
                        eventArgs.isValid = condition.compareToValue == 'True' ? ctrl.value.toInteger() >= condition.valueToCompare.toInteger() : ctrl.value.toInteger() >= ctrlToCompare.value.toInteger();
                        break;
                    case 'Float':
                        eventArgs.isValid = condition.compareToValue == 'True' ? ctrl.value.toFloat() >= condition.valueToCompare.toFloat() : ctrl.value.toFloat() >= ctrlToCompare.value.toFloat();
                        break;
                    case 'DateTime':
                        eventArgs.isValid = condition.compareToValue == 'True' ? ctrl.value.toDateTime() >= condition.valueToCompare.toDateTime() : ctrl.value.toDateTime() >= ctrlToCompare.value.toDateTime();
                        break;
                }
                break;
            case 'DataTypeCheck':
                switch (condition.dataType)
                {
                    case 'String':
                        eventArgs.isValid = true; 
                        break;
                    case 'Integer':
                        if (!ctrl.value.isInteger())
                            eventArgs.isValid = false;
                        else 
                            eventArgs.isValid = true;                        
                        break;
                    case 'Float':
                        if (!ctrl.value.isFloat())
                            eventArgs.isValid = false;
                        else 
                            eventArgs.isValid = true;
                        break;
                    case 'DateTime':
                        if (!ctrl.value.isDateTime())
                            eventArgs.isValid = false;
                        else 
                            eventArgs.isValid = true;  					            
                        break;
                }
                break;                
        }
    };
    
    this.validateModulusCondition = function(condition, eventArgs)
    {
        var ctrl = $(condition.controlToValidate);
        if (ctrl == null || ctrl == 'undefined')
            alert('validateModulusCondition was passed an unknown control to validate.');
        
        // If the length is 0, we don't need to validate, as the 
        // ChangeRequiredCondition is used to force the existence of content.
        if (ctrl.value.length == 0) return;

        if (!ctrl.value.isInteger())
            eventArgs.isValid = false;
        else
            eventArgs.isValid = ctrl.value.toInteger() % condition.modulus.toInteger() == 0;
    };
    
    this.validateCustomCondition = function(condition, eventArgs)
    {
        eventArgs.isValid = true;
    };
    
    this.validateAjaxCondition = function(condition, eventArgs)
    {
        this.processAjax(this.conditions.indexOf(condition), eventArgs);
    };
    
    // Helper Functions
    this.processAjax = function(conditionIndex, eventArgs)
    {
        var ajax = this.getAjaxObject();
        if (ajax == null)
        {
            alert('Could not instantiate as usable AJAX callback mechanism.');
            eventArgs.isValid = false;
            return;
        }
        
        var result = null;
        ajax.onreadystatechange = function()
        {
            if (ajax.readyState != 4) return;
            result = eval('(' + ajax.responseText + ')');
        }
        ajax.open('GET', this.properties.callbackPage + '?Validation_AJAX=true&conditionIndex=' + conditionIndex.toString() + this.getFormControls(), false);
        ajax.send();
        
        eventArgs.isValid = result;
    };
    
    this.getAjaxObject = function()
    {
        if (window.XMLHttpRequest)
            return new XMLHttpRequest();
        else
        {
		    if (window.XmlHttpRequestID)
			    return new ActiveXObject(window.XmlHttpRequestID);
		    else
		    {
			    var IDs = ['Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP'];
			    for (var i = 0; i < IDs.length; ++i)
			    {
				    var ID = IDs[i];
				    try
				    {
					    var requestObject = new ActiveXObject(ID);
					    window.XmlHttpRequestID = ID;
					    return requestObject;
				    }
				    catch(e)
				    {
				    }
			    }
		    }
	    }
	    return null;
    };
    
    this.getFormControls = function()
    {
        var form = $(this.properties.formID);
        var data = '';
        
        for (var i = 0; i < form.elements.length; i++)
        {
            var formElement = form.elements[i];
            var value = null;
            switch (formElement.nodeName.toLowerCase())
            {
                case 'input':
                    var type = formElement.getAttribute('type').toLowerCase();
                    if (type == 'text' || type == 'password' || type == 'hidden')
                        value = formElement.value;
                    else if (type == 'checkbox' || type == 'radio')
                        if (formElement.checked == true)
                            value = formElement.value;
                    break;
                case 'select':
                    if (formElement.multiple)
                    {
                        value = new Array();
                        for (var i = 0; i < formElement.options.length; i++)
                        {
                            if (formElement.options[i].selected)
                                value.push(formElement.options[i].value);
                        }
                    }
                    else
                        value = formElement.value;
                    break;
                case 'textarea':
                    value = formElement.value;
                    break;
            }
            
            if (value instanceof Array)
            {
                for (var i = 0; i < value.length; i++)
                    data += '&' + formElement.name + '=' + encodeURIComponent(value[i]);
            }
            else if (value != null)
                data += '&' + formElement.name + '=' + encodeURIComponent(value);
        }
        return data;
    };
    
    this.recurseRadioGroup = function(ctrl)
    {
        if (ctrl.type=="checkbox" && ctrl.checked==true)
		    return ctrl.checked;  
		            
        if (ctrl.type=="radio" && ctrl.checked==true)
		    return ctrl.value;
		    
        for (var i = 0; i < ctrl.childNodes.length; i++) 
        {
            var val = this.recurseRadioGroup(ctrl.childNodes[i]);
            if (val != '') return val;
        }
        return '';
    };
    
    this.displaySummary = function(errors, errorSources, validationSources)
    {
        var ctrl = $(this.div + '_errorStringsTarget');
        switch(this.properties.displayStyle)
        {
            case 'Summary':
                var addedCtrl;
                
                if (ctrl == null)
                    alert('Could not find the errorStringsTarget object.');
                
                while(ctrl.childNodes[0])
                    ctrl.removeChild(ctrl.childNodes[0]);
                for (var i = 0; i < errors.length; i++)
                {
                    var error = errors[i];
                    switch (this.properties.listStyle)
                    {
                        case 'LineBreak':
                            var br = document.createElement('br');
                            ctrl.appendChild(br);
                            var txt = document.createTextNode(error);
                            ctrl.appendChild(txt);
                            break;
                        case 'Paragraph':
                            var p = document.createElement('p');
                            ctrl.appendChild(p);
                            var txt = document.createTextNode(error);
                            ctrl.appendChild(txt);
                            break;
                        default:
                            var li = document.createElement('li');
                            var txt = document.createTextNode(error);
                            li.appendChild(txt);
                            ctrl.appendChild(li);
                            break;
                    }
                }
                
                $(this.div).style.display = 'block';             

                if(this.properties.highLightErrors == 'true')
                {
                    //To remove the backgroundColor for each control that caused validation failure during previous run
                    for (var j = 0; j < validationSources.length; j++)
                    {                
                        var validationSource = validationSources[j];
                        if (!validationSource.type && typeof(validationSource.firstChild) != 'undefined')
                        {
                            validationSource.style.backgroundColor = '#f3f3f3';  //set to default 
                        }         
                        else
                        {                   
                            validationSource.style.backgroundColor = '#ffffff'; //White color 
                        }   
                        
                        if(validationSource.disabled || validationSource.readOnly)                           
                        {
                            validationSource.style.backgroundColor = '#f3f3f3';  //set to default 
                        }
                    }                 
                    //To apply the backgorundColor for each control that caused validation failure
                    for (var k = 0; k < errorSources.length; k++)
                    {                
                        var errorSource = errorSources[k];
                        errorSource.style.backgroundColor = '#FFD0D0'; //Light red color
                    }   
                }                          
                break;
            case 'Alert':
                var message = this.properties.headerText + '\n\n';
                                
                for (var i = 0; i < errors.length - 1; i++)
                    message += '*  ' + errors[i] + '\n';
                if (errors.length > 0)
                    message += '*  ' + errors[errors.length - 1];
                    
                alert(message);
                break;
            case 'Popup':
                break;
        }
    };
}