// JavaScript Document

function Profile(firstName, lastName, category, image, title, email, phone, office, website, descript) {
	     this.firstName = firstName;
         this.lastName = lastName;
         this.category = category;  // key attribute
         this.image = image;
         this.title = title;
         this.email = email;
         this.phone = phone;
         this.office = office;
		 this.website = website;
         this.descript = descript;  
}

function makeDirectory(pplArray, srcFolder, thead) {
	         
		 document.writeln('<table class="cal" border="0" cellpadding="4" cellspacing="3" style="position: relative; left: 0px; top: 0px;">');   

		 drawProfiles(pplArray, srcFolder, thead); 
	         
		 document.writeln('</table>');
		 document.writeln('<p align="right"><a href="#">Top</a></p>');
		 document.writeln('<p>&nbsp;</p>');
		 
}

// function that controls the HTML layout of Faculty Page

function drawProfiles(pplArray, srcFolder, thead) {
         document.writeln('<tr><th colspan="2" class="section">' + '<a name="' + pplArray[0].category + '">' + '</a>' + thead + '</th></tr>');
		 createHeadings();
	
	     for (var i = 0; i < pplArray.length; i++) {
             document.writeln('<tr><td colspan="2" class="name">' + '<a class="target"' + 'name="' + pplArray[i].firstName.charAt(0).toLowerCase() + pplArray[i].lastName.toLowerCase() +'">' + '</a>' + pplArray[i].firstName + " " + pplArray[i].lastName + '</td></tr>'); 
			 document.writeln('<tr>');
			 document.writeln('<td class="title" rowspan="1" width="35%">' + pplArray[i].title + '</td>');
		     document.writeln('<td class="descript" rowspan="2" width="65%">' + pplArray[i].descript + '</td>');
	         document.writeln('</tr>');
			 document.writeln('<tr><td class="contact" rowspan="1">');
			 
			 
			 if (pplArray[i].image != "") { document.writeln('<img style="margin-bottom: 20px;" alt="' + pplArray[i].firstName + " " + pplArray[i].lastName + '"' + 'src="' + srcFolder + pplArray[i].image + '"' + ' />'); }
			 if (pplArray[i].email != "") { document.writeln('<p><a href="mailto:' + pplArray[i].email + '">' + pplArray[i].email + '</a><br />'); }
             if (pplArray[i].phone != "") { document.writeln(pplArray[i].phone + '<br />'); }
			 if (pplArray[i].office != "") { document.writeln(pplArray[i].office); }

			 if (pplArray[i].website != "") { document.writeln('</p>' + '<p><a href="' + pplArray[i].website + '"' + 'target="_blank" title="go to website"' + '>' + 'website' + '</a></p>'); }
			 
			 
			 document.writeln('</td></tr>'); 
		 }
}

function createHeadings() {
         document.writeln('<tr>');
           document.writeln('<th>' + 'profile' + '</th>');
           document.writeln('<th>' + 'about' + '</th>');
         document.writeln('</tr>');
}

function getEmailString(email) {
		     
			 var ccrmaSuffix = email.search(/@ccrma\.stanford\.edu/);
			 var stanfordSuffix = email.search(/@stanford\.edu/);
			 var alumniSuffix = email.search(/@stanfordalumni\.org/);

			 if (ccrmaSuffix > 0) {
				 return (email.replace(/@ccrma\.stanford\.edu/, "@ccrma"));
			 } else if (stanfordSuffix > 0) {
		         return (email.replace(/@stanford\.edu/, "@stanford"));
			 } else if (alumniSuffix > 0) {
		         return (email.replace(/@stanfordalumni\.org/, "@stanfordalumni"));
			 } else { 
			     return (email);
			 }
		
	}


/* This function formats a table for an alphabetically sorted faculty listing   */

function createAlphaDirectory(program, description) {
	         
	     document.writeln('<table cellspacing="0" align="center">');
		 document.writeln('<tr>');
		 document.writeln('<th>name | email</th>' + '<th>title</th>' + '<th>phone</th>' + '<th>office</th>');
         document.writeln('</tr>'); 
			 
	     for (var i = 0; i < program.length; i++) {
              if (i % 2 == 0) { document.writeln('<tr style="background-color: #D7C39A;">'); } 
			  else { document.writeln('<tr style="background-color: #EBE6D2;">'); }
				 
			  document.writeln('<td>' + '<a style="border: 0px;" href="faculty.html#' + program[i].firstName.charAt(0).toLowerCase() + program[i].lastName.toLowerCase() + '">' + program[i].firstName + ' ' + program[i].lastName + '<br />');
			  
			     (program[i].email != "") ? document.writeln('<a style="border: 0px; font-weight: normal;" href="mailto:' + program[i].email + '"' + ' title="' + program[i].email + '">' + getEmailString(program[i].email) + '</a>') : document.write(' ');

			  document.writeln('</td>');
			  document.writeln('<td>' + program[i].title + '</td>');
				 
			  document.writeln('<td>');
				 (program[i].phone != "") ? document.write(program[i].phone) : document.write('&nbsp;');
			  document.writeln('</td><td>');
				 (program[i].office != "") ? document.write(program[i].office) : document.write('&nbsp;');
			  document.writeln('</td>');
			  document.writeln('</tr>')	 
		}
	    document.writeln('</table>');	
	}


