
//////////////////////////////////////////////////////////////////////////////////////////////////
function write()
{
  x = document.getElementById('citation');
  x.style.display = "block";
  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/mla.jpg" align="top" height="22" width="35">&nbsp;'+"Citation Maker (based on the 7th edition)&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 sortWithoutArticles(a, b) {
var x = a.toLowerCase();
var y = b.toLowerCase();
while (x.match(/^(<i>|\(|,|&quot;|\s|the\s|an\s|a\s)/))
{

// ignore italics tag
if (x.match(/^<i>/)){
  x = x.substr(3);
}

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

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

// ignore "
if (x.match(/^&quot;/)){
  x = x.substr(6);
}

//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(/^(<i>|\(|,|&quot;|\s|the\s|an\s|a\s)/))
{

// ignore italics tag
if (y.match(/^<i>/)){
  y = y.substr(3);
}

// ignore (
if (y.match(/^\(/)){
  y = y.substr(1);
}

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

// ignore "
if (y.match(/^&quot;/)){
  y = y.substr(6);
}

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

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

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

// ignore an
if (y.match(/^an\s/)){
  y = y.substr(3);
}
}
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

//////////////////////////////////////////////////////////////////////////////////////////////////
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('<p align="center">Works Cited</p><br>');
  
  var e = document.getElementById('citation');
  newWindow.document.write(e.innerHTML);
  
  newWindow.document.write('<p>&nbsp;</p>');
  
  newWindow.document.write('<p align="center" style="font-size: small;">Created using MLA Citation Maker on www.oslis.org.</p>');
  
  newWindow.document.write('</body></html>');
  newWindow.document.close();
  }
  else {
  }
}

//////////////////////////////////////////////////////////////////////////////////////////////////
function exportToWordCitations()
{
// Get the citation from the citation div on the page
  var citationHTML = document.getElementById('citation').innerHTML;
  
// Add the heading to the top of the HTML and the credit to the bottom
  citationHTML = '<h1 align="center">Works Cited</h1>' + citationHTML + '<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><h4 align="center">Created using MLA Citation Maker on www.oslis.org.</h4>';
  
// Create a form that will send the citation to the converter page
  var myForm = document.createElement("form");
  myForm.method="post" ;
  myForm.action ="export-to-word.php" ;
  myForm.target ="_blank";
  
// Put the citation in the form input element and send it
  var myInput = document.createElement("input") ;
  myInput.setAttribute("name","citations") ;
  myInput.setAttribute("value", citationHTML);
  myForm.appendChild(myInput) ;
  document.body.appendChild(myForm) ;
  myForm.submit() ;
  document.body.removeChild(myForm) ;

}

//////////////////////////////////////////////////////////////////////////////////////////////////
function exportToGoogleDocsCitations()
{
// Get the citation from the citation div on the page
  var citationHTML = document.getElementById('citation').innerHTML;
  
// Add the heading to the top of the HTML and the credit on the bottom
  citationHTML = '<h1 align="center">Works Cited</h1>' + citationHTML + '<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><h4 align="center">Created using MLA Citation Maker on www.oslis.org.</h4>';
// Create a form that will send the citation to the converter page
  var myForm = document.createElement("form");
  myForm.method="post" ;
  myForm.action ="export-to-google-docs.php" ;
  myForm.target ="_blank";
  
// Put the citation in the form input element and send it
  var myInput = document.createElement("input") ;
  myInput.setAttribute("name","citations") ;
  myInput.setAttribute("value", citationHTML);
  myForm.appendChild(myInput) ;
  document.body.appendChild(myForm) ;
  myForm.submit() ;
  document.body.removeChild(myForm) ;

}

//////////////////////////////////////////////////////////////////////////////////////////////////
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;
}

