window.onload = function(){
    $(':input').click(function(){
        $('#medical_conditions').hide();
    });
    $('input[name=medical_condition]').click(function(){
        if ($(this).attr('value') == 'Yes') {
            $('#medical_conditions').show();
            $('#life_insurance_quotes select').show();
            $('input[name=current_weight]').show();
        }
        else {
            $('#medical_conditions').hide();
            $('#life_insurance_quotes select').show();
            $('input[name=current_weight]').show();
        }
    });
    $('#medical_conditions input').click(function(){
        $('#medical_conditions').show();
        var stringResult = '';
        $('#medical_conditions input:checked').each(function(){
            if (stringResult == '') {
                stringResult += $(this).attr('value');
            }
            else {
                stringResult += ',' + $(this).attr('value');
            }
        });
        $('#medical_condition_list').attr('value', stringResult);
    });
    $('input[name=current_weight]').focus(function(){
        if ($(this).attr('value') == '180') {
            $(this).attr('value', '');
        }
    });
    $('input[name=current_weight]').focusout(function(){
        if ($(this).attr('value') == '') {
            $(this).attr('value', '');
        }
    });
    $('input[name=term_life]').click(function(){
        if ($(this).attr('checked')) {
            $('select[name=coverage_years]').removeAttr('disabled');
        }
        else {
            $('select[name=coverage_years]').attr('disabled', 'disabled');
        }
    });
}

function closeConditionsWindow(){
    $('#medical_conditions').hide();
    $('#life_insurance_quotes select').show();
    $('input[name=current_weight]').show();
}

function validateInsuranceForm(){
	var error_level = [];
	var term_life = $('input[name=term_life]:not(:checked)').length;
	var whole_life = $('input[name=whole_life]:not(:checked)').length;
	var current_weight = Number($('input[name=current_weight]').val()=="");
	var birth_month = Number($('select[name=birth_month] option:selected').val()=="0");
	var birth_day = Number($('select[name=birth_day] option:selected').val()=="0");
	var birth_year = Number($('select[name=birth_year] option:selected').val()=="0");
	var medical_conditions = $('#medical_conditions input:checked').length;
	var condition = $('input[name=medical_condition]:checked').val();
	if(term_life && whole_life){
		error_level.push('Type of Insurance');
	}
	if(current_weight){
		error_level.push('Weight (lbs)');
	}	
	if(birth_month || birth_day || birth_year){
		error_level.push('Date of Birth');
	}
	if(medical_conditions==0 && condition=="Yes"){
		error_level.push('Medical condition?');
	}
	$('.validator').each(function(){
		$('.errorField').html('&nbsp;');
		if($(this).hasClass('error')){
			var text = $(this).children('div').html();
			$(this).html(text);
			$(this).removeClass('error');
		}
	});
	if(error_level.length>0){
		$('.errorField').html('<div style="float:left;"><img src="/images/form_starts/errorfield.png" /></div> Invalid Entry');
		$('.validator').each(function(){
			if(jQuery.inArray(jQuery.trim($(this).html()),error_level)>-1){
				$(this).addClass('error');
				var newCode = '<div style="margin-left:0px;">'+$(this).html()+'</div><img src="/images/form_starts/errorfield.png" border="0" style="margin-top:-1px;margin-left:10px" title="Error found in '+$(this).html()+'"/>';
				$(this).html(newCode);
			}
		});
		return false;
	}else{
		document.forms['life_quote_form'].submit();
		return true;
	}
}

