jq = jQuery.noConflict();
function ajaxFileUpload(fid,purl,did, img_url, img_div)
{
	if(jq("#"+fid+"").val()==''){
		alert('请先选择要上传的');
		jq("#"+fid+"").focus();
		return false;
	}
    jq(did).show();

    jq.ajaxFileUpload
    (
    {
        url:purl,
        //data: 'catid='+jq('#catid'),
        secureuri:false,
        fileElementId:fid,
        dataType: 'json',
        type: 'POST',
        success: function (data, status)
        {

            if(typeof(data.error) != 'undefined')
            {
                if(data.error != '')
                {
                    alert(data.error);
                }else
                {
                    //alert(data.url);
					jq("#"+img_url+"").val(data.url);
					$(img_div).innerHTML='<img src="'+data.url+'" width="150" height="120" style="margin-bottom:6px">';
                }
            }

            for(key in data) {
				
                if(data[key].code) {
                    code=data[key].code.replace(/&lt;/mg, "<");
                    eval(code);
                }
            }
			
        },
        error: function (data, status, e)
        {
            alert(allPrpos(data));
            alert(status);
            alert(allPrpos(e));
            alert('上传失败！请检查附件大小、格式！');
        },

        complete: function () {
            jq(did).hide();
        }
    }
    )

    return false;

}

function pics_delete(i,name){
    jq("#"+name+i).val("");
    image_preview(name+i,"",1);
    jq("#"+name+i+"_up").css("display","none");
}

function pics_delete1(name){
    jq("#pics"+name).val("");
    image_preview(name,"",1);
    jq("#pics"+name+"_up").css("display","none");
}

function getuploadhtml(i,name,purl){
    cname = name;
    name = name+i;
    var code = '<div id="'+name+'_up"><span id="'+name+'_preview"></span><br><br>地址：<input name="'+name+'" id="'+name+'" value="" size="50"/> <input id="'+name+i+'_del" type="button" name="delbutton" value="删除" onclick="pics_delete("'+name+i+'").value)" style="display:none;"><br><br>'
    code += '上传：<input type="file" name="'+name+'_upload" id="'+name+'_upload" style="width:400px" onchange="image_preview(\''+name+'\',this.value,1)"/>&nbsp;&nbsp;<input type="button" name="'+name+'upload" id="'+name+'upload'+i+'" onclick="return ajaxFileUpload2(\''+name+'_upload\',\''+purl+'\',\'#'+cname+'_loading\');" value="上传" /></div>';
    return code;
}
function getuploadhtml1(i,name,purl){
	//i=Integer.valueOf(i).intValue();
	//name=Integer.valueOf(name).intValue();
	cname = name; 
    name = name+1;
    var code = '<div id="pics'+cname+'_up"><span id="pics'+cname+'_preview"></span><br>地址：<input name="pics'+cname+'" id="pics'+cname+'" value="" size="50"/> <input id="pics'+cname+'_del" type="button" name="delbutton" value="删除" onclick="pics_delete1('+cname+')" style="display:none;"><br>'
    code += '上传：<input type="file" name="pics'+cname+'_upload" id="pics'+cname+'_upload" style="width:400px" onchange="image_preview(\'pics'+cname+'\',this.value,1)"/>&nbsp;&nbsp;<input type="button" name="pics'+cname+'upload" id="pics'+cname+'upload'+i+'" onclick="return ajaxFileUpload3(\'pics'+cname+'_upload\',\''+purl+'\',\'#pics'+cname+'_loading\','+name+');" value="上传" /></div>';
    return code;
}
var filecount =1;
function ajaxFileUpload2(fid,purl,did,name)//批量上传
{
    jq(did)
    .ajaxStart(function(){
        jq(this).show();
    })
    .ajaxComplete(function(){
        jq(this).hide();
    });

    jq.ajaxFileUpload
    (
    {
        url:purl,
        secureuri:false,
        fileElementId:fid,
        dataType: 'json',
        type: 'POST',
        success: function (data, status)
        {

            if(typeof(data.error) != 'undefined')
            {
                if(data.error != '')
                {
                    alert(data.error);
                }else
                {
                    alert(data.msg);
                }
            }

            for(key in data) {
				
                if(data[key].code) {
                    code=data[key].code.replace(/&lt;/mg, "<");
                    eval(code);
                }
            }
            jq(getuploadhtml(filecount,name,purl)).appendTo("#uploadarea");
            filecount++;
        },
        error: function (data, status, e)
        {
            alert('上传失败！请检查附件大小、格式！');
        },

        complete: function () {

        }
    }
    )

    return false;

}

function ajaxFileUpload3(fid,purl,did,name)//批量上传
{
    jq(did)
    .ajaxStart(function(){
        jq(this).show();
    })
    .ajaxComplete(function(){
        jq(this).hide();
    });

    jq.ajaxFileUpload
    (
    {
        url:purl,
        secureuri:false,
        fileElementId:fid,
        dataType: 'json',
        type: 'POST',
        success: function (data, status)
        {

            if(typeof(data.error) != 'undefined')
            {
                if(data.error != '')
                {
                    alert(data.error);
                }else
                {
                    alert(data.msg);
                }
            }

            for(key in data) {
				
                if(data[key].code) {
                    code=data[key].code.replace(/&lt;/mg, "<");
                    eval(code);
                }
            }
            jq(getuploadhtml1(filecount,name,purl)).appendTo("#uploadarea");
            filecount++;
        },
        error: function (data, status, e)
        {
            alert('上传失败！请检查附件大小、格式！');
        },

        complete: function () {

        }
    }
    )

    return false;

}


function image_preview(name,pic,limit) {
    if(pic) {
        jq("#"+name+"_del").css("display","");
        if(limit)
            jq(name+'_preview').innerHTML='<img src="'+pic+'" width="150">';
        else
            jq(name+'_preview').innerHTML='<img src="'+pic+'">';
    }
    else jq(name+'_preview').innerHTML='';
}


function addfiletoconent() {
    var FCKoEditor = FCKeditorAPI.GetInstance('content');
    inImg  = '<p><a href="'+get('attachment_path').value+'">附件：'+get('attachment_intro').value+'</a></p>';
    FCKoEditor.InsertHtml(inImg) ;
}

