// IMMA record to be edited
var Record;
var recordId;

// Display the data for one attachmentin the record_div
function displayAttachment(attN) {
    var Disp_html
    if(attN<0) {
        Disp_html = "<tt>"+Record.string+"</tt>";
    }
    else {
      if(attN!=99) {
        Disp_html = "<table cellpadding=3>";
        for(var i=0;i<IMMA.parameters[attN].length;i++) {
            if(i%4==0) Disp_html += "<tr>";
            Disp_html += "<td>"+IMMA.parameters[attN][i]+":</td>";
            if(Record[IMMA.parameters[attN][i]]!=null) {
                Disp_html +="<td><input type=\"text\" id=\""+
                                    IMMA.parameters[attN][i]+"\" "+
                        "value=\""+Record[IMMA.parameters[attN][i]]+"\" "+
                        "onChange=\"javascript:updateParameter"+
                         "(\'"+IMMA.parameters[attN][i]+"\')"+"\" "+
                        "size=\"10\" maxlength=\"10\" align=\"right\"></td>";
            }
            else {
                Disp_html +="<td><input type=\"text\" id=\""+
                                    IMMA.parameters[attN][i]+"\" "+
                        "value=\"\""+
                        "style=\"background-color: lightgrey\" " +
                        "onChange=\"javascript:updateParameter"+
                        "(\'"+IMMA.parameters[attN][i]+"\')"+"\" "+
                        "size=\"10\" maxlength=\"10\" align=\"right\"></td>";
        
            }
            Disp_html += "</td>";
            if(i%4==4 || i==IMMA.parameters[attN]-1) {
                Disp_html += "</tr>";
            }
        }
      Disp_html += "</table>"
     }
     else {  // Special treatment for supplemental data
         Disp_html ="SUPD:<br>"+
                     "<textarea id=\"SUPD\" "+
                     "cols=\"50\" rows=\"10\" "+
                     "onChange=\"javascript:updateParameter(\'SUPD\')\""+">"+
                     Record["SUPD"]+
                     "</textarea>";
     }
    }
    document.getElementById("record_div").innerHTML = Disp_html;  
}

// Function to Update the record from changes to the input fields
function updateParameter(parameter) {
    Record[parameter]=document.getElementById(parameter).value;
	if(Record[parameter].search(/\S/)==-1) { // Blank, set to null
		Record[parameter]=null;
		
	}
	if(Record[parameter]!=null &&
	   Record.checkParameter(parameter)==null) {
		Record[parameter]=null;
		document.getElementById(parameter).style.backgroundColor='red';
	}
    else if(Record[parameter]==null) {
		document.getElementById(parameter).style.backgroundColor='lightgrey';
    }
	else {
		document.getElementById(parameter).style.backgroundColor='white';
	}	
    Record.packString();
}

// Save the updated record back to the parent window
function saveRecord() {
	opener.updateRecord(Record,recordId);
}
// Save and quit
function saveRecordAndQuit() {
	saveRecord();
	window.close();
}

// Set the record to be edited and start the editor
// Called by the parent window
//function startUp(rec,id) {
	Record=opener.Records[opener.lastSelected];
	recordId = opener.lastSelected;

// Make the attachment selection buttons
var Nav_html;
Nav_html="<table cellpadding=5><tr>"+
         "<th><a href=\"javascript:displayAttachment(-1)\">String</a></th>";
for(i in Record.attachments) {
    Nav_html+="<th><a href=\"javascript:displayAttachment("+Record.attachments[i]+")\">"+
              IMMA.attachments[Record.attachments[i]]+"</a></th>";
}
Nav_html+="</tr></table>";
    document.getElementById("nav_div").innerHTML = Nav_html;  

// Show the core to start with
displayAttachment(0);
//}
