function formsErrorHandler(arrErrors){
    // for (var i in arrErrors){ $(arrErrors[i].field).style.border = '1px solid #FF0000'; }
    showAlert(arrErrors[0].error);
    document.getElementById(arrErrors[0].field).focus();
}

function formsValidate(oForm){
    if (!oForms) { return false; }

    var i = 0, j = 0, k = 0, iCount = 0, jCount = 0, result = true, sParam = "", arrErrors = [], sField = "", arrValidator = [];

    // for (i in oForms){ $(oForms[i].field).style.border = ''; }
    for (i = 0, iCount = oForms.length; i < iCount; i ++){
        sField = oForms[i].field;
        if (oForms[i].validate && document.getElementById(sField)){
            arrValidator = oForms[i].validate;
            if (arrValidator.length > 0 &&
                (arrValidator.indexOf("necessary") != -1 ||
                    document.getElementById(sField) != 'undefined')){

                if (arrValidator.indexOf('|') != -1){
                    arrValidator = arrValidator.split("|");
                    arrErrorMessages = oForms[i].error.split("|");
                } else {
                    temp = arrValidator; arrValidator = []; arrValidator[0] = temp;
                    arrErrorMessages = []; arrErrorMessages[0] = oForms[i].error;
                };

                for (j = 0, jCount = arrValidator.length; j < jCount; j ++){
                    sValidator =  arrValidator[j];
                    if (sValidator.indexOf("[")){
                        sParam = sValidator.substring(
                            (sValidator.indexOf("[") + 1),
                            sValidator.indexOf("]")
                        );
                        sValidator = sValidator.replace(/\[.+?\]/, "");
                    } else { sParam = ""; }
                    sFunction = "_forms_" + sValidator;
                    try {
                         result = eval("result = " + sFunction + "('" + sField + "', '" + sParam + "');");
                    } catch(e) { result = false; }
                    if (!result){
                        k = arrErrors.length;
                        arrErrors[k] = {};
                        arrErrors[k].title = oForms[i].title;
                        arrErrors[k].field = sField;
                        arrErrors[k].error = (arrErrorMessages[j]) ? arrErrorMessages[j] : arrErrorMessages;
                        arrErrors[k].validator = sValidator;
                    }
                }
            }
        }
    }

    if (arrErrors.length == 0){ return true; }

    formsErrorHandler(arrErrors);

    return false;
}

function _forms_necessary(s, param){
    s = document.getElementById(s).value;
    return (0 != s.replace(/\s/, "").length);
}

function _forms_maxlength(s, max){
    s = document.getElementById(s).value;
    return (0 == s.length || max >= s.length);
}

function _forms_minlength(s, min){
    s = document.getElementById(s).value;
    return (min <= s.length || 0 == s.length);
}

function _forms_confirm(s1, s2){
    s2 = formId + s2;
    s1 = document.getElementById(s1).value;
    s2 = document.getElementById(s2).value;
    return (s1 == s2);
}

function _forms_email(s, param){
    s = document.getElementById(s).value;
    var re = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    return (0 == s.length || -1 != s.search(re));
}

function _forms_file(s, param){
    s = document.getElementById(s).value;
    return (1 < s.length || 0 == s.length);
}

function _forms_select(s, param){
    s = document.getElementById(s).value;
    return (0 != s.value)
}

function _forms_checked(el, chk){
	el  = document.getElementById(el);
	chk = document.getElementById(formId + chk);

	return (chk.checked) ? _forms_necessary(el.id, chk.id) : true;
}

function _forms_unchecked(el, chk){
	el  = document.getElementById(el);
	chk = document.getElementById(formId + chk);

	return (chk.checked) ? true : _forms_necessary(el.id, chk.id);
}

function _forms_sex(s, param){
    s = document.getElementById(s).value;
    return (s == 1 || s == 0);
}

function _forms_month(s, param){
    s = document.getElementById(s).value;
    if ( s != 0 )
    	return (s > 0 && s < 13);
    return true;
}

function _forms_digit(s, param){
    s = document.getElementById(s).value;
    if ( s )
    	return (s.match(/^[0-9]+$/gi));
    return true;
}

function _forms_latin(s, param){
    s = document.getElementById(s).value;
    if ( s )
    	return (s.match(/^[a-z][a-z\_0-9]+$/gi));
    return true;
}
