
// Citation Class
var CCitation = function(creator, year, title, JournalTitle, JournalVolume, JournalPage, description, identifier, query )
{
	// Properties
			this.title					 = title;
			this.creator			 = creator;
			this.year				 = year;
			this.description		 = description;
			this.JournalTitle		 = JournalTitle;
			this.JournalVolume = JournalVolume;
			this.JournalPage	 = JournalPage;
			this.identifier			 = identifier;
			this.query				 = query;

			this.saved				= "";
			this.checked			= false;

			this.property				=	new Array();
			this.value					=  new Array();

			//this.Config			= new Object() ;
			// Events
			//this.OnError		= null ;	// function( source, errorNumber, errorDescription )
}


CCitation.prototype.Size= function()
{
return this.property.length;
}

CCitation.prototype.Add = function(name,val)
{
this.property[this.property.length] = name;
this.value[this.value.length] = val;
}

CCitation.prototype.ConvertSelf = function()
{
	if (this.title	!=null) this.Add("title",this.title	);
	if (this.creator!=null) this.Add("creator",this.creator);
	if (this.year!=null) this.Add("year",this.year);
	if (this.description!=null) this.Add("description",this.description);
	if (this.JournalTitle	!=null) this.Add("JournalTitle",this.JournalTitle	);
	if (this.JournalVolume!=null) this.Add("JournalVolume",this.JournalVolume);
	if (this.identifier!=null) this.Add("identifier",this.identifier);
	if (this.query!=null) this.Add("query",this.query);
}


CCitation.prototype.IsThere = function(name)
{
var i=0; 
	while ((i<this.property.length) && (this.property[i].toUpperCase()!=name.toUpperCase()))
	{
		i++;
	}
if (i==this.property.length) return false; else return true;

}

CCitation.prototype.GetId = function(name)
{
var i=0; 
//alert("searching for >>>> " + name);
	while ((i<this.property.length) && (this.property[i].toUpperCase()!=name.toUpperCase()))
	{
		i++;
	}

//alert(" ind = "+i.toString());

if (i==this.property.length) return -1; else return (i);

}

CCitation.prototype.Print = function()
{
//alert(format);
//this.s = " ":
var sss="";
	for (var i=0; i<this.property.length; i++)
		{
		sss = sss + " "+ this.property[i] + "= <strong>" + this.value[i] + "</strong> \n  ";
		//alert("printin "+i.toString());
		//alert(this.property[i] + "=" + this.value[i] );
		}
	return sss;
}


//////////////////////////////////
// Array of CCitations
//////////////////////////////////


var CCs = function(limit)
{
	this.cites = new Array();
	this.current = -1;
	this.limit = limit;
//	this.cmpAttr = 'identifier';
	this.cmpAttr = 'title';

}

CCs.prototype.Create= function()
{
	var cite1 = new CCitation() ;
	this.cites[this.current] =cite1; 
}


CCs.prototype.Add= function(name,val)
{
	this.cites[this.current].Add(name,val);
}

CCs.prototype.IsThere = function(name)
{
	return this.cites[this.current].IsThere(name);
}

CCs.prototype.Clear= function()
{
	this.cites = new Array();
	this.current = -1;
	return true;
}

CCs.prototype.Size= function()
{
//	return (this.current+1);
	return this.cites.length;
}

CCs.prototype.Next= function()
{
this.current++;

if (this.current>=this.limit) 
	{	
//		alert(this.current);
	this.current --;
	return false;
	}
		else 	return true;
}


CCs.prototype.Remove= function()
{
	this.cites.splice(this.current,1);
	if (this.current >= 0) this.current --;
	return true;
}


CCs.prototype.isFull= function()
{
if (this.current==(this.limit-1)) return true;
	else	return false;
}


CCs.prototype.Last = function()
{
	if (this.cites.length>0) this.current = this.cites.length - 1;
		else this.current = 0;
	return true;
}

CCs.prototype.First= function()
{
	this.current=0;
}

CCs.prototype.MoveUp = function() //not workin;  i guess it works now :)
{

	pos = -1;
	for (var i=0; i<(this.cites.length-1) && i<(this.limit-1) ; i++)
		{
			if (!this.cites[i].checked && !this.cites[i+1].checked) this.cites[i] = this.cites[i+1]; 
			else
			{
				if (this.cites[i].checked && !this.cites[i+1].checked && pos!=-1) this.cites[pos] = this.cites[i+1]; 
				else
				if (!this.cites[i].checked && this.cites[i+1].checked) pos = i; 
			} //else
		}//for

//	delete this.cites[i];
	this.cites.splice(i,1);

	if (this.current > 0) this.current --;
	return true;
}

CCs.prototype.MoveUp2 = function()
{
	//don't save checked items
	//***
	this.cites.splice(0,1)
	if (this.current > 0) this.current --;
	return true;
}


CCs.prototype.Capitalize = function(name)
{
//got from http://psacake.com/web/jc.asp
newVal = '';
name = name.split(' ');
for(var c=0; c < name.length; c++) {
	newVal += name[c].substring(0,1).toUpperCase() +
	name[c].substring(1,name[c].length) + ' ';
        }
return newVal;
}

CCs.prototype.getValue = function(ind,name)
{
//	ind--;
	if (ind<this.Size())
	{
//	alert(ind);
	var k = this.cites[ind].GetId(name);
	if (k>-1) return this.Capitalize(this.cites[ind].value[k]);
	}
return "?";
}


CCs.prototype.isChecked = function(ind)
{
//	ind--;
	if (ind<this.Size())
	{
	return this.cites[ind].checked;
	}
return false;
}

CCs.prototype.ChangeStatus = function(ind)
{
//	ind--;
	if (ind<this.Size())
	{
	this.cites[ind].checked = !this.cites[ind].checked;
	return true;
	}
return false;
}


CCs.prototype.PrintAll = function()
{
  var sss="";
	for (var i=0; i<this.cites.length; i++)
		{
		//	alert(this.cites[i].Print());
		sss = sss + "<br>Record#" + i+ "\n"+ this.cites[i].Print() ;
		}

	return sss;
}



CCs.prototype.URLIsExist = function(attrname, attrval)
{
	//alert(attrname + " = " +attrval);
	for (var i=0; i<(this.cites.length-1); i++)
		{
			if (this.cites[i].IsThere(attrname))
			{
					if (trim(this.cites[i].value[ this.cites[i].GetId(attrname) ].toUpperCase()) == trim(attrval.toUpperCase()))
					{
						return true;
					}
			}

		}
	return false;
}


///////////////////////////////////////////
//import XML file 
///////////////////////////////////////////
CCs.prototype.importXML = function(xmlObj, sourceurl)
{
//	for (var i=0; i<this.cites.length; i++)			alert(this.cites[i].Print());

	//addDebMsg('Recieved respond from server.');
//	if (xmlObj == null) return false;
//	if (!("status" in  xmlObj)) return false;

	var status = "";
	try{
	  status = xmlObj.status;  //yourXHRVariable.statusText;
		}
	catch(e){
	  status = "Trouble accessing it";
	  return false;
	}

//9/16/2006
//	addDebMsg('importXML  readyState:'+xmlObj.readyState+';   status='+status);

    if(xmlObj.readyState == 4){
		if (xmlObj.status == 200) {

		//9/15/2006
		var DLindex = findDLindex(sourceurl);
		if (DLindex>-1) dls_readystatus[DLindex] = true;

		var xmldoc = xmlObj.responseXML;
		var root = xmldoc.getElementsByTagName('record');
		
//		alert(root.length);

//		var root2 = xmldoc.getElementsByTagName('source');
//		var root3 = xmldoc.getElementsByTagName('keywords');

		if (root.length>0) 	
			for (var i=0; i<root.length; i++) 
			{

			if (this.Next()) //if buffer is not full
				{ 
				this.Create();
				
				for (j=0;j<root[i].childNodes.length;j++)
					{					

//						alert(' typeNode='+root[i].childNodes[j].nodeType);
//						alert(' j='+j+' root[i].childNodes.length='+root[i].childNodes.length);

						if (root[i].childNodes[j].nodeType != 1) continue;

//						alert(' nodeData='+root[i].childNodes[j].firstChild.data);

						if (root[i].childNodes[j].firstChild)
							{
								//alert(root[i].childNodes[j].nodeName+" = "+root[i].childNodes[j].firstChild.data); 
								this.Add(root[i].childNodes[j].nodeName,root[i].childNodes[j].firstChild.data);
								//alert(root[i].childNodes[j].nodeName+" = "+root[i].childNodes[j].firstChild.data); 
							}//if
					}//for
				
//

				//if duplicate
				//alert("1 = "+this.cmpAttr );
				//alert("IsThere = "+this.IsThere(this.cmpAttr)	);
				//alert("getValue = " + this.getValue(this.current,this.cmpAttr));
				//alert("URLEXIST = " +this.URLIsExist(this.cmpAttr, this.getValue(this.current,this.cmpAttr)));

				if ( this.IsThere(this.cmpAttr)	 && this.URLIsExist(this.cmpAttr, this.getValue(this.current,this.cmpAttr)))
					{
						this.Remove();
					}
					else
					{
						//save it to DB
						var rec_source_name = this.getValue(this.current,'source');
						//new ajaxAllPurposes('update_log.php', 'id_doc='+defualtdoc_id+'&object='+this.getValue(this.current,'identifier')+'&action=addref&value=', '', '', '');
					}

				} //if 
			}//for
			
			addDebMsg('Citation.js | ImportXML from '+sourceurl+': RESULTS #'+root.length);
			addDebMsg('RefBASE Size = '+this.Size());
//			addDebMsg('XML parsed OK!');	

			if (root.length>0 && this.Size()>=ScreenLimit) //if any returned
			{			
				//change # new button
				var tmpo = document.getElementById('newrefbutwind1');
				if (tmpo) tmpo.value = ' New (' + (this.Size()-ScreenLimit).toString()+ ')';

				var tmpo = document.getElementById('newrefbutwind3');
				if (tmpo &&  this.Size()>ScreenLimit) tmpo.style.visibility = 'visible';
			}

			if (root.length==0 && ScreenKeywords>2)
			{
				//request another DL:
				if (dls_url.length>1) setTimeout("OnChangeChain(ScreenKeywords-1,ScreenKeywords, 'AND');", rate_request_keywords/2);
					else 
						{
						//if there is no other DL subscribed, request a simplified query from the same DL
						//setTimeout("OnChangeChain(ScreenKeywords-1,ScreenKeywords, ' ');", rate_request_keywords/2);
						//	var tmppar1 = get_cite_query(ScreenKeywords, ScreenKeywords, "");
						//	if (tmppar1.indexOf(' ',2)>-1) setTimeout("OnChangeChain(ScreenKeywords,ScreenKeywords, ' ');", rate_request_keywords/2);
						}//else

			}


			return true;
		 }//if xmlObj.status == 200
//		 else
//			 addDebMsg('Some problems accesing refXML!');	
	 }//if xmlObj.readyState == 4


	return false;

	}//CCs.prototype.importXML



///////////////////////////////////////////
//request Sources from server 
//////////////////////////////////////////
//	if (this.importXML(xmlObj)) RefreshScreen();  


addDebMsg('Read citation.js OK!');