if (xajax)
{
	xajax.initFileInputs = function ()
	{
		inputs = document.getElementsByTagName('input');
		for( var i=0; i < inputs.length; i++)
		{
			inp=inputs[i];
			if (!inp.className)
				continue; //doesnt have a class defined
			if (inp.className.indexOf('xajax_file')==-1)
				continue; //not an xajax file upload
			if (inp.style.visibility=='hidden')
				continue; //already converted this file upload
			xajax.newFileUpload(inp.id, inp.id+'-'+xajax.workId)
				inp.style.visibility = 'hidden';
			inp.style.height = '0';
			inp.style.width = '0';
		} 
	}
	xajax.newFileUpload = function(sParentId, sId)
	{
		//xajax.insertAfter(sParentId, 'iframe', sId);
		//newFrame = xajax.$(sId);
		newFrame = document.createElement("IFRAME");

		newFrame.id=sId;
		newFrame.name=sId;
		newFrame.style.height="25px";
		//newFrame.style.height="80";
		newFrame.style.width="300px";
		newFrame.style.border="0";
		newFrame.style.overflow="hidden";
		newFrame.position="relative";
		newFrame.scrolling="no";
		newFrame.setAttribute('frameborder', "0");
		newFrame.setAttribute('marginheight', "0");
		newFrame.setAttribute('marginwidth', "0");
		newFrame.allowtransparency=true;
		newFrame.style.backgroundColor="transparent";
		xajax.$(sParentId).parentNode.insertBefore(newFrame,xajax.$(sParentId).nextSibling);
		//need to wait for Mozilla to notice there's an iframe
		setTimeout('xajax._fileUploadContinue("'+sParentId+'","'+sId+'");', 100);
	}
	xajax._fileUploadContinue = function(sParentId, sId)
	{
		//uploadIframe = window.frames[sId];
		uploadIframe = xajax.$(sId);
		origInput = xajax.$(sParentId);
		if (!uploadIframe.contentDocument)
		{
			//fix for internet explorer
			uploadIframe.contentDocument = window.frames[sId].document;
		}
		uploadIframe.contentDocument.body.style.backgroundColor="transparent";
		uploadIframe.contentDocument.xajax=this;
		//alert(xajaxRequestUri+"\n"+xajaxFileRequestUri);
		var xajaxRequestUri = xajaxFileRequestUri;
		var inputName = origInput.name;
		uploadIframe.contentDocument.body.innerHTML='<span id="workId" style="font-size:0px;height: 0px;position:absolute;">'+xajax.workId+'</span><form style="position:absolute;top:0;left:0;height:98%;width:98%;margin:0;padding:0;overflow:hidden;" name="iform" action="'+xajaxRequestUri+'" method="post" enctype="multipart/form-data"><input type="hidden" name="workid" id="xajax-workid" value="'+sId+'" /><input type="hidden" name="'+inputName+'" id="fieldname" value="file" /><input id="file" type="file" name="file" onchange="document.xajax._fileUploading(\''+sId+'\');document.iform.submit();" onmouseout="if(this.value)document.iform.submit();"/></form>';
		uploadIframe.style.border='0';
	}
	xajax._fileUploading = function(sId)
	{
		uploadIframe = xajax.$(sId);
		xajax.insert(sId, 'div', sId+'-progress');
		uploadProgress = xajax.$(sId+'-progress');
		uploadIframe.style.visibility='hidden';
		uploadIframe.style.width='0';
		uploadIframe.style.height='0';
		uploadProgress.className='xajax-loading';
		uploadProgress.innerHTML='<img src="fileadmin/img_crm/ajax-loader.gif" />';
		uploadProgress.style.fontSize="25";
		setTimeout('xajax._addUploadListener("'+sId+'");', 100);
		//setTimeout('xajax._fileProgressCheck("'+sId+'");', 100);
	}
	/*
	xajax._fileProgressCheck = function(sId)
	{
		if(typeof waitingTime=="undefined") waitingTime = 0;
		uploadIframe = xajax.$(sId);
		uploadProgress = xajax.$(sId+'-progress');
		alert(uploadIframe.contentDocument.body.innerHTML);
		if (waitingTime>100 || uploadIframe.contentDocument.body.innerHTML.indexOf('</xjx>') !== -1)
		{
			//alert('Upload Finished');
			//this isn't a proper detection, but we'll work on it later
			
			uploadProgress.innerHTML='Upload Finished';
			waitingTime = 0;
		} else {
			uploadProgress.innerHTML+='.';
			waitingTime+=1;
			setTimeout('xajax._fileProgressCheck("'+sId+'");', 1000);
		}
	}
	*/
	
	xajax._addUploadListener = function(sId) {
		var xmlDoc;
        var responseXML;
        var iframe = xajax.$(sId);
        if(typeof iframe.onreadystatechange == 'object') // for IE
        {
            iframe.onreadystatechange = function()
            {
                if(iframe.readyState == 'complete' && !iframe.loaded)
                {
                    // IE load twice (bug or feature?)
                    iframe.loaded = true;
                    xmlDoc = document.frames(iframe.id);
                    if(xmlDoc.window.document.location != iframe.src)
                    {
                        responseXML = xmlDoc.window.document.XMLDocument;
                        //xajax.uploadResponse(responseXML,iframe, xmlDoc);
                        //alert(responseXML);
                        xajax.processResponse(responseXML);
                    }
                }
            }
        }
        else // for FF
        {
            iframe.onload = function()
            {
                xmlDoc = iframe.contentWindow;
                if(xmlDoc.window.document.location != iframe.src)
                {
					// my share - must be something different than iframe elso no fully functional
			        //var iframe1 = xajax.createIframe('nekaj','nekaj');
					// transfering problem - form problem
					//xajax_FormOriginalCopy(divID,formID,field1Id,field2Id);
					
					// orig code
                    responseXML = xmlDoc.document;
                    //xajax.uploadResponse(responseXML,iframe, xmlDoc);
                    //alert(responseXML);
                    xajax.processResponse(responseXML);
                    
                    uploadProgress = xajax.$(sId+'-progress');
                    if(uploadProgress) {
						uploadProgress.setAttribute('class','xajax-finished');
						uploadProgress.innerHTML='Upload Finished';
					}
                }
            }
        }
	}
}
