﻿/*  

NEXSTEP FOSTERING FUNCTIONS
© Eonic Ltd 2010
Authors: Will Hancock & Rob Wakeford
*/


// jQuery exists function
jQuery.fn.exists = function () { return jQuery(this).length > 0; }

var redirectTimeout = 4000;

$(document).ready(function () {

    // IF APPLICATION FORM ON PAGE
    if ($("form#ApplicationForm").exists()) {
        var validator = validateApplicationForm();
        $(".cancel").click(function () {
            validator.resetForm();
        });

        // If Persons in form, set listener for edit button to show form.
        if ($("table.persons").exists()) {
            $("a.editperson").click(function () {

                var formName = $(this).attr('title');
                var showForm = $(this).hasClass('showForm');
                if (showForm) {
                    $("#" + formName).show();
                    $(this).html('&gt; Hide');
                    $(this).removeClass('showForm');
                    $("#" + formName).find(":input").not(':radio').not(':submit').addClass('required');
                }
                else {
                    $("#" + formName).hide();
                    $(this).html('&lt; Edit');
                    $(this).addClass('showForm');
                    $("#" + formName).find(":input").not(':radio').not(':submit').removeClass('required');
                }

            });
        }

    }

    if ($("form#suitabilityQuestionnaire").exists()) {

        $("form#suitabilityQuestionnaire").validate({

            submitHandler: function (form) {
                var myValidation = 0
                $("#suitabilityQuestionnaire input:checked").each(function () {
                    if ($(this).val() == 'No') {
                        //Validation Failed
                        myValidation = myValidation + 1;
                    }
                });



                if (myValidation == 0) {

                    $.cookie("fosterCandidate", "true", { expires: 350, path: '/' });
                    disableButton(form.submit);
                    form.submit();
                    $("form#suitabilityQuestionnaire input[name='suitabilityQuestionnaire']").click();
                }
                else {
                    $.cookie("fosterCandidate", null);
                    $('.resultFail').show();
                }

            }
        });
    }

    function verticalDividerHeight(group, targetDiv) {
        var tallest = 0;
        group.each(function () {
            thisHeight = $(this).height();
            if (thisHeight > tallest) {
                tallest = thisHeight;
            }
        });

        $(targetDiv).each(function () {
            $(this).height(tallest);
        });
    }

    function equalColumnHeight(group) {
        var tallest = 0;
        group.each(function () {
            thisHeight = $(this).height();
            if (thisHeight > tallest) {
                tallest = thisHeight;
            }
        });

        group.each(function () {
            $(this).height(tallest);
        });
    }

    // Modal Login Listener
    if ($(".loginButton a.login").exists()) {
        $(".loginButton a.login").click(function (ev) {
            ev.preventDefault();
            $('#membershipBrief').modal({
                opacity: '70',
                overlayClose: 'true'
            });
        });
    }
    // IF Alert show login box
    /* if ($('#registrationForm').find('span.alert').exists())
    {
    $('#registrationForm').modal({
    opacity: '70',
    overlayClose: 'true'
    });
    }*/

    // IF REGISTRATION TEST FOR SUITABLE CANDIDATE
    if ($("#registrationForm").exists()) {
        var suitableFoster = $.cookie('fosterCandidate');
        if (!suitableFoster) {
            $('#sQuestionnaire').modal({
                opacity: '70'
            });
        }
    }

    // IF REGISTRATION TEST FOR SUITABLE CANDIDATE
    if ($("div.loading").exists()) {
        if (!suitableFoster) {
            $('div.loading').modal({
                opacity: '70'
            });
        }
    }

    //remove opening &gt; from all backlinks.
    $('.Site p.backlink > a').each(function () {
        var origText = $(this).html();
        $(this).html('Back');
    })


    // remove background styling from 2col forms. - can't use css as
    // classes available and css3 styling won't work in ie6 - this wil!
    $(".Site form td[colspan='2']").each(function () {
        var cssObj = {
            'border-bottom': 'none',
            'background-color': '#fff'
        }
        $(this).css(cssObj);

    })

    //Select all column divs.
    $("div[id^='column']").each(function () {
        //If not column 1 add divider html before it.

        if ($(this).attr('id') != 'column1') {
            $(this).before('<div class="verticalDivider"><div class="verticalDividerBottomFade">&#160;</div></div>');
        }

    });

    //Equal height all the verticalDivider columns.

    equalColumnHeight($(".template_3_Columns .box .content"));
    verticalDividerHeight($("div[id^='column']"), '.verticalDivider');


    // =========== MembershipBrief Controls

    $("#cUserName").focus(function () {
        $(this).val('');
        $(this).removeClass('username');
    });

    $("#cPassword").focus(function () {
        $(this).val('');
        $(this).removeClass('password');
    });

    $("#cPassword").each(function () {
        if ($(this).val() != '') {
            $(this).removeClass('password');
        }
    });

    $("#cUserName").each(function () {
        if ($(this).val() != '') {
            $(this).removeClass('username');
        }
    });


});


function showDependant(dependant, allDependants) {
    /* Hide and Show Dependants */
    $("." + allDependants).addClass('hidden');
    $("." + allDependants).find("input[type!='radio']").removeClass('required');
    $("." + allDependants).find("textarea").removeClass('required');
    $("." + allDependants).find("select").removeClass('required');
    //$("#" + dependant).find(":input").not(':radio').not(':submit').removeClass('required');

    $("#" + dependant).removeClass('hidden');
    $("#" + dependant).find(":input").not(':radio').not(':submit').addClass('required');
}


function sessionListener() {
    // set interval to check session
    checkSession = setInterval('getSessionCheck()', 10000);
}


function getSessionCheck() {
    var randomVar = Math.floor(Math.random() * 100000000);

    // Load in html to trigger session timeout
    $("#sessionListener").load('/?checkSession=0&random=' + randomVar, function () {
        var sessionStatus = $("#sessionStatus").attr('class');

        //If timed out - Close down this mother!
        if (sessionStatus == 'sessionclosed') {
            // stop interval
            clearInterval(checkSession);

            // model loged off box
            $('.loading').modal({
                opacity: '70'
            });
        }
    });
} 


