﻿/* JS control form to validity of: Not empty, Is numeric, Is email*/

/* ERROR CONSTATNS for alert*/

var notFilled  = "Заполните все поля";         // Fild is empty
var notNumeric = "Введите числовые значения";  // Fild is not numeric
var notEmail   = "Введите валидный Email";     // Fild is not a valid email address
var notFilledFile = "Загрузите фото!";

function isValidForm(fm)
{
    var elemLength=fm.elements.length; //number of elements in form
    var isValid = true;
    for ( i=0; i<elemLength; i++ )
    {
        var item = fm.elements.item(i);
        if( item && !item.disabled )    //item exists && not disabled
        {
        
        if( item.getAttribute('IsNotEmpty') != null )
            {
                if( item.value == "" )
                {
                    alert( notFilled );
                    isValid = false;
                    return false;
                }
            }
        }
        if( item.getAttribute('IsNotFile') != null )
                   {
                        if( item.value == "" )
                          {
                               alert( notFilledFile );
                               isValid = false;
                               return false;
                          }
               }    
        if( item.getAttribute('IsNumeric') != null )
        {
            if( item.value.search(/^[\d|-]+$/) == -1 )  //if not only numbers
            {
                alert( notNumeric );
                isValid = false;
                return false;
            }
        }
        if( item.getAttribute('IsEmail') != null )
        {
            if( item.getAttribute('IsEmail') != null )
            {
                if( item.value != "" && item.value.search( /^[\w|-]+@[\w|-]+\.[\w]+$/ ) == -1 ) //if not bla_bla@bla.bla
                {
                    alert( notEmail );
                    isValid = false;
                    return false;
                }
            }
        }
    }
}
