/**
 * @author cruise
 */



function checkTextSearchBox() {
        
	var validator = new ValidateText();        
        var inputed_text = validator.checkTextAndPoint(document.text_search.key_id.value, true);
        
        if (inputed_text == null) {
                window.alert("Special characters can't be used to search.");
        } else if (inputed_text == "") {
                window.alert("A search box is empty.");
        } else if (inputed_text.length < 3) {
                window.alert("Use word that is consist of tree character over.");
        } else {
                document.text_search.submit();
        }
}


function checkPressedKeyOnSearch() {

    	if (window.event.keyCode == 13) {
                checkTextSearchBox();
        }
}


function checkFileAndArea() {
	
	form_elem = document.getElementById("id_file_upload");
	file_elem = document.getElementById("id_fasta_file");
        area_elem = document.getElementById("list_area");
	confirm_elem = document.getElementById("is_confirmed");
        
	is_acceptable = false;

        if (file_elem != null) {
                f_n_str = file_elem.value.replace(/(^\s+)|(\s+$)/g, "");
                if (f_n_str != "") {

                        exp_arr = new Array("HTM", "HTML", "TXT");

                        word_arr = f_n_str.split(".");
                        if (word_arr.length > 1) {
                                up_exp = word_arr[word_arr.length - 1].toUpperCase();
                                for (var i = 0 ; i < exp_arr.length; i++) {
                                        if (up_exp == exp_arr[i].toString()) {
                                                is_acceptable = true;
                                                break;
                                        }
                                }
                        }	
                }
        } else if (area_elem != null) {
                l_n_str = area_elem.value.replace(/(^\s+)|(\s+$)/g, "");
                if (l_n_str != "") {
                        is_acceptable = true;
                }
        }

	if (is_acceptable) {
		form_elem.submit();
	} else {
		window.alert("The file is not acceptable. Try again with another file.");
	}
}


function showDBSelection() {
        
        // db part //
        sel_db_html = "";

        is_profound = false;
        if (file_upload.sel_cont_type[2].checked) {
                is_profound = true;
        }
        
        sel_db_html += "<br>\n";
        sel_db_html += "<table width=\"100%\">\n";
        sel_db_html += "<tr>\n";
        sel_db_html += "<td width=\"15%\" class=\"box_left_left\">Database</td>\n";
	sel_db_html += "<td width=\"5%\" class=\"box_center_left\">:</td>\n";
        if (is_profound) { 
        	sel_db_html += "<td width=\"20%\" class=\"box_center_left\"><input type=\"radio\" name=\"sel_db\" value=\"nr\" onclick=\"javascript:showIDSelection();\" checked> NCBInr </td>\n";
                sel_db_html += "<td width=\"20%\" class=\"box_center_left\"> </td>\n";
        } else {
                sel_db_html += "<td width=\"20%\" class=\"box_center_left\"><input type=\"radio\" name=\"sel_db\" value=\"swissprot\" onclick=\"javascript:showIDSelection();\" checked> SwissProt </td>\n";
        	sel_db_html += "<td width=\"20%\" class=\"box_center_left\"><input type=\"radio\" name=\"sel_db\" value=\"nr\" onclick=\"javascript:showIDSelection();\" > NCBInr </td>\n";
        }
        sel_db_html += "<td width=\"20%\" class=\"box_center_left\"> </td>\n";
        sel_db_html += "<td width=\"20%\" class=\"box_right_left\"> </td>\n";
        sel_db_html += "</tr>\n";
        sel_db_html += "</table>\n";

        div_db_elem = document.getElementById("div_sel_db");
        div_db_elem.innerHTML = sel_db_html;

        // id part //
        id_txt_01 = null;
        id_val_01 = null;
        id_txt_02 = null;
        id_val_02 = null;
        
        sel_id_html = "";
        
        if (file_upload.sel_cont_type[3].checked) {
                if (file_upload.sel_db[0].checked) {
                        id_txt_01 = "Primary ID ( e.g. Q4U9M9 )";
                        id_val_01 = "pri";
                        id_txt_02 = "Secondary ID ( e.g. 104K_THEAN )";
                        id_val_02 = "sec";
                } else  {
                        id_txt_01 = "GI number";
                        id_val_01 = "gi";
                        id_txt_02 = "Accession ID";
                        id_val_02 = "acc";
                }

                sel_id_html += "<br>\n";
                sel_id_html += "<table width=\"100%\">\n";
                sel_id_html += "<tr>\n";
                sel_id_html += "<td width=\"15%\" class=\"box_left_left\">ID Type</td>\n";
		sel_id_html += "<td width=\"5%\" class=\"box_center_left\">:</td>\n";
                sel_id_html += "<td width=\"40%\" class=\"box_center_left\"><input type=\"radio\" name=\"sel_id_type\" value=\"" + id_val_01 + "\"> " + id_txt_01 + " </td>\n";
                sel_id_html += "<td width=\"40%\" class=\"box_center_left\"><input type=\"radio\" name=\"sel_id_type\" value=\"" + id_val_02 + "\" checked> " + id_txt_02 + " </td>\n";
                //sel_id_html += "<td width=\"20%\" class=\"box_center_left\"> </td>\n";
                //sel_id_html += "<td width=\"20%\" class=\"box_right_left\"> </td>\n";
                sel_id_html += "</tr>\n";
                sel_id_html += "</table><br>\n";
        }

        div_id_elem = document.getElementById("div_sel_id_type");
        div_id_elem.innerHTML = sel_id_html;

        // input //
        in_input_html = "";
        
        if (file_upload.sel_cont_type[3].checked) {
                in_input_html += "<table width=\"100%\">\n";
                in_input_html += "<tr>\n";
                in_input_html += "<td align=\"left\"><b>Protein List</b></td>\n";
                in_input_html += "</tr>\n";
                in_input_html += "<tr>\n";
                in_input_html += "<td><textarea name=\"list_area\" id=\"list_area\" rows=\"7\" style=\"{width : 100%}\"></textarea></td>\n";
                in_input_html += "</tr>\n";
                in_input_html += "</table>\n";
        } else {
                in_input_html += "<table width=\"100%\">\n";
                in_input_html += "<tr>\n";
                in_input_html += "<td align=\"left\"><b>File</b></td>\n";
                in_input_html += "</tr>\n";
                in_input_html += "<tr>\n";
                in_input_html += "<td>Select your file : <input type=\"file\" class=\"file\"  name=\"fasta_file\" id=\"id_fasta_file\"></td>\n";
                in_input_html += "</tr>\n";
                in_input_html += "</table>\n";		
	}

        div_in_input_elem = document.getElementById("div_input_file");
        div_in_input_elem.innerHTML = in_input_html;
}


function showIDSelection() {

        id_val_01 = null;
        id_val_02 = null;

        sel_id_html = "";
        
        if (file_upload.sel_cont_type[3].checked) {
                if (file_upload.sel_db[0].checked) {
                        id_txt_01 = "Primary ID ( e.g. Q4U9M9 )";
                        id_val_01 = "pri";
                        id_txt_02 = "Secondary ID ( e.g. 104K_THEAN )";
                        id_val_02 = "sec";
                } else  {
                        id_txt_01 = "GI number";
                        id_val_01 = "gi";
                        id_txt_02 = "Accession ID";
                        id_val_02 = "acc";
                }

                sel_id_html += "<br>\n";
                sel_id_html += "<table width=\"100%\">\n";
                sel_id_html += "<tr>\n";
                sel_id_html += "<td width=\"15%\" class=\"box_left_left\">ID Type</td>\n";
		sel_id_html += "<td width=\"5%\" class=\"box_center_left\">:</td>\n";
                sel_id_html += "<td width=\"40%\" class=\"box_center_left\"><input type=\"radio\" name=\"sel_id_type\" value=\"" + id_val_01 + "\"> " + id_txt_01 + " </td>\n";
                sel_id_html += "<td width=\"40%\" class=\"box_right_left\"><input type=\"radio\" name=\"sel_id_type\" value=\"" + id_val_02 + "\" checked> " + id_txt_02 + " </td>\n";
                //sel_id_html += "<td width=\"20%\" class=\"box_center_left\"> </td>\n";
                //sel_id_html += "<td width=\"20%\" class=\"box_right_left\"> </td>\n";
                sel_id_html += "</tr>\n";
                sel_id_html += "</table><br>\n";
        }

        div_id_elem = document.getElementById("div_sel_id_type");
        div_id_elem.innerHTML = sel_id_html;
}


function openNCBITaxIDSearchWindow() {
	
	window.open("./popup_find_ncbi_tax_id.jsp", "NCBI_Taxonomy_ID_Serarch", "toolbar=no, location=no, status=no, menubar=no, scrollbars=yes, resizable=no, left=0, top=0, width=500, height=500");
}
