﻿// JScript File

function BaseValidator(controlName, errorMessage) {
	this.controlName = controlName;
	this.errorMessage = errorMessage;
	
	this.init = function(controlName, errorMessage) {
		this.controlName = controlName;
		this.errorMessage = errorMessage;
	}
	
	this.trim = function(str) {
		if(str != null) {
			var newStr = str.replace(/^\s*/, '').replace(/\s*$/, ''); 
			if(newStr != '') {
				return newStr;
			}
		}
		
		return null;
	}
	
	this.displayErrorMessage = function() {
		alert(this.errorMessage);
	}
}



function UserNameValidator(controlName, nullMessage, invalidMessage) {
	this.init(controlName, nullMessage);
	this.nullMessage = nullMessage;
	this.invalidMessage = invalidMessage;
	
	
	this.isValid = function() {
		var control = document.getElementsByName(this.controlName);
		if(control[0].disabled) {
			return true;
		}
		
		var userName = this.trim(control[0].value);
		if(userName != null) {
		    var pattern = '^\\w+$';
		    var regex = new RegExp(pattern);
		    if(regex.test(userName)) {
		        return true;
		    } else {
		        this.errorMessage = this.invalidMessage;
		    }
		} else {
		    this.errorMessage = this.nullMessage;
		}

		control[0].focus();
		return false;
	}
}
UserNameValidator.prototype = new BaseValidator();
UserNameValidator.prototype.constructor = UserNameValidator;
UserNameValidator.superclass = BaseValidator.prototype;





function TextValidator(controlName, errorMessage) {
	this.init(controlName, errorMessage);
	
	this.isValid = function() {
		var control = document.getElementsByName(this.controlName);
		if(control[0].disabled) {
			return true;
		}
		
		if(this.trim(control[0].value) != null) {
			return true;
		}

		control[0].focus();
		return false;
	}
}
TextValidator.prototype = new BaseValidator();
TextValidator.prototype.constructor = TextValidator;
TextValidator.superclass = BaseValidator.prototype;


function EmailValidator(controlName, nullMessage, invalidMessage) {
	this.init(controlName, nullMessage);
	this.nullMessage = nullMessage;
	this.invalidMessage = invalidMessage
	
	this.isValid = function() {
		var control = document.getElementsByName(this.controlName);
		if(control[0].disabled) {
			return true;
		}
		
		if(control[0].value != null) {
		    var controlValue = this.trim(control[0].value);
			if(controlValue != null) {
			    if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(controlValue)) {
			        return true;
			    } else {
			        this.errorMessage = this.invalidMessage;
			    }
			    
			    /*
			    var pattern = '^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$';
			    
			    var regex = new RegExp(pattern);
			    if(regex.test(controlValue)) {
				    return true;
				} else {
				    this.errorMessage = this.invalidMessage;
				}
				*/
			}
		} else {
		    this.errorMessage = this.nullMessage;
		}

		control[0].focus();
		return false;
	}
}
EmailValidator.prototype = new BaseValidator();
EmailValidator.prototype.constructor = EmailValidator;
EmailValidator.superclass = BaseValidator.prototype;


function CheckboxValidator(controlName, errorMessage) {
    this.init(controlName, errorMessage);
    
    this.isValid = function() {
        var checkbox = document.getElementsByName(this.controlName);
        for(var i = 0; i < checkbox.length; i++) {
            if(checkbox[i].checked && (!checkbox[i].disabled)) {
                return true;
             }
        }
        
        return false;
    }
}
CheckboxValidator.prototype = new BaseValidator();
CheckboxValidator.prototype.constructor = CheckboxValidator;
CheckboxValidator.superclass = BaseValidator.prototype;





function RadioValidator(controlName, errorMessage) {
    this.init(controlName, errorMessage);
    
    this.isValid = function() {
        var checkbox = document.getElementsByName(this.controlName);
        for(var i = 0; i < checkbox.length; i++) {
            if(checkbox[i].checked && (!checkbox[i].disabled)) {
                return true;
             }
        }
        
        return false;
    }
    
    this.getValue = function() {
        var checkbox = document.getElementsByName(this.controlName);
        for(var i = 0; i < checkbox.length; i++) {
            if(checkbox[i].checked && (!checkbox[i].disabled)) {
                return checkbox[i].value;
             }
        }
        
        return '';
    }
}
RadioValidator.prototype = new BaseValidator();
RadioValidator.prototype.constructor = RadioValidator;
RadioValidator.superclass = BaseValidator.prototype;



function PasswordValidator(password1, password2, nullMessage, mismatchedMessage) {
	//this.init(null, nullMessage);
	this.nullMessage = nullMessage;
	this.mismatchedMessage = mismatchedMessage;
	this.password1 = password1;
	this.password2 = password2;
	
	this.isValid = function() {
	    var p1Control = document.getElementsByName(this.password1)[0];
	    var p2Control = document.getElementsByName(this.password2)[0];
	    
		var p1 = this.trim(p1Control.value);

		if(p1 == null) {
		    this.errorMessage = this.nullMessage;
		    p1Control.focus();
		} else {
		    var p2 = this.trim(p2Control.value);
		    if(p2 != null && p1 == p2) {
			    return true;
		    } else {
		        this.errorMessage = this.mismatchedMessage;
		        p2Control.focus();
		    }
		}
		
		return false;
	}
}
PasswordValidator.prototype = new BaseValidator();
PasswordValidator.prototype.constructor = PasswordValidator;
PasswordValidator.superclass = BaseValidator.prototype;



function FCKEditorValidator(controlName, errorMessage) {
	this.init(controlName, errorMessage);
	
	this.isValid = function() {
        var FCKeditor = FCKeditorAPI.GetInstance(this.controlName);
        if(FCKeditor != null) {
            if(this.trim(FCKeditor.GetXHTML()) != null) {
                return true;
            }
        } else {
            var control = document.getElementsByName(this.controlName);
		    if(control[0].disabled) {
			    return true;
		    }
    		
		    if(this.trim(control[0].value) != null) {
			    return true;
		    }

		    control[0].focus();
        }


		return false;
	}
}
FCKEditorValidator.prototype = new BaseValidator();
FCKEditorValidator.prototype.constructor = FCKEditorValidator;
FCKEditorValidator.superclass = BaseValidator.prototype;



function FormValidator() {
	this.validatorArray = new Array();
	
	this.addValidator = function(validator) {
		this.validatorArray.push(validator);
	}
	
	this.validate = function() {
		for(var i = 0; i < this.validatorArray.length; i++) {
			var obj = this.validatorArray[i];
			if(! obj.isValid()) {
				obj.displayErrorMessage();
				return false;
			}
		}
		return true;
	}
}
