function check(element)
{
    var elements = document.getElementsByName(element);
    var total = elements.length;
    for(i=0;i<total;i++)
        if(elements[i].checked)
            return true;
    return false;
}


function submit_app()
{

    var problems = "";

    if(!check('purpose_of_loan')){problems = problems+"- purpose of loan is required\n";}
    
    if(document.form1.amount_of_loan.value==""){problems = problems+"- amount of loan is required\n";}
    if(document.form1.first_name.value==""){problems = problems+"- applicant first name is required\n";}
    if(document.form1.last_name.value==""){problems = problems+"- applicant last name is required\n";}
    if(document.form1.sin.value==""){problems = problems+"- applicant SIN is required\n";}
    if(document.form1.date_of_birth.value==""){problems = problems+"- applicant date of birth is required\n";}
    if(!check('residential_status')){problems = problems+"- residential status is required\n";}
    
    if(document.form1.monthly_payment.value=="" && !document.form1.residential_status[3].checked){problems = problems+"- residential monthly payments is required\n";}
    
    if(document.form1.current_address.value==""){problems = problems+"- current address is required\n";}
    if(document.form1.city.value==""){problems = problems+"- current city is required\n";}
    if(document.form1.province.value==""){problems = problems+"- current province is required\n";}
    if(document.form1.postal_code.value==""){problems = problems+"- current postal code is required\n";}
    if(document.form1.years.value==""){problems = problems+"- years at the current residence is required\n";}
    if(document.form1.months.value==""){problems = problems+"- months at the current residence is required\n";}
    if(!check('employment_type')){problems = problems+"- employment type is required\n";}
    if(document.form1.current_employer.value=="" && (!document.form1.employment_type[2].checked && !document.form1.employment_type[4].checked && !document.form1.employment_type[3].checked)){problems = problems+"- current employer is required\n";}
    if(document.form1.occupation.value=="" && (!document.form1.employment_type[2].checked && !document.form1.employment_type[4].checked && !document.form1.employment_type[3].checked)){problems = problems+"- current occupation is required\n";} //???
    if(document.form1.length_of_employment.value=="" && (!document.form1.employment_type[2].checked && !document.form1.employment_type[4].checked && !document.form1.employment_type[3].checked)){problems = problems+"- length of employment is required\n";}
    if(!check('status_of_employment') && (!document.form1.employment_type[2].checked && !document.form1.employment_type[4].checked && !document.form1.employment_type[3].checked)){problems = problems+"- status of employment is required\n";}
    if(document.form1.work_number.value=="" && (!document.form1.employment_type[2].checked && !document.form1.employment_type[4].checked && !document.form1.employment_type[3].checked)){problems = problems+"- work phone number is required\n";}
    
    
    if(document.form1.gross_income1.value != "" && !check('income_type')){problems = problems+"- type of income 1 is required\n";}
    if(document.form1.gross_income2.value != "" && !check('income_type2')){problems = problems+"- type of income 2 is required\n";}
    if(document.form1.gross_income3.value != "" && !check('income_type3')){problems = problems+"- type of income 3 is required\n";}
    if(document.form1.gross_income4.value != "" && !check('income_type4')){problems = problems+"- type of income 4 is required\n";}
    if(document.form1.gross_income5.value != "" && !check('income_type5')){problems = problems+"- type of income 5 is required\n";}
    
    
    if(!check('previous_cosigner')){problems = problems+"- to answer if you have endorsed or co-signed a loan is required\n";}
    
    if(document.form1.previous_cosigner[0].checked)
    {
        if(document.form1.applicant_name.value==""){problems = problems+"\n- under the section \"Have you endorsed or co-signed a loan before\", if you select yes, then the \"Applicant\" field is required.";}
        if(document.form1.loan_amount.value==""){problems = problems+"\n- under the section \"Have you endorsed or co-signed a loan before\", if you select yes, then the \"Loan Amount\" field is required.";}
        if(document.form1.financial_institute.value==""){problems = problems+"\n- under the section \"Have you endorsed or co-signed a loan before\", if you select yes, then the \"Financial Institution\" field is required.";}
        if(document.form1.monthly_amount.value==""){problems = problems+"\n- under the section \"Have you endorsed or co-signed a loan before\", if you select yes, then the \"Monthly Amount\" field is required.";}
    }
    
    if (document.getElementById('email').value != "" && !(document.getElementById('email').value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\.biz)|(\.coop)|(\.info)|(\.name)|(\.pro)|(\.int)|(\.aero)|(\..{2,2}))$)\b/gi)))
    {
        problems = problems+"- your email address does not appear to be valid\n";
    }  

    if(!document.form1.contact_by_email.checked && !document.form1.contact_by_work_phone.checked && !document.form1.contact_by_home_phone.checked)
    {problems = problems+"- how would you like to be contacted when the request has been completed?  \n";}
    

    if(document.form1.contact_by_email.checked && document.getElementById('email').value == "")
    {
        problems = problems+"- under the section \"How would you like to be contacted when the request has been completed\", if you select E-mail, then the \"E-mail\" field is required.\n";
    }

    if(document.form1.contact_by_work_phone.checked && document.getElementById('work_phone').value == "")
    {
        problems = problems+"- under the section \"How would you like to be contacted when the request has been completed\", if you select Work Phone, then the \"Work Phone\" field is required.\n";
    }

    if(document.form1.contact_by_home_phone.checked && document.getElementById('home_phone').value == "")
    {
        problems = problems+"- under the section \"How would you like to be contacted when the request has been completed\", if you select Home Phone, then the \"Home Phone\" field is required.\n";
    }

    if (problems != "")
    {
        alert ("The following errors were encountered:\n"+problems+"\nPlease fix these problems and try again.");
    }
    else
    {
        document.getElementById('pancake').value = "Yummy!!!";
        document.getElementById('website').value = "";
        document.form1.submit();
    }
}




function submit_joint_app()
{
    var problems = "";
    if(!check('type_of_application')){problems = problems+"\n- type of application is required";}
    if(!check('purpose_of_loan')){problems = problems+"\n- purpose of loan is required";}
    if(document.form1.amount_of_loan.value==""){problems = problems+"\n- amount of loan is required";}
    if(document.form1.first_name.value==""){problems = problems+"\n- applicant first name is required";}
    if(document.form1.last_name.value==""){problems = problems+"\n- applicant last name is required";}
    if(document.form1.sin.value==""){problems = problems+"\n- applicant SIN is required";}
    if(document.form1.date_of_birth.value==""){problems = problems+"\n- applicant date of birth is required";}
    if(!check('residential_status')){problems = problems+"\n- applicant residential status is required";}
    if(document.form1.monthly_payment.value==""){problems = problems+"\n- applicant residential monthly payments is required";}
    if(document.form1.current_address.value==""){problems = problems+"\n- applicant current address is required";}
    if(document.form1.city.value==""){problems = problems+"\n- applicant current city is required";}
    if(document.form1.province.value==""){problems = problems+"\n- applicant current province is required";}
    if(document.form1.postal_code.value==""){problems = problems+"\n- applicant current postal code is required";}
    if(document.form1.years.value==""){problems = problems+"\n- years at the current residence is required";}
    if(document.form1.months.value==""){problems = problems+"\n- months at the current residence is required";}
    if(!check('employment_type')){problems = problems+"\n- applicant employment type is required";}
    if(document.form1.current_employer.value=="" && (!document.form1.employment_type[2].checked && !document.form1.employment_type[4].checked && !document.form1.employment_type[3].checked)){problems = problems+"\n- applicant current employer is required";}
    if(document.form1.occupation.value=="" && (!document.form1.employment_type[2].checked && !document.form1.employment_type[4].checked && !document.form1.employment_type[3].checked)){problems = problems+"\n- applicant current occupation is required";}
    if(document.form1.length_of_employment.value=="" && (!document.form1.employment_type[2].checked && !document.form1.employment_type[4].checked && !document.form1.employment_type[3].checked)){problems = problems+"\n- applicant length of employment is required";}
    if(!check('status_of_employment') && (!document.form1.employment_type[2].checked && !document.form1.employment_type[4].checked && !document.form1.employment_type[3].checked)){problems = problems+"\n- applicant status of employment is required";}
    
    if(document.form1.gross_income1.value != "" && !check('income_type')){problems = problems+"-\n applicant type of income 1 is required";}
    if(document.form1.gross_income2.value != "" && !check('income_type2')){problems = problems+"-\n applicant type of income 2 is required";}
    if(document.form1.gross_income3.value != "" && !check('income_type3')){problems = problems+"-\n applicant type of income 3 is required";}
    if(document.form1.gross_income4.value != "" && !check('income_type4')){problems = problems+"-\n applicant type of income 4 is required";}
    if(document.form1.gross_income5.value != "" && !check('income_type5')){problems = problems+"-\n applicant type of income 5 is required";}
    



    if(!check('previous_cosigner')){problems = problems+"\n- to answer if the applicant has endorsed or co-signed a loan is required";}
    if(document.form1.previous_cosigner[0].checked)
    {
        if(document.form1.applicant_name.value==""){problems = problems+"\n- under the section \"Have you endorsed or co-signed a loan before\", if you select yes, then the \"Applicant\" field is required.";}
        if(document.form1.loan_amount.value==""){problems = problems+"\n- under the section \"Have you endorsed or co-signed a loan before\", if you select yes, then the \"Loan Amount\" field is required.";}
        if(document.form1.financial_institute.value==""){problems = problems+"\n- under the section \"Have you endorsed or co-signed a loan before\", if you select yes, then the \"Financial Institution\" field is required.";}
        if(document.form1.monthly_amount.value==""){problems = problems+"\n- under the section \"Have you endorsed or co-signed a loan before\", if you select yes, then the \"Monthly Amount\" field is required.";}
    }
    if(document.form1.co_first_name.value==""){problems = problems+"\n- co-signer first name is required";}
    if(document.form1.co_last_name.value==""){problems = problems+"\n- co-signerlast name is required";}
    if(document.form1.co_sin.value==""){problems = problems+"\n- co-signer SIN is required";}
    if(document.form1.co_date_of_birth.value==""){problems = problems+"\n- co-signer date of birth is required";}
    if(!check('co_residential_status')){problems = problems+"\n- co-signer residential status is required";}
    if(document.form1.co_date_of_birth2.value==""){problems = problems+"\n- co-signer residential monthly payments is required";}
    if(document.form1.co_current_address.value==""){problems = problems+"\n- co-signer current address is required";}
    if(document.form1.co_city.value==""){problems = problems+"\n- co-signer current city is required";}
    if(document.form1.co_province.value==""){problems = problems+"\n- co-signer current province is required";}
    if(document.form1.co_postal_code.value==""){problems = problems+"\n- co-signer current postal code is required";}
    if(document.form1.co_years.value==""){problems = problems+"\n- co-signer years living at current residence is required";}
    if(document.form1.co_months.value==""){problems = problems+"\n- co-signer months living at current residence is required";}

    if(!check('co_employment_type')){problems = problems+"\n- co-signer employment type is required";}
    if(document.form1.co_current_employer.value=="" && (!document.form1.co_employment_type[2].checked && !document.form1.co_employment_type[4].checked && !document.form1.co_employment_type[3].checked)){problems = problems+"\n- co-signer current employer is required";}
    if(document.form1.co_occupation.value=="" && (!document.form1.co_employment_type[2].checked && !document.form1.co_employment_type[4].checked && !document.form1.co_employment_type[3].checked)){problems = problems+"\n- co-signer current occupation is required";}
    if(!check('co_status_of_employment') && (!document.form1.co_employment_type[2].checked && !document.form1.co_employment_type[4].checked && !document.form1.co_employment_type[3].checked)){problems = problems+"\n- co-signer status of employment is required";}
    if(document.form1.co_length_of_employment.value=="" && (!document.form1.co_employment_type[2].checked && !document.form1.co_employment_type[4].checked && !document.form1.co_employment_type[3].checked)){problems = problems+"\n- co-signer length of employment is required";}

    if(document.form1.co_gross1.value != "" && !check('co_income_type')){problems = problems+"\n- co-signer type of income 1 is required";}
    if(document.form1.co_gross2.value != "" && !check('co_income_type2')){problems = problems+"\n- co-signer type of income 2 is required";}
    if(document.form1.co_gross3.value != "" && !check('co_income_type3')){problems = problems+"\n- co-signer type of income 3 is required";}
    if(document.form1.co_gross4.value != "" && !check('co_income_type4')){problems = problems+"\n- co-signer type of income 4 is required";}
    if(document.form1.co_gross5.value != "" && !check('co_income_type5')){problems = problems+"\n- co-signer type of income 5 is required";}



    if(!check('co_previous_cosigner')){problems = problems+"\n- to answer if the co-signer has endorsed or co-signed a loan is required";}
    if(document.form1.co_previous_cosigner[0].checked)
    {
        
        if(document.form1.co_applicant_name2.value==""){problems = problems+"\n- under the section \"Has the co-signer endorsed or co-signed a loan before\", if you select yes, then the \"Applicant\" field is required.";}
        if(document.form1.co_loan_amount2.value==""){problems = problems+"\n- under the section \"Has the co-signer endorsed or co-signed a loan before\", if you select yes, then the \"Loan Amount\" field is required.";}
        if(document.form1.co_financial_institute2.value==""){problems = problems+"\n- under the section \"Has the co-signer endorsed or co-signed a loan before\", if you select yes, then the \"Financial Institution\" field is required.";}
        if(document.form1.co_monthly_amount2.value==""){problems = problems+"\n- under the section \"Has the co-signer endorsed or co-signed a loan before\", if you select yes, then the \"Monthly Amount\" field is required.";}
        
    }


    if (document.getElementById('email').value != "" && !(document.getElementById('email').value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\.biz)|(\.coop)|(\.info)|(\.name)|(\.pro)|(\.int)|(\.aero)|(\..{2,2}))$)\b/gi)))
    {
        problems = problems+"\n- your email address does not appear to be valid";
    }  

    if(!document.form1.contact_by_email.checked && !document.form1.contact_by_work_phone.checked && !document.form1.contact_by_home_phone.checked)
    {problems = problems+"\n- how would you like to be contacted when the request has been completed?  ";}
    

    if(document.form1.contact_by_email.checked && document.getElementById('email').value == "")
    {
        problems = problems+"\n- under the section \"How would you like to be contacted when the request has been completed\", if you select E-mail, then the \"E-mail\" field is required.";
    }

    if(document.form1.contact_by_work_phone.checked && document.getElementById('work_phone').value == "")
    {
        problems = problems+"\n- under the section \"How would you like to be contacted when the request has been completed\", if you select Work Phone, then the \"Work Phone\" field is required.";
    }

    if(document.form1.contact_by_home_phone.checked && document.getElementById('home_phone').value == "")
    {
        problems = problems+"\n- under the section \"How would you like to be contacted when the request has been completed\", if you select Home Phone, then the \"Home Phone\" field is required.";
    }

    if (problems != "")
    {
        alert ("The following errors were encountered:\n"+problems+"\n\nPlease fix these problems and try again.");
    }
    else
    {
        document.getElementById('pancake').value = "Yummy!!!";
        document.getElementById('website').value = "";
        document.form1.submit();
    }
}




function add_chequing()
{
    var problems = "";
    if(document.form1.first_name2.value==""){problems = problems+"\n- applicant first name is required";}
    if(document.form1.last_name2.value==""){problems = problems+"\n- applicant last name is required";}

    if(!check('branch')){problems = problems+"\n- branch is required";}
    if(!check('new_chequing_account_type')){problems = problems+"\n- the type of your new chequing account is required";}
    
   
    if (document.getElementById('email').value != "" && !(document.getElementById('email').value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\.biz)|(\.coop)|(\.info)|(\.name)|(\.pro)|(\.int)|(\.aero)|(\..{2,2}))$)\b/gi)))
    {
        problems = problems+"\n- your email address does not appear to be valid";
    }  

    if(!document.form1.contact_by_email.checked && !document.form1.contact_by_work_phone.checked && !document.form1.contact_by_home_phone.checked)
    {problems = problems+"\n- how would you like to be contacted when the request has been completed?  ";}
    

    if(document.form1.contact_by_email.checked && document.getElementById('email').value == "")
    {
        problems = problems+"\n- under the section \"How would you like to be contacted when the request has been completed\", if you select E-mail, then the \"E-mail\" field is required.";
    }

    if(document.form1.contact_by_work_phone.checked && document.getElementById('work_phone').value == "")
    {
        problems = problems+"\n- under the section \"How would you like to be contacted when the request has been completed\", if you select Work Phone, then the \"Work Phone\" field is required.";
    }

    if(document.form1.contact_by_home_phone.checked && document.getElementById('home_phone').value == "")
    {
        problems = problems+"\n- under the section \"How would you like to be contacted when the request has been completed\", if you select Home Phone, then the \"Home Phone\" field is required.";
    }
    
    

    if (problems != "")
    {
        alert ("The following errors were encountered:\n"+problems+"\n\nPlease fix these problems and try again.");
    }
    else
    {
        document.getElementById('pancake').value = "Yummy!!!";
        document.getElementById('website').value = "";
        document.form1.submit();
    }
}


function submit_payment_request()
{
    var problems = "";

    if(!check('branch')){problems = problems+"\n- Please select your branch";}
    if(document.form1.loan_loc_number.value==""){problems = problems+"\n- Loan / Line of Credit Number is required";}
    if(document.form1.full_name.value==""){problems = problems+"\n- Your first name and last name are required";}
    if(document.form1.full_name.value=="first name last name"){problems = problems+"\n- Your first name and last name are required";}
    if(document.form1.amount.value==""){problems = problems+"\n- The payment amount is required";}
    if(document.form1.account_number.value==""){problems = problems+"\n- Your account number/name is required";}
    if(!check('payment_type')){problems = problems+"\n- Please select how you want this payment to be applied";}
    if(document.form1.phone.value==""){problems = problems+"\n- Your phone number is required";}
    if(!check('agree_to_terms')){problems = problems+"\n- You must agree to the terms";}

    if (problems != "")
    {
        alert ("The following required fields were not completed:\n"+problems+"\n\nPlease fill in all the fields in the form and try again.");
    }
    else
    {
        document.getElementById('pancake').value = "Yummy!!!";
        document.getElementById('website').value = "";
        document.form1.action = "/send-payment-request.php";
        document.form1.submit();
    }
}





