$(document).ready(function () {

    $("em.asterisk").click(function () {
		//$(this).parents().next("div.note").show();
        $(this).parents().next("div.note").css('visibility','visible');
		
	});
	
	$("img.close").click(function () {
		$(this).parents("div.note").css('visibility','hidden');
        //$(this).parents().next("div.note").css('visibility','hidden');
        //alert('ok');
	});
	
	$(".submit-but").attr("disabled", true);
	
	$("#agreebox").click(function () {
		var button = $(".submit-but");
		if (button.is(":disabled")) {
			button.removeAttr("disabled")
		}
		else {
			button.attr("disabled", true)
		}
	});
	
	

	
	$("input[name='credit_type']").click(function(){
		var radio = $("input[name='credit_type']:checked").val();
		var pole1 = $("input[name='op_ob']");
		var pole2 = $("input[name='zalog']");
		var select = $("div.f-b-price select");
		if (radio == '1')  {
			pole1.attr("disabled", true);
			pole2.attr("disabled", true);
			select.attr("disabled", true);
		}
		else if ( radio == '2') {
			pole1.removeAttr("disabled");
			pole2.removeAttr("disabled");
			select.removeAttr("disabled");
		}
		else if ( radio == '3') {
			pole1.removeAttr("disabled");
			pole2.attr("disabled", true);
			select.attr("disabled", true);
		}
		else if ( radio =='4'){
			pole1.removeAttr("disabled");
			pole2.attr("disabled", true);
			select.attr("disabled", true);
		}
		else {
			pole1.removeAttr("disabled");
			pole2.removeAttr("disabled");
			select.removeAttr("disabled");
		}
        
		$(":input[name='op_ob']:disabled").css("background","#D8D8D8");
        $(":input[name='op_ob']:enabled").css("background","");
        
        $(":input[name='zalog']:disabled").css("background","#D8D8D8");
		$(":input[name='zalog']:enabled").css('background',"");
	});
    
    
    $(".submit-but").click(function(){
        
       $('p.agree').text(' ');
       // reset wrong fields
       $('input,select').removeClass('required'); 
        
       $.ajax({
        url: '/pdf/index.php'
        , data: $("div.form>form").serialize()+"&ab=fakeok"
        , type: "POST"
        , dataType : 'json'
        , success: function (data) {
            
            if (data.result) {
                $('p.agree').text('Заявка принята. Мы свяжемся с вами как можно скорее.');
            } else {
                $('p.agree').text('Все поля обязательны для заполнения. Заполните все поля.');
                HighLightFields(data.fields);
            }          
        }
       });
       
       return false;
        
    });
    
    
    $("input[name=sum],input[name=credit_period],input[name=zalog],input[name=person_icount],input[name=work_stazh],input[name=work_zp6],input[name=company_hr],input[name=company_fond],input[name=company_fonds],input[name=company_gain],input[name=company_clear_s],input[name=contact_telcode],input[name=contact_tel],input[name=contact_teladd],input[name=contact_faxcode],input[name=contact_fax],input[name=tel_mobcode],input[name=tel_mob],input[name=tel_housecode],input[name=tel_house],input[name=tel_workcode],input[name=tel_work],input[name=tel_workcodeadd]").bind('keypress',function(e) {
        
        if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) {
            return false;
        }      
    });
    
    
 });
 
 function HighLightFields(fields) {
    
    for (i=0; i< fields.length; i++) {
        $("input[name="+fields[i]+"]").addClass('required');
    }
    return false;
 }