function submit_search_form(event, page_number){
    if (event) {event.preventDefault(); }

    var obj, dbParam, xmlhttp, myObj, x, txt = "";
    page_number = page_number || 0;
    if (page_number == 0) {
        search_query = document.forms["search_form"]["search_query"].value;
        lang = document.forms["search_form"]["language"].value;
        conf_id = document.forms["search_form"]["conf_ids"].value;
    }
    obj = { "search_query":search_query, "conf_id":conf_id, "page_number":page_number,"lang":lang };
    //obj = { "search_query":"gene", "conf_id":"all", "page_number":page_number };
    //dbParam = JSON.stringify(obj);
    dbParam = obj;
    console.log(dbParam);
    $.post("/s2t/search_backend",obj)
    .done( data => {
        console.log(data)
        display_results(data, lang)
    })
    
    //for local testing:
    //var data = responseData;
    //display_results(data,lang);


    //$.getJSON("fakeS2t_searchResponse.json", function(json,lang) {
    //    console.log(json);
    //    console.log(lang);
    //    display_results(json,lang);
//
    //});



    current_page = page_number;
}

function secondsToHms(d) {
    d = Number(d);
    var h = Math.floor(d / 3600);
    var m = Math.floor(d % 3600 / 60);
    var s = Math.floor(d % 3600 % 60);

    var hDisplay = h > 0 ? h + (h == 1 ? " hour, " : " hours, ") : "";
    var mDisplay = m > 0 ? m + (m == 1 ? " minute, " : " minutes, ") : "";
    var sDisplay = s > 0 ? s + (s == 1 ? " second" : " seconds") : "";
    return hDisplay + mDisplay + sDisplay; 
}


function display_results(data, lang){
    myObj = JSON.parse(data)
    //myObj = data;    
    $(".search_result").each(function() { $(this).remove(); });

    var txt = "";
    //txt += "<table border='1'>";
    //txt += "<tr><th>Segment Text</th><th>Conference</th><th>Date</th><th>Time</th></tr>";
    for (x of myObj["hits"]["hits"]) {
        txt += '<tr class="search_result">';  
        start_time =  x["_source"]["start_time"];
        date_ = x["_source"]["recording_id"].split("_").slice(2,3);

	//console.log("DATE: " + date_ + " full string: " + x["_source"]["recording_id"]);
        recording_id = x["_source"]["recording_id"];
        conference_id = x["_source"]["conference_id"];
        recording_name = x["_source"]["recording_name"];
        //vod_url =  "search/" + recording_id + "+" + recording_name + "+" + start_time;
	vod_url =  "/s2t/" + conference_id + "/" + recording_id + ".html" + "?start_time="  + start_time;

	if (lang == 'en') {
            lang_specific_segment_text="segment_text";
        }
        else{
            lang_specific_segment_text = "segment_text_mt_"+lang;
        }

        txt += "<td><a href='" + vod_url +"'>" + x["highlight"][lang_specific_segment_text] + "</a></td>";
        txt += "<td>" + conference_id + "</td>";
        txt += "<td>" + date_ + "</td>";
        txt += "<td>" + secondsToHms(start_time) + "</td>";
        txt += "</tr>";
    }
    //txt += "</table>";
    //document.getElementById("demo").innerHTML = txt;
    $("#search_results").append(txt);
}

function checkForInitialSearch() {
	var urlString = window.location.href;
	var url = new URL(urlString);
	var query = url.searchParams.get("search_query");

	//console.log(query);

	if (query) {
		var setCommittee = url.searchParams.get("conf_id");
		if (setCommittee) {
			$("#conf_id").val(setCommittee);
			console.log("Committee: " + setCommittee);
		}

		var setLanguage = url.searchParams.get("language");
		if (setLanguage) {
			$("#search_language").val(setLanguage);
		}

		$("#search_query").val(query);

		submit_search_form(null, 0);
	}	
}

