﻿/*
 * FormValidator.js
 * Copyright (C) 2008
 * Author: Rutger Grootenhuis <rutger.grootenhuis@lightmaker.com>
 * Created: 27-11-2008
 */

function FormValidator(){
    this.valid = true;
    this.aspxPrefix = "";
}

FormValidator.prototype.validationStart = function() {
    this.valid = true;
}

FormValidator.prototype.validate = function(elementName, labeName) {
    return this.validateType("notEmpty", elementName, labeName);
}

FormValidator.prototype.validateEmail = function(elementName, labeName) {
    return this.validateType("email", elementName, labeName);
}

FormValidator.prototype.validateZipCode = function(elementName, labeName, overrideValue) {
    return this.validateType("postalcode", elementName, labeName, overrideValue);
}

FormValidator.prototype.validateTextOnly = function(elementName, labeName) {
    return this.validateType("textOnly", elementName, labeName);
}

FormValidator.prototype.validateIsNumeric = function(elementName, labeName) {
    return this.validateType("isNumeric", elementName, labeName);
}

FormValidator.prototype.validateType = function(type, elementName, labeName, overrideValue) {
    var valid = true;

    var element = document.getElementById(elementName);
    
    document.getElementById(labeName).style.display = "none";

    var string = element.value;
    if (overrideValue != "" && overrideValue != null)
        string = overrideValue;

    switch (type) {
        case "notEmpty":
            valid = this.isNotEmpty(string);
            break;
        case "textOnly":
            valid = this.isTextOnly(string);
            break;
        case "postalcode":
            valid = this.isValidZipCode(string);
            break;
        case "email":
            valid = this.isEmailValid(string);
            break;
        case "isNumeric":
            valid = this.isNumeric(string);
            break;
    }

    if (!valid) {
        document.getElementById(labeName).style.display = "block";
        this.valid = false;
        return false;
    }

    return true;
}

FormValidator.prototype.isNumeric = function(string) {
    if (string == null)
        return true;
        
    var stringSmall = string.replace(/^\s+/, '').replace(/\s+$/, '');
    if (stringSmall == "")
        return true;

    return string.match("^[0-9]+$");
}

FormValidator.prototype.isNotEmpty = function(string) {
    if (string == null)
        return false;

    var stringSmall = string.replace(/^\s+/, '').replace(/\s+$/, '');
    if (stringSmall == "")
        return false;

    return true;
}

FormValidator.prototype.isTextOnly = function(string) {
    if (string == null)
        return true;
        
    return string.match("^[a-zA-Z ]+$");
}

FormValidator.prototype.isValidZipCode = function(string) {
    if (string == null)
        return true;
        
    return string.match("^[1-9][0-9]{3}\s?[a-zA-Z]{2}$");
}

FormValidator.prototype.isEmailValid = function(string) {
    if (string == null)
        return true;
        
    var apos = string.indexOf("@");
    var dotpos = string.lastIndexOf(".");

    if (apos < 1 || dotpos - apos < 2)
        return false;

    return true;
}

FormValidator.prototype.validationComplete = function() {
    return this.valid;
}