//<SCRIPT language="Javascript">
//This example is from JavaScript: The Definitive Guide, 3rd Edition. 
//That book and this example were Written by David Flanagan.          
//They are Copyright (c) 1996, 1997, 1998 O'Reilly & Associates.       
//This example is provided WITHOUT WARRANTY either expressed or implied.
//You may study, use, modify, and distribute it for any purpose,        
//as long as this notice is retained.                                   

// A utility function that returns true if a string contains only 
// whitespace characters.
function isblank(s)
{
    for(var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
    }
    return true;
}

// This function is where the error messages are "retrieved".  This function is called from verify() and
// determines what error message to return based on the error condition and the "Carleton Name", which is our
// way of referencing a "component" or "factor" of a loan.

function error_msg(elem, blank)
{
    var msg;

    switch (elem.Carletonname)
    {
      case "ClosingCosts" :
        if (blank)
        {
          msg = "You have left the closing costs blank.  Please enter the closing costs and miscellaneous expenses.\n";
        } else {
          if (elem.value < elem.min)
          {
            msg = "The closing costs you have entered are too low.  Please enter a closing cost greater than $" + elem.min + ".\n";
          } else {
            msg = "The closing costs you have entered are too high.  Please enter a closing cost less than $" + elem.max + ".\n";
          }
        }
        break;
      case "DownPayment" :
        if (blank)
        {
          msg = "You have left the down payment blank.  Please enter the down payment amount.\n";
        } else {
          if (elem.value < elem.min)
          {
            msg = "The down payment you have entered is too low.  Please enter a down payment greater than $" + elem.min + ".\n";
          } else {
            msg = "The down payment you have entered is too high.  Please enter a down payment less than $" + elem.min + ".\n";
          }
        }
        break;
      case "GrossAnnualIncome" :
        if (blank)
        {
          msg = "You have left your gross annual income blank.  Please enter your gross annual income (your income before taxes.)\n";
        } else {
          if (elem.value < elem.min)
          {
            msg = "The gross annual income you have entered is too low.  Please enter an amount greater than $" + elem.min + ".\n";
          } else {
            msg = "The gross annual income you have entered is too large.  Please enter an amount less than $" + elem.max + ".\n";
          }
        }
        break;
      case "HomeOwnerInsRate" :
        if (blank)
        {
          msg = "You have left the home owner's insurance rate blank.  Please enter the appropriate insurance rate.\n";
        } else {
          if (elem.value < elem.min)
          {
            msg = "The home owner's insurance rate you have entered is too low.  Please enter a rate higher than " + elem.min + "%.\n";
          } else {
            msg = "The home owner's insurance rate you have entered is too high.  Please enter a rate less than " + elem.min + "%.\n";
          }
        }
        break;
      case "InterestRate" :
        if (blank)
        {
          msg = "You have left the interest rate blank.  Please enter the appropriate rate of interest.\n";
        } else {
          if (elem.value < elem.min)
          {
            msg = "The interest rate you have entered is too low.  Please enter a rate higher than " + elem.min + "%.\n";
          } else {
            msg = "The interest rate you have entered is too high.  Please enter a rate less than " + elem.min + "%.\n";
          }
        }
        break;
      case "LoanAmt" :
	if (blank)
	{
	  msg = "You have left the loan amount blank.  Please enter the principal of the loan.\n";
	} else {
	  if (elem.value < elem.min)
	  {
	    msg = "The loan amount you have entered is too low.  Please enter an amount greater than $" + elem.min + ".\n";
	  } else {
	    msg = "The loan amount you have entered is too large.  Please enter an amount less than $" + elem.max + ".\n";
	  }
	}
	break;
      case "MonthlyDebt" :
        if (blank)
        {
          msg = "You have left your average monthly debt blank.  Please enter your average monthly debt from things like credit cards, consumer loans, etc.\n";
	} else {
	  if (elem.value < elem.min)
	  {
	    msg = "The average monthly debt you have entered is too low.  Please enter an amount greater than $" + elem.min + ".\n";
	  } else {
	    msg = "The average monthly debt you have entered is too high.  Please enter an amount less than $" + elem.max + ".\n";
	  }
	}
	break;
      case "MortAmt" :
	if (blank)
	{
	  msg = "You have left the amount of the new mortgage blank.  Please enter the principal of the new mortgage.\n";
	} else {
	  if (elem.value < elem.min)
	  {
	    msg = "The principal of the new mortgage you have entered is too low.  Please enter an amount greater than $" + elem.min + ".\n";
	  } else {
	    msg = "The principal of the new mortgage you have entered is too large.  Please enter an amount less than $" + elem.max + ".\n";
	  }
	}
	break;
      case "MortIRate" :
	if (blank)
	{
	  msg = "You have left the interest rate of the new mortgage blank.  Please enter the appropriate rate of interest.\n";
	} else {
	  if (elem.value < elem.min)
	  {
	    msg = "The interest rate you have entered for the new mortgage is too low.  Please enter a rate greater than " + elem.min + "%.\n";
	  } else {
	    msg = "The interest rate you have entered for the new mortgage is too large.  Please enter an amount less than " + elem.max + "%.\n";
	  }
	}
	break;
      case "MortPayFreq" :
	if (blank)
	{
	  msg = "You have left payment frequency for the new mortgage blank.  Please select the payment frequency of the new mortgage.\n";
	} else {
	  if (elem.value < elem.min)
	  {
	    msg = "The payment frequency you have selected for the new mortgage is not valid, please choose another.\n";
	  } else {
	    msg = "The payment frequency you have selected for the new mortgage is not valid, please choose another.\n";
	  }
	}
	break;
      case "MortTerm" :
	if (blank)
	{
	  msg = "You have left the term of the new mortgage blank.  Please enter the term (length) of the new mortgage in years.\n";
	} else {
	  if (elem.value < elem.min)
	  {
	    msg = "The term you have entered for the new mortgage is too low.  Please enter a term greater than " + elem.min + ".\n";
	  } else {
	    msg = "The term you have entered for the new mortgage is too large.  Please enter a term less than " + elem.max + ".\n";
	  }
	}
	break;
      case "NumPts" :
	if (blank)
	{
	  msg = "You have left the number of points blank.  Please enter the number of points needed to discount the interest rate.\n";
	} else {
	  if (elem.value < elem.min)
	  {
	    msg = "The number of points you have entered is too low.  Please enter an amount greater than " + elem.min + ".\n";
	  } else {
	    msg = "The number of points you have entered is too large.  Please enter an amount less than " + elem.max + ".\n";
	  }
	}
	break;
      case "OrigMortAmt" :
	if (blank)
	{
	  msg = "You have left the amount of the original mortgage blank.  Please enter the principal of the original mortgage.\n";
	} else {
	  if (elem.value < elem.min)
	  {
	    msg = "The principal of the original mortgage you have entered is too low.  Please enter an amount greater than $" + elem.min + ".\n";
	  } else {
	    msg = "The principal of the original mortgage you have entered is too large.  Please enter an amount less than $" + elem.max + ".\n";
	  }
	}
	break;
      case "OrigMortIRate" :
	if (blank)
	{
	  msg = "You have left the interest rate of the original mortgage blank.  Please enter the appropriate rate of interest.\n";
	} else {
	  if (elem.value < elem.min)
	  {
	    msg = "The interest rate you have entered for the original mortgage is too low.  Please enter a rate greater than " + elem.min + "%.\n";
	  } else {
	    msg = "The interest rate you have entered for the original mortgage is too large.  Please enter an amount less than " + elem.max + "%.\n";
	  }
	}
	break;
      case "OrigMortMonth" :
	if (blank)
	{
	  msg = "You have left the originating month for the original mortgage blank.  Please select the month that the original mortgage began.\n";
	} else {
	  if (elem.value < elem.min)
	  {
	    msg = "The originating month you have selected for the original mortgage is not valid, please choose another.\n";
	  } else {
	    msg = "The originating month you have selected for the original mortgage is not valid, please choose another.\n";
	  }
	}
	break;
      case "OrigMortPayFreq" :
	if (blank)
	{
	  msg = "You have left payment frequency for the original mortgage blank.  Please select the payment frequency of the original mortgage.\n";
	} else {
	  if (elem.value < elem.min)
	  {
	    msg = "The payment frequency you have selected for the original mortgage is not valid, please choose another.\n";
	  } else {
	    msg = "The payment frequency you have selected for the original mortgage is not valid, please choose another.\n";
	  }
	}
	break;
      case "OrigMortTerm" :
	if (blank)
	{
	  msg = "You have left the term of the original mortgage blank.  Please enter the term (length) of the original mortgage in years.\n";
	} else {
	  if (elem.value < elem.min)
	  {
	    msg = "The term you have entered for the original mortgage is too low.  Please enter a term greater than " + elem.min + ".\n";
	  } else {
	    msg = "The term you have entered for the original mortgage is too large.  Please enter a term less than " + elem.max + ".\n";
	  }
	}
	break;
      case "OrigMortYear" :
	if (blank)
	{
	  msg = "You have left the originating year of the original mortgage blank.  Please enter the year that the original mortgage began.\n";
	} else {
	  if (elem.value < elem.min)
	  {
	    msg = "The year you have entered for the original mortgage is not valid.  Please enter a year in the format 19xx or 20xx.\n";
	  } else {
	    msg = "The year you have entered for the original mortgage is not valid.  Please enter a year in the format 19xx or 20xx.\n";
	  }
	}
	break;
      case "PropertyInsuranceRate" :
        if (blank)
        {
          msg = "You have left the property insurance rate blank.  Please enter the appropriate property insurance rate.\n";
        } else {
          if (elem.value < elem.min)
          {
            msg = "The property insurance rate you have entered is too low.  Please enter a rate higher than " + elem.min + "%.\n";
          } else {
            msg = "The property insurance rate you have entered is too high.  Please enter a rate less than " + elem.max + "%.\n";
          }
        }
        break;
      case "PropertyTaxRate" :
        if (blank)
        {
          msg = "You have left the property tax rate blank.  Please enter the appropriate property tax rate.\n";
        } else {
          if (elem.value < elem.min)
          {
            msg = "The property tax rate you have entered is too low.  Please enter a rate higher than " + elem.min + "%.\n";
          } else {
            msg = "The property tax rate you have entered is too high.  Please enter a rate less than " + elem.max + "%.\n";
          }
        }
        break;
      case "RatewoPts" :
	if (blank)
	{
	  msg = "You have left the interest rate without points blank.  Please enter the interest rate without points.\n";
	} else {
	  if (elem.value < elem.min)
	  {
	    msg = "The interest rate without points you have entered is too low.  Please enter a rate greater than " + elem.min + "%.\n";
	  } else {
	    msg = "The interest rate without points you have entered is too large.  Please enter a rate less than " + elem.max + "%.\n";
	  }
	}
	break;
      case "RatewPts" :
	if (blank)
	{
	  msg = "You have left the interest rate with points blank.  Please enter the interest rate that is reduced by paying points.\n";
	} else {
	  if (elem.value < elem.min)
	  {
	    msg = "The interest rate with points you have entered is too low.  Please enter a rate greater than %" + elem.min + "%.\n";
	  } else {
	    msg = "The interest rate with points you have entered is too large.  Please enter a rate greater than " + elem.max + "%.\n";
	  }
	}
	break;
      case "Term" :
	if (blank)
	{
	  msg = "You have left the loan term blank.  Please enter the term (length) of the loan in years.\n";
	} else {
	  if (elem.value < elem.min)
	  {
	    msg = "The loan term you have entered is too low.  Please enter a term greater than " + elem.min + ".\n";
	  } else {
	    msg = "The loan term you have entered is too large.  Please enter a term less than " + elem.max + ".\n";
	  }
	}
	break;
    }      

    return msg;    
}
// This is the function that performs form verification. It will be invoked
// from the onSubmit() event handler. The handler should return whatever
// value this function returns.
function verify(f)
{
    var msg;
    var empty_fields = "";
    var errors = "";
    var firstbad = 0;
    var firstbadset = false;

	// THIS SHOULD BE IN THE FORM.SUBMIT AS FOLLOWS:
	// onSubmit=" 
    // this.BORROWER_ADDRESS2.optional = true;
    // this.COBORROWER_FIRSTNAME.optional = true;
    // this.COBORROWER_MIDDLENAME.optional = true;
    // this.COBORROWER_LASTNAME.optional = true;
    // this.COBORROWER_SSN.optional = true;
    // this.COBORROWER_DATEOFBIRTH.optional = true;
    // this.COBORROWER_ADDRESS.optional = true;
    // this.COBORROWER_ADDRESS2.optional = true;
    // this.COBORROWER_CITY.optional = true;
    // this.COBORROWER_STATE.optional = true;
    // this.COBORROWER_ZIP.optional = true;
    // this.COBORROWER_COUNTY.optional = true;
    // this.COBORROWER_HOMEPHONE.optional = true;
    // this.BORROWER_ZIP.min = 0;
    // this.BORROWER_ZIP.max = 99999;
	//    return verify(this);"
		
//	alert("In verify");	
	
    // Loop through the elements of the form, looking for all 
    // text and textarea elements that don't have an "optional" property
    // defined. Then, check for fields that are empty and make a list of them.
    // Also, if any of these elements have a "min" or a "max" property defined,
    // then verify that they are numbers and that they are in the right range.
    // Put together error messages for fields that are wrong.
    for(var i = 0; i < f.length; i++)
    {
      var e = f.elements[i];
      if ( (((e.type == "text") || (e.type == "textarea") || (e.type == "select-one") || (e.type == "select-multiple")) && !e.optional) )
      {
        // first check if the field is empty
	if ( ((e.value == null) || (e.value == "") || isblank(e.value)) || ((e.type == "select-one" || e.type == "select-multiple") && e.value == "Not Selected") )
	{
	  if (!firstbadset)
	  {
	    firstbad = i;
	    firstbadset = true;
	  }					

//				namesarray = e.name.split("_");
//				thename = "";
//				for (var j = 0; j < namesarray.length; j++) {
//						thename += namesarray[j].substring(0,1) + namesarray[j].substring(1, (namesarray[j].length)).toLowerCase() + " ";
//				}									
//                empty_fields += "\n          " + e.name;
//                empty_fields += "\n          " + thename;

	  empty_fields += error_msg(e, true);
		  
	  continue;
	}

	// Now check for fields that are supposed to be numeric.
	if (e.numeric || (e.min != null) || (e.max != null))
	{
	  var v = parseFloat(e.value);
	  if (isNaN(v) ||
	      ((e.min != null) && (v < e.min)) ||
	      ((e.max != null) && (v > e.max)))
	  {
//	    namesarray = e.name.split("_");
//	    thename = "";
//	    for (var j = 0; j < namesarray.length; j++)
//	    {
//	      thename += namesarray[j].substring(0,1) + namesarray[j].substring(1, (namesarray[j].length)).toLowerCase() + " ";
//	    }

//	    errors += "- The field " + thename + " must be a number";
//	    if (e.min != null) 
//	      errors += " that is greater than " + e.min;
//	    if (e.max != null && e.min != null) 
//	      errors += " and less than " + e.max;
//	    else if (e.max != null)
//	      errors += " that is less than " + e.max;

//	    errors += ".\n";

	    errors += error_msg(e, false);
	    if (!firstbadset)
	    {
	      firstbad = i;
	      firstbadset = true;
	    }
	  }
	}
      }
    }

    // Now, if there were any errors, display the messages, and
    // return false to prevent the form from being submitted. 
    // Otherwise return true.
    if (!empty_fields && !errors)
    {
//		document.location = "./nextform_txt.asp";
      return true;
    }		

    msg  = "______________________________________________________\n\n"
    msg += "The initial data was not submitted because of the following:\n";
    msg += "Please make corrections and try again.\n";
    msg += "______________________________________________________\n\n"

    if (empty_fields)
    {
//      msg += "- The following required field(s) are empty:" + empty_fields + "\n";
//      if (errors) msg += "\n";
    }
    msg += empty_fields + "\n";
    msg += errors;
    alert(msg);
	
    if ( (f.elements[firstbad].type == "text") || (f.elements[firstbad].type == "textarea") )
    {
      f.elements[firstbad].select();
    }
    f.elements[firstbad].focus();
	
    return false;
}
//</SCRIPT>