var form;
var jValidateMsg = 'jvalidate-message';
var jAlert = 'jalert';

function jValidate(formClass)
{
    
    form = $('.'+formClass+':first').parent();
    
    //remove validation alerts if any
    $('.' + jValidateMsg).remove();
    $('.' + jAlert).remove();
    
    var bool = true;
    var value;
    
    $('.' + formClass).each(function(){
        
        if($(this).attr('required') && !_isValid( $(this).attr('datatype'), $(this).val() ))
        {
            bool = false;
            $(this).after('<div class="'+jAlert+'">'+$(this).attr('message')+'</div>');
        }
        else
        {
            
        }
        
    });
    
    if(!bool) _showJValidateMessage();

    return bool;
}

function _isValid(datatype, str)
{
    var bool = true;
    
    switch(datatype)
    {
        
        case 'string':
            //names, street addresses, comments
            if(str == '') bool = false;
            break;
        
        case 'email':
            //valid email address
            if(str.indexOf('@') < 1 || str.indexOf('.') < 4) bool = false;
            
            break;
        
        case 'number':
            //any whole number 0,1,2....365
    
            break;
        
        case 'int':
            //any +ve or -ve integer -23, -4, 0, 2, 56
            
            break;
        
        
    }
    
    return bool;
}

function _showJValidateMessage()
{
    
    $(form).prepend('<div class="'+jValidateMsg+'">Oops! You seemed to have missed some necessary field(s).  Please check below to complete. ;)</div>');
    var p = $(form).position();
    $('html,body').animate({scrollTop: p.top},'slow');
    
}
