﻿/// <reference path="../../jquery-1.3.2-vsdoc2.js" />

/**
 * tld.docs.sale.js
 * Manages the styles and functionality of the docs/sale.
 *
 * tld.docs.sale.js used for development only.  tld.docs.sale.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.sale closure
    tld.docs.sale = (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 = 6;
            
            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
                $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('Ownership', '1'));
                    $div.append(createButton('Physical Assets', '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 === 1) {
                if (val === '1' && confirm('Are you sure you want to select "' + $(btn).text() + '"?')) {
                     window.location = '/docs/sale/transfer-of-stock/';
                }
                else if (val === '2' && confirm('Are you sure you want to select "' + $(btn).text() + '"?')) {
                     window.location = '/docs/sale/sale-of-business/';
                }
                else {
                    $('#question' + curQuestion + ' :button').attr('disabled', false).css({ 'color': '#434343', 'border': 'solid 1px white' });
                }
            }   
        }
        
        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();
    
});
