var optional_entries = new Array('address2');
var special_entries = new Array('email', 'email2', 'state', 'country');

    
function check_mlist (thisForm) 
{
    var is_error = 0;
    var start_index = 0;
    var end_index = (thisForm.length - 1);

    is_error += validate_form(thisForm, start_index, end_index);
    

    // Special Handling
    
    is_error += validate_numeric(thisForm, Array('zip'));
    is_error += validate_alpha(thisForm, Array('state', 'country'));
    is_error += validate_email(thisForm, Array('email','email2'));
    
    if (is_error) 
	{
        alert ("Please correct the errors before continuing.   ");
		return false;
    }

    if (!thisForm.receive_email.checked)
    {
        alert ("You must agree to the Terms of Use & Policies before clicking Submit.");
        return false;
    }
        
    return true;
}

function check_contact (thisForm) 
{
    var is_error = 0;
    var start_index = 0;
    var end_index = (thisForm.length - 1);

    is_error += validate_form(thisForm, start_index, end_index);
    

    // Special Handling
    
    is_error += validate_email(thisForm, Array('email','email2'));
    
    if (is_error) 
	{
        alert ("Please correct the errors before continuing.   ");
		return false;
    }
    
    return true;
}

function check_valid_email (this_field)
{
    var this_form = this_field.form;
    
    if (!is_email(this_field.value) && this_form['receive_email'].checked)
    {
        this_form['receive_email'].checked = false;
    }   
}

function check_email (this_field)
{
    if (this_field.checked)
    {
        var this_form = this_field.form;

        if (!is_email(this_form['email'].value))
        {
            alert('You must enter a valid EMAIL before checking this box.');
            this_field.checked = false;
        }
    }
}

function confirm_email2 (this_form)
{
    var email_status = document.getElementById('email_status');
    var email1 = this_form['email'].value;
    var email2 = this_form['email2'].value;
    
    if (!trim(email2))
    {
        email_status.className = 'email_none';
    }
    else if (email1 == email2)
    {
        email_status.className = 'email_green';
    }
    else
    {
        email_status.className = 'email_red';
    }
}

function validate_form (thisForm, start, end)
{
    var is_error = 0;
    
    for (var x = start; x < end; x++)
    {
        var this_type = thisForm[x].type;
        
        if (this_type)
        {
            var this_entry = thisForm[x];
            var this_title = document.getElementById(this_entry.name + '_title');
            var spec_val = special_val(this_entry.name, special_entries);


            if (this_title)
            {
                this_title.className = 'form_valid';
            }
            
            if (!in_array(this_entry.name, optional_entries)) 
            {
                switch (this_type)
                {
                    case 'text':
                    case 'textarea':
                    {
                        var this_size = this_entry.size;
                            
                        if (!trim(this_entry.value) && !spec_val) 
                        {
                            if (this_title) 
                            {
                                this_title.className = 'form_invalid';
                            }
                            is_error++;
                        }
                        else if (spec_val) 
                        {
/*
                            var ret_arr = validate_group(x, thisForm, spec_val);
                                    
                            if (parseInt(ret_arr['is_error']) && this_title) 
                            {
                                this_title.className = 'form_invalid';
                            }
                                    
                            x = ret_arr['x'];
                            is_error += parseInt(ret_arr['is_error']);
*/
                        }
                        
                        break;
                    }
                    case 'select-one':
                    {
                        if (!this_entry.selectedIndex && !spec_val) 
                        {
                            if (this_title) 
                            {
                                this_title.className = 'form_invalid';
                            }
                            is_error++;
                        }
                        else if (spec_val) 
                        {
/*
                            var ret_arr = validate_group(x, thisForm, spec_val);
                                
                            if (parseInt(ret_arr['is_error']) && this_title) 
                            {
                                this_title.className = 'form_invalid';
                            }
                                
                            x = ret_arr['x'];
                            is_error += parseInt(ret_arr['is_error']);
*/
                        }
                        else 
                        {
                            var selected = this_entry.options[this_entry.selectedIndex];
                               
                            if (selected.value == 'OTHER') 
                            {
                                var other = document.getElementById(this_entry.name + '_other');
                                    
                                if (!trim(other.value)) 
                                {
                                    if (this_title) 
                                    {
                                        this_title.className = 'form_invalid';
                                    }
                                    is_error++;
                                }
                            }
                        }
                        
                        break;
                    }
                    case 'file':
                    {
                        break;
                    }
                    case 'radio':
                    {
                        var num_radios = thisForm[this_entry.name].length;
                        var rad_checked = 0;
                        
                        for (var y = 0; y < num_radios; y++) 
                        {
                            var this_rad = thisForm[this_entry.name][y];
                            
                            if (this_rad.checked) 
                            {
                                rad_checked++;
                            }
                        }
                        
                        if (!rad_checked && !spec_val) 
                        {
                            if (this_title) 
                            {
                                this_title.className = 'form_invalid';
                            }
                            is_error++;
                        }
                        
                        break;
                    }
                    case 'checkbox':
                    {
                        break;
                    }
                }
            }
        }
    }
    
    return is_error;
}

function validate_group (start, thisForm, special_val)
{
    var is_error = 0;
    var x_end = start - 1;
    var sel_arr = new Array();
    var sel_cnt = 0;

    for (var x = start; x < (thisForm.length - 1); x++)
    {
        if (thisForm[x]) 
        {
            var this_entry = thisForm[x];
            var this_type = this_entry.type;
            var this_name = this_entry.name;


            if (!strstr(this_name, special_val)) 
            {
                x_end = x - 1;
                x = thisForm.length;
            }
            else 
            {
                switch (this_type)
                {
                    case 'text':
                    case 'textarea':
                    {
                        if (!strstr(this_name, '_other')) 
                        {
                            var this_size = this_entry.size;
                            
                            if (!trim(this_entry.value) || (this_size != 9999 && this_entry.value.length < this_size)) 
                            {
                                is_error++;
                            }
                        }
                        
                        break;
                    }
                    case 'select-one':
                    {
                        if (!strstr(this_name, '_why') && !this_entry.selectedIndex)
                        {
                            is_error++;
                        }
                        else
                        {
                            var selected = this_entry.options[this_entry.selectedIndex];
                            
                            if (selected.value == 'OTHER')
                            {

                                var other = document.getElementById(this_name + '_other');

                                if (!trim(other.value))
                                {
                                    is_error++;
                                }
                            }
                            else if (selected.value == 'NONE')
                            {
                                
                            }
                            else
                            {
                                if (!strstr(this_name, '_why')) 
                                {
                                    if (in_array(selected.value, sel_arr)) 
                                    {
                                        // Duplicate selection 
                                        is_error++;
                                    }
                                    else 
                                    {
                                        sel_arr[sel_cnt] = selected.value;
                                        sel_cnt++;
                                    }
                                }
                            }
                        }

                        break;
                    }
                }
            }
        }
    }
    
    return {'x': x_end, 'is_error': is_error };
}


function validate_numeric (thisForm, numeric_entries)
{
    var is_error = 0;

    for (var x = 0; x < numeric_entries.length; x++) 
    {
        var entry_name = numeric_entries[x];
        var this_value = thisForm[entry_name].value;

        if ((!trim(this_value) && !in_array(entry_name, optional_entries)) || (trim(this_value) && !is_numeric(this_value))) 
        {
            var this_title = document.getElementById(entry_name + '_title');
            
            if (this_title) 
            {
                this_title.className = 'form_invalid';
                is_error++;
            }
        }
    }
    
    return is_error;
}

function validate_alpha (thisForm, alpha_entries)
{
    var is_error = 0;

    for (var x = 0; x < alpha_entries.length; x++) 
    {
        var entry_name = alpha_entries[x];
        var this_value = thisForm[entry_name].value;

        if (!is_alpha(this_value))
        {
            var this_title = document.getElementById(entry_name + '_title');
            
            if (this_title) 
            {
                this_title.className = 'form_invalid';
                is_error++;
            }
        }
    }
    
    return is_error;
}

function validate_email(thisForm, email_entries) 
{
    var is_error = 0;
    
    var entry_name1 = email_entries[0];
    var entry_name2 = email_entries[1];
    var entry1 = thisForm[entry_name1];
    var entry2 = thisForm[entry_name2];
    
    if (!is_email(entry1.value) && trim(entry1.value))
    {
        is_error++;
    }
    else if (entry1.value == entry2.value)
    {
        if (!is_email(entry1.value) || !is_email(entry2.value)) 
        {
            is_error++;
        }
    }
    else if (entry1.value != entry2.value)
    {
        is_error++;
    }
    
    if (is_error)
    {
        var title1 = document.getElementById(entry_name1 + '_title');
        var title2 = document.getElementById(entry_name2 + '_title');
            
        if (title1 && title2) 
        {
            title1.className = 'form_invalid';
            title2.className = 'form_invalid';
        }
    }

    return is_error;
}

function is_alpha (this_text)
{
    var reg_exp = /[a-zA-Z]+/;
    
    return reg_exp.test(this_text);
}

function is_email (this_email)
{
    var reg_exp = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
    
    return reg_exp.test(this_email);
}

function is_numeric (mixed_var) 
{
    return !isNaN(mixed_var);
}


function special_val (entry_name, special_entries)
{
    for (var x = 0; x < special_entries.length; x++)
    {
        if (strstr(entry_name, special_entries[x]))
        {
            return special_entries[x];
        }
    }
    
    return false;
}

function in_array (needle, haystack)
{
    for (var x = 0; x < haystack.length; x++)
    {
        if (needle == haystack[x])
        {
            return true;
        }
    }
    
    return false;
}

function strpos (haystack, needle, offset)
{
    if (!offset)
    {
        offset = 0;
    }
    
    var index = haystack.indexOf(needle, offset);
    
    if (index == -1)
    {
        return false;
    }
    
    return index;
}

function strstr (haystack, needle, before_needle)
{
    var pos = strpos(haystack, needle);

    if (pos === false)
    {
        return false;
    }
    else
    {
        if (before_needle) 
        {
            return haystack.substr(0, pos);
        }
        else
        {
            return haystack.substr(pos);
        }
    }
}

function str_replace (search, replace, subject)
{
    return subject.replace(search, replace);
}


function trim (text) 
{
	return text.replace(/^\s+|\s+$/g,"");
}

function ltrim (text) 
{
	return text.replace(/^\s+/,"");
}

function rtrim (text) 
{
	return text.replace(/\s+$/,"");
}