//////////////////////////////////////Email validation routines
function isEmail( mail ){
    var error = '';
    //if ( !mail ) { return false; }
    var mailstring = mail.value;
    //test for presence of email address
    if ( mailstring.length == 0 ) {
        error = 'An email address is required.';
        return error;
    }
    //test for presence of @ and only one occurence of @
    if ( mailstring.indexOf( '@' ) == -1 ) {
        error = 'The email address entered has no @ symbol.';
        return error;
    } else if ( mailstring.lastIndexOf( '@' ) != mailstring.indexOf( '@' )  ) {
        error = 'An email adress may only contain one \'@\' symbol.';
        return error;
    }
    //split into component parts
    var chunks = mailstring.split( '@' );
    var n = chunks[0];
    if ( chunks[0].length < 1) {
        error = "Name portion of email seems to not be long enough.";
    }
    if ( ! chunks[0] || ! chunks[1]) {
        error = "The email address provided seems to not be complete.";
        return error;
    }
    
    if ( ! chunks[1] ){
        error = 'Please provide a domain name for your email address.';
    } 
    /*
    else {
    //check for illegalities in the username portion
    if ( n.substring( 0,1 ) == '.' || n.substring( 0,1 ) == '-' || n.substring( n.length -1,1 ) == '.' ) {
        error = 'The user name portion of an email cannot start with a period or hyphen';
        error += ' and cannot end with a period.';
        return error;
    }
    //check invalid characters in name portion of email address
    if ( ! validEmailChars( n ) ) {
        error = 'The name portion of the email address contains one or more invalid characters.';
        return error;
    }
    //check domain characters
    n = chunks[1];
    if ( n.substring( 0,1 ) == '.' || n.substring( 0,1 ) == '-' || n.substring( n.length - 1,1 ) == '-' || n.substring( n.length - 1,1 ) == '.' ) {
        error = 'The domain name portion of the email address cannot start or end with a period or a hyphen.';
        return error;
    }
    if ( n.length < 3 ) {
        error = 'The domain portion of the email address seems too short.';
        return error;
    }
    
    if ( ! validEmailChars( n ) ) {
        error = 'The domain name portion of the email address contains one or more invalid characters.';
        return error;
    }
    }*/
    return error;
}
function validEmailChars( str ) {
    for ( var i = 0 ; i < str.length ; i++ ) {
        currentCode = str.charCodeAt( i );
        if ( currentCode == 45 || currentCode == 46 || currentCode == 95 || ( currentCode > 96 && currentCode < 123 ) || ( currentCode > 47 && currentCode < 58 ) ) {
            continue;
        } else {
            return false;
        }
    }
}
//////////////////////////////////////end email validation routines

///////////////////////////////////////validation of specific forms
function validateContactForm( cf ) {
    if ( ! document.getElementById || ! document.createTextNode ) { return; }
    var nameField = cf.name;
    var emailField = cf.email;
    var phoneField = cf.phone;
    var messageField = cf.message;
    var errbox = cf.getElementsByTagName( 'span' )[0];
    var msg = '';
    var achtung = "<img src=\"http://www.sevenspirits.org/images/achtung.jpg\" />";
    errbox.firstChild.nodeValue = ' ';
    if ( ! nameField || ! emailField || ! phoneField || ! messageField ) { return; }
    cf.sendBtn.value = "processing ...";
    if ( nameField.value == '' ) {
        msg = 'Please enter your name.';
        errbox.firstChild.nodeValue = msg;
        nameField.focus();
        cf.sendBtn.value = "Send";
        return false;
    }
    if ( emailField.value == '' && phoneField.value == '' ) {
        msg = 'Please enter either an email address or a phone number.';
        errbox.firstChild.nodeValue = msg;
        emailField.focus();
        cf.sendBtn.value = "Send";
        return false;
    }
    if ( emailField.value != '' ) {
        var err = isEmail( cf.email );
        if ( err != '' ) {
            errbox.firstChild.nodeValue = err;
            emailField.focus();
            cf.sendBtn.value = 'Send';
            return false;
        }
    }
    if ( messageField.value == '' ) {
        msg = 'Please enter a message to be sent.';
        errbox.firstChild.nodeValue = msg;
        messageField.focus();
        cf.sendBtn.value = "Send";
        return false;
    }    
    
    return valid;
    
    /*
    var valid = true;
    var error = '';
    cf.sendBtn.value = "processing ...";
    if ( cf.name.value == "") {
        valid = false;
        error = 'Please enter a name.';
    }
    if ( cf.email.value == "" && cf.phone.value == "" ) {
        valid = false;
        if (error.length > 0) { 
            error += '\nPlease enter either an email address or a phone number.';
        } else {
            error = 'Please enter either an email address or a phone number.';
        }
    } else {
        if ( cf.phone.value == '') {
            var err = isEmail( cf.email );
            if ( err !=  '') {
                valid = false;
                if (error.length > 0) { 
                    error += err;
                } else {
                    error = err;       
                }
            }
        }
    }
    if ( cf.message.value == '') {
        valid = false;
        if (error.length > 0) { 
            error += '\nPlease enter a message to be sent.';
        } else {
            error = 'Please enter a message to be sent.';
        }
    }
    if ( error != '') { alert( error ); }
    if (! valid){ cf.sendBtn.value = "Send"; }
    return valid;
    */
}







function validateForm( form ) {
    
    //get a grip on the span which will hold/display error messages
    var errbox = form.getElementsByTagName( 'span' )[0];
    if ( ! errbox ) { alert('no errbox'); return; }
    
    //
    var msg = '';
    errbox.firstChild.nodeValue = ' ';
    
    var name = nameField.value;
    if (nameField.value == "") {
        msg = 'A name is required';
        errbox.firstChild.nodeValue = msg;
        return false;
        
    }
    return;
}







function validateRsvpForm(rf){
    var valid = true;
    rf.sendBtn.value = "processing...";
    if ( rf.rsvp_name.value == "") {
        valid = false;
        alert("Please enter a name."); 
    } else {
        var err = isEmail( rf.rsvp_email );
        if ( err !=  '') {
            valid = false;
            alert( err );
        }
    }
    rf.sendBtn.value = "Send RSVP";
    return valid;
}
function validateBaptismConfirmationForm(bcf, formName){
    var valid = true;
}
///////////////////////////////////////end validation of specific forms