var uploadProcessSid = -1;
var aryUploadFieldInfo = [];

//aryInfo [inputname , page , uploaded event , loading Img , remove event , loading evt]
function jsInitUploadField(aryInfo)
{
	aryUploadFieldInfo = aryInfo;
	
	for (var i=0;i<aryInfo.length;i++)
	{
		$(aryInfo[i][0]).each(
			function(z)
			{
				strID = $(this).attr('id');
				strID = ((strID != "")? strID : "uuuuuploadffieldi"+i+"_"+z );
				$(this).attr('id' , strID);
				strName = $(this).attr('name');
				
				strHtml = "<div id='uuuuuploadffield"+i+"_"+z+"' class='fileUploadField' uuuzz='"+i+"' uuuid='"+strID+"' uuuname='"+strName+"' ></div>";
				$(this).attr('uploadajxv1',"uuuuuploadffield"+i+"_"+z);
				$(this).attr('uploadajxv2',i);			
				$(this).attr('uploadajxv3',z);						
				$(this).after(strHtml);
				
				$(this).change(
					function()
					{	
						funDoUpload($(this));	
						
						//$(this).css('display','none');
						//alert($(this).val());
					}
				);
			}
		)
	}
	
}

function funDoUpload(objOtem)
{
	var strTxt = (objOtem.val());
	//alert(strTxt);
						
	$("#"+objOtem.attr('uploadajxv1')).html("<div class='cssInfo'>"+strTxt+"</div><div class='cssLoading'></div><A href='javascript:;' onclick='jsUploadLoadingCancel(\""+objOtem.attr('id')+"\",\""+objOtem.attr('uploadajxv1')+"\")'>cancel</a>");
	jsUploadLoading("#"+objOtem.attr('uploadajxv1')+" div.cssLoading",objOtem.attr('uploadajxv2')); 		
	jsUploadFieldNow(objOtem.attr('uploadajxv2'),objOtem.attr('uploadajxv3'),objOtem.attr('id'),objOtem.attr('uploadajxv1'));
						
}

function jsUploadLoadingCancel(strID,strDesc)
{
	$("#"+strID).css('display','block');
	$("#"+strID).val('');
	$("#"+strDesc).html("");
	
	aryUploadFieldInfo[$("#"+strDesc).attr('uuuzz')][4](strID,strDesc);
	
	$("#"+strID).change(
		function()
		{	
			funDoUpload($(this));		
			//$(this).css('display','none');
			//alert($(this).val());
		}
	);
}

function jsUploadLoading(strHtml,strImg)
{
	clearInterval(uploadProcessSid);
	$(strHtml).html("<img src=\""+aryUploadFieldInfo[strImg][3]+"\" />");
	//$("#"+strIFormName).submit();	
	if (typeof(aryInfo[strImg][5]) == 'function' )
	{
		
		aryInfo[strImg][5]();
	}	
	
}
	
function jsUploadFieldNow(intIndex,intNum,strFileID,strDisplayField)
{
	var aryUInfo = aryUploadFieldInfo[intIndex];
	var strIFrameName = "uploadbuff_"+intIndex+"_"+intNum;
	var strIFormName = "uploadform_"+intIndex+"_"+intNum;
	var strFileID
	
	var objFrame = jsUploadcreateIFrame(strIFrameName);
	var objForm = jsUploadcreateIForm(strIFormName,strFileID,strDisplayField);
			
	$("#"+strIFormName).attr('method','post');
	$("#"+strIFormName).attr('action', aryUInfo[1]);
	$("#"+strIFormName).attr('target',strIFrameName);
	//$("#"+strIFormName).submit();
			
	uploadCallback = function()
	{
		var io = document.getElementById(strIFrameName);
       	var strReturn = {};			
				
		if(io.contentWindow)
		{
			 strReturn = io.contentWindow.document.body?io.contentWindow.document.body.innerHTML:null;
           	//strReturn = io.contentWindow.document.XMLDocument?io.contentWindow.document.XMLDocument:io.contentWindow.document;
		}
		else if(io.contentDocument)
		{
			 strReturn = io.contentDocument.document.body?io.contentDocument.document.body.innerHTML:null;
           	//strReturn = io.contentDocument.document.XMLDocument?io.contentDocument.document.XMLDocument:io.contentDocument.document;
		}		
		aryR = strReturn.split("}");
		eval("strReturn = "+aryR[0]+"};");
		//alert(strReturn);
		//aryUInfo[2](strReturn);					
		jsUploadedFile(strReturn);
		
	 }
			
	if(window.attachEvent)
	{
          	document.getElementById(strIFrameName).attachEvent('onload', uploadCallback);
    }
    else
    {
          	document.getElementById(strIFrameName).addEventListener('load', uploadCallback, false);
    }
    
    $("#"+strFileID).css('display','none');
	$("#"+strIFormName).submit();
    
}
	
function jsUploadcreateIFrame(strFrameId)
{	
	if ($("#"+strFrameId).length > 0)
		$("#"+strFrameId).remove();
	if(window.ActiveXObject) 
	{
		var io = document.createElement('<iframe id="' + strFrameId + '" name="' + strFrameId + '" />');
        
        if(typeof uri== 'boolean')
        {
        	io.src = 'javascript:false';
        }
       	else if(typeof uri== 'string')
       	{
        	io.src = uri;
        }
     }
     else 
     {
   		var io = document.createElement('iframe');
        io.id = strFrameId;
        io.name = strFrameId;
     }
     document.body.appendChild(io);
     io.style.position = 'absolute';
     io.style.top = '-1000px';
     io.style.left = '-1000px';
			
	return io;
}
		
function jsUploadcreateIForm(strFormName,strFileID,strDisplayField)
{
	if ($("#"+strFormName).length > 0)
		$("#"+strFormName).remove();	
	strHtml = "<form action=\"\" name=\""+strFormName+"\" id=\""+strFormName+"\" method=\"post\" enctype=\"multipart/form-data\">";
	//strHtml+= "<input type='hidden' name='df' value='"+strDisplayField+"'>";
	strHtml+= "</form>";
	
	objFileForm = $(strHtml);
			
	var oldElement = $('#' + strFileID);
	var newElement = $(oldElement).clone();
	//$(oldElement).attr('id', $(oldElement).attr('name'));
	//$(oldElement).attr('name', strDisplayField);
	//alert($(oldElement).attr('name'));
	//$(oldElement).attr('id', strFileID );
	
	$(oldElement).attr('dddf', $(oldElement).attr('name'));
	$(oldElement).attr('id', strDisplayField+"txt");
	$(oldElement).attr('name', strDisplayField);
	$(oldElement).before(newElement);
	$(oldElement).appendTo(objFileForm);
	
	$(objFileForm).css('position', 'absolute');
	$(objFileForm).css('top', '-1200px');
	$(objFileForm).css('left', '-1200px');
	$(objFileForm).appendTo('body');
			
	return objFileForm;
}

function jsUploadedFile(re)
{
//alert(re);
	if (re.code == 1)
	{
		var strName = ($("#"+re.field+"txt").attr('dddf'));
		strHtml = re.fname+" "+re.fsize+"kb";
		strHtml += "<input type='hidden' name='"+strName+"' value='"+(re.fsname)+"'>";
		//strHtml += "&nbsp;<a href='javascript' onclick=''>remove</a>";
		$("#"+re.field).children("div.cssInfo").html(strHtml);
		$("#"+re.field).children("div.cssLoading").html("");
		
		var intIndex = ($("#"+re.field).attr('uuuzz'));
		aryUploadFieldInfo[intIndex][2](re);
		//alert();
	}
}	
