   jQuery(document).ready(function()
    {
        if (jQuery('.date-selector')){
            jQuery('.date-selector').datepicker({dateFormat: "dd/mm/yy"});
        }
        
        jQuery('a#self-assessment-t-and-c').click(function(){

            t_and_c_url = "/terms_and_conditions.ajax.php";
            jQuery('<div />')
                .load(t_and_c_url)
                .dialog({
                    resizable: true,
                    height:400,
                    title: 'Terms and Conditions of this Website',
                    width:500,
                    modal: true,
                    buttons: {
                            'I Accept': function() {
                                        jQuery('input#agreeterms').attr('checked',true)
                                        jQuery(this).dialog('close');
                            },
                            Cancel: function() {
                                    jQuery(this).dialog('close');
                            }
                    }
                });

            return false;
        })

        var formvalid=false;
        jQuery('select#dealing-with').change(function(){
            jQuery(this).children('option:selected').each(function(){if(jQuery(this).attr('url')!=undefined){window.location=jQuery(this).attr('url')}})
            //window.location=jQuery(this).attr('url')
        });


//        <a href="#" class="accordion-next" thisnum="2">Next</a>
//        <a href="#" class="accordion-previous" thisnum="2">Previous</a>

        jQuery('button.accordion-next').click(function(){
            val=jQuery('#accordion').accordion( "option", "active" );
            val++;
            jQuery('#accordion').accordion( "option", "active", val);
            jQuery.scrollTo('#accordion');
            return false;
        }).show();
        jQuery('button.accordion-previous').click(function(){
            val=jQuery('#accordion').accordion( "option", "active" );
            val--;
           jQuery('#accordion').accordion( "option", "active", val);
           jQuery.scrollTo('#accordion');
           return false;
        }).show();
//        jQuery('button.accordion-next').click(function(){
//           thisnum = jQuery(this).attr('thisnum');
//           thisnum ++;
//           jQuery('#accordion').accordion( "option", "active", thisnum );
//        });
//        jQuery('button.accordion-previous').click(function(){
//           thisnum = jQuery(this).attr('thisnum');
//           thisnum --;
//           jQuery('#accordion').accordion( "option", "active", thisnum );
//        });

        jQuery('form#self-assessment').submit(function(){

            completed_fields = 0;
            required_fields = jQuery('div.question').length;

            jQuery('div.question').each(function(){
                found=false;
                jQuery(this).find("input[type='radio']:checked").each(
                    function(){
                        found=true;
                    }
                );
                jQuery(this).find("div.slider").each(
                    function(){
                        found=true;
                    }
                );
                if (found==false){
                    jQuery(this).css('border-color','#BF5217').css('border-width','3px');
                }
                else {
                    completed_fields++;
                    jQuery(this).css('border-color','#ACACAC').css('border-width','1px');
                }
            })
            formvalid = required_fields<=completed_fields;

            if (!formvalid){
                nice_alert('Please answer all questions: remaining questions are highlighted with orange', 'Not all fields are complete', true);
                return false;
            }
            formvalid = formvalid && jQuery('input#agreeterms').attr('checked');
            if (!formvalid){
                jQuery('<div></div>')
                .html('<p>You must accept these before you can send us your answers.</p> <br /> <p><i>You will find the link at the bottom of this questionaire</i></p>')
                .dialog({
                        resizable: false,
                        height:220,
                        width: 420,
                        modal: true,
                        title: 'Have you read the terms and conditions?',
                        buttons: {
                                'I have': function() {
                                        jQuery('input#agreeterms').attr('checked',true)
                                        jQuery(this).dialog('close');
                                        formvalid=true;
                                },
                                Cancel: function() {
                                        jQuery(this).dialog('close');
                                        formvalid=false;
                                }
                        }
                });
            }
            return formvalid;
        })
    });

    function self_assessment_slider(fieldname,to,from,increment){
        if(jQuery.browser.msie && (jQuery.browser.version<7))
        {
        }
        else
        {
            jQuery('#'+fieldname+'-accessible').hide();
            jQuery('#'+fieldname+'-js').show();
            jQuery('#'+fieldname+'-js li').append('<input type="hidden" name="'+fieldname+'" id="'+fieldname+'-hidden" value="0"  />');
            jQuery('#'+fieldname+'-slider').slider({
                    value: to/2,
                    min: from,
                    max: to,
                    step: increment,
                    slide: function(event, ui) {
                            jQuery("#"+fieldname+"-hidden").val(ui.value);
                    }
            });
        }
    }
    function nice_alert(message, title, modal)
    {
        var dialog = jQuery('<div></div>')
                .html('<p>'+message+'</p>')
                .dialog({
                        autoOpen: false,
                        title: title,
                        modal: modal
                });
        dialog.dialog('open');
    }

