
//////////////////////////////////////////////////////////////////////////////////////////////////
function write()
{
	x = document.getElementById('citation');
	var allText = "";
	 	for (var i = 0; i < citationArray.length; i++)
		{
		allText = allText + "<p>" + citationArray[i];
		}
	x.innerHTML = allText;
	writeArrayNumber();
}
//////////////////////////////////////////////////////////////////////////////////////////////////
function writeArrayNumber(){
	var numberOfCites = citationArray.length;
	var singularOrPlural = "";
	if (citationArray.length == 1){singularOrPlural = " citation";}
	else {singularOrPlural = " citations";}
	y = document.getElementById('arrayNumber');
	var divText = "";
	var divText = "<h4>"+ "&nbsp;&nbsp;&nbsp;"+'<img src="../images/apa2.gif" align="top" height="22" width="35">&nbsp;'+"APA Citation Maker (based on the 5th edition)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+"You have " + '<FONT COLOR="#CC6600">'+'<b>'+citationArray.length+'</b>'+"</font>" + singularOrPlural+"</h4>";
	
	y.innerHTML = divText;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
function sortCitations()
{
	citationArray.sort(sortWithoutArticles);
	x = document.getElementById('citation');
	var allText = "";
	 	for (var i = 0; i < citationArray.length; i++)
		{
		allText = allText + "<p>" + citationArray[i];
		}
	x.innerHTML = allText;
	
}
//////////////////////////////////////////////////////////////////////////////////////////////////
function totalCitations()
{
	var citations = citationArray.length();
	alert(citations);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
function deleteLast()
{	
	input_box=confirm("Are you sure you want to DELETE the last citation listed below?");
	if (input_box==true)
	{
	citationArray.pop();
	var allText = "";
 	for (var i = 0; i < citationArray.length; i++)
		{
		allText = allText + "<p>" + citationArray[i];
		}
	x.innerHTML = allText;
	}
	else {
	}
	writeArrayNumber();
}

//////////////////////////////////////////////////////////////////////////////////////////////////
function deleteCitations()
{
	input_box=confirm("Are you sure you want to DELETE ALL your citations?");
	if (input_box==true)
	{	
	x = document.getElementById('citation');
	var initialLength = citationArray.length
	for (var i = 0; i < initialLength; i++)
		{
		citationArray.pop();
		}
	
	var noText = "Your citations will appear here.";
	x.innerHTML = noText;
	}
	else {
	}
	writeArrayNumber();
}

//////////////////////////////////////////////////////////////////////////////////////////////////
function saveCitationsHTML()
{
	input_box=confirm("If you want to copy your citation:\n\n1. Click the OK button. A \"Works Cited\" window containing your citation(s) will open.\n2. Copy-paste your citation(s) into your document.\n\nIf you want to cancel the \"Copy\" operation, click the Cancel button to close the message box.");
	if (input_box==true)
	{
	var newWindow = window.open("","base","width=750,height=500,resizable=yes,menubar=yes,scrollbars=yes");
	newWindow.document.write('<html><head><title>Citations</title></head>');
	newWindow.document.write('<body>');

	newWindow.document.write("<center>","Works Cited","</center>","<br>");
	
	var e = document.getElementById('citation');
	newWindow.document.write(e.innerHTML);
	
	newWindow.document.write('</body></html>');
	newWindow.document.close();
	}
	else {
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////

function clearAnyForm(inFormID)
{
	form = document.getElementById(inFormID);
	form.reset();
}
function capitalizeFirstLetter(inString){
	var string = document.getElementById(inString);
	var firstLetter = string.value.charAt(0).toUpperCase();
	var capitalizedWord = firstLetter + string.value.substring(1,36);
	return capitalizedWord;
}

///////////////////////////////////////////////////////////////////////////////////////////////

function sortWithoutArticles(a, b) {
var x = a.toLowerCase();
var y = b.toLowerCase();
while (x.match(/^(\(|,|"|\s|the\s|an\s|a\s)/))
{
// ignore (
if (x.match(/^\(/)){
	x = x.substr(1);
}

// ignore ,
if (x.match(/^,/)){
	x = x.substr(1);
}

// ignore "
if (x.match(/^"/)){
	x = x.substr(1);
}

//ignore spaces 
while (x.match(/^\s/)){
	x = x.substr(1);
}

// ignore the
if (x.match(/^the\s/)){
	x = x.substr(4);
}

// ignore a
if (x.match(/^a\s/)){
	x = x.substr(2);
}

// ignore an
if (x.match(/^an\s/)){
	x = x.substr(3);
}

}
while (y.match(/^(\(|,|"|\s|the\s|an\s|a\s)/))
{
// ignore (
if (y.match(/^\(/)){
	y = y.substr(1);
}
if (y.match(/^,/)){
	y = y.substr(1);
}
if (y.match(/^"/)){
	y = y.substr(1);
}
while (y.match(/^\s/)){
	y = y.substr(1);
}
if (y.match(/^the\s/)){
	y = y.substr(4);
}
if (y.match(/^a\s/)){
	y = y.substr(2);
}
if (y.match(/^an\s/)){
	y = y.substr(3);
}
}
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}
