﻿
// source: http://www.jocys.com/
//=============================================================================
// Jocys.com JavaScript.NET Classes               (In C# Object Oriented Style)
// Created by Evaldas Jocys <evaldas@jocys.com>
//-----------------------------------------------------------------------------
// w[.w]@w[.w].[w]
//emailValidationPattern = new RegExp("^[A-Z0-9_%-]+(|([\.][A-Z0-9_%-]+)+)@[A-Z0-9_%-]+(|([\.][A-Z0-9_%-]+)+)$","i");
//strictEmailValidationPattern = new RegExp("^[A-Z0-9_%-]+(|([\.][A-Z0-9_%-]+)+)@[A-Z0-9_%-]+(|([\.][A-Z0-9_%-]+)+)[\.](([0-9]{1,3})|([A-Z]{2,3})|(aero|coop|info|museum|name))$","i");

//http://regexlib.com/UserPatterns.aspx?authorId=2c58598d-b6ac-4952-9a6a-bf2e6ae7dddc
var emailValidationRegexPattern = /^[\w-]+(\.[\w-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)*?\.[a-z]{2,6}|(\d{1,3}\.){3}\d{1,3})(:\d{4})?$/;


/* Generic user input object*/
function userInput(ctlID) {
    this.controlID = ctlID;
    this.control = document.getElementById(this.controlID);
    
    /// gets the type of control
    this["getControlType"] = function() {
        if (this.control != null)
            if (this.control.type)
                return this.control.type.toString();
        return null;
    }

    /// gets the value of control
    this["getControlValue"] = function() {
        var type = this.getControlType();
        if (type == null)
            return null;
        switch (type.toLowerCase()) {
            case "text":
            case "textarea":
            case "password":
                { return this.control.value; }
            case "checkbox":
                { return (this.control.checked) ? 1 : 0; }
            case "select-one":
                { return this.control.options[this.control.selectedIndex].value; }
            default: { break; }
        }
    }

    this["setFocus"] = function() {
    if (this.control.focus != null) {
        if (this.control.style["display"] != "none") {
            try { this.control.focus(); } catch (e) { }
            try { this.control.select(); } catch (e) { }
            }
        }
    }
}

/* Single Line input object inheriting Generic input */
function singleLineInput(ctlID) {
    ///<summary>singleLineInput</summary>
    ///<param>ctlID - control id</param>
    ///<returns>object representing current object</returns>
    userInput.call(this, ctlID);
    // method stating if control has value
    this["inputIsEmpty"] = function() {
        if (this.getControlValue() == null) return true;
        if (this.getControlValue().length == 0) return true;
        return this.getControlValue().replace(/^\s+|\s+$/g, '') == '';
    }

    /// returns true based on check type and regex match
    this["inputIsValidEmail"] = function(isOptionalEmail) {
    ///<summary>checks control value to be valid email address</summary>
    ///<param>isOptionalEmail - bool flag</param>
    ///<param>isStrict - bool flag</param>
    ///<returns>true if valid email, otherwise false</returns>    
        if (isOptionalEmail) {
            if (this.inputIsEmpty()) { return true; }
            else { return emailValidationRegexPattern.test(this.getControlValue().toString()); }
        }
        else {
            if (this.inputIsEmpty()) { return false; }
            else { return emailValidationRegexPattern.test(this.getControlValue().toString()); }
        }
    }
}

/* Multi Line input object inheriting Generic input */
function MultiLineInput(ctlID) {
    userInput.call(this, ctlID);
    this["inputIsEmpty"] = function() {
        if (this.getControlValue() == null) return true;
        if (this.getControlValue().length == 0) return true;
        return this.getControlValue().replace(/^\s+|\s+$/g, '') == '';
    }
}

/* Select input object inheriting Generic input */
function SelectInput(ctlID) {
    userInput.call(this, ctlID);
    this["selectionIsValid"] = function() {
        if (this.getControlValue() == null) return false;
        if (this.getControlValue().length == 0) return false;
        return (!(this.getControlValue().replace(/^\s+|\s+$/g, '') == ''));
    }
}

/* Check input object inheriting Generic input */
function CheckBoxInput(ctlID) {

}
