﻿// JScript File

function formField(fieldId,errorMsg,validationType) {
    this.fId = fieldId;
    this.e = $(fieldId);
    this.value = this.e.value;
    this.errorMsg = errorMsg;
    this.validationType = validationType;
}

formField.prototype.hasValue = function() {
    if (this.value != "") {
        return true;
    }
    else {
        return false;
    }
}

formField.prototype.validEmail = function() {
    if (this.e.value.search(/^[a-zA-Z.0-9]+[\w\-]*[@]([a-zA-Z0-9\-]+\.)+[a-zA-Z]{2,5}$/) == -1) {
        return false;
    }
    else {
        return true;
    }
}

formField.prototype.validPhone = function() {
    if (this.e.value.search(/((\(\d{3}\) ?)|(\d{3}(-|.)))?\d{3}(-|.)\d{4}/) == -1) {
        return false;
    }
    else {
        return true;
    }
}

formField.prototype.isCheckedSingle = function() {
    if (this.e.checked) {
        return true;
    }
    else {
        return false;
    }
}

formField.prototype.validNumber = function() {
    if (this.e.value.search(/^[0-9]/) == -1) {
        return false;
    }
    else {
        return true;
    }
    
}

formField.prototype.minLength = function(length) {
    if (this.e.value.length < length) {
        return false;
    }
    else {
        return true;
    }
    
}

formField.prototype.maxLength = function(length) {
    if (this.e.value.length > length) {
        return false;
    }
    else {
        return true;
    }
    
}

formField.prototype.equalsLength = function(length) {
    if (this.e.value.length != length) {
        return false;
    }
    else {
        return true;
    }
    
}

formField.prototype.validate = function() {
    switch(this.validationType) {
        case "email":
            return this.validEmail();
            break;
           
        case "singlecheck":
            return this.isCheckedSingle();
            break;
            
        case "null":
            return this.hasValue();
            break;

        case "phone":
            return this.validPhone();
            break;
        
        case "number":
            return this.validNumber();
            break;
        
        case "shortZip":
            var validNumber = this.validNumber();
            var validLength = this.equalsLength(5);
            var valid = validNumber && validLength ? true : false;
            
            return valid;
            
            break;
        
        default:
            return this.hasValue();
            break;
    
    }
    
}