function preparePage() {
  prepareNavbar('calc', 'c_qual');
}
function doCalc() {
  return doCalcOnForm(MM_findObj("qualForm"));
}
function calculator1() {
        var x = format_Number(MM_findObj("qualForm").txtFinancing1.value, 2);
        if (Number(x) > 1000) { command("calc", x); } else { alert("Please check the data"); }
}
function calculator2() {
        var x = format_Number(MM_findObj("qualForm").txtFinancing2.value, 2);
        if (Number(x) > 1000) { command("calc", x); } else { alert("Please check the data"); }
}
function calculator3() {
        var x = format_Number(MM_findObj("qualForm").txtFinancing3.value, 2);
        if (Number(x) > 1000) { command("calc", x); } else { alert("Please check the data"); }
}
function command(cmd, key) { 
    var fm=MM_findObj("qualForm"); 
    if (cmd) { fm.cmd.value = cmd; } 
    if (key) { fm.key.value = key; } 
    fm.submit(); 
}

//------------------------------------------------------------------------------

function setSelectValue(sel_obj, sel_val) {
  var opt = sel_obj.options;
  for (var i=0; i<opt.length; i++) {
    if (opt[i].value == sel_val) {
      opt[i].selected = true;
	  return;
    }
  }
}

//------------------------------------------------------------------------------

function getSelectValue(sel_obj) {
  var ind = sel_obj.options.selectedIndex;
  if (ind < 0) { return null; }
  var val = sel_obj.options[ind].value;
  return val;
}

//------------------------------------------------------------------------------

function setRadioValue(r_obj, sel_val) {
  for (var i=0; i<r_obj.length; i++) {
    if (r_obj[i].value == sel_val) {
      r_obj[i].checked = true;
	  return;
    }
  }
}

//------------------------------------------------------------------------------

function getRadioValue(r_obj) {
  for (var i=0; i<r_obj.length; i++) {
    if (r_obj[i].checked) {
	  return r_obj[i].value;
    }
  }
  return null;
}

//------------------------------------------------------------------------------

function setCheckboxValue(cb_obj, sel_val) {
  if (cb_obj.value == sel_val) {
    cb_obj.checked = true;
  }
}

//------------------------------------------------------------------------------

function getCheckboxValue(cb_obj) {
  if (cb_obj.checked) {
    return cb_obj.value;
  }
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

function formatRealNum(theNum,decplaces) {
// *** STOP USING THIS ***
// USE format_Real
// FORMAT A REAL NUMBER TO THE DESIRED DECIMAL PLACE
	var str = Math.round(parseFloat(filterNum(theNum.value)) * Math.pow(10,decplaces));
	str = ""+str;
	while (str.length <= decplaces) {
		str = "0" + str;
	}
	var decpoint = str.length - decplaces;
	return str.substring(0,decpoint) + "." + str.substring(decpoint,str.length);
}

//------------------------------------------------------------------------------

function formatInteger(theNum) {
// *** STOP USING THIS ***
// USE format_Integer
// FORMAT AN INTEGER
	var str = Math.round(parseFloat(filterNum(theNum.value)));
	str = ""+str;
	return str;
}

//------------------------------------------------------------------------------

function formatNum(theNum,decplaces,addcommas) {
// *** STOP USING THIS ***
// USE format_NUMBER
// FORMAT NUMERIC DATA

	var str = ""+theNum.value;
	if ((str == "") || (str == "null")) {
		theNum.value = "";
		return false;
	}
	if (decplaces == 0) {
		var fmtdnum = formatInteger(theNum,decplaces);
	} else {
		var fmtdnum = formatRealNum(theNum,decplaces);
	}
	if (addcommas) {
		fmtdnum = commaFmt(fmtdnum);
	}
	theNum.value = fmtdnum;
	return true
}

//------------------------------------------------------------------------------

function popupIsEmpty(theMenuObj) {
	var theSelection = theMenuObj.selectedIndex;
	if (theSelection <= 0) {
		return true;
	} else {
		return false;
    }
}

//------------------------------------------------------------------------------

function scrub_RollNum(theNum,otherAllowed,minWildcardPosition) {
// REMOVE ALL NON-NUMERIC CHARS EXCEPT FOR PROGRAMMER SPECIFIED 'OTHER' ALLOWABLE CHARS
	var result = "";
	var allowed = "";
	otherAllowed +="";
	if (otherAllowed == 'undefined')
		otherAllowed = "";
	allowed = "0123456789"+ otherAllowed;

	result = scrub_String(theNum,allowed);
	
	// remove commas from the end of the string
	for(i=result.length-1; i>=0; i--) {
		if(result.charAt(i) != ",")
			break;
		result = result.substring(0,i);
	}

	if (minWildcardPosition)
		result = parse_Wildcards (result,minWildcardPosition);

	return result;
}

//------------------------------------------------------------------------------

function scrub_String(theStr,charsAllowed,charsAllowedOnce) {
	// eliminate all unwanted chars
	var result = "";
	var specialCharAt = -1;
	var specialChar = "";
	for(i=0;i<=theStr.length;i++) {
		theChar = theStr.charAt(i);
		if (charsAllowed.indexOf(theChar) != -1)
 			result +=theChar;
 		else if (charsAllowedOnce){
 			specialCharAt = charsAllowedOnce.indexOf(theChar);
 		 	if (specialCharAt != -1) {
 				result += theChar;
 				// remove the char from the list so it will not be allowed
				charsAllowedOnce = charsAllowedOnce.substring(0,specialCharAt) 
					+ charsAllowedOnce.substring(specialCharAt+1,charsAllowedOnce.length);
// specialChar = charsAllowedOnce.charAt(specialCharAt-1);      
// charsAllowedOnce = charsAllowedOnce.replace(specialChar,""); 
 			}
 		}
	}
	return result;
}

//------------------------------------------------------------------------------

function filterNum(theNum) {
	var minusStr = "";
	var result = "";

	if (theNum.indexOf("-") != -1)
		minusStr = "-";

 	result = scrub_String(theNum,"0123456789",".")

 	if (result == "")
 		return "";
	else
		return minusStr + result;
}

//------------------------------------------------------------------------------

function commaFmt(numEle) {
	var tempStr = ""+numEle;
	
	// already has commas
	var charCheck = tempStr.indexOf(",");
	if ((charCheck+0) >= 0) {
		return numEle;
	}

	// separate the decimal from the whole number
	var decStr = "";
	var decInt = tempStr.indexOf(".");
	if (decInt!=-1) {
		decStr = tempStr.substring(decInt,tempStr.length);
		tempStr = tempStr.substring(0,decInt);
	}

	// if negative - save sign
	var isNeg = false;
	if (tempStr.indexOf("-")!=-1) {
		isNeg=true;
        tempStr=tempStr.substring(1,tempStr.length);
	}

	// short - no commas needed
	if (tempStr.length<=3) { 
		return numEle;
	}

	// add commas
	var newStr = "";
	var jInt = 0;
	for (var iInt=tempStr.length-1;iInt>=0;iInt--) {
		jInt++;
		newStr = tempStr.charAt(iInt) + newStr;
		if (jInt%3==0) {
			if (iInt-1>=0) {
				newStr = ","+newStr;
			}
		}
	}

	// re-assemble the parts
	if (decInt!=-1)
		newStr = newStr + decStr;
	if (isNeg)
		newStr = "-"+newStr;

	return newStr;
}

//------------------------------------------------------------------------------

function format_Real(theNum,decplaces) {
// FORMAT A REAL NUMBER TO THE DESIRED DECIMAL PLACE
	var str = filterNum(theNum);
	str = Math.round(parseFloat(str) * Math.pow(10,decplaces));
	str = ""+str;
	while (str.length <= decplaces) {
		str = "0" + str;
	}
	var decpoint = str.length - decplaces;
	return str.substring(0,decpoint) + "." + str.substring(decpoint,str.length);
}

//------------------------------------------------------------------------------

function format_Integer(theNum) {
// FORMAT AN INTEGER
	var str = filterNum(theNum);
	str = Math.round(parseFloat(str));
	return ""+str;
}

//------------------------------------------------------------------------------

function format_Number(theNum,decplaces,addcommas) {
// FORMAT NUMERIC DATA
	var str = ""+theNum;
	if ((str == "") || (str == "null"))
		return "";

	if (decplaces == 0)
		var fmtdnum = format_Integer(str,decplaces);
	else
		var fmtdnum = format_Real(str,decplaces);
		
	if (addcommas) 
		fmtdnum = commaFmt(fmtdnum);
//	if (isNaN(fmtdnum))
//		return ""; 
	return fmtdnum;
}

//------------------------------------------------------------------------------

function validateNum(theNum,decplaces,min,max,addcommas) {
// VALIDATE & FORMAT USER INUPT
	var str = filterNum(theNum.value);
	if ((str == "") || (str == "null")) {
		theNum.value = "";
		return false;
	}
	var tmpFloat = parseFloat(filterNum(theNum.value));
	
	if (tmpFloat < min || tmpFloat > max) {
		alert("Please enter a number between " + min + " and " + max + ".");
		theNum.value = "";
		theNum.focus();
		return false;
	}
	if (decplaces == 0) {
		var fmtdnum = formatInteger(theNum,decplaces);
	} else {
		var fmtdnum = formatRealNum(theNum,decplaces);
	}
	if (addcommas) {
		fmtdnum = commaFmt(fmtdnum);
	}
	theNum.value = fmtdnum;
	return true
}

//------------------------------------------------------------------------------

function validatePrice(theNum,min,max,addcommas) {
// VALIDATE & FORMAT USER INUPT
	var str = filterNum(theNum.value);
	if ((str == "") || (str == "null")) {
		theNum.value = "";
		return false;
	}
	var tmpFloat = parseFloat(filterNum(theNum.value));
	
	if (tmpFloat < min || tmpFloat > max) {
		alert("Please enter a number between " + min + " and " + max + ".");
		theNum.value = "";
		theNum.focus();
		return false;
	}
	if (tmpFloat < 100) {
		decplaces = 2;
	} else {
		decplaces = 0;
	}
	
	if (decplaces == 0) {
		var fmtdnum = formatInteger(theNum,decplaces);
	} else {
		var fmtdnum = formatRealNum(theNum,decplaces);
	}
	if (addcommas) {
		fmtdnum = commaFmt(fmtdnum);
	}
	theNum.value = fmtdnum;
	return true
}

//------------------------------------------------------------------------------

// SUBMIT VALIDATION

//------------------------------------------------------------------------------

function str_Empty (theFormObj){
	if (theFormObj.value+"" == "null")
		return true;
	else if (theFormObj.value == "")
		return true;
	return false;
}

//------------------------------------------------------------------------------

function popup_Empty(theMenuObj) {
	var theSelection = theMenuObj.selectedIndex;
	if (theSelection == -1)
		return true;
	else if (theMenuObj.options[theSelection].value == '')
		return true;
	else
		return false;
}

//------------------------------------------------------------------------------

function field_Empty (theFormObj){
	if (!theFormObj) {
		//alert("Please report this to systems support.\nUNKOWN FORM OBJECT"+theFormObj);
		//return false;
		return false;
	}
//	alert("field_Empty: " + theFormObj.name + " / " + theFormObj.type);
	var theType = theFormObj.type;
	if (theType.indexOf('select') != -1)
		return popup_Empty(theFormObj)
	else
		return str_Empty (theFormObj)
}

//------------------------------------------------------------------------------

function list_Empty(theMenuObj) {
	var isEmpty = true;
	for(i=1;i<theMenuObj.length;i++) {
		if (theMenuObj.options[i].selected)
			isEmpty = false;
	}
	return isEmpty;
}

//------------------------------------------------------------------------------

function numberRange_OK(fromNumObj,toNumObj)
{
	if (fromNumObj.value == "" || toNumObj.value == "")
		return true;
		
	var tmpFromFloat = parseFloat(filterNum(fromNumObj.value));
	var tmpToFloat = parseFloat(filterNum(toNumObj.value));
	if (tmpToFloat < tmpFromFloat) {
		alert(toNumObj.name+"  is greater than  "+toNumObj.name+"   Please re-enter");
		fromNumObj.value = "";
		toNumObj.value = "";
		fromNumObj.focus();
		return false;
	}
	return true;
}

//------------------------------------------------------------------------------

function wildcard_Alert () {
	alert('Wildcard Search Enabled\n  "?" - Searches for any single character.\n'
		+ '  "*" - Searches for any characters of any length.');
}

//------------------------------------------------------------------------------

function scrub_multi_value_field(fieldContents,minWildcardPosition) {
// Scrub fields that allow for multiple comma delimited values for queries
	var result = "";
	if (fieldContents == "" || fieldContents=='undefined')
		return "";
	if (minWildcardPosition == "" || minWildcardPosition=='undefined')
		minWildcardPosition = 0;

	fieldContents = scrub_Text (fieldContents,true);
	if (fieldContents.indexOf(",") == -1) {
			result = parse_Wildcards (fieldContents,minWildcardPosition);
	}
	else {
		var fieldContents_Array = fieldContents.split(",");
	
		for(var sc_x=0;sc_x < fieldContents_Array.length; sc_x++) {
	  		if (fieldContents_Array[sc_x] == "") 
	  			continue;
	  		if(fieldContents_Array[sc_x].charAt(0) == " ")
				fieldContents_Array[sc_x] = fieldContents_Array[sc_x].substring(1,fieldContents_Array[sc_x].length);
	  		if(fieldContents_Array[sc_x].charAt(fieldContents_Array[sc_x].length - 1) == " ")
				fieldContents_Array[sc_x] = fieldContents_Array[sc_x].substring(0,fieldContents_Array[sc_x].length - 1);
			result = result + parse_Wildcards (fieldContents_Array[sc_x],minWildcardPosition);
			result = result + ",";
		}
		// remove commas from the end of the string
		for(sc_i=result.length-1; sc_i>=0; sc_i--) {
			if(result.charAt(sc_i) != ",")
				break;
			result = result.substring(0,sc_i);
		}
	}
	return result;
}

//------------------------------------------------------------------------------

// onChange='return scrub_select_mult(this, 5);' //
function scrub_select_mult(elem, maxn) { 
	var valid = true;
	var cnt = 0;
	for (var i=0; i<elem.options.length; i++) {
		if (valid) {
			if (elem.options[i].selected) { 
				cnt ++;
				if (cnt > maxn) { 
					valid = false;
					alert("You can select only " + maxn + " items in the list"); 
					elem.options[i].selected = false; 
				}
			}
		} else {
			elem.options[i].selected = false; 
		}
	}
	return valid; 
}

//------------------------------------------------------------------------------

// Postal Code Check and Scrub space
function checkPostal(theObject,removeSpace) {
	var a = theObject.value;
	var b = "";
	var u = "";
	var numString="1234567890";
	var failed = false;

	a = scrub_Text(a,true);
	a = scrub_String_Unwanted(a," ");
	if (a.length < 1)
		return true;
		
	for(i=0;i<=5;i++) {
		var u = a.charAt(i);
		
		if (i==1||i==3||i==5) {
			if (!isNum(u)) 
				failed=true;
		} 
		else {
			if (isChar(u)) 
				u=u.toUpperCase();
			else 
				failed=true;
		}
		var b = b + u;
	}
	if (!removeSpace)
	  b = b.substring(0,3)+" "+b.substring(3,6);

	theObject.value = b;

	return !(failed);
}

//------------------------------------------------------------------------------

function isNum(inString)  {
	if(inString.length!=1) 
		return false;
	var refString="1234567890";
	if (refString.indexOf(inString,0) == -1) 
		return false;
	return true;
}

//------------------------------------------------------------------------------

function isChar(inString)  {
	if(inString.length!=1) 
		return false;
	var refString="abcdefghijklmnopqrstuvwxyzABCEDFGHIJKLMNOPQRSTUVWXYZ";
	if (refString.indexOf(inString,0) == -1) 
		return false;
	return true;
}

//------------------------------------------------------------------------------

// USED FUNCTIONS

//------------------------------------------------------------------------------

function replace_Char (unwantedChar,wantedChar,theStr) {
	while ((x=theStr.indexOf(unwantedChar)) != -1) {
		theStr = theStr.substring(0,x) + wantedChar 
			+ theStr.substring(x+unwantedChar.length,theStr.length);
	}	
	return theStr;
}

//------------------------------------------------------------------------------

function scrub_String_Unwanted(theStr,charsUnwanted) {
	// eliminate all unwanted chars
	var result = "";
	var theChar = "";
	var ok = true;
	var i = 0;
	for(i=0;i<=theStr.length;i++) {
		theChar = theStr.charAt(i);
		if (theChar == '"') // really bad ones
			result += "`"
		else if (theChar == "'")
			result += "`"
//		else if (theChar == "`")
//			theChar = ""
		else if (charsUnwanted.indexOf(theChar) == -1)
 			result += theChar;
	}
	return result;
}

//------------------------------------------------------------------------------

function parse_Wildcards (theStr,minWildcardPosition) {
	theStr = scrub_String_Unwanted(theStr,"%_");
	if (minWildcardPosition == 0) {
		if (theStr.indexOf("*") != -1 || theStr.indexOf("?") != -1 ) {
			alert("Wildcards are not allowed in this field.");
			theStr = replace_Char("*","",theStr);
			theStr = replace_Char("?","",theStr);
		}
	} else {
		var tmpStr = theStr.substring(0,minWildcardPosition);
		if (tmpStr.indexOf("*") != -1 || tmpStr.indexOf("?") != -1 ) {
			alert("A wildcard can only be used after character " + minWildcardPosition 
				+ " in this field.");
			theStr = replace_Char("*","",theStr);
			theStr = replace_Char("?","",theStr);
		} else {
			var x = theStr.indexOf("*");
			if (x != -1) {
				tmpStr = theStr.substring(x+1,theStr.length);
				if (tmpStr.indexOf("*") != -1 ) {
					alert("Only one * wildcard is allowed in this field.");
					theStr = replace_Char("*","",theStr);
					theStr = replace_Char("?","",theStr);
				}
			}
		}
	}
	return theStr;
}

//------------------------------------------------------------------------------

function scrub_Text (theStr,makeUpper,minWildcardPosition,allowQuotes) {
// strip spaces off ends of string
	theStr += "";
	if (theStr == "null" || theStr == "")
		return "";
	for(var i=0; i<theStr.length; i++) {
		if(theStr.charAt(0) != " ")
			break;
		theStr = theStr.substring(1,theStr.length);
	}
	for(i=theStr.length-1; i>=0; i--) {
		if(theStr.charAt(i) != " ")
			break;
		theStr = theStr.substring(0,i);
	}

	var perc = "";
	if (makeUpper) {
		theStr = theStr.toUpperCase();
		perc = "%_";
	}
	if (allowQuotes != 'true')
		theStr = scrub_String_Unwanted(theStr,perc);

	if (minWildcardPosition && minWildcardPosition > '')
		theStr = parse_Wildcards (theStr,minWildcardPosition);

	return theStr;
}

//------------------------------------------------------------------------------

// Date & Time Entry and Formating
// ANK - May, 1999

//------------------------------------------------------------------------------

function format_date(d) {
	if (!d.getTime())  return "Invalid Date";
	var y = d.getFullYear();
	var m = d.toString().substring(4,7);
	var a = d.getDate(); 
	return a + "-" + m.toUpperCase() + "-" + y;
//	return m + " " + a + ", " + y;
}

//------------------------------------------------------------------------------

function format_time(d) {
	if (!d.getTime())  return "Invalid Time";
	var h = d.getHours();    h = (h<10)?("0"+h):(""+h);
	var m = d.getMinutes();  m = (m<10)?("0"+m):(""+m);
	var s = d.getSeconds();  s = (s<10)?("0"+s):(""+s);
	return h + ":" + m + ":" + s;
}

//------------------------------------------------------------------------------

/*
function join_date(date, time, type, def_date) {
	if ((!date)&&(!time)) return "";

	if (!date) { 
		var d;
		if (def_date) d = new Date(def_date);
			else      d = new Date(); 
		date = format_date(d); 
	}
	if (!time) {
		switch (type) {
			case "start":   time = "00:00:00"; break;
			case "end":     time = "23:59:59"; break;
		    case "current": time = format_time(new Date()); break;
		    case "default": time = format_time(new Date(def_date)); break;
		    default:        return "";
		}
	}
	return date + "  " + time;
}
*/

//------------------------------------------------------------------------------

function scrub_Date(theStr, def_date) {
	var str = String(theStr);
	if ((!str)||(str == "")) { return ""; }

	var p;
	while((p=str.indexOf("-")) >= 0)
		str = str.substring(0,p) + " " + str.substring(p+1,str.length);

	var c;
	if (def_date) c = new Date(def_date);
		else      c = new Date();
	var d = new Date(str);
	if (!d.getTime()) { d = new Date(str + " " + c.getFullYear()); }
	if (!d.getTime()) {	d = new Date((c.getMonth()+1) + "/" + str + " " + c.getFullYear()); }
	if (!d.getTime()) { return ""; }
    var x = c.getFullYear();
	var y = d.getFullYear();
	if (y < x-10) { d.setFullYear(x-10); }
	if (y > x+10) { d.setFullYear(x+10); }
	return format_date(d);
}

//------------------------------------------------------------------------------

function date_format_help() {
    alert('Date: \n \n'
		+ 'Please use one of the following formats:\n'
		+ '"31-MAY-1999" or\n'
		+ '"May 31 1999", "05/31/1999", "May 31", "31"');
}

//------------------------------------------------------------------------------

function scrub_Time(theStr) {
	var str = String(theStr);
	if ((!str)||(str == "")) { return ""; }
	
	var p, pm="";
	str = str.toUpperCase();
	if ((p=str.indexOf("PM")) >= 0) { pm = "PM"; str = str.substring(0,p); }
	if ((p=str.indexOf("AM")) >= 0) { pm = "AM"; str = str.substring(0,p); }

	while((p=str.indexOf(" :")) >= 0)
		str = str.substring(0,p) + str.substring(p+1,str.length);
	while(str.charAt(str.length-1) == " ")
		str = str.substring(0,str.length-1);

	var d = new Date("Jan 1, 1970 " + str + " " + pm);
	if (!d.getTime()) { d = new Date("1/1/1970 " + str + ":00" + " " + pm); }
	if (d.getTime()) {
		if ((pm=="AM") && (d.getHours()==12)) d.setHours(0);
		if ((pm=="PM") && (d.getHours()==0)) d.setHours(12);
		return format_time(d);
	} else {
		alert("Invalid time: " + theStr +"\n \n"
			+ "Please use one of the following formats:\n"
			+ "14:15:00,  14:15,  14  or  2 PM");
		return "";
	}
}

//------------------------------------------------------------------------------
//------ New style Functions ---------------------------------------------------
//------------------------------------------------------------------------------

function _not_ready(elem, msg) {
	elem.form._relem = elem;
	elem.form._ready = false;
	setTimeout("alert('" + msg + "');", 1);
}

//------------------------------------------------------------------------------

function try_to_submit(fm) {
	for (var i=0; i<fm.length; i++) {
		if (fm.elements[i].blur) { 
			fm.elements[i].blur(); // triggers onChange() before submit() (for IE)
		} 
	}
	if (!fm._ready) {
		fm._relem.focus();
		return;
	}
	fm.submit();
}

//------------------------------------------------------------------------------

function scrubDate(elem) {
	var x = scrub_Date(elem.value);
	if (x == '' && elem.value != '') {
		_not_ready(elem, 'Invalid date format: "' + elem.value +'"\\n \\n'
			+ 'Please use one of the following formats:\\n'
			+ '"31-MAY-1999" or\\n'
			+ '"May 31 1999", "05/31/1999", "May 31", "31"');
	}
	elem.value = x;
}

//------------------------------------------------------------------------------

function scrubText(elem) {
	elem.value = scrub_Text(elem.value,false);
}

//------------------------------------------------------------------------------

function scrubUpperText(elem) {
	elem.value = scrub_Text(elem.value,true);
}

//------------------------------------------------------------------------------
    
function scrubLongText(elem, numb) {
    numb = Number(numb);
	var res = scrub_Text(elem.value, false);
	if (res.length > numb) { 
		res = res.substring(0,numb); 
		_not_ready(elem, 'The text was longer than '+ numb +' characters. It has been truncated.'); 
	}
    elem.value = res;
}

//------------------------------------------------------------------------------

<!--SCRIPT LANGUAGE="JAVASCRIPT"-->

function clearColumn(form, nColumn) {
        form[("txtDownPayment" + nColumn)].value = "";
        form[("txtFirstMortgage" + nColumn)].value = "";
        form[("txtCMHC" + nColumn)].value = "";
        form[("txtFinancing" + nColumn)].value = "";
        form[("txtPI" + nColumn)].value = "";
        form[("txtExpenses" + nColumn)].value = "";
        form[("txtTotal" + nColumn)].value = "";
        form[("txtIncome" + nColumn)].value = "";
}

function clearResults(form) {
        var nCounter = 0;
        for (nCounter = 1; nCounter <=3; nCounter++) {
                clearColumn(form, nCounter);
        }
}

function checkForm(form) {
        var nCounter = 0;
        var sResult = 0;
        var nResult = -1;
        var nIndex = 0;
        for (nCounter = 1; nCounter <= 3; nCounter++) {
                if ((form[("cboPercentDown" + nCounter)].options[form[("cboPercentDown" + nCounter)].selectedIndex].value == "") || 
                        (form[("cboPercentDown" + nCounter)].options[form[("cboPercentDown" + nCounter)].selectedIndex].value == "other")) {
                        while ((nResult < 5) || (nResult > 100)) {
                                sResult = prompt(decodeMsg("DOWNPAYMENT_PERCENT"), "5");
                                if (sResult == null) {
                                        alert("DOWNPAYMENT_REQUIRED");
                                        sResult = "0";
                                } else {
                                        if (sResult.indexOf(".") > 0) sResult = sResult.substring(0, sResult.indexOf(".") + 3);
                                        nResult = Number(filterNum("0" + sResult));
                                        if ((nResult < 5) || (nResult > 100)) {
                                                alert("INVALID_DOWNPAYMENT")
                                        }
                                }
                        }
        
                        if (form[("cboPercentDown" + nCounter)].options[form[("cboPercentDown" + nCounter)].selectedIndex].value == "other") {
                                nIndex = form[("cboPercentDown" + nCounter)].selectedIndex + 1;
                        } else {
                                nIndex = form[("cboPercentDown" + nCounter)].selectedIndex;
                        }
                        form[("cboPercentDown" + nCounter)].options[nIndex].value = nResult;
                        form[("cboPercentDown" + nCounter)].options[nIndex].text = nResult + "%";
                        form[("cboPercentDown" + nCounter)].selectedIndex = nIndex;
                }
        }
        validateNum(form.txtPrice,0,1000,9999999,true);
        validateNum(form.txtRate,3,1,100);
        validateNum(form.txtHeating,0,0,99999,true);
        validateNum(form.txtTax,0,0,99999,true);
        validateNum(form.txtOther,0,0,99999,true);
        validateNum(form.txtGDS,2,1,100);

        if ((form.txtPrice.value != "") &&
                (form.txtRate.value != "") &&
                (form.txtGDS.value != "")) {
                return true;
        } else {
                return false;
        }
}

/*//old
function CMHCRate(PercentDown) {
        var CMHC_LV = 100 - PercentDown;
//      if (CMHC_LV <= 65)
//              return .5;
//      if (CMHC_LV <= 75)
//              return .75;
        if (CMHC_LV <= 75)
                return 0;
        if (CMHC_LV <= 80)
                return 1.25;
        if (CMHC_LV <= 85)
                return 2.0;
        if (CMHC_LV <= 90)
                return 2.5;
        if (CMHC_LV <= 95)
                return 3.75;
        return 0;
}
*/
/*
function CMHCRate(PercentDown) {
        var CMHC_LV = 100 - PercentDown;
        if (CMHC_LV <= 65)
                return .5;
        if (CMHC_LV <= 75)
                return 0.65;
        if (CMHC_LV <= 80)
                return 1.0;
        if (CMHC_LV <= 85)
                return 1.75;
        if (CMHC_LV <= 90)
                return 2.0;
        if (CMHC_LV <= 95)
                return 3.25;
        return 0;
}
*/


//31dec2004
function CMHCRate(PercentDown) {
        var CMHC_LV = 100 - PercentDown;
        if (CMHC_LV <= 65)
                return 0;
        if (CMHC_LV <= 75)
                return 0;
        if (CMHC_LV <= 80)
                return 1.0;
        if (CMHC_LV <= 85)
                return 1.75;
        if (CMHC_LV <= 90)
                return 2.0;
        if (CMHC_LV <= 95)
                return 2.75;
        return 0;
}



function mortgagePayment(nAmount, nRate, nAmort) {
        var nAmortMonths = nAmort * 12;
        var nPaymentsPer6Months = 6;    

        return (nAmount / ( (1 / ( Math.pow((1 + nRate / 200), (1 / nPaymentsPer6Months ))  - 1) ) * (1 - Math.pow((1 + nRate / 200), (-nAmortMonths / nPaymentsPer6Months)) ) ) );     
}

function currencyString(nNumber) {
        nNumDec = 2;
        if (nNumber > 999999) nNumDec = 0;
        var str = "" + Math.round(nNumber * Math.pow(10,nNumDec));
        while (str.length <= nNumDec) {
                str = "0" + str;
        }
        var decpoint = str.length - nNumDec;
        var result = commaFmt(str.substring(0,decpoint) + "." + str.substring(decpoint,str.length));
        if (result.charAt(result.length - 1) == ".") result = result.substring(0, result.length - 1);
        return result;


}

function doCalcOnForm(form) {
        //var form = document.forms[0];
        var bCMHCAlert = true;
        var nDownPayment = 0;
        var nFirstMortgage = 0;
        var nCMHCPremium = 0;
        var nTotalFinancing = 0;
        var nPayment = 0;
        var nExpenses = 0;
        var nTotal = 0;
        var nIncome = 0;
        if (checkForm(form)) {
                var nCounter = 0;
                for (nCounter = 1; nCounter <= 3; nCounter++) {
                        if (Number(form[("cboPercentDown" + nCounter)].options[form[("cboPercentDown" + nCounter)].selectedIndex].value) < 10) {
                                if (Number(filterNum(form.txtPrice.value)) > Number(form.cboLocation.options[form.cboLocation.selectedIndex].value)) {
                                        if (bCMHCAlert) alert(decodeMsg("LOCATION_REQUIREMENT", form.cboLocation.options[form.cboLocation.selectedIndex].value));
                                        bCMHCAlert = false;
                                        clearColumn(form, nCounter);
                                        continue;
                                }
                        } 
                        // calculate downpayment
                        nDownPayment = (filterNum(form.txtPrice.value)) * (Number(form[("cboPercentDown" + nCounter)].options[form[("cboPercentDown" + nCounter)].selectedIndex].value) / 100);
                        form[("txtDownPayment" + nCounter)].value = currencyString(nDownPayment);
                        // calculate first mortgage value
                        nFirstMortgage = (filterNum(form.txtPrice.value) - nDownPayment);
                        form[("txtFirstMortgage" + nCounter)].value = currencyString(nFirstMortgage);
                        // calculate CMHC Premium
                        nCMHCPremium = (CMHCRate(Number(form[("cboPercentDown" + nCounter)].options[form[("cboPercentDown" + nCounter)].selectedIndex].value))) * (nFirstMortgage) / 100;
                        form[("txtCMHC" + nCounter)].value = currencyString(nCMHCPremium);
                        nTotalFinancing = nFirstMortgage + nCMHCPremium;
                        form[("txtFinancing" + nCounter)].value = currencyString(nTotalFinancing);
                        nPayment = mortgagePayment(nTotalFinancing, Number(form.txtRate.value), (Number(form.cboAmortization.options[form.cboAmortization.selectedIndex].value)));
                        form[("txtPI" + nCounter)].value = currencyString(nPayment);
                        nExpenses = ( (Number(filterNum(form.txtHeating.value)) + Number(filterNum(form.txtTax.value)) + Number(filterNum(form.txtOther.value))) / 12);
                        form[("txtExpenses" + nCounter)].value = currencyString(nExpenses);
                        nTotal = nExpenses + nPayment;
                        form[("txtTotal" + nCounter)].value = currencyString(nTotal);
                        nIncome = nTotal * 12 / (Number(form.txtGDS.value) / 100);
                        form[("txtIncome" + nCounter)].value = currencyString(nIncome);
                }
        } else {
                clearResults(form);
        }
}

function decodeMsg(code, par) { // ENGLISH
  var res = "";
  
  if      (code == "INVALID_POSTAL_CODE")    { res = "Invalid postal code: <value>"; }
  else if (code == "LONG_TEXT_LENGTH")       { res = "The text was longer than <value> characters. It has been truncated."; }
  else if (code == "INVALID_NUMBER")         { res = "Invalid number: <value>"; }
  else if (code == "NUMBER_IS_TOO_SMALL")    { res = "The number should not be less than <value>"; }
  else if (code == "NUMBER_IS_TOO_LARGE")    { res = "The number should not be greater than <value>"; }
  else if (code == "DATE_FORMAT_HELP")       { res = 'Please use one of the following formats:\\n'
                                                   + '"2000/05/31", "31-MAY-2000" or "May 31, 2000"'; }
  else if (code == "INVALID_DATE_FORMAT")    { res = 'Invalid date format: "<value>"\\n'
                                                   + ' \\n'
		                                           + 'Please use one of the following formats:\\n'
	                                               + '"2000/05/31", "31-MAY-2000" or "May 31, 2000"'; }
  else if (code == "INVALID_TIME_FORMAT")    { res = "Invalid time: <value> \\n"
                                                   + " \\n"
			                                       + "Please use one of the following formats:\n"
			                                       + "14:15:00,  14:15,  14  or  2 PM"; }
  else if (code == "INVALID_DATE")           { res = "Invalid Date Exception"; }
  else if (code == "INVALID_TIME")           { res = "Invalid Time Exception"; }
  
   else if (code == "DOWNPAYMENT_PERCENT")   { res = "Please enter a percent for the downpayment between 5 and 100."; }
   else if (code == "DOWNPAYMENT_REQUIRED")  { res = "You have pressed cancel.  You must enter a percent for the downpayment.  Please try again."; }
   else if (code == "INVALID_DOWNPAYMENT")   { res = "You have entered an invalid downpayment percentage, please try again."; }
   else if (code == "LOCATION_REQUIREMENT")  { res = "For the selected property location, CMHC/GE require that for homes valued greater than $<value>, you must have a downpayment no less than 10%."; }

   else if (code == "COMPLETE_MORTGAGE_1")   { res = "Please complete all fields for 'Mortgage 1' before calculating."; }
   else if (code == "COMPLETE_MORTGAGE_2")   { res = "Please complete all fields for 'Mortgage 2' before calculating, or check 'Ignore'."; }
   else if (code == "COMPLETE_FOR_AMORTIZ")  { res = "Please complete all fields for 'Mortgage 1' \n or 'Mortgage 2' before requesting a Amortization Schedule."; }
   
   else if (code == "L_INSURANCE_MAXIMUM")   { res = "The maximum life insurance amount is $<value>"; }
   else if (code == "CI_INSURANCE_MAXIMUM")  { res = "The maximum critical illness insurance amount is $<value>"; }
   else if (code == "INSURANCE_MINIMUM")     { res = "The minimum mortgage amount is $<value>"; }
   else if (code == "INS_TYPE_REQUIRED")     { res = "You must select Insurance Type."; }
   else if (code == "INS_AMOUNT_REQUIRED")   { res = "You must enter a value for the Insurance Amount."; }
   else if (code == "BORROWER_AGE_REQUIRED") { res = "You must select an age for the borrower."; }
   
  if (res == "") { res = code; }

  var p = res.indexOf("<value>");
  if (p >= 0) {
    res = res.substring(0,p) + par + res.substring(p+7, res.length);
  } 
  else {
    if (par != "" && par != null) {
      res += ": " + par;
    }
  }
  return res;
}

/*
function monthToEnglish(theStr) { // ENGLISH
    return theStr;
}
*/

function monthToEnglish(theStr) { // FRENCH AND ENGLISH
	var str = theStr;
	var p1 = -1; 
	var p2 = -1;
	var fs = str.toLowerCase();
	for (var i=0; i<fs.length; i++) {
	    var c = fs.substring(i,i+1);
		var isChar = (c >= "a") && (c <= "z");
		if (p1 < 0 && p2 < 0) { // before
		  if (isChar) { p1 = i; }
		} 
		else if (p1 >= 0 && p2 < 0) { // during
		  if (!isChar) { p2 = i; }
		}
	}
	if (p1 >= 0) {
	  if (p2 < 0) { p2 = fs.length; }
	  var wrd = fs.substring(p1,p2);
	  var eng = wrd;
	  if (wrd.length >= 2) {
	    if      ("janvier".indexOf(wrd) == 0)   { eng = "jan"; }
	    else if ("fevrier".indexOf(wrd) == 0)   { eng = "feb"; }
	    else if ("février".indexOf(wrd) == 0)   { eng = "feb"; }
	    else if ("mars".indexOf(wrd) == 0)      { eng = "mar"; }
	    else if ("avril".indexOf(wrd) == 0)     { eng = "apr"; }
	    else if ("mai".indexOf(wrd) == 0)       { eng = "may"; }
	    else if ("juin".indexOf(wrd) == 0)      { eng = "jun"; }
	    else if ("juillet".indexOf(wrd) == 0)   { eng = "jul"; }
	    else if ("aout".indexOf(wrd) == 0)      { eng = "aug"; }
	    else if ("août".indexOf(wrd) == 0)      { eng = "aug"; }
	    else if ("septembre".indexOf(wrd) == 0) { eng = "sep"; }
	    else if ("octobre".indexOf(wrd) == 0)   { eng = "oct"; }
	    else if ("novembre".indexOf(wrd) == 0)  { eng = "nov"; }
	    else if ("decembre".indexOf(wrd) == 0)  { eng = "dec"; }
	    else if ("décembre".indexOf(wrd) == 0)  { eng = "dec"; }
		
		if (p2 < fs.length && fs.substring(p2,p2+1) == ".") { 
		  p2 = p2 + 1; 
		} 
		if (eng != wrd) { 
		  str = str.substring(0,p1) + eng + str.substring(p2); 
		}
	  }
    }
    return str;
}

function monthToLocale(theStr) { // ENGLISH
    return theStr;
}

/* ttt stop translating to French
function monthToLocale(theStr) { // FRENCH
	var str = theStr.toLowerCase();
	var p = -1; 
	var r = -1;
	var wrd = "";
	if      ((p=str.indexOf("jan")) >= 0 ) { wrd = "janv."; r = p; }
	else if ((p=str.indexOf("feb")) >= 0 ) { wrd = "fevr."; r = p; } // wrd = "févr.";
	else if ((p=str.indexOf("mar")) >= 0 ) { wrd = "mars";  r = p; }
	else if ((p=str.indexOf("apr")) >= 0 ) { wrd = "avr.";  r = p; }
	else if ((p=str.indexOf("may")) >= 0 ) { wrd = "mai";   r = p; }
	else if ((p=str.indexOf("jun")) >= 0 ) { wrd = "juin";  r = p; }
	else if ((p=str.indexOf("jul")) >= 0 ) { wrd = "juil."; r = p; }
	else if ((p=str.indexOf("aug")) >= 0 ) { wrd = "aout."; r = p; } // wrd = "août.";
	else if ((p=str.indexOf("sep")) >= 0 ) { wrd = "sept."; r = p; }
	else if ((p=str.indexOf("oct")) >= 0 ) { wrd = "oct.";  r = p; }
	else if ((p=str.indexOf("nov")) >= 0 ) { wrd = "nov.";  r = p; }
	else if ((p=str.indexOf("dec")) >= 0 ) { wrd = "dec.";  r = p; } // wrd = "déc.";

    var res;
    if (wrd != "") { 
	    res = str.substring(0,r) + wrd + str.substring(r+3); 
    } else {
	    res = str;
	}
    return res;
}
*/

