var editorTotal = "";
var intextAuthorTotal = "";
var intextAuthor = new Array();
var authorFirst = new Array();
var authorLast = new Array();
var contributor = new Array();
var MI = new Array();
var authorFirstIn = new Array();
var authorLastIn = new Array();
var MIIn = new Array();
var contributorIn = new Array();
var authorTotal = "";

function clearDefault(e1) {
    if (e1.defaultValue == e1.value) {
        e1.value = "";
    }
    e1.style.color = 'black';
}

function replaceDefault(e1) {
    if (e1.value === "") {
        e1.value = e1.defaultValue1.style.color = '#ccc';
    }
}

function author(contributor, authorLast, authorFirst, MI, etal, ellipsis) {
	authorTotal = "";
    editorTotal = "";
    intextAuthorTotal = "";
    
    editor = false;
    myauthor = false;
    s = "";
    var anumb = 0;
    var enumb = 0;
    j = 0;
	
    for (var i = 1; i <= aCount; i++) {
      
        if (contributor[i] == "Ed") {
          
		    editor = true;
        };
        if (contributor[i] == "Au") {
            myauthor = true;
        };
         intextAuthor[i] = "";
        intextAuthor[i + aCount] = "";
        if (authorFirst[i] === undefined) authorFirst[i] = "";
      // authorFirstIn[i]= authorFirst[i];
	    if (authorFirst[i + aCount] === undefined) authorFirst[i + aCount] = "";
       // authorFirstIn[i + aCount]= authorFirst[i+ aCount];
		if (authorLast[i] === undefined) authorLast[i] = "";
		authorLastIn[i]= authorLast[i];
        if (authorLast[i] === undefined) authorLast[i + aCount] = "";
		//authorLastIn[i]= authorLast[i];
        if (MI[i] === undefined) MI[i] = "";
        if (MI[i] === undefined) MI[i + aCount] = "";


    }
    authorLast[aCount + 1] = "";
  
    if (myauthor == true && editor === true) enumb = aCount;
    for (var i = 1; i <= aCount; i++) {
        tmp = contributor[i];
        j = i;

		if (type !== "Book"  || tmp == "Au"  || (tmp =="Ed" && myauthor == false)) {
            anumb = anumb + 1;
            j = anumb;
        } else {
            enumb = parseInt(enumb) + 1;
            j = enumb;
			
        }
        if (tmp == "Au" || tmp == "Ed") {
            contributor[j] = tmp;
        }
   
        if (MI[i] !== "" && MI[i] !== "MI") {
            MI[j] = " " + MI[i].substring(0, 2).toUpperCase() + ".";
        } else {
            MI[j] = "";
        }
        if (authorFirst[i] === "First Name" || authorFirst[i] == "") {
            authorFirst[j] = "";
        } else {
            authorFirst[j] = authorFirst[i].charAt(0).toUpperCase() + authorFirst[i].substring(1, authorFirst[i].length);
        }
        if (authorLast[i] === "Last Name" || authorLast[i] == "") {;
            authorLast[j] = "";
        } else {
            authorLast[j] = authorLast[i].charAt(0).toUpperCase() + authorLast[i].substring(1, authorLast[i].length);
        }
    }
    if (anumb == 0) anumb = aCount;
    if ((myauthor != true && anumb > 1) || (editor == true && enumb - aCount > 1)) {
        s = "s";
    }


    if (style == 'APA') {
        for (var i = 1; i <= anumb; i++) {
            if (authorFirst[i] !== "") {
                authorFirst[i] = authorFirst[i].charAt(0) + ".";
				
            }
		}
		
        for (var i = 1 + aCount; i <= enumb; i++) { //alert("ineditor");
             if (authorFirst[i] !== "") {
                 authorFirst[i] = authorFirst[i].charAt(0) + ".";
				 
             }
        }
        for (var i = 2; i <= anumb; i++) {
			
            if (type != "Book" || contributor[i] == "Au" || (contributor[i] == "Ed" && myauthor == false)) {
                if (aCount <= 6) {

                    if ((authorLast[i + 1] == "" || i == anumb) && authorLast[i] !== "") {
                        authorLast[i] = " " + "&" + " " + authorLast[i];
                    }
                }
            }
		
			if (i==7) {
				if (ellipsis == true) {
					if (authorLast[7] !== "") {
						authorLast[7] = " " + " . . . " + " " + authorLast[7];
					}
				} else {
					
					if (authorLast[7] !== "") {
						authorLast[7] = " " + "&" + " " + authorLast[7];
					}
				}
			}
		
		}
		if (authorFirst[1] !== "" && authorLast[1] !== "") {
            authorFirst[1] = ", " + authorFirst[1];
            
        } //ands on comma
        for (var i = 2; i <= anumb; i++) {
            if (authorFirst[i] !== "" && authorLast[i] !== "") {
                authorFirst[i] = ", " + authorFirst[i];
            }
            //adds on period
            if (authorLast[i] !== "") {
                authorLast[i] = "," + " " + authorLast[i];
            }
            //adds comma
        }
    for (var i = 1; i <= anumb; i++) {
		authorTotal = authorTotal + authorLast[i] + authorFirst[i] + MI[i];
       
	   if (anumb < 6 || i == 1) intextAuthorTotal = intextAuthorTotal + authorLast[i];
    }
    if (anumb >= 6){intextAuthorTotal = intextAuthorTotal + " et al.";}
	if (anumb ==2){intextAuthorTotal= intextAuthorTotal.replace(", "," ");}
	if (editor == true && myauthor == false  && authorTotal !="") {
	    authorTotal = authorTotal + " (Ed" + s + ".). ";
		
    }
  
   //APA editor information
    if (editor == true && myauthor == true) {
		
        for (var i = 1 + aCount; i <= enumb; i++) {
			
            if (enumb - aCount > 1) { 
                if ((authorLast[i + 1] === ""  || authorLast[i + 1] == undefined || i==enumb) && authorLast[i] !== "") {
               		if (authorFirst[i] === "")	authorLast[i] = ", & " +  authorLast[i];
					else     	authorFirst[i] = ", & " + authorFirst[i];
                } else{
           	
			 if (authorFirst[i] === ""  && authorLast[i] != "" && i > (1+ aCount)){
		
				authorLast[i] = ", " + authorLast[i];}
				else if (authorFirst[i] !== "" && i > (1+ aCount))authorFirst[i] = ", " + authorFirst[i];
			 }
			}
		 	
				editorTotal = editorTotal + authorFirst[i] + " " + MI[i] + " " + authorLast[i];
        }
		if (editorTotal.search(/\w/) >= 0)
{
       if (ATitle == "")
	    editorTotal = editorTotal + " (Ed" + s + ".). ";
		else editorTotal = editorTotal + " (Ed" + s + ".), ";
		}
    }}
 
    if (style == 'MLA' || style == 'Chicago') {
		if ((authorLast[2] === "" || authorLast[2] == "Last Name") && etal !== true) {
           // MI[1] = MI[1].substring(0, 2);alert(MI[1]); this was removing period for only editor MLA
        }
        if (authorFirst[1] !== "" && authorLast[1] !== "") {
			authorFirst[1] = ", " + authorFirst[1];
        }
        //ands on comma
        for (var i = 2; i <= anumb; i++) {
            if (i==anumb || (authorLast[i + 1] === "" && authorLast[i] !== "")) {
                
				authorFirst[i] = "and" + " " + authorFirst[i];
                intextAuthor[i] = " and" + " " + authorLast[i];
				
            }
            if (authorLast[i] !== "") {
                authorLast[i] = " " + authorLast[i];
                if (authorLast[i + 1] != ""  && i < anumb) intextAuthor[i] = "," + authorLast[i];
				            }
            if (authorLast[i] !== "" && authorFirst[i] === "") {
                authorLast[i] = "," + authorLast[i];
            } 
            if (authorFirst[i] !== "") {
			    authorFirst[i] = ", " + authorFirst[i];
            }
            //ands on comma
        }

        authorTotal = authorLast[1] + authorFirst[1] + MI[1];
        intextAuthorTotal = authorLast[1];
        for (var i = 2; i <= anumb; i++) {
            authorTotal = authorTotal + authorFirst[i] + MI[i] + authorLast[i];
            intextAuthorTotal = intextAuthorTotal + intextAuthor[i];
        }
        if (etal === true) {
            authorTotal = authorTotal + ", et al.";
			intextAuthorTotal = authorLast[1] + " et al.";
        } else {
            
			if (authorTotal !== "") {
                if (myauthor !== true && editor == true) {
                    authorTotal = authorTotal + ", ed" + s;
                }
                authorTotal = authorTotal + ". ";
				authorTotal =authorTotal.replace("..",".");
            }
        }
  
	//MLA & Chicago editor info
	if (editor == true && myauthor == true){
		i= 1 + aCount;
		if (authorLast[i] !== ""  || authorFirst[i] !== "") editorTotal = editorTotal + authorFirst[i] + " " + MI[i] + " " + authorLast[i];
		for (var i = 2 + aCount; i <= enumb; i++) {
            if (( i==enumb || authorLast[i + 1] === ""|| authorLast[i + 1]===undefined) && authorLast[i] !== "") {
                if (authorFirst[i] === "")	authorLast[i] = " " + "and" + " " +  authorLast[i];
					else     	authorFirst[i] = " " + "and" + " " + authorFirst[i];}
            else {
				if (authorLast[i] !== "" && authorFirst[i] === "" ) {
                authorLast[i] = ", " + authorLast[i];
            	}else if (authorFirst[i] !== "" )authorFirst[i] = ", " + authorFirst[i];
           	 
			}
           
			
           editorTotal = editorTotal + authorFirst[i] + " " + MI[i] + " " + authorLast[i];
        }
	}
		
		if (editorTotal.search(/\w/) >= 0)
		
	    if (editor == true && myauthor == true) {
           if (editorTotal.search(/\w/) >= 0)
		    if (style == "MLA") {
                editorTotal = "Ed. " + editorTotal + ". ";
            } else {
                if (style == "Chicago") {
                    editorTotal = "edited by " + editorTotal;
                }
            }
        }
    }
	
    authorTotal = authorTotal + "::" + intextAuthorTotal + "::" + editorTotal;
    saveaCount = aCount;
	
	return authorTotal;
}
//end function

