/*
Copyright Aram [arnix.at@gmail.com]
December 2006
*/

// Config start
var errorColor = 'red';
var normalColor = '#999999';
// Config end


var checkInputIds = new Array();
var checkInputIdsTypes = new Array();
var checkInputIdsParams = new Array();
var varTypes = new Array("int", "str", "email", "manual");

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

function arraySearch(arr, s)
{
    var n = arr.length;
    var i;
    for (i = 0; i < n; i++)
    {
        if (arr[i] == s) return i+1;
    }
    return false;
}

function registerInput(id, varType, varParams)
{
    var varTypeIndex = arraySearch(varTypes, varType);
    if ((GEBI(id)) && (varTypeIndex != false))
    {
        varTypeIndex--;
        checkInputIds[checkInputIds.length] = id;
        checkInputIdsTypes[checkInputIdsTypes.length] = varTypeIndex;
        checkInputIdsParams[checkInputIdsParams.length] = varParams;
        return true;
    }
    else return false;
}

function checkInputs(canAlert)
{
    var errorMessages = '';
    var errorObjs = new Array();
    var n = checkInputIds.length;
    var i;
    var obj;
    var regexpString;
    var re;

    for (i = 0; i < checkInputIds.length; i++)
    {
        GEBI(checkInputIds[i]).style.borderColor = normalColor;
    }

    for(i = 0; i < n; i++)
    {
        obj = GEBI(checkInputIds[i]);
        switch (varTypes[checkInputIdsTypes[i]])
        {
            case "int":
                if ((checkInputIdsParams[i][0] != undefined) && (typeof(checkInputIdsParams[i][0]) == 'boolean'))
                {
                    if ((checkInputIdsParams[i][0] == true) && (obj.value == ''))
                    {
                        errorMessages += 'The "' + obj.title + '" field is required' + '\n';
                        errorObjs[errorObjs.length] = obj;
                    }
                }
                isReal = false;
                if ((checkInputIdsParams[i][3] == undefined) || (typeof(checkInputIdsParams[i][3]) != 'boolean'))
                    checkInputIdsParams[i][3] = false;
                if (obj.value != '')
                {
                    if ((checkInputIdsParams[i][1] != undefined) && (typeof(checkInputIdsParams[i][1]) == 'number'))
                        var minInt = checkInputIdsParams[i][1];
                    if ((checkInputIdsParams[i][2] != undefined) && (typeof(checkInputIdsParams[i][2]) == 'number'))
                        var maxInt = checkInputIdsParams[i][2];

                    if ((minInt != undefined) && (obj.value < minInt))
                    {
                        errorMessages += 'The minimum value of "' + obj.title + '" field is ' + minInt + '\n';
                        errorObjs[errorObjs.length] = obj;
                    }
                    if ((maxInt != undefined) && (obj.value > maxInt))
                    {
                        errorMessages += 'The maximum value of "' + obj.title + '" filed is ' + maxInt + '\n';
                        errorObjs[errorObjs.length] = obj;
                    }
                    if ((obj.value.match((checkInputIdsParams[i][3] == true) ? /^[0-9\-]{1}[0-9\.]*$/ : /^[0-9\-]{1}[0-9]*$/) == null))
                    {
                        errorMessages += 'The type of "' + obj.title + '" field must be ' + ((checkInputIdsParams[i][3] == true) ? 'Real ':' ')  + 'Number' + '\n';
                        errorObjs[errorObjs.length] = obj;
                    }
                }
                break;
            case "str":
                if (typeof(obj.value) == 'string')
                {
                    if ((checkInputIdsParams[i][0] != undefined) && (typeof(checkInputIdsParams[i][0]) == 'boolean'))
                    {
                        if ((checkInputIdsParams[i][0] == true) && (obj.value == ''))
                        {
                            errorMessages += 'The "' + obj.title + '" field is required' + '\n';
                            errorObjs[errorObjs.length] = obj;
                        }
                    }
                    if (obj.value != '')
                    {
                        if ((checkInputIdsParams[i][3] != undefined) && (typeof(checkInputIdsParams[i][3]) == 'object'))
                        {
                            regexpString = '[^';
                            if (arraySearch(checkInputIdsParams[i][3], "eng") != false) regexpString += 'A-Za-z';
                            if (arraySearch(checkInputIdsParams[i][3], "rus") != false) regexpString += '\\u0401\\u0410-\\u042F\\u0430-\\u0451';
                            if (arraySearch(checkInputIdsParams[i][3], "arm") != false) regexpString += '\\u0531-\\u0556\\u0561-\\u0587';
                            if (arraySearch(checkInputIdsParams[i][3], "numbers") != false) regexpString += '\\d';
                            if (arraySearch(checkInputIdsParams[i][3], "space") != false) regexpString += '\\x20';
                            if ((checkInputIdsParams[i][4] != undefined) && (typeof(checkInputIdsParams[i][4]) == 'string'))
                            {
                                regexpString += checkInputIdsParams[i][4];
                            }
                            regexpString += ']';
                            if (regexpString != '[^]')
                            {
                                re = new RegExp(regexpString);
                                if (re.test(obj.value) == true)
                                {
                                    errorMessages += 'There are some unallowed characters in the "' + obj.title + '" field' + '\n';
                                    errorObjs[errorObjs.length] = obj;
                                }
                            }
                        }
                        if ((checkInputIdsParams[i][1] != undefined) && (typeof(checkInputIdsParams[i][1]) == 'number'))
                            var minLength = checkInputIdsParams[i][1];
                        if ((checkInputIdsParams[i][2] != undefined) && (typeof(checkInputIdsParams[i][2]) == 'number'))
                            var maxLength = checkInputIdsParams[i][2];
                        if ((minLength != undefined) && (obj.value.length < minLength))
                        {
                            errorMessages += 'The minimum length of "' + obj.title + '" filed is ' + minLength + '\n';
                            errorObjs[errorObjs.length] = obj;
                        }
                        if ((maxLength != undefined) && (obj.value.length > maxLength))
                        {
                            errorMessages += 'The maximum length of "' + obj.title + '" field is ' + maxLength + '\n';
                            errorObjs[errorObjs.length] = obj;
                        }
                    }
                }
                else
                {
                    errorMessages += 'The type of "' + obj.title + '" field must be String' + '\n';
                    errorObjs[errorObjs.length] = obj;
                }
                break;
            case "email":
                if ((checkInputIdsParams[i][0] != undefined) && (typeof(checkInputIdsParams[i][0]) == 'boolean'))
                {
                    if ((checkInputIdsParams[i][0] == true) && (obj.value == ''))
                    {
                        errorMessages += 'The "' + obj.title + '" field is required' + '\n';
                        errorObjs[errorObjs.length] = obj;
                    }
                }
                if (obj.value != '')
                {
                    if ((checkInputIdsParams[i][1] != undefined) && (typeof(checkInputIdsParams[i][1]) == 'number'))
                        var minLength = checkInputIdsParams[i][1];
                    if ((checkInputIdsParams[i][2] != undefined) && (typeof(checkInputIdsParams[i][2]) == 'number'))
                        var maxLength = checkInputIdsParams[i][2];
                    if ((minLength != undefined) && (obj.value.length < minLength))
                    {
                        errorMessages += 'The minimum length of "' + obj.title + '" filed is ' + minLength + '\n';
                        errorObjs[errorObjs.length] = obj;
                    }
                    if ((maxLength != undefined) && (obj.value.length > maxLength))
                    {
                        errorMessages += 'The maximum length of "' + obj.title + '" field is ' + maxLength + '\n';
                        errorObjs[errorObjs.length] = obj;
                    }
                    if ((typeof(obj.value) != 'string') || (obj.value.match(/^[A-Za-z0-9\-\_\.]+@[A-Za-z0-9\-\_\.]+\.[A-Za-z]{2,4}$/) == null))
                    {
                        errorMessages += 'The type of "' + obj.title + '" field must be E-Mail' + '\n';
                        errorObjs[errorObjs.length] = obj;
                    }
                }
                break;
            case "manual":
                if ((checkInputIdsParams[i][0] != undefined) && (typeof(checkInputIdsParams[i][0]) == 'boolean'))
                {
                    if ((checkInputIdsParams[i][0] == true) && (obj.value == ''))
                    {
                        errorMessages += 'The "' + obj.title + '" field is required' + '\n';
                        errorObjs[errorObjs.length] = obj;
                    }
                }
                if (obj.value != '')
                {
                    if ((checkInputIdsParams[i][1] != undefined) && (typeof(checkInputIdsParams[i][1]) == 'string'))
                    {
                        regexpString = checkInputIdsParams[i][1];
                        re = new RegExp(regexpString);
                        if (re.test(obj.value) == false)
                        {
                            errorMessages += 'There are some unallowed characters in the "' + obj.title + '" field' + '\n';
                            errorObjs[errorObjs.length] = obj;
                        }
                    }
                }
                break;
        }
    }
    if (errorObjs.length > 0)
    {
        errorObjs[0].focus();
        for (i = 0; i < errorObjs.length; i++)
        {
            errorObjs[i].style.borderColor = errorColor;
        }
        if (canAlert == true)
            window.alert(errorMessages);
        return errorMessages;
    }
    return true;
}