﻿// JScript methods for the Application Employment user control.
var txtIdEmploy = 0;
var btnIdEmploy = 0;

function filterText(tbValue) {
    // Remove commas, pipes and underscores from the text since they are being used as array delimiters 
    tbValue.value = tbValue.value.replace(/,/g, "");
    tbValue.value = tbValue.value.replace(/_/g, "");
    tbValue.value = tbValue.value.replace(/\174/g, "");    
}

function processEmploymentInfo(employerID, positionID, startDateID, endDateID, isStartDateVisible, isEndDateVisible) {
	
	var employment = "";
	var content = document.getElementById("tableEmploymentContainer");

	if (content != null) {
	    var employRows = content.getElementsByTagName("tr");
	    var employHidden;

	    for (var i = 0; i < employRows.length; i = i + 1) {
	        if (employRows[i].getElementsByTagName("input").length > 0) {
	            var collectionText = employRows[i].getElementsByTagName("input");
	            var collectionList = employRows[i].getElementsByTagName("select");
	            if (isStartDateVisible && isEndDateVisible) {	                
	                if (collectionText[0].value != "" || collectionList[0].value != 0 || collectionText[1].value != "" || collectionText[2].value != "") {
	                    employment = employment + collectionText[0].value + "_EmployerNameTextBox,";
	                    employment = employment + collectionList[0].value + "_PositionDropDownList,";
	                    employment = employment + collectionText[1].value + "_StartDate,";
	                    employment = employment + collectionText[2].value + "_EndDate";
	                    employment = employment + "|";
	                }
	            }
	            if (isStartDateVisible && !isEndDateVisible) {
	                if (collectionText[0].value != "" || collectionList[0].value != 0 || collectionText[1].value != "" ) {
	                    employment = employment + collectionText[0].value + "_EmployerNameTextBox,";
	                    employment = employment + collectionList[0].value + "_PositionDropDownList,";
	                    employment = employment + collectionText[1].value + "_StartDate,";
	                    employment = employment + "_EndDate";
	                    employment = employment + "|";
	                }
	            }
	            if (!isStartDateVisible && isEndDateVisible) {
	                if (collectionText[0].value != "" || collectionList[0].value != 0 || collectionText[1].value != "") {
	                    employment = employment + collectionText[0].value + "_EmployerNameTextBox,";
	                    employment = employment + collectionList[0].value + "_PositionDropDownList,";
	                    employment = employment + "_StartDate,";
	                    employment = employment + collectionText[1].value + "_EndDate";
	                    employment = employment + "|";
	                }
	            }
	            if (!isStartDateVisible && !isEndDateVisible) {
	                if (collectionText[0].value != "" || collectionList[0].value != 0) {
	                    employment = employment + collectionText[0].value + "_EmployerNameTextBox,";
	                    employment = employment + collectionList[0].value + "_PositionDropDownList,";
	                    employment = employment + "_StartDate,";
	                    employment = employment + "_EndDate";
	                    employment = employment + "|";
	                }
	            }
	        }
	    }

	    for (var i = 0; i < document.forms[0].elements.length; i++) {
	        var element = document.forms[0].elements[i];
	        if (element.id.substring(element.id.length - 21, element.id.length) == '_EmploymentInfoHidden') {
	            employHidden = document.getElementById(element.id);
	            employHidden.value = employment;
	        }
	    }
	    employHidden.value = employment;
	}
}

// Handle the initial state for employ container
function initializeGridEmployment(employerNameTextBox, positionDropDownList, startDateBox, startDateCalendarPopup, endDateBox, endDateCalendarPopup,
    defaultValues, removeButtonLabelText, isStartDateVisible, isEndDateVisible ){

    removeButtonLabel = removeButtonLabelText;

    if (defaultValues.length > 0) {
        var name;
        var position;
        var startDate;
        var endDate;    

        for (var i = 0; i < document.forms[0].elements.length; i++) {
            var element = document.forms[0].elements[i];
            if (element.id.substring(element.id.length - 20, element.id.length) == '_EmployerNameTextBox') {
                name = document.getElementById(element.id);
                if (name != null) name.value = defaultValues[0][0];                
            }
            else if (element.id.substring(element.id.length - 21, element.id.length) == '_PositionDropDownList') {
                position = document.getElementById(element.id);
                if (position != null) selectDropDownOption(position, defaultValues[0][1]);
            }
            else if (element.id.substring(element.id.length - 10, element.id.length) == '_StartDate') {
                startDate = document.getElementById(element.id);
                if (startDate != null) startDate.value = defaultValues[0][2];
            }
            else if (element.id.substring(element.id.length - 8, element.id.length) == '_EndDate') {
                endDate = document.getElementById(element.id);
                if (endDate != null && isStartDateVisible) { 
                    endDate.value = defaultValues[0][3];
                }
                if (endDate != null && !isStartDateVisible) { 
                    endDate.value = defaultValues[0][2];
                }                
            }
        }
    }

    for (var i = 1; i < defaultValues.length; i++) {
	    if(isStartDateVisible && isEndDateVisible){
		    addEmployment(employerNameTextBox, positionDropDownList, startDateBox, startDateCalendarPopup, endDateBox, endDateCalendarPopup, removeButtonLabel,
			    isStartDateVisible, isEndDateVisible, defaultValues[i][0], defaultValues[i][1], defaultValues[i][2], defaultValues[i][3]);
        }
        if (isStartDateVisible && !isEndDateVisible) {
            addEmployment(employerNameTextBox, positionDropDownList, startDateBox, startDateCalendarPopup, endDateBox, endDateCalendarPopup, removeButtonLabel,
			    isStartDateVisible, isEndDateVisible, defaultValues[i][0], defaultValues[i][1], defaultValues[i][2], "");
        }
        if (!isStartDateVisible && isEndDateVisible) {
            addEmployment(employerNameTextBox, positionDropDownList, startDateBox, startDateCalendarPopup, endDateBox, endDateCalendarPopup, removeButtonLabel,
			    isStartDateVisible, isEndDateVisible, defaultValues[i][0], defaultValues[i][1], "", defaultValues[i][2]);
        }
        if (!isStartDateVisible && !isEndDateVisible) {
            addEmployment(employerNameTextBox, positionDropDownList, startDateBox, startDateCalendarPopup, endDateBox, endDateCalendarPopup, removeButtonLabel,
			    isStartDateVisible, isEndDateVisible, defaultValues[i][0], defaultValues[i][1], "", "");
        }
	}		    
}

function addEmployment(employerNameTextBoxId, positionDropDownId, startDate, startDateCalendar, endDate, endDateCalendar, removeButtonLabel, 
		isStartDateVisible, isEndDateVisible, selectedEmployer, selectedPosition, selectedStartDate, selectedEndDate){
    
	var container = document.getElementById("tableEmploymentContainer");	
	var employRow = container.insertRow(-1);
	
	var dupeEmployerBox;
    var dupePositionBox;    
    var dupeStartDateBox;
    var dupeStartCalendar;
    var dupeEndDateBox;
    var dupeEndCalendar;
    var calendarImg;
    var cellNumber = 0;

    //Employ field
	dupeEmployerBox = document.getElementById(employerNameTextBoxId).cloneNode(true);
	dupeEmployerBox.id = "_EmployerNameHidden" + eval(txtIdEmploy += 1);
	dupeEmployerBox.value = selectedEmployer;
    container.rows[container.rows.length - 1].insertCell(cellNumber);
    container.rows[container.rows.length - 1].cells[cellNumber].appendChild(dupeEmployerBox);
    cellNumber++;
    
    //Position field
    dupePositionBox = document.getElementById(positionDropDownId).cloneNode(true);
	dupePositionBox.id = "_PositionHidden" + eval(txtIdEmploy += 1);
	dupePositionBox.selectIndex = selectedPosition;
	selectDropDownOption(dupePositionBox, selectedPosition)
    container.rows[container.rows.length - 1].insertCell(cellNumber);
    container.rows[container.rows.length - 1].cells[cellNumber].appendChild(dupePositionBox);
    cellNumber++;
    	    	
    //Start date field 
    if (isStartDateVisible) {
		dupeStartDateBox = document.getElementById(startDate).cloneNode(true);
		dupeStartDateBox.id = "_StartDateHidden" + eval(txtIdEmploy += 1);
		dupeStartDateBox.value = selectedStartDate;
		container.rows[container.rows.length - 1].insertCell(cellNumber);
		container.rows[container.rows.length - 1].cells[cellNumber].appendChild(dupeStartDateBox);

		dupeStartCalendar = document.getElementById(startDateCalendar).cloneNode(true);
		dupeStartCalendar.id = "_StartDateCalendarPopupHidden" + eval(txtIdEmploy += 1);
		dupeStartCalendar.onclick = function() { calendarPicker(dupeStartDateBox.id); return false; }
		container.rows[container.rows.length - 1].cells[cellNumber].appendChild(dupeStartCalendar);
		cellNumber++;
    }    
    
    //End date field
    if (isEndDateVisible) {
		dupeEndDateBox = document.getElementById(endDate).cloneNode(true);
		dupeEndDateBox.id = "_EndDateHidden" + eval(txtIdEmploy += 1);
		dupeEndDateBox.value = selectedEndDate;
		container.rows[container.rows.length - 1].insertCell(cellNumber);
		container.rows[container.rows.length - 1].cells[cellNumber].appendChild(dupeEndDateBox);

		dupeEndCalendar = document.getElementById(endDateCalendar).cloneNode(true);
		dupeEndCalendar.id = "_EndDateCalendarPopupHidden" + eval(txtIdEmploy += 1);
		dupeEndCalendar.onclick = function() {calendarPicker(dupeEndDateBox.id); return false;};	
		container.rows[container.rows.length - 1].cells[cellNumber].appendChild(dupeEndCalendar);
		cellNumber++;
    }    
	
	// Add remove button
	var btnRemove = document.createElement("input");
	btnRemove.type = "button";
	btnRemove.value = removeButtonLabel;	
	btnRemove.id = "remove_" + eval(btnIdEmploy += 1);
	btnRemove.className = "btnRemove";	
    container.rows[container.rows.length - 1].insertCell(cellNumber);
    container.rows[container.rows.length - 1].cells[cellNumber].appendChild(btnRemove);	 		
	
	reInitializeEmploy();    
}

// Add event for delete a employ
function reInitializeEmploy() {
	var removeButtons = document.getElementById("tableEmploymentContainer").getElementsByTagName("input");
	for (i = 0; i < removeButtons.length; i ++) {		
		if((removeButtons[i].type == "button") && (removeButtons[i].className != "glassFormButtonMed") ){		    		    
			removeButtons[i].onclick = function () {
    			this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);  			
			 }
			removeButtons[i].className = "btnRemove";
		}
	}
}

