// --------------------------------------------------------------------------------------------------
function isEmpty(s)
{
    var i;
    var whitespace = " \t\n\r";
    var c;
    
    if((s === null) || (s.length === 0))
        {return true;}
   
    // Search string looking for characters that are not whitespace
    for (i = 0; i < s.length; i++)
    {
        c = s.charAt(i);
        if (whitespace.indexOf(c) == -1)
            {return false;}
    }
    
    // All characters are whitespace.
    return true;
}
// --------------------------------------------------------------------------------------------------
function isValidCreditCard(s)
{
    // It must be 16 digits starting with a 4, 5 or 6.    
    var creditcard = new RegExp('^([4-6])([0-9]{15})');
    if (!creditcard.test(s))
        {return false;}

    // Set the string length and parity
    var number_length = s.length;
    var parity = number_length % 2;
 
      // Loop through each digit and do the math.
      var total = 0;
    for (i=0; i < number_length; i++) 
        {
            var digit=s.charAt(i);
            
            // Multiply alternate digits by two    
            if (i % 2 == parity) 
                {
                      digit=digit * 2;
                      // If the sum is two digits, add them together (in effect)
                      if (digit > 9) 
                        {digit=digit - 9;}
                }
            // Total up the digits
            total = total + parseInt(digit,10);
          }
 
      // If the total mod 10 equals 0, the number is valid
      if (total % 10 === 0)
        {return true;}
    else
        {return false;}
}
// --------------------------------------------------------------------------------------------------
function isValidEmail(s)
{
    //var mail = new RegExp("^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$");
	var mail = new RegExp("^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+");
    return mail.test(s);
}
// --------------------------------------------------------------------------------------------------
function isValidCvv(s)
{
    var cvv = new RegExp("^([0-9]{3})");
    return cvv.test(s);
}
// --------------------------------------------------------------------------------------------------
function isValidExpDate()
{
    // Get both the month and year from the form.
    var objMonth = document.getElementById('expmonth');
    var objYear = document.getElementById('expyear');
    var expMonth = parseInt(objMonth.value,10);
    var expYear = objYear.value;

    // Get current month and year.
    today = new Date();
    var curMonth = today.getMonth() + 1;
    var curYear = today.getFullYear();

    // Compare years.
    if (expYear < curYear)
        {
            // Set month and year yellow.
            objMonth.style.backgroundColor = 'yellow';
            objYear.style.backgroundColor = 'yellow';
            return false;
        }
    else
        {
            // If same year, then check months.
            if (expYear == curYear)
                {
                    if (expMonth < curMonth)
                        {
                        // Set month and year yellow.
                        objMonth.style.backgroundColor = 'yellow';
                        objYear.style.backgroundColor = 'yellow';
                        return false;
                        }
                }
        }
        
    return true;    
}
// --------------------------------------------------------------------------------------------------
function isValidPassword(s)
{
    if ((s.length<6) || (s.length>16))
        {return false;}
    else
        {return true;}
}
// --------------------------------------------------------------------------------------------------
function isValidPhone(s)
{
    var phone = new RegExp("^([0-9]{3}-[0-9]{3}-[0-9]{4})");
    
    if (s=="000-000-0000")
        {return false;}

    if (phone.test(s))
        {return true;}
    else
        {return false;}
}
// --------------------------------------------------------------------------------------------------
function isValidUsername(s)
{
    if ((s.length<3) || (s.length>16))
        {return false;}
    else
        {return true;}
}
// --------------------------------------------------------------------------------------------------
function isValidZipcode(s)
{
    var zip = new RegExp("^([0-9]{5})(-[0-9]{4})?$");
    
    if (s=="00000-0000")
        {return false;}

    if (zip.test(s))
        {return true;}
    else
        {return false;}
}
// --------------------------------------------------------------------------------------------------
function sameValues(s1, s2)
{
    if (s1==s2)
        {return true;}
    else
        {return false;}
}
// --------------------------------------------------------------------------------------------------
function fixPhone(id)
{
    var phone = new RegExp("^([0-9]{10})");    
    var curElement = document.getElementById(id);
    var s = curElement.value;
    s = s.replace(/[\- ()]/g, '');
    
    if (phone.test(s))
        {curElement.value = s.substr(0,3) + "-" + s.substr(3,3) + "-" + s.substr(6,4);}
}
// --------------------------------------------------------------------------------------------------
function fixZipcode(id)
{
    var zip = new RegExp("^([0-9]{9})");    
    var curElement = document.getElementById(id);
    var s = curElement.value;
    
    if (zip.test(s))
        {curElement.value = s.substr(0,5) + "-" + s.substr(5,4);}
}
// --------------------------------------------------------------------------------------------------
function fixCreditCard()
{
    // Clean out spacers within the card number.
    var creditcard = new RegExp('^([4-5])([0-9]{15})');
    var cardno = document.getElementById('cardno');    
    var s = cardno.value;
    s = s.replace(/[\- ]/g, '');
    
    if (creditcard.test(s))
        {cardno.value = s;}
    
    // Set the card type to visa or mastercard.
    var cardtype = document.getElementById('cardtype');
    var digit=s.charAt(0);
    if (digit==4)
        {cardtype.value = 'V';}
    if (digit==5)
        {cardtype.value = 'M';}
}
// --------------------------------------------------------------------------------------------------
function validate(formId)
{
    var myForm;
    var name;
    var value;
    var useBill;
    var billToPattern = RegExp('^b_');
    var shipToPattern = RegExp('^s_');

    // Get a handle on the form.
    myForm = document.getElementById(formId);
    
    // Test if shipto billing checkbox is defined on page.
    if (myForm.shiptobilling)
        {useBill = myForm.shiptobilling.checked;}
    if (myForm.shiptolist)        
        {
            if (myForm.shiptolist.value===0)
                {useBill = true;}
            else
                {useBill = false;}
        }
    
    // Check for each field in the form.
    for (var i=0; i<myForm.length; i++)
        {
            // Get the field name and value.
            name = myForm.elements[i].name;
            value = myForm.elements[i].value;

            // Skip if a blank field name.
            if (isEmpty(name))
                {continue;}
                
            // Remove first two characters for the billing prefix.
            if (billToPattern.test(name))
                {name = name.substring(2);}
            
            // Remove first two characters for the shipping prefix,
            // but skip validation if using billing address for shipping.
            if (shipToPattern.test(name))
                {
                    if (useBill)
                        {continue;}
                    else
                        {name = name.substring(2);}
                }
                
            switch (name)
                {
                    case 'address2' :     
                        break;            
        
                    case 'company' :     
                        break;            

                    case 'cardno' :
                        if (!isValidCreditCard(value))
                            {
                                alert("Your credit card number is invalid.");
                                myForm.elements[i].style.backgroundColor = 'yellow';
                                myForm.elements[i].focus();
                                return false;
                            }                            
                        break;
                    
                    case 'cvv' :
                        if (!isValidCvv(value))
                            {
                                alert("Your signature panel code is invalid.");
                                myForm.elements[i].style.backgroundColor = 'yellow';
                                myForm.elements[i].focus();
                                return false;                                
                            }
                        break;
                        
                    case 'email' : 
                        if (!isValidEmail(value))
                            {
                                alert("Your email address is invalid.");
                                myForm.elements[i].style.backgroundColor = 'yellow';
                                myForm.elements[i].focus();
                                return false;
                            }                            
                        break;
                        
                    case 'email2' : 
                        if (!sameValues(myForm.email.value, myForm.email2.value))
                            {
                                alert("Your email address must be the same in both fields.");
                                myForm.email.style.backgroundColor = 'yellow';
                                myForm.email2.style.backgroundColor = 'yellow';
                                myForm.email2.focus();
                                return false;
                            }                            
                        break;    
                        
                    case 'expmonth' :
                        if (!isValidExpDate())
                            {
                                alert("The expiration date is invalid.");
                                myForm.elements[i].focus();
                                return false;
                            }
                        break;
            
                    case 'extension' :
                        break;    
                        
                    case 'password' : 
                        if (!isValidPassword(value))
                            {
                                alert("The password must be between 6 and 16 characters.");
                                myForm.elements[i].style.backgroundColor = 'yellow';
                                myForm.elements[i].focus();
                                return false;
                            }
                        break;
                    
                    case 'password2' :     
                        if (!sameValues(myForm.password.value, value))
                            {
                                alert("Your pasword must be the same in both fields.");
                                myForm.password.style.backgroundColor = 'yellow';
                                myForm.password2.style.backgroundColor = 'yellow';
                                myForm.password2.focus();
                                return false;
                            }                            
                        break;    
                        
                    case 'phone' :
                        if (!isValidPhone(value))
                            {
                                alert("Your phone number must be of the format 000-000-0000.");
                                myForm.elements[i].style.backgroundColor = 'yellow';
                                myForm.elements[i].focus();
                                return false;                            
                            }
                        break;
                        
                    case 'title' : 
                        break;

                    case 'username' : 
                        if (!isValidUsername(value))
                            {
                                alert("The username must be between 3 and 16 characters.");
                                myForm.elements[i].style.backgroundColor = 'yellow';
                                myForm.elements[i].focus();
                                return false;
                            }
                        break;                    
                    
                    case 'zipcode' :                    
                        if (!isValidZipcode(value))
                            {
                                alert("Your zipcode is invalid.");
                                myForm.elements[i].style.backgroundColor = 'yellow';
                                myForm.elements[i].focus();
                                return false;                            
                            }
                        break;
                    
                    // Most fields must not be blank.
                    default : 
                        if (isEmpty(value))
                            {
                                alert("The " + name + " field can not be left blank.");
                                myForm.elements[i].style.backgroundColor = 'yellow';                                
                                myForm.elements[i].focus();
                                return false;                                
                            }
                        break;                        
                }
        }
    
    return true;        
}
// --------------------------------------------------------------------------------------------------
function validAdvSearch()
{
    // Only if all 7 fields are empty is the form invalid.
    if ((isEmpty(document.searchForm.arrangement.value)) && 
        (isEmpty(document.searchForm.contributor.value)) && 
        (isEmpty(document.searchForm.format.value)) && 
        (isEmpty(document.searchForm.itemno.value))    && 
        (isEmpty(document.searchForm.publisher.value)) && 
        (isEmpty(document.searchForm.series.value)) && 
        (isEmpty(document.searchForm.title.value)))    
        {
            alert('You must enter search criteria in to at least one field.');
            return false;        
        }
    else
        {
            return true;
        }
}
// --------------------------------------------------------------------------------------------------
function validKeySearch()
{
    if (isEmpty(document.keySearchForm.keywords.value))    
        {
            alert('You must enter at least one search keyword.');
            return false;
        }
    else
        {
            return true;
        }
}
// --------------------------------------------------------------------------------------------------

