Event.observe(window, 'load', function() {
	$('s_all').style.display = 'none'; // Hide all initially
	$("s_simultnotice").style.display = 'none'; // Start by Hiding Simultaneous Notice
	$("s_simultnotice2").style.display = 'none'; // Start by Hiding Simultaneous Notice
	
	Event.observe('opt1_purchase', 'click', doPurchase);
	Event.observe('opt1_refinance', 'click', doRefinance);
	Event.observe('purchaseprice', 'blur', doBlurP);
	Event.observe('loanamount', 'blur', doBlurL);
	Event.observe('priorloan_yes', 'click', getPremiums);
	Event.observe('priorloan_no', 'click', getPremiums);
	
	// Set Default Option
	$('opt1_purchase').checked = true;
	doPurchase();
	
	// Set Default Focus
	$('purchaseprice').focus();
	
	// Set Default Prior Loan Value
	$('priorloan_no').checked = true;
	
	updateEndorsements();
});

// Global Vars
var mLoanType = '';
var mEndorsements = 0;
var mLenderCharge = 0;
var mOwnerCharge = 0;
var mOtherCharge = 0;
var mTotalCharge = 0;

function stripAlphaChars(pstrSource) { 
	var m_strOut = new String(pstrSource); 
	m_strOut = m_strOut.replace(/[^0-9/.]/g, ''); 

	return m_strOut; 
}

function doBlurP(){
	$('purchaseprice').value = stripAlphaChars($('purchaseprice').value);
	getPremiums();
}

function doBlurL(){
	$('loanamount').value = stripAlphaChars($('loanamount').value);
	getPremiums();
}

function doPurchase() {
	mLoanType = 'p';
	$('lblPurchase').style.textDecoration = "underline";
	$('lblRefinance').style.textDecoration = "";
	
	hideFields(true);
	mOtherCharge = feeOtherPurchase;
	$("courier_total").innerHTML = formatCurrency(feeOtherCourierPurchase);
	$('other_total').innerHTML = formatCurrency(feeOtherPurchase);
	updateTotals();
	getPremiums();
}

function doRefinance() {
	mLoanType = 'r';
	$('lblPurchase').style.textDecoration = "";
	$('lblRefinance').style.textDecoration = "underline";

	hideFields(false);
	mOtherCharge = feeOtherRefinance;
	$("courier_total").innerHTML = formatCurrency(feeOtherCourierRefinance);
	$('other_total').innerHTML = formatCurrency(feeOtherRefinance);
	updateTotals();
	getPremiums();
}

function doReset() {
	mLenderCharge = 0;
	mOwnerCharge = 0;
	updatePremiums();
}

function hideFields(isPurchase) {
	$('s_all').style.display = '';
	$('s_priorloan').style.display = hideToCss(isPurchase);
	$('s_purchaseprice').style.display = hideToCss(!isPurchase);
	$('li_searchfee').style.display = hideToCss(isPurchase);
	$('s_ownercharge').style.display = hideToCss(!isPurchase);
	$("s_simultnotice").style.display = 'none';
	$("s_simultnotice2").style.display = 'none';
}

function hideToCss(hide) {
	if (hide) {
		return "none";
	} else {
		return "";
	}	
}

function updateEndorsements() {
	var ct;
	ct = 0;
	
	if($('end_arm').checked) {
		$('lblArm').style.textDecoration = "underline";
		ct++;
	} else {
		$('lblArm').style.textDecoration = "";
	}
	
	if($('end_balloon').checked) {
		$('lblBalloon').style.textDecoration = "underline";
		ct++;
	} else {
		$('lblBalloon').style.textDecoration = "";
	}
				
	if($('end_manufactured').checked) {
		$('lblManufactured').style.textDecoration = "underline";
		ct++;
	} else {
		$('lblManufactured').style.textDecoration = "";
	}			
	
	mEndorsements = ct * feeEndorsement;
	updateTotals();
	$('endorsements_total').innerHTML = formatCurrency(mEndorsements);
}

function updatePremiums() {
	$('lender_total').innerHTML = formatCurrency(mLenderCharge);
	$('lender_2').innerHTML = formatCurrency(50);
	$('owner_total').innerHTML = formatCurrency(mOwnerCharge);
	$('owner_2').innerHTML = formatCurrency(mOwnerCharge + mLenderCharge - 50);
	updateTotals();
}

function updateTotals() {
	if (mLoanType == "p") {
		mTotalCharge = mEndorsements + mLenderCharge + mOwnerCharge + mOtherCharge;
	} else if (mLoanType == "r") {
		mTotalCharge = mEndorsements + mLenderCharge + mOtherCharge;
	}
	
	$('all_total').innerHTML = formatCurrency(mTotalCharge);
}

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g, '');
	if (isNaN(num)) num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num * 100 + 0.50000000001);
	cents = num % 100;
	num = Math.floor(num / 100).toString();
	if (cents < 10) cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
	num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
	
	return (((sign) ? '' : '-') + '$' + num + '.' + cents);
}

function getPremiums() {
	if($('priorloan_yes').checked) {
		$('lblPriorYes').style.textDecoration = "underline";
		$('lblPriorNo').style.textDecoration = "";
	} else if($('priorloan_no').checked) {
		$('lblPriorYes').style.textDecoration = "";
		$('lblPriorNo').style.textDecoration = "underline";
	}
	
	// Start by Hiding Simultaneous Notice
	$("s_simultnotice").style.display = 'none';
	$("s_simultnotice2").style.display = 'none';
	
	if($('opt1_purchase').checked){
		// Purchase
		if(!isNaN($('purchaseprice').value) && !isNaN($('loanamount').value)) {
			// Show Simultaneous Notice
			if($('loanamount').value > 0){
				$("s_simultnotice").style.display = '';
				$("s_simultnotice2").style.display = '';
			}
		
			if($('purchaseprice').value > 0) {// && $('loanamount').value > 0) {
				//if($('loanamount').value < $('purchaseprice').value) {
					ajaxUpdate('p', $('purchaseprice').value, $('loanamount').value, 'n');
				//} else {
				//	doReset();
				//	alert("The loan amount may not exceed the purchase price.");
				//}
			} else {
				// We don't have numbers!
				doReset();
			}
		}
		
	} else if($('opt1_refinance').checked) {
		// Refinance
		if(!isNaN($('loanamount').value)) {
			if($('loanamount').value > 0) {
				if($('priorloan_yes').checked) {
					ajaxUpdate('r', 0, $('loanamount').value, 'y');						
				} else if($('priorloan_no').checked) {
					ajaxUpdate('r', 0, $('loanamount').value, 'n');
				}
			} else {
				doReset();
			}
		} else {
			doReset();
		}
	}
}

function ajaxUpdate(loanType, purchAmt, loanAmt, payoff) {
	var ajaxUrl = 'ajax.asp?loantype=' + loanType + '&purchamt=' + purchAmt + '&loanamt=' + loanAmt + '&payoff=' + payoff;

	new Ajax.Request(ajaxUrl, {
		method: 'get',
		onSuccess: function(transport) {

			var res = transport.responseText;
	
			// Did we get a JSON response?
			if(res.isJSON()) {
				var jsonArr = res.evalJSON();

				mLenderCharge = jsonArr.pLender;
				mOwnerCharge = jsonArr.pOwner;

				updatePremiums();
			} else {
				// No JSON here!
			}
		}
	});
}