/**
 * checkFields
 *
 * examples:
 *      1:      return checkFields( Array( 'author', 'comment', 'email' ) , 'email' )
 *      2:      return checkFields( Array( 'author', 'comment', 'captcha' ) , '' ) 
 *
 * @author      Roman Kutsy
 * @version     2.2.20110610
 */
function checkFields(ids, email_id)
{
    var errors = 0;
    for (var id in ids)
    {
        if( $('#'+ids[id]) && $('#'+ids[id]).val()!==null )
        {            
            if( $('#'+ids[id]).val().length<=0 || 
                ( $('#'+ids[id]+' option:selected').attr('disabled')!==undefined && $('#'+ids[id]+' option:selected').attr('disabled')==true ) )
            {
                errors++;    
                $('#err_'+ids[id]).show();
                $('#'+ids[id]).addClass('error_inp');
            }
            else
            {
                $('#err_'+ids[id]).hide();  
                $('#'+ids[id]).removeClass('error_inp');   
                
                if( email_id!==undefined && email_id.length>0 && email_id==ids[id] )
                {                 
                    var email = $('#'+email_id).val();

                    if (email.match(/(?:[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/))
                    {
                        $('#err_'+email_id).hide();
                        $('#'+email_id).removeClass('error_inp');     
                    }
                    else
                    {
                        errors++;  
                        $('#err_'+email_id).show();
                        $('#'+email_id).addClass('error_inp');
                    }              
                }
            }
        }
        else
        {
            errors++;
        }
    }     
    return (errors>0) ? false : true;
} 

/**
 * checkMatch
 *
 * @author      Roman Kutsy
 * @version     1.0.20110609
 */
function checkMatch(id1,id2)
{
    if( $('#'+id1) && $('#'+id1).val()!==null && $('#'+id2) && $('#'+id2).val()!==null )
    {    
        if( $('#'+id1).val().length>0 && $('#'+id2).val().length>0 )
        {
            if( $('#'+id1).val()==$('#'+id2).val() )
            {
                $('#err_'+id1+'_notmatch').hide();
                $('#'+id1).removeClass('error_inp');
            
                return true;
            }
        }
        
        $('#err_'+id1+'_notmatch').show();
        $('#'+id1).addClass('error_inp');
    }
    
    return false;
}

/**
 * checkLength
 *
 * @author      Roman Kutsy
 * @version     1.0.20110609
 */
function checkLength(id,minLength,maxLength)
{
    if( minLength==undefined )
    {
        minLength = 1;
    }
    if( maxLength==undefined )
    {
        maxLength = 255;
    }
    if( maxLength<minLength )
    {
        maxLength = minLength + 1;
    }

    if( $('#'+id) && $('#'+id).val()!==null )
    {    
        if( $('#'+id).val().length>=minLength && $('#'+id).val().length<=maxLength )
        {
            $('#err_'+id+'_len').hide();
            $('#'+id).addClass('error_inp');
        
            return true;
        }    
        
        $('#err_'+id+'_len').show();
        $('#'+id).addClass('error_inp');
    }
    
    return false;
}

