﻿function findTitlePremium(form) {

	var titlePremium = 0;

	// clean up policy amount
	policyAmt = chkNumeric(form.txtValue.value);
	if (policyAmt<0) {
		return false;
	}

	// calculate it
	if (policyAmt<100000) {
		alert ('Please input a number greater than $100,000');
	}
	else if (policyAmt==100000) {
		titlePremium = 843;
	}
	else if (policyAmt<=1000000) {
		titlePremium = Math.round((policyAmt-100000)*.00534) + 843;
	}
	else if (policyAmt<=5000000) {
		titlePremium = Math.round((policyAmt-1000000)*.00439) + 5649;
	}
	else if (policyAmt<=15000000) {
		titlePremium = Math.round((policyAmt-5000000)*.00362) + 23209;
	}
	else if (policyAmt<=25000000) {
		titlePremium = Math.round((policyAmt-15000000)*.00257) + 59409;
	}
	else {
		titlePremium = Math.round((policyAmt-25000000)*.00154) + 85109;
	}

	titlePremium = '$' + titlePremium;

	form.txtPremium.value=titlePremium;
}

function chkNumeric(chkNum)
{
	var checkOK = "0123456789" + "," + "$" + ".";
	var allNum = "";
	var allValid = true;

	for (i = 0;  i < chkNum.length;  i++) {

		// Look at each character
		ch = chkNum.charAt(i);

		// Make sure character is in checkOK string
		for (j = 0;  j < checkOK.length;  j++) {
			if (ch == checkOK.charAt(j)) { break; }
		}

		// If went passed entire string then must be invalid char
		if (j == checkOK.length) {
			allValid = false;
			break;
		}

		// Put all the number together
		if ( (ch!=',') && (ch!='$')) {
			allNum += ch;
		}
	}

	if (!allValid) {
		alertsay = "Please enter only numberic values.";
		alert(alertsay);
		return -1;
	} else {
		return allNum;
	}
}
