// Business logic subject to periodic updates

function changeLoanType(lc, lt)
{	var objField = document.myForm.LoanType;
	var aOptionList;

	if (lc == 0)
	{	for (i=0; i < document.myForm.LoanCategory.length; i++) {
			if (document.myForm.LoanCategory[i].checked) {
				lc = i+1;
			}
		}
	}
	
	if (lc == 1) // Conforming
		aOptionList = new Array("1|30 Year Fixed Rate",
		                        "2|20 Year Fixed Rate",
		                        "3|15 Year Fixed Rate",
		                        "5|1 Year Adjustable Rate",
		                        "6|3 Year Adjustable Rate",
		                        "7|5 Year Adjustable Rate",
		                        "8|3/1 Adjustable Rate",
		                        "9|5/1 Adjustable Rate",
		                        "10|7/1 Adjustable Rate",
		                        "11|10/1 Adjustable Rate",
		                        "12|5/25 Balloon Program",
		                        "13|7/23 Balloon Program",
		                        "14|5/25 Two-Step Program",
		                        "15|7/23 Two-Step Program"
		                       );
	else if (lc == 2) // Jumbo
		aOptionList = new Array("1|30 Year Fixed Rate",
		                        "2|20 Year Fixed Rate",
		                        "3|15 Year Fixed Rate",
		                        "5|1 Year Adjustable Rate",
		                        "6|3 Year Adjustable Rate",
		                        "7|5 Year Adjustable Rate",
		                        "8|3/1 Adjustable Rate",
		                        "9|5/1 Adjustable Rate",
		                        "10|7/1 Adjustable Rate",
		                        "11|10/1 Adjustable Rate"
		                       );
	else if (lc == 3) // FHA
		aOptionList = new Array("1|30 Year Fixed Rate",
		                        "3|15 Year Fixed Rate",
		                        "5|1 Year Adjustable Rate"
		                       );
	else if (lc == 4) // VA
		aOptionList = new Array("1|30 Year Fixed Rate",
		                        "3|15 Year Fixed Rate"
		                       );
	else if (lc == 5)	//Less-than-perfect Credit
		aOptionList = new Array("1|30 Year Fixed Rate",
								"4|6 Month Adjustable Rate",
		                        "5|1 Year Adjustable Rate",
		                        "16|2/28 Adjustable Rate",
		                        "17|3/27 Adjustable Rate"
		                       );
	
	updateOptions(objField, aOptionList, 2);
	changePointBucket(lc, lt);
}

function changePointBucket(lc, lt)
{
	if (lt==0) 
		lt = document.myForm.LoanType.options[document.myForm.LoanType.selectedIndex].value;
	
	if (lc==0)
	{	for (i=0; i < document.myForm.LoanCategory.length; i++) {
			if (document.myForm.LoanCategory[i].checked) {
				lc = i+1;
			}
		}
	}
	
	// Determine which Point Bucket Type to display
	if((lc==1) || (lc==2)){
		if(lt>=6){
			pointBucketGroups(2);
		}
		else {
			pointBucketGroups(1);
		}
	}
	else if ((lc==3) || (lc==4)) {
		pointBucketGroups(3);
	}
	else if ((lc==5) || (lc==6) || (lc==7)) {
		pointBucketGroups(2);
	}
}

function pointBucketGroups(bt)
{	var objField = document.myForm.PointBucketID;
	var aOptionList

	//bt = Bucket Type
	if (bt == 1)
		aOptionList = new Array("1|Zero Points/Zero Costs", 
		                        "2|0 - 1",
		                        "3|1 - 2",
		                        "4|2 - 3"
		                       );
	else if (bt == 2)
		aOptionList = new Array("2|0 - 1",
		                        "3|1 - 2",
		                        "4|2 - 3",
		                        "5|3 +"
		                       );
	else
		aOptionList = new Array("2|0 - 1",
		                        "3|1 - 2",
		                        "4|2 - 3"
		                       );
		                       
	updateOptions(objField, aOptionList, 2);
	
}

function updateOptions(objField, aOptions, nStartItem)
{
	var aOptionItems;
	var nIndex;
	var sTest = "";

	// Get current selection
	var sSelectedText = objField.options[objField.selectedIndex].text;
	
	objField.length = aOptions.length + nStartItem;
	
	for (nIndex=0; nIndex<aOptions.length; ++nIndex)
	{	aOptionItems = aOptions[nIndex].split("|");
		objField.options[nIndex + nStartItem].value = aOptionItems[0];
		objField.options[nIndex + nStartItem].text = aOptionItems[1];
		sTest += aOptionItems[0] + ', ';
		if (aOptionItems[1] == sSelectedText)
			objField.options[nIndex + nStartItem].selected = true;
	}		
}

function maxConformingLoanAmount()
{	return 417000;
}

function getLoanAmountBucket(sAmount)
{	var nAmount = keepNumbers(sAmount);

	if (nAmount < 100000)
		return 1;
	else
		return 2;
}
	
    function checkLoanType()
	{	var lc = -1;
		for (i=0; i < document.myForm.LoanCategory.length; i++) {
			if (document.myForm.LoanCategory[i].checked) {
				lc = i+1;
			}
		}
		if(lc!=1){
			setLoanTypeAndPoints();
		}
	}


	function setLoanTypeAndPoints()
	{	var nLoanTypeID = 0;
		
		changeLoanType(getLoanCatID(), nLoanTypeID);
	}

	function getLoanCatID()
	{	var objElement;
		var nIndex=0; 
		var nLoanCatID;
		var lMaxConforming = maxConformingLoanAmount();
		
		objElement = document.myForm.LoanCategory;
		for (nIndex=0; nIndex<objElement.length; ++nIndex)
		{	if (objElement[nIndex].checked)
				nLoanCatID = objElement[nIndex].value;
		}
		if (nLoanCatID == 0)
		{	if (isPoorCredit())
				nLoanCatID = 5;
			else if (keepNumbers(document.myForm.txtLoanAmount.value) > lMaxConforming)
				nLoanCatID = 2;
			else
				nLoanCatID = 1;
		}
		return nLoanCatID;
	}

	function getCreditRateCode() 
	{	var objCRElement = document.myForm.optCreditRate;
		var sCRSelection="";
		var nIndex;

		if (objCRElement.type && objCRElement.type.indexOf("select") >= 0)
			sCRSelection = objCRElement.options[objCRElement.selectedIndex].value;
		else
		{	for(nIndex=0;nIndex<objCRElement.length;++nIndex)
			{	if (objCRElement[nIndex].checked)
				{	sCRSelection = objCRElement[nIndex].value;
					break;
				}
			}
		}
		
		return sCRSelection;
	}


	function isPoorCredit() 
	{	var sCRSelection= getCreditRateCode();
		var nSelectionID;

		nIndex = sCRSelection.indexOf(":");
		if (nIndex < 1) nIndex = 1;
		
		nSelectionID = sCRSelection.substring(0, nIndex);

		if (nSelectionID == 3 || nSelectionID == 4)
			return true;
		else
			return false;
	}

	function keepChars(sInput, sCharList)
	{	var n;
		var sNextChar;
		var sOut="";
		
		for(n=0;n<sInput.length;++n)
		{	sNextChar = sInput.substring(n, n+1);
			if (sCharList.indexOf(sNextChar) >= 0)
				sOut += sNextChar;
		}
		return sOut;
	}

	function keepNumbers(sInput)
	{	return keepChars(sInput, "0123456789");
	}
	
	function enterCurrency(objFld)
	{	var sAmount;
		sAmount = addCommas(keepNumbers(objFld.value, "$., "));
		if (sAmount.length)
			sAmount = "$" + sAmount;
		objFld.value = sAmount;
	}

	function addCommas(sInput)
	{	var n;
		var sNextChar;
		var sOut="";
		
		for(n=0;n<sInput.length;++n)
		{	sOut += sInput.substring(n, n+1);
			if (n<sInput.length-1 && !((sInput.length-n-1)%3))
				sOut += ",";
		}
		return sOut;
	}
	
	
	function validate_form(objForm) 
	{	var objElement = objForm.State;
		var nIndex;
		var nCatID;
		var bPoorCredit;
		var nAmount;
			if (objElement.options[objElement.selectedIndex].value=="0" )
			{
				alert("You must choose a State. ");
				objElement.focus();
				return false; 
			}
	
		/*	if (document.myForm.optLoanPurpose.options[objForm.optLoanPurpose.selectedIndex].value.length == 0) {
				alert("Please select a Loan Purpose. ");
				objForm.optLoanPurpose.focus();
				return false;
			}	
	
			objElement = objForm.optCreditRate;
			if (objElement && objElement.options)
			{	if (objElement.options[objElement.selectedIndex].value == "")
				{	alert("Please select an estimated credit rating. ");
					objElement.focus();
					return false;
				}
			}
	
			objElement = objForm.LoanType;
			if (objElement.options[objElement.selectedIndex].value=="0" )
			{
				alert("Please select a Loan Type. ");
				objElement.focus();
				return false;
			}
			
			objElement = objForm.PointBucketID;
			if (objElement.options[objElement.selectedIndex].value == "0") 
			{
				alert("Please selection a Point Range. ");
				objElement.focus();
			return false; 
			}
	
	*/		objElement = objForm.txtLoanAmount;
			nAmount = keepNumbers(objElement.value);
			if (nAmount.length == 0)
			{	alert("Please enter a loan amount. ");
				objElement.focus();
				return false;
			}
		/*	if (keepNumbers(objForm.txtPropValue.value).length == 0)
			{	alert("Please enter a property value. ");
				objForm.txtPropValue.focus();
				return false;
			} 
			if (objForm.optLoanPurpose.options[document.myForm.optLoanPurpose.selectedIndex].text.indexOf("Construction") == -1 && keepNumbers(document.myForm.txtPropValue.value) - nAmount < 0)
			{	alert("Loan amount can't exceed property value. ");
				objElement.focus();
				return false;
			}*/
			
		submit_form(objForm);
		return false;
	}
	
	function submit_form(frmSource)
	{	var frmSubmit = document.subForm;
		//var sCreditRatingCode = getCreditRateCode();
		
		// Set parameters and submit form
		frmSubmit.NumberOfPoints.value = 2;
		frmSubmit.GeoID.value = frmSource.State.value;
		frmSubmit.LoanType.value = 1;
		frmSubmit.txtLoanAmount.value = keepNumbers(frmSource.txtLoanAmount.value);
		frmSubmit.txtPropValue.value = ""; //keepNumbers(frmSource.txtPropValue.value);
		frmSubmit.optLoanPurpose.value = 1;//frmSource.optLoanPurpose.value;
		frmSubmit.LoanAmountBucketID.value = getLoanAmountBucket(frmSource.txtLoanAmount.value);
		frmSubmit.LoanCategory.value = 1;
		frmSubmit.optCreditRate.value = "Good";
		//	sCreditRatingCode.substring(sCreditRatingCode.indexOf(":")+1, sCreditRatingCode.length);
		                                             
		frmSubmit.submit();
	}
	
	function newWindow(name, nHeight) 
	{	var helpWin
		var nPos
		var nPos2

		helpWin = window.open(name, 'helpWin', 'width=460,height=' + nHeight + ',toolbar=no,scrollbars=yes,resizable=yes');
		helpWin.focus();
	}