﻿
    function validate(id) {
    
        var isValid = true;
        jQuery('#'+id + ' .invalid').removeClass("invalid");
        jQuery('#'+id).find('.required, .optional').each(function(){
            if (jQuery(this).hasClass("required")) {
                if (jQuery(this).val().replace(/ /g,"") == ""){
                    isValid = false;
                    processField(jQuery(this), id);
                }
                else {
                    if (jQuery(this).hasClass("isEmail")) {
                        if (!isValidEmail(jQuery(this).val())) {
                            isValid = false;
                            processField(jQuery(this), id);
                        }
                    }
                }
            }
            else {
                if (jQuery(this).val()!= ""){
                    if (jQuery(this).hasClass("isEmail")) {
                        if (!isValidEmail(jQuery(this).val())) {
                            isValid = false;
                            processField(jQuery(this), id);
                        }
                    } 
                }
            }     
        });
        
        jQuery('#'+id).find('.isEmail').each(function(){
              if (!isValidEmail(jQuery(this).val())) {
                            isValid = false;
                            processField(jQuery(this), id);
                        }
        
        });
        
        
        jQuery('#'+id).find('.checkbox_required, .radiobutton_required').each(function(){
            var isGroupValid = false;
            jQuery(this).children().each(function(){
                if (jQuery(this).attr('checked'))
                {
                    isGroupValid = true;
                }
                
            });
            
            if (!isGroupValid){
                isValid = false;
                processField(jQuery(this), id);
            }
            
        
        });
        
       
        return isValid;

    }
      
    function processField(Object, idParent) {
        var labelFor = jQuery(Object).attr("id");
        jQuery("#" + idParent + " label").each(function(){
             if (jQuery(this).attr("for")==labelFor)
             {
               jQuery(this).addClass("invalid");
             }
        });
    }
    
    function isValidEmail(email) {
        var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
        return filter.test(email);
    }
    
    
    function checkDefaultValues(inputsContainer, defaultValues){

        var valid = true;
        jQuery("#" + inputsContainer + " input[type='text'], " + "#" + inputsContainer + " textarea").each(function(){
            if (jQuery.inArray(jQuery(this).val(),defaultValues) != -1 || jQuery(this).val().replace(/ /g,"") == "")
            {
               processField(jQuery(this), inputsContainer);
               valid=false;            
            }
        });
        return valid;
    }
    
    function validateWithDefaultFields(inputsContainer,defaultValues){
        
        
        isValid1 = validate(inputsContainer);
        
        isValid2 = checkDefaultValues(inputsContainer,defaultValues);
        
        return isValid1 && isValid2;
    
    }
