var limitQuots=0;
var flistCount=0;
var currentQuots=false;
var sendTimeOut=false;
//  getLoad('statistic');

function getLoad(mode, value)
{
	var method='';
	var req=null;
	if (typeof (mode)=='object')
	{
		var nodes=$('SpanUploadFile').childNodes;
		if (nodes.length==0) return;
		method='POST';
		req=new j2httpRequest_js;
	} else
	{
		method='GET';
		req=new j2httpRequest_js;
	}
	req.onreadystatechange = function()
	{
		if (req.readyState == 4)
		{
			var result=req.responseJS;
			if (result['flist']) setFList(result['flist']);
			setError(result['error']);
			setUList();
			if ($('SpanUploadFile').childNodes.length>0) resetTimer();
		}
	}
	req.open(method, '/upload/form_load.php', true);
	req.send({'mode': mode,'value': value});
}

function addChildSpan(node)
{
	$('SpanUploadFile').appendChild(node);
}

function delChildSpan(node)
{
	n=findSpan(node);
	if (n)
	{
		$('SpanUploadFile').removeChild(n);
		return true;
	}
	return false;
}

function findSpan(node)
{
	var find=null;
	var nodes=$('SpanUploadFile').childNodes;
	for (var i=0; i<nodes.length; i++)
	{
		if (typeof (node)=="string")
		{
			if (node.length==nodes[i].value.length||nodes[i].value.indexOf(node)+node.length==nodes[i].value.length)
			{
				return nodes[i];
			}
		} else
		{
			if (node.id==nodes[i].id && node.type==nodes[i].type)
			{
				return nodes[i];
			}
		}
	}
	return false;
}

function setFList(flist)
{
	var s='';
	var f=new Array(flist.length);
	if (flist.length)
	{
		i = 0;
		delChildSpan(flist[i].filename);
		f[i]=flist[i].filename;
		s+='<td><a href="/upload/get_tmp_file.php?name='+flist[i].filename+'">'+flist[i].filename+'</a></td>';
		s+='<TD>'+flist[i].mime+'</TD>';
		s+='<TD>('+flist[i].size+')</TD>';
		s+="<TD><INPUT class=FormInputButton type=button value=Remove onclick=\"getLoad('remove','"+flist[i].filename+"');\"></TD>";
		$('FileAttachmentsList').innerHTML=s;
		$('hiddenFileList').value=f;
		$('FileNotLoaded').style.display = 'none';
		$('FileLoaded').style.display = 'block';
	} else
	{
		$('FileNotLoaded').style.display = 'block';
		$('FileLoaded').style.display = 'none';
	}
}

function setUList()
{
	var s = '';
	var nodes=$('SpanUploadFile').childNodes;
	for (var k=nodes.length-1;k>=0; k--)
	{
		if (nodes[k].value.length==0)
		{
			delChildSpan(nodes[k]);
		}
	}
	for (var i=0; i<nodes.length; i++)
	{
		s = nodes[i].value;
	}
	$('SpanUploadFile').style.display="none";
	if (s)
	{
		if ($('UploadingFile').innerHTML!=s) $('UploadingFile').innerHTML=s;
		$('UploadingFile').style.display = 'block';
	} else
	{
		$('UploadingFile').style.display = 'none';
	}
}

function setError(errors)
{
	/*    var s='';
	var t=$('FileAttachmentsError');
	var d='';
	if (errors)
	{
	for (i=0;i<errors.length;i++)
	{
	if (errors[i][0].length) s+='<LI class="error">'+errors[i][0]+'</LI>';
	if (errors[i][1].length) delChildSpan(errors[i][1]);
	}
	}
	t.innerHTML=s;
	if (s.length==0) d='none';
	if (t.style.display!=d) t.style.display=d;*/
}

/*  function setQuots(quots)
{
limitQuots = quots[0].quota;
checkLimit(1);
checkLimit(2,quots[1].value, quots[1].quota,quots[1].message);
checkLimit(3,quots[2].value, quots[2].quota,quots[2].message);
}

function checkLimit(i,value,quots,message)
{
var t;
var cl;
if (i==1)
{
value=flistCount;
var nodes=$('SpanUploadFile').childNodes;
for (var k=0;k<nodes.length; k++)
{
if (nodes[k].value.length>0) value++;
}
quots=limitQuots;
message='Message attachments: '+value+' of '+quots;
cl='FileAttachementsQuota';
} else if (i==2)
{
cl='FileAttachmentSizeQuota';
} else
{
cl='FileStorageQuota';
}
t=$(cl);
t.innerHTML=message;
var part=value/quots;
var c='0';
if (part>0.9)
{
c='3';
} else if (part>0.5)
{
c='2';
}
if (t.className!=cl+c) t.className=cl+c;
$('FormUploadButtonID').disabled=part>=1;
$('FormUplinkButtonID').disabled=part>=1;
}*/

function nowSendUploadFile()
{
	getLoad($('main_form'));
	if (sendTimeOut) window.clearTimeout(sendTimeOut);
}

function sendUploadFile()
{
	if (sendTimeOut) window.clearTimeout(sendTimeOut);
	setUList();
	var nodes=$('SpanUploadFile').childNodes;
	var send=false;
	for (var i=0; i<nodes.length; i++)
	{
		nodes[i].style.display='none';
		if (nodes[i].value.length) send=true;
	}
	if (send) resetTimer();
	//    checkLimit(1);
}

function resetTimer(newInterval)
{
	if (!newInterval) newInterval=3000;
	if (sendTimeOut) window.clearTimeout(sendTimeOut);
	sendTimeOut=window.setTimeout(nowSendUploadFile, newInterval);
}

function hideUploadFile()
{
	this.style.display="none";
}

function selectUploadFile()
{
	var sAgent=navigator.userAgent.toLowerCase();
	if (sendTimeOut) window.clearTimeout(sendTimeOut);
	nodes=$('SpanUploadFile').childNodes;
	for (var k=0;k<nodes.length; k++)
	{
		if (nodes[k].type=='file')
		{
			if (nodes[k].style.display=='' && nodes[k].value.length==0)
			{
				nodes[k].focus();
				nodes[k].select();
				return;
			}
			if (nodes[k].style.display!='none') nodes[k].style.display=='none';
			k++;
		}
	}
	$('SpanUploadFile').style.display="block";
	var newInput=document.createElement('input');
	var id= 'FileUpload' + k;
	newInput.setAttribute('type', 'file');
	newInput.setAttribute('value', 'upload');
	newInput.setAttribute('id', id);
	newInput.setAttribute('name', id);
	newInput.setAttribute('size', '20');
	newInput.setAttribute('style', 'width: 90%; margin: 0; padding: 4px; border: 1px; background-color: #FFE7FF;');
	if (sAgent.indexOf('msie')!=-1)
	{
		newInput.setAttribute('onchange', sendUploadFile);
		newInput.setAttribute('onblur', hideUploadFile);
		addChildSpan(newInput);
		newInput.focus();
		newInput.select();
	} else if (sAgent.indexOf('opera')!=-1)
	{
		newInput.setAttribute('onchange', 'sendUploadFile()');
		newInput.setAttribute('onblur', 'hideUploadFile()');
		addChildSpan(newInput);
		newInput.focus();
		newInput.select();
	} else
	{
		newInput.setAttribute('onclick', 'sendUploadFile()');
		addChildSpan(newInput);
		newInput.focus();
		newInput.select();
	}
}

function selectUplinkFile()
{
	if (sendTimeOut) window.clearTimeout(sendTimeOut);
	var enterURL = prompt('Enter the complete URL for the hyperlink', 'http://');
	if (enterURL && (enterURL.toLowerCase()!='http://'||enterURL.toLowerCase()!='ftp://'))
	{
		nodes=$('SpanUploadFile').childNodes;
		for (var k=0;k<nodes.length; k++)
		{
			if (nodes[k].type=='hidden' && nodes[k].value==enterURL.toLowerCase()) return;
		}
		var newInput=document.createElement('input');
		newInput.setAttribute('type', 'hidden');
		newInput.setAttribute('value', enterURL);
		newInput.setAttribute('id', 'FileUplink' + nodes.length);
		newInput.setAttribute('name', 'FileUplink[]');
		addChildSpan(newInput);
		setUList();
	}
	resetTimer();
	checkLimit(1);
}

function $(text) {return document.getElementById(text);}