


$(function() {
	
	$("#tags").blur(function(){
		
		var url="/checkClient";
		//var name=$(this).val();
		
		var name=$("#tags").attr('data-name');
		var company=$("#tags").attr('data-company');
		
		$.ajax({
			url:url,
			type: "post",
			cache: false,
			dataType: "json",
			data: {
				name:name,
				company:company
			},
			global: true,
			success: function(data){
				
				if(data.success){
					$("#tags").css("background-color","#fff");
				}else{
					$("#tags").css("background-color","#f15b6c");
				}
			},
			error:function(err){
				$("#tags").css("background-color","#f15b6c");
			}
			});
		
		
	});
	
	$("#tags").autocomplete({
        source: function( request, response ) {
            $.ajax({
                url: "/searchClient",
                dataType: "json",
                data:{
                    searchDbInforItem: request.term
                },
                success: function( data ) {
                    response( $.map( data, function( item ) {
                    	
                        return {
                        	name:item.clientname,
                        	value: item.clientname+"-"+item.companyname,
                        	company:item.companyname
//                            name:item.clientname+"-"+item.companyname,
//                            value: item.clientname+"-"+item.companyname
                        }
                    }));
                }
            });
        },
        minLength: 1,
        select: function( event, ui ) {
        	$("#tags").attr('data-name',ui.item.name);
        	$("#tags").attr('data-company',ui.item.company);
        	$("#name").val(ui.item.name);
        	$("#company").val(ui.item.company);
        	//console.log(ui.item.lebal);
            $("#tags").val(ui.item.value);
            
        }
    });

	$("#tags2").blur(function(){
		
		var url="/checkClient";
		
		var name=$("#tags2").attr('data-name');
		var company=$("#tags2").attr('data-company');
		
		$.ajax({
			url:url,
			type: "post",
			cache: false,
			dataType: "json",
			data: {
				name:name,
				company:company
			},
			global: true,
			success: function(data){
				
				if(data.success){
					$("#tags2").css("background-color","#fff");
				}else{
					$("#tags2").css("background-color","#f15b6c");
				}
			},
			error:function(err){
				$("#tags2").css("background-color","#f15b6c");
			}
			});
		
		
	});
	
	$( "#tags2" ).autocomplete({
        source: function( request, response ) {
            $.ajax({
                url: "/searchClient",
                dataType: "json",
                data:{
                    searchDbInforItem: request.term
                },
                success: function( data ) {
                	
                    response( $.map( data, function( item ) {
                    	
                        return {
                        	name:item.clientname,
                        	value: item.clientname+"-"+item.companyname,
                        	company:item.companyname
                        }
                    }));
                }
            });
        },
        minLength: 1,
        select: function( event, ui ) {
        	$("#tags2").attr('data-name',ui.item.name);
        	$("#tags2").attr('data-company',ui.item.company);
        	$("#name").val(ui.item.name);
        	$("#company").val(ui.item.company);
           $("#tags2").val(ui.item.dbId);
            
        }
    });

	
$("#tags3").blur(function(){
		
		var url="/checkLongle";
		
		var name=$(this).val();
		
		$.ajax({
			url:url,
			type: "post",
			cache: false,
			dataType: "json",
			data: {
				name:name
			},
			global: true,
			success: function(data){
				
				if(data.success){
					$("#tags3").css("background-color","#fff");
				}else{
					$("#tags3").css("background-color","#f15b6c");
				}
			},
			error:function(err){
				$("#tags3").css("background-color","#f15b6c");
			}
			});
		
		
	});
	
	$( "#tags3" ).autocomplete({
        source: function( request, response ) {
            $.ajax({
                url: "/searchLongle",
                dataType: "json",
                data:{
                    searchDbInforItem: request.term
                },
                success: function( data ) {
                	
                    response( $.map( data, function( item ) {
                    	
                        return {
                            name:item.key_num+"-"+item.product,
                            
                            value: item.key_num+"-"+item.product
                        }
                    }));
                }
            });
        },
        max: 12,
        minLength: 3,
        select: function( event, ui ) {
            $("#tags3").val(ui.item.dbId);
            
        }
    });
	
	
	$("#tags4").blur(function(){
		
		var url="/checkClient";
		
		var name=$("#tags4").attr('data-name');
		var company=$("#tags4").attr('data-company');
		
		$.ajax({
			url:url,
			type: "post",
			cache: false,
			dataType: "json",
			data: {
				name:name,
				company:company
			},
			global: true,
			success: function(data){
				
				if(data.success){
					$("#tags4").css("background-color","#fff");
				}else{
					$("#tags4").css("background-color","#f15b6c");
				}
			},
			error:function(err){
				$("#tags4").css("background-color","#f15b6c");
			}
			});
		
		
	});
	
	$( "#tags4" ).autocomplete({
        source: function( request, response ) {
            $.ajax({
                url: "/searchClient",
                dataType: "json",
                data:{
                    searchDbInforItem: request.term
                },
                success: function( data ) {
                	
                    response( $.map( data, function( item ) {
                    	
                        return {
                        	name:item.clientname,
                        	value: item.clientname+"-"+item.companyname,
                        	company:item.companyname
                        }
                    }));
                }
            });
        },
        minLength: 1,
        select: function( event, ui ) {
        	$("#tags4").attr('data-name',ui.item.name);
        	$("#tags4").attr('data-company',ui.item.company);
        	$("#name").val(ui.item.name);
        	$("#company").val(ui.item.company);
           $("#tags4").val(ui.item.dbId);
            
        }
    });
	
	
$("#tags5").blur(function(){
		
		var url="/checkCompany";
		
		var name=$(this).val();
		
		$.ajax({
			url:url,
			type: "post",
			cache: false,
			dataType: "json",
			data: {
				name:name
			},
			global: true,
			success: function(data){
				
				if(data.success){
					$("#tags5").css("background-color","");
					$("#contactPanelNew").html("<table><tbody><tr><td colspan='4'><input type='hidden' name='cid' value='"+data.data.cid+"'/><input type='submit' value='确定'  class='button' /></td></tr></tbody></table>");
				}else{
					$html='	<table><tbody>';
					
					$html+='<tr><th>单位传真</th><td><input type="text" name="fax"> </td><th>主页</th><td><input type="text" name="webservice"> </td></tr>';
					$html+='<tr><th>单位地区</th><td colspan="3"><select name="lv21" id="lv21" onchange="dislv(\'lv21\',22);">';
					$html+=data.nature['district'];
					$html+='</select><span id="mlv22"><select><option></option></select></span>';
					$html+='<span id="mlv23"><select><option></option></select></span>';
					$html+='</td></tr>';
					$html+='<tr><th>单位地址</th><td colspan="3"><input type="text" name="address"> </td></tr>';
					$html+='<tr><th>单位性质</th><td colspan="5">';
					
					for(var i=1;i<=data.nature.naturelenght;i++){
						$html+='<label><input type="checkbox" name="nature[]" value="'+data.nature[i]+'">'+data.nature[i]+'</label>&nbsp;';
					}
					
					$html+='<tr><th>乘车路线</th><td colspan="3"><input type="text" name="ride"> </td></tr>';
					$html+='<tr><th>地标建筑</th><td colspan="3"><input type="text" name="landmarks"> </td></tr>';
					$html+='<tr><th>住宿参考</th><td colspan="3"><input type="text" name="stay"> </td></tr>';
					$html+='<tr><td colspan="4"><input type="submit" value="确定"  class="button" /></td></tr>';
						
					$html+='</tbody></table>';
					
					$("#contactPanelNew").html($html);
					$("#tags5").css("background-color","#f15b6c");
				}
			},
			error:function(err){
				$("#contactPanelNew").html("");
				$("#tags5").css("background-color","#f15b6c");
			}
			});
		
		
	});
	
	$( "#tags5" ).autocomplete({
        source: function( request, response ) {
            $.ajax({
                url: "/searchCompany",
                dataType: "json",
                data:{
                    searchDbInforItem: request.term
                },
                success: function( data ) {
                	
                    response( $.map( data, function( item ) {
                    	
                        return {
                            name:item.companyname+"-"+item.local,
                            
                            value: item.companyname+"-"+item.local
                        }
                    }));
                }
            });
        },
        minLength: 1,
        select: function( event, ui ) {
        	//var str=ui.item.dbId;console.log(str);
        	//var str2=str.split(" ");
           $("#tags5").val(ui.item.dbId);
            
        }
    });
	
	
	
	

})

