/* form validation object */

function Validation() {
    this.error = false;
    this.page = this.startString;
    this.message = this.startString;
}

Validation.prototype.startString = '<ul>';
Validation.prototype.endString = '</ul><p>';
Validation.prototype.preString = '<li style="color: red; font-size: 12pt; font-weight: bold;">';
Validation.prototype.postString = '</li>';
Validation.prototype.preMsgString = '<li style="font-size: 12pt; font-weight: bold;">';
Validation.prototype.postMsgString = '</li>';

Validation.prototype.reset = function() {
    this.error = false;
    this.page = this.startString;
    this.message = this.startString;
};


Validation.prototype.formatError = function(text) {
    return this.startString + this.preString + text + this.postString + this.endString;
};

Validation.prototype.formatMessage = function(text) {
    return this.startString + this.preMsgString + text + this.postMsgString + this.endString;
};

Validation.prototype.isValid = function() {
    return !this.error;
};

Validation.prototype.addError = function(text) {
    this.error = true;
    this.page += this.preString + text + this.postString;
};

Validation.prototype.addMessage = function(text) {
    this.message += this.preMsgString + text + this.postMsgString;
};

Validation.prototype.endValidation = function() {
    this.page += this.endString;
    this.message += this.endString;
    return !this.error;
};

Validation.prototype.getErrorText = function() {
    return this.error ? this.page : '';
};

Validation.prototype.getMessageText = function() {
    return this.message;
};

Validation.prototype.isNotEmpty = function(fieldValue, fieldName, text) {
    if (fieldValue == null || fieldValue.length == 0) {
        text = text || "A value is required for field '" + fieldName + "'";
        this.addError(text);
        return false;
    }
    return true;
};

Validation.prototype.isNonBlank = function(fieldValue, fieldName, text) {
    text = text || "A non-blank value is required for field '" + fieldName + "'";
    if (!this.isNotEmpty(fieldValue, fieldName, text))
        return false;
    var l = fieldValue.length;
    for (var i = 0; i < l; ++i) {
        if (fieldValue.charAt(i) != " ")
            return true;
    }
    this.addError(text);
    return false;
};

Validation.prototype.isPositiveNumber = function(fieldValue, fieldName, text) {
    text = text || "A positive number is required for field '" + fieldName + "'";
    if (!this.isNotEmpty(fieldValue, fieldName, text))
        return false;
	var seenNonZero = false;
    for (var i = 0; i < fieldValue.length; i++) {
        var oneChar = fieldValue.charAt(i);
        if (oneChar < "0" || oneChar > "9") {
            this.addError(text);
            return false;
        }
		else if (oneChar != "0")
			seenNonZero = true;
    }
	if (!seenNonZero) {
        this.addError(text);
        return false;
	}
    return true;
};

Validation.prototype.isNumber = function(fieldValue, fieldName, text) {
    text = text || "A number is required for field '" + fieldName + "'";
    if (!this.isNotEmpty(fieldValue, fieldName, text))
        return false;
    for (var i = 0; i < fieldValue.length; i++) {
        var oneChar = fieldValue.charAt(i);
        if (i == 0 && oneChar == '-')
            continue;
        if (oneChar < "0" || oneChar > "9") {
            this.addError(text);
		    return false;
        }
    }
    return true;
};

Validation.prototype.isInRange = function(fieldValue, fieldName, min, max, text) {
    text = text || "Field '" + fieldName + "' must be in the range " + min + " to " + max;
    if (!this.isNumber(fieldValue, fieldName, text))
        return false;
    var val = parseInt(fieldValue);
    if (val < min || val > max) {
        this.addError(text);
        return false;
    }
    return true;
};



Validation.prototype.isValidEmailAddress = function(fieldValue, fieldName, text) {
    text = text || "Field '" + fieldName + "' must be a valid email address";
    if (!this.isNotEmpty(fieldValue, fieldName, text))
        return false;
    var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)|\\s");
    var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
    if(r1.test(fieldValue) || !r2.test(fieldValue)) {
		this.addError(text);
		return false;
	}
	return true;
};


Validation.prototype.isValidDate = function(fieldValue, fieldName, text) {
    text = text || "Field '" + fieldName + "' must be a valid date";
    if (!this.isNotEmpty(fieldValue, fieldName, text))
        return null;
    var r = new RegExp("^([0-9]{1,2})/([0-9]{1,2})/([0-9]{4})$");
    var result = r.exec(fieldValue);
    if (result == null) {
		this.addError(text);
		return null;
	}
    var mm = parseInt(result[1], 10);
    var dd = parseInt(result[2], 10);
    var yyyy = parseInt(result[3], 10);
    var date = new Date(yyyy, mm - 1, dd);
    if (date == null) {
        this.addError(text);
        return null;
    }
    var m = date.getMonth() + 1;
    var d = date.getDate();
    if (m != mm || d != dd) {
        this.addError(text);
        return null;
    }
	return date;
};

