// ============================================================================
// Developed by Kernel Team.
// http://kernel-team.com
// ============================================================================

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

// Common functions for reuse =================================================

function stub() {
}

function commonGet(id) {
    return document.getElementById(id);
}

function commonValidId(id) {
    return (id && commonGet(id));
}

function commonShow(id) {
    if (commonValidId(id)) {
        commonGet(id).style.display = 'block';
    }
}

function commonHide(id) {
    if (commonValidId(id)) {
        commonGet(id).style.display = 'none';
    }
}

function commonGetRadioGroupValue(form, fieldName) {
    var group = form[fieldName];
    if (group.tagName == 'INPUT') {
        group = [group];
    }
    for (var i = 0; i < group.length; i++) {
        if (group[i].checked) {
            return group[i].value;
        }
    }
    return 0;
}

function commonProcessFieldError(fieldName, errorId) {
    for (var i = 0; i < 10; i++) {
        commonHide(fieldName + '_error_' + i);
    }
    commonShow(fieldName + '_' + errorId);
    return (errorId == null);
}

function commonValidateRequired(form, fieldName, errorCode) {
    if (!form[fieldName]) {
        return true;
    }
    if (form[fieldName].tagName.toLowerCase() == 'select') {
        if (form[fieldName].selectedIndex == 0) {
            return commonProcessFieldError(fieldName, errorCode);
        } else {
            return commonProcessFieldError(fieldName, null);
        }
    }
    if (form[fieldName].value.trim().length == 0) {
        return commonProcessFieldError(fieldName, errorCode);
    } else {
        return commonProcessFieldError(fieldName, null);
    }
}

function commonValidateMinLength(form, fieldName, length, errorCode) {
    if (!form[fieldName]) {
        return true;
    }
    if (form[fieldName].value.trim().length < length) {
        return commonProcessFieldError(fieldName, errorCode);
    } else {
        return commonProcessFieldError(fieldName, null);
    }
}

function commonValidateSymbols(form, fieldName, errorCode) {
    if (!form[fieldName]) {
        return true;
    }
    if (form[fieldName].value.trim().length == 0) {
        return commonProcessFieldError(fieldName, null);
    }
    var symbols = /^[0-9A-Za-z._\-@]+$/;
    if (!symbols.test(form[fieldName].value)) {
        return commonProcessFieldError(fieldName, errorCode);
    } else {
        return commonProcessFieldError(fieldName, null);
    }
}

function commonValidatePasswords(form, fieldName1, fieldName2, errorCode) {
    if (!form[fieldName1] || !form[fieldName2]) {
        return true;
    }
    if (form[fieldName1].value != form[fieldName2].value) {
        return commonProcessFieldError(fieldName2, errorCode);
    } else {
        return commonProcessFieldError(fieldName2, null);
    }
}

function commonValidateEmail(form, fieldName, errorCode) {
    if (!form[fieldName]) {
        return true;
    }
    if (form[fieldName].value.trim().length == 0) {
        return commonProcessFieldError(fieldName, null);
    }
    var email = /^\w[\w\-]*(\.\w[\w\-]*)*@\w[\w\-]+(\.\w[\w\-]+)*\.(a[c-gil-oq-uwz]|b[a-bd-jm-or-tvwyz]|c[acdf-ik-orsuvx-z]|d[ejkmoz]|e[ceghr-u]|f[i-kmorx]|g[abd-ilmnp-uwy]|h[kmnrtu]|i[delm-oq-t]|j[emop]|k[eg-imnprwyz]|l[a-cikr-vy]|m[acdghk-z]|n[ace-giloprtuz]|om|p[ae-hk-nrtwy]|qa|r[eouw]|s[a-eg-ort-vyz]|t[cdf-hjkm-prtvwz]|u[agkmsyz]|v[aceginu]|w[fs]|y[etu]|z[admrw]|com|edu|net|org|mil|gov|biz|pro|aero|coop|info|name|int|museum)$/;
    if (!email.test(form[fieldName].value)) {
        return commonProcessFieldError(fieldName, errorCode);
    } else {
        return commonProcessFieldError(fieldName, null);
    }
}

// Invite friend block functions ==============================================

function inviteFriendEnableForm(params) {
    var form = params['form_id'];

    if (commonValidId(form)) {
        commonGet(form).onsubmit = function() {
            var errorField = null;
            if (!commonValidateRequired(this, 'email', 'error_1')) {
                errorField = (errorField ? errorField : 'email');
            } else if (!commonValidateEmail(this, 'email', 'error_2')) {
                errorField = (errorField ? errorField : 'email');
            }
            if (!commonValidateRequired(this, 'message', 'error_1')) {
                errorField = (errorField ? errorField : 'message');
            }
            if (!commonValidateRequired(this, 'code', 'error_1')) {
                errorField = (errorField ? errorField : 'code');
            }
            if (errorField) {
                this[errorField].focus();
            }
            return !errorField;
        }
    }
}

