﻿// JScript File

function validateEmpty(txtboxClientID, imgClientID)
{
    var txt = document.getElementById(txtboxClientID);
    var img = document.getElementById(imgClientID);
    
    if (trim(txt.value) != '')
    {
        img.src = '/images/icon-text-validation-ok.gif';
    }
    else
    {
        img.src = '/images/icon-text-validation.gif';
    }
}

function validatePassword(txtboxClientID, imgClientID)
{
    var txt = document.getElementById(txtboxClientID);
    var img = document.getElementById(imgClientID);
    
    if (txt.value.length >= 5)
    {
        img.src = '/images/icon-text-validation-ok.gif';
    }
    else
    {
        img.src = '/images/icon-text-validation.gif';
    }
}


function validateDropDownEmpty(dropdownClientID, imgClientID)
{
    var dropdown = document.getElementById(dropdownClientID);
    var img = document.getElementById(imgClientID);
    
    if (dropdown.selectedIndex > 0)
    {
        img.src = '/images/icon-text-validation-ok.gif';
    }
    else
    {
        img.src = '/images/icon-text-validation.gif';
    }
}

function validateChangePassword(txtboxNewPassword, txtboxConfirmPassword)
{
    var newPassword = document.getElementById(txtboxNewPassword).value;
    var confirmPassword = document.getElementById(txtboxConfirmPassword).value;
    
    if (newPassword != confirmPassword)
    {
        alert('New passwords must match.');
        return false;
    }
    
    if (newPassword.length < 5)
    {
        alert('New password must be greater than 4 characters long.');
        return false;
    }    
    
    return true;

}

function validateCheckout(txtboxNameOnCard, dropdownCreditCardType, txtboxCreditCardNumber, dropdownExpirationMonth, dropdownExpirationYear, txtboxBillingZipcode)
{
    var isValid;

    var nameOnCard = trim(document.getElementById(txtboxNameOnCard).value);
    var creditCardType = document.getElementById(dropdownCreditCardType).selectedIndex
    var creditCardNumber = trim(document.getElementById(txtboxCreditCardNumber).value);
    var expirationMonth = document.getElementById(dropdownExpirationMonth).selectedIndex;
    var expirationYear = document.getElementById(dropdownExpirationYear).selectedIndex;
    var billingZipcode = trim(document.getElementById(txtboxBillingZipcode).value);
    
    if (nameOnCard == '' || creditCardType <= 0 || creditCardNumber == '' || expirationMonth <= 0 || expirationYear <= 0 || billingZipcode == '')
        isValid = false;
    else
        isValid = true;   
        
    var regularCCExpr = /^[0-9]*$/; 
    var validCC = regularCCExpr.test(creditCardNumber);
    if (!validCC)
    {
        alert('Invalid credit card number.  Please re-enter.');
        return false;
    }
        
    if (!isValid)
    {
        alert('Make sure all the questions with exclamation points are filled out appropriately.');
    }
    
    return isValid;

}


function validateRegistration(txtboxFirstName, txtboxLastName, txtboxEmailAddress, txtboxPassword, txtboxConfirmPassword)
{
    var isValid;

    var firstName = trim(document.getElementById(txtboxFirstName).value);
    var lastName = trim(document.getElementById(txtboxLastName).value);
    var emailAddress = trim(document.getElementById(txtboxEmailAddress).value);
    var password = trim(document.getElementById(txtboxPassword).value);
    var passwordConfirm = trim(document.getElementById(txtboxConfirmPassword).value);
    
    if (firstName == '' || lastName == '' || emailAddress == '' || password == '' || passwordConfirm == '')
        isValid = false;
    else
        isValid = true;   
        
    if (!isValid)
    {
        alert('Make sure all the questions with exclamation points are filled out appropriately.');
        return false;
    }
    
    if (password != passwordConfirm)
    {
        alert('Password and confirm password fields do not match.');
        document.getElementById(txtboxPassword).value = '';
        document.getElementById(txtboxConfirmPassword).value = '';
        return false;
    }
    
    if (password.length < 5)
    {
        alert('Password must be greater than 4 characters long.');
        document.getElementById(txtboxPassword).value = '';
        document.getElementById(txtboxConfirmPassword).value = '';
        return false;
    }
    
    return isValid;
}

function validateContactForm(txtboxYourName, txtboxPhoneNumbr, txtboxEmailAddress, txtboxBusinessName)
{
    var isValid;

    var yourName = trim(document.getElementById(txtboxYourName).value);
    var phoneNumber = trim(document.getElementById(txtboxPhoneNumbr).value);
    var emailAddress = trim(document.getElementById(txtboxEmailAddress).value);
    var businessAddress = trim(document.getElementById(txtboxBusinessName).value);
    
    if (yourName == '' || phoneNumber == '' || emailAddress == '' || businessAddress == '')
        isValid = false;
    else
        isValid = true;   
        
    if (!isValid)
    {
        alert('Make sure all the questions with exclamation points are filled out appropriately.');
    }
    
    return isValid;
}

function validateContactMiniForm(txtboxYourName, txtboxPhoneNumbr, txtboxEmailAddress)
{
    var isValid;

    var yourName = trim(document.getElementById(txtboxYourName).value);
    var phoneNumber = trim(document.getElementById(txtboxPhoneNumbr).value);
    var emailAddress = trim(document.getElementById(txtboxEmailAddress).value);
    
    if (yourName == '' || phoneNumber == '' || emailAddress == '')
        isValid = false;
    else
        isValid = true;   
        
    if (!isValid)
    {
        alert('Make sure all the questions with exclamation points are filled out appropriately.');
    }
    
    return isValid;
}