function Scrap()
{
	this.oAjaxThreads = new Array();
	this.loadingThreads = 0;
	this.temp = new Array();
}

Scrap.prototype.getNextThread = function()
{
	var i = 0;
	
	if (this.oAjaxThreads.length == 0)
	{
		var oAjax = new AJAX();
		this.oAjaxThreads.push(oAjax);		
	}

	while (this.oAjaxThreads[i] && this.oAjaxThreads[i].working && i < this.oAjaxThreads.length )
	{
		i++;
	}
	
	if (!this.oAjaxThreads[i])
	{
		var oAjax = new AJAX();
		this.oAjaxThreads.push(oAjax);		
	}
	
	return this.oAjaxThreads[i];
}

Scrap.prototype.replyScrap = function(scrapId)
{
	var trid = 'scraptr_'+scrapId;
	var tdid = 'scraptd_'+scrapId;

	document.getElementById(tdid).innerHTML = '<form method="post" action="/zspace/sendreply/'+scrapId+'" id="replyform_'+scrapId+'"><input type="hidden" name="replyScrap" value="1"><textarea name="content" cols="70" rows="4"></textarea><br/><input type="button" onclick="javascript:oScrap.remoteReply('+scrapId+')" value="Reply"></form>';
	document.getElementById(trid).style.display='';
}

Scrap.prototype.deleteScrap = function(scrapId)
{
	if(confirm('Are you sure you want to delete this message?'))
	{
		var id = 'scrapsBox_'+scrapId;
		document.getElementById(id).style.display = 'none';
		this.remoteDelete(scrapId);
	}
}

Scrap.prototype.remoteDelete = function(scrapId)
{
	var oAjax =  this.getNextThread();
	oAjax.url = '/zspace/deletescrap/'+scrapId;
	oAjax.method = 'post';
	oAjax.data = '';
	oAjax.addRequestListener(this, this.scrapRemoved, false);
	oAjax.open();	
}

Scrap.prototype.scrapRemoved = function(xmlDocument, text)
{
	alert(text);
}

Scrap.prototype.remoteReply = function(scrapId)
{
	var formId = 'replyform_'+scrapId;
	var formObj = document.getElementById(formId);
	var oAjax =  this.getNextThread();
	oAjax.addRequestListener(this, this.replySent, false);
	oAjax.submit(formObj);
}

Scrap.prototype.remoteGroup = function()
{
	var formId = 'sendScrapGroup';
	var formObj = document.getElementById(formId);
	var oAjax =  this.getNextThread();
	oAjax.addRequestListener(this, this.groupSend, false);
	oAjax.submit(formObj);
}

Scrap.prototype.groupSend = function(xmlDocument, text)
{
	var scrapId = text;
	//var trid = 'scraptr_'+scrapId;
	//var tdid = 'scraptd_'+scrapId;
	
	//document.getElementById(tdid).innerHTML = '';
	//document.getElementById(trid).style.display='none';
	alert('Your message was successfully sent.');
	window.open(window.location.href, "_self");
}

Scrap.prototype.replySent = function(xmlDocument, text)
{
	var scrapId = text;
	var trid = 'scraptr_'+scrapId;
	var tdid = 'scraptd_'+scrapId;
	
	document.getElementById(tdid).innerHTML = '';
	document.getElementById(trid).style.display='none';
	alert('Your message was successfully sent.');
}

Scrap.prototype.openWindow = function()
{
	document.getElementById('scrapWindow').style.display = '';
}

Scrap.prototype.closeWindow = function()
{
	document.getElementById('scrapForm').reset();
	document.getElementById('scrapWindow').style.display = 'none';
}

Scrap.prototype.sendScrap = function()
{
	var formId = 'scrapForm';
	var formObj = document.getElementById(formId);
	var oAjax =  this.getNextThread();
	oAjax.addRequestListener(this, this.scrapSent, false);
	oAjax.submit(formObj);
}

Scrap.prototype.scrapSent = function(xmlDocument, text)
{
	alert('Your message was successfully sent');
	this.closeWindow();
	window.location.reload();
}

Scrap.prototype.reply = function(obj)
{
	var reply = document.getElementById('reply'+obj);
	
	if(reply.style.display == "")
	{
		reply.style.display = "none";
		var oDOMHelper = new DOMHelper();
		oDOMHelper.removeChilds(reply);
	}
	else
	{
		reply.style.display = "";
		
		var valueGroupId = document.getElementById('replyGroupId').value;
		var valueUserId = document.getElementById('replyUserId').value;
		
		var oTd = document.createElement('td');
		reply.appendChild(oTd);
		
		var oTd = document.createElement('td');
		reply.appendChild(oTd);
					
		var oForm = document.createElement('form');
		oForm.setAttribute('action', "/zspace/sendMailGroup");
		oForm.setAttribute('method', 'post');
		oTd.appendChild(oForm);
		
		var input = document.createElement('input');
		input.setAttribute('type', "hidden");
		input.setAttribute('name', "groupId");
		input.setAttribute('id', "groupId");
		input.setAttribute('value', valueGroupId);
		oForm.appendChild(input);
		
		var input = document.createElement('input');
		input.setAttribute('type', "hidden");
		input.setAttribute('name', "userId");
		input.setAttribute('id', "userId");
		input.setAttribute('value', valueUserId);
		oForm.appendChild(input);
		
		var input = document.createElement('input');
		input.setAttribute('type', "hidden");
		input.setAttribute('name', "groupMailId");
		input.setAttribute('id', "groupMailId");
		input.setAttribute('value', obj);
		oForm.appendChild(input);
		
		var oTable = document.createElement('table');
		oTable.setAttribute('border', 0);
		oForm.appendChild(oTable);
		
		var oTr = document.createElement('tr');
		oTable.appendChild(oTr);
		
		var oTd = document.createElement('td');
		oTr.appendChild(oTd);
		
		var oB = document.createElement('b');
		oB.innerHTML = "Name:";
		oTd.appendChild(oB);
		
		var oTd = document.createElement('td');
		oTr.appendChild(oTd);
		
		var input = document.createElement('input');
		input.setAttribute('type', "text");
		input.setAttribute('name', "nameForMail");
		input.setAttribute('id', "nameForMail");
		input.setAttribute('size', '35');
		oTd.appendChild(input);
		
		var oTr = document.createElement('tr');
		oTable.appendChild(oTr);
		
		var oTd = document.createElement('td');
		oTr.appendChild(oTd);
		
		var oB = document.createElement('b');
		oB.innerHTML = "Subject:";
		oTd.appendChild(oB);
		
		var oTd = document.createElement('td');
		oTr.appendChild(oTd);
		
		var input = document.createElement('input');
		input.setAttribute('type', "text");
		input.setAttribute('name', "subject");
		input.setAttribute('id', "subject");
		input.setAttribute('size', '35');
		oTd.appendChild(input);
		
		var oTr = document.createElement('tr');
		oTable.appendChild(oTr);
		
		var oTd = document.createElement('td');
		oTd.setAttribute('colspan', '2');
		oTr.appendChild(oTd);
		
		var oB = document.createElement('b');
		oB.innerHTML = "Body:";
		oTd.appendChild(oB);
			
		var oBr = document.createElement('br');
		oTd.appendChild(oBr);
		
		var textarea = document.createElement('textarea');
		textarea.setAttribute('name', "body");
		textarea.setAttribute('id', "body");
		textarea.setAttribute('rowls', '30');
		textarea.setAttribute('cols', '80');
		oTd.appendChild(textarea);
		
		var oTr = document.createElement('tr');
		oTable.appendChild(oTr);
		
		var oTd = document.createElement('td');
		oTr.appendChild(oTd);
		
		var input = document.createElement('input');
		input.setAttribute('type', "submit");
		input.setAttribute('value', 'Send Reply');
		oTd.appendChild(input);
	}
}

var oScrap = new Scrap();