var style = "";
var type = "";
var myrefresh, editmade,  myclicked;
var myeditting = false;
var cp = false;
var mydelete = false;
var ii, iii, wordexporting;
var k, begin;
var citation = [];
var authorstuff = [];
var inShowInfo = false;
//think I don't need this. Check it out.
var citationInfo = [];
var mcetitle, justSaved;
//this.intextArray = [];
this.authorNum = "0";
this.hasChangedAuthor = false;
var yourCookieNum = null;
this.numberRows = 0;
this.authorRows = 0;
var myalert = 0;
var medium = "";
var total = "";
var authorString = "";
var tmptotal = "";
var mytotal = "";
var total2;
var dateStr = "";
var dateStringATitle = "";
var mydate = "";
var myMedTitle = "";
var BPag = "";
var NPage = "";
var intext = "";
var pre = "";
var tmp = "";
var workingType, workingMedium;

var editting = "";
var wholeThing = "";
var EditionOut = "";
var EditionAPA = "";
var text = "";
var headbib = "";
var cookienum = null;
var wholeIntext = "";
var pre1 = "";
//var mysave = "";
var citationNum = "";
var savetext = "";
//var justloaded = true;
var BPage = "";
var arrow2 = false;
var arrow3 = false;
var arrow4 = false;
var square = false;
var instuff = [];
var inString = [];
var sortfield = "";
var /*2*/ ATitle, /*3*/MedTitle, /*4*/City, /*5*/Year, /*6*/Publisher, /*7*/State, /*8*/Volume, /*9*/Issue, /*10*/Edition, /*11*/Page, /*12*/DOI, /*13*/URL, /*14*/Dbase, /*15*/Day, /*16*/Month, /*17*/Month2, /*18*/Day2, /*19*/Year2, ellipsisIn, etalIn;

var etal = "false";
var ellipsis = false;
var ATitleIn, MedTitleIn, CityIn, YearIn, PublisherIn, StateIn, VolumeIn, IssueIn, EditionIn, PageIn, DOIIn, URLIn, DbaseIn, DayIn, MonthIn, Month2In, Day2In, Year2In;
var intextATitle = "";
var styleTitle;
var editorCount = 0;
var ATitleCh;

var preAPA = "<p>Enter the following information in your paper according to the Intext Citation Guide. Use your own <span style=\"color:red\">page numbers</span>.</p>";
 

function myfocusload() {
    if (style !== "") {
        popupIntextWindow('arrow2', true);
    } else {
        popupIntextWindow('arrow1', true);
    }
}

function createCookie(name, value, minutes) { 
    if (minutes) {
        var date = new Date();
        date.setTime(date.getTime() + (minutes * 60 * 24 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    } else {var expires = ""; }
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}


function putBack() {
    total = "";
    cp = false;
    document.getElementById('copypastetextarea').style.display = "none";
}

function putBacktp() {
    total = "";
    cp = false;
    document.getElementById('copypastetitlepage').style.display = "none";

}

function copypastetp() {
    document.getElementById("copypastetitlepage").style.display = "block";
    if (styleTitle == "MLA") {
        begin = "<span style=\"color:red\">Begin paper here</span>"
    } else {
        begin = "";
    }

    document.getElementById("copypastetitlepage").innerHTML = "<center><strong>Copy highlighted text and paste into your paper.</strong><a style=\"float:left\"   href=\"javascript:void(0)\" onclick=\"fnSelect('mycopytp');return false;\">Highlight text</a><a  style=\"float:right\" href=\"javascript:void(0)\" onclick=\"putBacktp();return false;\">Close</a></center><br /><div style='font-size:12pt;font-family:\"Times New Roman\"' id='mycopytp'>" + total2 + "</div>" + begin + "<a  style=\"float:right\" href=\"#\" onclick=\"putBacktp();return false;\">Close</a>";
    fnSelect('mycopytp');

}

function restoredefaulttab(){
popupIntextWindow('popup'+ style,false);
	$("ul.tabs li").removeClass("active"); //Remove any "active" class
    $(".tab_content").hide();
	$("ul.tabs li:first").addClass("active").show(); //Activate first tab
    $(".tab_content:first").show();
	activeTab = "#tab1";
}

function resetEnterMode(){
	document.getElementById("citationsaved").innerHTML = "";
    if (document.getElementById("ae" + justSaved) !== null) {document.getElementById("ae" + justSaved).style.backgroundColor="#FFF";
	document.getElementById("newarea").innerHTML = "Type in boxes to add new citation";
			document.getElementById("newarea").className = "newarea";
		}
}
function exportWord(){
 if (total != "" && editmade != false) refresh();
 resetEnterMode();
}
function copypaste() { 
	if (total !== "" && editmade !== false) refresh(); //editmade only becomes false in editmode - otherwise true
	resetEnterMode();
	if (activeTab == "#tab2"){
		restoredefaulttab();
	}
    if (cp !== true) { //what is this for?  maybe if already in copy
        popupIntextWindow('square', false);
        var tmpcp = "";
        cp = true;
        for (iii = 0; iii < citationNum; iii++) { 
            readArray(citation[iii]);
			tmpcp = tmpcp + "<p style='font-size:12pt;font-family:\"Times New Roman\";text-indent:-30px;margin:0;margin-left:30px;line-height:27.5pt' class='noformat' ><span class='noformat'>" + total + "</span></p>";
            total = "";
            aCount = 1;
        }
        document.getElementById("copypastetextarea").style.display = "block";
        document.getElementById("copypastetextarea").innerHTML = "<center><strong>Copy highlighted text and paste into your paper.</strong><a style=\"float:left\"   href=\"javascript:void(0)\" onclick=\"fnSelect('mycopy');return false;\">Highlight text</a><a  style=\"float:right\" href=\"javascript:void(0)\" onclick=\"putBack();return false;\">Close</a></center><br /><div style='font-size:12pt;font-family:\"Times New Roman\"' id='mycopy'><div style=\"text-align:center\">" + headbib + "</div>" + tmpcp + "</div><a  style=\"float:right\" href=\"javascript:void(0)\" onclick=\"putBack();return false;\">Close</a>";
        fnSelect('mycopy');
    }
}

function confirmerase() {
    document.getElementById("citationsaved").innerHTML = "";
    var zz = confirm("Are you sure you want to clear bibliography? All citations will be permanently deleted.");
    if (zz == true) eraseCookie();
}

function eraseCookie(name) {
     total = "";
    intext = "";
    pre = "";
    cookienum = readCookie('yourCookieNum');
    createCookie('yourCookieNum', "", -1);
    for (i = 0; i <= cookienum; i++) {
        createCookie('cite' + i, "", -1);
        citation[i] = null;
        citation.length = 0;
    }
    wholeThing = "";
    wholeIntext = "";
    Year = "";
    intextString = "";

    citationNum = 0;
    cookienum = null;
    pre1 = "";
    myrefresh = false;
    if (myeditting == true) { //clears entries if in edit mode
        myeditting = false;
        editting = "";
    }
    showInfo(type);
    document.getElementById("sortedcitations").innerHTML = "";
    document.getElementById("newarea").innerHTML = newAreaTxt;
    document.getElementById("newarea").className = "newarea";
    document.getElementById("intxt").innerHTML = "";

}

function deleteCitation(i) { 
if (total !== "" && myeditting == true)  zz = confirm("If you continue without saving your edits you will lose them. Continue?");
else{
    var zz = confirm("Are you sure you want to delete this citation?"); }
    if (zz == true) {
        createCookie('cite' + i, "", -1);
        myrefresh = true;
        mydelete = true;
       
		for (var j = i; j < citationNum - 1; j++) { //citationNum is always one more thannumber of actual citations
            citation[j] = citation[j + 1];
            createCookie('cite' + j, citation[j], 1);
        }

        citation[citationNum - 1] = null;
        createCookie('cite' + j, "", -1);
        cookienum = readCookie('yourCookieNum');
        if (cookienum > 0) {
            cookienum = cookienum - 1;
            createCookie('yourCookieNum', cookienum, 1);
        } else createCookie('yourCookieNum', "", -1); //only one citation
        citationNum = citationNum - 1;

        citation.length = citation.length - 1;
		var tmpTotal ="";
		
		if (editting === ""){tmpTotal = total;} //adding new one keeps it.
        displayRefresh();
	
        mydelete = false;
        if (myeditting !== false) {
            myeditting = false;
            editting = "";
            showInfo(type);
        }
   
    aCount = counter;

	if (tmpTotal !== ""){
		 document.getElementById("newarea").innerHTML= tmpTotal;
	total = tmpTotal;} //this is for if you delete 2 in a row
	else {
		document.getElementById("newarea").innerHTML = newAreaTxt;
	
	document.getElementById("newarea").className ="newarea";}
	 if (document.getElementById("ae" + justSaved) !== null) {document.getElementById("ae" + justSaved).style.backgroundColor="#FFF";
	 justSaved ="";
	 }
	 
    if (workingType !== undefined) type = workingType;
    if (workingMedium !== undefined) medium = workingMedium;

   }  //this is if you are in the middle of a citation with multiple authors when you delete
if (total !== "") sendToBottom(); //this is if in middle of a citation when deleting - so you can save immediately.
} //end deleteCitation
//FUNCTION display REFRESH //  loops through array to read it and loops rewrites cookies 


function displayRefresh() {
    var wordinfo = "";
    wholeThing = ""; //move to refresh
    wholeIntext = ""; // move to refresh
    i = 0;
    edittingstyle = "";
    wordinfo = "";

    if (editting !== "" || mydelete == true) mystop = citationNum - 1;
    else {
        mystop = citationNum
    }
    citation.sort();
    for (iii = 0; iii <= mystop; iii++) {
        if (firsttime !== true && mydelete !== true) {
            createCookie('cite' + iii, citation[iii], 1);
        }
        readArray(citation[iii]);
		
        myicons = "<div style='float:right'><a href='javascript:void(0)' onclick=\"updateCitation(" + iii + ");return false;\" class=\"info left2 narrow noline\"><img title='' src='images/edit.gif' alt='edit.gif' width='20' /><span>Edit this item</span></a> <a href='#' onclick=\"deleteCitation(" + iii + ");return false;\"><img title='Delete' tabindex=\"-1\" src='images/delete.gif' alt='delete.gif' width='20' /></a></div>"; // move to refresh
        wordinfo = wordinfo + "<p class=\"indent\" >" + total + "</p>"; //
        //CITATIONS AFTER REFRESH
       if (justSaved === iii){tmp = "<div id='ae" + iii + "' style='float:left;clear:both;width:312px;background-color:#FAA;'><p style='margin:0;padding:5px 0 5px 3px' id='pae" + iii + "'>" + total + "</p></div>" + myicons;}
	   else {
	    tmp = "<div id='ae" + iii + "' style='float:left;clear:both;width:312px;background-color:#FFF;'><p style='margin:0;padding:5px 0 5px 3px' id='pae" + iii + "'>" + total + "</p></div>" + myicons;}
 
       if (style == "MLA")wholeIntext = wholeIntext + "<p id='at" +iii + "'>(<span style=\"color:blue\">" + intextString + " </span><span style=\"color:red\"> xx-xx</span>)</p>";
	   
	   if (style == "APA"){
	    if (YearIn == "") YearIn = "YYYY";
        wholeIntext = wholeIntext + "<p  id='at" +iii + "'>(<span style=\"color:blue\">" + intextString + ",</span><span style=\"color:green\"> " + YearIn + "</span>, p.<span style=\"color:red\"> xx-xx</span>)</p>";
	   }
	    wholeThing = wholeThing + tmp; //good
    }
    if (mydelete !== true && firsttime !== true) {
        document.getElementById("newarea").innerHTML = "Citation saved | Type in boxes to add new citation";
        document.getElementById("newarea").className = "newareasaved";
    	if (myeditting == true)document.getElementById("newarea").innerHTML = "Citation updated | Type in boxes to add new citation";
	}
    document.getElementById("sortedcitations").innerHTML = wholeThing
    if (style !== "Chicago") document.getElementById("intxttype").innerHTML = newAreaInTxt;
    document.getElementById('intxt').innerHTML = wholeIntext;
    if (mydelete !== true) aCount = 1;
    myrefresh = false;
    document.getElementById("wordexport").value = "<div style=\"text-align:center\">" + headbib + "</div>" + wordinfo;
    
	total = ""; //have to set total to empty so can bring in total from text boxes

} // end displayRefresh()

function updateBox(which) {
    
    //KEY TO WHOLE PROGRAM - shows as you are typing//	
    if (total === "") { // in updateBox total is empty
        document.getElementById("newarea").innerHTML = newAreaTxt;
        document.getElementById("newarea").className = "newarea";
       if (style !== "Chicago") {document.getElementById("intxttype").innerHTML = newAreaInTxt;}
	   else {document.getElementById("intxttype").innerHTML = "Coming soon";}
    } else {

        if (editting !== "") { //editting a citation
            document.getElementById("pae" + editting).innerHTML = total;
            
			if (style == "APA")
			document.getElementById("at" + editting).innerHTML = "<span style=\"color:blue\">" + intextString + ",</span><span style=\"color:green\"> " + Year + "</span>, p.<span style=\"color:red\"> xx-xx</span>)";
        if (style == "MLA") document.getElementById("at" + editting).innerHTML = "<span style=\"color:blue\">(" + intext + "</span><span style=\"color:red\"> xx</span>)";
		
		} else { // 
            document.getElementById("newarea").innerHTML = total;
            document.getElementById("newarea").className = "newareatype";
          
            if (style == "MLA") document.getElementById('intxttype').innerHTML = "<span style=\"color:blue\">(" + intext + "</span><span style=\"color:red\"> xx</span>)";
            
if (style == "APA") {document.getElementById('intxttype').innerHTML = "(<span style=\"color:blue\">" + intextString + ",</span><span style=\"color:green\"> " + Year + "</span>, p.<span style=\"color:red\"> xx-xx</span>)";}
            if (style == "Chicago"){document.getElementById("intxttype").innerHTML = "Coming soon"}
        }
    }
 }
//end function updateBox

function quitEdit() { 
	 if (editmade !== true)showInfo(type);
	 document.getElementById('step3legend').innerHTML = 'Step 3: Fill in all known information';
	
	document.getElementById('authorFirst1').style.backgroundColor = '#F8EBC0';
            document.getElementById('MI1').style.backgroundColor = '#F8EBC0';
		    document.getElementById('authorLast1').style.backgroundColor = '#F8EBC0';
			if (editmade === true) {savetext = "<font color=\"red\"><strong>Citation updated</strong></font>";}
			else {document.getElementById("newarea").innerHTML = "Type in boxes to add new citation";
			document.getElementById("newarea").className = "newarea";
			document.getElementById('ae' + editting).style.backgroundColor = '#FFF';
			savetext="";
			}
			myeditting = false;
			editting="";
			}

function refresh() {
    if (editmade == false){quitEdit()}// else {
	
    if (total !== "") { //excludes for word export where citation is already saved 
		myrefresh = true;
        myalert = 0;
        document.getElementById("myButton").value = 'Save Citation';
        //document.getElementById("myButton").disabled = "disabled";
        
		deleteInfoRow(numberRows);  //clears textboxes
		
        hasChangedAuthor = false;
		// SHOW INFO FUNCTION 
        if (type !== ""  && inShowInfo !== true) showInfo(type);  //loads textboxes
        if (style !== "" && type == "") {
           
            document.getElementById('temp').className = "show";
        }

        if (editting !== "") { /*only hits this part */
            thenumber = editting;
                 } else {
            thenumber = citationNum;
            createCookie('yourCookieNum', citationNum, 1);
        }
        //more exclusion if saving to word
        if (editmade !== false){ //does not save citations if no editting was done
		var authorSt = "";
        for (var w = 1; w <= saveaCount; w++) {
            authorSt = authorSt + contributorIn[w] + "??";
            authorSt = authorSt + authorFirstIn[w] + "??";
            authorSt = authorSt + MIIn[w] + "??";
            authorSt = authorSt + authorLastIn[w] + "++";
        }
        if (style == "APA") authorSt = authorSt + ellipsisIn;
        else if (style == "MLA") authorSt = authorSt + etalIn;
		
        sortfield = intextString.replace(/"/g, "").toLowerCase();
        sortfield = sortfield.replace("<em>", "");
        sortfield = sortfield.replace(/^\s+|\s+$/g, '');
		if (sortfield == "") sortfield = "zzz"; //puts citations at end that are missing information;
		authorSt = "jsj" +authorSt;  //jsj = just saved
        citation[thenumber] = sortfield + "::" + type + "::" + saveaCount + "++" + authorSt + "::" + ATitleIn + "::" + MedTitleIn + "::" + CityIn + "::" + YearIn + "::" + PublisherIn + "::" + StateIn + "::" + VolumeIn + "::" + IssueIn + "::" + EditionIn + "::" + PageIn + "::" + DOIIn + "::" + URLIn + "::" + DbaseIn + "::" + DayIn + "::" + MonthIn + "::" + Month2In + "::" + Day2In + "::" + Year2In +"::" + medium;
        createCookie('style', style, 1);
        createCookie('type', type, 1);
		intextString = "";

        intext = "";
        var tmptype = type; //type gets changed when looping through citations
        //DISPLAY REFRESH
        total = "";
		
        displayRefresh(); /*type="notChosen"; reason is to add icons//updatebox seems to fuck it up for editting*/
  
	    if (workingMedium !== undefined) medium = workingMedium;
		type = tmptype; 
		
		if (editting === "") {
            citationNum = parseInt(citationNum) + 1;
			 savetext = "<font color=\"red\"><strong>Citation saved</strong></font>";
        } //increase citationNum important!! not sure if in correct spot
        if (myeditting == true) { quitEdit();}
		          
        }
       
		
        wordexporting = false;

        total = "";
        document.getElementById("citationsaved").innerHTML = savetext;
        popupIntextWindow('arrow4', false);
        popupIntextWindow('arrow3', false);
        if (square === false) popupIntextWindow('square', true);
        square = true;
        if (activeTab == "#tab2") {
            document.getElementById("mytab1").className = "graybk"
        }
    } 
}


function refreshStyle() {

    document.getElementById('Magazine').checked = false;
    document.getElementById('Book').checked = false;
    document.getElementById('Journal').checked = false;
    document.getElementById('Newspaper').checked = false;
    document.getElementById('Website').checked = false;
    for (i = 1; i <= 3; i++) {
        document.getElementById('style' + i).checked = false;
    }
    document.getElementById('Print').checked = true;

}

function sendToBottom() { 
	editmade = true;
	if (myeditting !== false  ) document.getElementById("myButton").value = 'Update Citation';
	    document.getElementById("citationsaved").innerHTML = "";
    if (document.getElementById("ae" + justSaved) !== null) {document.getElementById("ae" + justSaved).style.backgroundColor="#FFF";
	}
	justSaved ="";
	if (cp == true) putBack();
    //dateStr = "";
    EditionOut = "";
    for (var w = 1; w <= aCount; w++) {
        authorLast[w] = "";
        authorFirst[w] = "";
        MI[w] = "";
        contributor[w] = "";
    }

    editorCount = 0;
    for (var w = 1; w <= aCount; w++) {
        try {
            contributorIn[w] = "";
            if (document.getElementById("contributor" + w) !== null) {
                contributor[w] = document.getElementById("contributor" + w).value;
                contributorIn[w] = contributor[w];
                if (contributor[w] == "Ed") editorCount = editorCount + 1;
            }
            authorLast[w] = document.getElementById("authorLast" + w).value;

            authorLastIn[w] = authorLast[w];
            authorFirst[w] = document.getElementById("authorFirst" + w).value;
            authorFirstIn[w] = authorFirst[w];

            MI[w] = document.getElementById("MI" + w).value;
            MIIn[w] = MI[w];
        } catch (err) {
            alert("inerror 2")
        }
    }
    try {
        etal = document.getElementById("etal").checked;
        etalIn = etal;
    } catch (err) {
        etal = "";

    }
    try {
        ellipsis = document.getElementById("ellipsis").checked;
        ellipsisIn = ellipsis;
    } catch (err) {
        ellipsis = "";
    }
    authorString = author(contributor, authorLast, authorFirst, MI, etal, ellipsis);

    editorString = authorString.split("::")[2];
    intextString = authorString.split("::")[1];
    if (editorString == undefined) {
        editorString = "";
    }
    authorString = authorString.split("::")[0]; 
    //document.getElementById("myButton").disabled = false;
    if (arrow4 === false) popupIntextWindow('arrow4', true);
    arrow4 = true;
    popupIntextWindow('arrow3', false);
    popupIntextWindow('arrow2', false);
    popupIntextWindow('square', false);
    this.total = ""; /******* GET INFORMATION FROM OTHER FIELDS ***/
    try {
        ATitle = document.getElementById("ATitle").value;
    } catch (err) {
        ATitle = "";
    }
    citationInfo['ATitle'] = ATitle;
    try {
        MedTitle = document.getElementById("MedTitle").value;
    } catch (err) {
        MedTitle = "";
    }
    citationInfo['MedTitle'] = MedTitle;
    try {
        City = document.getElementById("City").value;
    } catch (err) {
        City = "";
    }
    try {
        Publisher = document.getElementById("Publisher").value;
    } catch (err) {
        Publisher = "";
    }
    try {
        State = document.getElementById("State").value;
    } catch (err) {
        State = "";
    }
    try {
        Volume = document.getElementById("Volume").value;
    } catch (err) {
        Volume = "";
    }
    try {
        Issue = document.getElementById("Issue").value;
    } catch (err) {
        Issue = "";
    }
    IssueIn = Issue;
    try {
        Edition = document.getElementById("Edition").value;
    } catch (err) {
        Edition = "";
    }
    try {
        Page = document.getElementById("Page").value;
    } catch (err) {
        Page = "";
    }
    try {
        DOI = document.getElementById("DOI").value;
    } catch (err) {
        DOI = "";
    }
    try {
        URL = document.getElementById("URL").value;
    } catch (err) {
        URL = "";
    }
    try {
        Dbase = document.getElementById("Dbase").value;
    } catch (err) {
        Dbase = "";
    }
    try {
        Day = document.getElementById("Day").value;
        if (Day.substr(0, 1) === "0") Day = Day.substr(1, 1);
        if (Day == "dd") Day = "";
    } catch (err) {
        Day = "";
    }
    DayIn = Day;
    try {
        Month = document.getElementById("Month").value;
        if (Month == "Month") {
            Month = "";
        }
    } catch (err) {
        Month = "";
    }
    try {
        Year = document.getElementById("Year").value;
        if (Year == "yyyy") {
            Year = "";
        }
    } catch (err) {
        Year = "";
    }
    citationInfo['Year'] = Year;
    try {
        Day2 = document.getElementById("Day2").value;
        if (Day2.substr(0, 1) === "0") Day2 = Day2.substr(1, 1);
        if (Day2 == "dd") {
            Day2 = "";
        }
    } catch (err) {
        Day2 = "";
    }
    try {
        Month2 = document.getElementById("Month2").value;
        if (Month2 == "Month") {
            Month2 = "";
        }
    } catch (err) {
        Month2 = "";
    }
    try {
        Year2 = document.getElementById("Year2").value;
        if (Year2 == "yyyy") {
            Year2 = "";
        }
    } catch (err) {
        Year2 = "";
    }
    formatFields();
} //end function sendtobottom

function formatFields() { /********* FORMAT FIELDS ***********/
	dateStr = "";
	EditionOut="";
	intextATitle = ATitle;
	intextMedTitle = MedTitle;
    ATitleIn = ATitle;
    MedTitleIn = MedTitle;
    CityIn = City;
	if (CityIn =="Only include if city not in the title and is local") CityIn ="";
    YearIn = Year;
    PublisherIn = Publisher;
    StateIn = State;
    VolumeIn = Volume;
    IssueIn = Issue;
    EditionIn = Edition;
    PageIn = Page;
    DOIIn = DOI;
	URLIn = URL;
	if (URLIn =="http://") URLIn ="";
    DbaseIn = Dbase;
    DayIn = Day;
    MonthIn = Month;
    Month2In = Month2;
    Day2In = Day2;
    Year2In = Year2;

    if (Edition !== "" && Edition != undefined) {
        var lastnum = Edition.substr(Edition.length - 1, 1);
        if (Edition == 11 || Edition == 12) lastnum = 4;
        switch (lastnum) {
        case "1":
            EditionOut = Edition + "st ed.";
            break;
        case "2":
            EditionOut = Edition + "nd ed.";
            break;
        case "3":
            EditionOut = Edition + "rd ed.";
            break;
        default:
            EditionOut = Edition + "th ed.";
        }
    }
    if (Year2 !== "") {
        Year2Period = Year2 + ". ";
        Year2Comma = Year2 + ", ";
    } else {
        Year2Period = "";
        Year2Comma = "";
    }
    if (Year !== "") {
        var YearColon = Year + ": ";
        var YearPeriod = Year + ". ";
        var YearBracketColon = " (" + Year + "): ";
        var YearComma = Year + ", ";
        if (Month === "" && Day === "") {
            YearComma = Year;
        } else {
            YearComma = Year + ", ";
        }
    } else {
        YearColon = "";
        YearPeriod = "";
        YearComma = "";
        YearBracketColon = "";
    }
    if (Day !== "") {
        SpaceDay = " " + Day;
        DayComma = Day + ", ";
    } else {
        SpaceDay = "";
        DayComma = "";
    }
    if (Day2 !== "") {
        Day2Comma = Day2 + ", ";
    } else {
        Day2Comma = "";
    }
    if (Volume !== "") {
        VolumeComma = Volume + ", ";
    } else {
        VolumeComma = "";
    }
    if (Issue !== "") {
        IssueNo = "no. " + Issue;
    } else {
        IssueNo = "";
    }
    if (Year !== "") {
        YearColon = Year + ": ";
    } else {
        YearColon = "";
    }
    if (Publisher !== "") {
        PublisherComma = Publisher + ", ";
    } else {
        PublisherComma = "";
    }
    if (URL !== "" && URL !== "http://") {
        myURL = " &lt;" + URL + "&gt;.";
    } else {
        myURL = "";
    }
    if (type !== "Newspaper") {
        if (City !== "" && State != "") {
            City = City + ", " + State + ": ";
        }
        if (City !== "" && State == "") {
            City = City + ": ";
        }
        if (City == "" && State !== "") {
            City = State + ": ";
        }
    }
    switch (style) {
    case "APA":
        EditionAPA = "";
        if (Edition != "") {
            EditionAPA = " (" + EditionOut + "). ";
        }
        if (ATitle !== "") {
            intextATitle = ATitle;
            ATitle = " " + ATitle + ". ";
            if (type == "Book") {
                ATitle = ATitle + "In "
            }
        }
        if (type === "Journal") {
            if (MedTitle !== "") {
                MedTitle = " <em>" + MedTitle + "</em>, ";
            }
        } else {
            if (MedTitle !== "") {
                if (type === "Newspaper") {
                    if (City != "" && City != "Only include if city not in the title and is local") {
                        CityFormat = " (" + City + ")";
                    } else {
                        CityFormat = "";
                    }
                    MedTitle = " <em>" + MedTitle + CityFormat + "</em>, ";
                } else {
                    MedTitle = " <em>" + MedTitle + "</em>. ";
                }
            }
        }
        if (type == "Book" && MedTitle != "") intextATitle = MedTitle;
        if (Publisher !== "" || (URL !== "" && URL !== "http://")) {
            var From = " from ";
        } else {
            From = " ";
        }
        if (Publisher !== "") {
            Publisher = Publisher + ". ";
        }
        if (Volume !== "") {
            Volume = "<em>" + Volume + "</em>";
        }
        if (Volume !== "" && Issue === "") {
            if (Page == "") Volume = Volume + ". ";
            else Volume = Volume + ", ";
        }
        if (Issue !== "") {
            if (Page == "") Issue = "(" + Issue + "). ";
            else Issue = "(" + Issue + "), ";
        }
        if (Day !== "" || Month !== "" || Year !== "") {
            dateString = " (" + YearComma + Month + SpaceDay + ").";
        } else {
            dateString = " ";
        }
        if (MedTitle != "" && dateString == " ") dateString = " (n.d.).";
        if (Day2 !== "" || Month2 !== "" || Year2 !== "") {
            dateString2 = " " + Month2 + " " + Day2Comma + " " + Year2Comma;
        } else {
            dateString2 = "";
        }
        if (DOI !== "") {
            DOI = "doi:" + DOI
        }
        if (dateString2 !== "" || (URL !== "" && URL !== "http://")) {
            Retrieved = " Retrieved ";
        } else {
            Retrieved = "";
        }
        if (Page !== "" && Page !== undefined) {
            NPage = "p. " + Page;
            if (Page.indexOf("-") != -1 || Page.indexOf(",") != -1) {
                NPage = "pp. " + Page;
            }
            BPage = "(" + NPage + "). ";
            Page = Page + ". ";
            NPage = NPage + ". ";
        } else {
            NPage = "";
			BPage ="";
        }
        break;
    case "MLA":
        if (type == "Website" || medium == "Web") {
            outmedium = " Web."
        } else {
            outmedium = " Print.";
        }
        if (ATitle !== "") {
            ATitle = " \"" + ATitle + ".\" ";
            intextATitle = ATitle;
        }
        if (MedTitle !== "") {
            if (type == "Book") intextATitle = MedTitle;
            if (City != "" && City != "Only include if city not in the title and is local" && type == "Newspaper") {
                CityFormat = " [" + City + "]";
            } else {
                CityFormat = "";
            }
            if (type == "Website" || type == "Book") {
                MedTitle = " <em>" + MedTitle + "</em>. ";
            } else {
                MedTitle = " <em>" + MedTitle + "</em>" + CityFormat + " ";
            }
        }
        if (Publisher !== "") {
            Publisher = Publisher + ", ";
        } else Publisher = "N.p., "
        if (Publisher == "N.p., " && MedTitle == "") Publisher = " ";
        if (Issue !== "" && Volume !== "") {
            Issue = "." + Issue;
        }
        if (Page !== "") {
            Page = Page + ". ";
        } else Page = " ";
        if (MedTitle != "" && type !== "Book" && Page == " ") Page = "n.pag. ";
        if (Month !== "") {
            Month = Month + " ";
        }
        if (DOI !== "") {
            DOI = DOI + "."
        }
        if (Year != "") {
            if (type == "Magazine" || type == "Newspaper") {
                myYear = Year + ": ";
            } else if (type == "Journal") {
                myYear = YearBracketColon;
            } else {
                myYear = YearPeriod;
            }
        } else {
            myYear = "";
        }
        if (Day !== "" || Month !== "" || Year !== "") {
            dateStr = SpaceDay + " " + Month + myYear;
        } else {
            if (MedTitle != "") {
                dateStr = "n.d. ";
            }
        }
        if (Day2 !== "" || Month2 !== "" || Year2 !== "") {
            dateString2 = " " + Day2 + " " + Month2 + " " + Year2Period;
        } else {
            dateString2 = "";
        }
        if (Dbase !== "") {
            Dbase = " <em>" + Dbase + "</em>."
        }
        break;
    case "Chicago":
        ATitleCh = "";
        if (ATitle !== "") {
            ATitleCh = " \"" + ATitle + ".\" ";
            if (type == "Book") ATitleCh = ATitleCh + "In "
        }
        myMedTitle = "";
        // if (MedTitle !== "") {
        if (Day !== "" || Month !== "" || Year !== "") {
            dateStr = Month + SpaceDay + ", " + Year;
            if (Page == "") dateStr = dateStr + ".";
            else dateStr = dateStr + ",";
        }
        if (type == "Book") {
            if (Page != "") {
                myMedTitle = ", " + Page
            }
            if (editorString != "") {
                myMedTitle = ", " + editorString + myMedTitle
            }
            if (Edition != "") {
                myMedTitle = ", " + EditionOut + myMedTitle
            }
            if (Page == "") myMedTitle = " <em>" + MedTitle + "</em>" + myMedTitle + " ";
            else myMedTitle = " <em>" + MedTitle + "</em>" + myMedTitle + ". ";
        } else {
            if (City != "" && City != "Only include if city not in the title and is local") {
                CityFormat = " (" + City + ")";
            } else {
                CityFormat = "";
            }
            if (type == "Journal") {
                myMedTitle = " <em>" + MedTitle + "</em> ";
            } else {
                myMedTitle = " <em>" + MedTitle + CityFormat + "</em>, ";
            }
        }
        // }
        if (DOI !== "") {
            DOI = "doi:" + DOI + ". "
        }

        if (Publisher !== "") {
            Publisher = Publisher + ".";
        }
        if (Issue !== "") {
            Issue = "(" + Issue + ") ";
        }
        if (Issue != "" && Volume != "") Volume = Volume + ", ";
        if (Day2 !== "") {
            Day2 = " " + Day2;
        }
        if (Day2 !== "" || Month2 !== "" || Year2 !== "") {
            dateString2 = " Accessed " + Month2 + Day2 + ", " + Year2 + ". ";
        } else {
            dateString2 = "";
        }
        if (Page !== "") {
            Page = " " + Page + ". ";
            NPage = " p." + Page;
        } else {
            NPage = "";
        }
        //if (URL.indexOf("10.")==0) URL ="doi:"+ URL + ".";
        if (URL != "" && URL !== "http://") {
            URL = URL + ".";
        } else {
            URL = ""
        };
        if (Month != "" || DayComma != "" || YearPeriod != "") {
            modified = "Last modified "
        } else {
            modified = "";
        }
        break;
    }
    if (URL !== "" && URL !== "http://") {
        fromURL = " from " + URL;
    } else {
        fromURL = "";
    }
    if (authorNum === 0) {
        authorString = "";
    }
    if (medium == "Print" && type != "Website") {
        Dbase = "";
        dateString2 = "";
        myURL = "";
        Retrieved = "";
        fromURL = "";
        DOI = "";
    }
    switch (style) {
    case "APA":
        if (authorString == "") {
            dateStringATitle = ATitle + dateString;
            if (type == "Book") dateStringMedTitle = MedTitle + dateString;
        } else {
            dateStringATitle = dateString + ATitle;
            if (type == "Book") dateStringMedTitle = dateString + " " + MedTitle;
        }
        switch (type) {
            //******* APA MAGAZINE **************//
        case "Magazine":
            total = authorString + dateStringATitle + MedTitle + Volume + Issue + Page + DOI + Retrieved + dateString2 + fromURL;
            break;
            //******* APA BOOK **************//
        case "Book":
            if (ATitle !== "") total = authorString + dateString + " " + ATitle + editorString + MedTitle + " " + EditionAPA + BPage + City + Publisher + DOI + Retrieved + fromURL;
            else total = authorString + dateStringMedTitle + editorString + " " + EditionAPA + BPage + City + Publisher + DOI + Retrieved + fromURL;
            break;
            //******* APA JOURNAL **************//
        case "Journal":
            total = authorString + dateStringATitle + MedTitle + Volume + Issue + Page + DOI + Retrieved + dateString2 + fromURL;
            break;
            //******* APA NEWSPAPER **************//
        case "Newspaper":
            total = authorString + dateStringATitle + " " + MedTitle + Volume + Issue + NPage + DOI + Retrieved + dateString2 + fromURL;
            break;
            //******* APA WEBSITE **************//
        case "Website":
            total = authorString + dateStringATitle + " " + MedTitle + Retrieved + dateString2 + fromURL;
            if (authorString == "" && ATitle == "") total = MedTitle + dateString + Retrieved + dateString2 + fromURL;
            break;
        }
		if (Year == "") Year = "<span style=\"color:#736F6E\">YYYY</span>";
        if (intextATitle !== undefined) intextATitle = intextATitle.replace(/^\s+|\s+$/g, '')
        if (authorString == "") intextString = "\"" + intextATitle + "\"";
        if (intextATitle == "" && authorString == "" && type == "Website") {
            intextString = "\"" + intextMedTitle + "\"";
        }
		 
        intext = intextString + "||" + Year;
        
		break;
    case "MLA":
        switch (type) {
            //******* MLA MAGAZINE **************//
        case "Magazine":
            total = authorString + ATitle + MedTitle + dateStr + Page + Dbase + outmedium + dateString2 + myURL;
            break;
            //******* MLA BOOK **************//
        case "Book":
            total = authorString + ATitle + MedTitle + editorString + EditionOut + " " + City + Publisher + dateStr + Page + Dbase + outmedium + dateString2 + myURL;
            break;
            //******* MLA JOURNAL **************//
        case "Journal":
            total = authorString + ATitle + MedTitle + Volume + Issue + dateStr + Page + Dbase + outmedium + dateString2 + myURL;
            break;
            //******* MLA Newspaper **************//
        case "Newspaper":
            total = authorString + ATitle + MedTitle + dateStr + Page + Dbase + outmedium + dateString2 + myURL;
            break;
            //******* MLA WEBSITE **************//
        case "Website":
            total = authorString + ATitle + MedTitle + Publisher + dateStr + DOI + outmedium + dateString2 + " " + myURL;
            break;
        }

        total = total.replace(/(\s+)/g, " ");
        if (total == " Print." || total == "Print." || total == " Web." || total == " Web." || total == "n.pag. Print." || total == "n.pag. Web." || total == " N.p., Print.") total = "";
        if (intextString == "") intextString = intextATitle.replace(/^\s+|\s+$/g, '')
        intext = intextString;
        break;
    case "Chicago":

        switch (type) {
            //******* CHICAGO BOOK **************//
        case "Book":
            total = authorString + ATitleCh + myMedTitle + City + PublisherComma + YearPeriod + dateString2 + URL;
            break;
            //******* CHICAGO MAGAZINE**************//
        case "Magazine":
            total = authorString + ATitleCh + myMedTitle + Month + " " + DayComma + YearPeriod + dateString2 + URL;
            break;
            //******* CHICAGO JOURNAL **************//
        case "Journal":
            total = authorString + ATitleCh + myMedTitle + Volume + IssueNo + YearBracketColon + Page + dateString2 + DOI + URL;

            break;
            //******* CHICAGO NEWSPAPER**************//
        case "Newspaper":
            total = authorString + ATitleCh + myMedTitle + Volume + Issue + dateStr + Page + dateString2 + URL;
            break;
            //******* CHICAGO WEBSITE **************//
        case "Website":
            total = authorString + ATitleCh + myMedTitle + Publisher + modified + Month + " " + DayComma + " " + YearPeriod + dateString2 + URL;
            break;
        }
        pre = "<div class=\"nomarg\"><p>In Chicago style, footnotes or endnotes are used to reference pieces of work in the text.</p><p>To cite from a source a superscript number is placed after a quote or a paraphrase.</p><p>Example: </p> Cole found that &quot;The bones were very fragile&quot; (33-34).<sup>1</sup></p><p>Each superscript number refers to a numbered citation in the footnotes or endnotes.<strong>*</strong>Please refer to the Chicago style guide for more information. <p><strong>Options</strong></p><p><strong> 1.</strong> If you have not referenced the citation in the text yet.</p><p><strong>2.</strong> If you have already referenced the citation in the text.</p><p><strong>3.</strong> If you have two consecutive citations from the same location</p></div>";
        intext = "<p> <span style=\"color:blue\">(" + intextString + "</span><span style=\"color:red\"> xx</span>). <br /></p>";
        intext = "See Chicago Intext Guide for instructions";

        total = total.replace("<em></em>", "");
        if (total == ",") total = "";
        break;
    }
    total = total.replace(/(\s+)/g, " ");

  //  if (total == "" || total == " ") document.getElementById("myButton").disabled = true;
    if (total == " ") total = "";
    tmptotal = total;
    Page = Page.replace(".", "");
    Page = Page.replace(" ", "");
    pre1 = "<p>Enter the following information in your paper according to the Intext Citation Guide. Use your own <span style=\"color:red\">page numbers</span>.</p>";
//alert(myrefresh);
    if (myrefresh !== true && cp !== true) updateBox(); //eliminates first time hit page getting cookie information
}
//end function format fields

function getfields() {
    try {
        var runninghead = document.getElementById("runninghead").value;
    } catch (err) {
        runninghead = "";
    }
    try {
        var mytitle = document.getElementById("mytitle").value;
    } catch (err) {
        mytitle = "";
    }
    try {
        var mytitle2 = document.getElementById("mytitle2").value;
    } catch (err) {
        mytitle2 = "";
    }
    try {
        var myfirstname = document.getElementById("myfirstname").value;
    } catch (err) {
        myfirstname = "";
    }
    try {
        var myMI = document.getElementById("myMI").value;
    } catch (err) {
        myMI = "";
    }
    try {
        var myname = document.getElementById("myname").value;
    } catch (err) {
        myname = "";
    }
    try {
        var institution = document.getElementById("institution").value;
    } catch (err) {
        institution = "";
    }
    try {
        var instructor = document.getElementById("instructor").value;
    } catch (err) {
        instructor = "";
    }
    try {
        var mydate = document.getElementById("mydate").value;
    } catch (err) {
        mydate = "";
    }
    try {
        var course = document.getElementById("course").value;
    } catch (err) {
        course = "";
    }
    try {
        var authornotetxt = document.getElementById("authornotetxt").value;
    } catch (err) {
        authornotetxt = "";
    }
    if (myMI != "") myMI = " " + myMI;
    switch (styleTitle) {
    case "APA":
        if (authornotetxt != "") authornotetxt = "Author Note<br />" + authornotetxt;
        if (runninghead !== "") {
            // runninghead = "Running head: " + runninghead.toUpperCase() + "<p style=\"float:right\">1</p><br /><br /><br />";
            runninghead = "Running head: " + runninghead.toUpperCase();
        }
        total2 = "<div style=\"padding:5px;width:600px\"><div style='font-size:12px;'><table style='border:1px solid white' width=\"600\"><tr><td style='border:1px solid white'> " + runninghead + "</td><td style='text-align:right;border:1px solid white'>1</td></table><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /></div><div style='text-align: center;line-height:27.5pt'>" + mytitle + "<br />" + myfirstname + myMI + " " + myname + "<br />" + institution + "<br /><br /><br /><br /><br /><br /><br /><br />" + authornotetxt + "</div></div>";
        break;
    case "MLA":
        total2 = "<div style=\"width:600;\"><div style=\"text-align:right\"> " + myname + " 1</div>" + myfirstname + myMI + " " + myname + "<br /><br />" + instructor + "<br /><br />" + course + "<br /><br />" + mydate + "</div><div style='text-align: center'>" + mytitle2 + ":</div>";
        break;
    case "Chicago":
        total2 = "<div style=\"width:600\"><div style='font-size:15px;'><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /></div><div style='text-align: center'>" + mytitle + "<br/><br /><br/><br />" + myfirstname + myMI + " " + myname + "<br /><br /><br /><br /><br /><br /><br /><br />" + course + "<br />" + instructor + "<br />" + mydate + "</div></div>";
        break;

    }

    //updateBox();
} //end function getFields

function onlyNumbers(tmp) {
    if (!isNaN(tmp.value)) {
        sendToBottom();
    } else {
        tmp.value = tmp.value.replace(/[^0-9]/g, '')
        return tmp;
    }
}

function getMonthName(monthnumber) {
    var myMonths = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
    monthlongname = myMonths[monthnumber];
    return monthlongname;
}

function popupIntextWindow(o, d) {
    // o - Object to display.
    // d - Display, true = display, false = hide
    var obj = document.getElementById(o);
    if (d) obj.style.display = 'block';
    else obj.style.display = 'none';
}



function updateCitation(i) { 
	if (editting !== i){
	arrow3 = true;   //stops arrow3 from showing so can quit edit
	popupIntextWindow('arrow3', false);
	var qqq = true;
    if (myeditting === true){ 
	editmade = true;
	 refresh(); //refreshes if in editting mode
	} else {
		if (total !=="") qqq = confirm("You will lose the citation you are entering if you continue. Continue?");}
	if (qqq== true ){
	total ="";	
	myeditting = true;
    if (editting !== "") {
        document.getElementById('ae' + editting).style.border = 'none';
        document.getElementById('ae' + editting).style.padding = '0';
    } 
document.getElementById("newarea").innerHTML = "Editting citation";
            document.getElementById("newarea").className = "newareaedit";
    instuff = citation[i].split("::");

    type = instuff[1];
	medium = instuff[21];
    showInfo(type); //loads empty fields
    
	mytmp = document.getElementById("Step4").getElementsByTagName("input");
    for (var bb = 0; bb < mytmp.length; bb++) {
        if (mytmp[bb].type == "text" && mytmp[bb].value !== "Issue") {
			mytmp[bb].style.color = "#000";
            mytmp[bb].style.backgroundColor = '#DFE8F2';
        }
    }
    authorstuff = instuff[2].split("++"); //qq divides each author
    inauthornum = parseInt(authorstuff[0]);
	var tmpContrib = "";
	editorCount =0;
    for (var u = 1; u <= inauthornum; u++) {
        if (u !== 1) authorbox();
			tmpContrib =authorstuff[u].split("??")[0];
			if (tmpContrib.search("jsj") == 0) {
		tmpContrib= tmpContrib.replace("jsj","");
			}
			if (type == "Book") {
			
			
			document.getElementById('contributor' + u).value = tmpContrib;
        	if (tmpContrib == "Ed") editorCount = editorCount +1;
		}
		document.getElementById('authorFirst' + u).value = authorstuff[u].split("??")[1];
        document.getElementById('MI' + u).value = authorstuff[u].split("??")[2];
        document.getElementById('authorLast' + u).value = authorstuff[u].split("??")[3];
    }
    if (style == "APA") {
        ellipsis = (authorstuff[inauthornum + 1]);
        if (aCount - editorCount == 7 && ellipsis == "true") document.getElementById('ellipsis').checked = true;
    } else if (style == "MLA") {
        etal = (authorstuff[inauthornum + 1]);
        if (aCount - editorCount == 1 && etal == "true") document.getElementById('etal').checked = true;
    }
    if (document.getElementById('ATitle') !== null) {
        document.getElementById('ATitle').value = instuff[3];
    }

    if (document.getElementById('MedTitle') !== null) {
        document.getElementById('MedTitle').value = instuff[4];
    }
    if (document.getElementById('City') !== null) {
        document.getElementById('City').value = instuff[5];
	if (document.getElementById('City').value =="" && type == "Newspaper"){ document.getElementById('City').value = "Only include if city not in the title and is local";  
		document.getElementById('City').style.color = "#767474" }
    }
    if (document.getElementById('Year') !== null) {
        document.getElementById('Year').value = instuff[6];
        if (document.getElementById('Year').value == "" && type !== "Journal" && type !== "Book") {
            document.getElementById('Year').value = "yyyy";
            document.getElementById('Year').style.color = "#767474"
        }
    }
    if (document.getElementById('Publisher') !== null) {
        document.getElementById('Publisher').value = instuff[7];
    }
    if (document.getElementById('State') !== null) {
        document.getElementById('State').value = instuff[8];
    }
    if (document.getElementById('Volume') !== null) {
        document.getElementById('Volume').value = instuff[9];
    }
    if (document.getElementById('Issue') !== null) {
        document.getElementById('Issue').value = instuff[10];
    }
    if (document.getElementById('Edition') !== null) {
        document.getElementById('Edition').value = instuff[11];
    }
    if (document.getElementById('Page') !== null) {
        document.getElementById('Page').value = instuff[12];
    }
    if (document.getElementById('DOI') !== null) {
        document.getElementById('DOI').value = instuff[13];
    }
    if (document.getElementById('URL') !== null) {
        document.getElementById('URL').value = instuff[14]; 
		if (document.getElementById('URL').value =="") document.getElementById('URL').value = "http://";  
    }
    if (document.getElementById('Dbase') !== null) {
        document.getElementById('Dbase').value = instuff[15];
    }
    if (document.getElementById('Day') !== null) {
        document.getElementById('Day').value = instuff[16];

        if (document.getElementById('Day').value == "") {
            document.getElementById('Day').value = "dd";
            document.getElementById('Day').style.color = "#767474"
        }

    }
    if (document.getElementById('Month') !== null) {
        document.getElementById('Month').value = instuff[17];
        document.getElementById('Month').style.backgroundColor = '#DFE8F2';
    }
    if (document.getElementById('Month2') !== null) {
        document.getElementById('Month2').value = instuff[18];
		 document.getElementById('Month2').style.backgroundColor = '#DFE8F2';
    }
    if (document.getElementById('Day2') !== null) {
        document.getElementById('Day2').value = instuff[19];
        if (document.getElementById('Day2').value == "") {
            document.getElementById('Day2').value = "dd";
            document.getElementById('Day2').style.color = "#767474"
        }
    }
    if (document.getElementById('Year2') !== null) {
        document.getElementById('Year2').value = instuff[20];
        if (document.getElementById('Year2').value == "") {
            document.getElementById('Year2').value = "yyyy";
            document.getElementById('Year2').style.color = "#767474"
        }
    }
if (document.getElementById("ae" + justSaved) !== null) {document.getElementById("ae" + justSaved).style.backgroundColor="#FFF";
justSaved ="";}
    document.getElementById('ae' + i).style.backgroundColor = '#DFE8F2';

    document.getElementById('step3legend').innerHTML = 'Step 3: Editting citation';
    if (editting !== "") {
        document.getElementById('ae' + editting).style.backgroundColor = '#FFF';
    }
    //document.getElementById('ae' + i).style.padding = '5px 0 0 8px';
    document.getElementById("myButton").value = 'Quit Edit';

    editting = i;
	 document.getElementById("citationsaved").innerHTML = "";
   	editmade = false; //have not done any edits yet
}
	}
} //end update citation
function readArray() { //this is for after come back to page and for refresh and for copy paste

	if (myrefresh == true || cp == true) k = iii; //k is only k first time hit page
	if (citation[k] !== null) { //should be able to get rid of this and catch sooner
    instuff = citation[k].split("::");
 
    type = instuff[1];
	medium = instuff[21];

    authorstuff = instuff[2].split("++"); //++ divides each author
    inauthornum = authorstuff[0];
    aCount = parseInt(inauthornum);
    editorCount = 0;
    var tmpContrib;
	for (var u = 1; u <= inauthornum; u++) {
        tmpContrib = authorstuff[u].split("??")[0];
		if (tmpContrib.search("jsj") === 0) { 
		citation[k] = citation[k].replace("jsj","")
		createCookie('cite' + k, citation[k], 1);
		justSaved = k;
		}
		contributor[u] = tmpContrib.replace("jsj",""); //might cause problems if not book
        if (contributor[u] == "Ed") editorCount = editorCount + 1;
		authorFirst[u] = authorstuff[u].split("??")[1];
        MI[u] = authorstuff[u].split("??")[2];
        authorLast[u] = authorstuff[u].split("??")[3];
    }
    if (style == "APA") {
        ellipsis = (authorstuff[aCount + 1]);
    } else if (style == "MLA") {
        etal = (authorstuff[aCount + 1]);
    }

    if (style == "APA" && (aCount - editorCount == 7) && ellipsis == "true") {
        ellipsis = true;
    } else if (style == "MLA" && aCount - editorCount == 1 && etal == "true") {
        etal = true;
    }
    authorString = author(contributor, authorLast, authorFirst, MI, etal, ellipsis);

    intextString = authorString.split("::")[1];
    editorString = authorString.split("::")[2];
    authorString = authorString.split("::")[0];

    ATitle = instuff[3];
    MedTitle = instuff[4];
    City = instuff[5];
    Year = instuff[6];
    Publisher = instuff[7];
    State = instuff[8];
    Volume = instuff[9];
    Issue = instuff[10];

    Edition = instuff[11];
    Page = instuff[12];
    DOI = instuff[13];
    URL = instuff[14];
	//if (URL =="") URL = "http://";  
    Dbase = instuff[15];
    Day = instuff[16];
    Month = instuff[17];
    Month2 = instuff[18];
    Day2 = instuff[19];
    Year2 = instuff[20];
    document.getElementById("mytab1").className = "active";
    formatFields();
	 
}} //end readArray

function fnSelect(objId) {
    fnDeSelect();
    if (document.selection) {
        var range = document.body.createTextRange();
        range.moveToElementText(document.getElementById(objId));
        range.select();
    } else if (window.getSelection) {
        var range = document.createRange();
        range.selectNode(document.getElementById(objId));
        window.getSelection().addRange(range);
    }
}

function fnDeSelect() {
	
//if (document.selection) {alert("pop");document.selection.empty();}
    //else
	 if (window.getSelection){ window.getSelection().removeAllRanges();}
}

/***********************************************
 * Disable Text Selection script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
 * This notice MUST stay intact for legal use//
 * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
 ***********************************************/

function disableSelection(target) {
    if (typeof target.onselectstart != "undefined") //IE route
    target.onselectstart = function () {
        return false
    } 
	else if (typeof target.style.MozUserSelect != "undefined") //Firefox route
    target.style.MozUserSelect = "none"
    else //All other route (ie: Opera)
    target.onmousedown = function () {
        return false
    }
    target.style.cursor = "default"
}

function enableSelection(target) {
    if (typeof target.onselectstart != "undefined") //IE route
    target.onselectstart = function () {
        return true;
    } 
	else if (typeof target.style.MozUserSelect != "undefined") //Firefox route
    target.style.MozUserSelect = ""
    else //All other route (ie: Opera)
    target.onmousedown = function () {
        return true;
    }
    target.style.cursor = "default"
}
//Sample usages
//disableSelection(document.body) //Disable text selection on entire body
//disableSelection(document.getElementById("mydiv")) //Disable text selection on element with id="mydiv"
