// Last updated 2006-02-21
function addRowToTable(tableName,text)
{
	var tbl = document.getElementById(tableName);
	var lastRow = tbl.rows.length;
	// if there's no header row in the table, then iteration = lastRow + 1
	var iteration = lastRow;
	var row = tbl.insertRow(lastRow);
	row.id=tableName+lastRow;
	
	// left cell
	var cellLeft = row.insertCell(0);
	if (text) var textNode = document.createTextNode(text+':');
	else var textNode = document.createTextNode(tableName+' ('+iteration+'):');
	cellLeft.appendChild(textNode);
	
	// right cell
	var cellRight = row.insertCell(1);
	var el = document.createElement('input');
	el.type = 'file';
	el.id = 'upload_'+iteration;
	el.name = el.id;
	el.size = 20;
	cellRight.appendChild(el);
	
}

function addRowToPanelTable(tableName,nRow)
{
	var tbl = document.getElementById(tableName);
	var lastRow = tbl.rows.length;
	// if there's no header row in the table, then iteration = lastRow + 1
	var iteration = lastRow;
	var row = tbl.insertRow(lastRow);
	row.id=tableName+lastRow;
}

function addCellToPanelTable(rawName,nCell,data)
{
	
	var raw = document.getElementById(rawName);
	var cell = row.insertCell(nCell);
	var textNode = document.createTextNode(data);
	cell.appendChild(textNode);
	
	// right cell
	/*
	var cellRight = row.insertCell(1);
	var el = document.createElement('input');
	el.type = 'file';
	el.id = 'upload_'+iteration;
	el.name = el.id;
	el.size = 20;
	cellRight.appendChild(el);
	*/
	
}
	
function keyPressTest(e, obj){
	var validateChkb = document.getElementById('chkValidateOnKeyPress');
	if (validateChkb.checked) {
		var displayObj = document.getElementById('spanOutput');
		var key;
		if(window.event) {
			key = window.event.keyCode; 
		} else if(e.which) {
			key = e.which;
		}
		var objId;
		if (obj != null) {
			objId = obj.id;
		} else {
			objId = this.id;
		}
		displayObj.innerHTML = objId + ' : ' + String.fromCharCode(key);
	}
}

function removeRowFromTable(tableName){
	var tbl = document.getElementById(tableName);
	var lastRow = tbl.rows.length;
	if (lastRow > 2) tbl.deleteRow(lastRow - 1);
}

function openInNewWindow(frm){
	// open a blank window
	var aWindow = window.open('', 'TableAddRowNewWindow',
	'scrollbars=yes,menubar=yes,resizable=yes,toolbar=no,width=700,height=700');
	
	// set the target to the blank window
	frm.target = 'TableAddRowNewWindow';
	
	// submit
	frm.submit();
}

function validateRow(frm,tableName){
	var chkb = document.getElementById('chkValidate');
	if (chkb.checked) {
		var tbl = document.getElementById(tableName);
		var lastRow = tbl.rows.length;
		var i;
		var error=0;
		for (i=0; i<lastRow; i++) {
			var aRow = document.getElementById(tableName+i);
			var aRowInput = document.getElementById('upload_'+i);
			if (aRowInput.value.length <= 0) {
				aRow.firstChild.setAttribute("bgcolor","red");
				error=1;
			}else{
				aRow.firstChild.setAttribute("bgcolor","");
			}
			
		}
		if (! error) openInNewWindow(frm);
	}else{
		//openInNewWindow(frm);
		frm.submit();
	}
}

function initTable(){
	addRowToTable('panel','Family model');
	addRowToTable('panel');
}

function findAllelesRow(table,nRow,a1,a2){
	//alert("findAllelesRow");
	//alert(table+" "+nRow+" "+a1+" "+a2);
	var i;
	var status;
        var counter=0;
	for(i=0;i<nRow;i++){
		status=document.getElementById("allele"+table+i).checked;
		//alert("status "+i+": "+status);
		if (status){
			counter++;
			if (counter==1)a1.setValue(i);
		  	if (counter==2)a2.setValue(i);
		}
	}
	//alert("a1: "+a1+" ;a2: "+a2);
	if (counter!=2){
		alert("You should select exactly two alleles rows");
		return 0;
	}else{
		return 1;
	}
}

function param()
{
    this.array = new Array(1);

    this.setValue = function(v) { this.array[0] = v; }
    this.getValue = function()  { return this.array[0]; }
}


function resetLine(obj,table,line,nRow){
	//alert("Alleles Reset! this:="+obj);
	//alert("Value a1= "+document.getElementById("cell"+table+line+a1).innerHTML);
	//alert("Value a1= "+document.getElementById("cell"+table+line+a1).title);
	//alert("resetAlleles -- status: "+status);
	//alert("a1: "+a1.getValue()+" ;a2: "+a2.getValue());
	var a1 = new param;
	var a2 = new param;
	var status=findAllelesRow(table,nRow,a1,a2);
	var a1Row=a1.getValue();
	var a2Row=a2.getValue();
	var a1Value=document.getElementById("input"+table+line+a1Row).title;
	var a2Value=document.getElementById("input"+table+line+a2Row).title;

	var i;
	for(i=0;i<nRow;i++){
		//document.getElementById("input"+table+line+i).innerHTML=
		//	document.getElementById("input"+table+line+i).title;
		if(document.getElementById("input"+table+line+i).title=="null")
			document.getElementById("input"+table+line+i).value="";
		else
			document.getElementById("input"+table+line+i).value=
				document.getElementById("input"+table+line+i).title;
	}
	homoStatus(table,line,a1Value,a2Value);
	zeroStatus(table,line,a1Value,a2Value);
}


function zeroAlleles(obj,table,line,nRow)
{
	var a1 = new param;
	var a2 = new param;
	var status=findAllelesRow(table,nRow,a1,a2);
	var a1Row=a1.getValue();
	var a2Row=a2.getValue();
	var a1Value=document.getElementById("input"+table+line+a1Row).title;
	var a2Value=document.getElementById("input"+table+line+a2Row).title;
	//alert("Alleles Reset! this:="+obj);
	//document.getElementById("input"+table+line+a1.getValue()).innerHTML="0";
	//document.getElementById("input"+table+line+a2.getValue()).innerHTML="0";
	document.getElementById("input"+table+line+a1.getValue()).value="0";
	document.getElementById("input"+table+line+a2.getValue()).value="0";
	document.getElementById("zero"+table+line).type="hidden";
	document.getElementById("homo"+table+line).type="hidden";
}

function copyAllele(obj,table,line,nRow){
	var a1 = new param;
	var a2 = new param;
	var status=findAllelesRow(table,nRow,a1,a2);
	var a1Row=a1.getValue();
	var a2Row=a2.getValue();
	var a1Value=document.getElementById("input"+table+line+a1Row).title;
	var a2Value=document.getElementById("input"+table+line+a2Row).title;
	//alert("cell"+table+line+a1+"("+a2+")");
	//alert("Value a1= "+document.getElementById("cell"+table+line+a1).innerHTML);
	//document.getElementById("cell"+table+line+a2.getValue()).innerHTML=
		//document.getElementById("cell"+table+line+a1.getValue()).innerHTML;
	if (document.getElementById("input"+table+line+a2.getValue()).title="null")
		document.getElementById("input"+table+line+a2.getValue()).value=
			document.getElementById("input"+table+line+a1.getValue()).value;
	else document.getElementById("input"+table+line+a1.getValue()).value=
			document.getElementById("input"+table+line+a2.getValue()).value;
	
	document.getElementById("homo"+table+line).type="hidden";
	zeroStatus(table,line,a1Value,a2Value);
}

function homoStatus(table,line,a1,a2){
	if(parseInt(a1)){
		if(!parseInt(a2))
			document.getElementById("homo"+table+line).type="button";
		else if (parseInt(a1)==parseInt(a2))
			document.getElementById("homo"+table+line).type="hidden";
		else
			document.getElementById("homo"+table+line).type="button";
	}else{
		document.getElementById("homo"+table+line).type="hidden";
	}

}

function zeroStatus(table,line,a1,a2){
	if((parseInt(a1))&&(parseInt(a2)))
		document.getElementById("zero"+table+line).type="hidden";
	else
		document.getElementById("zero"+table+line).type="button";

}


function initButtons(table,nRow){
	//alert("initButon");
	var a1 = new param;
	var a2 = new param;
	var status=findAllelesRow(table,nRow,a1,a2);
	var a1Row=a1.getValue();
	var a2Row=a2.getValue();
	var a1Value;
	var a2Value;
	var nLines=document.getElementById("xLines"+table).value;
	var i;
	if(status){
		for (i=1;i<nLines;i++){
			a1Value=document.getElementById("input"+table+i+a1Row).title;
			a2Value=document.getElementById("input"+table+i+a2Row).title;
			homoStatus(table,i,a1Value,a2Value);
			zeroStatus(table,i,a1Value,a2Value);
		}
	}
}
