$(function(){
	$("#searchStructureBtb").click(function(){
		searchStructure();
	});
	
	var options = { 
        beforeSubmit:  checkStructure,
        success:       callback,  
        type:      "post"        
    }; 
    
//	$("#customizedForm").submit(function(){
  //     checkStructure();
  //     $("#customizedForm").submit();
       // $(this).ajaxSubmit(options); 
  //      return false; 
//	})
})

function searchStructure() {
	var smilesfile;
	var searchType = $("#searchType").val().replace(/(^\s*)|(\s*$)/g, "");
	var threshold = $("#similarityThreshold").val().replace(/(^\s*)|(\s*$)/g, "");
	if (threshold == undefined || threshold == null || threshold=="") {threshold=0;}
	if (document["MSketch"] == undefined || document["MSketch"] == null) {
		smilesfile = "";
	} else {
		smilesfile = document["MSketch"].getMol("smiles");
	}
	if ((searchType == "similarity" || searchType == "exact") && isEmpty(smilesfile)) {
		alert($("#emptyStructure").val());
		return false;
	}
	$("#key").attr("value", searchType);
	$("#value").val(smilesfile);
	$("#structureForm").attr("action",path+"/product/search");
	$("#structureForm").submit();
}

function searchCompound() {
	if (isEmpty($.trim($("#searchvalue").val()))) {
		alert($("#emptyValue").val());
		return;
	}
	window.location.href = path + "/product/search/"+$("#searchkey").val()+"/"+$.trim($("#searchvalue").val());
}

function showThreshold() {
	if ($("#searchType").val() == "similarity") {
		$("#similarityThresholdDiv").show(100);
	} else {
		$("#similarityThresholdDiv").hide(100);
	}
}

function search(id) {
		var url=path+"/product/show";
		var html = "<input type='hidden' name='id' value='"+id+"'/>";
		$("#catalogFrom").append(html);
		$("#catalogFrom").attr("action",url);
		$("#catalogFrom").submit();
}

function searchIndex() {
	window.location.href=path+"/product/structure";
}

function productList() {
	window.location.href=path+"/product/list";
}

function recommendList() {
	window.location.href=path+"/product/recommend";
}

function customizedCompound() {
	window.location.href=path+"/customized";
}

function propertyChange(){
	$("#molFile").val(document["MSketch"].getMol("smiles"));
}

function checkStructure() {
	var smilesfile;
	if (document["MSketch"] == undefined || document["MSketch"] == null) {
		smilesfile = "";
	} else {
		smilesfile = document["MSketch"].getMol("smiles");
	}
	if (isEmpty(smilesfile)) {
		//alert($("#emptyStructure").val());
		return false;
	}
	$("#molFile").val(smilesfile);
	return true;
	//$("#customizedForm").submit();
}

function callback(data) {
	if ("success" == data) {
		alert($("#customizedSuccess").val());
	}
	if ("failed" == data) {
		alert($("#customizedFailed").val());
	}
}
