ICON_EMPTY = 'img/ures.gif';
ICON_ERROR = 'img/error-small.gif';


if (!String.prototype.startsWith) {
    String.prototype.startsWith = function(prefix) {
        return this.match('^' + prefix) == prefix;
    }
}

if (!String.prototype.endsWith) {
    String.prototype.endsWith = function(postfix) {
        return this.match(postfix + '$') == postfix;
    }
}

if (!String.prototype.trim) {
    String.prototype.trim = function() {
        return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
    }
}

if (!Array.prototype.removeByIndex) {
    Array.prototype.removeByIndex = function(index) {
        this.splice(index, 1);
    }
}

if (!String.prototype.replaceAll) {
    String.prototype.replaceAll = function(search, replace) {
        return this.replace(new RegExp(search, 'g'),replace);
    }
}

if (!String.prototype.pad) {
    String.prototype.pad = function(l, s, t){
        return s || (s = " "), (l -= this.length) > 0 ? (s = new Array(Math.ceil(l / s.length)
            + 1).join(s)).substr(0, t = !t ? l : t == 1 ? 0 : Math.ceil(l / 2))
            + this + s.substr(0, l - t) : this;
    }
}

if (!String.prototype.wordWrap) {
    String.prototype.wordWrap = function(m, b, c){
        var i, j, s, r = this.split("\n");
        if(m > 0) for(i in r){
            for(s = r[i], r[i] = ""; s.length > m;
                j = c ? m : (j = s.substr(0, m).match(/\S*$/)).input.length - j[0].length || m,
                r[i] += s.substr(0, j) + ((s = s.substr(j)).length ? b : "")
            );
            r[i] += s;
        }
        return r.join("\n");
    };
}

if (!String.prototype.withoutAccents) {
    String.prototype.withoutAccents = function() {
        var s = '';
        for (var i = 0; i < this.length; i++) {
            var ch = this.charAt(i);
            switch (ch) {
                case '\u00F3': case '\u00F6': case '\u0151':s += 'o';break;
                case '\u00D3': case '\u00D6': case '\u0150':s += 'O';break;
                case '\u00FA': case '\u00FC': case '\u0171':s += 'u';break;
                case '\u00DA': case '\u00DC': case '\u0170':s += 'U';break;
                case '\u00E1':s += 'a';break;
                case '\u00C1':s += 'A';break;
                case '\u00E9':s += 'e';break;
                case '\u00C9':s += 'E';break;
                case '\u00ED':s += 'i';break;
                case '\u00CD':s += 'I';break;
                default:s += ch;break;
            }
        }
        return s;
    };
}


if (!window.addLoadEvent) {
    function addLoadEvent(func) {
        var oldonload = window.onload;

        if (typeof oldonload != 'function') {
            window.onload = func;
        } else {
            window.onload = function() {
                oldonload();
                func();
            }
        }
    }
 }

Util = {};
Util.getComboValue = function(combo) {
    return combo.options[combo.selectedIndex].value;
};

Util.getComboText = function(combo) {
    return combo.options[combo.selectedIndex].text;
};

Util.selectByValue = function(combo, value) {
    for (var i = 0; i < combo.options.length; i++) {
        if (combo.options[i].value == value) {
            combo.options[i].selected = true;
            break;
        }
    }
};

Util.postData = function(url, data) {
    var form = document.createElement('form');
    form.method = 'post';
    form.action = url;

    for (var name in data) {
        var field = document.createElement('input');
        field.setAttribute('type', 'hidden');
        field.setAttribute('name', name);
        field.setAttribute('value', data[name]);
        form.appendChild(field);
    }

    document.body.appendChild(form);
    form.submit();
    document.body.removeChild(form);
};


Util.validateDateTime = function(datetime) {
    var reg = /^\d{4}\/\d{1,2}\/\d{1,2}\s\d{1,2}\:\d{1,2}$/;
    return datetime.match(reg);
};

Util.validateEmail = function(email) {
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    return email.match(reg);
};

WIT = {};
WIT.Misc = {};


WIT.Misc.setError = function(id, title) {
    var img = document.getElementById(id + '_icon');

    if (img) {
        img.src = ICON_ERROR;
        img.title = title;
    }
};

WIT.Misc.removeError = function(id) {
    var img = document.getElementById(id + '_icon');

    if (img) {
        img.src = ICON_EMPTY;
        img.title = '';
    }
};


WIT.Misc.validateFields = function(fields) {
    var valid = true;

    for (var i = 0; i < fields.length; i++) {
        var field = fields[i];
        var o = {};
        o.failed = false;

        if (typeof field == 'object') {
            o.id = field.id;
            o.allowBlank = field.allowBlank;
            o.validator = (typeof field.validator == 'function') ? field.validator : null;
        } else if (typeof field == 'string') {
            o.id = field;
            o.allowBlank = false;
            o.validator = null;
        }

        var input = document.getElementById(o.id);

        if (o.allowBlank && input.value.trim() == '') {
            WIT.Misc.removeError(o.id);
            continue;
        }

        if (!o.allowBlank && input.value.trim() == '') {
            WIT.Misc.setError(o.id, 'K\u00F6telez\u0151en kit\u00F6ltend\u0151 mez\u0151');
            o.failed = true;
            valid = false;
        } else {
            WIT.Misc.removeError(o.id);
        }

        if (!o.failed && o.validator) {
            var error = o.validator(input.value);
            if (error !== true) {
                WIT.Misc.setError(o.id, error);
                o.failed = true;
                valid = false;
            } else {
                WIT.Misc.removeError(o.id);
            }
        }
    }

    return valid;
};

function szam_ellenorzese(szam) {
    if (isNaN(szam)) {
        return 'Nem sz\u00E1m';
    }

    if (szam < 0) {
        return 'Csak nemnegat\u00EDv sz\u00E1m lehet'
    }

    return true;
}

function datum_ellenorzese(datum) {
    if (!Util.validateDateTime(datum)) {
        return 'Rossz form\u00E1tum';
    }
    
    return true;
}

function email_ellenorzese(email) {
    if (!Util.validateEmail(email)) {
        return 'Rossz form\u00E1tum';
    }

    return true;
}


