var xmlHttp

function addBrick(bb_id, type) {

  xmlHttp=GetXmlHttpObject()
  if (xmlHttp==null)
    {
    alert ("Your browser does not support AJAX!");
    return;
    }
  var url="http://neptune.cems.umn.edu/designer/FancyBricks.php";
  url = url+"?add=1"
  url = url+"&bb_id="+bb_id;
  url = url+"&type="+type;
  url = url+"&sid="+Math.random();

  xmlHttp.onreadystatechange=stateChanged;
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);

}

function findBrick(bb_id, start, load) {

  if(start == null) {
    start = 0;
  }

  xmlHttp=GetXmlHttpObject()
  if (xmlHttp==null)
    {
    alert ("Your browser does not support AJAX!");
    return;
    } 
  var url="http://neptune.cems.umn.edu/designer/FancyBricks.php";
  url = url+"?"
  if(load == null) {
    url = url+"bb_id="+bb_id;
  }
  if(device = document.getElementById('device')) {
	url = url+"&device="+device.value;
  }
  url = url+"&start="+start;
  url = url+"&sid="+Math.random();

  if(load == "load" && !device) {
	xmlHttp.onreadystatechange=stateChangedLoad;
  } else {
  	xmlHttp.onreadystatechange=stateChanged;
  }
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);

} 

function protSearch(prot) {

  xmlHttp=GetXmlHttpObject()
  if (xmlHttp==null)
    {
    alert ("Your browser does not support AJAX!");
    return;
    }
  var url="http://neptune.cems.umn.edu/designer/BrickSearch.php";
  url = url+"?"
  url = url+"prot="+prot;
  url = url+"&sid="+Math.random();

  xmlHttp.onreadystatechange=stateChangedFancy;
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);

}

function stateChanged() {
  if (xmlHttp.readyState==4) {
    document.getElementById("data").innerHTML=xmlHttp.responseText;
    getBrick('add');
  }
}

function stateChangedLoad() {
  if (xmlHttp.readyState==4) {
    document.getElementById("data").innerHTML=xmlHttp.responseText;
	getBrick('load');
  }
}

function stateChangedFancy() {
	if (xmlHttp.readyState==4) {
		document.getElementById("content").innerHTML=xmlHttp.responseText;
	}
}

function getBrick(id, final) {

// "Deselect" old tab
var old = document.getElementById('bb_id');
if(old) {
        var old_id = old.innerHTML;
        document.getElementById(old_id).className = '';
}

// Format any edits as JSON, if necessary
if (document.getElementById('type')) {
	var bb_id = document.getElementById(old_id).innerHTML;
	var type = document.getElementById('type').innerHTML;

if(type == "Coding DNA") {
	var prot = document.getElementById('prot_name').innerHTML;
	var prot_old = document.getElementById('prot_name').name;
	var prot_type = document.getElementById('prot_type').innerHTML;
	if(document.getElementById('prot_name_default') != null) {
		var prot_def = document.getElementById('prot_name_default').name;
	}
	switch(prot_type) {
	case "Activator":
		prot_type = 1; break;
	case "Reporter":
		prot_type = 3; break;
	case "Repressor":
		prot_type = 2; break;
	default:
		prot_type = 0;
	}
	//EDIT var edits = '{ "proteins" : { "old" : "'+prot_old+'", "new" : "'+prot+'", ';
	var edits = '{ "biobricks" : { "new" : "'+old_id+'", ';
	//EDIT edits = edits+' "values" : { "part" : "'+bb_id+'", "name" :"'+prot+'", "type" :'+prot_type+' } } }';
	if(prot != "") {
		edits = edits+' "values" : { "id" : "'+bb_id+'", "type" :"'+type+'", "prot" : "'+prot+'" '
		if(prot_def != null) {
			edits = edits+', "prot_def" : "'+prot_def+'"';
		}
		edits = edits+'} },';
		edits = edits+'"proteins" : { "new" : "'+prot+'", ';
		edits = edits+' "values" : { "type": "'+prot_type+'"} } }';
	} else {
		 edits = edits+' "values" : { "id" : "'+bb_id+'", "type" :"'+type+'" } } }';
	}
}

if(type == "Promoter") {
	var on = document.getElementById('const').innerHTML;
	if(on == "ON") {
		on = 1;
	} else {
		on = 0;
	}
	var oplist = document.getElementById('oplist').childNodes;
	var ops = ''; var old_ops = ''; var new_ops = ''; var vals = '';
	for (i=0; i<oplist.length;i++) {
		var op_name_old = oplist[i].id;
		var op_name = document.getElementById('op_'+op_name_old).innerHTML;
		var op_type = document.getElementById('op_type_'+op_name_old).innerHTML;
		switch(op_type) {
		case "Upstream of -35":
			op_type = 1; break;
		case "Between -35 and -10":
			op_type = 2; break;
		case "Downstream of -10":
			op_type = 3; break;
		}
		ops = ops+'"'+op_name+'" : '+op_type;
		old_ops = old_ops+'"'+op_name_old+'"';
		new_ops = new_ops+'"'+op_name+'"';
		vals = vals+op_type;
		if(i+1 !== oplist.length) {
			ops = ops+','; old_ops = old_ops+','; new_ops = new_ops+','; vals=vals+',';
		}
	}
	edits = '{ "promoters" : { "new": "'+bb_id+'",';
	edits = edits+'"values": { "on": '+on+', "operators": { '+ops+'} } }';
//	edits = edits+'"operators" : { "old": [ '+old_ops+'], "new": [ '+new_ops+'], "values": [ '+vals+'] } }';
	edits = edits+'}';
} }

// Select new tab
document.getElementById(id).className = 'active';

// If "add" tab, send to "BrickSearch.php," otherwise to "BrickInfo.php" or "BrickHelp.php" or "BrickLoad.php"
if(id=="add") {
	$sendto = "BrickSearch.php";
} else if (id == "load") {
	$sendto = "BrickLoad.php";
} else if (id == "help") {
	$sendto = "BrickHelp.php";
} else {
	$sendto = "BrickInfo.php";
}

// Select device if necessary
if(device = document.getElementById('device')) {
	device = device.value;
}       

xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  }
var url="http://neptune.cems.umn.edu/designer/"+$sendto;
url=url+"?id="+id;
url=url+"&sid="+Math.random();

if(type == "Coding DNA" || type == "Promoter") {
	url = url+"&edits="+edits;
}
if(device != null) {
	url = url+"&device="+device;
}
if(final == 1) {
	url = url+"&final=1";
}

xmlHttp.onreadystatechange=stateChangedFancy;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);

}

function stateChangedFancy()
{
if (xmlHttp.readyState==4)
{
document.getElementById("content").innerHTML=xmlHttp.responseText;
}
}

function stateChanged2() {
	if (xmlHttp.readyState==4) {
		document.getElementById("data").innerHTML=xmlHttp.responseText;
		getBrick('add');
	}
}

function shift(dir) {

var old = parseInt(document.getElementById('bb_id').innerHTML);
var newa = old + dir;

// Confirm brick deletion
if(newa==old) {
	if (!confirm('Are you sure you want to delete '+ document.getElementById(old).innerHTML+'?')) {
		return;
	}
}

xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  }
var url="http://neptune.cems.umn.edu/designer/shiftBrick.php";
url=url+"?old="+old;
url=url+"&newa="+newa;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=function() { stateChangedShift(newa) };
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}


function stateChangedShift(id)
{
if (xmlHttp.readyState==4)
{
printAfterShift(id);
}
}


function printAfterShift(id) {
xmlHttp=GetXmlHttpObject();
var url="http://neptune.cems.umn.edu/designer/FancyBricks.php";
url=url+"?feat=2";
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=function() { stateChangedShift2(id) };
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}


function stateChangedShift2(id) {
	if (xmlHttp.readyState==4) {
		document.getElementById("data").innerHTML=xmlHttp.responseText;
		if(id != 0) {
			getBrick(id-1);
		} else {
			getBrick('add');
		}
	}
}


function clear_all() {

// Delete any composite list
if(document.getElementById('composites')) {
	var comps = document.getElementById('composites');
	comps.parentNode.removeChild(comps);
}

xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  }
var url="http://neptune.cems.umn.edu/designer/FancyBricks.php";
url=url+"?clear=1";
xmlHttp.onreadystatechange=stateChangedClear;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function stateChangedClear()
{
if (xmlHttp.readyState==4)
{
document.getElementById("data").innerHTML=xmlHttp.responseText;
getBrick('add');
}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}
