var notify_valid = true;

function captchaRefresh() {
    $('.captcha-right img', '#notify').remove();
    $('.captcha-right', '#notify').prepend('<img width="150" height="50" src="/captcha/default.html?'+Math.floor(Math.random()*10000001)+'" alt="Captcha" />');
}

$.ui.dialog.defaults.bgiframe = true;

$(document).ready(function(){
    $("#login").hide();
    $(".login").click(function() {
        $("#login").dialog({height: 160});
        return false;
    });


    $('#advanced-search .newField').click(function(){
        var parEl = $(this).parent().parent().attr('id');

        var el = $('#'+parEl).find('.dnone');
        if ( el.hasClass('dnone') ) {
            el.eq(0).removeClass('dnone');
            el.eq(0).find('select').removeAttr('disabled');
        };
        if ( !el.hasClass('dnone') ) {
            $('#'+ parEl +' div a').hide();
        }
        return false;
    });

    $('#advanced-search #btn_reset').click(function(){
        $('#advanced-search .additional').addClass('dnone');
        $('#advanced-search .newField:hidden').show();
    });

    $('dl dt:odd, dl dd:odd', '#organisation-details').addClass('odd');


    $('#add_case_note').click(function() {
//        console.log($(this).attr('href'));
        $('<iframe id="companyDetail" class="companyDetail" src="' + $(this).attr('href') + '" />').dialog({
            title: 'Create new Case Note',
            width: 900,
            height: 600,
            resizable: true,
            autoResize: true,
            position:'top',
            modal: true,
            overlay: {
                opacity: 0.5,
                background: "black"
            }
        }).width(900).height(600);
        return false;
    });

    $('#notify').dialog({
        autoOpen: false,
        title: 'Notify',
        width:600,
        autoResize:true,
        position: 'center',
        overlay: {
            opacity: 0.5,
            background: "black"
        }
    });

    $('#notify-link').click(function() {
        $('#notify').dialog('open');
        return false;
    });

    try {
        $('#notify form').validate({
            onfocusout: true,
            errorClass: 'invalid',
            errorElement: "span",
            focusInvalid: false,
            errorPlacement: function(error, element) {
                var el_container = element.parent().parent().parent();
                if ( el_container.hasClass('radiolist') || el_container.hasClass('checklist') ) {
                    error.appendTo( el_container );
                } else {
                    error.appendTo( element.parent() );
                }
            },
            success: function() {
                notify_valid = true;
            },
            invalidHandler: function(form, validator) {
                var errors = validator.numberOfInvalids();
                if (errors) {
                    notify_valid = false;
                } else {
                    notify_valid = true;
                }
            }
        });
    } catch (exception) {
    }

    $('#notify form').submit(function() {
        if (notify_valid == true) {
            $.ajax({
                url: 'ajax_notify',
                data: $(this).serialize(),
                type: 'post',
                dataType: 'json',
                success: function(data) {
                    if (data.result == true) {
                        $('#notify').empty().html('<p>Thank you for your notify</p>');
                        $('.notify-link').remove();
                    } else {
                        if (data.email_error == true) {
                            $('#email').after('<span class="invalid">Please enter a valid email address.</span>');
                        }
                        if (data.captcha_error == true) {
                            $('#captcha').after('<span class="invalid">The enter code is wrong.</span>');
                        }
                        // refresh captcha image
                        captchaRefresh();
                    }
                }
            });
        }
        return false;
    });

    $('.captcha-right', '#notify').live('click', function() {
        captchaRefresh();
    });

});

