
var destination;
var ESSearchListHTTP="";
var PTSearchListUID="";
var ELMSearchListString="";
var ESSearchListString="";
var IncSearchListString="";
var ExSearchListString="";


function AddToIncludeSearchList(uid, name) {

	IncSearchListString = document.prod_search_form.ES_List.value;
	if (name.indexOf(" ") > 0)
		name = "+\"" + name + "\"";
	else
		name = "+" + name;
	if (IncSearchListString == "")
		IncSearchListString = name;
	else
		IncSearchListString = IncSearchListString + " " + name;
	document.prod_search_form.ES_List.value = IncSearchListString;
}

function AddToExcludeSearchList(uid, name) {

	ExSearchListString = document.prod_search_form.ES_List.value;
	if (name.indexOf(" ") > 0)
		name = "-\"" + name + "\"";
	else
		name = "-" + name;
	if (ExSearchListString == "")
		ExSearchListString = name;
	else
		ExSearchListString = ExSearchListString + " " + name;
	document.prod_search_form.ES_List.value = ExSearchListString;
}


function RemoveFromSearchList(searchList, value, dst) {

	if (value.indexOf(" ") > 0)
		value = "\"" + value + "\"";
		searchList = searchList.replace(" "+value+" ", " ");
		searchList = searchList.replace(" "+value, "");
		searchList = searchList.replace(value+" ", "");
		searchList = searchList.replace("&"+value, "");
		searchList = searchList.replace(value, "");
		dst.value=searchList;

return searchList;
}

function AddToELMSearchList(uid, name) {

	ELMSearchListString = document.elm_form.ES_List.value;
	if (name.indexOf(" ") > 0)
		name = "\"" + name + "\"";
	if (ELMSearchListString == "")
		ELMSearchListString = name;
	else
		ELMSearchListString = ELMSearchListString + " " + name;
	document.elm_form.ES_List.value = ELMSearchListString;
}

function AddToESSearchList(uid, name) {

	ESSearchListString = document.es_form.ES_List.value;
	if (name.indexOf(" ") > 0)
		name = "\"" + name + "\"";
	if (ESSearchListString == "")
		ESSearchListString = name;
	else
		ESSearchListString = ESSearchListString + " " + name;
	document.es_form.ES_List.value = ESSearchListString;
}

function AddToPTSSearchList(uid, name) {

	PTSearchListString = document.pt_form.ES_List.value;
	if (name.indexOf(" ") > 0)
		name = "\"" + name + "\"";
	if (PTSearchListString == "")
		PTSearchListString = name;
	else
	PTSearchListString = PTSearchListString + " " + name;
	document.pt_form.ES_List.value = PTSearchListString;
}

function RemoveFromELMSearchList(uid, name) {

	ELMSearchListString = RemoveFromSearchList(document.elm_form.ES_List.value, name, document.elm_form.ES_List);
}

function RemoveFromESSearchList(uid, name) {

	ESSearchListString = RemoveFromSearchList(document.es_form.ES_List.value, name, document.es_form.ES_List);
}

function RemoveFromPTSSearchList(uid, name) {

	PTSearchListString = RemoveFromSearchList(document.pt_form.ES_List.value, name, document.pt_form.ES_List);
}

function UpdateProductsTableAndDisplayFirstProduct(table_doc) {

		tables = table_doc.getElementsByTagName('table');
		p_table = tables['products'];
		prepareTable(p_table);
		body = p_table.getElementsByTagName('tbody');
		rows = body[0].getElementsByTagName('tr');
		first = rows[0];
		//lockRow(table, first.id);
		//alert(first.id);
		DisplayProduct(first.id);
		sorttable.makeSortable(p_table);
}


function SwitchSearch(switchTo) {

	if (switchTo == "Basic")
		GetMessage_Enhanced("web^Templates^basic_search.html^", "search_display");
	else if (switchTo == "Advanced")
		GetMessage_Enhanced("web^Templates^advanced_search.html", "search_display");
	else
		GetMessage_Enhanced("web^Templates^basic_search.html", "search_display");
	
}

function SearchForProducts(items, action, type, destination, limit, offset) {

var offset = (offset != null) ? offset : "";
var limit = (limit != null) ? limit : "";
var n_items;
var list;
var xmlhttp;

if (0 == 1) {
	list = items.split(" ");
	n_items = "ES_List[]="+list[0];
	for (i = 1; i < list.length; i++) {
		if (list[i] != "")
			n_items = n_items+"&ES_List[]="+list[i];
	}
}
else
	n_items = "ES_List[]="+items;
	
	document.getElementById("ProductDetails").innerHTML="";
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp == null)	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	 }
	xmlhttp.uniqueIDMemo = "SearchResults";
	var url="HTTPWrapper.php";
	url=url+"?action="+action;
	url=url+"&type="+type;
	url=url+"&method=js";
	url=url+"&"+n_items;
	url=url+"&offset="+offset;
	url=url+"&limit="+limit;
	xmlhttp.open("GET",url,true);
	xmlhttp.onreadystatechange = function() {	
				if (xmlhttp.readyState == 4) {
					document.getElementById(xmlhttp.uniqueIDMemo).innerHTML=xmlhttp.responseText;
					UpdateProductsTableAndDisplayFirstProduct(document.getElementById(xmlhttp.uniqueIDMemo));
					delete(self);
				}
			}
	xmlhttp.send(null);
}


function SearchForProduct(uid, action, type, destination, start, end) {

var xmlhttp;

	xmlhttp=GetXmlHttpObject();
	if (xmlhttp == null)	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	 }
	xmlhttp.uniqueIDMemo = destination;
	var url="HTTPWrapper.php";
	url=url+"?action="+action;
	url=url+"&type="+type;
	url=url+"&method=js";
	url=url+"&ES_List[]="+uid;
	url=url+"&start="+start;
	url=url+"&end="+end;

	xmlhttp.open("GET",url,true);
	xmlhttp.onreadystatechange = function() {	
				if (xmlhttp.readyState == 4) {
					document.getElementById(xmlhttp.uniqueIDMemo).innerHTML=xmlhttp.responseText;
					UpdateProductsTableAndDisplayFirstProduct(document.getElementById(xmlhttp.uniqueIDMemo));
					delete(self);
				}
			}
	xmlhttp.send(null);
}

function SearchByCompound(uid) {

	document.getElementById("ProductDetails").innerHTML="";
	SearchForProduct(uid, "FindProducts", "pbc", "SearchResults");

}


function SearchByProductType(uid) {

	document.getElementById("ProductDetails").innerHTML="";
	SearchForProduct(uid, "FindProducts", "pbpt", "SearchResults");

}

function SearchByProduct(uid) {

	document.getElementById("ProductDetails").innerHTML="";
	SearchForProduct(uid, "FindProducts", "pbu",  "SearchResults");

}

function DisplayProduct(uid) {

	tables = document.getElementsByTagName('table');
	t_table = tables['products'];
	lockRow(t_table, uid);
	//alert(t_table.innerHTML);
	SearchForProduct(uid, "FindProducts", "pbu",  "ProductDetails");
}

function ListElements(start, end) {
	
	l_start = start+"a";
	h_end = start+"m";
	
	SearchForProduct("", "ListElements", "ES", "elements_select_col1", start, h_end);
	
	l_start = start+"n";
	h_end = start+"z";
	
	SearchForProduct("", "ListElements", "ES", "elements_select_col2", l_start, h_end);

}

function ListCompounds(start, end) {
	
	l_start = start+"a";
	h_end = start+"m";

	SearchForProduct("", "ListCompounds", "CS", "compounds_select_col1", start, h_end);
	
	l_start = start+"n";
	h_end = start+"z";
	
	SearchForProduct("", "ListCompounds", "CS", "compounds_select_col2", l_start, h_end);
	
}

function ListProductTypes(start, end) {
	l_start = start+"a";
	h_end = start+"m";
	
	SearchForProduct("", "ListProductTypes", "PTS", "product_types_select_col1", start, h_end);
	
	l_start = start+"n";
	h_end = start+"z";
	
	SearchForProduct("", "ListProductTypes", "PTS", "product_types_select_col2", l_start, h_end);
	
		
}

function stateChanged() {

var self = this;

	if (self.readyState == 4) {
		document.getElementById(self.uniqueIDMemo).innerHTML=self.responseText;
		delete(self);
	}
}

function GetXmlHttpObject() {
var request;

	if (window.XMLHttpRequest) {
	  // code for IE7+, Firefox, Chrome, Opera, Safari
	  request = new XMLHttpRequest();
	  //request.onreadystatechange = stateChanged;
	  return request;
	}
	if (window.ActiveXObject) {
	  // code for IE6, IE5
	  return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}


function GetMessage(messStr) {

	GetMessage_Enhanced(messStr, "SearchResults");

}


function GetMessage_Enhanced(messStr, dest) {

var xmlhttp;

	xmlhttp=GetXmlHttpObject();
	if (xmlhttp == null)	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	 }
	xmlhttp.uniqueIDMemo = dest;
	var url="HTTPWrapper.php";
	url=url+"?action=GetMessage";
	url=url+"&message="+messStr;
	xmlhttp.open("GET",url,true);
	xmlhttp.onreadystatechange = function changedState() {	
				if (xmlhttp.readyState == 4) {
					document.getElementById(xmlhttp.uniqueIDMemo).innerHTML=xmlhttp.responseText;
					delete(self);
				}
			}
	xmlhttp.send(null);
}

function RestoreInstructions(messageName) {
		document.getElementById("search_results").innerHTML="For the moment, reload the page to see the instructions";
}


function prepareTable(t) {
//alert("prepareTable" + t.innerHTML);
	var tbodies = t.getElementsByTagName("tbody");
	for (var j=0; j<tbodies.length; j++) {
		var rows = tbodies[j].getElementsByTagName("tr");
		for (var i=0; i<rows.length; i++) {
			// store the base class
			 rows[i].oldClassName = rows[i].className;
			 //alert(rows[i].oldClassName);
		}
	}
	
}

function lockRow(t_table, row_id) {
//alert(t_table.innerHTML);
	var t_body = t_table.getElementsByTagName('tbody');
	var t_rows = t_body[0].getElementsByTagName('tr');
//	alert(t_rows.length);
//	var t_row = t_rows.getElementById(row_id);
//	alert(t_row.innerText);
	
	var tbodies = t_table.getElementsByTagName("tbody");
	for (var j=0; j<tbodies.length; j++) {
		var rows = tbodies[j].getElementsByTagName("tr");
		for (var i=0; i<rows.length; i++) {
			// restore base class
			 rows[i].className = rows[i].oldClassName;
			 if (rows[i].id == row_id) {
				// select the specific row
				rows[i].className = "selected";
				}
		}
	}
	
}

function addClass(element,value) {
  if (!element.className) {
    element.className = value;
  } else {
    newClassName = element.className;
    newClassName+= " ";
    newClassName+= value;
    element.className = newClassName;
  }
}

function init(t) {

	if (t == "@@init")
		SearchForProducts("", "FindProducts", "rap", "SearchResults", 20, 0);
	else
		DisplayProduct(t);

}

function stopRKey(evt) { 
  var evt = (evt) ? evt : ((event) ? event : null); 
  var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null); 
  if ((evt.keyCode == 13) && (node.name=="ES_List"))
  {
  		SearchForProducts(document.prod_search_form.ES_List.value, 'FindProducts', 'prod_search', 'search_result', '10', '0');
  		return false;
	}
}


document.onkeypress = stopRKey;
