var saveModifiedData = "";
var safeClose = false;

function addEducation()
{
	var schoolName = "";
	var schoolId = "";
	//get school name and id
	//if anything is in the school name text box then use that, otherwise use the drop down list
//	if(document.getElementById('txtSchoolName').value != "")
//	{
//		schoolName = document.getElementById('txtSchoolName').value;
//		schoolId = -1;
//	}
//	else
//	{
		var selSchool = document.getElementById('selSchool');
		var arr = selSchool.options;        
	
		var selectedIndex = selSchool.selectedIndex;
		var out = "";
		
		schoolName = arr[selectedIndex].text;
	    schoolId = arr[selectedIndex].value;
	//}
	
	var degreeName = "";
	var degreeId = "";
	
	//same goes for degree
//	if(document.getElementById('txtDegreeName').value != "")
//	{
//		degreeName = document.getElementById('txtDegreeName').value;
//		degreeId = -1;
//	}
//	else
//	{
		var selDeg = document.getElementById('selDegree');
		var arr = selDeg.options;        
	
		var selectedIndex = selDeg.selectedIndex;
	
	    degreeName = arr[selectedIndex].text;
	    degreeId = arr[selectedIndex].value;
//	}

	var selYears = document.getElementById('years');
	var arr = selYears.options;        
	
	var selectedIndex = selYears.selectedIndex;
	
    var year = arr[selectedIndex].value;
	var honors = document.getElementById('txtHonors').value;
	
	var bIsLawSchool = document.getElementById('rdLawSchool').checked;
	
	var IsLawSchool;
	if(bIsLawSchool)
		IsLawSchool = 1;
	else
		IsLawSchool = 0;
	
	var strAlert = schoolName + " | " + schoolId + "\n";
	strAlert += degreeName + " | " + degreeId + "\n";
	strAlert += year + "\n";
	strAlert += honors + "\n";
	strAlert += IsLawSchool;
	
	
	var myEducation = document.getElementById('eduVals');
	var eduVals = myEducation.value.split(';');
	
	var bAdd = true;
	//check to make sure we're not adding an already existing item
	for(var i=0; i < eduVals.length; i++)
	{
			var arrCurDetails = eduVals[i].split('|');
			var _schoolName = 0;
			var _schoolId = 1;
			var _degreeName = 2;
			var _degreeId = 3;
			var _year = 4;
			var _honors = 5;
			var _IsLawSchool = 6;
			
			if(schoolName != arrCurDetails[_schoolName] || 
				degreeName != arrCurDetails[_degreeName] || 
				year != arrCurDetails[_year])
				{
					bAdd = true;
				}
				else
				{
					bAdd = false;
					break;
				}
			
	}
	
	var strAlert = "Error:";
	
	if(schoolName == "Select One" || degreeName == "Select One")
	{
		bAdd = false;
		strAlert += " Please provide a school and degree.";
	}
	else
	{
		strAlert += "This item appears to already exist.";
	}
	
	if(bAdd){
		myEducation.value += schoolName + "|" + schoolId + "|" + degreeName + "|" + degreeId + "|" + year + "|" + honors + "|" + IsLawSchool + "|;"
	
		buildEducationTable(true);
	}
	else alert(strAlert);
	
	//reset the dropdowns to the default values
	ResetDropDown(document.frmEducation.selSchool);
	ResetDropDown(document.frmEducation.selDegree);
	ResetDropDown(document.frmEducation.years);
	document.frmEducation.txtDegreeName.value = "";
	document.frmEducation.txtSchoolName.value = "";
	document.frmEducation.rdLawSchool[1].checked = true;
	document.frmEducation.txtHonors.value = "";
}

function ResetDropDown(sel)
{
	for (i=0; i<sel.length; i++) 
	{
		if (sel.options[i].value == "-1")
			sel.options[i].selected = true;
		else if (sel.options[i].value == "1920")
			sel.options[i].selected = true;
		else
			sel.options[i].selected = false;
	}
}

function buildEducationTable(bShowDel)
{
	var myTable = document.getElementById('tblEducation');

	if (document.getElementById('eduVals'))
	{
		var myEducation = document.getElementById('eduVals').value;
		
		var arrEducation = myEducation.split(';');
		
		if(arrEducation.length > 0 && arrEducation[0] != "")
		{
			var aryBGColors = new Array(2);
			aryBGColors[0] = "dddddd";
			aryBGColors[1] = "eeeeee";
			var strTable = '<table class="bioFormLong" style="width:100%;" id="tblEducation"><tr><td><strong>School</strong></td><td><strong>Degree</strong></td><td><strong>Year</strong></td><td><strong>Law&nbsp;School?</strong></td></tr>';

			for(var i= 0; i < arrEducation.length; i++)
			{
				var arrCurDetails = arrEducation[i].split('|');
				if(arrCurDetails.length > 0)
				{
					//0 = school name, 1=schoolid (-1 if added by user), 2=degree name, 3=deg id (-1 if added by user) , 4=Year, 5=honors, 6=IsLawSchool
					var schoolName = 0;
					var schoolId = 1;
					var degreeName = 2;
					var degreeId = 3;
					var year = 4;
					var honors = 5;
					var IsLawSchool = 6;
					
					if(arrCurDetails[schoolName] != "" && arrCurDetails[degreeName] != "")
					{
						var strIsLawSchool = "";
						if(arrCurDetails[IsLawSchool] == "1")
						{
							strIsLawSchool = "Yes";
						}
						else strIsLawSchool = "No";
						
						strTable += "<tr bgcolor="+aryBGColors[i%2]+"><td>" + unescape(arrCurDetails[schoolName]) + "</td>";
						strTable += "<td>" + unescape(arrCurDetails[degreeName]) + "</td>";
						strTable += "<td>" + arrCurDetails[year] + "</td>";
						strTable += "<td>" + strIsLawSchool + "</td>";
						//key is schoolname|year
						if(bShowDel)
						{
							strTable += '<td><a href="#" onclick=' + '"' + "delItem('" + escape(arrCurDetails[schoolName]) + "|" + escape(arrCurDetails[degreeName]) + "|" + arrCurDetails[year] + "'" + ', true);return false;">Delete</a></td>';
							strTable += '<td><a href="#" onclick=' + '"' + "modItem('" + escape(arrCurDetails[schoolName]) + "', '" + escape(arrCurDetails[degreeName]) + "', '" +  escape(arrCurDetails[year]) + "', '" + escape(arrCurDetails[IsLawSchool]) + "', '" + escape(arrCurDetails[honors]) + "'" + ');return false;">Modify</a></td></tr>';
						}
						else
						{
							strTable += '</tr>';
						}
					}
				}
			}
		
			strTable += "</table>";
			myTable.style.display = "";
			myTable.innerHTML = strTable;	
		}
		else
		{
			myTable.style.display = "";
			myTable.innerHTML = "No education details selected.";
		}
	}
}

function modItem(schoolName, degreeName, year, lawSchool, honors)
{
//	alert(schoolName + "\n" + degreeName + "\n" + year + "\n" + lawSchool + "\n" + honors);
	
	var bFoundSchool = selectItemByText('selSchool', unescape(schoolName));
	var bFoundDegree = selectItemByText('selDegree', unescape(degreeName));
	
	//set custom boxes, but this shouldn't ever happen since we can only be here the first
	//time we edit someone and they can't have custom education at this point
	if(!bFoundSchool)
	{
		document.getElementById('txtSchoolName').value = unescape(schoolName);
	}
	
	if(!bFoundDegree)
	{
		document.getElementById('txtDegreeName').value = unescape(degreeName);
	}
	
	selectItemByText('years', unescape(year));
	document.getElementById('txtHonors').value = unescape(honors);
	
	var rdLawSchool = document.frmEducation.rdLawSchool;
	//var rdLawSchool = document.getElementById('rdLawSchool');
	
	if(lawSchool == '0')
	{
		rdLawSchool[0].checked = false;
		rdLawSchool[1].checked = true;   
	}
	else
	{
		rdLawSchool[0].checked = true;
		rdLawSchool[1].checked = false;  
	}
	
	saveModifiedData = schoolName + "|" + degreeName + "|" + year;

	//Show the modify buttons
	(document.getElementById("btnUpdate")).style.display="";
	(document.getElementById("btnCancelMod")).style.display="";
	(document.getElementById("btnAdd")).style.display="none";
}

function updateEducation()
{
	if (saveModifiedData != "") //check to make sure before we delete
		delItem(saveModifiedData, false);

	addEducation();

	//reset the modify buttons
	saveModifiedData = "";
	(document.getElementById("btnUpdate")).style.display="none";
	(document.getElementById("btnCancelMod")).style.display="none";
	(document.getElementById("btnAdd")).style.display="";
}

function cancelUpdateEducation()
{
	//reset everything
	saveModifiedData = "";
	
	(document.getElementById("btnUpdate")).style.display="none";
	(document.getElementById("btnCancelMod")).style.display="none";
	(document.getElementById("btnAdd")).style.display="";

	ResetDropDown(document.frmEducation.selSchool);
	ResetDropDown(document.frmEducation.selDegree);
	ResetDropDown(document.frmEducation.years);
	document.frmEducation.rdLawSchool[1].checked = true;
	document.frmEducation.txtHonors.value = "";
	document.frmEducation.txtDegreeName.value = "";
	document.frmEducation.txtSchoolName.value = "";
}

function EducationBeforeUnload()
{
	if (!safeClose)
	{
		if (document.frmEducation.eduVals.value == "")
			alert("Education Details are required to save your information. Any changes you may have made to this form have not been saved.\n"+
				"To save your changes, reopen the Education Details form and use the \"Save Education Details and Close\" button.");
		else
		{
			var bSave = confirm("To close this window, please use the \"Save Education Details and Close\" button.\nWould you like to save your data?\n\n"+
				"Click OK to save your data.\nClick Cancel to discard any changes you have made in this window.");
			if (bSave)
				updateMainPage();
		}
	}
}

function selectItemByText(selID, strText)
{
	var selObj = document.getElementById(selID);
	arr = selObj.options; 
	
	var bReturn = false;
	for(var i = 0; i < arr.length; i++)
	{
		if(arr[i].text == unescape(strText))
		{
			selObj.selectedIndex = i;
			bReturn = true;
			break;
		}
	}
	
	return bReturn;
}

function delItem(schoolName_degree_year, showAlert)
{
	var schoolName = "";
	var year = "";
	var degreeName = "";
	
	var aSN_D_Y = schoolName_degree_year.split('|');
	
	if(!showAlert || confirm("Click OK to confirm the deletion of "+unescape(aSN_D_Y[0])+", "+unescape(aSN_D_Y[1])+" from "+aSN_D_Y[2]+"."))
	{
		schoolName = unescape(aSN_D_Y[0]);
		degreeName = unescape(aSN_D_Y[1]);
		year = aSN_D_Y[2];
		
		var myEducation = document.getElementById('eduVals');
		var eduVals = myEducation.value.split(';');
		
		var strNewVal = "";
		//check to make sure we're not adding an already existing item
		for(var i=0; i < eduVals.length; i++)
		{
			var arrCurDetails = eduVals[i].split('|');
			var _schoolName = 0;
			var _schoolId = 1;
			var _degreeName = 2;
			var _degreeId = 3;
			var _year = 4;
			var _honors = 5;
			var _IsLawSchool = 6;
			
			//delete the school selected
			if((schoolName != arrCurDetails[_schoolName] || 
				degreeName != arrCurDetails[_degreeName] || 
				year != arrCurDetails[_year]) &&
				arrCurDetails[_schoolName] != null && 
				arrCurDetails[_year] != null && 
				arrCurDetails[_degreeName] != null)
				{
					strNewVal += arrCurDetails[_schoolName] + "|" + arrCurDetails[_schoolId] + "|" + arrCurDetails[_degreeName] + "|" + arrCurDetails[_degreeId] + "|" + arrCurDetails[_year] + "|" + arrCurDetails[_honors] + "|" + arrCurDetails[_IsLawSchool] + "|;"
				}
		}

		myEducation.value = strNewVal;
		buildEducationTable(true);
	}
}

function updateMainPage()
{
	safeClose = true;
	if (document.getElementById('eduVals').value=="")
	{
		var bAreYouSure = confirm("Education listings are required to save your information. \nAre you sure you want to exit this form with no schools saved?" + 
				"\n\nClick OK to exit this form with no education background selected.\nClick Cancel to return to the Education form.");
		if (!bAreYouSure)
		{
			safeClose=false;
			return false;
		}
	}

	var myParent = window.opener;
	var parentEduVals =  myParent.document.getElementById('eduVals');
	
	parentEduVals.value = document.getElementById('eduVals').value;
	try
	{
		myParent.buildEducationTable(false);
	}
	catch(e)
	{
		alert(e.message);
	}
	window.close();
	return false;
}

function ShowEducationPopup()
{   
	var eduVals = document.getElementById('eduVals').value;
	window.open('editEducation.aspx?eduVals=' + eduVals, 'newwindow','width=690,height=600,scrollBars=yes,status=yes');
}
