// Form Validation Script
// (c)2009-2010 Well & Lighthouse, LLC, all rights reserved.

// Validate Contribution Form
function contribValidate(thisForm) {
    var err = "Please correct the following problems: \n\n";
    err += isEmpty(thisForm.name.value);
    err += isEmpty(thisForm.addr.value);
    err += isEmpty(thisForm.city.value);
    err += isEmpty(thisForm.state.value);
    err += checkZip(thisForm.zip.value);
    err += checkEmail(thisForm.email.value);
    err += isEmpty(thisForm.employer.value);
    err += isEmpty(thisForm.occupation.value);
    err += amounts(thisForm);
    err += isEmpty(thisForm.cc.value);
    err += isEmpty(thisForm.cvv.value);
/*    if ((checkDropdown(thisForm.exp_mo.value) != "") || (checkDropdown(thisForm.exp_yr.value) != "")) {
        err += "Please Enter an expiration date.\n";
    } */
    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 signupValidate(thisForm) {
    var err = "Please correct the following problems: \n\n";
    err += checkEmail(thisForm.fields_email.value);
    err += isEmpty(thisForm.fields_fname.value);
    err += isEmpty(thisForm.fields_lname.value);
    if (err != "Please correct the following problems: \n\n") {
       alert(err);
       return false;
    }
return true;
}

// Validate Media form
function mediaValidate(thisForm) {
    var err = "Please correct the following problems: \n\n";
    err += isEmpty(thisForm.firstname.value);
    err += isEmpty(thisForm.lastname.value);
    err += checkEmail(thisForm.email.value);
    err += checkPhone(thisForm.phone.value);
/*
    err += isEmpty(thisForm.custom-119.value);
    err += isEmpty(thisForm.custom-146.value);
*/
    if (err != "Please correct the following problems: \n\n") {
       alert(err);
       return false;
    }
return true;
}

// Validate Contact form
function contactValidate(thisForm) {
    var err = "Please correct the following problems: \n\n";
    err += checkEmail(thisForm.email.value);
    err += isEmpty(thisForm.name.value);
    err += isEmpty(thisForm.subject.value);
    err += isEmpty(thisForm.message.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.email.value);
    err += checkPhone(thisForm.phone.value);
    err += checkZip(thisForm.zip.value);
    if (err != "Please correct the following problems: \n\n") {
       alert(err);
       return false;
    }
return true;
}

// Validate Sidebar Signup form
function sideValidate(thisForm) {
    var err = "Please correct the following problems: \n\n";
    err += checkEmail(thisForm.email.value);
    err += checkZip(thisForm.zip.value);
    if (err != "Please correct the following problems: \n\n") {
       alert(err);
       return false;
    }
return true;
}

// are one of the amounts checked?
function amounts (form) {
    var error = "";
    if (!form.amt_a.checked &&
        !form.amt_b.checked &&
        !form.amt_c.checked &&
        !form.amt_d.checked &&
        !form.amt_e.checked &&
        !form.amt_f.checked &&
        !form.amt_g.checked &&
        !form.amt_h.checked) {
        error = "Please Select an Amount.\n";
    } else {
        if (isEmpty(form.o_amount.value) && form.amt_h.checked) {
            error = "Please Enter an amount in \"Other\".\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;
}

// phone number - strip out delimiters and check for 10 digits
function checkPhone (strng) {
    var error = "";
    if (strng == "") {
        error = "Please enter a valid 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 = "Please enter a valid phone number, including 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 = "Please enter an email address.\n";
    }
    var emailFilter=/^.+@.+\..{2,4}$/;
    // 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;
}