function Article()
{
	this.oAjaxThreads = new Array();
	this.loadingThreads = 0;
	this.temp = new Array();
	this.ARTICLEID_ZNET = 1;
	this.ARTICLEID_ZMAG = 2;
}


Article.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];
}

Article.prototype.loadAll = function(id)
{
	var oAjax =  this.getNextThread();
	oAjax.url = '/admin/article/loadAll'
	oAjax.data = 'id='+id;
	oAjax.method = 'post';
	oAjax.addRequestListener(this, this.allLoaded, false);
	oAjax.open();
}

Article.prototype.allLoaded = function(xmlDocument)
{
	document.getElementById("account_view").style.display= "";
	
	var oDOMHelper = new DOMHelper();
	oDOMHelper.removeChilds(document.getElementById('account_view'));
	
	var oParser = new XMLParser(xmlDocument);
	var oArticles = oParser.getNodeValue('article');
	
	if(oArticles)
	{
		var oTable = document.createElement('table');
		oTable.setAttribute('border', 1);
		oTable.setAttribute('width', '100%');
		document.getElementById('account_view').appendChild(oTable);
		
		var oTBody = document.createElement('tbody');
		oTable.appendChild(oTBody);
		
		var oTr = document.createElement('tr');
		oTBody.appendChild(oTr);
		
		var oTd = document.createElement('td');		
		oTd.setAttribute('colspan',"15");
		var a = document.createElement("a");
		a.setAttribute('href', "javascript:void(0);");
		a.setAttribute('onclick', "document.getElementById('account_view').style.display='none';");
		a.innerHTML = '[X]';
		oTd.appendChild(a);
		
		oTr.appendChild(oTd);
		
		var oTr = document.createElement('tr');
		oTBody.appendChild(oTr);
		
		var oTd = document.createElement('td');
		oTd.innerHTML = 'Id';
		oTr.appendChild(oTd);
		
		var oTd = document.createElement('td');
		oTd.innerHTML = 'Account';
		oTr.appendChild(oTd);
		
		var oTd = document.createElement('td');
		oTd.innerHTML = 'Created On';
		oTr.appendChild(oTd);
		
		var oTd = document.createElement('td');
		oTd.innerHTML = 'Modified On';
		oTr.appendChild(oTd);
		
		var oTd = document.createElement('td');
		oTd.innerHTML = 'title';
		oTr.appendChild(oTd);
		
		var oTd = document.createElement('td');
		oTd.innerHTML = 'Short Title';
		oTr.appendChild(oTd);
		
		var oTd = document.createElement('td');
		oTd.innerHTML = 'Subtitle';
		oTr.appendChild(oTd);
		
		var oTd = document.createElement('td');
		oTd.innerHTML = 'Description';
		oTr.appendChild(oTd);
		
		var oTd = document.createElement('td');
		oTd.innerHTML = 'Issue Date';
		oTr.appendChild(oTd);
		
		var oTd = document.createElement('td');
		oTd.innerHTML = 'Number';
		oTr.appendChild(oTd);
		
		var oTd = document.createElement('td');
		oTd.innerHTML = 'First Name';
		oTr.appendChild(oTd);
		
		var oTd = document.createElement('td');
		oTd.innerHTML = 'Last Name';
		oTr.appendChild(oTd);
		
		var oTd = document.createElement('td');
		oTd.innerHTML = 'Show Reverse';
		oTr.appendChild(oTd);
		
		var oTd = document.createElement('td');
		oTd.innerHTML = 'Body';
		oTr.appendChild(oTd);
		
		var oTd = document.createElement('td');
		oTd.innerHTML = 'Actions';
		oTr.appendChild(oTd);
		
		for(var i = 0; i < oArticles.length; i++)
		{
			var oArticleParser = new XMLParser(oArticles[i]);
			
			var id = oArticleParser.getNodeValue('id');
			var ownerId = oArticleParser.getNodeValue('ownerId');
			var ownerType = oArticleParser.getNodeValue('ownerType');
			
			if (ownerType == '1')
			{
				ownerType = 'Uso pr&oacute;prio';
			}
			else
			{
				ownerType = 'Uso global';
			}
	
			var id = oArticleParser.getNodeValue('id');
			var accountId = oArticleParser.getNodeValue('accountId');
			var createdOn = oArticleParser.getNodeValue('createdOn');
			var modifiedOn = oArticleParser.getNodeValue('modifiedOn');
			var title = oArticleParser.getNodeValue('title');
			var shortTitle = oArticleParser.getNodeValue('shortTitle');
			var subtitle = oArticleParser.getNodeValue('subtitle');
			var description = oArticleParser.getNodeValue('description');
			var issueDate = oArticleParser.getNodeValue('issueDate');
			var number = oArticleParser.getNodeValue('number');
			var firstName = oArticleParser.getNodeValue('firstName');
			var lastName = oArticleParser.getNodeValue('lastName');
			var showReverse = oArticleParser.getNodeValue('showReverse');
			var body = oArticleParser.getNodeValue('body');
				
			var oTr = document.createElement('tr');
			oTr.setAttribute('id', id);
			oTBody.appendChild(oTr);
			
			var oTd = document.createElement('td');
			oTd.innerHTML = id;
			oTr.appendChild(oTd);
			
			var oTd = document.createElement('td');
			oTd.innerHTML = accountId;
			oTr.appendChild(oTd);
			
			var oTd = document.createElement('td');
			oTd.innerHTML = createdOn;
			oTr.appendChild(oTd);
			
			var oTd = document.createElement('td');
			oTd.innerHTML = modifiedOn;
			oTr.appendChild(oTd);
			
			var oTd = document.createElement('td');
			oTd.innerHTML = title;
			oTr.appendChild(oTd);
			
			var oTd = document.createElement('td');
			oTd.innerHTML = shortTitle;
			oTr.appendChild(oTd);
			
			var oTd = document.createElement('td');
			oTd.innerHTML = subtitle;
			oTr.appendChild(oTd);
			
			var oTd = document.createElement('td');
			oTd.innerHTML = description;
			oTr.appendChild(oTd);
			
			var oTd = document.createElement('td');
			oTd.innerHTML = issueDate;
			oTr.appendChild(oTd);
			
			var oTd = document.createElement('td');
			oTd.innerHTML = number;
			oTr.appendChild(oTd);
			
			var oTd = document.createElement('td');
			oTd.innerHTML = firstName;
			oTr.appendChild(oTd);
			
			var oTd = document.createElement('td');
			oTd.innerHTML = lastName;
			oTr.appendChild(oTd);
			
			var oTd = document.createElement('td');
			oTd.innerHTML = showReverse;
			oTr.appendChild(oTd);
			
			var oTd = document.createElement('td');
			oTd.innerHTML = body;
			oTr.appendChild(oTd);
			
			var oTd = document.createElement('td');
			oTr.appendChild(oTd);
			
			var delLink = document.createElement('a');
			delLink.setAttribute('href', 'javascript:void(0);');
			var me = this;
			delLink.setAttribute('id', id);
			delLink.onclick = function () { me.deleteArticle(this) };
			delLink.innerHTML = '[D]';
			oTd.appendChild(delLink);
			
			/*
			var editLink = document.createElement('a');
			editLink.setAttribute('href', 'javascript:void(0);');
			var me = this;
			editLink.setAttribute('id', id);
			editLink.onclick = function () { me.newForm(this) };
			editLink.innerHTML = '[E]';
			oTd.appendChild(editLink);
			*/ 
		}
	}
	else
	{
		document.getElementById("account_view").style.display= "none";
		alert("Not found")
	}
}

Article.prototype.deleteArticle = function(delLink)
{
	var oAjax =  this.getNextThread();
	
	var id = delLink.getAttribute('id');
	oAjax.url = '/admin/article/delete'
	oAjax.data = 'id='+id;

	oAjax.method = 'post';
	oAjax.addRequestListener(this, this.articleDeleted, false);
	oAjax.open();
}

Article.prototype.articleDeleted = function(xmlDocument)
{
	alert("Articles successfully removed.");
	var oParser = new XMLParser(xmlDocument);
    var id = oParser.getNodeValue('id');
    
	var oTr = document.getElementById(id);
	oTr.style.display = 'none';
}

Article.prototype.view = function(id)
{
	var oAjax =  this.getNextThread();
	oAjax.url = '/zmag/viewArticle'
	oAjax.data = 'id='+id;
	oAjax.method = 'post';
	oAjax.addRequestListener(this, this.viewFinish, false);
	oAjax.open();	
}

Article.prototype.viewFinish = function(xmlDocument)
{
	var oAjax = this.getNextThread();
	
	var closeButton = document.createElement("a");
	closeButton.setAttribute("href", "javascript: void(0);");
	closeButton.setAttribute("onclick", "document.getElementById(\"viewArticle\").style.display = 'none';");
	closeButton.appendChild(document.createTextNode("Close"));
	
	var viewArticle = document.getElementById("viewArticle");
	
	//current scroll
	if(navigator.appName.indexOf("Netscape") != -1)
	{
		var topPage = new Number(pageYOffset);
	}
	else if(document.all) 
	{
		var topPage = new Number(document.documentElement.scrollTop);
	}
	if(topPage==0)
	{
		topPage = 200;
	}
	topPage = topPage+30;
	
	viewArticle.style.top = topPage+"px";
	viewArticle.innerHTML = '';
	viewArticle.appendChild(closeButton);
	viewArticle.appendChild(document.createElement("br"));
	viewArticle.innerHTML += oAjax.xmlHttpRequest.responseText;
	viewArticle.appendChild(document.createElement("br"));
	viewArticle.appendChild(closeButton);
	viewArticle.style.display = '';
	
	/* with XML , doesnt works the html :(
	var oParser = new XMLParser(xmlDocument);
	
	var closeButton = document.createElement("a");
	closeButton.setAttribute("href", "javascript: void(0);");
	closeButton.setAttribute("onclick", "document.getElementById(\"viewArticle\").style.display = 'none';");
	closeButton.appendChild(document.createTextNode("Close"));
	viewArticle.appendChild(closeButton);
	viewArticle.innerHTML += oParser.getNodeValue("body");
	
	viewArticle.style.display = '';*/
	
}

Article.prototype.addComment = function()
{
	var bodyCommentDiv = document.getElementById("bodyCommentDiv");
	oDOMHelper.removeChilds(bodyCommentDiv);
	
	var textarea = document.createElement("textarea");
	textarea.setAttribute("id", "bodyComment");
	textarea.setAttribute("name", "bodyComment");
	textarea.setAttribute("value", "");
	bodyCommentDiv.appendChild(textarea);
	
	//<textarea id="bodyComment" name="bodyComment" value=""></textarea>
	
	oDOMHelper.toggleObjDisplayById('newComment');
	oUtils.createFCK('bodyComment', 520, 300, 'Default');
}

Article.prototype.addCommentNotLogged = function()
{
	oDOMHelper.toggleObjDisplayById('newCommentNotLogged');
}

Article.prototype.zmagFeatures = function(articleTypeId)
{
	switch(articleTypeId) 
	{
		case this.ARTICLEID_ZNET+"":
			document.getElementById("trZmagFlags").style.display = "none";
			document.getElementById("trZmagFreeOrder").style.display = "none";
			
			document.getElementById("trIssueId").style.display = "none";
			document.getElementById("trIssueSectionId").style.display = "none";
			
			break;
			
		case this.ARTICLEID_ZMAG+"":
			document.getElementById("trZmagFlags").style.display = "";
			document.getElementById("trZmagFreeOrder").style.display = "";
			
			document.getElementById("trIssueId").style.display = "";
			document.getElementById("trIssueSectionId").style.display = "";
			
			break;
		
		default:
			document.getElementById("trZmagFlags").style.display = "none";
			document.getElementById("trZmagFreeOrder").style.display = "none";
			break;
	}
}

Article.prototype.setIsBookInterview = function(status,id)
{
	if(status)
	{
		var isBookInterviewStatus = "1";
	}
	else
	{
		var isBookInterviewStatus = "0";
	}
	var oAjax =  this.getNextThread();
	oAjax.url = '/admin/article/setIsBookInterview';
	oAjax.data = 'isBookInterview='+isBookInterviewStatus+'&id='+id;
	oAjax.method = 'post';
	oAjax.addRequestListener(this, this.setIsBookInterviewFinished, false);
	oAjax.open();
}

Article.prototype.setIsBookInterviewFinished = function(xmlDocument, text)
{
	alert("Article updated.");
}

Article.prototype.validateSubmission = function() 
{
	var msg = '';
	var error = false;
	var free = false;

	if(document.getElementById('accountId'))
	{
		if(document.getElementById('accountId').value == '')
		{
			msg += 'You have not filled field author'+'\n';
			error = true;
		}
	}
	
	if(document.getElementById('articleTypeId'))
	{
		if(document.getElementById('articleTypeId').value == '')
		{
			msg += 'You have not filled field Article Type'+'\n';
			error = true;
		} else if (document.getElementById('articleTypeId').value == "2") {//zmag
			if(document.getElementById("confirmIssueDate"))
			{
				if(document.getElementById("confirmIssueDate").value != 1)
				{
					msg += 'Issue don\'t exists for issue date '+'\n';
					error = true;
				}
			}
		}
	}
	
	
	if(document.getElementById('title').value == '')
	{
		msg += 'You have not filled field title'+'\n';
		error = true;
	}
	
	if(document.getElementById('shortTitle').value == '')
	{
		msg += 'You have not filled field short title'+'\n';
		error = true;
	}
	
	if(document.getElementById('description').value == '')
	{
		msg += 'You have not filled field description'+'\n';
		error = true;
	}
	
	if(document.getElementById('firstName'))
	{
		if(document.getElementById('firstName').value == '')
		{		
			msg += 'You have not filled field first name'+'\n';
			error = true;
		}
		if(document.getElementById('lastName').value == '')
		{		
			msg += 'You have not filled field last name'+'\n';
			error = true;
		}
	}
	
	if(navigator.appName!='Netscape')
	{
		if(FCKeditorAPI.GetInstance( 'body' ).GetHTML() == '')
		{
			msg += 'You have not filled Body Content'+'\n';
			error = true;
		}
	}

	if(error)
	{
		alert(msg);
	}
	else
	{
		oCategorySelector.save('article');
	}
}

Article.prototype.validateArticleType = function() 
{
	var msg = '';
	var error = false;
	var free = false;

	if(document.getElementById('name').value == '')
	{
		msg += 'You have not filled field name'+'\n';
		error = true;
	}
	
	if(error)
	{
		alert(msg);
	}
	else
	{
		document.getElementById('articleType').submit();
	}
}

Article.prototype.validateSubmissionPreference = function()
{
	var msg = '';
	var error = false;

	if(document.getElementById('articleText'))
	{
		if(document.getElementById('articleText').value == '')
		{
			msg += 'You have not filled field Article'+'\n';
			error = true;
		}
	}
	
	if(document.getElementById('accountText'))
	{
		if(document.getElementById('accountText').value == '')
		{
			msg += 'You have not filled field Account'+'\n';
			error = true;
		}
	}	
	
	if(error)
	{
		alert(msg);
	}
	else
	{
		document.getElementById('ArticlePreference').submit();
	}
}	

Article.prototype.loadIssueSections = function(object, articleId)
{
	var oAjax =  this.getNextThread();
	oAjax.url = '/admin/issueSection/getAllAjax';
	oAjax.data = 'issueId='+object.value+"&articleId="+articleId;
	oAjax.method = 'post';
	oAjax.addRequestListener(this, this.loadIssueSectionsFinish, false);
	oAjax.open();
}

Article.prototype.loadIssueSectionsFinish = function(data, txt)
{
	document.getElementById("issueSections").innerHTML = txt;
}

Article.prototype.verifyIssueExists = function(object)
{
	document.getElementById("confirmIssueDate").value = "0";
	var oAjax =  this.getNextThread();
	oAjax.url = '/admin/issue/issueExists';
	oAjax.data = 'issueDate='+object.value;
	oAjax.method = 'post';
	oAjax.addRequestListener(this, this.loadVerifyIssueExistsFinish, false);
	oAjax.open();
}

Article.prototype.loadVerifyIssueExistsFinish = function(data, txt)
{
	if(txt == "1")
	{
		document.getElementById("confirmIssueDate").value = "1";
	} else {
		document.getElementById("confirmIssueDate").value = "0";
	}
}

var oArticle = new Article();