﻿// JScript File
function validateForm(theForm)
{
	var flag;
	
    if (theForm.LoanAmount.selectedIndex == 0)
    {
        alert("You are required to choose a LOAN AMOUNT.");
        theForm.LoanAmount.focus();
        return (false);
    }
	
    //  VALIDATE BORROWER NAME
    if (theForm.BorrowerFirstName.value.trim().length == 0)
    {
        alert("You are required to provide a borrower's FIRST NAME.");
        theForm.BorrowerFirstName.focus();
        return (false)
    }

    if (theForm.BorrowerLastName.value.trim().length ==0)
    {
        alert("You are required to provide a borrower's LAST NAME.");
        theForm.BorrowerLastName.focus();
        return (false)
    }

    if (theForm.BorrowerMiddleInitial.value.trim().length ==0)
    {
        alert("You are required to provide a borrower's MIDDLE INITIAL.");
        theForm.BorrowerMiddleInitial.focus();
        return (false)
    }

    if (theForm.BorrowerStreetAddress.value.trim().length ==0)
    {
        alert("You are required to provide a borrower's ADDRESS.");
        theForm.BorrowerStreetAddress.focus();
        return (false)
    }

    if (theForm.BorrowerCity.value.trim().length ==0)
    {
        alert("You are required to provide a borrower's CITY.");
        theForm.BorrowerCity.focus();
        return (false)
    }

    if (theForm.BorrowerState.selectedIndex == 0)
    {
        alert("You are required to provide a borrower's STATE.");
        theForm.BorrowerState.focus();
        return (false);
    }

    if (theForm.BorrowerZipCode.value.trim().length ==0)
    {
        alert("You are required to provide a borrower's ZIP CODE.");
        theForm.BorrowerZipCode.focus();
        return (false)
    }

    //  VALIDATE EMAIL ADDRESS
    if (theForm.BorrowerEmail.value.trim().length == 0 || theForm.BorrowerEmail.value.indexOf("@") < 0)
    {
        alert("You have entered an invalid EMAIL ADDRESS.");
        theForm.BorrowerEmail.focus();
        return (false);
    }
    
    if (theForm.BorrowerHomePhone1.value.trim().length != 3 || theForm.BorrowerHomePhone2.value.trim().length != 3 || theForm.BorrowerHomePhone3.value.trim().length != 4)
    {
        alert("You have entered an invalid HOME PHONE.");
        theForm.BorrowerHomePhone1.focus();
        return (false);
    }
    
    //  VALIDATE DOB
    if (theForm.DOB_Month.selectedIndex == 0 || theForm.DOB_Month.options[theForm.DOB_Month.selectedIndex].text.length == 0 || theForm.DOB_Day.options[theForm.DOB_Day.selectedIndex].text.length == 0 || theForm.DOB_Year.options[theForm.DOB_Year.selectedIndex].text.length == 0)
    {
        alert("You are required to provide a borrower's DATE OF BIRTH.");
        theForm.DOB_Month.focus();
        return (false);
    }

    //  VALIDATE BORROWER SSN
    if (theForm.BorrowerSSN1.value.trim().length != 3 || theForm.BorrowerSSN2.value.trim().length != 2 || theForm.BorrowerSSN3.value.trim().length != 4)
    {
        alert("You have entered an invalid SOCIAL SECURITY NUMBER.");
        theForm.BorrowerSSN1.focus();
        return (false);
    }

	flag = false;
	for (var i=0; i < theForm.PropertyType.length; i++)
	{
		if (theForm.PropertyType[i].checked)
		{
			  flag = true;
		}
	}

	if (!flag)
	{
		  alert("You have not indicated if RENT, OWN, OTHER.");
		  theForm.PropertyType[0].focus();
		  return (false);
	}
	
    if (theForm.DriversLicNo.value.trim().length == 0)
    {
        alert("You are required to provide a borrower's DRIVER LICENSE NO.");
        theForm.DriversLicNo.focus();
        return (false);
    }

    if (theForm.DriversLicState.selectedIndex == 0)
    {
        alert("You are required to provide a borrower's ISSUING STATE.");
        theForm.DriversLicState.focus();
        return (false);
    }

    if (theForm.BankName.value.trim().length == 0)
    {
        alert("You are required to provide a borrower's BANK NAME.");
        theForm.BankName.focus();
        return (false);
    }

    //  VALIDATE BANK ROUTING NUMBER
    if ((theForm.RoutingNo.value.trim().length < 8) || (theForm.RoutingNo.value.trim().length > 9))
    {
        alert("You have entered an invalid BANK ROUNTING NUMBER. Must be an 8-digit or 9-digit number.");
        theForm.RoutingNo.focus();
        return (false);
    }

    if (theForm.AccountType.selectedIndex == 0 || theForm.AccountType.options[theForm.AccountType.selectedIndex].text.length == 0)
    {
        alert("You are required to provide a borrower's ACCOUNT TYPE.");
        theForm.AccountType.focus();
        return (false);
    }

    //  VALIDATE BANK ACCOUNT NUMBER
    if (theForm.AccountNo.value.trim().length == 0)
    {
        alert("You are required to provide a borrower's BANK ACCOUNT NUMBER.");
        theForm.AccountNo.focus();
        return (false);
    }

    if (theForm.BankPhone1.value.trim().length != 3 || theForm.BankPhone2.value.trim().length != 3 || theForm.BankPhone3.value.trim().length != 4)
    {
        alert("You have entered an invalid BANK PHONE NUMBER.");
        theForm.BankPhone1.focus();
        return (false);
    }

	flag = false;
	for (var i=0; i < theForm.Account30Plus.length; i++)
	{
		if (theForm.Account30Plus[i].checked)
		{
			  flag = true;
		}
	}

	if (!flag)
	{
		  alert("You have not indicated if the ACCOUNT HAS BEEN OPEN FOR 30 DAYS.");
		  theForm.Account30Plus[0].focus();
		  return (false);
	}

    if (theForm.RefFirstName.value.trim().length == 0)
    {
        alert("You are required to provide a REFERENCE FIRST NAME.");
        theForm.RefFirstName.focus();
        return (false);
    }

    if (theForm.RefPhone1.value.trim().length != 3 || theForm.RefPhone2.value.trim().length != 3 || theForm.RefPhone3.value.trim().length != 4)
    {
        alert("You have entered an invalid REFERENCE PHONE NUMBER.");
        theForm.RefPhone1.focus();
        return (false);
    }

    if (theForm.RefLastName.value.trim().length == 0)
    {
        alert("You are required to provide a REFERENCE LAST NAME.");
        theForm.RefLastName.focus();
        return (false);
    }

    if (theForm.RefRelationship.value.trim().length == 0)
    {
        alert("You are required to provide a REFERENCE RELATIONSHIP.");
        theForm.RefRelationship.focus();
        return (false);
    }

    if (theForm.EmployerName.value.trim().length == 0)
    {
        alert("You are required to provide an EMPLOYER NAME.");
        theForm.EmployerName.focus();
        return (false);
    }

    if (theForm.EmployerCity.value.trim().length == 0)
    {
        alert("You are required to provide an EMPLOYER CITY.");
        theForm.EmployerCity.focus();
        return (false);
    }

    if (theForm.EmployerState.selectedIndex == 0)
    {
        alert("You are required to provide an EMPLOYER STATE.");
        theForm.EmployerState.focus();
        return (false);
    }

    if (theForm.EmployerZip.value.trim().length == 0)
    {
        alert("You are required to provide an EMPLOYER ZIP CODE.");
        theForm.EmployerZip.focus();
        return (false);
    }

    if (theForm.WorkPhone1.value.trim().length != 3 || theForm.WorkPhone2.value.trim().length != 3 || theForm.WorkPhone3.value.trim().length != 4)
    {
        alert("You have entered an invalid EMPLOYER PHONE NUMBER.");
        theForm.WorkPhone1.focus();
        return (false);
    }

    if (theForm.EmploymentPosition.value.trim().length == 0)
    {
        alert("You are required to provide a JOB POSITION.");
        theForm.EmploymentPosition.focus();
        return (false);
    }

    if (theForm.EmployerState.selectedIndex == 0)
    {
        alert("You are required to provide an EMPLOYER STATE.");
        theForm.EmployerState.focus();
        return (false);
    }

    if (theForm.MonthlyIncome.selectedIndex == 0 || theForm.MonthlyIncome.options[theForm.MonthlyIncome.selectedIndex].text.length == 0)
    {
        alert("You are required to provide a borrower's NET MONTHLY INCOME.");
        theForm.MonthlyIncome.focus();
        return (false);
    }

    //  VALIDATE PAY FREQ
    if (theForm.PayFrequency.selectedIndex == 0 || theForm.PayFrequency.options[theForm.PayFrequency.selectedIndex].text.length == 0)
    {
        alert("You are required to provide a borrower's PAY FREQUENCY.");
        theForm.PayFrequency.focus();
        return (false);
    }
    
    if (theForm.LastPayDay.value.trim().length == 0)
    {
        alert("You are required to provide LAST PAY DAY.");
        theForm.LastPayDay.focus();
        return (false);
    }

    if (theForm.NextPayDay1.value.trim().length == 0)
    {
        alert("You are required to provide NEXT PAY DAY.");
        theForm.NextPayDay1.focus();
        return (false);
    }

    if (theForm.NextPayDay2.value.trim().length == 0)
    {
        alert("You are required to provide 2ND PAY DAY.");
        theForm.NextPayDay2.focus();
        return (false);
    }

    if (theForm.DateOfHire.value.trim().length == 0)
    {
        alert("You are required to provide DATE OF HIRE.");
        theForm.DateOfHire.focus();
        return (false);
    }

    return (true);
}

function validateShortForm(theForm)
{
    if (theForm.LoanAmount.selectedIndex == 0)
    {
        alert("You are required to choose a LOAN AMOUNT.");
        theForm.LoanAmount.focus();
        return (false);
    }

    //  VALIDATE BORROWER NAME
    if (theForm.BorrowerFirstName.value.trim().length == 0)
    {
        alert("You are required to provide a borrower's FIRST NAME.");
        theForm.BorrowerFirstName.focus();
        return (false)
    }

    if (theForm.BorrowerLastName.value.trim().length ==0)
    {
        alert("You are required to provide a borrower's LAST NAME.");
        theForm.BorrowerLastName.focus();
        return (false)
    }

    if (theForm.BorrowerMiddleInitial.value.trim().length ==0)
    {
        alert("You are required to provide a borrower's MIDDLE INITIAL.");
        theForm.BorrowerMiddleInitial.focus();
        return (false)
    }

    //  VALIDATE EMAIL ADDRESS
    if (theForm.BorrowerEmail.value.trim().length == 0 || theForm.BorrowerEmail.value.indexOf("@") < 0)
    {
        alert("You have entered an invalid EMAIL ADDRESS.");
        theForm.BorrowerEmail.focus();
        return (false);
    }
    
    //  VALIDATE BORROWER SSN
    if (theForm.BorrowerSSN1.value.trim().length != 3 || theForm.BorrowerSSN2.value.trim().length != 2 || theForm.BorrowerSSN3.value.trim().length != 4)
    {
        alert("You have entered an invalid SOCIAL SECURITY NUMBER.");
        theForm.BorrowerSSN1.focus();
        return (false);
    }
    
    if (theForm.NextPayDay1.value.trim().length == 0)
    {
        alert("You are required to provide NEXT PAY DAY.");
        theForm.NextPayDay1.focus();
        return (false);
    }

    if (theForm.NextPayDay2.value.trim().length == 0)
    {
        alert("You are required to provide 2ND PAY DAY.");
        theForm.NextPayDay2.focus();
        return (false);
    }

    //  VALIDATE PROMO CODE
    if (theForm.PromoCode.value.trim().length > 0)
    {   
        if (theForm.PromoCode.value.trim().toUpperCase() != "SILVER25" && theForm.PromoCode.value.trim().toUpperCase() != "GOLD20" && theForm.PromoCode.value.trim().toUpperCase() != "PLATINUM15")
        {
            alert("You have entered an invalid PROMOTIONAL CODE.");
            theForm.PromoCode.focus();
            return (false);
        }
    }
        
    if (theForm.MonthlyIncome.selectedIndex == 0 || theForm.MonthlyIncome.options[theForm.MonthlyIncome.selectedIndex].text.length == 0)
    {
        alert("You are required to provide a borrower's NET MONTHLY INCOME.");
        theForm.MonthlyIncome.focus();
        return (false);
    }

    return (true);
}

function validateAgreement(theForm)
{
    //  VALIDATE BORROWER SSN ENTRY
    if (theForm.BorrowerSSN1.value.trim().length != 3 || theForm.BorrowerSSN2.value.trim().length != 2 || theForm.BorrowerSSN3.value.trim().length != 4)
    {
        alert("You have entered an invalid social security number.");
        theForm.BorrowerSSN1.focus();
        return (false);
    }

    //  COMPARE ENTERED SSN WITH PREVIOUSLY ENTERED SSN
    if (theForm.BorrowerSSN1.value.trim() != theForm.PrevSSN1.value.trim() || theForm.BorrowerSSN2.value.trim() != theForm.PrevSSN2.value.trim() || theForm.BorrowerSSN3.value.trim() != theForm.PrevSSN3.value.trim())
    {
        alert("The SOCIAL SECURITY NUMBER you entered does not match the one you provided on the application.");
        theForm.BorrowerSSN1.focus();
        return (false);
    }
    
    return (true);
}

function validateContactForm(theForm)
{
    try
    {
        //  VALIDATE BORROWER NAME
        if (theForm.Name.value.trim().length == 0)
        {
            alert("You are required to provide a contact NAME.");
            theForm.Name.focus();
            return (false)
        }

        //  VALIDATE PHONE
        if (theForm.Phone1.value.trim().length != 3 || theForm.Phone2.value.trim().length != 3 || theForm.Phone3.value.trim().length != 4)
        {
            alert("You have entered an invalid PHONE NUMBER.");
            theForm.Phone1.focus();
            return (false);
        }

        //  VALIDATE EMAIL ADDRESS
        if (theForm.email.value.trim().length == 0 || theForm.email.value.indexOf("@") < 0)
        {
            alert("You have entered an invalid EMAIL ADDRESS.");
            theForm.email.focus();
            return (false);
        }

        //  VALIDATE EMAIL ADDRESS
        if (theForm.Comments.value.trim().length == 0)
        {
            alert("You are required to provide QUESTIONS/COMMENTS/PAYMENT REQUESTS information.");
            theForm.Comments.focus();
            return (false);
        }
            
        return (true);
    }
    catch(err)
    {
        alert(err.description);
        return (false);
    }
}

function viewAgreement(thisForm)
{
    thisForm.Application.action = "cgi-bin/agreement.pl";
    thisForm.Application.submit();
}

function autoTab(input,len, e) {
    var isNN = (navigator.appName.indexOf("Netscape")!=-1);
    var keyCode = (isNN) ? e.which : e.keyCode; var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46]; if(input.value.length >= len && !containsElement(filter,keyCode)) { input.value = input.value.slice(0, len);

    input.form[(getIndex(input)+1) % input.form.length].focus(); } function containsElement(arr, ele) { var found = false, index = 0; while(!found && index < arr.length) if(arr[index] == ele) found = true; else
    index++;

    return found;
}

function getIndex(input) {
    var index = -1, i = 0, found = false;
    while (i < input.form.length && index == -1) if (input.form[i] == input)index = i; else i++; return index; } return true;
}

function isNumberKey(evt)
{
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

    return true;
}

function isDateKey(evt)
{
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 47 || charCode > 57))
        return false;

    return true;
}

function isMissouri(ctl) 
{
    if (ctl.value == 'MO') {
        document.getElementById('MoLicThumb').style.display = 'inline';
        document.getElementById('MoLicThumb').style.visibility = 'visible';
    }
    else {
        document.getElementById('MoLicThumb').style.display = 'none';
        document.getElementById('MoLicThumb').style.visibility = 'hidden';
    }
}

