﻿/// <reference name="MicrosoftAjax.js"/>
Type.registerNamespace("TraderMagazine");

TraderMagazine.Error = function() {
    TraderMagazine.Error.initializeBase(this);
    //  global Error

    this.RegExp = new Object();
    this.RegExp.Integer = /^[0-9]+$/;
    this.RegExp.Credit = /^[0-9 ]+$/;
    this.RegExp.Float = /^[0-9.]+$/;
    this.RegExp.ExtendedName = /^[0-9A-Za-zßüÜäÄöÖèêèàáâòóôùúûìíî\ -.]+$/;
    this.RegExp.Name = /^[A-Za-zßüÜäÄöÖèêèàáâòóôùúûìíî\ -.]+$/;
    this.RegExp.AccountName = /^[a-zA-Z0-9 ]+$/;
    this.RegExp.Singleline = /^[0-9A-Za-zßüÜäÄöÖèêèàáâòóôùúûìíî!#$§%&()+,-.:=_?@´\/ ]+$/;
    this.RegExp.Plz = /^[a-zA-Z0-9 ]+$/;
    this.RegExp.Username = /^[0-9A-Za-z_?]+$/;
    this.RegExp.Credential = /^[0-9A-Za-z!#$§%()+,-=_?]+$/;
    this.RegExp.EMail = new RegExp('^([a-zA-Z0-9\\-\\.\\_]+)' + '(\\@)([a-zA-Z0-9\\-\\.]+)' + '(\\.)([a-zA-Z]{2,4})$');
    this.RegExp.MultiLineText = /^[0-9A-Za-zßüÜäÄöÖèêèàáâòóôùúûìíî!#$§%&()\/+,-.:=´_?@ \n\r]+$/;


}

TraderMagazine.Error.prototype = {
    initialize: function() {
        TraderMagazine.Error.callBaseMethod(this, 'initialize');




    },
    /// <summary>
    /// check inputs wether string ist to long or forbidden signs were used
    /// </summary>
    checkInput: function(string, l) {
        var success = true;
        if (string.length < 1 || string.length > l) {
            success = false;
        }
        else if (!string.match(this.RegExp.Singleline)) {
            success = false;
        }
        return success;
    },
    /// <summary>
    /// check string wether login properties were matched or not
    /// </summary>
    checkLogin: function(string) {
        var success = true;
        if (string.length > 20 || string.length < 5) {
            success = false;
        }
        else if (!string.match(this.RegExp.Credential)) {
            success = false;
        }
        return success;
    },
    /// <summary>
    /// check inputs wether string ist to long or forbidden signs were used
    /// </summary>
    checkInput: function(string, l) {
        var success = true;
        if (string.length < 1 || string.length > l) {
            success = false;
        }
        else if (!string.match(this.RegExp.Singleline)) {
            success = false;
        }
        return success;
    },
    /// <summary>
    /// check inputs wether string is empty or forbidden signs were used
    /// returns specific error message including forbidden signs when used
    /// </summary>
    checkField: function(_input, _error_id, _check, _length, _tooShortMsg) {
        var success = true;
        if (_input.length < _length) {
            this.showError(_error_id, _tooShortMsg);
            success = false;
        } else {
            var inputError = this.checkSignInput(_input, _check);
            if (inputError != "0") {
                this.showError(_error_id, inputError);
                success = false;
            }
        }
        if (success) {
            this.hideError(_error_id);
        }
        return success;
    },
    /// <summary>
    /// check inputs on forbidden signs 
    /// returns specific error message including forbidden signs when used
    /// </summary>
    checkFieldOnSigns: function(_input, _error_id, _check) {
        var success = true;
        var inputError = this.checkSignInput(_input, _check);
        if (inputError != "0") {
            this.showError(_error_id, inputError);
            success = false;
        }
        if (success) {
            this.hideError(_error_id);
        }
        return success;
    },
    /// <summary>
    /// display error in error field  under input field
    /// </summary>
    showError: function(id, errorMessage) {

        if ($get(id + "Cell") != null) $get(id + "Cell").style.height = "15px";
        if ($get(id) != null) {
           
            $get(id).style.visibility = 'visible';
            $get(id).innerHTML = "<div id='inner_err'>" + errorMessage + "</div>"; ;
            var errorHeight = Sys.UI.DomElement.getBounds($get('inner_err')).height;
            $get(id).style.height = (errorHeight + 2) + "px";
        }
    },
    /// <summary>
    /// hide error 
    /// </summary>
    hideError: function(_id) {
        if ($get(_id) != null) {
            $get(_id).innerHTML = "";
            $get(_id).style.visibility = 'hidden';
            $get(_id).style.height = "0px";
        }
        if ($get(_id + "Cell") != null) $get(_id + "Cell").style.height = "0px";
    },
    /// <summary>
    /// check string if forbidden signs were used 
    /// returns specific error message including the forbidden signs when used
    /// </summary>
    checkSignInput: function(string, c) {
        var error = "0";
        var forbiddenSigns = [];
        if (c == "int") var re = this.RegExp.Integer;
        if (c == "float") var re = this.RegExp.Float;
        if (c == "name") var re = this.RegExp.Name;
        if (c == "singleline") var re = this.RegExp.Singleline;
        if (c == "multiline") var re = this.RegExp.MultiLineText;
        if (c == "credential") var re = this.RegExp.Credential;
        if (c == "username") var re = this.RegExp.Username;
        if (c == "plz") var re = this.RegExp.Plz;
        if (c == "accountname") var re = this.RegExp.AccountName;
        if (c == "credit") var re = this.RegExp.Credit;
        for (var i = 0; i < string.length; i++) {
            var _sign = string;
            if (!string.charAt(i).match(re)) {
                if (!Array.contains(forbiddenSigns, string.charAt(i)))
                    Array.add(forbiddenSigns, string.charAt(i));
            }
        }
        if (forbiddenSigns.length > 0) {
            error = Text.errors.e30 + " ";
            for (var item in forbiddenSigns) {
                error += forbiddenSigns[item] + " ";
            }
        }

        return error;
    },
    /// <summary>
    /// check string if forbidden signs were used 
    /// </summary>
    checkEMail: function(s) {
        var a = false;
        var success = false;
        if (typeof (RegExp) == 'function') {
            var b = new RegExp('abc');
            if (b.test('abc') == true) { a = true; }
        }
        if (a == true) {
            reg = this.RegExp.EMail;
            success = (reg.test(s));
        }
        else {
            success = (s.search('@') >= 1 &&
             s.lastIndexOf('.') > s.search('@') &&
             s.lastIndexOf('.') >= s.length - 5)
        }
        return (success);
    },
    /// <summary>
    /// check string if forbidden signs were used 
    /// </summary>
    checkFinderInput: function(string) {
        var success = true;
        if (!string.match(this.RegExp.Singleline)) {
            success = false;
        }
        return success;
    },
    /// <summary>
    /// check string wether plz properties were matched or not
    /// </summary>
    checkPLZ: function(string) {
        var success = true;
        if (string.length > 10) {
            success = false;
        }
        else if (!string.match(this.RegExp.Plz)) {
            success = false;
        }
        return success;
    },


    dispose: function() {
        //Add custom dispose actions here
        TraderMagazine.Error.callBaseMethod(this, 'dispose');
    }
}
TraderMagazine.Error.registerClass('TraderMagazine.Error', Sys.Component);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();