﻿/// <reference path="../../jquery-1.3.2-vsdoc2.js" />

/**
 * tld.docs.loan.js
 * Manages the styles and functionality of the docs/transfer.
 *
 * tld.docs.loan.js used for development only.  tld.docs.loan.min.js used for production.
 *
 * @author: Jeremy Burton (jermbo002@gmail.com)
 * @company: SohoPros Inc. (www.sohopros.com)
 * @date: 16 December 2009
 * @copyright TexasLegalDocs.com
 */

// tld object and tld.docs object
var tld = window.tld || {};
tld.docs = window.tld.docs || {};

// on load function
$(function() {
    
    // tld.docs.loan closure
    tld.docs.loan = (function() {
    
        var categoryId, curQuestion, questions, answers;
    
        function initVars() {
            // This value must match the categoryId of Transfer of Ownership-Contract Agreements and Conveyance Documents in the TLD_Doc_Categories table
            categoryId = 4;
            
            curQuestion = 0;
            questions = $('#questions');
            answers = [];
        }
        
        function loadNextQuestion() {
            tld.ajax('/WebServices/wsDocs.asmx/getNextQuestion', JSON.stringify({ prevQuestion: curQuestion, cId: categoryId }), loadQuestionSuccess, loadQuestionFailure);
        }
        
        function loadQuestionSuccess(question) {
            if (question) {
                
                // update curQuestion
                curQuestion = question.displayOrder;
                
                // show the question
                var $div = $('<div></div>').attr('id', 'question' + curQuestion).addClass('question').appendTo(questions);
                $('<p></p>').text(curQuestion + '. ' + question.question).appendTo($div);
                
                // show possible answers based on which question 
                if (curQuestion === 1) {
                    $div.append(createButton('Yes', '1'));
                    $div.append(createButton('No', '2'));
                }
                else if (curQuestion === 2) {
                    /**
                     * for question 2, 2 options availabe:
                     * 1. Contract Only - Redirect to Contract page
                     * 2. Full Conveyance - load next question
                     */
                    $div.append(createButton('Real Estate', '1'));
                    $div.append(createButton('Personal Property', '2'));
                }
                else if (curQuestion === 3) {
                    /**
                     * for question 3, 2 options:
                     * 1. Transfer Now - load next question
                     * 2. Keep Until Full Payment - Redirect to Contract for Deed page
                     */
                    $div.append(createButton('Yes', '1'));
                    $div.append(createButton('No', '2'));
                }
                
                $('.question-button:enabled').hover(function() { 
                        $(this).css({ 'color': '#1e1e1e', 'border': '1px solid #717171' }); 
                    }, function() { 
                        $(this).css({ 'color': '#434343', 'border': 'solid 1px white' }); 
                    }).click(function() {
                        $('#question' + curQuestion + ' :button').attr('disabled', true);
                        handleClick(this, question);
                        return false;
                    });
            }
        }
        
        function handleClick(btn, question) {
            var val = $(btn).attr('v');
            if (curQuestion < 3) {
                if (val !== question.redirectAnswer.toString() && confirm('Are you sure you want to select "' + $(btn).text() + '"?')) {
                    answers.push($(btn).text());
                    
                    // load next question
                    loadNextQuestion();
                }
                else if (val === question.redirectAnswer.toString() && confirm('Are you sure you want to select "' + $(btn).text() + '"?')) {
                    window.location = '/docs/loan/' + question.redirectPage;
                }
                else {
                    $('#question' + curQuestion + ' :button').attr('disabled', false).css({ 'color': '#434343', 'border': 'solid 1px white' });
                }
            }
            else if (curQuestion === 3) {
                if (val === '1' && confirm('Are you sure you want to select "' + $(btn).text() + '"?')) {
                    $('<p></p>')
                        .text('Please call the TLD office at 817.737.7201 for further information, as Texas homestead laws limit the types of loans allowed to be secured by homestead property.')
                        .css( 'font-weight', 'bold' )
                        .appendTo(questions);
                }
                else if (val === '2' && confirm('Are you sure you want to select "' + $(btn).text() + '"?')) {
                    window.location = '/docs/loan/' + question.redirectPage;
                }
            }   
        }
        
        function createButton(text, value) {
            return $('<button></button>').addClass('question-button').attr('v', value).text(text);
        }
        
        function loadQuestionFailure(error) {
        
        }
        
        return {
            
            init: function() {
                initVars();
                
                // load first question
                loadNextQuestion();
                
            }
            
        };
    
    })().init();
    
});
