function IsEmpty(a_strToTest)
{
    if ((a_strToTest.length == 0) || (a_strToTest == null))
    {
        // Value is empty
        return true;
    }
   else
   { 
       // Value is not empty
       return false;
   }
}

function IsValidEmail(a_strToTest)
{
    // Address must have at least 6 characters
    if (a_strToTest.length < 6)
    {
        // Not valid
        return false;
    }

    // @ must be the 2nd character at the very least
    if (a_strToTest.indexOf("@") < 1)
    {
        // Not valid
        return false;
    }

    // . must be present
    if (a_strToTest.indexOf(".") < 0)
    {
        // Not valid
        return false;
    }

    // Valid
    return true;
}

function IsNumeric(a_strToTest)
{
    // Create list of valid numeric characters
    v_strValidChars = "0123456789.-";

    // Test and return the result
    return AllCharsValid(v_strValidChars, a_strToTest);
}

function IsNumbers(a_strToTest)
{
    // Create list of valid number characters
    v_strValidChars = "0123456789";

    // Test and return the result
    return AllCharsValid(v_strValidChars, a_strToTest);
}

function AllCharsValid(a_strValidChars, a_strToTest)
{
    // Initialize variables
    v_bIsValid = true;

    // Iterate through characters in the string to be tested
    for (i = 0; i < a_strToTest.length && v_bIsValid == true; i++) 
    {
        // Get the character at position i
        v_cChar = a_strToTest.charAt(i);

       // Check to see if the character is in the list of valid characters
       if (a_strValidChars.indexOf(v_cChar) == -1) 
       {
           // Character is not valid
           v_bIsValid = false;
       }
    }

    // Return the result
    return v_bIsValid;
}

function ValidateOrder(a_frmForm)
{
    // At least one quantity must be > 0

    // Ashbory Bass
    if(a_frmForm.selQuantity01.value > 0)
    {
        // Valid
        return true;
    }

    // Ashbory Bundle
    if(a_frmForm.selQuantity02.value > 0)
    {
        // Valid
        return true;
    }

    // Strings
    if(a_frmForm.selQuantity03.value > 0)
    {
        // Valid
        return true;
    }

    // 3 sets strings (free Slyde-Rite)
    if(a_frmForm.selQuantity04.value > 0)
    {
        // Valid
        return true;
    }

    // Strap
    if(a_frmForm.selQuantity05.value > 0)
    {
        // Valid
        return true;
    }

    // Slyde-Rite
    if(a_frmForm.selQuantity06.value > 0)
    {
        // Valid
        return true;
    }

    // PT-100 Tuner
    if(a_frmForm.selQuantity09.value > 0)
    {
        // Valid
        return true;
    }

    // None of the quantities was > 0, so order is invalid

    // Alert user
    alert('Please select at least one item to order.');

    // Fail
    return false;
}

function ValidateCreditCardInfo(a_frmForm)
{
    // Credit Card Number must not be empty
    if(IsEmpty(a_frmForm.txtCreditCardNumber.value))
    {
        // Alert User
        alert('Please enter your Credit Card Number.');

        // Focus on control
        a_frmForm.txtCreditCardNumber.focus();

        // Fail
        return false;
    }

    // Credit Card Number must be numbers only
    if(!IsNumbers(a_frmForm.txtCreditCardNumber.value))
    {
        // Alert User
        alert('Your Credit Card Number must contain numbers only.');

        // Focus on control
        a_frmForm.txtCreditCardNumber.focus();

        // Fail
        return false;
    }

    // Credit Card Number must contain 16 digits
    if(a_frmForm.txtCreditCardNumber.value.length < 16)
    {
        // Alert User
        alert('Your Credit Card Number must contain 16 digits.');

        // Focus on control
        a_frmForm.txtCreditCardNumber.focus();

        // Fail
        return false;
    }

    // Security Code
    if(a_frmForm.selCreditCardType.value == "Amex")
    {
        // Security Code must have 3 or 4 digits
        if(a_frmForm.txtSecurityCode.value.length != 3 && a_frmForm.txtSecurityCode.value.length != 4)
        {
            // Alert User
            alert('Your credit card requires a 3 or 4-digit Security Code.');

            // Focus on control
            a_frmForm.txtSecurityCode.focus();

            // Fail
            return false;
        }
    }
    else
    {
        // Security Code must have 3 digits
        if(a_frmForm.txtSecurityCode.value.length != 3)
        {
            // Alert User
            alert('Your credit card requires a 3-digit Security Code.');

            // Focus on control
            a_frmForm.txtSecurityCode.focus();

            // Fail
            return false;
        }
    }

    // First Name must be entered
    if(IsEmpty(a_frmForm.txtFirstName.value))
    {
        // Alert User
        alert('Please enter your First Name.');

        // Focus on control
        a_frmForm.txtFirstName.focus();

        // Fail
        return false;
    }

    // Last Name must be entered
    if(IsEmpty(a_frmForm.txtLastName.value))
    {
        // Alert User
        alert('Please enter your Last Name.');

        // Focus on control
        a_frmForm.txtLastName.focus();

        // Fail
        return false;
    }

    // Phone Number must be entered
    if(IsEmpty(a_frmForm.txtContactPhone.value))
    {
        // Alert User
        alert('Please enter your Phone Number.');

        // Focus on control
        a_frmForm.txtContactPhone.focus();

        // Fail
        return false;
    }

    // Address must be entered
    if(IsEmpty(a_frmForm.txtAddress1.value))
    {
        // Alert User
        alert('Please enter your Street Address.');

        // Focus on control
        a_frmForm.txtAddress1.focus();

        // Fail
        return false;
    }

    // City must be entered
    if(IsEmpty(a_frmForm.txtCity.value))
    {
        // Alert User
        alert('Please enter your City.');

        // Focus on control
        a_frmForm.txtCity.focus();

        // Fail
        return false;
    }

    // Email Address must be valid
    if(!IsValidEmail(a_frmForm.txtEmailAddress.value))
    {
        // Alert User
        alert('Please enter a valid Email Address.');

        // Focus on control
        a_frmForm.txtEmailAddress.focus();

        // Fail
        return false;
    }

    // Form is valid
    return true;
}

function ValidateCheckInfo(a_frmForm)
{
    // First Name must be entered
    if(IsEmpty(a_frmForm.txtFirstName.value))
    {
        // Alert User
        alert('Please enter your First Name.');

        // Focus on control
        a_frmForm.txtFirstName.focus();

        // Fail
        return false;
    }

    // Last Name must be entered
    if(IsEmpty(a_frmForm.txtLastName.value))
    {
        // Alert User
        alert('Please enter your Last Name.');

        // Focus on control
        a_frmForm.txtLastName.focus();

        // Fail
        return false;
    }

    // Phone Number must be entered
    if(IsEmpty(a_frmForm.txtContactPhone.value))
    {
        // Alert User
        alert('Please enter your Phone Number.');

        // Focus on control
        a_frmForm.txtContactPhone.focus();

        // Fail
        return false;
    }

    // Address must be entered
    if(IsEmpty(a_frmForm.txtAddress1.value))
    {
        // Alert User
        alert('Please enter your Street Address.');

        // Focus on control
        a_frmForm.txtAddress1.focus();

        // Fail
        return false;
    }

    // City must be entered
    if(IsEmpty(a_frmForm.txtCity.value))
    {
        // Alert User
        alert('Please enter your City.');

        // Focus on control
        a_frmForm.txtCity.focus();

        // Fail
        return false;
    }

    // Email Address must be entered
    if(!IsValidEmail(a_frmForm.txtEmailAddress.value))
    {
        // Alert User
        alert('Please enter a valid Email Address.');

        // Focus on control
        a_frmForm.txtEmailAddress.focus();

        // Fail
        return false;
    }

    // Form is valid
    return true;
}

