// Validate Contribution Form
function contribValidate(thisForm) {
    var err = "Please correct the following problems: \n\n";
    err += isEmpty(thisForm.firstname.value);
    err += isEmpty(thisForm.lastname.value);
    err += isEmpty(thisForm.addr1.value);
    err += isEmpty(thisForm.city.value);
    err += checkZip(thisForm.zip.value);
    err += checkEmail(thisForm.email.value);
    err += checkPhone(thisForm.phone.value);
    err += isEmpty(thisForm.employer.value);
    err += isEmpty(thisForm.occupation.value);
    err += amounts(thisForm);
    err += cardType(thisForm);
    err += isEmpty(thisForm.cc_number.value);
    if ((checkDropdown(thisForm.cc_expir_month.value) != "") || (checkDropdown(thisForm.cc_expir_year.value) != "")) {
        err += "Please Enter an expiration date.\n";
    }
    err += legal(thisForm.qf_4fc637.checked);
    if (err != "Please correct the following problems: \n\n") {
       alert(err);
       return false;
    }
if (document.getElementById) { var submitbutton = document.getElementById('processbutton'); if (submitbutton) {submitbutton.disabled = true;}} 
return true;
}


// Validate Sustainer Contribution Form
function sustainValidate(thisForm) {
    var err = "Please correct the following problems: \n\n";
    err += isEmpty(thisForm.firstname.value);
    err += isEmpty(thisForm.lastname.value);
    err += isEmpty(thisForm.addr1.value);
    err += isEmpty(thisForm.city.value);
    err += checkZip(thisForm.zip.value);
    err += checkEmail(thisForm.email.value);
    err += checkPhone(thisForm.phone.value);
    err += isEmpty(thisForm.employer.value);
    err += isEmpty(thisForm.occupation.value);
    err += amounts(thisForm);
    err += cardType(thisForm);
    err += isEmpty(thisForm.cc_number.value);
    if ((checkDropdown(thisForm.cc_expir_month.value) != "") || (checkDropdown(thisForm.cc_expir_year.value) != "")) {
        err += "Please Enter an expiration date.\n";
    }
    err += legal(thisForm.qf_b7e0ff.checked);
    err += recurring(thisForm.qf_22df88.checked);
    if (err != "Please correct the following problems: \n\n") {
       alert(err);
       return false;
    }
if (document.getElementById) { var submitbutton = document.getElementById('processbutton'); if (submitbutton) {submitbutton.disabled = true;}} 
return true;
}

// Validate Join our Team form
function joinValidate(thisForm) {
    var err = "Please correct the following problems: \n\n";
    err += checkEmail(thisForm.email.value);
    err += isEmpty(thisForm.firstname.value);
    err += isEmpty(thisForm.lastname.value);
    err += checkZip(thisForm.zip.value);
    if (err != "Please correct the following problems: \n\n") {
       alert(err);
       return false;
    }
return true;
}

// Validate Quick Signup form
function quickValidate(thisForm) {
    var err = "Please correct the following problems: \n\n";
    err += checkEmail(thisForm.quickEmail.value);
    err += checkZip(thisForm.quickZip.value);
    if (err != "Please correct the following problems: \n\n") {
       alert(err);
       return false;
    }
return true;
}

// Validate Quick Signup form
function sidebarValidate(thisForm) {
    var err = "Please correct the following problems: \n\n";
    err += checkEmail(thisForm.sidebarQuickEmail.value);
    err += checkZip(thisForm.sidebarQuickZip.value);
    if (err != "Please correct the following problems: \n\n") {
       alert(err);
       return false;
    }
return true;
}

// are one of the amounts checked? (set for Reid Default Contrib page)
function amounts (form) {
    var error = "";
    if (!form.qf_1892ab.checked &&
        !form.qf_1df67d.checked &&
        !form.qf_5f171b.checked &&
        !form.qf_2181ea.checked &&
        !form.qf_475abf.checked &&
        !form.qf_0b6446.checked &&
        !form.qf_91356d.checked &&
        !form.qf_cd3681.checked) {
        error = "Please Select an Amount.\n";
    }
    return error;
}

// has a card type been checked? (set for Reid Default Contrib page)
function cardType (form) {
    var error = "";
    if (!form.qf_266f78.checked &&
        !form.qf_943756.checked &&
        !form.qf_590d89.checked) {
        error = "Please Select Card Type.\n";
    }
    return error;
}

// is Legal checked?
function legal (box) {
    var error = "";
    if (!box) {
        error = "Please read and check the Legal Compliance Section.\n";
    }
    return error;
}

// is Legal checked?
function recurring (box) {
    var error = "";
    if (!box) {
        error = "Please read and agree to the recurring monthly contribution.\n";
    }
    return error;
}

// phone number - strip out delimiters and check for 10 digits
function checkPhone (strng) {
    var error = "";
    if (strng == "") {
        error = "You didn't enter a phone number.\n";
    }
    var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
        if (isNaN(parseInt(stripped))) {
           error = "The phone number contains illegal characters.\n";
        }
        if (!(stripped.length == 10)) {
            error = "The phone number is the wrong length. Make sure you included an area code.\n";
        } 
    return error;
}

// strip out dashes and make sure it's numbers and not too long.
function checkZip (strng) {
    var error = "";
    if (strng == "") {
        error = "Please enter a valid ZIP.\n";
    }
    var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
        if (isNaN(parseInt(stripped))) {
           error = "Please enter a valid ZIP.\n";
        }
        if (!(stripped.length == 5) && !(stripped.length == 9)) {
            error = "Please enter a valid ZIP.\n";
        } 
    return error;
}

// non-empty textbox
function isEmpty(strng) {
    var error = "";
        if (strng.length == 0) {
            error = "Please fill out all required fields.\n"
        }
    return error;	  
}

// test for valid e-mail address
function checkEmail (strng) {
    var error="";
    if (strng == "") {
        error = "You didn't enter an email address.\n";
    }
    var emailFilter=/^.+@.+\..{2,3}$/;
    // does it make sense?
    if (!(emailFilter.test(strng))) { 
        error = "Please enter a valid email address.\n";
    } else {
    // does email have illegal characters?
        var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
        if (strng.match(illegalChars)) {
            error = "The email address contains illegal characters.\n";
        }
    }
    return error;
}

// valid selector from dropdown list
function checkDropdown(choice) {
var error = "";
    if (choice == 0) {
    error = "Please enter an Expiration Date.\n";
    }    
return error;
}