﻿$(document).ready(function(){

    var span = $("<span>*</span>");
    span.css("color","red")
        .css("padding-left","5px");

    $(span).insertAfter(".Required, .RequiredCheckBox");
    
    $(".Required").keyup(function(){    
        if ($(this).val()!="")
        {
            $(this).next("span").hide();
        }
        else
        {
            $(this).next("span").show();
        }
    });
    
    $(".RequiredCheckBox input").click(function(){    
    
    
        if ($(this).attr("checked")==true)
        {        
            $(this).parent().next("span").hide();
        }
        else
        {
            $(this).parent().next("span").show();
        }
    });
    
    

});

function ValidateFields()
{
    var hasErrors = false;
    
    $.each($(".Required"), function(){
        if ($(this).val()=="")
        {
            $(this).next("span").text("* Required").show();
            hasErrors=true;
        }
    });
    
    
    $.each($(".RequiredCheckBox input"), function(){
        if ($(this).attr("checked")!=true)
        {
            $(this).parent().next("span").text("* Required").show();
            hasErrors=true;
        }
    });
    
    
    $.each($(".Email"), function(){
        if (!validateEmail($(this).val()))
        {
            $(this).next("span").text("* Invalid email").show();
                        
            hasErrors=true;
        }
    });
    
    if (hasErrors)
    {
        return false;
    }
    else
    {
        return true;
    }
}

function validateEmail(Email){
    var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;        
    if(Email.match(emailRegEx)){
        return true;
    }else{
        return false;
    }
}

