/**
 * 新增商城url
 * 
 */
$(document).ready(function(){

	//自动填充
	$("#orgName").autocomplete({
		width : 300,
		max : 10,
		delay : 100,
		minLength : 1,
		autoFocus : true,
		cacheLength : 1,
		highlight : false,
		mustMatch:true,
		source : function(request, response) {
			var orgName = $("#orgName").val();
			if(orgName) orgName = orgName.trim();
    		$("#organizationId").val(""); 
			$.ajax({
				url : "../shoppingMall/queryOrganizaiton",
				data :{"orgName":orgName},
				type : "POST",
				dataType: "json",
				success : function(data) {
					response($.map(eval(data.msg), function(item) {
						return {
							value : item.name,
							orgName : item.name,
							id : item.id
						};
						
					}));
				}
			});
	
		},
		select : function(event, ui) {
			var data = ui.item;
			ui.item.value = data.orgName;
			$("#organizationId").val(data.id);
		},
		messages: {//设置不显示提示
	        noResults: '',
	        results: function() {}
	    }
	
	});
	
	//新增商城url
	$("#addMallUrl").click(function(){
		var mallName = $("#mallName").val();
		var id = $("#id").val();
		var url = $("#url").val();
		var organizationId = $("#organizationId").val();
		var urlKey = $("#urlKey").val();
		var addressCode = $("#addressCode").val();
		if(mallName == null || mallName == ""){
			alert("请输入商城url名称");
			return;
		}
		if(url == null || url == ""){
			alert("请输入商城url");
			return;
		}
//		var RegUrl = new RegExp(); 
//			RegUrl.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
//		if(!RegUrl.test(url)) {
//			alert("商城url不正确");
//			return; 
//		} 
		
		if(organizationId == null || organizationId == ""){
			alert("请输入所属项目");
			return;
		}
		if(urlKey == null || urlKey == ""){
			alert("请输入商城唯一key");
			return;
		}
		if(addressCode == null || addressCode == ""){
			alert("请输入商城addressCode");
			return;
		}
		
		$.ajax({
			type : 'POST',
			url : '../shoppingMall/addMallUrl',
			data:{"id":id,"mallName":mallName,"url":url,"organizationId":organizationId,"urlKey":urlKey,"addressCode":addressCode},
			dataType : 'json',// 返回值类型 一般设置为json
			success : function(data) // 服务器成功响应处理函数
			{
				if(data.code == '0'){
					alert(data.msg);
					window.location.href="../shoppingMall/list";
				}else{
					alert(data.msg);
				}
			}
		});
	});
	
	//取消返回
	$("#back").click(function(){
		window.location.href="../shoppingMall/list";
	});
    
});
