var mapWindow;
var gSearchType;

function SubmitgoToMap(dataid, layer_type, colored_by){
	var theForm = window.document.goToMap;

    if (window.opener && !window.opener.closed && window.opener.document.title.indexOf("Map Service") >= 0){
        mapWindow = window.opener;
    } else if (mapWindow && !mapWindow.closed  && mapWindow.document.title.indexOf("Map Service") >= 0){
       mapWindow = mapWindow;
    } else {
        mapWindow = open("", "mapservice");
    }

    if (mapWindow.document.forms[0] && !mapWindow.closed) {
        mapWindow.focus();
        var mapForm = mapWindow.document.forms['main'];
        mapForm.LAYERMANAGER_ACTION.value = "ADD";

        mapForm.layer_type.value = layer_type;
        mapForm['dataid[]'].value = dataid;

        mapForm.coloredBy.value = colored_by;
        switch (colored_by) {
        case "dataset":
        case "_dataset":
            tab_index = 1;
            break;
        case "species":
        case "_sp_tsn":
            tab_index = 0;
            break;
        case "series":
        case "_series":
            tab_index = 2;
            break;
        default:
            tab_index = 0;
        }

       	mapForm['tab_index'].value = tab_index;

        if (theForm && theForm['includeEffort'] && theForm['includeEffort'].value == 'true') {
        	mapForm['includeEffort'].value = 'true';
        }
        mapForm.submit();
    } else {
        theForm['dataid[]'].value = dataid;
        theForm['coloredBy'].value = colored_by;
        theForm['dataid[]'].value = dataid;
        if (theForm['add']) {	// For new Mapper
			theForm['add'].value = dataid;
        }

        if (document.forms['FilterForm'] && theForm['serialized_param']) {
        	theForm['serialized_param'].value = $('FilterForm').serialize();	// New Mapper requires serialized_param
        }
        theForm.submit();
    }

    return false;
}


function show_quick_search() {
	var divName = "ajax_species_search";
	gSearchType = "species";
	determineBoxPosition("result_list");

	close_button = false;
	var objDiv = getObject(divName);
	objDiv.style.display = "block";

}

function submit_from_quick_search() {
	// Only one species is added to a map
	var criteriaTextBox = document.forms['search']['species'];
	var species = criteriaTextBox.value;
	species = species.replace(/\r/g,'');
	species = species.replace(/\n/g,'');
	species = Trim(species);
	//alert("species selected :" + species + ".");
	var url = g_SPECIES_URL + 'ajax_species_search.php';
	var get_params = 'sci=' + criteriaTextBox.value;
	url += "?" + get_params;
	sendRequest2(add_from_quick_search, '', 'GET', url, false);


}

function add_from_quick_search(oj) {
	var tsn  = oj.responseText;
	tsn = tsn.replace(/\r/g,'');
	tsn = tsn.replace(/\n/g,'');
	tsn = Trim(tsn);
	var RegExp = /^(-)?(\d*)(\.?)(\d*)$/;
	if (tsn.match(RegExp) == 0 || tsn.length <= 1 || tsn == " ") {
		alert("species not found or no records for the species found.");
		return false;
	}

	var mapForm = document.forms['main'];
    mapForm.LAYERMANAGER_ACTION.value = "ADD";

	//alert("species added :" + tsn + ".");
    mapForm.layer_type.value = "species";
    mapForm['dataid[]'].value = tsn;

    mapForm.coloredBy.value = "_dataset";
    tab_index = 1;
    mapForm['tab_index'].value = tab_index;
    mapForm.submit();

}

function show_extraction(dummy, sid) {
	//alert(sid);
	var param = "&" + sid;
	var url = g_STAT_CENTER_URL + "index_content.php?map_mode=normal" + param;
	//var url = g_EXTRACTION_URL + "index_content.php?map_mode=normal" + param;
	window.open(url, "extraction");
}