// Add Event Handlers
Event.observe(window, 'load', function() {
	$('s_all').style.display = 'none'; // Hide all initially
	Event.observe('opt1_purchase', 'click', doPurchase);
	Event.observe('opt1_refinance', 'click', doRefinance);
	Event.observe('loanamount', 'blur', doBlurL);
	Event.observe('purchaseprice', 'blur', doBlurP);
	//Event.observe('recording_pages', 'blur', getRecordingFees);

	//Event.observe('loc_state', 'change', setState);
	Event.observe('loc_county', 'change', getPremiums);
	Event.observe('policytype', 'change', getPremiums);
	
	// Set Default Option
	$('opt1_purchase').checked = true;
	doPurchase();
	
	// Set Default Focus
	$('purchaseprice').focus();
	
	// Set Default State / County
	//$('loc_state').value="MO";
	//setState();
	enableEndorsements(false); //Disable Endorsements Initially
	updateEndorsements();
});

// Global Vars
var mLoanType = '';
var mState = '';
var mCounty = '';

var mEndorsements = 0;
var mLenderCharge = 0;
var mOwnerCharge = 0;
var mOtherCharge = 0;
var mTotalCharge = 0;
var mConveyanceTax = 0;
var mRecordingFee = 0;

var mCplFee = 0;
var mSearchFee = 0;
var mFiledPremium = 0;

// Functions
function doBlurP(){
	$('purchaseprice').value = stripAlphaChars($('purchaseprice').value);
	getPremiums();
}

function doBlurL(){
	$('loanamount').value = stripAlphaChars($('loanamount').value);
	getPremiums();
	
	if($('loanamount').value > 0) {
		enableEndorsements(true);
	} else {
		enableEndorsements(false);
	}
}

function doPurchase() {
	mLoanType = 'p';
	$('lblPurchase').style.textDecoration = "underline";
	$('lblRefinance').style.textDecoration = "";
	
	//$('loanamount').value = '';
	
	hideFields(true);
	//mOtherCharge = feeOtherPurchase;
	//$('other_total').innerHTML = formatCurrency(feeOtherPurchase);
	updateTotals();
	getPremiums();
	
	$('purchaseprice').focus();
}

function doRefinance() {
	mLoanType = 'r';
	$('lblPurchase').style.textDecoration = "";
	$('lblRefinance').style.textDecoration = "underline";
	
	$('purchaseprice').value = '';

	hideFields(false);
	//mOtherCharge = feeOtherRefinance;
	//$('other_total').innerHTML = formatCurrency(feeOtherRefinance);
	updateTotals();
	getPremiums();
	
	$('loanamount').focus();
}

function doReset() {
	mLenderCharge = 0;
	mOwnerCharge = 0;
	mCplFee = 0;
	mSearchFee = 0;
	mFiledPremium = 0;
	updatePremiums();
}

function getCounty() {
	return mCounty;
}

/*
function getRecordingFees() {
	$("recording_pages").value = stripAlphaChars($("recording_pages").value);
	
	var pageCt = parseInt($("recording_pages").value);
	var recordingFee;
	var recordingFeeMinimum = 28;
	var recordingFeePerPage = 8;
	
	if (pageCt > 0) {
		recordingFee = recordingFeeMinimum;
		
		pageCt = pageCt - 2;
		
		if (pageCt > 0) {
			recordingFee = recordingFee + (pageCt * recordingFeePerPage);
		}
	} else {
		recordingFee = 0;
	}
	
	mRecordingFee = recordingFee;
	//$("recordingfee_total").innerHTML = formatCurrency(recordingFee);
	updatePremiums();
}
*/

function hideFields(isPurchase) {
	$('s_all').style.display = '';
	$('s_purchaseprice').style.display = hideToCss(!isPurchase);
	//$('s_loanamount').style.display = hideToCss(isPurchase);
	//$('li_searchfee').style.display = hideToCss(!isPurchase);
	$('s_ownercharge').style.display = hideToCss(!isPurchase);
	$('s_endorsements_all').style.display = hideToCss(!isPurchase);
	$("s_policytype").style.display = hideToCss(!isPurchase);
	$("refi_disclaimer").style.display = hideToCss(isPurchase);
}

function updateEndorsements() {
	var endorsementsTotal;
	endorsementsTotal = 0.0;
	
	var arrEndorsements = $("s_endorsements").getElementsByTagName("input");
	
	for (var i = 0; i < arrEndorsements.length; i++) {
		if(arrEndorsements[i].checked) {
			endorsementsTotal = endorsementsTotal + parseFloat(arrEndorsements[i].value);
		}
	}

	mEndorsements = endorsementsTotal;
	updateTotals();
	$('endorsements_total').innerHTML = formatCurrency(mEndorsements);
}

function enableEndorsements(enabled) {
	var arrEndorsements = $("s_endorsements").getElementsByTagName("input");
	
	for (var i = 0; i < arrEndorsements.length; i++) {
		if(i!=3) { // Always allow the 3rd endorsement to be checked (ICL endorsement)
			arrEndorsements[i].disabled = !enabled;
			
			if(!enabled) {
				arrEndorsements[i].checked = false;
			}
		}
	}
	
	updateEndorsements();
}

function updatePremiums() {
	$('lender_total').innerHTML = formatCurrency(mLenderCharge);
	$('owner_total').innerHTML = formatCurrency(mOwnerCharge);
	
	/*
		$('cplfee').innerHTML = formatCurrency(mCplFee);
		$('searchfee').innerHTML = formatCurrency(mSearchFee);
		$('filedpremium').innerHTML = formatCurrency(mFiledPremium);
	*/
	
	
	updateTotals();
}

function updateTotals() {
	if (mLoanType == "p") {
		mTotalCharge = mEndorsements + mLenderCharge + mOwnerCharge + mOtherCharge + mConveyanceTax + mRecordingFee;
	} else if (mLoanType == "r") {
		mTotalCharge = mLenderCharge + mOtherCharge + mRecordingFee;
	}
	
	$('all_total').innerHTML = formatCurrency(mTotalCharge);
}

function getPremiums() {
	if($('opt1_purchase').checked){
		// Purchase
		if($('purchaseprice').value > 500000) {
			alert("Please contact our local office for purchase prices exceeding $500,000.");
			$('purchaseprice').value = '';
			$('purchaseprice').focus();
			doReset();
			return;
		}
		
		if(!isNaN($('purchaseprice').value)) {
			if($('purchaseprice').value > 0) {
				if(parseInt($('loanamount').value) > parseInt($('purchaseprice').value)) {
					alert("Please contact our local office for simultaneous loan policies with the loan amount exceeding the purchase price.");
					$('loanamount').value = '';
					$('loanamount').focus();
					doReset();
					return;
				} else {
					ajaxUpdate('p', $('policytype').value, $('purchaseprice').value, $('loanamount').value, $('loc_county').value);
				}
			} else {
				// We don't have numbers!
				doReset();
			}
		}
	}
	else if($('opt1_refinance').checked) {
		// Refinance
		if($('loanamount').value > 500000) {
			alert("Please contact our local office for loan amounts exceeding $500,000.");
			$('loanamount').value = '';
			$('loanamount').focus();
			doReset();
			return;
		}
		
		if(!isNaN($('loanamount').value)) {
			if($('loanamount').value > 0) {
					ajaxUpdate('r', $('policytype').value, 0, $('loanamount').value, $('loc_county').value);
			} else {
				doReset();
			}
		} else {
			doReset();
		}
	}
}

function ajaxUpdate(loanType, policyType, purchAmt, loanAmt, county) {
	var ajaxUrl = 'ajax.asp?loantype=' + loanType + '&policytype=' + policyType + '&purchamt=' + purchAmt + '&loanamt=' + loanAmt + '&county=' + county;

	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!
			}
		}
	});
}
