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_number, "Telephone Number Required!")==false)
            {phone_number.focus();return false;}
        if (check_phone(phone_number)==false)
            {phone_number.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(personal_desc, "Tell us more about you!")==false)
            {personal_desc.focus();return false;}
        if (validateCheckBox(thisform)==false)
            {return false;}
        if (validate_required(upload_pic_names, "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;
    }
}

function validateCheckBox(form) {
    var total = 0;
    var max = form['available_for[]'].length;
    for (var idx = 0; idx < max; idx++) {
        if (form['available_for[]'][idx].checked == true) {
            total += 1;
        }
    }
    if(total < 1) {
        alert("At least one category should be checked!");
        return false;
    }
}