﻿var educationList = new EducationListContainer();
var search = new SearchFunctions();
var validateETSCodeHandler;

function InitializeEducationLists() {
    educationList.InitializeEducation();
}

function ShowSearchInLightBox() 
{
    var isIe6 = isIE6Explorer();
    showInLightbox(searchControls.InstitutionSearchLightBox, 'fade', isIe6);
}

function HideLightBox() 
{
	search.ClearControls();
	//Call to thinckbox.js function
	tb_remove();
}

function AddInstitution(organizationId, institutionName, etsCode, city, stateProvinceId, countryId, handlerPath)
{
	$("#"+educationControls.FiceCodeTextBox).val(organizationId);
	$("#"+educationControls.InstNameTextBox).val(institutionName);
	$("#"+educationControls.EtsCodeTextBox).val(etsCode);
	$("#"+educationControls.InstCityTextBox).val(city);
	$("#"+educationControls.InstStateDropDown).val(stateProvinceId);
	$("#"+educationControls.InstCountryDropDown).val(countryId);
	educationList.UpdateSelectedRow();
	HideLightBox();
	Page_ClientValidate("EducationGroup");
}

function ClientValidateEtsCodeValid(source, clientside_arguments)
{    
	clientside_arguments.IsValid = true;
	var etsCode = $("#"+educationControls.EtsCodeTextBox).val();
	var validateETSCode = educationControls.ValidateEtsCode;
		
	try {
		xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"); 
	} catch (e) {
		alert(ResourceManager.GetString('JsInstitutionSearchBrowserNotSupported'));
		return;
	}
	xmlhttp.open("POST", validateETSCodeHandler, false);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send("etsCode="+etsCode+"&validateETSCode="+validateETSCode);
	responseText = xmlhttp.responseText;
	if (responseText != "True")
	{
		clientside_arguments.IsValid = false;
	}
}
	
function SearchFunctions()
{
	this.ClearControls = function()
	{
		$("#"+searchControls.InstNameSearchTextBox).val("");
		$("#"+searchControls.EtsCodeSearchTextBox).val("");
		$("#"+searchControls.CitySearchTextBox).val("");
		$("#"+searchControls.StateSearchDropDown).val("");
		$("#"+searchControls.CountrySearchDropDown).val("");

		var resultsSpan = document.getElementById(searchControls.SearchResultsLiteral);
		resultsSpan.innerHTML = "";	
	}
	
	this.SearchInstitution = function(handler)
	{
		var institutionName = $("#"+searchControls.InstNameSearchTextBox).val();
		var etsCode = $("#"+searchControls.EtsCodeSearchTextBox).val();
		var city = $("#"+searchControls.CitySearchTextBox).val();
		var state = $("#"+searchControls.StateSearchDropDown).val();
		var contry = $("#"+searchControls.CountrySearchDropDown).val();
		
		try {
			xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"); 
		} catch (e) {
			alert(ResourceManager.GetString('JsInstitutionSearchBrowserNotSupported'));
			return;
		}
		xmlhttp.onreadystatechange = function() {
											UpdateEducationControl();
										}
		// Send the request. 
		xmlhttp.open("POST", handler);
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlhttp.send("institutionName="+institutionName+"&etsCode="+etsCode+"&city="+city+"&state="+state+"&contry="+contry
			+"&institutionListXSLFile="+ResourceManager.GetString('InstitutionListXSLFile'));
	}
	
	function UpdateEducationControl()
	{
		if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200))
		{ 
			var resultsSpan = document.getElementById(searchControls.SearchResultsLiteral);
			if (xmlhttp.responseText != "")
				resultsSpan.innerHTML = xmlhttp.responseText;
			else
				resultsSpan.innerHTML = "";
		}
	}
};

function EducationListContainer() 
{
	var selectedRow;
	var templateRow;
	var removedRow;
	
	this.AddDegreeObject = function(degreeObject, degreeHiddenField)
	{
		var actualDegree;
		 
		if ($("#"+degreeHiddenField).val() != "" )
			actualDegree = JSON.parse($("#"+degreeHiddenField).val());
		else
			actualDegree = new Array();

		actualDegree.push(degreeObject);
		$("#"+degreeHiddenField).val(JSON.stringify(actualDegree));
	
		return actualDegree;
	}

	this.AddDegreeRow = function() {
		if (Page_ClientValidate("EducationGroup")) {
			var selectedDegreeIndex = $("#degreeTable").find("tr[class=selectedrow]").attr("indexId");
			var currentDegreeObject = this.GetDegreeObjectFromPanel(educationControls);
			this.EditDegreeObject(selectedDegreeIndex, educationControls.HiddenField, currentDegreeObject);
			var allDegreeObjects = this.AddDegreeObject(new degree(0, 0, 0, "", "", "", "", "", 0, 0, ""),
				educationControls.HiddenField);
			this.BuildDegreeTable(allDegreeObjects, educationControls.HiddenField);
			FillDegreePanel(new degree(0, 0, 0, "", "", "", "", "", 0, 0, ""), educationControls);
			this.SelectRow($("#degreeTable").find("tr:last"), false);
		}
	}
	
	this.BuildDegreeTable = function(listDegree, degreeHiddenField) 
	{
        $("#degreeTable").empty();
        for (var i = 0; i <= (listDegree.length - 1); i = i + 1) {
        	var removeButton =
                "<a name='removeButton' href='javascript:void(0);'>" +
                "<img height='18' src='../App_Themes/Default/Images/icon_delete_x.gif' border='0'/></a>";

        	if (listDegree[i].InstitutionName == null) listDegree[i].InstitutionName = "";

        	$("#degreeTable").append(
                        "<tr indexId='" + i + "'>" +
                            "<td width='90%'>" +
								"<span style='font-weight:bold'>" + GetFixedText(listDegree[i].InstitutionName) + "&nbsp;</span>" +
                                "<span>" +
                                GetFixedText(GetTextFromValue(educationControls.DegreeDropDown, listDegree[i].DegreeId) + " " + GetTextFromValue(educationControls.CurriculumDropDown, listDegree[i].CurriculumId)) +
                                "</span>" +
	                            "</td>" +
                            "<td width='10%'>" +
                                removeButton +
                            "</td>" +
                        "</tr>");

        	if (listDegree.length == 1)
        		this.SelectRow($("#degreeTable").find("tr:first"), false);
        }

        $("#degreeTable").find("tr").click(
                    function() {
                        educationList.SelectRow(this, true)
                    }
                    );

        $("#degreeTable").find("tr").find("a").click(
                    function() {
                        educationList.RemoveRow(this.parentNode.parentNode)
                    }
                    );
    }
    
    this.DeleteDegreeObject = function(indexObject, degreeHiddenField) 
    {
        var actualDegree;

        if ($("#" + degreeHiddenField).val() != "")
            actualDegree = JSON.parse($("#" + degreeHiddenField).val());

        actualDegree.splice(indexObject, 1);
        $("#" + degreeHiddenField).val(JSON.stringify(actualDegree));

        return actualDegree;
    }

    this.EditDegreeObject = function(indexObject, degreehiddenField, newDegreeObject) {
    	var actualDegree;
    	if ($("#" + degreehiddenField).val() != "" && $("#" + degreehiddenField).val() != undefined)
    		actualDegree = JSON.parse($("#" + degreehiddenField).val());

    	if (actualDegree != undefined) {
    		actualDegree[indexObject] = newDegreeObject;
    		$("#" + degreehiddenField).val(JSON.stringify(actualDegree));
    	}
    }
    
    function FillDegreePanel(degreeObject, targetControlDegree) {

    	if (degreeObject == undefined)
    		return;
    
        if (degreeObject.DegreeId != 0)
            $("#" + targetControlDegree.DegreeDropDown).val(degreeObject.DegreeId);
        else
            $("#" + targetControlDegree.DegreeDropDown).val("");
            
		if (degreeObject.CurriculumId != 0)
            $("#" + targetControlDegree.CurriculumDropDown).val(degreeObject.CurriculumId);
        else
            $("#" + targetControlDegree.CurriculumDropDown).val("");
            
        if (degreeObject.HonorsId != 0)
            $("#" + targetControlDegree.HonorsDropDown).val(degreeObject.HonorsId);
        else
            $("#" + targetControlDegree.HonorsDropDown).val("");

        if (degreeObject.StartDate != null)
            $("#" + targetControlDegree.FromDate).val(degreeObject.StartDate);
        else
            $("#" + targetControlDegree.FromDate).val("");

        if (degreeObject.EndDate != null)
            $("#" + targetControlDegree.ToDate).val(degreeObject.EndDate);
        else
            $("#" + targetControlDegree.ToDate).val("");

        if (degreeObject.InstitutionName != null)
            $("#" + targetControlDegree.InstNameTextBox).val(degreeObject.InstitutionName);
        else
            $("#" + targetControlDegree.InstNameTextBox).val("");
        
        if (degreeObject.EtsCode != null)
            $("#" + targetControlDegree.EtsCodeTextBox).val(degreeObject.EtsCode);
        else
            $("#" + targetControlDegree.EtsCodeTextBox).val("");

        if (degreeObject.City != null)
            $("#" + targetControlDegree.InstCityTextBox).val(degreeObject.City);
        else
            $("#" + targetControlDegree.InstCityTextBox).val("");
            
        if (degreeObject.StateProvinceId != 0)
            $("#" + targetControlDegree.InstStateDropDown).val(degreeObject.StateProvinceId);
        else
            $("#" + targetControlDegree.InstStateDropDown).val("");
            
        if (degreeObject.CountryId != 0)
            $("#" + targetControlDegree.InstCountryDropDown).val(degreeObject.CountryId);
        else
            $("#" + targetControlDegree.InstCountryDropDown).val("");

        if (degreeObject.FiceCode != null)
            $("#" + targetControlDegree.FiceCodeTextBox).val(degreeObject.FiceCode);
        else
            $("#" + targetControlDegree.FiceCodeTextBox).val("");
    }

    this.GetAllDegreeObject = function(degreeHiddenField) {
        if ($("#" + degreeHiddenField).val() != "" && $("#" + degreeHiddenField).val() != undefined )
            return JSON.parse($("#" + degreeHiddenField).val());
        else
            return new Array();
    }
    
    this.GetDegreeObjectFromPanel = function(targetControlDegree) 
    {
        var degreeObject = new degree(0, 0, 0, "", "", "", "", "", 0, 0, "");
        degreeObject.DegreeId = parseInt($("#" + targetControlDegree.DegreeDropDown).val());
        degreeObject.CurriculumId = parseInt($("#" + targetControlDegree.CurriculumDropDown).val());
        degreeObject.HonorsId = parseInt($("#" + targetControlDegree.HonorsDropDown).val());
        degreeObject.StartDate = $("#" + targetControlDegree.FromDate).val();
        degreeObject.EndDate = $("#" + targetControlDegree.ToDate).val();
        degreeObject.InstitutionName = $("#" + targetControlDegree.InstNameTextBox).val();
        degreeObject.EtsCode = $("#" + targetControlDegree.EtsCodeTextBox).val();
        degreeObject.City = $("#" + targetControlDegree.InstCityTextBox).val();
        degreeObject.StateProvinceId = parseInt($("#" + targetControlDegree.InstStateDropDown).val());
        degreeObject.CountryId = parseInt($("#" + targetControlDegree.InstCountryDropDown).val());
        degreeObject.FiceCode = $("#" + targetControlDegree.FiceCodeTextBox).val();
        return degreeObject;
    }
    
    function GetDegreeObject(indexObject, degreeHiddenField)
	{
		var actualDegree;
		if ($("#" + degreeHiddenField).val() != "" && $("#" + degreeHiddenField).val() != undefined)
			actualDegree = JSON.parse($("#" + degreeHiddenField).val());
		
		if (actualDegree != undefined)
			return actualDegree[indexObject];
	}
    
    function GetTextFromValue(targetDropDown, value) 
    {
		if (value == null || value == "")
			return "";
		else
		{
			if (!isNaN(value))
			{
				var dropDown = document.getElementById(targetDropDown);
				var index;
				for (i=0; i<dropDown.length; i=i+1)
				{
					if (dropDown.options[i].value == value)
					{
						index = i;
						break;
					}
				}
				var dropDownTex = dropDown.options[index].text;
				if (dropDownTex != undefined)
					return dropDownTex;
				else
					return "";
			}
			else
				return "";
		}
    }

    this.InitializeEducation = function() {

        var actualDegree = this.GetAllDegreeObject(educationControls.HiddenField);
        if (actualDegree.length == 0 || actualDegree.length == undefined) {
        }
        else {
            this.BuildDegreeTable(actualDegree, educationControls.HiddenField);
        }

        this.SelectRow($("#degreeTable").find("tr:first"), false);

        $("#" + educationControls.DegreeDropDown).change(this.UpdateSelectedRow);
        $("#" + educationControls.CurriculumDropDown).change(this.UpdateSelectedRow);
        $("#" + educationControls.HonorsDropDown).change(this.UpdateSelectedRow);
        $("#" + educationControls.FromDate).change(this.UpdateSelectedRow);
        $("#" + educationControls.ToDate).change(this.UpdateSelectedRow);
        $("#" + educationControls.InstNameTextBox).change(this.UpdateSelectedRow);
        $("#" + educationControls.EtsCodeTextBox).change(this.UpdateSelectedRow);
        $("#" + educationControls.InstCityTextBox).change(this.UpdateSelectedRow);
        $("#" + educationControls.InstStateDropDown).change(this.UpdateSelectedRow);
        $("#" + educationControls.InstCountryDropDown).change(this.UpdateSelectedRow);
        $("#" + educationControls.FiceCodeTextBox).change(this.UpdateSelectedRow);
        
    }
	
	this.RemoveRow = function(row) 
    {
		this.StoreCurrentObject();
    
        var degreeIndexDelete = $(row).attr("indexId");
        var actualSelectedDegreeIndex = $("#degreeTable").find("tr[class=selectedrow]").attr("indexId");
        var degreeDeleteRowClass = $(row).attr("class");
        var actualDegree = this.DeleteDegreeObject(degreeIndexDelete, educationControls.HiddenField);
        if (actualDegree.length == 0 || actualDegree.length == undefined)
        {
        	FillDegreePanel(new degree(0, 0, 0, "", "", "", "", "", 0, 0, ""), educationControls);
        }
		this.BuildDegreeTable(this.GetAllDegreeObject(educationControls.HiddenField), educationControls.HiddenField);
        
        if (degreeDeleteRowClass == "selectedrow")
            this.SelectRow($("#degreeTable").find("tr:first"), false);
        else {
            if (degreeIndexDelete < actualSelectedDegreeIndex)
                this.SelectRow($("#degreeTable").find("tr[indexId=" + (actualSelectedDegreeIndex - 1) + "]"), false);
            else
                this.SelectRow($("#degreeTable").find("tr[indexId=" + actualSelectedDegreeIndex + "]"), false);
		}
		Page_ClientValidate("EducationGroup");
    }
	
	this.SelectRow = function(row, isEdit) 
    {
        var isValidControl = true;

        if (isEdit == true) {
            isValidControl = Page_ClientValidate("EducationGroup");
            if (isValidControl == true) {
                var oldIndexObject = $("#degreeTable").find("tr[class=selectedrow]").attr("indexId");
                var oldDegreeObject = this.GetDegreeObjectFromPanel(educationControls);
                this.EditDegreeObject(oldIndexObject, educationControls.HiddenField, oldDegreeObject);
            }
        }
        if (isValidControl == true) 
        {
            $(row).parent().find("tr").attr("class", "");
            $(row).attr("class", "selectedrow")

            var newIndexObject = $(row).attr("indexId");
            var degreeObject = GetDegreeObject(newIndexObject, educationControls.HiddenField);
            FillDegreePanel(degreeObject, educationControls);
            $("#" + educationControls.indexDegreeHidden).val(newIndexObject);
        }
    }
    
    this.StoreCurrentObject = function()
	{
		var selectedDegreeIndex = $("#degreeTable").find("tr[class=selectedrow]").attr("indexId");
		var currentDegreeObject = this.GetDegreeObjectFromPanel(educationControls);
		this.EditDegreeObject(selectedDegreeIndex, educationControls.HiddenField, currentDegreeObject);
	}
     
    function UpdateRow(row) 
    {
        var spans = $(row).find("span");

//        if (spans.length > 0) {
//        	var degreeText = GetTextFromValue(educationControls.DegreeDropDown, $("#" + educationControls.DegreeDropDown).val());
//        	var curriculumText = GetTextFromValue(educationControls.CurriculumDropDown, $("#" + educationControls.CurriculumDropDown).val());
//        	spans[0].innerHTML = degreeText;
//        	spans[1].innerHTML = "&nbsp;&nbsp;" + curriculumText;
//        }

        if (spans.length > 0) {
        	var degreeText = GetTextFromValue(educationControls.DegreeDropDown, $("#" + educationControls.DegreeDropDown).val());
        	var curriculumText = GetTextFromValue(educationControls.CurriculumDropDown, $("#" + educationControls.CurriculumDropDown).val());
        	var educationName = $("#" + educationControls.InstNameTextBox).val();

        	spans[0].innerHTML = GetFixedText(educationName);
        	spans[1].innerHTML = GetFixedText(degreeText + " " + curriculumText);
        }
    }

    this.UpdateSelectedRow = function() {
    
		var educationObjects = educationList.GetAllDegreeObject(educationControls.HiddenField);
		if (educationObjects.length == 0) {
    		var listDegree = educationList.AddDegreeObject(educationList.GetDegreeObjectFromPanel(educationControls), educationControls.HiddenField);
    		educationList.BuildDegreeTable(listDegree, educationControls.HiddenField);
		}
    
        var selectedRow = $("#degreeTable").find("tr[class=selectedrow]");
        UpdateRow(selectedRow);
       }

       function GetFixedText(textToFix) {
       	if (textToFix.length > 25)
       		textToFix = textToFix.substring(0, 25) + "..." + "<br />";
       	else {
       		if (textToFix != "")
       			textToFix = textToFix + "<br />";
       	}
       	return textToFix;
       }
};
