//微信图片上传
if(!appId)appId='';
if(!signature)signature='';
var wxchooserimage=false;
if(appId && signature){
	wx.config({  
		debug: false,  
		appId: appId,  
		timestamp: timestamp,  
		nonceStr: nonceStr,  
		signature: signature,  
		jsApiList: [
			// 所有要调用的 API 都要加到这个列表中 
			'checkJsApi',
			'chooseImage',
            'uploadImage'
		]
	});
	wx.ready(function (){
	   wxchooserimage=true;
       $('input[type="file"]').attr('type','button');
    });  
}
function upd_borrow_file(obj, file_id, name, thumb) {
	if (!thumb) thumb = 'image_file_image';
	if (!name) name = 'idno_img';    
    var okfun=function(data,status){
        if (data.error == 0 && data.url) {
			if (thumb != 'NULL') $("#" + thumb).attr("src", data.url + "?r=" + Math.random());
			$("input[name='idno_imgs']").val(data.url);
			$("input[name='" + name + "']").val(data.url);
		}
        if (data.responseText && data.responseText.indexOf('<script') > 0) {
			strs = data.responseText.split("<script");
			arr = eval("(" + strs[0] + ")");
			if (arr['error'] == 0) {
				if (thumb != 'NULL') $("#" + thumb).attr("src", arr['url'] + "?r=" + Math.random());
				$("input[name='" + name + "']").val(arr['url']);
				$("input[name='idno_imgs']").val(arr['url']);
			}
		}
        $(".fileuploading").hide();
    };
    if(wxchooserimage){
        $(".fileuploading").show();
    	commonupload(file_id,okfun,okfun);
    }else{
        $("input[name='" + file_id + "']").bind("change", function() {
    		$(".fileuploading").show();
    		commonupload(file_id,okfun,okfun);
    		$("input[name='" + file_id + "']").unbind("change");
    	});
    }
}
function upd_borrow_files(obj, file_id, name, thumb) {
	if (!thumb) thumb = 'image_file_images';
	if (!name) name = 'ming_img';
    var okfun=function(data,status){
        if (data.error == 0 && data.url) {
			if (thumb != 'NULL') $("#" + thumb).attr("src", data.url + "?r=" + Math.random());
			$("input[name='ming_imgs']").val(data.url);
		}
        if (data.responseText && data.responseText.indexOf('<script') > 0) {
			strs = data.responseText.split("<script");
			arr = eval("(" + strs[0] + ")");
			if (arr['error'] == 0) {
				if (thumb != 'NULL') $("#" + thumb).attr("src", arr['url'] + "?r=" + Math.random());
				$("input[name='ming_imgs']").val(arr['url']);
			} else {
			}
			$(".fileuploadings").hide();
			return;
		}
		$(".fileuploadings").hide();
    };
    if(wxchooserimage){
        $(".fileuploading").show();
    	commonupload(file_id,okfun,okfun);
    }else{
        $("input[name='" + file_id + "']").bind("change", function() {
    		$(".fileuploading").show();
    		commonupload(file_id,okfun,okfun);
    		$("input[name='" + file_id + "']").unbind("change");
    	});
    }
}
function upd_borrow_file_header(obj, file_id, name, thumb) {
	if (!thumb) thumb = 'image_file_image_header';
	if (!name) name = 'header_img';
    var okfun=function(data,status){
		if (data.error == 0 && data.url) {
			if (thumb != 'NULL') $("#" + thumb).attr("src", data.url + "?r=" + Math.random());
			$("input[name='" + name + "']").val(data.url);
		}else{
		  if(data.message)showtoast(data.message);
		}
        if (data.responseText && data.responseText.indexOf('<pre')) {
			strs = data.responseText.split("<pre");
			arr = eval("(" + strs[0] + ")");
			if (arr['error'] == 0) {
				if (thumb != 'NULL') $("#" + thumb).attr("src", arr['url'] + "?r=" + Math.random());
				$("input[name='" + name + "']").val(arr['url']);
			} else {}//alert(arr['message']);
			$(".fileuploading").hide();
		}$(".fileuploading").hide();
	};
    if(wxchooserimage){
        $(".fileuploading").show();
    	commonupload(file_id,okfun,okfun);
    }else{
        $("input[name='header_imgs']").bind("change", function() { // 1-19 zjn header_imgs-->file_id
    		$(".fileuploading").show();
    		commonupload(file_id,okfun,okfun);
    		$("input[name='" + file_id + "']").unbind("change");
    	});
    }
}

function upd_borrow_file_header1(obj, i, file_id, name, thumb) {
	if (!thumb) thumb = 'image_file_image_header' + i;
	if (!name) name = 'header_img' + i;
    
    var okfun=function(data,status){
        if (data.error == 0 && data.url) {
			if (thumb != 'NULL') $("#" + thumb).attr("src", data.url + "?r=" + Math.random());
			$("input[name='" + name + "']").val(data.url);
		}
        if (data.responseText && data.responseText.indexOf('<script') > 0) {
			strs = data.responseText.split("<script");
			arr = eval("(" + strs[0] + ")");
			if (arr['error'] == 0) {
				if (thumb != 'NULL') $("#" + thumb).attr("src", arr['url'] + "?r=" + Math.random());
				$("input[name='" + name + "']").val(arr['url']);
			}
		}
		$(".fileuploading" + i).hide();        
    };
    if(wxchooserimage){
        $(".fileuploading").show();
    	commonupload(file_id,okfun,okfun);
    }else{
        $("input[name='" + file_id + "']").bind("change", function() {
    		$(".fileuploading").show();
    		commonupload(file_id,okfun,okfun);
    		$("input[name='" + file_id + "']").unbind("change");
    	});
    }
}
function commonupload(file_id,okfun='',errfun=''){
    if(wxchooserimage){
        wx.chooseImage({
            count: 1, // 默认9
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function(res) {
                var localIds = res.localIds; // 返回选定照片的本地ID列表，localId可以作为img标签的src属性显示图片
                //$(obj).attr('src', localIds);
                //console.log(localIds[0]);
                // 上传照片
                wx.uploadImage({
                    localId: localIds[0],
                    isShowProgressTips: 1,
                    success: function(res) {
                        serverId = res.serverId;
                        //console.log(serverId);
                        $.ajax({
                    		url:APP + 'Home/File/upload',
                    		data:{'mediaid':serverId},
                    		type:"post",
                    		success:function(data){
                    			if(data.status==1) {
                                    if(okfun)okfun(data,1);
        	            		}else{
        	                		if(errfun)errfun(data,status);
        	            		}
                    		}
                    	})
                        
                    }
                });
            },error: function(data, status, e) {
                alert(data);
    			if(errfun)errfun(data,status);
    		}
        });
    }else{
        $.ajaxFileUpload({
    		url: APP + 'Home/File/upload',
    		secureuri: false,
    		fileElementId: file_id,
    		dataType: 'json',
    		success: function(data, status) {
    		    if(okfun)okfun(data,status);
    		},
    		error: function(data, status, e) {
    			if(errfun)errfun(data,status);
    		}
    	});
    }
}