/*************** APPLY TO SUMMER INTERNSHIP ***************/
function applyToInternships(ContentFor){
	if(typeof ContentFor != 'undefined' && ContentFor == ''){
		alert("Please select who you are before applying online.")
		return
	}

	var choices = ["FirstChoiceID", "SecondChoiceID", "ThirdChoiceID"]
	var query = ''
	for(var i=0;i<3;i++){
		if(internships[i])
			query += ((query != '')?'&':'') + choices[i] + '=' + internships[i].ID
	}
	
	if(query == "")
		query = "FirstChoiceID=" + window.ID
	if(ContentFor)
		query += "&ContentFor=" + ContentFor
	
	
	location = '/mskcc/html/' + InternshipApplyID + '.cfm?' + query
}

/*************** ADD INTERNSHIP ***************/
function setInternshipCookie(reload){
	var choices = []
	for(var i=0;i<3;i++){
		choices[i] = (internships[i])?internships[i].ID:0
	}

	setCookie(InternshipName, choices.join(","))
	if(reload)
		history.go(-1)
}

/*************** ADD INTERNSHIP ***************/
function addInternship(choice){
	internships.lookUpID[window.ID] = internships[choice] = {ID:window.ID, title:window.Title}

	reformatChoices()
	setInternshipCookie( (document.getElementById)?false:true)
}

/*************** REMOVE INTERNSHIP ***************/
function removeInternship(choice){
	internships.lookUpID[internships[choice].ID] = null
	internships[choice] = null

	reformatChoices()
	setInternshipCookie( (document.getElementById)?false:true)
}

/*************** SET CURRENT INTERNSHIP SELECTED ***************/
var SelectedInternship 
function setCurrentInternshipSelected(){
	for(var i=0;i<3;i++){
		if(internships[i])
			if(internships[i].ID == window.ID) return true
	}
	return false
}

/*************** REFORMAT CHOICES ***************/
function reformatChoices(){
	window.SelectedInternship = setCurrentInternshipSelected()

	for(var i=0;i<3;i++){
		if(document.getElementById("Choice" + i)){
			document.getElementById("Choice" + i).innerHTML = formatChoice(i, internships[i])
		}
	}
}

/*************** FORMAT CHOICES ***************/
function formatChoices(){
	window.SelectedInternship = setCurrentInternshipSelected()

	var text = '<OL>'
	for(var i=0;i<InternshipChoices;i++){
		text += '<LI ID="Choice' + i + '" STYLE="margin-bottom:5">' + formatChoice(i, internships[i]) + '</LI>'
	}
	text += '</OL>'
	return text
}

/*************** FORMAT CHOICE ***************/
function formatChoice(choice, fellowship){
	if(fellowship){
		return  ((fellowship.ID == window.ID)?fellowship.title:'<A HREF="' + formatURL(fellowship.ID) + '">' + fellowship.title + '</A>') + 
			'<INPUT TYPE="Button" CLASS="removeButton" NAME="remove" VALUE="Remove" ONCLICK="removeInternship(' + choice + ')">'
	} else if(SelectedInternship) {
		return '<SPAN><B><A HREF="' + formatURL(InternshipFindID) + '">Find an ' + InternshipTitle + '</A></B></SPAN>'
	} else {
		return '<INPUT TYPE="Button" CLASS="addButton" NAME="add" VALUE="Add" ONCLICK="addInternship(' + choice + ')">'
		return '<A HREF="javascript:addInternship(' + choice + ')"><IMG SRC="/mskcc_resources/forms/internships/images/add.gif" ALT="Add \'' + window.Title + '\'" WIDTH="35" HEIGHT="15" BORDER="0" ALIGN="middle"></A>'
	}	

	// format url
	function formatURL(ID){
		if(location.pathname.indexOf('/_temp/') != -1)
			return '/inettool/site/' + window.DisplayType + "?ID=" + ID + "&PageType=" + window.TEMPLATE_TYPE;
		else 
			return '/mskcc/html/' + ID + '.cfm'
	}

}
document.write(formatChoices())

