﻿// Root Namespace
var aspdnsf = new function(){};

/********** Constants  Namespace **************/
aspdnsf.Constants = new function(){};

aspdnsf.Constants.EMPTY_STRING = '';
aspdnsf.Constants.VAT_SETTING_INCLUSIVE = 1;
aspdnsf.Constants.VAT_SETTING_EXCLUSIVE = 2;

/********** End Constants Namespace **************/

/********** Utils Namespace **************/

aspdnsf.Utils = new function(){};
aspdnsf.Utils.showRow = function(id) {
    var row = $(id);
    if(null != row) {
        row.style.display = "";
    }
};

aspdnsf.Utils.hideRow = function(id) {
    var row = $(id);
    if(null != row) {
        row.style.display = "none";
    }
};

/********** End Utils Namespace **************/


/********** Validators Namespace **************/

aspdnsf.Validators = new function(){};
aspdnsf.Validators.ValidationSummary = Class.create();
aspdnsf.Validators.ValidationSummary.prototype = {

    initialize : function(controlId) {
        this.focusControl = $(controlId + '_Focus');
        this.summaryControl = $(controlId + '_Board');
        this.errors = $(controlId + '_Board_Errors');
    },
    
    clear : function() {
        if(this.errors) {
            this.errors.innerHTML = '';
        }
    },
    
    display : function(message) {
        if(this.errors) {
            var li = document.createElement('li');
            li.innerHTML = message;
            this.errors.appendChild(li);
        }
    },
    
    focus : function() {
        if(this.focusControl) {
            this.focusControl.focus();
        }
    }
}

// Validators...
aspdnsf.Validators.BaseValidator = {
    
    setEvaluationDelegate : function(delegate) {
        this.evalDelegate = delegate;
    },
    
    shouldEvaluate : function() {
        var evaluate = true;
        
        if(undefined != this.evalDelegate && null != this.evalDelegate) {
            evaluate = this.evalDelegate(); // <- invoke
        }
        
        return evaluate;
    }
}

aspdnsf.Validators.RequiredFieldValidator = Class.create();
aspdnsf.Validators.RequiredFieldValidator.prototype = {

    initialize : function(controlId, errorMessage, next) {
        Object.extend(this, aspdnsf.Validators.BaseValidator);
        this.control = $(controlId);
        this.errorMessage = errorMessage;
        this.next = next;
        
        this.isValid = true;
    },
    
    validate : function() {
        this.isValid = true;
        if(this.control) {
            if(this.control.value == aspdnsf.Constants.EMPTY_STRING) {
                this.isValid = false;
            }
        }
    },
    
    toString : function() {
        return 'Required Validator:' + this.control.id;
    }
}

aspdnsf.Validators.CompareValidator = Class.create();
aspdnsf.Validators.CompareValidator.prototype = {
    
    initialize : function(controlId, compareWithControlId, errorMessage, next) {
        Object.extend(this, aspdnsf.Validators.BaseValidator);
        this.control = $(controlId);
        this.compareWithControl = $(compareWithControlId);
        this.errorMessage = errorMessage;
        this.next = next;
        
        this.isValid = true;
    },
    
    validate : function() {
        this.isValid = true;
        
        if(this.control && this.compareWithControl) {
            if(this.control.value != this.compareWithControl.value) {
                this.isValid = false;
            }
        }
    },
    
    toString : function() {
        return 'Compare Validator:' + this.control.id;
    }
}

aspdnsf.Validators.RegExValidator = Class.create();
aspdnsf.Validators.RegExValidator.prototype = {

    initialize : function(controlId, expression, errorMessage) {
        Object.extend(this, aspdnsf.Validators.BaseValidator);
        this.control = $(controlId);
        this.expression = expression;
        this.errorMessage = errorMessage;
        
        this.isValid = true;
    },
    
    validate : function() {
        this.isValid = true;
        
        if(this.control && this.expression && this.expression != aspdnsf.Constants.EMPTY_STRING) {
            var regEx = new RegExp(this.expression);
            this.isValid = regEx.test(this.control.value);
        }
    },
    
    toString : function() {
        return 'Reg Ex Validator:' + this.control.id;
    }
}

aspdnsf.Validators.InputLengthValidator = Class.create();
aspdnsf.Validators.InputLengthValidator.prototype = {

    initialize : function(controlId, minLength, maxLength, errorMessage, next) {
        Object.extend(this, aspdnsf.Validators.BaseValidator);
        this.control = $(controlId);
        this.minLength = minLength;
        this.maxLength = maxLength;
        this.errorMessage = errorMessage;
        this.next = next;
        
        this.isValid = true;
    },
    
    validate : function() { 
        this.isValid = true;
        
        if(this.control) {
            if(this.minLength) {
                
                if(this.control.value.length < this.minLength || this.control.value.length > this.maxLength) {
                    this.isValid = false;
                }
            }
            else {
                if(this.control.value.length > this.maxLength) {
                    this.isValid = false;
                }
            }
            
        }
    },
    
    toString : function() {
        return 'Input Length Validator:' + this.control.id;
    }

}

aspdnsf.Validators.DropDownListValidator = Class.create();
aspdnsf.Validators.DropDownListValidator.prototype = {

    initialize : function(controlId, invalidIndex, errorMessage, next) {
        Object.extend(this, aspdnsf.Validators.BaseValidator);
        this.control = $(controlId);
        this.invalidIndex = invalidIndex;
        this.errorMessage = errorMessage;
        this.next = next;
        
        this.isValid = true;
    },
    
    validate : function() {
        this.isValid = true;
        
        if(this.control) {
            this.isValid = (this.control.selectedIndex != this.invalidIndex);
        }
    },
    
    toString : function() {
        return 'Drop Down List Validator: ' + this.control.id;
    }

}

aspdnsf.Validators.ValidationController = Class.create();
aspdnsf.Validators.ValidationController.prototype = {
    
    initialize : function() {
        this.validators = new Array();
        this.validationSummary = null;
    },
    
    setValidationSummary : function(summary) {
        this.validationSummary = summary;
    },
    
    register : function(validator) {
        this.validators[this.validators.length] = validator;
    },
    
    clear : function() {
        this.validationSummary.clear();
    },
    
    validate : function(clear) {
        // we'll be running on the form context from this point on...
        var validators = this.validators;
        var summary = this.validationSummary;
        
        var anyNotValid = false;
        
        var validationRoutine = function(currentValidator) {
            if(null != currentValidator) {
                if(!currentValidator.shouldEvaluate()) return;
                
                currentValidator.validate();
                if(!currentValidator.isValid) {
                    anyNotValid = true;
                    summary.display(currentValidator.errorMessage);
                }
            }
        }
        
        if(clear) {
            this.clear();
        }
        
        for(var ctr=0; ctr<validators.length; ctr++) {
            var currentValidator = validators[ctr];
            validationRoutine(currentValidator);
            
            // chain
            while(null != currentValidator && currentValidator.isValid) {
                currentValidator = currentValidator.next;
                validationRoutine(currentValidator);
            }
        }
        
        if(anyNotValid) {
            summary.focus();
        }
        
        return !anyNotValid;
    }
    
}
/********** End Validators Namespace **************/

// Ensure Root Namespaces are existing..


/********** Pages Namespace **************/
aspdnsf.Pages = new function(){};
aspdnsf.Pages.BasePage = {
    
    loadControl : function(id, controller, setControlDelegate) {
        var control = controller.getControl(id);
        if(control) {
            setControlDelegate(control);
        }
        else {
            var observer = { 
            
                notify : function(control) {
                    if(control) {
                        if(control.id == id) {
                            setControlDelegate(control);
                        }
                    }
                }
            }
            
            controller.addObserver(observer);
        }
    },
    
    setForm : function(id) {
        this.form = $(id);
        
        if(this.onFormSet) {
            this.onFormSet();
        }
    }
    
}

/********** End Pages Namespace **************/

aspdnsf.StringResource = {
    
    initialize : function() {
        this.strings = new Hash();
    },

    registerString : function(key, value) {
        this.strings[key] = value;
    },
    
    getString : function(key) {
        if(this.strings[key]) {
            return this.strings[key];
        }
        else {
            return key;
        }
    }
    
}

aspdnsf.StringResource.initialize();
