
function change()
{
selected = document.loanform.PRODUCT.value;

 var existingloana = MM_findObj("pzipFields");
 var existingloanb = MM_findObj("pyearFields");
 var existingloanc = MM_findObj("purpFields");
 var existingloand = MM_findObj("balanceFields");
 var existingloane = MM_findObj("addcashFields");
 var existingloanf = MM_findObj("existingRate");
 var existingloang = MM_findObj("addcashRefi");
 var existingloanh = MM_findObj("addcashEquity");
 var existingloani = MM_findObj("addcashDebtcon");
 var existingloanj = MM_findObj("secmtgquestionFields");
 var existingloank = MM_findObj("secondMortgageFields");


 var newhomeloana = MM_findObj("pareaCodeFields");
 var newhomeloanb = MM_findObj("whenbuyFields");
 var newhomeloanc = MM_findObj("foundhomeFields");
 var newhomeloand = MM_findObj("dpmtFields");
 var newhomeloane = MM_findObj("ftbFields");
 var newhomeloanf = MM_findObj("desiredRate");
 var newhomeloang = MM_findObj("agentFields");

 // reload when product has changed after hitting submit
 if (existingloana == null || newhomeloana == null) {
  window.location.reload();
  selected = "PP_REFI";
	return;
 }

 if (selected == "PP_NEWHOME") {
   existingloana.style.display="none";
   existingloanb.style.display="none";
   existingloanc.style.display="none";
   existingloand.style.display="none";
   existingloane.style.display="none";
   existingloanf.style.display="none";
   existingloang.style.display="none";
   existingloanh.style.display="none";
   existingloani.style.display="none";
   existingloanj.style.display="none";
   existingloank.style.display="none";   
   newhomeloana.style.display="";
   newhomeloanb.style.display="";
   newhomeloanc.style.display="";
   newhomeloand.style.display="";
   newhomeloane.style.display="";
   newhomeloanf.style.display="";
   newhomeloang.style.display="none";
}
 else if (selected == "PP_DEBTCON") {
   existingloana.style.display="";
   existingloanb.style.display="";
   existingloanc.style.display="";
   existingloand.style.display="";
   existingloane.style.display="";
   existingloanf.style.display="";
   existingloang.style.display="none";
   existingloanh.style.display="none";
   existingloani.style.display="";
   existingloanj.style.display="";
   existingloank.style.display="";
   newhomeloana.style.display="none";
   newhomeloanb.style.display="none";
   newhomeloanc.style.display="none";
   newhomeloand.style.display="none";
   newhomeloane.style.display="none";
   newhomeloanf.style.display="none";
   newhomeloang.style.display="none";
}

 else if (selected == "PP_REFI") {
   existingloana.style.display="";
   existingloanb.style.display="";
   existingloanc.style.display="";
   existingloand.style.display="";
   existingloane.style.display="";
   existingloanf.style.display="";
   existingloang.style.display="";
   existingloanh.style.display="none";
   existingloani.style.display="none";
   existingloanj.style.display="";
   existingloank.style.display="";
   newhomeloana.style.display="none";
   newhomeloanb.style.display="none";
   newhomeloanc.style.display="none";
   newhomeloand.style.display="none";
   newhomeloane.style.display="none";
   newhomeloanf.style.display="none";
   newhomeloang.style.display="none";   
}
 else if (selected == "PP_HOME_EQUITY") {
   existingloana.style.display="";
   existingloanb.style.display="";
   existingloanc.style.display="";
   existingloand.style.display="";
   existingloane.style.display="";
   existingloanf.style.display="";
   existingloang.style.display="none";
   existingloanh.style.display="";
   existingloani.style.display="none";
   existingloanj.style.display="";
   existingloank.style.display="";
   newhomeloana.style.display="none";
   newhomeloanb.style.display="none";
   newhomeloanc.style.display="none";
   newhomeloand.style.display="none";
   newhomeloane.style.display="none";
   newhomeloanf.style.display="none";
   newhomeloang.style.display="none";
}
}



function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

/* Show/hide method */

function showLoan(select) {

 var others = MM_findObj("tblOthers");
 var newhome = MM_findObj("tblPurchase");
 
 // reload when product has changed after hitting submit
 if (others == null || newhome == null) {
    window.location.reload();
    select.value = "PP_REFI";
	return;
 }

 if (select.value == "PP_NEWHOME") {
   others.style.display="none";
   newhome.style.display="";
 }
 else {
   others.style.display="";
   newhome.style.display="none";
 }
}

function showLoanFields(select) {

 var existingloana = MM_findObj("pzipFields");
 var existingloanb = MM_findObj("pyearFields");
 var existingloanc = MM_findObj("purpFields");
 var existingloand = MM_findObj("balanceFields");
 var existingloane = MM_findObj("addcashFields");
 var existingloanf = MM_findObj("existingRate");
 var existingloang = MM_findObj("addcashRefi");
 var existingloanh = MM_findObj("addcashEquity");
 var existingloani = MM_findObj("addcashDebtcon");
 var existingloanj = MM_findObj("secmtgquestionFields");
 var existingloank = MM_findObj("secondMortgageFields");


 var newhomeloana = MM_findObj("pareaCodeFields");
 var newhomeloanb = MM_findObj("whenbuyFields");
 var newhomeloanc = MM_findObj("foundhomeFields");
 var newhomeloand = MM_findObj("dpmtFields");
 var newhomeloane = MM_findObj("ftbFields");
 var newhomeloanf = MM_findObj("desiredRate");
 var newhomeloang = MM_findObj("agentFields");

 // reload when product has changed after hitting submit
 if (existingloana == null || newhomeloana == null) {
  window.location.reload();
  select.value = "PP_REFI";
	return;
 }

 if (select.value == "PP_NEWHOME") {
   existingloana.style.display="none";
   existingloanb.style.display="none";
   existingloanc.style.display="none";
   existingloand.style.display="none";
   existingloane.style.display="none";
   existingloanf.style.display="none";
   existingloang.style.display="none";
   existingloanh.style.display="none";
   existingloani.style.display="none";
   existingloanj.style.display="none";
   existingloank.style.display="none";   
   newhomeloana.style.display="";
   newhomeloanb.style.display="";
   newhomeloanc.style.display="";
   newhomeloand.style.display="";
   newhomeloane.style.display="";
   newhomeloanf.style.display="";
   newhomeloang.style.display="none";
}
 else if (select.value == "PP_DEBTCON") {
   existingloana.style.display="";
   existingloanb.style.display="";
   existingloanc.style.display="";
   existingloand.style.display="";
   existingloane.style.display="";
   existingloanf.style.display="";
   existingloang.style.display="none";
   existingloanh.style.display="none";
   existingloani.style.display="";
   existingloanj.style.display="";
   existingloank.style.display="";
   newhomeloana.style.display="none";
   newhomeloanb.style.display="none";
   newhomeloanc.style.display="none";
   newhomeloand.style.display="none";
   newhomeloane.style.display="none";
   newhomeloanf.style.display="none";
   newhomeloang.style.display="none";
}

 else if (select.value == "PP_REFI") {
   existingloana.style.display="";
   existingloanb.style.display="";
   existingloanc.style.display="";
   existingloand.style.display="";
   existingloane.style.display="";
   existingloanf.style.display="";
   existingloang.style.display="";
   existingloanh.style.display="none";
   existingloani.style.display="none";
   existingloanj.style.display="";
   existingloank.style.display="";
   newhomeloana.style.display="none";
   newhomeloanb.style.display="none";
   newhomeloanc.style.display="none";
   newhomeloand.style.display="none";
   newhomeloane.style.display="none";
   newhomeloanf.style.display="none";
   newhomeloang.style.display="none";   
}
 else if (select.value == "PP_HOME_EQUITY") {
   existingloana.style.display="";
   existingloanb.style.display="";
   existingloanc.style.display="";
   existingloand.style.display="";
   existingloane.style.display="";
   existingloanf.style.display="";
   existingloang.style.display="none";
   existingloanh.style.display="";
   existingloani.style.display="none";
   existingloanj.style.display="";
   existingloank.style.display="";
   newhomeloana.style.display="none";
   newhomeloanb.style.display="none";
   newhomeloanc.style.display="none";
   newhomeloand.style.display="none";
   newhomeloane.style.display="none";
   newhomeloanf.style.display="none";
   newhomeloang.style.display="none";
}
}

/*** functions for limiting values of dropdowns based on the value of another input ***/

/* Limit b's selections upto whatever a's selected value is. */
function limit(maxValue, b) {

 // the current max value in b's list.
 var bMax = getInteger(b.options[b.options.length-1].value);
 
 if (bMax > maxValue) {
  trim(b, maxValue);
 }
 else if (bMax < maxValue) {
  extend(b, bMax, maxValue);
 }
}

/* extends list from bMax to maxValue. */
function extend(b, bMax, maxValue) {
 
 var i = bMax;
 while (i < maxValue) {
 
  var inc = 0;
 
  if (i < 197500) {
   inc = 5000;
   i+=inc;
  }
  else if (i == 197500) {
   inc = 10000;
   i = 205000;
  }
  else if (i < 395000) {
   inc = 10000;
   i+=inc;
  }
  else if (i == 395000) {
   inc = 20000;
   i = 410000;
  }
  else if (i < 990000) {
   inc = 20000;
   i+=inc;
  }
  else {
   break;
  }

  lower = i-((inc/2)-1);
  upper = i+(inc/2);
  
  addOption(toMoneyInt(lower) + ' - ' + toMoneyInt(upper), i, b);  
 }
 
}

function addOption(text,value,list) {
  var opt = document.createElement('option');
  opt.text = text;
  opt.value = value;
  
  try {
   list.add(opt, null); // standards compliant; doesn't work in IE
  }
  catch(ex) {
   list.add(opt); // IE only
  }
}

/* Trim a list down to maxValue. */
function trim(b, maxValue) {
 for(var i=0; i<b.options.length; i++) {
  if (getInteger(b.options[i].value) > maxValue) {
   cullPoint = i-1;
   break;
  }
 }

 for(var i=b.options.length; i>cullPoint; i--) {
  b.remove(i);
 }
 
}

/** Makes 10000 look like 10,000, etc. */
function toMoneyInt(num) {

 str = num + ""
 len = str.length
 s = ""
 
 for(var i=len,j=0; i>=0; i--,j++) {
  s = str.charAt(i) + s;
  
  if (j==3 && i > 0) {
   s = "," + s;
   j = 0;
  }
 }

 return s;
}

/**** util methods */

function getInteger(vNum) {
	vNum = getIntegerString(vNum.toString());
	if (vNum == "") { vNum = 0; }
	return Number(vNum);
}

function getIntegerString(strIn) {
	return strIn.replace(/[^0-9]/g, "");
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function trimString(strIn) {
	if (/^\s/.test(strIn)) { strIn = strIn.replace(/^\s{1,}/, ""); }
	if (/\s$/.test(strIn)) { strIn = strIn.replace(/\s{1,}$/, ""); }
	return strIn;
}

function getDecimalString(strIn) {
	strIn = strIn.replace(/[^0-9\.]/g, "");
	var iPoint = strIn.indexOf(".");
	if (iPoint > -1) {
		strIn = strIn.substring(0, iPoint + 1) + getIntegerString(strIn.substring((iPoint + 1), strIn.length));
	}
	return strIn;
}

function isValueInSelectbox(hSelectbox, strValue) {
	for (var i=0; i < hSelectbox.options.length; i++) {
		if (hSelectbox.options[i].value == strValue) {
			return true;
		}
	}
	return false;
}

function getParsedPhoneStr(strIn) {
	strIn = strIn.replace(/\.|-| |\(|\)/g,"");
	return strIn;
}


/*
 * DOM show/hide functions.
 * Do not set display to "block", it breaks things like table rows.
 */
function showElement(strID) {
	try { document.getElementById(strID).style.display = ""; } catch(ex) {}
}
function hideElement(strID) {
	try { document.getElementById(strID).style.display = "none"; } catch(ex) {}
}


function isNotHiddenFormField(form, strName) {
	// Tells you if a non-hidden field exists in a form.
	var type;
	var field = form.elements[strName];
	if (field && (typeof(field) == "object")) {
		try {
			type = field.getAttribute("type");
		} catch(ex) {
			// probably a radio button group (no attribute)
			return true;
		}
		if ((type) && (type.toLowerCase() == "hidden")) {
			return false;
		}
		return true;
	}
	return false;
}



function getRadioValue(hRadioGroup) {
	// Gets the selected value of a radio button group. If no radio button is selected, returns an empty string.
	for (var i=0; i < hRadioGroup.length; i++) {
		if (hRadioGroup[i].checked) { return hRadioGroup[i].value; }
	}
	return "";
}

function getMultipleSelectBoxValues(hSelect) {
	var i, option, arrSelected = new Array();
	while (hSelect.selectedIndex >= 0) {
		arrSelected[arrSelected.length] = hSelect.selectedIndex;
		hSelect.options[hSelect.selectedIndex].selected = false;
	}
	for (i=0; i < arrSelected.length; i++) {
		option = hSelect.options[arrSelected[i]];
		option.selected = true;
		arrSelected[i] = option.value;
	}
	return arrSelected;
}

function getFormFieldValue(hField) {
	if (!hField) { return undefined; }
	try {
		if (hField.type) {
			if (hField.type == "radio") {
				return getRadioValue(hField.form.elements[hField.name]);
			} else if (hField.type == "select-multiple") {
				return getMultipleSelectBoxValues(hField);
			} else {
				return hField.value;
			}
		}
	} catch (ex) {}
	try {
		if (hField.length && hField[0] && (hField[0].type == "radio")) {
			return getRadioValue(hField[0].form.elements[hField[0].name]);
		}
	} catch(ex) {}
	return undefined;
}

function addCommasToNumString(strIn) {
	var arrTemp = strIn.split("");
	var i = strIn.length - 4;
	var iPoint = strIn.indexOf(".");
	if (iPoint > -1) { i -= (strIn.length - iPoint); }
	for (i; i >= 0; i-=3) { arrTemp[i] += ","; }
	return arrTemp.join("");
}


function filterIntegerAddCommas(hField) {
	hField.value = addCommasToNumString(getIntegerString(hField.value));
}

function filterInteger(hField) {
   hField.value = getIntegerString(hField.value);
}


function toggleOtherField(hSelectbox, strOtherFormBlock) {
	// Shows another form block if an option with value "other" is selected in a SELECT form element.
	if (hSelectbox.value == "other") {
		showElement(strOtherFormBlock);
	} else {
		hideElement(strOtherFormBlock);
	}
}

function qs() {
var query = window.location.search.substring(1);
var parms = query.split('&');
for (var i=0; i<parms.length; i++) {
   var pos = parms[i].indexOf('=');
   if (pos > 0) {
      var key = parms[i].substring(0,pos);
      var val = parms[i].substring(pos+1);
      qsParm[key] = val;
      }
   }
}

function validateRadioButton(hRadioButton, strAlert){
	var selectedOption = -1;
	for(i=hRadioButton.length-1; i > -1; i--){
		if (hRadioButton[i].checked) {
			selectedOption= i;
		}
	}
	if(selectedOption == -1){
		return validationAlert(strAlert, hRadioButton[0]);
	}
	return true;
}

function validateCheckBox(hCheckBox, strAlert){
	if(!hCheckBox.checked){
		return validationAlert(strAlert, hCheckBox);
	}
	return true;
}