var optional_entries = new Array('page');
var special_entries = new Array('emailaddress', 'state', 'zip');

function validate_comment_form (this_form, form_type)
{
    switch (form_type)
    {
        case 'text':
        {
            if (!trim(this_form.comments.value))
            {
                alert('You must enter a comment before clicking Submit');
                return false;
            }

            break;
        }
        case 'image':
        {
            if (!trim(this_form.comment_image_upload.value) && !trim(this_form.comment_image_link.value))
            {
                alert('You must upload or provide a link to an image before clicking Submit');
                return false;
            }
            
            if (trim(this_form.comment_image_upload.value) && !is_image(this_form.comment_image_upload.value))
            {
                alert('You an invalid image type. Please correct the error before clicking Submit');
                return false;
            }
            
            if (trim(this_form.comment_image_link.value) && !is_url_file(this_form.comment_image_link.value))
            {
                alert('You specified an invalid link. Please correct the error before clicking Submit');
                return false;
            }
            
            if (!trim(this_form.comment_image_desc.value))
            {
                alert('You must enter a description for the image before clicking Submit');
                return false;
            }
            
            break;
        }
        case 'video':
        {
            if (!trim(this_form.comment_video_link.value))
            {
                alert('You must provide a link to a video before clicking Submit');
                return false;
            }
            
            var vid_link = strtolower(this_form.comment_video_link.value);
            
            if ((!is_url(this_form.comment_video_link.value) && !strstr(vid_link, '<embed')) && (!strstr(vid_link, 'youtube') && !strstr(vid_link, 'myspace')))
            {
                alert('You specified an invalid link. Please correct the error before clicking Submit');
                return false;
            }
            
            if (!trim(this_form.comment_video_desc.value))
            {
                alert('You must enter a description for the video before clicking Submit');
                return false;
            }

            break;
        }
        case 'link':
        {
            if (!trim(this_form.comment_link_title.value) || !trim(this_form.comment_link_url.value))
            {
                alert('You must enter both a title and a url before clicking Submit');
                return false;
            }
            
            if (!is_url(this_form.comment_link_url.value))
            {
                alert('You specified an invalid link. Please correct the error before clicking Submit');
                return false;
            }

            break;
        }
    }

    return true;
}
    
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'));
    is_error += validate_email(thisForm, Array('emailaddress'));
    
    if (is_error) 
	{
        alert ("Please correct the errors before continuing.   ");
		return false;
    }

    if (!thisForm.emailonly.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_numeric(thisForm, Array('zip'));
    is_error += validate_alpha(thisForm, Array('state'));
    is_error += validate_email(thisForm, Array('email'));
    
    if (is_error) 
	{
        alert ("Please complete all necessary fields before continuing.   ");
		return false;
    }
    
    return true;
}

function check_app_sap (thisButton, thisForm) 
{
    var is_error = 0;
    var start_index = 0;
    var end_index = (thisForm.length - 1);
    
    var days_keys = get_field_keys(thisForm, 'amb_work_days');
    var hours_keys = get_field_keys(thisForm, 'amb_work_hours');

    is_error += validate_form(thisForm, start_index, end_index);
    

    // Special Handling

    is_error += validate_email(thisForm, Array('email'));
    
    if (thisForm['ec_email'] && trim(thisForm['ec_email'].value))
    {
        is_error += validate_email(thisForm, Array('ec_email'));
    }
    
    if (days_keys.length) 
    {
        is_error += validate_checkboxes(thisForm, days_keys, 'amb_work_days');
    }
    
    if (hours_keys.length) 
    {
        is_error += validate_checkboxes(thisForm, hours_keys, 'amb_work_hours');
    }
    
    is_error += validate_amb(thisForm);
    is_error += validate_crime(thisForm);
    is_error += validate_emp(thisForm);
    
    
    if (is_error) 
	{
        alert ("Please complete all necessary fields before continuing.   ");
		return false;
    }
    
//    thisButton.disabled = true;

    return true;
}

function check_valid_email (this_field)
{
    var this_form = this_field.form;
    
    if (!is_email(this_field.value) && this_form['emailonly'].checked)
    {
        this_form['emailonly'].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 check_phone (this_field)
{
    if (this_field.value.length == 3 || this_field.value.length == 7)
    {
        this_field.value += '-';
    }
    
    if (strstr(this_field.value, '--'))
    {
        this_field.value = str_replace('--', '-', this_field.value);
    }
}

function check_date (this_field)
{
    if (this_field.value.length == 2 || this_field.value.length == 5)
    {
        this_field.value += '/';
    }
    
    if (strstr(this_field.value, '//'))
    {
        this_field.value = str_replace('//', '/', this_field.value);
    }
}

function check_date_short (this_field)
{
    if (this_field.value.length == 2)
    {
        this_field.value += '/';
    }
    
    if (strstr(this_field.value, '//'))
    {
        this_field.value = str_replace('//', '/', this_field.value);
    }
}

function check_image (this_field)
{
    if (!is_image(this_field.value)) 
    {
        alert('The image that you are attempting to upload must be a GIF, JPG, or PNG.');
    }
}

function check_doc (this_field)
{
    if (!is_doc(this_field.value)) 
    {
        alert('The document that you are attempting to upload must be a DOC, PDF, or TXT.');
    }
}

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++;
                        }
                        
                        break;
                    }
                    case 'select-one':
                    {
                        if (!this_entry.selectedIndex && !spec_val) 
                        {
                            if (this_title) 
                            {
                                this_title.className = 'form_invalid';
                            }

                            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;
                        
                        x += (num_radios - 1);


                        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_amb (thisForm)
{
    is_error = 0;

    if (thisForm['amb_bcbg'])
    {
        document.getElementById('amb_bcbg_position_title').className = 'form_valid';
        document.getElementById('amb_bcbg_when_title').className = 'form_valid';
        document.getElementById('amb_bcbg_where_title').className = 'form_valid';
    }

    if (thisForm['amb_bcbg'] && thisForm['amb_bcbg'][0].checked)
    {
        if (thisForm['amb_bcbg_position'] && !trim(thisForm['amb_bcbg_position'].value))
        {
            document.getElementById('amb_bcbg_position_title').className = 'form_invalid';
            is_error++;
        }

        if (thisForm['amb_bcbg_when_start'] && trim(thisForm['amb_bcbg_when_start'].value).length != 10)
        {
            document.getElementById('amb_bcbg_when_title').className = 'form_invalid';
            is_error++;
        }

        if (thisForm['amb_bcbg_when_end'] && trim(thisForm['amb_bcbg_when_end'].value).length != 10)
        {
            document.getElementById('amb_bcbg_when_title').className = 'form_invalid';
            is_error++;
        }
        
        if (thisForm['amb_bcbg_where'] && !trim(thisForm['amb_bcbg_where'].value))
        {
            document.getElementById('amb_bcbg_where_title').className = 'form_invalid';
            is_error++;
        }
    }
    
    if (thisForm['amb_bcbg']) 
    {
        document.getElementById('amb_terminated_text_title').className = 'form_valid';
        document.getElementById('amb_photo_title').className = 'form_valid';
        document.getElementById('amb_resume_title').className = 'form_valid';
        
        if (thisForm['amb_terminated'] && thisForm['amb_terminated'][0].checked) 
        {
            if (thisForm['amb_terminated_text'] && !trim(thisForm['amb_terminated_text'].value)) 
            {
                document.getElementById('amb_terminated_text_title').className = 'form_invalid';
                is_error++;
            }
        }
        
        if (thisForm['amb_photo'] && !trim(thisForm['amb_photo'].value))
        {
            document.getElementById('amb_photo_title').className = 'form_invalid';
            is_error++;            
        }
        else if (thisForm['amb_photo'] && trim(thisForm['amb_photo'].value)) 
		{
            if (!is_image(thisForm['amb_photo'].value)) 
			{
                document.getElementById('amb_photo_title').className = 'form_invalid';
                is_error++;
            }
        }
        
        if (thisForm['amb_resume'] && !trim(thisForm['amb_resume'].value))
        {
            document.getElementById('amb_resume_title').className = 'form_invalid';
            is_error++;            
        }
        else if (thisForm['amb_resume'] && trim(thisForm['amb_resume'].value)) 
		{
            if (!is_doc(thisForm['amb_resume'].value)) 
			{
                alert('The resume that you are attempting to upload must be a DOC, PDF, or TXT.');
                document.getElementById('amb_resume_title').className = 'form_invalid';
                is_error++;
            }
        }
        
        if (thisForm['amb_letter'] && !trim(thisForm['amb_letter'].value))
        {
            document.getElementById('amb_letter_title').className = 'form_invalid';
            is_error++;            
        }
        else if (thisForm['amb_letter'] && trim(thisForm['amb_letter'].value)) 
		{
            if (!is_doc(thisForm['amb_letter'].value)) 
			{
                document.getElementById('amb_letter_title').className = 'form_invalid';
                is_error++;
            }
        }
    }
    
    return is_error;
}

function validate_crime (thisForm)
{
    is_error = 0;

    if (thisForm['crime_convicted'])
    {
        document.getElementById('crime_convicted_amt_title').className = 'form_valid';
        document.getElementById('crime_prison_title').className = 'form_valid';
        document.getElementById('crime_prison_amt_title').className = 'form_valid';
        document.getElementById('crime_convicted_text_title').className = 'form_valid';
    }

    if (thisForm['crime_convicted'] && thisForm['crime_convicted'][0].checked)
    {
        var crime_convicted_amt = thisForm['crime_convicted_amt'].selectedIndex;
        crime_convicted_amt = thisForm['crime_convicted_amt'].options[crime_convicted_amt].value;

        if (thisForm['crime_convicted_amt'] && crime_convicted_amt === '0')
        {
            document.getElementById('crime_convicted_amt_title').className = 'form_invalid';
            is_error++;
        }

        if (!thisForm['crime_prison'][0].checked && !thisForm['crime_prison'][1].checked)
        {
            document.getElementById('crime_prison_title').className = 'form_invalid';
            is_error++;
        }
        else if (thisForm['crime_prison'][0].checked)
        {
            var crime_prison_amt = thisForm['crime_prison_amt'].selectedIndex;
            crime_prison_amt = thisForm['crime_prison_amt'].options[crime_prison_amt].value;

            if (thisForm['crime_prison_amt'] && crime_prison_amt === '0')
            {
                document.getElementById('crime_prison_amt_title').className = 'form_invalid';
                is_error++;
            }
        }
        
        if (thisForm['crime_convicted_text'] && !trim(thisForm['crime_convicted_text'].value))
        {
            document.getElementById('crime_convicted_text_title').className = 'form_invalid';
            is_error++;
        }
    }

    return is_error;
}


function validate_emp (thisForm)
{
    is_error = 0;

    if (thisForm['emp_company1'])
    {
        for (var x = 1; x <= 2; x++)
        {
            document.getElementById('emp_company' + x + '_title').className = 'form_valid';
            document.getElementById('emp_position' + x + '_title').className = 'form_valid';
            document.getElementById('emp_supervisor' + x + '_title').className = 'form_valid';
            document.getElementById('emp_start_date' + x + '_title').className = 'form_valid';
            document.getElementById('emp_industry' + x + '_title').className = 'form_valid';
            document.getElementById('emp_phone' + x + '_title').className = 'form_valid';
            document.getElementById('emp_address' + x + '_title').className = 'form_valid';
            document.getElementById('emp_city' + x + '_title').className = 'form_valid';
            document.getElementById('emp_state' + x + '_title').className = 'form_valid';
            document.getElementById('emp_zip' + x + '_title').className = 'form_valid';
            document.getElementById('emp_duties' + x + '_title').className = 'form_valid';
            document.getElementById('emp_reason' + x + '_title').className = 'form_valid';
            document.getElementById('emp_contact' + x + '_title').className = 'form_valid';
            
            if (trim(thisForm['emp_company' + x].value))
            {
                if (!trim(thisForm['emp_position' + x].value))
                {
                    document.getElementById('emp_position' + x + '_title').className = 'form_invalid';
                    is_error++;
                }                
                if (!trim(thisForm['emp_supervisor' + x].value))
                {
                    document.getElementById('emp_supervisor' + x + '_title').className = 'form_invalid';
                    is_error++;
                }
                if (!trim(thisForm['emp_start_date' + x].value))
                {
                    document.getElementById('emp_start_date' + x + '_title').className = 'form_invalid';
                    is_error++;
                }
                if (!trim(thisForm['emp_end_date' + x].value))
                {
                    document.getElementById('emp_start_date' + x + '_title').className = 'form_invalid';
                    is_error++;
                }
                if (!trim(thisForm['emp_industry' + x].value))
                {
                    document.getElementById('emp_industry' + x + '_title').className = 'form_invalid';
                    is_error++;
                }
                if (!trim(thisForm['emp_phone' + x].value))
                {
                    document.getElementById('emp_phone' + x + '_title').className = 'form_invalid';
                    is_error++;
                }
                if (!trim(thisForm['emp_address' + x].value))
                {
                    document.getElementById('emp_address' + x + '_title').className = 'form_invalid';
                    is_error++;
                }
                if (!trim(thisForm['emp_city' + x].value))
                {
                    document.getElementById('emp_city' + x + '_title').className = 'form_invalid';
                    is_error++;
                }
                if (!trim(thisForm['emp_state' + x].value))
                {
                    document.getElementById('emp_state' + x + '_title').className = 'form_invalid';
                    is_error++;
                }
                if (!trim(thisForm['emp_zip' + x].value))
                {
                    document.getElementById('emp_zip' + x + '_title').className = 'form_invalid';
                    is_error++;
                }
                if (!trim(thisForm['emp_duties' + x].value))
                {
                    document.getElementById('emp_duties' + x + '_title').className = 'form_invalid';
                    is_error++;
                }
                if (!trim(thisForm['emp_reason' + x].value))
                {
                    document.getElementById('emp_reason' + x + '_title').className = 'form_invalid';
                    is_error++;
                }
                if (!thisForm['emp_contact' + x][0].checked && !thisForm['emp_contact' + x][1].checked) 
                {
                    document.getElementById('emp_contact' + x + '_title').className = 'form_invalid';
                    is_error++;
                }                
            }
        }

    }

    return 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];
        
        if (thisForm[entry_name]) 
        {
            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];
        
        if (thisForm[entry_name]) 
        {
            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];

    if (thisForm[entry_name1]) 
    {
        var entry1 = thisForm[entry_name1];
        
        if (!is_email(entry1.value)) 
        {
            is_error++;
        }
        
        if (is_error) 
        {
            var title1 = document.getElementById(entry_name1 + '_title');
            
            if (title1) 
            {
                title1.className = 'form_invalid';
            }
        }
    }

    return is_error;
}


function validate_checkboxes (thisForm, keys, title)
{
    var is_error = 0;
    var is_check = 0;

    title = document.getElementById(title + '_title');
    title.className = 'form_valid';

	for (var x = 0; x < keys.length; x++)
	{
        var key = keys[x];

        if (thisForm[key].checked)
		{
			is_check++;
        } 
	}
	
	if (!is_check)
	{
        title.className = 'form_invalid';
        is_error++;
    }
    
    return is_error;
}

function is_image (this_img)
{
    var reg_exp = /(\.gif|\.png|\.jpg|\.jpeg)$/i;
    
    return reg_exp.test(this_img);
}

function is_doc (this_doc)
{
    var reg_exp = /(\.doc|\.txt|\.pdf)$/i;
    
    return reg_exp.test(this_doc);
}


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 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 get_field_keys (thisForm, fieldname)
{
    var field_cnt = 0;
    var keys = new Array();

    for (var x = 0; x < (thisForm.length - 1); x++)
    {
		var this_entry = thisForm[x].name;

        if (this_entry)
		{
    		if (this_entry.indexOf(fieldname) == 0) 
            {
                keys[field_cnt] = x;
    			field_cnt++;
    		}
        }
    }
    
    return keys;
}