function validate_required(field,alerttxt)
{
    with (field)
    {
        if (value==null||value=="")
            {alert(alerttxt);return false;}
        else {return true}
    }
}

function validate_form(thisform)
{
    with (thisform)
    {
        if (validate_required(first_name, "First Name Required!")==false)
            {first_name.focus();return false;}
        if (validate_required(last_name, "Last Name Required!")==false)
            {last_name.focus();return false;}
        if (validate_required(phone, "Telephone Number Required!")==false)
            {phone.focus();return false;}
        if (validate_required(location, "Location Required!")==false)
            {location.focus();return false;}
        if (check_phone(phone)==false)
            {phone.focus();alert("Not a valid telephone number 123-456-7890!"); return false;}
        if (validate_required(email, "Email Required!")==false)
            {email.focus();return false;}
        if (validate_email(email,"Not a valid e-mail address!")==false)
            {email.focus();return false;}
        if (validate_required(more_info, "Tell us more about you!")==false)
            {more_info.focus();return false;}
        if (validate_required(uploadFile1, "At least one picture should be uploaded!")==false)
            {return false;}
    }
}

function validate_email(email, alerttxt) {
    var email = email.value;
    if (! allValidChars(email)) { // check to make sure all characters are valid
        alert(alerttxt);
        return false;
    }
    if (email.indexOf("@") < 1) { // must contain @, and it must not be the first character
        alert(alerttxt);
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) { // last dot must be after the @
        alert(alerttxt);
        return false;
    } else if (email.indexOf("@") == email.length) { // @ must not be the last character
        alert(alerttxt);
        return false;
    } else if (email.indexOf("..") >=0) { // two periods in a row is not valid
        alert(alerttxt);
        return false;
    } else if (email.indexOf(".") == email.length) { // . must not be the last character
        alert(alerttxt);
        return false;
    }
    return true;
}

function allValidChars(email) {
    var parsed = true;
    var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
    for (var i=0; i < email.length; i++) {
        var letter = email.charAt(i).toLowerCase();
        if (validchars.indexOf(letter) != -1)
            continue;
        parsed = false;
        break;
    }
    return parsed;
}

function check_phone(phone_number)
{
    with (phone_number)
    {
        if((value.match(/^[ ]*[(]{0,1}[ ]*[0-9]{3,3}[ ]*[)]{0,1}[-]{0,1}[ ]*[0-9]{3,3}[ ]*[-]{0,1}[ ]*[0-9]{4,4}[ ]*$/)==null))
        {
            return false;
        }
        return true;
    }
}

