	var isProcess = 0;
	var matchKind = 1;
	var isSearchProduct = 0;
	var rate = 100;
	var moleStr = "";
	var productName = "";
	var searchProductType = "";
	var page = 0;
	var firstpage = "";
	var nextpage = "";
	var prepage = "";
	var currentPage = 1;
	var searchPName = "";
	var searchPType = "";
	function getMolFile() {
	  if (isProcess == 1) {
	  	alert("It is seaching, please wait for a moment.");
	  	return;
	  }
	  var mol = document.JME.molFile();
	  document.searchForm.molestr.value = mol;
	  if (mol.length < 200) {
	    	alert("The graphics you draw is too simple to search.");
	    }
	  else {
	       isSearchProduct = 0;
	       currentPage = 1;
	       startSearchRequest("");
	    }
	}
	
	function cleanJme() {
	 document.JME.reset();
	}
	
	var xmlHttp;
	
	function createXMLHttpRequest() {
		 if(window.XMLHttpRequest){
	        xmlHttp = new XMLHttpRequest();
	     } else if(window.ActiveXObject){
	         xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	     } else {
	      return;
	    } 
	}
	
	function startSearchRequest(uri) {
		var url = "";
		if (uri == "")  {
		      molstr = document.searchForm.molestr.value;
		      molstr = molstr.replace(/\n/g, "_");
    		if (isSearchProduct == 0) {
    		  rate = document.searchForm.rate.value;
    		  url = "match?" +"match_kind="  + matchKind + "&rate="+rate+"&q="+molstr;
    		} else if(isSearchProduct == 1) {
    		  if (searchPName != "") {
    		      productName = searchPName;
    		  } else {
    		      productName = document.getElementById('product_Name').value;
    		      searchPName = productName;
    		  } 
    		  
    		  if (searchPType != "") {
    		      searchProductType = searchPType;
    		  } else {
    		      searchProductType = document.getElementsByName('searchProductType').value;
    		      
    		       var rPort = document.getElementsByName("searchProductType");
                　　for(i=0;i<rPort.length;i++)
                　　{
                    　　 if(rPort[i].checked) {
                           　　searchProductType=rPort[i].value;
                           break;
                       }
                　　}
    		      
    		      searchPType = searchProductType;
    		  }

    		  url = "search_product?" +"productName="  + productName + "&searchProductType="+searchProductType;
    		}
		} else {
		  url = uri;
		}

		var index = url.indexOf("?");

		var requestURL = url.substring(0, index); 
        var requestParemeter = url.substring(index+1); 
		createXMLHttpRequest();
		xmlHttp.onreadystatechange = handleStateChange;
		
		xmlHttp.open("POST",requestURL,true);
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		document.getElementById('search_result').innerHTML = "";
		firstpage = "";
	    nextpage = "";
	    prepage = "";
	    document.getElementById('alert_content').innerHTML = "It's searching, please wait for a moment...";
		show_alert();
		isProcess = 1;
		
		xmlHttp.send(requestParemeter);
		
	}
	
	function handleStateChange() {
		isProcess = 0;
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200 || xmlHttp.status == 0) {
				var respText = xmlHttp.responseText;
				if (respText == "-1") {
				    close_alert();
					alert("Search error!");
				} else if (respText == "0") {
				    if (isSearchProduct == 0) {
				        document.getElementById('alert_content').innerHTML = "Sorry, not found, you can <a href='tell_us.jsp'>click here</a> to get help."; 

				    } else if(isSearchProduct == 1) {
				        var con = "Sorry, not found, you can <a href='tell_us.jsp?productName="+productName+"&searchProductType="+searchProductType+"'>click here</a> to get help."; 
				        document.getElementById('alert_content').innerHTML = con;
				    }
				    show_alert();
				} else {
					var showSpan = "<table class='listable' >";
					
					var moleItem = respText.split("|$$$|");
					if (moleItem.length < 2) {
						if (isSearchProduct == 0) {
				            document.getElementById('alert_content').innerHTML = "Sorry, not found, you can <a href='tell_us.jsp'>click here</a> to get help."; 

				        } else if(isSearchProduct == 1) {
				             var con = "Sorry, not found, you can <a href='tell_us.jsp?productName="+productName+"&searchProductType="+searchProductType+"'>click here</a> to get help."; 
				             document.getElementById('alert_content').innerHTML = con;
				        }
				        show_alert();
						return;
					}
					 close_alert(); 
					var totalInfo = moleItem[moleItem.length - 1];
					var totalItems = totalInfo.split("|$|");
					var startIndex = parseInt(totalItems[0]);
                    var endIndex = parseInt(totalItems[1]);
                    var limit = parseInt(totalItems[2]);
                    
                    if (moleItem.length > limit) {
                        if (isSearchProduct == 0) {
    		                  url = "match?" +"match_kind="  + matchKind + "&rate="+rate+"&start="+endIndex+"&q="+molstr;
    		              } else if(isSearchProduct == 1) {
    		                  url = "search_product?" +"productName="  + productName + "&searchProductType="+searchProductType+"&start="+endIndex;
    		                 
    		             }   
                        nextpage = url;

                    }
                    
                    if (currentPage > 1 && (startIndex > 0)) {
                        if (isSearchProduct == 0) {
    		                  url = "match?" +"match_kind="  + matchKind + "&rate="+rate+"&start="+startIndex+"&reverse=1&q="+molstr;
    		              } else if(isSearchProduct == 1) {
    		                  url = "search_product?" +"productName="  + productName + "&searchProductType="+searchProductType+"&start="+startIndex+"&reverse=1";
    		                 
    		             }   
                        prepage = url;
                    }
                    
                    if (currentPage > 1 && isSearchProduct == 0 && (page != 0)) {
    		                  firstpage = "match?" +"match_kind="  + matchKind + "&rate="+rate+"&q="+molstr;
		              } else if(isSearchProduct == 1 && (page != 0)) {
		                  firstpage = "search_product?" +"productName="  + productName + "&searchProductType="+searchProductType;
		              }  
					
					for (var i = 0; i < moleItem.length-1; i++) {
						var strItems = moleItem[i].split("|$|");
						
						if (strItems.length < 6) {
							break;
						}
						
						showSpan = showSpan + "<tr><td class='imag'> <applet code='JME.class' archive='JME.jar' width=215 height=140><param name='options' value='depict'><param name='jme' value='" + strItems[7] + "'></applet></td>";
						showSpan = showSpan + "<td class='attr'>";
						if (strItems[0] != "null") {	
							showSpan = showSpan + "Name:"+strItems[0]+"&nbsp;&nbsp;&nbsp;<a href='tell_us.jsp?moleId="+strItems[3]+"' target='_blank'>Send enquiry</a><br />";
						}
						
						if (strItems[1] != "null") {
							showSpan = showSpan + "Cas No:"+strItems[1]+"<br />";
						}
						
						if (strItems[2] != "null") {
							showSpan = showSpan + "Purity:"+strItems[2]+"<br />";
						}
						
						if (strItems[3] != "null") {
							showSpan = showSpan + "Id:"+strItems[3]+"<br />";
						}
						
						if (strItems[4] != "null") {
							showSpan = showSpan + "Pack:"+strItems[4]+"<br />";
						}
						
						if (strItems[5] != "null") {
							showSpan = showSpan + "Smile:"+strItems[5]+"<br />";
						}
						
						if (strItems[6] != "null") {
							showSpan = showSpan + "<a href='http://pubchem.ncbi.nlm.nih.gov/assay/assay.cgi?q=cids&cid="+strItems[6]+ "' target='_blank' >BioActivity</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color:#000;\">(BioActivity data from The National Center for Biotechnology Information (NCBI),our honorable partner)</span>"+"<br />";
						}
						
						showSpan = showSpan+"</td></tr>";
						
						moleItem[i] = strItems[7];
					}
					
					showSpan = showSpan+"</table>";
					if (firstpage != "" || nextpage != "" || prepage != "") {
					   showSpan = showSpan+"<p>";
					   if (firstpage != "") {
					       showSpan = showSpan + "<a href='#' name='first page' ' onclick='firstPage();'>First page</a>  &nbsp;";
					   }
					   if (prepage != "") {
					       showSpan = showSpan + "<a href='#' name='pre page' ' onclick='prePage();'>Pre page</a>  &nbsp;";
					   }
					   if (nextpage != "") {
					       showSpan = showSpan + "<a href='#' name='next page' ' onclick='nextPage();'>Next page</a>  &nbsp;";
					   }
					   showSpan = showSpan+"</p>";
					}
					
					document.getElementById('search_result').innerHTML = showSpan;
					
				}
			}
			else {
				alert("Access error!");
			}
		}
	}

    function exact_select() {
        matchKind = 1;
        $('rate').style.display = "none";
    }

    function sub_select() {
        matchKind = 2;
        $('rate').style.display = "block";
    }
    
    function searchByProduct() {
        if (isProcess == 1) {
	  	    alert("It is operating, please wait for moment.");
	  	    return;
	   }

        var pn = document.getElementById('product_Name').value;
       
        if (pn != '' && pn.length > 1) {
            isSearchProduct = 1;  
            searchPName = document.getElementById('product_Name').value;
            
            var rPort = document.getElementsByName("searchProductType");
            　　for(i=0;i<rPort.length;i++)
            　　{
                　　 if(rPort[i].checked) {
                   　　   searchPType=rPort[i].value;
                        break;
                    }
            　　}
            currentPage = 1;
            startSearchRequest("");
        }
    }
    
    function firstPage() {
        if (firstpage !="") {
            page = 0;
            startSearchRequest(firstpage);
            currentPage = 1;
        }
    }
    
    function nextPage() {
        if (nextpage !="") {
            page = 1;
            startSearchRequest(nextpage);
            currentPage = currentPage + 1;
         }
    }
    
    function prePage() {
        if (prepage !="") {
            page = 0;
            startSearchRequest(prepage);
            currentPage = currentPage -1;
         }
    }
    
    function goNextPage(uri) {
        isSearchProduct = 1;
        if (uri !="") {
            page = 1;
            startSearchRequest(uri);
            currentPage = currentPage + 1;
         }
    }
    
    function close_alert() {
      $('flowNotice').style.display = "none";
    }
    
    function show_alert() {
     $('flowNotice').style.display="block";
     
    }
