// ============================================================================
// 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);
    }
}

function commonGetAjaxParams() {
    return 'mode=async&rand=' + new Date().getTime();
}

function commonSendRequest(url, data, isPost, callback) {
    var req = null;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject('Microsoft.XMLHTTP');
    }
    if (!req) {
        return null;
    }
    try {
        var postData = null;
        var method = null;
        if (isPost) {
            method = 'POST';
            postData = data;
        } else if (!data) {
            method = 'GET';
        } else if (data.length > 0) {
            method = 'GET';
            if (url.indexOf('?') >= 0) {
                url += '&' + data;
            } else {
                url += '?' + data;
            }
        }
        req.open(method, encodeURI(url), true);
        if (method == 'POST') {
            req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        }
        req.onreadystatechange = function() {
            if (req.readyState == 4) {
                var xml = req.responseXML;
                if (xml && xml.documentElement) {
                    if (xml.documentElement.nodeName == 'success') {
                        callback(xml.documentElement, null);
                    } else if (xml.documentElement.nodeName == 'failure') {
                        var childs = xml.documentElement.childNodes;
                        var errors = [];
                        for (var i = 0; i < childs.length; i++) {
                            if (childs[i].nodeName == 'error') {
                                var content = null;
                                if (childs[i].textContent) {
                                    content = childs[i].textContent;
                                } else if (childs[i].innerText) {
                                    content = childs[i].innerText;
                                } else if (childs[i].text) {
                                    content = childs[i].text;
                                }
                                errors.push(content);
                            }
                        }
                        callback(null, errors);
                    }
                }
            }
        };
        req.send(postData);
    } catch (e) {
        return e;
    }
    return 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;
        }
    }
}

// Feedback block functions ==============================================

function feedbackEnableForm(params) {
    var form = params['form_id'];
    var useCaptcha = params['use_captcha'];
    var useCustom1 = params['use_custom1'];
    var useCustom2 = params['use_custom2'];
    var useCustom3 = params['use_custom3'];

    if (commonValidId(form)) {
        commonGet(form).onsubmit = function() {
            var errorField = null;
            if (!commonValidateRequired(this, 'message', 'error_1')) {
                errorField = (errorField ? errorField : 'message');
            }
            if (useCustom1) {
                if (!commonValidateRequired(this, 'custom1', 'error_1')) {
                    errorField = (errorField ? errorField : 'custom1');
                }
            }
            if (useCustom2) {
                if (!commonValidateRequired(this, 'custom2', 'error_1')) {
                    errorField = (errorField ? errorField : 'custom2');
                }
            }
            if (useCustom3) {
                if (!commonValidateRequired(this, 'custom3', 'error_1')) {
                    errorField = (errorField ? errorField : 'custom3');
                }
            }
            if (useCaptcha) {
                if (!commonValidateRequired(this, 'code', 'error_1')) {
                    errorField = (errorField ? errorField : 'code');
                }
            }
            if (errorField) {
                this[errorField].focus();
            }
            return !errorField;
        }
    }
}

