var listCounters = new Array();
listCounters['academicItems'] = 1;
listCounters['awardItems'] = 1;
listCounters['communityItems'] = 1;
listCounters['workItems'] = 1;
listCounters['extraCurricularItems'] = 1;
var setInitialNumber = 0;
var deleteEntries = "";

function addAwardsSlot(initialNumber){
	addLinkWarning();
	if(initialNumber > listCounters['awardItems'] && setInitialNumber == 0){
		setInitialNumber++
		listCounters['awardItems'] = initialNumber;
	}
	
	listCounters['awardItems']++;
	document.awardsForm.numberOfEntries.value = listCounters['awardItems'];
	var newAwardRow = document.getElementById('awardsList').insertRow(document.getElementById('awardsList').getElementsByTagName("tr").length);
	if(listCounters['awardItems'] % 2 == 0){
		newAwardRow.style.background = "#EAEAEA";
	}
	
	var newAwardNumber = newAwardRow.insertCell(0);
	newAwardNumber.innerHTML = "<a href=\"javascript:deleteEntry('awardsList','"+listCounters['awardItems']+"')\">"+listCounters['awardItems']+".</a>";

	var newAwardGroup = newAwardRow.insertCell(1);
	newAwardGroup.innerHTML = "<input style=\"width:80%\" name=\"group[]\" type=\"text\">";
	
	var newAwardTitle = newAwardRow.insertCell(2);
	newAwardTitle.innerHTML = "<input size=\"10\" name=\"title[]\" type=\"text\">";
	
	var newAwardGrade = newAwardRow.insertCell(3);
	newAwardGrade.innerHTML = "<select name=\"year[]\"><option value=\"1\">--</option><option value=\"9th\">9th</option><option value=\"10th\">10th</option><option value=\"11th\">11th</option><option value=\"12th\">12th</option></select>";
	
	var newAwardDescrip = newAwardRow.insertCell(4);
	newAwardDescrip.innerHTML = "<textarea name=\"desc[]\" style=\"width:90%\" rows=\"3\"></textarea>";
	
}

function addAcademicsSlot(initialNumber){
	addLinkWarning();
	if(initialNumber > listCounters['academicItems'] && setInitialNumber == 0){
		setInitialNumber++
		listCounters['academicItems'] = initialNumber;
	}
	
	listCounters['academicItems']++;
	document.academicsForm.numberOfEntries.value = listCounters['academicItems'];

	var newAcademicRow = document.getElementById('academicsList').insertRow(document.getElementById('academicsList').getElementsByTagName("tr").length);
	if(listCounters['academicItems'] % 2 == 0){
		newAcademicRow.style.background = "#EAEAEA";
	}
	var newAcademicNumber = newAcademicRow.insertCell(0);
	newAcademicNumber.innerHTML = "<a href=\"javascript:deleteEntry('academicsList','"+listCounters['academicItems']+"')\">"+listCounters['academicItems']+".</a>";
	
	var newAcademicTitle = newAcademicRow.insertCell(1);
	newAcademicTitle.innerHTML = "<input size=\"30\" name=\"title[]\" type=\"text\">";
	
	var newAcademicLevel = newAcademicRow.insertCell(2);
	newAcademicLevel.innerHTML = "<select name=\"level[]\"><option value=\"--\">--</option><option value=\"AP\">AP</option><option value=\"Honors\">Honors</option></select>";
	
	var newAcademicGradeTaken = newAcademicRow.insertCell(3);
	newAcademicGradeTaken.innerHTML = "<select style=\"width:100%;\" name=\"grade[]\"><option value=\"--\">--</option><option value=\"9th\">9th</option><option value=\"10th\">10th</option><option value=\"11th\">11th</option><option value=\"12th\">12th</option></select>";
	
	var newAcademicSpacer = newAcademicRow.insertCell(4);
	newAcademicSpacer.innerHTML = "<input name=\"grade_recieved[]\" type=\"text\" id=\"grade_recieved[]\">";
}





function addCommunitySlot(initialNumber){
	addLinkWarning();
	if(initialNumber > listCounters['communityItems'] && setInitialNumber == 0){
		setInitialNumber++
		listCounters['communityItems'] = initialNumber;
	}
	
	listCounters['communityItems']++;
	document.commForm.numberOfEntries.value = listCounters['communityItems'];

	var newCommRow = document.getElementById('communitysList').insertRow(document.getElementById('communitysList').getElementsByTagName("tr").length);
	if(listCounters['communityItems'] % 2 == 0){
		newCommRow.style.background = "#EAEAEA";
	}
	var newCommNumber = newCommRow.insertCell(0);
	newCommNumber.innerHTML = "<a href=\"javascript:deleteEntry('communitysList','"+listCounters['communityItems']+"')\">"+listCounters['communityItems']+".</a>";
	
	var newCommGroup = newCommRow.insertCell(1);
	newCommGroup.innerHTML = "<input name=\"org[]\" type=\"text\" size=\"15\">";
	
	var newCommActivity = newCommRow.insertCell(2);
	newCommActivity.innerHTML = "<input size=\"15\" name=\"activity[]\" type=\"text\">";
	
	var newCommGrade = newCommRow.insertCell(3);
	newCommGrade.innerHTML = "<input type=\"checkbox\" class=\"checkbox\" name=\"grades["+(listCounters['communityItems']-1)+"][]\" value=\"9th\">9th<br><input type=\"checkbox\" class=\"checkbox\" name=\"grades["+(listCounters['communityItems']-1)+"][]\" value=\"10th\">10th<br><input type=\"checkbox\" class=\"checkbox\" name=\"grades["+(listCounters['communityItems']-1)+"][]\" value=\"11th\">11th<br><input type=\"checkbox\" class=\"checkbox\" name=\"grades["+(listCounters['communityItems']-1)+"][]\" value=\"12th\">12th";
	
	var newTimeSpent = newCommRow.insertCell(4);
	newTimeSpent.innerHTML = "<input name=\"time_amt[]\" type=\"text\" size=\"2\"><br><select name=\"time_units[]\"><option value=\"--\">--</option><option value=\"hrs\">hrs</option><option value=\"hrs/week\">hrs/week</option><option value=\"hrs/month\">hrs/month</option><option value=\"hrs/year\">hrs/year</option></select>";
	
	var newCommDescrip = newCommRow.insertCell(5);
	newCommDescrip.innerHTML = "<textarea name=\"desc[]\" cols=\"23\" rows=\"5\"></textarea>";
}

function addWorkSlot(initialNumber){
	addLinkWarning();
	if(initialNumber > listCounters['workItems'] && setInitialNumber == 0){
		setInitialNumber++
		listCounters['workItems'] = initialNumber;
	}
	
	listCounters['workItems']++;
	document.workForm.numberOfEntries.value = listCounters['workItems'];
	
	var newWorkRow = document.getElementById('worksList').insertRow(document.getElementById('worksList').getElementsByTagName("tr").length);
	if(listCounters['workItems'] % 2 == 0){
		newWorkRow.style.background = "#EAEAEA";
	}
	var newWorkNumber = newWorkRow.insertCell(0);
	newWorkNumber.innerHTML = "<a href=\"javascript:deleteEntry('worksList','"+listCounters['workItems']+"')\">"+listCounters['workItems']+".</a>";
	
	var newWorkEmployer = newWorkRow.insertCell(1);
	newWorkEmployer.innerHTML = "<input name=\"employer[]\" type=\"text\" id=\"workEmployer\" size=\"20\">";
	
	var newWorkStart = newWorkRow.insertCell(2);
	newWorkStart.innerHTML = "<select name=\"smonth[]\"><option value=\"--\">--</option><option value=\"Jan\">Jan</option><option value=\"Feb\">Feb</option><option value=\"Mar\">Mar</option><option value=\"April\">April</option><option value=\"May\">May</option><option value=\"June\">June</option><option value=\"July\">July</option><option value=\"Aug\">Aug</option><option value=\"Sept\">Sept</option><option value=\"Oct\">Oct</option><option value=\"Nov\">Nov</option><option value=\"Dec\">Dec</option></select><select name=\"syear[]\"><option value=\"--\">--</option><option value=\"1995\">1995</option><option value=\"1996\">1996</option><option value=\"1997\">1997</option><option value=\"1998\">1998</option><option value=\"1999\">1999</option><option value=\"2000\">2000</option><option value=\"2001\">2001</option><option value=\"2002\">2002</option><option value=\"2003\">2003</option><option value=\"2004\">2004</option><option value=\"2005\">2005</option></select>";
	
	var newWorkEnd = newWorkRow.insertCell(3);
	newWorkEnd.innerHTML =   "<select name=\"emonth[]\"><option value=\"--\">--</option><option value=\"Jan\">Jan</option><option value=\"Feb\">Feb</option><option value=\"Mar\">Mar</option><option value=\"April\">April</option><option value=\"May\">May</option><option value=\"June\">June</option><option value=\"July\">July</option><option value=\"Aug\">Aug</option><option value=\"Sept\">Sept</option><option value=\"Oct\">Oct</option><option value=\"Nov\">Nov</option><option value=\"Dec\">Dec</option></select><select name=\"eyear[]\"><option value=\"--\">--</option><option value=\"1995\">1995</option><option value=\"1996\">1996</option><option value=\"1997\">1997</option><option value=\"1998\">1998</option><option value=\"1999\">1999</option><option value=\"2000\">2000</option><option value=\"2001\">2001</option><option value=\"2002\">2002</option><option value=\"2003\">2003</option><option value=\"2004\">2004</option><option value=\"2005\">2005</option></select>";

	var newWorkDescrip = newWorkRow.insertCell(4);
	newWorkDescrip.innerHTML = "<textarea name=\"desc[]\" cols=\"18\" rows=\"3\"></textarea>";
}

function addExtraCurricularSlot(initialNumber){
	addLinkWarning();
	if(initialNumber > listCounters['extraCurricularItems'] && setInitialNumber == 0){
		setInitialNumber++
		listCounters['extraCurricularItems'] = initialNumber;
	}
	
	listCounters['extraCurricularItems']++;
	document.extraForm.numberOfEntries.value = listCounters['extraCurricularItems'];

	var newExtraRow = document.getElementById('extraCurricularsList').insertRow(document.getElementById('extraCurricularsList').getElementsByTagName("tr").length);
	if(listCounters['extraCurricularItems'] % 2 == 0){
		newExtraRow.style.background = "#EAEAEA";
	}
	var newExtraNumber = newExtraRow.insertCell(0);
	newExtraNumber.innerHTML = "<a href=\"javascript:deleteEntry('extraCurricularsList','"+listCounters['extraCurricularItems']+"')\">"+listCounters['extraCurricularItems']+".</a>";
	
	var newExtraGroup = newExtraRow.insertCell(1);
	newExtraGroup.innerHTML = "<input style=\"width:95%\" name=\"group[]\" type=\"text\">";
	
	var newExtraGrade = newExtraRow.insertCell(2);
	newExtraGrade.innerHTML = "<input name=\"grades["+(listCounters['extraCurricularItems']-1)+"][]\" type=\"checkbox\" class=\"checkbox\" value=\"9th\"> 9th <input name=\"grades["+(listCounters['extraCurricularItems']-1)+"][]\" type=\"checkbox\" class=\"checkbox\" value=\"10th\"> 10th <input name=\"grades["+(listCounters['extraCurricularItems']-1)+"][]\" type=\"checkbox\" class=\"checkbox\" value=\"11th\"> 11th <input name=\"grades["+(listCounters['extraCurricularItems']-1)+"][]\" type=\"checkbox\" class=\"checkbox\" value=\"12th\"> 12th";

	var newExtraPosition = newExtraRow.insertCell(3);
	newExtraPosition.innerHTML = "<input name=\"position[]\" type=\"text\" id=\"extraPosition\">";
}

function deleteSlot(slotId,formId){	
	var itemId = slotId.replace('sList','Items');
	var type = slotId.replace('sList','');
		var findRows = document.getElementById(slotId).getElementsByTagName("tr");
		if(findRows.length > 2){
			var lastRowCourseTitle = findRows[findRows.length-1].getElementsByTagName("input");
			if((lastRowCourseTitle[0].value) != ""){
				alert("There are no more empty slots to delete. \r\n To delete an existing entry, click its number to the left.");
			}
			else{
				document.getElementById(slotId).deleteRow(findRows.length-1);
				listCounters[itemId]--;
				document.forms[formId].numberOfEntries.value--;
			}
			/*
			if((lastRowCourseTitle[0].value) != ""){
				var user_response = confirm("Are you sure you want to delete "+type+" entry: \n\n" + lastRowCourseTitle[0].value + "  ?\n\nThis action is not reversable");
				if(user_response == true){
					document.getElementById(slotId).deleteRow(findRows.length-1);
					listCounters[itemId]--;
				}
			}
			else{
				document.getElementById(slotId).deleteRow(findRows.length-1);
				listCounters[itemId]--;
			}*/
		}
	
}

function deleteEntry(slotId,val){
var findRows = document.getElementById(slotId).getElementsByTagName("tr");
var findCells = findRows[val].getElementsByTagName("td");
var checkEmpty = findRows[val].getElementsByTagName("input");
if(checkEmpty[0].value != ""){
addLinkWarning();
var numberEntries = document.getElementsByName('numberOfEntries');
if(deleteEntries.indexOf('@'+val+'@') > 0){
	deleteEntrieis = deleteEntries.replace('@'+val+'@','');
	numberEntries[0].value++;
} else {
	deleteEntries = deleteEntries+'@'+val+'@';
	numberEntries[0].value--;
}
		
		for(var i=1;i<findCells.length;i++){
			if(findCells[i].className == 'entryMarkedAsDelete'){
				findCells[i].className = null;
			} else {
				findCells[i].className = 'entryMarkedAsDelete';
			}
		}

		
		var getTextInputs = findRows[val].getElementsByTagName("input");
		for(var i=0;i<getTextInputs.length;i++){
			if(getTextInputs[i].disabled == true){
				getTextInputs[i].disabled = false;
			} else {
				getTextInputs[i].disabled = true;
			}
		}
		
		var selectInputs = findRows[val].getElementsByTagName("select");
		for(var i=0;i<selectInputs.length;i++){
			if(selectInputs[i].disabled == true){
				selectInputs[i].disabled = false;
			} else {
				selectInputs[i].disabled = true;
			}
		}
		
		var textAreaInputs = findRows[val].getElementsByTagName("textarea");
		for(var i=0;i<textAreaInputs.length;i++){
			if(textAreaInputs[i].disabled == true){
				textAreaInputs[i].disabled = false;
			} else {
				textAreaInputs[i].disabled = true;
			}
		}
} else {
	alert("Sorry, this entry is empty. Use the delete slot link instead.");
}
}

function addEntrySlottest() {
  var div=document.getElementById("academicListEntries");
  var button=document.getElementById("addEntry");
  awardItems++;
	newitem ="text"

  var newnode=document.createElement("div");
  newnode.innerHTML=newitem;
  div.insertBefore(newnode,button);
}
function addOnChangeWarning(){
	var forms = document.forms;
	var formElements = forms[1].elements;
	for(var i=0;i<formElements.length;i++){
		formElements[i].onchange = function (){
			addLinkWarning();
		}
	}
}

function addLinkWarning(){
	var links = document.getElementsByTagName("a");
	for(var i=0;i<links.length;i++){
		if(links[i].name != "skip"){
			links[i].onclick = function (){
				var check = confirm("Before you leave this page, please make sure to save your info. Would you like to continue without saving?");
				if(check == false){
					return false;
				} else {
					return true;
				}
			}
		}
	}
}

function example(link){
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open('/component/example.php?id="+link+"', '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=585,height=400,left = 10,top = 10');");
}