﻿jQuery(function($){

    if( $('#frmBrochureDownload').length > 0 )
    {
        
        var isTellMeMore = $('#tellMeMore').attr('checked');
        var industryId = $('#industry_selectBox').val();
        
        if( !isTellMeMore )
        {
            $('div[rel="tellMeMore"]').each(function(){
                $(this).hide();
            });
        }
        
        
        if( industryId === '7' )
        {
            $('div[rel="industry_selectBox"]').show();
            $('#txtIndustryOther').addClass('required');
        }
        else
        {
            $('div[rel="industry_selectBox"]').hide();
            $('#txtIndustryOther').removeClass('required');
        }
        
        
        // toggle other industry //
        $('#industry_selectBox').change(function(){
            
            industryId = $(this).val();
            
            if( industryId === '7' )
            {
                $('div[rel="industry_selectBox"]').show();
                $('#txtIndustryOther').addClass('required');
            }
            else
            {
                $('div[rel="industry_selectBox"]').hide();
                $('#txtIndustryOther').removeClass('required');
            }
                        
        });
        
        // toggle form //
        $('#tellMeMore').click(function(){
        
            if( $(this).is(':checked') )
            {
                $('div[rel="tellMeMore"]').each(function(){
                    $(this).show(); 
                    
                    industryId = $('#industry_selectBox').val();
            
                    if( industryId === '7' )
                    {
                        $('div[rel="industry_selectBox"]').show();
                        $('#txtIndustryOther').addClass('required');
                    }
                    else
                    {
                        $('div[rel="industry_selectBox"]').hide();
                        $('#txtIndustryOther').removeClass('required');
                    }
                                   
                });
                
                $("#industry_selectBox").rules("add", "required");
                
            }
            else
            {
                $('div[rel="tellMeMore"]').each(function(){
                    $(this).hide();
                    
                    $('div[rel="industry_selectBox"]').hide();
                    $('#txtIndustryOther').removeClass('required');
                    $("#industry_selectBox").removeClass('required');
                });

                
                $("#industry_selectBox").rules("remove", "required");
                                
            }
        
        });
        
        // validate brochure //
        $('#frmBrochureDownload').validate({
            debug:false,
            errorElement: 'span',
            errorContainer: $('.errorDisplay'),
            errorPlacement: function(error, elem){
                
                var tag = elem.parent().get(0).tagName;
                                
                if(tag == "P")
                {
                    error.appendTo(elem.parent().children('span.errorDisplay'));
                }

            },
            success: function(span){
                span.html("&nbsp;&nbsp;").addClass('success');
            },
            rules:{
                firstName:{required:'#tellMeMore:checked'},
                lastName:{required:'#tellMeMore:checked'},
                organization:{required:'#tellMeMore:checked'},
                phoneNumber:{required:'#tellMeMore:checked'},
                postalCode:{required:'#tellMeMore:checked'},
                subject:{required:'#tellMeMore:checked'},
                industry:{required:'#tellMeMore:checked'},
                comments:{required:'#tellMeMore:checked'}
            },
            messages:{
                firstName:{required: '&nbsp;'},
                lastName:{required: '&nbsp;'},
                organization:{required: '&nbsp;'},
                email:{required: '&nbsp;', email: '&nbsp;'},
                postalCode:{required: '&nbsp;'},
                phoneNumber:{required: '&nbsp;'},
                subject:{required: '&nbsp;'},
                industry:{required: '&nbsp;'},
                industryOther:{required: '&nbsp;'},
                comments:{required: '&nbsp;'}
            }
            
            
        });
        
        
    }

});