// Add Event Handlers
Event.observe(window, 'load', function() {
	Event.observe('loc_county', 'change', setCounty);
	Event.observe('loanamount', 'blur', doBlurL);
	Event.observe('concurrent', 'click', doConcurrent);
	Event.observe('mailout', 'click', doMailout);

	$("s_fixedquote").style.display = hideToCss(true);
	
	updateTotals();
});

// Global Vars
var mCounty = '';
var mConcurrent = 0;

var mLenderTotal = 0;
	var mLenderCharge = 0;
	var mClosingFee = 0;
	var mFixedQuote = 0;
	var mOtherCharge = 0;

var mRecordingFee = 151.00;

var mTotalCharge = 0;


// Event Handlers
function fullReset() {
	$("loc_county").value = "Eagle";
	$('loanamount').value = "";
	setCounty();
	
	$("end_3").disabled = false;
	$("end_4").disabled = false;
	//doReset();
	//updateTotals();
}

function setCounty() {
	mCounty = $("loc_county").value;

	$('s_concurrent').style.display = hideToCss(mCounty!="Eagle" && mCounty!="Pitkin" && mCounty!="Garfield");
	$('s_mailout').style.display = hideToCss(mCounty!="Eagle");
	
	// Reset Mailout Flag
	mOtherCharge = 0;
	$("s_other").style.display = hideToCss(mCounty!="Eagle");
	//$("s_fixedquote").style.display = hideToCss(mCounty=="Summit" || mCounty=="Garfield" || mCounty=="Pitkin");
	$("mailout").checked = false;
	
	// Show Endorsements for Summit
	$("s_endorsements_all").style.display = hideToCss(mCounty!="Summit");
	
	if(mCounty=="Summit") {
		$("s_closingfee").className = "tHeader2";
		$("s_lendercharge").className = "tHeader2";
		$("s_recordingfee").className = "tHeader2";
	} else {
		$("s_closingfee").className = "tHeader";
		$("s_lendercharge").className = "tHeader";
		$("s_recordingfee").className = "tHeader";
	}
	
	getPremiums();
}

function doBlurL(){
	$('loanamount').value = stripAlphaChars($('loanamount').value);
	getPremiums();
}

function doConcurrent() {
	if($("concurrent").checked) {
		$('lblConcurrent').style.textDecoration = "underline";
		mConcurrent = 1;
	} else {
		$('lblConcurrent').style.textDecoration = "";
		mConcurrent = 0;
	}
	
	getPremiums();
}

function doMailout() {
	if($("mailout").checked) {
		$('lblMailout').style.textDecoration = "underline";
		mOtherCharge = 17.50;
	} else {
		$('lblMailout').style.textDecoration = "";
		mOtherCharge = 0.00;
	}

	updateTotals();
}

function getEndorsements() {
	var arrEndorsements = $("s_endorsements").getElementsByTagName("input");
	var arrValues = "";
	
	for(i=0;i<arrEndorsements.length;i++) {
		if(arrEndorsements[i].checked) {
			if(arrValues!="") {
				arrValues = arrValues + "|";
			}
			arrValues = arrValues + arrEndorsements[i].value;
		}
	}
	
	return arrValues;
}

function updateEndorsements(e) {
	if(e.id=="end_3" || e.id=="end_4"){
		$("end_3").disabled = false;
		$("end_4").disabled = false;

		if(e.value==3 && e.checked) {
			$("end_4").checked = false;
			$("end_4").disabled = true;
		} else if(e.value==4 && e.checked) {
			$("end_3").checked = false;
			$("end_3").disabled = true;
		}
	}
	
	getPremiums();
}

// Functions
function updateTotals() {
	// Lender Policy & Closing Fees
	$("lender_total").innerHTML = formatCurrency(mLenderCharge);
	$("closingfee_total").innerHTML = formatCurrency(mClosingFee);
	$("fixedquote_total").innerHTML = formatCurrency(mFixedQuote);
	$("other_total").innerHTML = formatCurrency(mOtherCharge);
		$("mailout_total").innerHTML = formatCurrency(mOtherCharge);
	
	mLenderTotal = mLenderCharge + mClosingFee + mFixedQuote + mOtherCharge;
	$("lenderclosing_total").innerHTML = formatCurrency(mLenderTotal);

	// Recording Fee
	$("recording_total").innerHTML = formatCurrency(mRecordingFee);
	
	// Total of All Charges
	mTotalCharge = mLenderTotal + 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 > 1000000) {
			alert("Please contact our local office for loan amounts exceeding $1,000,000.");
			$('loanamount').value = '';
			$('loanamount').focus();
			//doReset();
			return;
		}
		
		if(!isNaN($('loanamount').value)) {
			if($('loanamount').value >= 0) {
					ajaxUpdate($('loc_county').value, $('loanamount').value, mConcurrent, getEndorsements());
			} else {
				//doReset();
			}
		} else {
			//doReset();
		}
		
		/*
	}
	*/
}

function ajaxUpdate(county, loanAmt, concurrent, endorsements) {
	var ajaxUrl = 'ajax.asp?county=' + county + '&loanamt=' + loanAmt + '&concurrent=' + mConcurrent + '&endorsements=' + endorsements;

	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.Lender;
					mClosingFee = jsonArr.FixedQuote;
					//mClosingFee = jsonArr.ClosingFee;
					//mFixedQuote = jsonArr.FixedQuote;
					

					updateTotals();
			} else {
				// No JSON here!
			}
		}
	});
}









/*
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();
	updateTotals();
}


/*
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++) {
		arrEndorsements[i].disabled = !enabled;
		
		if(!enabled) {
			arrEndorsements[i].checked = false;
		}
	}
	
	updateEndorsements();
}
*/

/*
function doBlurP(){
	$('purchaseprice').value = stripAlphaChars($('purchaseprice').value);
	getPremiums();
}


*/

/*
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();
}
*/




//var mLoanType = '';
//var mState = '';
//var mEndorsements = 0;
//var mOwnerCharge = 0;
//var mConveyanceTax = 0;


	
	
	
	
	
	
	//	$('s_all').style.display = 'none'; // Hide all initially	
	//	Event.observe('purchaseprice', 'blur', doBlurP);
	//Event.observe('recording_pages', 'blur', getRecordingFees);

	//Event.observe('loc_state', 'change', setState);
	
	//	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();
	
	
	
/*
var mCplFee = 0;
var mSearchFee = 0;
var mFiledPremium = 0;
*/
