var script_steps = {
	'start' : {
		'desc' : '<p>Welcome to our new Smart Application tool designed to give you financing answers quickly before starting the financing process with Preferred Capital Alliance, Inc. Answer the questions and the business loan application will help you pre-qualify for a small business loan, equipment lease, commercial mortgage and much more!</p><p>Please, answer the following questions. If you need to return to a previous question, please, use Back to Previous Questions button and not Back function in your Browser</p><p>Where is your company based?</p>', 
		'options' : {
			'United States' : 'type-of-financing', 
			'Other Country' : 'non-us'
		} 
	},
	'type-of-financing' : {
		'desc' : '<p>What type of financing are you looking for?</p>',
		'options' : {
			'Start a new business' : 'start-new-business',
			'Buy a franchise' : 'start-new-business',
			'Expand existing business' : 'expand-business',
			'Buy a company' : 'buy-company',
			'Buy equipment' : 'buy-equipment',
			'Buy commercial real estate' : 'buy-realestate',
			'Refinance Commercial Mortgage' : 'refinance-mortgage',
			'Obtain working capital' : 'working-capital'
		}
	},

	'refinance-mortgage' : {
		'desc' : '<p>How is your personal credit?  Your credit score is between</p>',
		'options' : {
			'400-500' : 'rm400-640',
			'500-600' : 'rm400-640',
			'600-640' : 'rm400-640',
			'640-680' : 'rm640+',
			'680-700' : 'rm640+',
			'700+'    : 'rm640+'
		}
	},
	'rm400-640' : {
		'desc' : '<p>We are sorry, all of our programs for start up businesses require a minimum 640 score.</p>'
	},
	'rm640+' : {
		 'desc' : '<p>What is the total loan amount you need to refinance commercial real estate?</p>'
		,'custom' : {
			'print' : '$<input id="amount"><button onclick="process_amount(\'amount\', \'rm$\', \'100000-\')">Continue</button><br>'
		}
	},
	'rm$out' : {
		'desc' : '<p>Our minimum investment amount requirements are $100,000.</p>'
	},
	'rm$in' : {
		'desc' : '<p>How much is your current mortgage?</p>'
		,'textfield' : 'mortgage'
		,'nextstep'  : 'rm-interest'
	},
	'rm-interest' : {
		'desc' : '<p>What is your current interest rate?</p>'
		,'textfield' : 'interestrate'
		,'nextstep'  : 'rm-baloon-payment'
	},
	'rm-baloon-payment' : {
		'desc' : '<p>Does your current mortgage have a balloon payment?</p>',
		'options' : {
			'Yes' : 'rm-mortgage-due',
			'No'  : 'rm-owner-investment'
		}
	},
	'rm-mortgage-due' : {
		'desc' : '<p>When is the current mortgage due?</p>'
		,'textfield' : 'mortgagedue'
		,'nextstep'  : 'rm-owner-investment'
	},
	'rm-owner-investment' : {
		'desc' : '<p>Is your property owner-occupied or investment property?</p>'
		,'options' : {
			'Owner-occupied' : 'rm-property-type',
			'Investment property'  : 'rm-property-type'
		}
	},
	'rm-property-type' : {
		'desc' : '<p>What type of property (office, multifamily, retail, mobile home park, etc)?</p>'
		,'textfield' : 'propertytype'
		,'nextstep'  : 'rm-purpose'
	},
	'rm-purpose' : {
		'desc' : '<p>What is the purpose of the refinance?</p>'
		,'textfield' : 'propertytype'
		,'cols'      : 40
		,'rows'      : 5
		,'nextstep'  : 'rm-form'
	},
	'rm-form' : {
		'desc' : '<p>Please, provide your contact details and the business finance consultant will contact you to discuss further.</p>',
		'custom' : {
			'print' : '',
			'fn' : show_submit_form
		}
	},





	'non-us' : {
		'desc' : '<p>Preferred Capital Alliance, Inc.  currently has programs only for the United States based companies that are looking for financing for their U.S.-based projects.  We are sorry, we will not be able to help you at this time.</p>'
	},
	'start-new-business' : {
		'desc' : '<p>How is your personal credit?  Your credit score is between</p>',
		'options' : {
			'400-500' : 'sb400-640',
			'500-600' : 'sb400-640',
			'600-640' : 'sb400-640',
			'640-680' : 'sb640+',
			'680-700' : 'sb640+',
			'700+'    : 'sb640+'
		}
	},
	'sb400-640' : {
		'desc' : '<p>We are sorry, all of our programs for start up businesses require a minimum 640 score.</p>'
	},
	'sb640+' : {
		 'desc' : '<p>What is the total amount you need to start the business?</p>'
		,'custom' : {
			'print' : '$<input id="amount"><button onclick="process_amount(\'amount\', \'sb$\', \'-220000-310000-2400000-\')">Continue</button><br>'
		}
	},
	'sb$-220000' : {
		'desc' : '<p>Our investment amount requirements are $220,000 to $2,400,000, unless the majority of your investment is equipment or commercial real estate related.</p>',
		'options' : {
			'Buy Equipment' : 'be640+',
			'Buy Commercial Real Estate' : 're640+'
		}
	},
	'sb$220000-310000' : {
		'desc' : '<p>Our business start-up loans of $249,000 or less require a cash injection from the borrower of 30% (unless commercial real estate is included, then ~20%) of the total investment costs.  Will you have such funds available?</p>',
		'options' : {
			'Yes' : 'sb-funds-available',
			'No'  : 'funds-not-available'
		}
	},
	'sb$310000-2400000' : {
		'desc' : '<p>Our business start-up loans require a cash injection from the borrower of 15%-20% of the total investment costs.  Will you have such funds available?</p>',
		'options' : {
			'Yes' : 'sb-funds-available',
			'No'  : 'funds-not-available'
		}
	},
	'funds-not-available' : {
		'desc' : '<p>We are sorry, our financing programs require a cash injection from the borrower to align their interests with the lender.  Please, contact us again in the future, when you are able to contribute to the start-up costs of your business.</p>'
	},
	'sb-funds-available' : {
		'desc' : '<p>Do you have previous management or business ownership experience?</p>',
		'options' : {
			'Yes' : 'have-management-experience',
			'No' : 'no-management-experience'
		}
	},
	'have-management-experience' : {
		'desc' : '<p>Do you have current lawsuits outstanding against you or your company?</p>',
		'options' : {
			'Yes' : 'have-lawsuits',
			'No'  : 'no-lawsuits'
		}
	},
	'no-management-experience' : {
		'desc' : '<p>Previous management or business ownership experience is required for our start up loan programs.  Do you have a partner that has such experience?</p>',
		'options' : {
			'Yes' : 'have-management-experience',
			'No'  : 'no-experienced-partner'
		}
	},
	'no-experienced-partner' : {
		'desc' : '<p>We are sorry, we will not be able to help you at this time.</p>'
	},
	'no-lawsuits' : {
		'desc' : '<p>Based on your answers above, you appear to meet the basic requirements for our start up loan program.  Please, complete the following contact information fields and one of our Business Finance Consultants will contact you to determine if you can meet the collateral and ratio requirements and to start the loan process.</p>',
		'custom' : {
			'print' : '',
			'fn' : show_submit_form
		}
	},
	'have-lawsuits' : {
		'desc' : '<p>We are sorry our loan programs require that you have no outstanding lawsuits against you.</p>'
	},
	'buy-company' : {
		'desc' : '<p>How is your personal credit?  Your credit score is between</p>',
		'options' : {
			'400-500' : 'bb400-640',
			'500-600' : 'bb400-640',
			'600-640' : 'bb400-640',
			'640-680' : 'bb640+',
			'680-700' : 'bb640+',
			'700+'    : 'bb640+'		
		}
	},
	'bb400-640' : {
		'desc' : '<p>We are sorry, all of our programs for business acquisition require a minimum 640 score.</p>'
	},
	'bb640+' : {
		'desc' : '<p>What is the total amount you need to buy the business?</p>',
		'custom' : {
			'print' : '$<input id="amount"><button onclick="process_amount(\'amount\', \'bb$\', \'-125000-310000-2400000-\')">Continue</button><br>'
		}
	},
	'bb$-125000' : {
		'desc' : '<p>Our investment amount requirements are $125,000 to $2,400,000, unless the majority of your investment is equipment or commercial real estate related.</p>',
		'options' : {
			'Buy Equipment' : 'be640+',
			'Buy Commercial Real Estate' : 're640+'
		}
	},
	'bb$125000-310000' : {
		'desc' : '<p>Our business acquisition loans in this range require a cash injection from the borrower of 20% of the total investment costs.  Will you have such funds available?</p>',
		'options' : {
			'Yes' : 'bb-experience',
			'No' : 'funds-not-available'
		}
	},
	'bb$310000-2400000' : {
		'desc' : '<p>Our business acquisition loans in this range typically require a cash injection from the borrower of 15% of the total investment costs.  Will you have such funds available?</p>',
		'options' : {
			'Yes' : 'bb-experience',
			'No' : 'funds-not-available'
		}
	},
	'bb-experience' : {
		'desc' : '<p>Do you have previous management or business ownership experience?</p>',
		'options' : {
			'Yes' : 'bb-lawsuits',
			'No' : 'bb-partner-experience'
		}
	},
	'bb-partner-experience' : {
		'desc' : '<p>Previous management or business ownership experience is required for our acquisition loan programs.  Do you have a partner that has such experience?</p>',
		'options' : {
			'Yes' : 'bb-lawsuits',
			'No' : 'no-experienced-partner'
		}
	},
	'bb-lawsuits' : {
		'desc' : '<p>Do you have current lawsuits outstanding against you or your company?</p>',
		'options' : {
			'Yes' : 'have-lawsuits',
			'No'  : 'bb-submit'
		}
	},
	'bb-submit' : {
		'desc' : '<p>Based on your answers above, you appear to meet the basic requirements for our business aquisition program. Please, complete the following contact information fields and one of our Business Finance Consultants will contact you to determine if you can meet the collateral and ratio requirements and to start the loan process.</p>',
		'custom' : {
			'print' : '',
			'fn' : show_submit_form
		}
	},
	'working-capital' : {
		'desc' : '<p>How is your personal credit?  Your credit score is between</p>',
		'options' : {
			'400-500' : 'wc-640',
			'500-600' : 'wc-640',
			'600-640' : 'wc-640',
			'640-680' : 'wc640+',
			'680-700' : 'wc640+',
			'700+'    : 'wc640+'
		}
	},
	'wc-640' : {
		'desc' : '<p>Does your business sell to consumers or other businesses?</p>',
		'options' : {
			'Consumers' : 'wc-consumers',
			'Businesses' : 'wc-businesses'
		}
	},
	'wc-consumers' : {
		'desc' : '<p>We are sorry we will not be able to help you.  Our programs for businesses that sell to consumers require minimum credit scores of 640</p>'
	},
	'wc-businesses' : {
		'desc' : '<p>Do you carry Accounts Receivable?</p>',
		'options' : {
			'Yes' : 'carry-ac',
			'No'  : 'no-ac'
		}
	},
	'no-ac' : {
		'desc' : '<p>We are sorry, we will not be able to help you.  Our programs for people with credit scores below 640 are based on Accounts Receivable.</p>'
	},
	'carry-ac' : {
		'desc' : '<p>Is your average monthly Accounts Receivable outstanding more than $10,000?</p>',
		'options' : {
			'Yes' : 'wc-submit',
			'No' : 'not-enough-ac'
		}
	},
	'not-enough-ac' : {
		'desc' : '<p>We are sorry, our Accounts Receivable Factoring programs require a minimum of $10,000 in monthly average Accounts Receivable.</p>'
	},
	'wc640+' : {
		'desc' : '<p>How much do you need for your working capital?</p>',
		'custom' : {
			'print' : '$<input id="amount"><button onclick="process_amount(\'amount\', \'wc$\', \'-100000-2000000-\')">Continue</button><br>'
		}
	},
	'wc$in' : {
		'desc' : '<p>Based on your answers above, you appear to meet the basic requirements for our SBA working capital business loan, please, provide the following contact information and one of our Business Finance Consultants will contact you to discuss further.</p>',
		'custom' : {
			'print' : '',
			'fn' : show_submit_form
		}
	},
	'wc-submit' : {
		'desc' : '<p>You might be a candidate for our Accounts Receivable Factoring program.  Please, provide the following contact information and one of our Business Finance Consultants will contact you to discuss further.</p>',
		'custom' : {
			'print' : '',
			'fn' : show_submit_form
		}
	},
	'wc$out' : {
		'desc' : '<p>Our investment amount requirements are $100,000 to $2,400,000.</p>'
	},
	'expand-business' : {
		'desc' : '<p>What will you use the funds for</p>',
		'options' : {
			'Equipment Acquisition' : 'buy-equipment',
			'Working Capital' : 'working-capital',
			'Acquire Another Business' : 'buy-company'
		}
	},
	'buy-equipment' : {
		'desc' : '<p>How is your personal credit?  Your credit score is between</p>',
		'options' : {
			'400-500' : 'be-640',
			'500-600' : 'be-640',
			'600-640' : 'be-640',
			'640-680' : 'be640+',
			'680-700' : 'be640+',
			'700+'    : 'be640+'
		}
	},
	'be-640' : {
		'desc' : '<p>We are sorry, all of our programs for business acquisition require a minimum 640 score.</p>'
	},
	'be640+' : {
		'desc' : '<p>What is the total amount you need to obtain equipment?</p>',
		'custom' : {
			'print' : '$<input id="amount"><button onclick="process_amount(\'amount\', \'be$\', \'-5000\')">Continue</button><br>'
		}
	},
	'be$out' : {
		'desc' : '<p>We are sorry, our minimum amount for equipment financing is $5,000.</p>'
	},
	'be$in' : {
		'desc' : '<p>How long have you been in business?</p>',
		'options' : {
			'Less than 1 year' : '<=1',
			'1 year' : '<=1',
			'2 years' : '>=2',
			'3 years' : '>=2',
			'4 years' : '>=2',
			'5 years' : '>=2',
			'more than 5 years' : '>=2'
		}
	},
	'<=1' : {
		'desc' : '<p>Do you have previous management, business ownership or industry experience?</p>',
		'options' : {
			'Yes' : '>=2',
			'No'  : 'be-no-experience'
		}
	},
	'be-no-experience' : {
		'desc' : '<p>Our startup equipment financing programs require that you have previous work experience.  Unless you have a partner with experience that qualifies for this type of financing, unfortunately we will not be able to assist you.</p>',
		'options' : {
			'Have a partner' : '>=2'
		}
	},
	'>=2' : {
		'desc' : '<p>You might be a candidate for our  Equipment Leasing program, please, provide the following contact information and one of our Business Finance Consultants will contact you to discuss further.</p>',
		'custom' : {
			'print' : '',
			'fn' : show_submit_form
		}
		
	},
	'buy-realestate' : {
		'desc' : '<p>How is your personal credit?  Your credit score is between</p>',
		'options' : {
			'400-500' : 're-640',
			'500-600' : 're-640',
			'600-640' : 're-640',
			'640-680' : 're640+',
			'680-700' : 're640+',
			'700+'    : 're640+'
		}
	},
	're-640' : {
		'desc' : '<p>We are sorry, all of our programs for start up businesses require a minimum 640 score.</p>'
	},
	're640+' : {
		'desc' : '<p>What is the total amount you need to buy commercial real estate?</p>',
		'custom' : {
			'print' : '$<input id="amount"><button onclick="process_amount(\'amount\', \'re$\', \'-125000\')">Continue</button><br>'
		}
	},
	're$out' : {
		'desc' : '<p>Our minimum investment amount requirements are $125,000.</p>'
	},
	're$in' : {
		'desc' : '<p>Our commercial real estate acquisition require a cash downpayment from the borrower that typically varies from 10% to 40% depending on a transaction.  Will you have such funds available?</p>',
		'options' : {
			'Yes' : 're-submit',
			'No'  : 're-no-funds'
		}
	},
	're-no-funds' : {
		'desc' : '<p>Unfortunately we do not have 100% financing loan programs for real estate at this point.  We are sorry, we are not able to help you at this time.</p>'
	},
	're-submit' : {
		'desc' : '<p>You might be a candidate for our  Commercial Real Estate Loan programs, provide the following contact information and one of our Business Finance Consultants will contact you to discuss further.</p>',
		'custom' : {
			'print' : '',
			'fn' : show_submit_form
		}
	}
};

function get_answer(step, nextstep) {
	var s = script_steps[step];
	var ns = script_steps[nextstep];
	if (nextstep.search(/\$/) != -1) {
		return '$' + theamount;
	}
	if (s && s.options) {
		for (o in s.options) {
			if (s.options[o] == nextstep)
				return o;
		}
	}
}

function show_submit_form() {
	document.getElementById('form').style.display = 'block';
	document.getElementById('options').style.display = 'none';
	var history = document.getElementById('history');
	
	history.value = '';
	for (var i = 0; i < steps_history.length - 1; i++) {
		var s = script_steps[steps_history[i]];
		var ns = script_steps[steps_history[i + 1]];
		history.value += s.desc + "\n";
		var ans = answers_history[i] || get_answer(steps_history[i], steps_history[i + 1]);
		history.value += '<p>&nbsp;&nbsp;&nbsp;&nbsp;<em>' + ans + "</em></p>\n\n";
	}
//	document.getElementById('form').innerHTML += history.value;//debug
}

var theamount = 0;
function process_amount(id, prefix, ranges) {
	var amount = document.getElementById('amount').value;
	amount = parseInt(amount.replace(/[^0-9]+/g, ''));
	theamount = amount;
	if (prefix == 'sb$') {
		if (amount < 220000)
			show_step(prefix + '-220000');
		else if (amount < 310000)
			show_step(prefix + '220000-310000');
		else if (amount < 2400000)
			show_step(prefix + '310000-2400000');
		else
			show_step(prefix + '-220000');
	} else if (prefix == 'bb$') {
		if (amount < 125000)
			show_step(prefix + '-125000')
		else if (amount < 310000)
			show_step(prefix + '125000-310000')
		else if (amount < 2400000)
			show_step(prefix + '310000-2400000')
		else
			show_step(prefix + '-125000');
	} else if (prefix == 'wc$') {
		if (amount < 100000)
			show_step(prefix + 'out');
		else if (amount < 2000000) 
			show_step(prefix + 'in')
		else 
			show_step(prefix + 'out');
	} else if (prefix == 'be$') {
		if (amount < 5000)
			show_step(prefix + 'out');
		else
			show_step(prefix + 'in');
	} else if (prefix == 're$') {
		if (amount < 125000)
			show_step(prefix + 'out');
		else
			show_step(prefix + 'in');
	} else if (prefix == 'rm$') {
		if (amount < 100000)
			show_step(prefix + 'out');
		else
			show_step(prefix + 'in');
	}
}

function go_back() {
	if (steps_history.length > 1) {
		steps_history.pop();
		answers_history.pop();
		document.getElementById('form').style.display = 'none';
		show_step(steps_history.pop());
	}
}

function process_textfield(step) {
	var s = script_steps[step];
	if (s) {
		var inp = document.getElementById('inp_' + s.textfield);
		show_step(s.nextstep, inp && inp.value);
	}	
}

var steps_history = [];
var answers_history = [];
function show_step(step, answer) {
	var s = script_steps[step];
	if (s) {
		if (steps_history.length > 0)
			answers_history.push(answer);
		steps_history.push(step);
		document.getElementById('desc').innerHTML = s['desc'] || '';
		var divopt = document.getElementById('options');
		divopt.innerHTML = '';
		if (s.options) {
			for (o in s.options) {
				divopt.innerHTML += '<button class="option" onclick="show_step(\'' + s.options[o] + '\', \'' + o + '\')">' + o + '</button><br>';
			}
		} else if (s.custom) {
			divopt.innerHTML = s.custom.print || '';
			s.custom.fn && 'function' == typeof s.custom.fn && s.custom.fn();
		} else if (s.textfield) {
			if (s.rows && s.cols) {
				divopt.innerHTML = (s.label || '') + '<textarea id="inp_' + s.textfield + '" cols="' + s.cols + '" rows="' + s.rows + '"></textarea><br><button onclick="process_textfield(\'' + step + '\')">Continue</button><br>'
			} else {
				divopt.innerHTML = (s.label || '') + '<input id="inp_' + s.textfield + '"><button onclick="process_textfield(\'' + step + '\')">Continue</button><br>'
			}
		}
		if (steps_history.length > 1) {
			divopt.innerHTML += '<button class="back" onclick="go_back()">Back to previous questions</button>';
		}
	}
}

