/**
 * File: validateform.js
 *
 * Author: Lawrence Stewart, 29/06/2005, lstewart@314.net.au
 *
 * This file contains functions used by the Loans Insurance website
 * (www.loansinsurance.com.au) to validate the required elements of
 * 2 separate forms: an online express application form and a customer
 * feedback form. The online express application form can be found in
 * the file expressloanappl.php sitting in the root directory of the website.
 * The online customer feedback form can be found in the file feedback_form.php
 * sitting in the root directory of the website.
 *
 */


/**
 * Function to determine if a form element was left empty when
 * it was submitted.
 *
 * @param formInputElement - the form element to check
 * @return true if the form element is empty, false otherwise
 */
function isEmpty(formInputElement)
{
        with(formInputElement)
        {
                if(value == null || value == "")
                {
                        return true;
                }
                else
                {
                        return false;
                }
        }
}

/**
 * Function to validate the contents of the loan amount form element.
 *
 * @param loanAmountInput - the loan amount form element to validate
 * @return true if the contents are valid, false otherwise
 */
function validateLoanAmount(loanAmountInput)
{
        //alert("loanAmountInput.value " + loanAmountInput.value);
        //alert("loanAmountInput.value.length " + loanAmountInput.value.length);

        if(isEmpty(loanAmountInput))
        {
                return false;
        }

        if(loanAmountInput.value.length == 1 && loanAmountInput.value.charAt(0) == '$')
        {
                return false;
        }

        return true;
}


/**
 * Function to validate the contents of the email address form element.
 *
 * @param emailInputElement - the email address form element to validate
 * @return true if the contents are valid, false otherwise
 */
function validateRequiredEmail(emailInputElement)
{
        if(isEmpty(emailInputElement) == false)
        {
                with(emailInputElement)
                {
                    apos=value.indexOf("@");
                    dotpos=value.lastIndexOf(".");
                    lastpos=value.length-1;

                    if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2)
                    {
                            return false;
                    }

                    else
                    {
                            return true;
                    }
            }
        }

        return false;
}



/**
 * Function to validate the contents of the email address form element.
 *
 * @param emailInputElement - the email address form element to validate
 * @return true if the contents are valid, false otherwise
 */
function validateNonRequiredEmail(emailInputElement)
{
        if(isEmpty(emailInputElement) == true)
        {
                return true;
        }

        else
        {
            with(emailInputElement)
            {
                apos=value.indexOf("@");
                dotpos=value.lastIndexOf(".");
                lastpos=value.length-1;

                if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2)
                {
                    return false;
                }

                else
                {
                    return true;
                }
            }
        }

        return false;
}

/**
 * Function to validate the contents of the phone number form elements.
 * As long as input has been provided for at least 1 element, it is considered
 * valid.
 *
 * @param phonebh - the business hours phone number form element to validate
 * @param phoneah - the after hours phone number form element to validate
 * @param phonemob - the mobile phone number form element to validate
 * @return true if at least one element's contents are valid, false otherwise
 */
function validatePhoneNumbers(phonebh, phoneah, phonemo)
{
        if(isEmpty(phonebh)
                && isEmpty(phoneah)
                        && isEmpty(phonemo))
        {
                return false;
        }

        return true;
}



/**
 * Function to validate the contents of the post code form element.
 *
 * @param postCode - the postCode form element to validate
 * @return true if the contents are valid, false otherwise
 */
function validatePostCode(postCode)
{
        if(isEmpty(postCode))
        {
                return false;
        }

        //if(isNaN(postCode.value) == false)
        //{
        //        return true;
        //}

        return true;
}

/*
 * Function to loop through a radio button group
 * and check to make sure that one has been selected.
 *
 * @param radioGroup - array of radio group options to check
 * @return true if one of the radio group options is checked, otherwise false
 */
function validateRadio(radioGroup)
{
        var i;

        //alert("radioGroup.length " + radioGroup.length);

        for(i = 0; i < radioGroup.length; i++)
        {
                if(radioGroup[i].checked)
                        return true;
        }

        return false;
}

/**
 * Function to validate the Loans Insurance express loan application
 * form contents before being submitted for processing. If a required
 * element is found to be invalid, a prompt is displayed to the user
 * and they can then go back and edit their submission and resubmit.
 *
 * @param document - the document containing the form elements to validate
 * @return true if all required elements are valid, false otherwise
 */
function validateExpressLoanApplication(document)
{

        with(document)
        {
                if(isEmpty(firstname) == true)
                {
                        alert("Please enter your first name");
                        firstname.focus();
                        return false;
                }


                if(isEmpty(lastname) == true)
                {
                        alert("Please enter your last name");
                        lastname.focus();
                        return false;
                }

                if(validatePhoneNumbers(phonebh, phoneah, phonemo) == false)
                {
                        alert("Please enter at least 1 contact phone number");
                        phonebh.focus();
                        return false;
                }

                if(isEmpty(address) == true)
                {
                        alert("Please enter your address");
                        address.focus();
                        return false;
                }

                if(isEmpty(suburb) == true)
                {
                        alert("Please enter your suburb");
                        suburb.focus();
                        return false;
                }

                if(validatePostCode(postcode) == false)
                {
                        alert("Please enter your post code");
                        postcode.focus();
                        return false;
                }

                if(validateRequiredEmail(email) == false)
                {
                        alert("Please enter a valid E-mail address");
                        email.focus();
                        return false;
                }

                if(preferredtime.selectedIndex == 0)
                {
                        alert("Please select a preferred contact time");
                        preferredtime.focus();
                        return false;
                }

                if(findus.selectedIndex == 0)
                {
                        alert("Please select an option for how you found us");
                        findus.focus();
                        return false;
                }

                if(validateLoanAmount(loanam) == false)
                {
                        alert("Please enter an estimated loan amount");
                        loanam.focus();
                        return false;
                }

                if(validateRadio(commercial) == false)
                {
                        alert("Please specify whether this application is for a commercial loan or not");
                        //commercial.focus();
                        return false;
                }

                if(validateRadio(signed) == false)
                {
                        alert("Please specify whether you have a signed contract of sale or not");
                        //signed.focus();
                        return false;
                }

                if(validateRadio(existing) == false)
                {
                        alert("Please specify whether you are refinancing an existing property or not");
                        //existing.focus();
                        return false;
                }


        }

        return true;
}







/**
 * Function to validate the Loans Insurance customer feedback
 * form contents before being submitted for processing. If a required
 * element is found to be invalid, a prompt is displayed to the user
 * and they can then go back and edit their submission and resubmit.
 *
 * @param document - the document containing the form elements to validate
 * @return true if all required elements are valid, false otherwise
 */
function validateFeedbackForm(document)
{
        with(document)
        {
                if(isEmpty(firstname) == true)
                {
                        alert("Please enter your first name");
                        firstname.focus();
                        return false;
                }

                if(isEmpty(lastname) == true)
                {
                        alert("Please enter your last name");
                        lastname.focus();
                        return false;
                }

                if(validatePhoneNumbers(phonebh, phoneah, phonemo) == false)
                {
                        alert("Please enter at least 1 contact phone number");
                        phonebh.focus();
                        return false;
                }

                if(validateNonRequiredEmail(email) == false)
                {
                        alert("Please enter a valid E-mail address");
                        email.focus();
                        return false;
                }

                if(preferredtime.selectedIndex == 0)
                {
                        alert("Please select a preferred contact time.");
                        preferredtime.focus();
                        return false;
                }
        }

        return true;
}






/**
 * Function to validate the Loans Insurance insurance enquiry/application
 * form contents before being submitted for processing. If a required
 * element is found to be invalid, a prompt is displayed to the user
 * and they can then go back and edit their submission and resubmit.
 *
 * @param document - the document containing the form elements to validate
 * @return true if all required elements are valid, false otherwise
 */
function validateInsuranceEnquiryForm(document)
{
        /* Validating the "Interested Products" field is hard.
         * The field is called "insuranceproducts[]" with an array notation
         * so that PHP will get an array of values from the request variable.
         *
         * But javascript doesnt like having an element called ...[] as it tries
         * to treat it as a javascript array.
         *
         * To get around this we access this element via its index (12) in the form.
         * If you change the items in the form, You may have to change this number.
         */
         interestedProductsFormId = 12;


        with(document)
    {
        if(isEmpty(firstname) == true)
        {
            alert("Please enter your first name");
            firstname.focus();
            return false;
        }

        if(isEmpty(lastname) == true)
        {
            alert("Please enter your last name");
            lastname.focus();
            return false;
        }

        if(validatePhoneNumbers(phonebh, phoneah, phonemo) == false)
        {
            alert("Please enter at least 1 contact phone number");
            phonebh.focus();
            return false;
        }

        if(validateNonRequiredEmail(email) == false)
                {
                        alert("Please enter a valid E-mail address");
                        email.focus();
                        return false;
                }

        if(preferredtime.selectedIndex == 0)
                {
                        alert("Please select a preferred contact time.");
                        preferredtime.focus();
                        return false;
                }

            } //end of with block.

                //We have exited the with block here so that we can access document
                // with array notation.
           if(document[interestedProductsFormId].selectedIndex < 0)
           {
                        alert("Please select an Insurance Product or Products");
              return false;
           }


        return true;
}
