$j(document).ready(function(){
	$j('#id_form').submit(function(){
		$j("input.f_deftxt").val("");							  
	});
						   
    $j('#btn_save').click(function(){
        $j('#id_form').submit()
    });
    $j('#btn_cancel').click(function(){
        window.location="/personal/profile/"
    });
    $j("#id_status").click(function(){
        if ($j(this).attr("checked"))
            $j(".for_autosalon").css("display", "");
        else
            $j(".for_autosalon").css("display", "none");
    });
    
    $j("#id_type_person_0").click(function(){ 
         $j("#id_requisites").slideUp();
		 $j("#id_logo").css("display", "none");
		 $j("#id_photo").css("display", "inline");
    });
    $j("#id_type_person_1").click(function(){
         $j("#id_requisites").slideDown();
		 $j("#id_logo").css("display", "inline");
		 $j("#id_photo").css("display", "none");
    });
    if ($j("#id_type_person_1").attr('checked')) {
        $j("#id_requisites").css("display", "");
    }
     if ($j("#id_status").attr('checked')) {
        $j(".for_autosalon").css("display", "");
    }
	
	
	$j('#id_location1').ajaxselect({ajaxpath:"'/classifier/get/children/'+this.value+'/related/'", target_select:$j("#id_location2"), spinner_class:'spinner15', first_option:false});
	// onload
	if ($j('#id_location1').get(0).selectedIndex == 0){
		$j("'#id_location12").attr('disabled', 'disabled');
	}else if ($j("#id_location2 option").length<1){ // else - for firm/edit only
		$j('#id_location1').map(function(){
			if ( !this.value || isNaN(this.value) ) {
				return;
			}
			var id_location2 = $j("#id_location2")
			id_location2.html('');
			$j.getJSON('/classifier/get/children/'+this.value +'/related/', {}, function(json){
				$j.each(json.items, function(i, item){
					id_location2.append($j("<option value='"+item.id+"'>"+ item.title +"</option>"))
				});
				var region_val = $j('#region_val').text();
				if (region_val.length > 0){
					$j('#id_location2').val(region_val); // ie6 bug
				}
			});
			id_location2.attr('disabled', '');
		});
	};
	
	
    /*
	$j('#id_location1').change(function(){
		if ( !this.value || isNaN(this.value) ) {
            return;
        }
        $j.getJSON('/classifier/get/children/'+this.value +'/related/', {}, function(json){
           id_location2 = $j("#id_location2")
           id_location2.html('');
            $j.each(json.items, function(i, item){
                id_location2.append($j("<option value='"+item.id+"'>"+ item.title +"</option>"))
            });
        });
    });
    $j("#id_location2").bind("ajaxSend", function(){
        $j(this).html('loading...');
        $j(this).attr('disabled', 'disabled')
    }).bind("ajaxSuccess", function(){
       $j(this).removeAttr('disabled')
    });
	
	// onload
	$j('#id_location1').map(function(){
		if ( !this.value || isNaN(this.value) ) {
            return;
        }
        $j.getJSON('/classifier/get/children/'+this.value +'/related/', {}, function(json){
           id_location2 = $j("#id_location2")
           id_location2.html('');
            $j.each(json.items, function(i, item){
                id_location2.append($j("<option value='"+item.id+"'>"+ item.title +"</option>"))
            });
		    var region_val = $j('#region_val').text();
			if (region_val.length > 0){
				$j('#id_location2').val(region_val);
			}
        });
    });
	*/
	
	function showNDS() {
		var obj=$j("#id_requisites-0-not_vat_payer");
		if (obj.attr("checked")){
			$j("#id_requisites-0-number_vat_certificate").attr("disabled", "disabled");
			obj.parents("#id_requisites tr.required").removeClass("required");
		}else{
			$j("#id_requisites-0-number_vat_certificate").attr("disabled", "");
			obj.parents("#id_requisites tr").addClass("required");
		}	
	};
		
	showNDS();
	$j("#id_requisites-0-not_vat_payer").click(function(){ showNDS(); });
	
});

