$(function(){
    
    // click the enquire button and show the modal window
    $('#prequalifyButton').click(function(){
        Overlay.mask({
            color: '#a2ba54',
            opacity: 0.8
        });
        $('#pop-wrapper').show();
        return false;
    });
    
    // click the close link in the modal window and close it
    $('.pop-close').click(function(){
        $('#pop-wrapper').hide();
        Overlay.unmask();
        return false;
    });

    // click the submit answers button
    $('#step1').click(function(){
        
        // get the values from the radio buttons
        var homeloan = $("input[name='homeloan']:checked").val();
        var totaldebts = $("input[name='totaldebts']:checked").val();
        var newstart = $("input[name='newstart']:checked").val();
        var bankrupt = $("input[name='bankrupt']:checked").val();
        
        // if any not checked, then show error
        if (homeloan == undefined || totaldebts == undefined || newstart == undefined || bankrupt == undefined) {
            $('#error1').show();
        
        // all checked so process
        } else {
        
            var pass = true;
            var pass3 = true;
            
            // (q1) either, so don't need to check
            
            // (q2) (if (q1)=yes) then either;  (if (q1)=No) then must be Yes
            if (homeloan == 'No' && totaldebts == 'No') {
                pass = false;
                pass3 = false;
            }
            
            
            // (q3) must be yes
            if (newstart == 'No') {
                pass = false;
                pass3 = true;
            }
            
            // (q4) (if (q1)=yes) then either;  (if (q1)=No) then must be No
            if (homeloan == 'No' && bankrupt == 'Yes') {
                pass = false;
                pass3 = false;
            }
            
            // AND to override the q3 trap for q4
            if (homeloan == 'No' && bankrupt == 'Yes' && newstart == 'No') {
                pass = false;
                pass3 = false;
            }
            
            // AND to override the q3 trap dor q4
            if (homeloan == 'No' && totaldebts == 'No' && newstart == 'No') {
                pass = false;
                pass3 = false;
            }
            
            // hide section 1
            $('#pop-screen1').hide();
            
            // Pass = show screen-2
            if (pass) {
                $('#pop-screen2').show();
                _gaq.push(['_trackEvent', 'Yes We Can Help', 'Button']);
            }    
                
           // Pass3 = show screen-4
            else if (pass3) {
                $('#pop-screen4').show();
                _gaq.push(['_trackEvent', 'Unable to Assist Ð No Income', 'Button']);     
                
            // Fail = show screen-3
            } else {
                $('#pop-screen3').show();
                _gaq.push(['_trackEvent', 'Unable to Assist', 'Button']);
            }
            
        }
        return false;   
    });
    
    
     // click the Go Back button
    $('#buttonGoBack').click(function(){
    	$('#pop-screen1').show();
    	$('#pop-screen2').hide();
    	$('#pop-screen3').hide();
    	$('#pop-screen4').hide();
    	return false;
    });
    
     // click the Go Back button
    $('#buttonGoBack2').click(function(){
    	$('#pop-screen1').show();
    	$('#pop-screen2').hide();
    	$('#pop-screen3').hide();
    	$('#pop-screen4').hide();
    	return false;
    });
    

});

Overlay = {
    
    /**
     * The current z-index of the top most mask
     */
    curIndex: 9000,
    
    /**
     * An array to hold all current masks
     */
    masks: null,

    /**
     * Creates a mask over the page or elements
     *
     * @params object initialisation parameters
     */
    mask: function(params) {
        if (!this.masks) {
            this.masks = new Array;
        }
        params ? null : params = {};
        params.elm ? null : params.elm = 'body';
        params.id ? null : params.id = 'q-mask';
        params.color ? null : params.color = '#000';
        params.opacity ? null : params.opacity = 0.85;
        params.zindex ? this.curIndex = params.zindex : null;
        if (params.elm === 'body') {
            $('body').append("<div id='" + params.id + "'></div>");
        } else if ($('#' + params.elm).length) {
            $('#' + params.elm).append("<div id='" + params.id + "'></div>");
        }
        var parent = $('#' + params.id).parent();
        $('#' + params.id).css({
            display: 'block',
            height: parent.height(),
            width: parent.width(),
            position: 'fixed',
            left: parent.position().left,
            top: parent.position().top,
            'z-index': Overlay.curIndex,
            opacity: params.opacity,
            background: params.color
        });
        Overlay.curIndex++;
        if ($('body').height() < $(window).height()) {
            $('#' + params.id).height($(window).height());
        }
        return this.masks[params.elm] = params.id;
    },

    /**
     * Removes a given mask from the page
     *
     * @params object initialisation parameters
     */
    unmask: function(params) {
        params ? null : params = {};
        if (!params.elm) {
            $('#' + this.masks['body']).remove();
            delete this.masks['body'];
        } else {
            $('#' + params.id).remove();
            delete this.masks[params.elm];
        }
    }

};
