
var DUAL_MEMBERSHIP_ITEM_ID = 2;

// displays the table allowing a user to enter the
// address where their credit card statement is sent
function show_billAddress() { document.getElementById('billAddress').style.display = 'block'; }

// hides the table allowing a user to enter the
// address where their credit card statement is sent
function hide_billAddress() { document.getElementById('billAddress').style.display = 'none'; }


// display fields to fill out second member name
function show_second_member() {  document.getElementById('secondMember').style.display='block'; }

function hide_second_member() { document.getElementById('secondMember').style.display='none'; }



 // AJAX call to retrieve form html from mohc ajax server
function get_frm(frmID) {
    new ajax().request("../inc/mohc.ajax.php", {'cmd':'get:' + frmID}, get_frm_callback, frmID);
}

// processes get_form AJAX callback
function get_frm_callback(response, frmID) {
    if (response) {
        
        // inject response html
        document.getElementById('frm_wrp').innerHTML = response;
        
        // attach form event handlers
        frm_init(frmID);
    }
}


// AJAX call to get regions associated with the passed country
function get_regions(country, region_elID) {
    new ajax().request("../inc/mohc.ajax.php", {'cmd':'get:regions', 'country':country}, get_regions_callback, region_elID);
}


// proccess get_regions AJAX callback response

function get_regions_callback(response, region_elID) {
    
    var regions;
    
    //alert(response);
    
    if (response && response.length > 0 && (regions = JSON.parse(response))) {
        
        var i;
        var region_el = document.getElementById(region_elID);
        var opt;

        // clear out previously loaded regions
        clear_children(region_elID);
        
        // add regions from response
        for (region in regions) {            
            opt = document.createElement('option');
            opt.value = regions[region].RegionCode;
            opt.innerHTML = regions[region].Region;            
            region_el.appendChild(opt);            
        }        
    }
}



// removes all children of the passed element

function clear_children(elID) {
    
    if (document.getElementById(elID)) {
        var el = document.getElementById(elID);
        while (el.firstChild) el.removeChild(el.firstChild);        
    }
}



// processes form before submit

function process_frm() {
    
    frm = this;
    submit = false;

    switch (this.id) {
        
        case "membership.frm":
        case "donation.frm":
        case "membership_donation.frm":
        case "gift.frm":
            
            // validate the donation
            submit = validate_donation(this.id);
            break;
        
        default:
        
    }
    
    return submit;
    
}


// performs validation on the donation form
function validate_donation(frm_name) {
    
    var valid = true;
    var validated = false;

    var err_details = "";
    var err_msg = "";
    
    frm = document.getElementById(frm_name);
    
    // applies to all donation forms:
    
    // required: title
    validated = false;
    if (is_checked(frm.id, "title") || frm.otherTitle.value) {
        if (frm.otherTitle.value && is_name(frm.otherTitle.value))
            validated = true;
        else if (is_checked(frm.id, "title"))
            validated = true;
        else        
           err_details += "\n- There are invalid characters in your salutation title."; 
    }
    else { err_details += "\n- Please specify a title.";  }   
    valid = valid && validated;
    

    // required: first
    validated = false;
    if (frm.name.value) {
        if (is_name(frm.name.value))    
            validated = true;
        else
           err_details += "\n- There are invalid characters in your name."; 
    }
    else { err_details += "\n- Please enter your first name.";  }   
    valid = valid && validated;
    
    
    // required: address
    validated = false;
    if (frm.address.value) {
        if (is_address(frm.address.value))    
            validated = true;
        else
           err_details += "\n- There are invalid characters in your address line."; 
    }
    else { err_details += "\n- Please enter an address.";  }   
    valid = valid && validated;
    

    // required: city
    validated = false;
    if (frm.city.value) {
        if (is_address(frm.city.value))    
            validated = true;
        else
           err_details += "\n- There are invalid characters in your city name."; 
    }
    else { err_details += "\n- Please enter a city.";  }   
    valid = valid && validated;

   
    // required: province/state
    validated = false;
    if (frm.prov.value) {
        if (is_alpha(frm.prov.value))    
            validated = true;
        else
           err_details += "\n- There are invalid characters in your province/state."; 
    }
    else { err_details += "\n- Please specify a province/state.";  }   
    valid = valid && validated;
    
   
    // required: country
    validated = false;
    if (frm.country.value) {
        if (is_alpha(frm.country.value))    
            validated = true;
        else
           err_details += "\n- There are invalid characters in your country."; 
    }
    else { err_details += "\n- Please specify a country.";  }   
    valid = valid && validated;
    
    
    // required: postal code
    validated = false;
    if (frm.postal.value) {
        if (is_text(frm.postal.value))    
            validated = true;
        else
           err_details += "\n- There are invalid characters in your postal code."; 
    }
    else { err_details += "\n- Please specify a postal code.";  }   
    valid = valid && validated;
    
    
    // required: email    
    validated = false;
    if (frm.email.value) {
        if (is_email(frm.email.value))  {
            if (frm.email.value == frm.confirmEmail.value)            
                validated = true;
            else
                err_details += "\n- Please ensure your email and confirmation email addresses match."; 
        }
        else
           err_details += "\n- There are invalid characters in your email address."; 
    }
    else { err_details += "\n- Please specify an email address.";  }   
    valid = valid && validated;
    
    
    
    // required: confirmation email
    validated = false;
    if (frm.confirmEmail.value) {
        if (is_email(frm.confirmEmail.value))    
            validated = true;
        else
           err_details += "\n- There are invalid characters in your confirmation email address."; 
    }
    else { err_details += "\n- Please specify a confirmation email address.";  }   
    valid = valid && validated;
    
    
    
    // required: telephone
    validated = false;
    if (frm.phone.value) {
        if (is_phone(frm.phone.value))    
            validated = true;
        else
           err_details += "\n- There are invalid characters in your phone number."; 
    }
    else { err_details += "\n- Please specify a phone number.";  }   
    valid = valid && validated;
    
    
    validated = false;
    // optional: check the billing address, if one is required    
    if (is_checked(frm.id, "sameBillAddress")) {    
        
        // validate the billing address
        if (document.getElementById('sameBillAddress_n').checked) {
                        
                // required: address
                validated = false;
                if (frm.billAddress.value) {
                    if (is_address(frm.billAddress.value))    
                        validated = true;
                    else
                       err_details += "\n- There are invalid characters in your billing address line."; 
                }
                else { err_details += "\n- Please enter a billing address.";  }   
                valid = valid && validated;
                
            
                // required: city
                validated = false;
                if (frm.billCity.value) {
                    if (is_address(frm.billCity.value))    
                        validated = true;
                    else
                       err_details += "\n- There are invalid characters in your billing city name."; 
                }
                else { err_details += "\n- Please enter a city in your billing address.";  }   
                valid = valid && validated;
            
               
                // required: province/state
                validated = false;
                if (frm.billProv.value) {
                    if (is_alpha(frm.billProv.value))    
                        validated = true;
                    else
                       err_details += "\n- There are invalid characters in your billing province/state."; 
                }
                else { err_details += "\n- Please specify a province/state in your billing address.";  }   
                valid = valid && validated;
               
               
                // required: country
                validated = false;
                if (frm.billCountry.value) {
                    if (is_alpha(frm.billCountry.value))    
                        validated = true;
                    else
                       err_details += "\n- There are invalid characters in your billing country."; 
                }
                else { err_details += "\n- Please specify a country in your billing address.";  }   
                valid = valid && validated;
                
                
                // required: postal code
                validated = false;
                if (frm.billPostal.value) {
                    if (is_alphanumeric(frm.billPostal.value))    
                        validated = true;
                    else
                       err_details += "\n- There are invalid characters in your billing postal code."; 
                }
                else { err_details += "\n- Please specify a postal code in your billing address.";  }   
                valid = valid && validated;
            
        }
        // yes was selected, don't need to validate
        else { validated = true; }
        
    } else { err_details += "\n- Please specify whether the address entered is the address on your credit card statement.";  }   
    valid = valid && validated;

    
       
    // donation specific validation
    switch(frm_name) {
        
        case "gift.frm":
            
            // required: donation amount > 0
            validated = false;
            if (frm.amount.value) {
                if (is_decimal(frm.amount.value) && parseInt(frm.amount.value) > 0)    
                    validated = true;
                else
                   err_details += "\n- Please ensure your donation amount is greater than $0."; 
            }
            else { err_details += "\n- Please specify a donation amount.";  }   
            valid = valid && validated;
            
            break;
        
        
        case "donation.frm":
            
            // required: donation amount > 0
            validated = false;
            if (frm.amount.value) {
                if (is_decimal(frm.amount.value) && parseInt(frm.amount.value) > 0)    
                    validated = true;
                else
                   err_details += "\n- Please ensure your donation amount is greater than $0."; 
            }
            else { err_details += "\n- Please specify a donation amount.";  }   
            valid = valid && validated;
            
            
            // required: donation category
            validated = false;
            if (frm.itemID.value) { validated = true; }
            else { err_details += "\n- Please specify a donation category.";  }   
            valid = valid && validated;
            
            break;
        
        
        case "membership_donation.frm":
            
            // required: donation amount > 0
            validated = false;
            if (frm.amount.value) {
                if (is_decimal(frm.amount.value) && parseInt(frm.amount.value) > 0)    
                    validated = true;
                else
                   err_details += "\n- Please ensure your donation amount is greater than $0."; 
            }
            else { err_details += "\n- Please specify a donation amount.";  }   
            valid = valid && validated;            
            
            // required: donation category
            validated = false;
            if (frm.itemID_2.value) { validated = true; }
            else { err_details += "\n- Please specify a donation category.";  }   
            valid = valid && validated;
                        
            // required: membership type
            validated = false;
            if (frm.itemID.value) { validated = true; }
            else { err_details += "\n- Please specify a membership type.";  }   
            valid = valid && validated;
            
            // required: membership promo
            validated = false;
            if (frm.promoID.value) { validated = true; }
            else { err_details += "\n- Please specify whether you are a student or senior.";  }   
            valid = valid && validated;
            
            // required (if dual membership was selected)
            if (frm.itemID.value == DUAL_MEMBERSHIP_ITEM_ID) {
                
                validated = false;
                if (is_checked(frm.id, "secondTitle") || frm.secondOtherTitle.value) {
                    if (frm.secondOtherTitle.value && is_name(frm.secondOtherTitle.value))
                        validated = true;
                    else if (is_checked(frm.id, "secondTitle"))
                        validated = true;
                    else        
                       err_details += "\n- There are invalid characters in your second membership salutation title."; 
                }
                else { err_details += "\n- Please specify a second membership title.";  }   
                valid = valid && validated;
                
                                
                // required: name
                validated = false;
                if (frm.secondName.value) {
                    if (is_name(frm.secondName.value))    
                        validated = true;
                    else
                       err_details += "\n- There are invalid characters in your second member name."; 
                }
                else { err_details += "\n- Please enter your second member name.";  }   
                valid = valid && validated;
                
            }
            
                        
            break;
        
        
        case "membership.frm":
            
            // required: membership type
            validated = false;
            if (frm.itemID.value) { validated = true; }
            else { err_details += "\n- Please specify a membership type.";  }   
            valid = valid && validated;
            
            // required: membership promo
            validated = false;
            if (frm.promoID.value) { validated = true; }
            else { err_details += "\n- Please specify whether you are a student or senior.";  }   
            valid = valid && validated;
            
            // required (if dual membership was selected)
            if (frm.itemID.value == DUAL_MEMBERSHIP_ITEM_ID) {
                
                validated = false;
                if (is_checked(frm.id, "secondTitle") || frm.secondOtherTitle.value) {
                    if (frm.secondOtherTitle.value && is_name(frm.secondOtherTitle.value))
                        validated = true;
                    else if (is_checked(frm.id, "secondTitle"))
                        validated = true;
                    else        
                       err_details += "\n- There are invalid characters in your second membership salutation title."; 
                }
                else { err_details += "\n- Please specify a second membership title.";  }   
                valid = valid && validated;
                
                
                // required: name
                validated = false;
                if (frm.secondName.value) {
                    if (is_name(frm.secondName.value))    
                        validated = true;
                    else
                       err_details += "\n- There are invalid characters in your second member name."; 
                }
                else { err_details += "\n- Please enter your second member name.";  }   
                valid = valid && validated;
                
            }
            
            break;
        
    }
    
    
    // display error message, if applicable
    if (!valid) {	
         err_msg = "There was a problem when trying to submit your donation\n\nThe following errors were detected and must be resolved before ";
         err_msg += " the donation can be given: \n" + err_details;
     
         alert(err_msg);    
    }
 
    return valid;
    
}


