//////////////////////////////////////////////////////////////////////
// This file contains JavaScript functions which are common to all
// web pages of the application.
//////////////////////////////////////////////////////////////////////

// this is the error window
var ewindow = null;

//////////////////////////////////////////////////////////////////////
// This function will display a new window containing the errors.
//////////////////////////////////////////////////////////////////////
function openErrorWindow( error_arr, error_num) {

   // if we have not yet created the window (or the user closed it)
   if ( (ewindow == null) || (ewindow.closed) ) {
     ewindow = window.open("","Errors","width=600,height=350,resizable=yes,scrollbars=yes");
   }

   // extract the document object from the window so we can write to it
   var MyDoc = ewindow.document;
   
   // create the boilerplate stuff
   MyDoc.writeln('<html>');
   MyDoc.writeln('<head>');
   MyDoc.writeln('<link rel="stylesheet" href="css/broker.css" type="text/css">');
   MyDoc.writeln('</head>');
   MyDoc.writeln('<title>Please Correct These Errors</title>');
   MyDoc.writeln('<body class="BodyError" TEXT="BLACK" TOPMARGIN=4 MARGINHEIGHT=4 LEFTMARGIN=4 MARGINWIDTH=4>');
   MyDoc.writeln('<table width="100%" height="100%" cellpadding="2" cellspacing="0" border="1" bordercolor="#880029">');
   MyDoc.writeln('<tr><td bgcolor="#880029">');
   MyDoc.writeln('<font face="Verdana, Arial" size="2" color="#FFFFFF"><b>&nbsp;Sorry, before you can proceed, please fix ');
   MyDoc.writeln('the following errors.</b></font>');
   MyDoc.writeln('</td></tr>');
   MyDoc.writeln('<tr><td>');
   MyDoc.writeln('<table width="100%" height="100%" cellpadding="0" cellspacing="0" border="0">');
   MyDoc.writeln('<tr><td>');
   MyDoc.writeln('<font face="Verdana, Arial" size="2" color="#000000">&nbsp;&nbsp;&nbsp;The items in question each have a <img src="/images/error.gif" align="absmiddle"> displayed next ');
   MyDoc.writeln('to them.</font>');
   MyDoc.writeln('</td></tr>');
   MyDoc.writeln('<tr><td>');
   MyDoc.writeln('<font face="Verdana, Arial" size="2" color="#000000">&nbsp;&nbsp;&nbsp;Once you have fixed the error(s), you may proceed.</font>');
   MyDoc.writeln('</td></tr>');
   MyDoc.writeln('<tr><td><hr width="95%"></td></tr>');
   MyDoc.writeln('<tr><td><ul>');

   // loop thru each of the errors we found
   for (var i = 0; i < error_arr.length; i++) 
   {
	// add this error to the displayable list
      MyDoc.writeln('<li><font face="Verdana, Arial" size="2" color="#880029"><b>' + error_arr[i] + '</b></font></li>');
   }
      
   // end the boilerplate stuff
   MyDoc.writeln('</ul></td></tr>');
   MyDoc.writeln('<tr><td><hr width="95%"></td></tr>');
   MyDoc.writeln('<tr><td align="center">');
   MyDoc.writeln('<form>');
   MyDoc.writeln('<input type="button" name="OK" value="Close Window" onClick="self.close()">');
   MyDoc.writeln('</form>');
   MyDoc.writeln('</td></tr></table>');
   MyDoc.writeln('</td></tr></table>');
   MyDoc.writeln('</body>');
   MyDoc.writeln('</html>');
   MyDoc.close('');

   // make sure this window is visible
   ewindow.focus();
   return;
}

// some constants used to reference the error graphics
var NO_ERROR_GRAPHIC 	= 1
var ERROR_GRAPHIC 	= 2
var TIP_NORMAL 		= 3
var TIP_ROLLOVER 		= 4
var CONTINUE_NORMAL 	= 5
var CONTINUE_ROLLOVER 	= 6

// small array of graphics used to highlight validation errors
var FeedBack = new Array()
FeedBack[NO_ERROR_GRAPHIC]  	 = new Image(100,200)
FeedBack[ERROR_GRAPHIC] 	 = new Image(100,200)
FeedBack[TIP_NORMAL] 	 	 = new Image
FeedBack[TIP_ROLLOVER] 	 	 = new Image
FeedBack[CONTINUE_NORMAL] 	 = new Image
FeedBack[CONTINUE_ROLLOVER] 	 = new Image

FeedBack[NO_ERROR_GRAPHIC].src 	= "../../../images/erroroff.gif"
FeedBack[ERROR_GRAPHIC].src 	 	= "../../../images/error.gif"
// FeedBack[TIP_NORMAL].src 	 	= "../../../images/tipoff.gif"
// FeedBack[TIP_ROLLOVER].src 	 	= "../../../images/tiproll.gif"
// FeedBack[CONTINUE_NORMAL].src  	= "../../../images/continueoff.gif"
// FeedBack[CONTINUE_ROLLOVER].src	= "../../../images/continueroll.gif"

//////////////////////////////////////////////////////////////////
// This function will move back 1 item in the history.
//////////////////////////////////////////////////////////////////
function GoBack()
{ 
   window.history.back();
}

//////////////////////////////////////////////////////////////////
// Displays a graphic when the user mouses "over" a tip graphic.
//////////////////////////////////////////////////////////////////
function ShowGraphic(what,where)
{ 
   document.loan_form[where].src = FeedBack[what].src; 
}

//////////////////////////////////////////////////////////////////
// indicates the input field is valid by displaying a clear graphic
//////////////////////////////////////////////////////////////////
function ClearError(where)
{ 
   document.loan_form[where].src = FeedBack[NO_ERROR_GRAPHIC].src; 
}

//////////////////////////////////////////////////////////////////
// indicates the input field is invalid by displaying a check mark graphic
//////////////////////////////////////////////////////////////////
function SetError(where)
{ 
   document.loan_form[where].src = FeedBack[ERROR_GRAPHIC].src; 
}

//////////////////////////////////////////////////////////////////
// Displays a graphic when the user mouses "over" a tip graphic.
//////////////////////////////////////////////////////////////////
function TipOn(where)
{ 
   document.loan_form[where].src = FeedBack[TIP_ROLLOVER].src; 
}

//////////////////////////////////////////////////////////////////
// Displays a graphic when the user mouses "off" a tip graphic.
//////////////////////////////////////////////////////////////////
function TipOff(where)
{ 
   document.loan_form[where].src = FeedBack[TIP_NORMAL].src; 
}

//////////////////////////////////////////////////////////////////
// Displays a graphic when the user mouses "over" the continue graphic.
//////////////////////////////////////////////////////////////////
function ContinueOn()
{ 
   document.loan_form.ContinueButton.src = FeedBack[CONTINUE_ROLLOVER].src; 
}

//////////////////////////////////////////////////////////////////
// Displays a graphic when the user mouses "off" the continue graphic.
//////////////////////////////////////////////////////////////////
function ContinueOff()
{ 
   document.loan_form.ContinueButton.src = FeedBack[CONTINUE_NORMAL].src; 
}

//////////////////////////////////////////////////////////////////
// set the left nav frame to indicate which step we are on
//////////////////////////////////////////////////////////////////
function setProgressIndicator(url) {
   if ( parent.xmininav == null) return;
// don;t know why this does not work but the deprecated "location" does work
//   parent.xmininav.document.URL = url;
   parent.xmininav.document.location = url;
   return;
}

var gwindow; 		if(!gwindow) 	  { gwindow=null; }
var cwindow; 		if(!cwindow) 	  { cwindow=null; }
var twindow; 		if(!twindow) 	  { twindow=null; }
var swindow; 		if(!swindow) 	  { swindow=null; }
var zwindow; 		if(!zwindow) 	  { zwindow=null; }
var creditwindow; 	if(!creditwindow)   { creditwindow=null; }
var ratewindow;   	if(!ratewindow)     { ratewindow=null; }
var approvalwindow; 	if(!approvalwindow) { approvalwindow=null; }
var mwindow; 		if(!mwindow) 	  { mwindow=null; }
var emailwindow;   	if(!emailwindow)    { emailwindow=null; }

//////////////////////////////////////////////////////////////////
// Displays a daughter window containing the requested glossary entry.
//////////////////////////////////////////////////////////////////
function openGlossaryWindow(term) { 
    window_url = "http://www.villagewholesalelending.com/glossary/index.html?" + term; 
    if(gwindow) { gwindow.close();
        gwindow = null;   }
         gwindow = window.open(window_url, "glossary_window", "height=300,width=230,resizable=no"); 
 if(!gwindow) { openGlossaryWindow(term); } return; 
}

//////////////////////////////////////////////////////////////////
// Displays a daughter window containing the requested copyright entry.
//////////////////////////////////////////////////////////////////
function openWindow(url) {
   if(cwindow) {
 cwindow.close(); cwindow = null; }
     cwindow = window.open(url,"copyright_window","width=714,height=350,resizable=no,scrollbars=yes");

     if(!cwindow) { openWindow(url);   }
   return;
}

//////////////////////////////////////////////////////////////////
// Displays a daughter window containing the requested tip.
//////////////////////////////////////////////////////////////////
function openTipWindow(url) 
{
//   if(twindow) 
//   {
// 	twindow.close(); 
//	twindow = null; 
//   }
//   twindow = window.open(url,"tip_window","width=300,height=300");
//
//   if(!twindow) { openWindow(url);   }
//   return;

   // if we did not create the window yet, or we did create it but the
   // user closed it
   if(!twindow || twindow.closed) 
   {
      // recreate the window
   	twindow = window.open(url,"tip_window","width=300,height=300,scrollbars=yes,resizable=yes");
   }
   else
   {
      // the window still exists but me be behind another window.
      // Close it and open it again to bring it to the top.
 	twindow.close(); 
   	twindow = window.open(url,"tip_window","width=300,height=300,scrollbars=yes,resizable=yes");
   }

   return;
}

//////////////////////////////////////////////////////////////////
// Displays a daughter window containing cities and counties are
// are located in the input zip code.
//	Zip - Zip code of interest
//	MyCityHtmlFieldName- name of an HTML field which should be set
//				   to the user selected city value
//	MyStateHtmlFieldName- name of an HTML field which should be set
//				   to the user selected state value
//	MyCountyHtmlFieldName- name of an HTML field which should be set
//				   to the user selected county value
// NOTE: If no HTML field exists for City, State, or County, supply
//	   the string 'none'
//////////////////////////////////////////////////////////////////
function openZipLookupWindow(ZipEntryField, MyCityHtmlFieldName, MyStateHtmlFieldName, MyCountyHtmlFieldName) 
{
   // NOTE:	Since JavaScript allows us to create user defined properties,
   // 	we will create and use the user defined property "PreviousZip".  
   //		to remember what the zip code was the last time this function
   //		was called.  By doing this, we can avoid displaying the zip
   //		window if the user tabs thru a zip field without changing it.

   // if we do not have a valid zip code
   if (ZipEntryField.value.length != 5)
   {
	// we want to keep track of this invalid zip code so we will know
	// when the user enters a valid one
      ZipEntryField.PreviousZip = ZipEntryField.value;

      return;
   }

   // if we have setup the user defined property for previous zip and the user
   // has not changed the zip code
   if ( (ZipEntryField.PreviousZip != null) && (ZipEntryField.value == ZipEntryField.PreviousZip) )
   {
      return;
   }

   // remember the zip that caused us to display the zip window
   // so we don't display it agin until the zip code changes.
   ZipEntryField.PreviousZip = ZipEntryField.value;

   // this will cause the servlet to query DB for info about the input zip code
   var url = "/WebLoansServlet?Java_Class_Name=ZipCodeLookup&PageAction=ServePage&ZipToLookup=" + ZipEntryField.value + "&ZipEntryFieldName=" + ZipEntryField.name + "&CityHtmlFieldName=" + MyCityHtmlFieldName + "&StateHtmlFieldName=" + MyStateHtmlFieldName + "&CountyHtmlFieldName=" + MyCountyHtmlFieldName;

   // if the window already exists, it may be behind another window.
   // Close it and open it again to bring it to the top.
   if(zwindow && !zwindow.closed) 
   {
      // Close it and open it again to bring it to the top.
 	zwindow.close(); 
   }

   // open the zip selection window
   zwindow = window.open(url,"zip_lookup_window","width=500,height=300,scrollbars=yes,resizable=yes");

   return;
}

//////////////////////////////////////////////////////////////////
// Displays a daughter window containing the requested special.
//////////////////////////////////////////////////////////////////
function openSpecialsWindow(url) 
{
   	// if the window already exists, it may be behind another window.
   	// Close it and open it again to bring it to the top.
   	if(swindow && !swindow.closed) 
   	{
      	// Close it and open it again to bring it to the top.
 		swindow.close(); 
   	}

	// open the window and make sure it's on top
	swindow=window.open(url,"specials_window","width=350,height=250,scrollbars=yes,resizable=yes");
 	swindow.focus(); 

   	return;
}

//////////////////////////////////////////////////////////////////
// Displays a new browser window containing the requested URL.
//////////////////////////////////////////////////////////////////
function openNormalWindow(url) {
   window.open(url);
   return;
}

//////////////////////////////////////////////////////////////////
// Displays a daughter window containing a printable credit report.
//////////////////////////////////////////////////////////////////
function openCreditWindow(url) 
{
   	// if the window already exists, it may be behind another window.
   	// Close it and open it again to bring it to the top.
   	if(creditwindow && !creditwindow.closed) 
   	{
      	// Close it and open it again to bring it to the top.
 		creditwindow.close(); 
   	}

	// open the window and make sure it's on top
	creditwindow=window.open(url,"credit_window","width=750,height=500,scrollbars=yes,resizable=yes");
 	creditwindow.focus(); 

   	return;
}

//////////////////////////////////////////////////////////////////
// Displays a daughter window containing a printable credit report.
//////////////////////////////////////////////////////////////////
function openConditionalApprovalWindow(url) 
{
   	// if the window already exists, it may be behind another window.
   	// Close it and open it again to bring it to the top.
   	if(approvalwindow && !approvalwindow.closed) 
   	{
      	// Close it and open it again to bring it to the top.
 		approvalwindow.close(); 
   	}

	// open the window and make sure it's on top
	approvalwindow=window.open(url,"approval_window","width=750,height=500,scrollbars=yes,resizable=yes");
 	approvalwindow.focus(); 

   	return;
}

//////////////////////////////////////////////////////////////////
// Displays a daughter window containing a rate sheet.
//////////////////////////////////////////////////////////////////
function openRateWindow(InState) 
{
   	// if the window already exists, it may be behind another window.
   	// Close it and open it again to bring it to the top.
   	if(ratewindow && !ratewindow.closed) 
   	{
      	// Close it and open it again to bring it to the top.
 		ratewindow.close(); 
   	}

//	//is state selected
//	if ( (document.loan_form.ProgramGroup.selectedIndex == -1) 
//     	|| (!isStateSelected(document.loan_form.ProgramGroup[document.loan_form.ProgramGroup.selectedIndex].value)) 
//	)
//	{
//		alert("Please select a Rate Sheet");
//		return;
// 	}
//
//	//is state selected
//	if ( (document.loan_form.State.selectedIndex == -1) 
//   	|| (!isStateSelected(document.loan_form.State[document.loan_form.State.selectedIndex].value)) 
//	)
//	{
//		alert("Please select a State");
//		return;
//   	}

	//is state selected
	if (!isStateSelected(InState))
	{
		alert("Please select a State from the drop-down list or click on a state in the map above.");
		return;
   	}

//	var ProgramGroup=document.loan_form.ProgramGroup[document.loan_form.ProgramGroup.selectedIndex].value
	var ProgramGroup=999999

      var url = "/WebLoansServlet?Java_Class_Name=RateSheet&PageAction=ServePage&StateOnlySearch=1&State=" + InState + "&ProgramGroupId=" + ProgramGroup;

	// open the window and make sure it's on top
	ratewindow=window.open(url,"rate_window","width=790,height=600,scrollbars=yes,resizable=yes");
 	ratewindow.focus(); 

   	return;
}

//////////////////////////////////////////////////////////////////
// Displays a daughter window containing entry fields for an email
// message to a user.
//////////////////////////////////////////////////////////////////
function openEmailWindow(
		InRecipientEmail, 
		InLoanId)
{
   	// if the window already exists, it may be behind another window.
   	// Close it and open it again to bring it to the top.
   	if(emailwindow && !emailwindow.closed) 
   	{
      	// Close it and open it again to bring it to the top.
 		emailwindow.close(); 
   	}

      var url = "/WebLoansServlet?Java_Class_Name=EmailUser&PageAction=ServePage&&RecipientEmailAddress=" + InRecipientEmail + "&LoanId=" + InLoanId;

	// set the height and width of the new window
	height = 250;
	width  = 420;

	// use the available screen and new window dimensions to center the window
	xLocation = ((screen.availWidth / 2)  - (width / 2)); 
	yLocation = ((screen.availHeight / 2) - (height / 2)) 

	// open the window and make sure it's on top
	emailwindow=window.open(url,"email_window","width=" + width + ",height=" + height + ",scrollbars=yes,resizable=yes");
      emailwindow.moveTo(xLocation, yLocation);
 	emailwindow.focus(); 

   	return;
}

//////////////////////////////////////////////////////////////////
// Determines if the input string contains only whitespace.
//////////////////////////////////////////////////////////////////
function isBlank(s) {
   if (s == "") return true
   for (var i = 0; i < s.length; i++) {
      var c = s.charAt(i);
      if ((c != " ") && (c != '\n') && (c != '\t')) return false;
   }
   return true; 
}

//////////////////////////////////////////////////////////////////
// Determines if the input string represents a syntactically correct
// email address.
//////////////////////////////////////////////////////////////////
function isValidEmail(addr) {
   if ((addr.indexOf("@") == -1) || (addr.indexOf(".") == -1)) {
      return false;
   } else {
      return true;
   }
}

//////////////////////////////////////////////////////////////////
// returns true if the input string is a valid number.
//////////////////////////////////////////////////////////////////
function isNumber(s) {
   var rtn = ""
   // loop thru each character in the string
   for (var i = 0; i < s.length; i++) {
	// grab the next character
      var c = s.charAt(i);
	// if this char can be used in a number
      if ( ((c >= 0 ) && (c <= 9)) || c == '.' || c == ',' || c == '$') 
	{
	   // hang onto this one
         rtn += c
      }
      else
      {
         // no need to go any further
         return false
      }
   }

   // round the number
   if (!isBlank(rtn)) rtn = Math.round(rtn)

   // if this is really a number
   if (parseInt(rtn) == 0) {
      return false;
   } else {
      return true;
   }
}

//////////////////////////////////////////////////////////////////
// Removes everything except digits (0 - 9) from the input string.
// If the resultant string is zero, blank is returned.
//////////////////////////////////////////////////////////////////
function stripNumber(s) {
   var rtn = ""
   for (var i = 0; i < s.length; i++) {
      var c = s.charAt(i);
      if (((c >= 0 ) && (c <= 9)) || c == '.') {
           rtn += c
      }
   }
   if (!isBlank(rtn)) rtn = Math.round(rtn)
   if (parseInt(rtn) != 0) {
      return rtn;
   } else {
      return "";
   }
}

//////////////////////////////////////////////////////////////////
// Removes everything except digits (0 - 9) from the input string.
// If the resultant string is zero, "0" is returned.
//////////////////////////////////////////////////////////////////
function stripInterest(s) 
{
   var rtn = ""
   for (var i = 0; i < s.length; i++) {
      var c = s.charAt(i);
      if (((c >= 0 ) && (c <= 9)) || c == '.' || c == '-') {
           rtn += c
      }
   }
   if (rtn == 0) {
      return "";
   } else {
      return rtn;
   }
}

//////////////////////////////////////////////////////////////////
// Removes everything except digits (0 - 9) from the input string.
// If the resultant string is zero, "0" is returned.  This function
// rounds the return value.
//////////////////////////////////////////////////////////////////
function stripNumberAllowZero(s) {
   var rtn = ""
   for (var i = 0; i < s.length; i++) {
      var c = s.charAt(i);
      if (((c >= 0 ) && (c <= 9)) || c == '.') {
           rtn += c
      }
   }
   if (!isBlank(rtn)) rtn = Math.round(rtn)
   return rtn;
}

//////////////////////////////////////////////////////////////////
// Removes everything except digits (0 - 9) from the input string.
// If the resultant string is zero, "0" is returned.  This function
// does not round the return value.
//////////////////////////////////////////////////////////////////
function stripNumberAllowZeroNoRound(s) {
   var rtn = ""
   for (var i = 0; i < s.length; i++) {
      var c = s.charAt(i);
      if (((c >= 0 ) && (c <= 9)) || c == '.') {
           rtn += c
      }
   }
   if (isBlank(rtn)) rtn = 0
   return rtn;
}

//////////////////////////////////////////////////////////////////
// Determines if the input appears to be a decimal number.
//////////////////////////////////////////////////////////////////
function isDecimal(v) 
{
   var myNum = stripNumberAllowZeroNoRound(v);  
   if (myNum.indexOf(".") == -1) 
   {
      return false
   } 
   else 
   {
      return true
   }
}

//////////////////////////////////////////////////////////////////
// Determines if the input Select list index represents an
// unselected entry "[Select]"
//////////////////////////////////////////////////////////////////
function isSelected(v) {
   if (v < 0) {
      return false
   } else {
      return true
   }
}

//////////////////////////////////////////////////////////////////
// Determines if the input State Select list index represents an
// unselected entry "[Select]".  The state select lists use the
// string value "XX" to denote the [Select] item.
//////////////////////////////////////////////////////////////////
function isStateSelected(v) {
   // Although "Select" (with an internal value "XX") is
   // the first item in the drop down list, it is not
   // actually Selected.  IE returns "XX" if the user does not
   // select a state, nowever Netscape returns null
   if ( (v == "XX") || (v == null) ) {
      return false
   } else {
      return true
   }
}

//////////////////////////////////////////////////////////////////
// Determines if the input string begins with a letter.
//////////////////////////////////////////////////////////////////
function startsWithLetter(s) 
{
   if (s.length == 0) 
   {
      return false;
   } 

   var c = s.charAt(0);

   if ( ((c >= "a" ) && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
   {
      return true;
   } 
   return false;
}

//////////////////////////////////////////////////////////////////
// Removes everything except letters (a - z) and spaces from 
// the input string.
//////////////////////////////////////////////////////////////////
function stripLetters(s) {
   var rtn = ""
   for (var i = 0; i < s.length; i++) {
      var c = s.charAt(i);
      if (((c >= "a" ) && (c <= "z")) || ((c >= "A") && (c <= "Z")) || (c == " ")) {
           rtn += c
      }
   }
   return rtn;
}

//////////////////////////////////////////////////////////////////
// Removes everything except letters (a - z), spaces and "-" from 
// the input string.
//////////////////////////////////////////////////////////////////
function stripName(s) 
{
   var rtn = "";
   for (var i = 0; i < s.length; i++) 
   {
      var c = s.charAt(i);
      if ( ((c >= "a" ) && (c <= "z")) || ((c >= "A") && (c <= "Z")) || (c == " ") || (c == "-") ) 
	{
           rtn += c;
      }
   }
   return rtn;
}

//////////////////////////////////////////////////////////////////
// Removes everything except letters (a - z) from the input string.
//////////////////////////////////////////////////////////////////
function stripLettersAndSpaces(s) {
   var rtn = ""
   for (var i = 0; i < s.length; i++) {
      var c = s.charAt(i);
      if (((c == " " )) || ((c >= "a" ) && (c <= "z")) || ((c >= "A") && (c <= "Z"))) {
           rtn += c
      }
   }
   return rtn;
}

//////////////////////////////////////////////////////////////////
// Removes everything except digits (0 - 9) from the input string.
//////////////////////////////////////////////////////////////////
function stripZip(s) {
   var rtn = ""
   for (var i = 0; i < s.length; i++) {
      var c = s.charAt(i);
      if ((c >= 0 ) && (c <= 9)) {
         rtn += c
      }
   }
      return rtn;
 }

//////////////////////////////////////////////////////////////////
// Removes everything except digits (0 - 9) from the input string.
//////////////////////////////////////////////////////////////////
function stripPhone(s) {
   var rtn = ""
   for (var i = 0; i < s.length; i++) {
      var c = s.charAt(i);
      if ((c >= 0 ) && (c <= 9) && (c != ' ')) {
         rtn += c
      }
   }
      return rtn;
}

//////////////////////////////////////////////////////////////////
// Removes everything except digits (0 - 9) from the input string.
//////////////////////////////////////////////////////////////////
 function stripSSN(s) {
   var rtn = ""
   for (var i = 0; i < s.length; i++) {
      var c = s.charAt(i);
      if ((c >= 0 ) && (c <= 9) && (c != ' ')) {
         rtn += c
      }
   }
      return rtn;
 }
 

//////////////////////////////////////////////////////////////////////
// Edit the Form
// This function steps through all text and password fields placing the 
// cursor in each if it is blank - this moves the cursor down the page
// as you fill out the page - 
// Best used with a function that tests for the keypress event and traps
// the enter key
//////////////////////////////////////////////////////////////////////
function checkForm(form)
{
    var inputStr;
	for (var  i = 0; i < form.elements.length; i++)
	{
	  //alert ("form.elements = " + form.elements[i] + " - " + form.elements[i].type );
	  if (form.elements[i].type  == "text" || form.elements[i].type  == "password" )
	    if (form.elements[i].value == "")				
	    {
	      setFocusToField(form.elements[i]);
		//alert("Please Fill out ALL Fields");			
		return false;
	    }
	    else
	    {
            //Set to Upper case
	      //inputStr = form.elements[i].value.toUpperCase();
		//form.elements[i].value = inputStr;		
	    }
	  }
	  return true;
}


//////////////////////////////////////////////////////////////////////
// This function will make sure the input string represents a valid
// date in mm/dd/yyyy format.
//////////////////////////////////////////////////////////////////////
function isValidDate(str)

   {
   var format=0;
   var month=0;
   var day=0;
   var year=0;
   var err=false;

   if(str.length<8  || str.length>10)
      err=true;

   if(str.length>=8  && str.length<=10)
      {
      // mm/dd/yyyy format
      if(str.charAt(2) == "/" && str.charAt(5) == "/" && str.length == 10)
         {
         format=1;
         // Check that month is a number.
         for (var i = 0; i < 2; i++) 
            { var ch = str.substring(i, i + 1); if(ch < "0" || "9" < ch) err=true; }
         // Check that day is a number.
         for (var i = 3; i < 5; i++) 
            { var ch = str.substring(i, i + 1); if(ch < "0" || "9" < ch) err=true; }
         // Check that year is a number.
         for (var i = 6; i < 10; i++) 
            { var ch = str.substring(i, i + 1); if(ch < "0" || "9" < ch) err=true; }
         // Set variables
         if(err==false)
            { month=eval(str.substring(0,2)); day=eval(str.substring(3,5)); year=eval(str.substring(6,str.length)); }
         }

      // m/dd/yyyy format
      if(str.charAt(1) == "/" && str.charAt(4) == "/" && str.length == 9)
         {
         format=2;
         // Check that month is a number.
         for (var i = 0; i < 1; i++) 
            { var ch = str.substring(i, i + 1); if(ch < "0" || "9" < ch) err=true; }
         // Check that day is a number.
         for (var i = 2; i < 4; i++) 
            { var ch = str.substring(i, i + 1); if(ch < "0" || "9" < ch) err=true; }
         // Check that year is a number.
         for (var i = 5; i < 9; i++) 
            { var ch = str.substring(i, i + 1); if(ch < "0" || "9" < ch) err=true; }
         // Set variables
         if(err==false)
            { month=eval(str.substring(0,1)); day=eval(str.substring(2,4)); year=eval(str.substring(5,str.length)); }
         }

      // mm/d/yyyy format
      if(str.charAt(2) == "/" && str.charAt(4) == "/" && str.length == 9)
         {
         format=3;
         // Check that month is a number.
         for (var i = 0; i < 2; i++) 
            { var ch = str.substring(i, i + 1); if(ch < "0" || "9" < ch) err=true; }
         // Check that day is a number.
         for (var i = 3; i < 4; i++) 
            { var ch = str.substring(i, i + 1); if(ch < "0" || "9" < ch) err=true; }
         // Check that year is a number.
         for (var i = 5; i < 9; i++) 
            { var ch = str.substring(i, i + 1); if(ch < "0" || "9" < ch) err=true; }
         // Set variables
         if(err==false)
            { month=eval(str.substring(0,2)); day=eval(str.substring(3,4)); year=eval(str.substring(5,str.length)); }
         }

      // m/d/yyyy format
      if(str.charAt(1) == "/" && str.charAt(3) == "/" && str.length == 8)
         {
         format=4;
         // Check that month is a number.
         for (var i = 0; i < 1; i++) 
            { var ch = str.substring(i, i + 1); if(ch < "0" || "9" < ch) err=true; }
         // Check that day is a number.
         for (var i = 2; i < 3; i++) 
            { var ch = str.substring(i, i + 1); if(ch < "0" || "9" < ch) err=true; }
         // Check that year is a number.
         for (var i = 4; i < 8; i++) 
            { var ch = str.substring(i, i + 1); if(ch < "0" || "9" < ch) err=true; }
         // Set variables
         if(err==false)
            { month=eval(str.substring(0,1)); day=eval(str.substring(2,3)); year=eval(str.substring(4,str.length)); }
         }

      // Incorrect format.
      if(format !=1 && format !=2 && format !=3 && format !=4)
         var err=true;
      }

   // Check that month is between 1 &12.
   if(month<=0 || month>=13)
      err=true;

  // Check that day is right depending on month.
   if( month==2 && ((year/4)==parseInt(year/4)) )
      { if(day<=0 || day>29) err=true; }
   if( month==2 && ((year/4)!=parseInt(year/4)) )
      { if(day<=0 || day>28) err=true; }
   if( month==4 || month==6 || month==9 || month==11 )
      { if(day<=0 || day>30) err=true; }
   if( month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12 )
      { if(day<=0 || day>31) err=true; }

   // Check that year is OK
   //if(year<=1990 || year>2100) err=true;

   // If anything appears incorrect, display error message.
   if(err==true)
      {
      //alert("\nPlease re-enter the date.");
      return false;
      }

   // If all is OK, return true.
   //alert("\nDate is filled in properly!");

   return true;

   }


//////////////////////////////////////////////////////////////////////
// This function is called during the form load event.  It performs
// load related processing that is common to all pages.
//////////////////////////////////////////////////////////////////////
function CommonLoadProcessing()
{ 
   // determine if the user is running a Netscape browser
   var isNav = (navigator.appName.indexOf("Netscape") !=-1);

   // Netscape Browser requires us to capture events before we can
   // onstall an onleypress event handler
   if (isNav) {document.captureEvents(Event.KEYPRESS) }

   // tell the browser to call the checkkey event when the user
   // presses any key
   document.onkeypress = checkKey
}

//////////////////////////////////////////////////////////////////////
// Check for enter key, if so edit and submit
//////////////////////////////////////////////////////////////////////
function checkKey(evt)
{
  var isNav = (navigator.appName.indexOf("Netscape") !=-1);

  whichASC = (isNav) ? evt.which : event.keyCode;
  if (whichASC == "13")
  {
//     document.loan_form.submit(); 
     submitForm("Next"); 
  }
}

//////////////////////////////////////////////////////////////////////
// This javascript function actually does nothing.  It can be handy
// to use as an href (<a href="javascript:DoNothing">) for those 
// cases where the href is just being used to supply a tooltip
// via the <a title="tooltip"> attribute.
//////////////////////////////////////////////////////////////////////
function DoNothing()
{ 
}

//////////////////////////////////////////////////////////////////////
// selected item
//////////////////////////////////////////////////////////////////////
function SelectedValue(SelectObject, DefaultValue)
{ 
	var ReturnValue = DefaultValue;

	// loop thru all options
   	for (var i = 0; i < SelectObject.options.length; i++) 
	{
	   // if this option is selected
	   if (SelectObject.options[i].selected)
	   {
		return SelectObject.options[i].value;
	   }
	}
	return DefaultValue;
}

///////////////////////////////////////////////////////////////////////
// This function will return the value assigned to the currently
// selected Radio button.  Unlike other form elements for which
// form.element.value returns the current value, radio button
// operate differently.  This function mimics the action that
// "should" happen if form.radioButtonName.value operated
// like all the others.
//////////////////////////////////////////////////////////////////////
function RadioButtonValue(RadioButtonName)
{ 
	// loop thru all possible radio button values
   	for (var i = 0; i < RadioButtonName.length; i++) 
	{
	   // if this one is selected
	   if (RadioButtonName[i].checked == true)
	   {
		// return its value
		return RadioButtonName[i].value;
	   }
	}
	// we should never get here since one radio button should always be selected
	return -1;
}

///////////////////////////////////////////////////////////////////////
// This function will visually display the inut element.
//////////////////////////////////////////////////////////////////////
function ShowElement(ElementId)
{ 
	// make sure the element exists
	if (document.getElementById(ElementId) != null)
	{
		// hide "update in progress" text and display
		document.getElementById(ElementId).style.display="";
		document.getElementById(ElementId).style.visibility="visible";
	}
	else
	{
//		alert('Could not find HTML element with id attribute = "'+ElementId+'"');
	}
}

///////////////////////////////////////////////////////////////////////
// This function will visually hide the inut element.
//////////////////////////////////////////////////////////////////////
function HideElement(ElementId)
{ 
	// make sure the element exists
	if (document.getElementById(ElementId) != null)
	{
		// hide "update in progress" text and display
		document.getElementById(ElementId).style.display="none";
		document.getElementById(ElementId).style.visibility="hidden";
	}
	else
	{
//		alert('Could not find HTML element with id attribute = "'+ElementId+'"');
	}
}

//////////////////////////////////////////////////////////////////
// Displays a daughter window containing mortgage tradelines from
// the credit report.
//	HolderHtmlField  - HTML Form field that contains the mortgage holder name
//	BalanceHtmlField - HTML Form field that contains the mortgage balance
//	MonthlyHtmlField - HTML Form field that contains the mortgage monthly payment
//
// NOTE: If no HTML field exists for any of these parameters, pass the string 'none'.
//////////////////////////////////////////////////////////////////
function openTradeLineChooserWindow(
			AssociatedTradeLineIdHtmlField, 
			HolderHtmlField, 
			BalanceHtmlField, 
			MonthlyHtmlField,
			Num30LateHtmlField,
			Num60LateHtmlField,
			Num90LateHtmlField,
			DateAccountOpenedHtmlFieldName)
{
   // this will cause the servlet to query DB for info about the input zip code
   var url = "/WebLoansServlet?Java_Class_Name=MortgageTradelineChooser&PageAction=ServePage&HolderHtmlFieldName=" + HolderHtmlField.name + "&BalanceHtmlFieldName=" + BalanceHtmlField.name + "&MonthlyHtmlFieldName=" + MonthlyHtmlField.name + "&Num30DaysLateHtmlFieldName=" + Num30LateHtmlField.name + "&Num60DaysLateHtmlFieldName=" + Num60LateHtmlField.name + "&Num90DaysLateHtmlFieldName=" + Num90LateHtmlField.name + "&AssociatedTradeLineIdHtmlFieldName=" + AssociatedTradeLineIdHtmlField.name + "&DateAccountOpenedHtmlFieldName=" + DateAccountOpenedHtmlFieldName;

   // if the window already exists, it may be behind another window.
   // Close it and open it again to bring it to the top.
   if(mwindow && !mwindow.closed) 
   {
      // Close it and open it again to bring it to the top.
 	mwindow.close(); 
   }

   // open the zip selection window
   mwindow = window.open(url,"mortgage_chooser_window","width=600,height=300,scrollbars=yes,resizable=yes");

   return;
}
//////////////////////////////////////////////////////////////////////
// This function will determine the number of selected items exist
// in the input Select list form element.
//////////////////////////////////////////////////////////////////////
function NumberOfOptionsSelected(SelectObject)
{ 
	var NumSelected = 0;

	// loop thru all options
   	for (var i = 0; i < SelectObject.options.length; i++) 
	{
	   // if this option is selected
	   if (SelectObject.options[i].selected)
	   {
		NumSelected++;
	   }
	}
	return NumSelected;
}
