/*
  Name: with_displayed
  Input:
      action - the action to be taken
  Output:
      If a pilot's id number is in the query string the specified action will be 
      taken. Otherwise the user will be prompted with instructions to place the 
      id in the query string  
  Purpose:
      Ensure that a pilot is displayed when an action requiring a user to be 
      displayed is taken.
  Written By: J. Thomas Enders
  Written On: 5-8-2004
  Revision History
*/
function with_displayed(action) {
    var args = new Array();
    var arg_names = new Array('id','action_btn','old_action','blet','fn','ln','keep_spouse');
    for(i=0;i<arg_names.length;i++) {
        args[arg_names[i]] = '';
    } //end for
    var query = location.search.substring(1);
    var pairs = query.split("&");
    for (i=0;i<pairs.length;i++) {
        var pos = pairs[i].indexOf("=");
        if(pos == -1) continue;
        args[pairs[i].substring(0,pos)] = pairs[i].substring(pos+1)
    } //end for
    
    if(args['id'] != '') {
        if(action == 'edit') {
            var cnfm = "Are you sure you wish to edit the currently displayed member?";
        } else if (action == 'make_admin') {
            var cnfm = "Are you sure you wish to make the currently displayed member an administrator?";
        } else {
            var cnfm = "Are you sure you wish to make the currently displayed member deceased?";
        } //end if
        
        if(!confirm(cnfm)) {return;}
        else {
            var url = location.protocol + '//' + location.host + location.pathname + '?';
            args['action_btn'] = action;
            for(i=0;i<arg_names.length;i++) {
                if(args[arg_names[i]] != '') {
                    url = url + arg_names[i] + '=' + escape(args[arg_names[i]]) + '&';
                } //end if
            } //end for
            var url = url.substring(0,(url.length - 1));
            document.location = url;
        }
    } else {
        var msg   = "To use any of the with displayed buttons the pilot's id value\n";
        msg = msg + "must appear in the location bar and there is currently no id\n";
        msg = msg + "in this place. To put one there use the search or browse\n";
        msg = msg + "function to locate the desired pilot and then click on their\n";
        msg = msg + "name to display their information.";
        alert(msg);
    } //end if 
} //end with_displayed

/*
  Name: validate_edit
  Input:
      form - the form object
  Output:
      On an error an alert box with the details of the error is displayed
      On success the edit form is submitted to the script
  Purpose:
      When a user edit a record this function provides preliminary 
      checking of the data entered.
  Written By: J. Thomas Enders
  Written On: 5-7-2004
  Revision History
*/
function validate_edit(form) {
    var errors = '';
    var e_test = /^[\w\.\-]+\@\w+\.[a-zA-Z]+/;
    if(form.title.value == '') {
        errors = errors + "You must enter a title.\n";
    }
    if(form.first_name.value == '') {
        errors = errors + "You must enter a first name.\n";
    }
    if(form.last_name.value == '') {
        errors = errors + "You must enter a last name.\n";
    }
    if(form.status.value == '') {
        errors = errors + "You must enter a status.\n";
    }
    if(form.email.value != '' && !e_test.test(form.email.value)) {
        errors = errors + "Invalidly formed email entered.\n";
    }
    if(form.old_pass.value == '' && form.new_pass.value != '') {
        errors = errors + "You must enter the current pass phrase to change the pass phrase.\n";
    } else if (form.old_pass.value != '' && form.new_pass.value != form.new_pass_2.value) {
        errors = errors + "New passwords do not match.\n";
    }
    
    if(errors == '') {
        form.action_btn.value = 'Save Edit';
        form.submit();
    } else {
        alert(errors);
    }
} //end validate_edit

/*
  Name: validate_edit
  Input:
      form - the form object
  Output:
      On an error an alert box with the details of the error is displayed
      On success the edit form is submitted to the script
  Purpose:
      When a user edit a record this function provides preliminary 
      checking of the data entered.
  Written By: J. Thomas Enders
  Written On: 5-7-2004
  Revision History
*/
function validate_add(form) {
    var errors = '';
    var e_test = /^[\w\.\-]+\@\w+\.[a-zA-Z]+/;
    if(form.title.value == '') {
        errors = errors + "You must enter a title.\n";
    }
    if(form.first_name.value == '') {
        errors = errors + "You must enter a first name.\n";
    }
    if(form.last_name.value == '') {
        errors = errors + "You must enter a last name.\n";
    }
    if(form.status.value == '') {
        errors = errors + "You must enter a status.\n";
    }
    if(form.email.value != '' && !e_test.test(form.email.value)) {
        errors = errors + "Invalidly formed email entered.\n";
    }
    if(form.psswrd.value == '') {
        errors = errors + "You must enter a pass phrase.\n";
    } else if (form.psswrd.value != form.psswrd2.value) {
        errors = errors + "Pass Phrases do not match.\n";
    }
    
    if(errors == '') {
        form.action_btn.value = 'Add Member';
        form.submit();
    } else {
        alert(errors);
    }
} //end validate_add

/*
  Name: validate_search
  Input:
      fn - the value in the first name field
      ln - the value in the last name field
      actn_hldr - the object for the action_btn form field
      form - the form object
  Output:
      On an error an alert box with the details of the error is displayed
      On success the search form is submitted to the script
  Purpose:
      When a user searches the database this function provides preliminary 
      checking of the data entered.
  Written By: J. Thomas Enders
  Written On: 5-6-2004
  Revision History
*/
function validate_search(fn,ln,actn_hldr,form) {
    var pass = true;
    if(fn.length == 0 && ln.length == 0) {
        alert('You must text in either the first or last name fields.');
        pass = false;        
    } //end if
    
    var t_test = /^[a-zA-z\.\ ]+$/;
    if(!t_test.test(fn) && !t_test.test(ln)) {
        alert('You can only enter letters, periods and spaces in the first and last name fields.');
        pass = false;
    } //end if
    
    if(pass) {
        actn_hldr.value = 'search';
        form.submit();
    } //end if
} //end validate_search

/*
  Name: send_info
  Input:
      email_field - the field object that will hold the email address
      actn_hldr - the object for the action_btn form field
      form - the form object
  Output:
      The function loops until a vaild email is entered, then it submits the form
  Purpose:
      When a user requests their log in information sent this script is run instead
      of performing a page refresh to load a new form.
  Written By: J. Thomas Enders
  Written On: 4-28-2004
  Revision History
*/
function send_info(email_field,actn_hldr,form) {
    var email = '';
    var msg = '';
    var e_test = /^[\w\.\-]+\@\w+\.[a-zA-Z]+/;
    var tries = 0;
    
    while (!e_test.test(email) && tries < 3) {
        if(tries != 0) {msg = "Please enter a vaildly formed email address.\n";}
        
        msg = msg + "Enter the email you have listed in the directory.";
        email = prompt(msg,"");
        tries++;
    } //end while
    
    if(tries == 3) {
        alert("You typed an invalidly formed email address too many times.\nReturning to web page.");
    }
    
    email_field.value = email;
    actn_hldr.value = 'send_info';
    form.submit();
} //end send info

/*
  Name: validate_login
  Input:
      id - the value of the user id form field
      pass - the value of the pass phrase form field
      actn_hldr - the object for the action_btn form field
      form - the form object
  Output:
      false on error, the form is submitted on success.
  Purpose:
      This script creates initial validation of the login in form. This way the 
      user does not have to wait for a page refresh befor basic data scrubbing
      occurs.
  Written By: J. Thomas Enders
  Written On: 4-27-2004
  Revision History
*/
function validate_login(id,pass,actn_hldr,form) {
    var idnum = /^\d{5}$/;
    var password = /^[\w\.]+$/;
    var error = '';
    
    if(!idnum.test(id)) {error = error + "User ID must be a five digit numeric value.\n";}
    if(!password.test(pass)) {error = error + "Pass phrase must be at least 8 alpha numeric digits.\n";}
    
    if(error != '') {
        alert(error);
        return FALSE;
    } else {
        actn_hldr.value = 'login';
        form.submit();
    }
    return;
} //end validate_login

function confirm_delete(form,action_field) {
    var cfm = '***WARNING***\nThis will automatically delete ALL selected members.\n\n';
        cfm = cfm + 'Please double check to ensure you wish to delete ALL select members.\n';
        cfm = cfm + 'Click OK if you wish to delete ALL selected members or\n';
        cfm = cfm + 'Cancel if you wish to make corrections first.';
    if(confirm(cfm)) {
        action_field.value = 'Delete Selected';
        form.submit();
    } //end if
} //end confirm_delete

function validate_massmail(form,action_btn) {
    var errors = '';
    if(form.subject.value == '')
        errors = errors + 'You must enter a subject.\n';
    if(form.message.value == '')
        errors = errors + 'You must enter a message.';
    
    if(errors != '') 
        alert(errors);
    else {
        action_btn.value = 'Send Mail';
        form.submit();
    } //end if
} //end validate_massmail 

function validate_addr(form) {
    var errors = '';
    var today = new Date();
    
    if(form.first.value == '')
        errors = errors + 'You must enter a first name.\n';
    
    if(form.last.value == '')
        errors = errors + 'You must enter a last name.\n';
    
    if(form.doe.value > today.getFullYear()) 
        errors = errors + 'Year of Death cannot be in the future.\n';
    else if(form.doe.value < 1900 && form.doe.value != 0)
        errors = errors + 'Year of Death cannot be less then 1900 or zero for unsure.\n';
    
    if(form.srs.value > today.getFullYear())
        errors = errors + 'Seniority Roster Start Year cannot be in the future.\n';
    else if(form.srs.value < 1900 && form.srs.value != 0) 
        errors = errors + 'Seniority Roster Start Year cannot be less then 1900 or zero for unsure.\n';
    else if(form.srs.value > form.doe.value)
        errors = errors + 'Seniority Roster Start Year cannot be greater then Year of Death.\n';
    
    if(form.sre.value > today.getFullYear())
        errors = errors + 'Seniority Roster End Year cannot be in the future.\n'
    else if(form.sre.value < 1900 && form.sre.value != 0)
        errors = errors + 'Seniority Roster End Year cannot be less then 1900 or zero for unsure.\n';
    else if(form.sre.value < form.srs.value && form.srs.value != 0 && form.sre.value != 0)
        errors = errors + 'Seniority Roster End Year cannot be less then Seniority Roster Start Year.\n';
    else if(form.sre.value > form.doe.value && form.sre.value != 0 && form.doe.value != 0)
        errors = errors + 'Seniority Roster End Year cannot be greater then Year of Death.\n';
        
    if(errors == '') {
        if(form.only_admins.value != 1) {
            var msg = 'Sending Mass Mails To Then Entire Membership Could Take\n';
            msg = msg + 'Up To Ten Minutes In Processing. During This Time It Will\n';
            msg = msg + 'That You Browser Is Hung Up. Donot Click Send Mail Again\n';
            msg = msg + 'Or Press The Esc Key. If You Do Not Wish To Wait This Long\n';
            msg = msg + 'Right Now Click Cancel Below, Otherwise Click OK.';
            if(confirm(msg)) {
                form.action_btn.value = 'Add Remembrance';
                form.submit();
            } //end if
        } else {
            form.action_btn.value = 'Add Remembrance';
            form.submit();
        } //end if
    } else
        alert(errors);
} //end validate_addr