// JavaScript Document

function CreateAjaxRequest()
{
	var xh;
	if (window.XMLHttpRequest)
		xh = new window.XMLHttpRequest();
	else
		xh = new ActiveXObject("Microsoft.XMLHTTP");
	
	xh.open("POST", handlerurl, false, null, null);
	xh.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
	return xh;
}

function CuteWebUI_AjaxUploader_OnPostback()
{
	var uploader = document.getElementById("multiuploader");
	var guidlist = uploader.value;
	var processedlist = document.getElementById("processedlist").value;

	//Send Request
	var xh=CreateAjaxRequest();
	xh.send("guidlist=" + guidlist+"&processedlist="+processedlist);

	//call uploader to clear the client state
	uploader.reset();

	if (xh.status != 200)
	{
		alert("http error " + xh.status);
		setTimeout(function() { document.write(xh.responseText); }, 10);
		return;
	}

	var filelist = document.getElementById("filelist");
	var list = eval(xh.responseText); //get JSON objects
	
	// processed list
	document.getElementById("processedlist").value = list[0].GuidList;
	
	//Process Result:
	for (var i = 1; i < list.length; i++)
	{
		var item = list[i];
		
		var file_size = parseInt(list[i].FileSize);
		var measure_unit = '';
		
		if (file_size >= 10240)
		{
			file_size = parseInt(list[i].FileSize) / (1024 * 1024);
			measure_unit = 'MB';
		}
		else
		{
			file_size = parseInt(list[i].FileSize) / 1024;
			measure_unit = 'KB';
		}
		
		var msg = "Uploaded file: " + list[i].FileName + ' | Size: ' + (Math.round( file_size * 100 ) / 100 ).toString() + ' ' + measure_unit + ' | <a href="javascript:void(0)" onclick="Attachment_Remove(this)">remove</a>' ;
		var li = document.createElement("li");
		li.setAttribute("fileguid",list[i].FileGuid);
		li.setAttribute("filename",list[i].FileName);
		li.innerHTML = msg;
		filelist.appendChild(li);
	}
}

function CuteWebUI_AjaxUploader_OnBrowse()
{
	var files_selected=0;

	var guidArray=document.getElementById("processedlist").value.split("/");
	
	if ((guidArray.length==1)&&(guidArray[0]==''))
	{
		guidArray.splice(0,1);
	}
	
	files_selected=guidArray.length;
	
	if (mode_contact_form==false)
	{
		var already_attached=document.getElementById("att_amts").value;
		
		if (already_attached!=null)
		{
			files_selected=files_selected+parseInt(already_attached);
		}
	}

	//alert(files_selected+'>='+max_files_allowed);

	if (files_selected>=max_files_allowed)
	{
		alert('The maximum number of files allowed to be uploaded is set to '+max_files_allowed+'.');
		return false;
	}
	else
	{
		return true;
	}
}

function CuteWebUI_AjaxUploader_OnSelect(files)
{
	for (var i=0;i<files.length;i++)
	{
		if ((files[i].FileName.indexOf("_x27_") > -1) || (files[i].FileName.indexOf("'") > -1))
		{
			alert("Name(s) of selected file(s) contain forbidden symbols (e.g. single quotes).\nPlease, rename file(s) and try to upload again.");
			return false;
		}
	}
	
	var guidArray=document.getElementById("processedlist").value.split("/");
	
	if ((guidArray.length==1)&&(guidArray[0]==''))
	{
		guidArray.splice(0,1);
	}

	var files_selected=parseInt(guidArray.length)+parseInt(files.length);
	
	if (mode_contact_form==false)
	{
		var already_attached=document.getElementById("att_amts").value;
		
		if (already_attached!=null)
		{
			files_selected=files_selected+parseInt(already_attached);
		}
	}

	//alert(files_selected+' > '+max_files_allowed);
	
	if (files_selected>max_files_allowed)
	{
		alert('The maximum number of files allowed to be uploaded is set to '+max_files_allowed+'.');
		return false;
	}
	else
	{
		return true;
	}
}

function Attachment_FindItem(element)
{
	while(true)
	{
		if(element.nodeName=="LI")
			return element;
		element=element.parentNode;
	}
}

function Attachment_Remove(link)
{
	var list_item=Attachment_FindItem(link);
	
	if(!confirm("Are you sure you want to delete '"+list_item.getAttribute("filename")+"'?"))
		return;
	
	var guid=list_item.getAttribute("fileguid");
	
	var xh=CreateAjaxRequest();
	xh.send("delete=" + guid);

	//alert(xh.responseText);
	
	if ((xh.status==200) && (xh.responseText=="OK"))
	{
		var file_list=document.getElementById("filelist");
		file_list.removeChild(list_item);

		var fileArray=document.getElementById("processedlist").value.split("/");
		
		for(var i=0;i<fileArray.length;i++)
		{
			if(fileArray[i]==guid)
			{
				fileArray.splice(i,1);
				break;
			}
		}
		
		document.getElementById("processedlist").value=fileArray.join("/");
	}
}
