﻿/*  

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 ($('#membershipBrief').find('span.alert').exists())
    {
        $('#membershipBrief').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');
        }
    });



    // ROB LONG VALIDATION - REPLACED BY JQUERY VALIDATE.
    //    $("#suitabilityQuestionnaire").submit(function()
    //    {
    //        // Set Validation variable to count 'No' answers
    //        var myValidation = 0;
    //        // Count total number of radio button on the form
    //        var questionCount = $('#suitabilityQuestionnaire input:radio[value="Yes"]').length;
    //        // Count how many questions have been answered
    //        var countAnsweredQuestions = $('#suitabilityQuestionnaire input:radio:checked').length;
    //        // Check to see that all questions have been answered
    //        var questionCalc = questionCount - countAnsweredQuestions


    //        //Loop through responded questions looking for 'No' answers
    //        $("#suitabilityQuestionnaire input:radio:checked").each(function()
    //        {
    //            if ($(this).val() == 'No')
    //            {
    //                //Validation Failed
    //                myValidation = myValidation + 1;
    //            }
    //        });

    //        //Check to see that all questions have been answered and that
    //        //none of the questions have been answered as 'No'
    //        if (myValidation == 0 && (questionCalc == 0))
    //        {
    ////            $('.resultFail').hide();
    ////            $('.resultSuccess').show();
    //            //Write 'success' cookie.
    //            $.cookie("fosterCandidate", "true", { expires: 350, path: '/' });
    //            setTimeout("window.location=window.location", redirectTimeout);
    //        }
    //        else
    //        {
    //            $('.resultSuccess').hide();
    //            $('.resultFail').show();
    //            //Delete success cookie if it exists.
    //            $.cookie("fosterCandidate", null);
    //        }

    //        //Do not submit form
    //        return false;
    //    });

});


//function loadInSIFR() {

//    var AventGarde = { src: '/ewcommon/fonts/avantgardebook.swf', ratios: [6, 1.32, 7, 1.35, 8, 1.31, 9, 1.28, 10, 1.26, 11, 1.25, 12, 1.24, 13, 1.23, 14, 1.25, 15, 1.23, 16, 1.23, 17, 1.22, 18, 1.21, 19, 1.21, 20, 1.2, 21, 1.21, 22, 1.2, 23, 1.2, 24, 1.2, 25, 1.19, 26, 1.19, 27, 1.18, 28, 1.19, 29, 1.19, 30, 1.19, 31, 1.19, 32, 1.18, 33, 1.18, 34, 1.19, 35, 1.18, 36, 1.18, 37, 1.18, 38, 1.18, 39, 1.18, 40, 1.17, 41, 1.18, 42, 1.18, 43, 1.17, 44, 1.17, 45, 1.17, 46, 1.17, 47, 1.17, 48, 1.18, 49, 1.17, 50, 1.17, 51, 1.17, 52, 1.17, 53, 1.17, 54, 1.17, 55, 1.17, 56, 1.17, 57, 1.17, 58, 1.17, 59, 1.17, 60, 1.17, 61, 1.16, 62, 1.17, 63, 1.17, 64, 1.17, 65, 1.17, 66, 1.16, 67, 1.16, 68, 1.17, 69, 1.17, 70, 1.16, 71, 1.16, 72, 1.16, 73, 1.16, 74, 1.16, 75, 1.17, 76, 1.16, 77, 1.16, 78, 1.16, 79, 1.16, 80, 1.16, 81, 1.16, 82, 1.16, 83, 1.16, 84, 1.16, 85, 1.16, 86, 1.16, 87, 1.16, 88, 1.16, 89, 1.16, 90, 1.16, 91, 1.16, 92, 1.16, 93, 1.16, 94, 1.16, 95, 1.16, 96, 1.16, 97, 1.16, 98, 1.16, 99, 1.16, 100, 1.16, 101, 1.16, 102, 1.16, 103, 1.16, 104, 1.16, 105, 1.16, 106, 1.16, 107, 1.16, 108, 1.16, 109, 1.16, 110, 1.16, 111, 1.16, 113, 1.16, 114, 1.16, 115, 1.16, 116, 1.16, 117, 1.16, 118, 1.16, 119, 1.16, 120, 1.16, 121, 1.16, 122, 1.16, 123, 1.16, 124, 1.16, 1.16] };

//    sIFR.delayCSS = true;
//    sIFR.activate(AventGarde);

//    sIFR.replace(AventGarde, { selector: '.Site h1', wmode: 'transparent', css: ['.sIFR-root {color: #510452;text-align: left;font-size: 30px;padding: 0;}', 'a { text-decoration: none; }', 'a:link { color: #414141; }', 'a:hover { color: #555555; }'], tuneHeight: 0, offsetTop: 0, offsetLeft: 0, forceSingleLine: true, preventWrap: true });
//    sIFR.replace(AventGarde, { selector: '.box h2.title', wmode: 'transparent', css: ['.sIFR-root {color: #ffffff;text-align: left;font-size: 20px;padding: 0;}'], tuneHeight: 0, offsetTop: 0, offsetLeft: 0, forceSingleLine: true, preventWrap: true });
//    sIFR.replace(AventGarde, { selector: '.Site h2', wmode: 'transparent', css: ['.sIFR-root {color: #90a830;text-align: left;font-size: 24px;padding: 0;}', 'a { text-decoration: none; }', 'a:link { color: #414141; }', 'a:hover { color: #555555; }'], tuneHeight: 0, offsetTop: 0, offsetLeft: 0, forceSingleLine: true, preventWrap: true });
//    //    sIFR.replace(AventGarde, { selector: 'h3', wmode: 'transparent', css: ['.sIFR-root {color: #510452;text-align: left;font-size: 22px;padding: 0;}', 'a { text-decoration: none; }', 'a:link { color: #414141; }', 'a:hover { color: #555555; }'], tuneHeight: 0, offsetTop: 0, offsetLeft: 0, forceSingleLine: true, preventWrap: true });
//    //    sIFR.replace(AventGarde, { selector: 'h4', wmode: 'transparent', css: ['.sIFR-root {color: #000000;text-align: left;font-size: 18px;padding: 0;}', 'a { text-decoration: none; }', 'a:link { color: #414141; }', 'a:hover { color: #555555; }'], tuneHeight: 0, offsetTop: 0, offsetLeft: 0, forceSingleLine: true, preventWrap: true });
//    //    sIFR.replace(AventGarde, { selector: 'h5', wmode: 'transparent', css: ['.sIFR-root {color: #510452;text-align: left;font-size: 14px;padding: 0;}', 'a { text-decoration: none; }', 'a:link { color: #414141; }', 'a:hover { color: #555555; }'], tuneHeight: 0, offsetTop: 0, offsetLeft: 0, forceSingleLine: true, preventWrap: true });
//    //    sIFR.replace(AventGarde, { selector: 'h6', wmode: 'transparent', css: ['.sIFR-root {color: #510452;text-align: left;font-size: 13px;padding: 0;}', 'a { text-decoration: none; }', 'a:link { color: #414141; }', 'a:hover { color: #555555; }'], tuneHeight: 0, offsetTop: 0, offsetLeft: 0, forceSingleLine: true, preventWrap: true });

//}

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()', 90000);
}


function getSessionCheck()
{
    var randomVar = Math.floor(Math.random() * 10000);
    
    // 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'
            });
        } 
    });
} 

