//
// Flowchart Q&A wizard
// --------------------------------------------
// Copyright 2009 Page1solutions, LLC
// Author: mchrisneglia@gmail.com
// Last Updated 2.2.2009
// ---------------------------------------------
// Uses custom json object to provide questions and 
// decisions based on user choices.
//

//
// Globals, holds question positions; initialize to beginning
//
var debug = true; 

var starting_question = "Open Federal Tax Liens?";
var curr_question = starting_question; 
var last_question = starting_question;



var starting_choice = "N";

var curr_choice = starting_choice; 
var last_choice = starting_choice; 

var title_token = "%question_title%"; 
var text_token  = "%question_text%";

//
// Here is the template of the form that is presented
// Replaceable variables are the Title and Text of the question.
//
var ask_template = "" +
     " <h3>%question_title%</h3> " +
     " <h4>%question_text%</h4> " +
     " <form name='question' action='javascript:void(0);'> " +
     "      <input type='radio' name='answer' value='Y' onclick='javascript:get_next_question(this.value);' /> Yes " +
     "      <input type='radio' name='answer' value='N' onclick='javascript:get_next_question(this.value);' /> No " +
     " </form> " +
     " <br /><br /> " +
     " <a href='' onclick='javascript: go_back();return false;'>Go Back</a> &nbsp;or&nbsp; <a href='' onclick='javascript: start_over();'>Start Over</a> ";
       
var end_template = "" +
     " <h3>%question_title%</h3> " +
     " <h4>%question_text%</h4> " + 
     " <br /><br /> " +
     " <a href='' onclick='javascript: go_back();return false;'>Go Back</a> &nbsp;or&nbsp; <a href='' onclick='javascript: start_over();'>Start Over</a> ";

var start_template = "" +
     " <h3></h3>" + 
     " <form name='question' action='javascript:void(0);'> " +
     "    <input type='submit' value='Start' name='Start' onclick='javascript:ask(curr_question)'/> " +
     " </form> ";

//
// @TODO@ Change the (1) and (2) to &nbsp; and &nbsp;&nbsp;  or something like that.
//
var questions = {

     "Open Federal Tax Liens?" : {
          "question_text" : "Does your law firm or any of it's partners have any unpaid federal tax liens?", 
          "Y" : "Sorry, we are not a good fit for you.", 
          "N" : "Positive Net Worth?"
     }, 
    
     "Sorry, we are not a good fit for you." : {
          "question_text" : "Sorry, but based upon your answers, we do not appear to be a good fit for your law firm.  If you have further questions, feel free to call us toll free at 1.877.894.9724 or <a href='contact.html'>click here</a> to send us an email.",
          "Y" : "%end%",
          "N" : "%end%"
     }, 
     
     "Positive Net Worth?": {
          "question_text" : "Does your law firm and its partners have a combined net worth that is positive?  (All assets minus all liabilities)", 
          "Y"        : "Beacon Score >= 620", 
          "N"        : "Signed Settlement?" 
     }, 
     
     "Beacon Score >= 620": {
          "question_text" : "Does the average credit score of all of your law firm partners exceed 620?  (Our standard is the \"\"Beacon\"\" score as calculated by Equifax at www. Equifax.com)", 
          "Y"        : "Gross Fees >=$250K?", 
          "N"        : "Signed Settlement?" 
     },
     
     "Signed Settlement?": {
          "question_text" : "Does your law firm have a signed settlement agreement in hand for which the payment has been delayed?", 
          "Y"        : "Settlement Funding is best for you. <a href='https://secure.page1forms.com/clients/advocatecapital/apply.htm'>Click Here</a>.", 
          "N"        : "Sorry, we are not a good fit for you." 
     },
     
     "Settlement Funding is best for you. <a href='https://secure.page1forms.com/clients/advocatecapital/apply.htm'>Click Here</a>.": {
          "question_text" : "Based upon your answers, it appears that the most cost-effective solution for your law firm is our Settlement Funding product.  <a href='https://secure.page1forms.com/clients/advocatecapital/apply.htm'>Click here</a> to enroll now or call toll free 1.877.894.9724 to speak with one of our knowledgeable financial advisors.", 
          "Y"        : "%end%", 
          "N"        : "%end%"
     },
     
      "Gross Fees >=$250K?": {
          "question_text" : "Is the average gross fee revenues of your law firm over the past three years at least $225,000?", 
          "Y"        : "Cash Flow Positive?", 
          "N"        : "Signed Settlement?" 
     },
     
      "Cash Flow Positive?": {
          "question_text" : "Has your law firm generated a positive cash flow over the past three years (on average)?", 
          "Y"        : "$50K Out in Case Expenses ", 
          "N"        : "$50K Out in Case Expenses" 
     },
     
      "$50K Out in Case Expenses": {
          "question_text" : "Does your law firm have at least $50,000 invested in case expenses that will be reimbursable at the conclusion of those cases?", 
          "Y"        : "Case Expense Funding is best for you. <a href='https://secure.page1forms.com/clients/advocatecapital/apply.htm'>Click Here</a>.", 
          "N"        : "Signed Settlement?"
     },
     
      "$50K Out in Case Expenses ": {
          "question_text" : "Does your law firm have at least $50,000 invested in case expenses that will be reimbursable at the conclusion of those cases?", 
          "Y"        : "A combination of Case Expense Funding  and Working Capital is best for you. <a href='https://secure.page1forms.com/clients/advocatecapital/apply.htm'>Click Here</a>.", 
          "N"        : "Gross Fees >=$450K?"
     },
     
      "Case Expense Funding is best for you. <a href='https://secure.page1forms.com/clients/advocatecapital/apply.htm'>Click Here</a>.": {
          "question_text" : "Based upon your answers, it appears that the most cost-effective solution for your law firm is our Case Expense Funding product.  <a href='https://secure.page1forms.com/clients/advocatecapital/apply.htm'>Click here</a> to enroll now or call toll free 1.877.894.9724 to speak with one of our knowledgeable financial advisors.", 
          "Y"        : "%end%", 
          "N"        : "%end%" 
     },
     
      "Gross Fees >=$450K?": {
          "question_text" : "Is the average gross fee revenues of your law firm over the past three years at least $450,000?", 
          "Y"        : "Signed Settlement? ", 
          "N"        : "Signed Settlement?" 
     },
     
      "Signed Settlement? ": {
          "question_text" : "Does your law firm have a signed settlement agreement in hand for which the payment has been delayed?", 
          "Y"        : "A combination of Settlement Funding  and Working Capital is best for you. <a href='https://secure.page1forms.com/clients/advocatecapital/apply.htm'>Click Here</a>.", 
          "N"        : "Working Capital is best for you.  <a href='https://secure.page1forms.com/clients/advocatecapital/apply.htm'>Click Here</a>." 
     },
    
      "Working Capital is best for you.  <a href='https://secure.page1forms.com/clients/advocatecapital/apply.htm'>Click Here</a>.": {
          "question_text" : "Based upon your answers, it appears that the most cost-effective solution for your law firm is our Working Capital product.  <a href='https://secure.page1forms.com/clients/advocatecapital/apply.htm'>Click here</a> to enroll now or call toll free 1.877.894.9724 to speak with one of our knowledgeable financial advisors.", 
          "Y"        : "%end%", 
          "N"        : "%end%" 
     },
     
      "A combination of Case Expense Funding  and Working Capital is best for you. <a href='https://secure.page1forms.com/clients/advocatecapital/apply.htm'>Click Here</a>.": {
          "question_text" : "Based upon your answers, it appears that the most cost-effective solution for your law firm would be a combination of our Case Expense Funding and Working Capital products.  <a href='https://secure.page1forms.com/clients/advocatecapital/apply.htm'>Click Here</a> to enroll now or call toll free 1.877.894.9724 to speak with one of our knowledgeable financial advisors.", 
          "Y"        : "%end%", 
          "N"        : "%end%" 
     },
     
     "A combination of Settlement Funding  and Working Capital is best for you. <a href='https://secure.page1forms.com/clients/advocatecapital/apply.htm'>Click Here</a>.": {
          "question_text" : "Based upon your answers, it appears that the most cost-effective solution for your law firm would be a combination of our Settlement Funding and/or Working Capital products.  <a href='https://secure.page1forms.com/clients/advocatecapital/apply.htm'>Click here</a> to enroll now or call toll free 1.877.894.9724 to speak with one of our knowledgeable financial advisors.", 
          "Y"        : "%end%", 
          "N"        : "%end%"
     }
}; // end questions

//
// Functions
//

//
// Reset all and start over with first question.
//
function start_over () {
     
     state = "start";
     curr_question = starting_question; 
     last_question = starting_question;
     
     ask(curr_question);
}

//
// Ask the last question.
//
function go_back () {
     
     //
     // BugFIX: It's never at the end if you go back.  Duh! :-)
     //
     state = "middle";
     curr_question = last_question; 
     
     ask(curr_question); 
}

//
// Given a question, will make the question and prompt the user with Y | N choices
//
function ask (question) {

     var output = ""; 
     
     if ( typeof(questions[question]) == "undefined" ) {
     
          state = "start"; 
     }
     else {
          
          if ( (questions[question].Y == "%end%") || (questions[curr_question].Y == "%end%") ) {
               
               state = "end";
          }
     }
     
     switch (state) {
          
          case ('start') :
               output = start_template;
               state = "middle"; 
               break; 
          
          case ('middle') : 
          
               output = ask_template;
               output = output.replace(title_token, question);
               output = output.replace(text_token, questions[question].question_text);
               break; 
          
          case ('end') :
               output = end_template;
               output = output.replace(title_token, question);
               output = output.replace(text_token, questions[question].question_text);  
               break; 
     }
    
     jQuery('#wizard').html(output); 
}

//
// Given an answer to the current question, will give the next question.
// Accepts "Y" or "N"
//
function get_next_question (answer) {

     var next_question = questions[curr_question][answer]; 
     last_question = curr_question;
     curr_question = next_question;
     
     if (debug) { 
        //  console.log("gnq answer : " + answer);
        //  console.log("gnq curr_question : " + curr_question);
        //  console.log("gnq questions[curr_question] : " + questions[curr_question]);
        //  console.log("gnq questions[curr_question][answer] : " + questions[curr_question][answer]);
     }; 
     
     ask(next_question);
}

//
// Begin script.
//
jQuery(function(){

     ask("start"); 
});