// JavaScript Document


//~ this is a poorly named function because it will actually open or close a box, it just changes the state
function openBox(elId)
    {
	var el = document.getElementById(elId);
	if (el.style.display == 'block') {el.style.display = 'none';}
	else { el.style.display = 'block'; }
    }

//~ This function opens multiple boxes
function openBoxes(boxes){
    for( var i=0; i<boxes.length; i++ ){
        openBox( boxes[i] );
    }
}

//~ This function ONLY opens a div
function openDiv(elID)
{
    var el = document.getElementById(elID);
    el.style.display='block';
}

//~ This function ONLY closes a div
function closeDiv(elID)
{
    var el = document.getElementById(elID);
    el.style.display='none';
}



function checkForSelectedValues(){
    var experiments_select = document.getElementById('experiments_select');
    var path_location_excl_select = document.getElementById('path_location_excl_select');
    var path_location_incl_select = document.getElementById('path_location_incl_select');
    var path_type_excl_select = document.getElementById('path_type_excl_select');
    var path_type_incl_select = document.getElementById('path_type_incl_select');
    
    if(experiments_select.selectedIndex != -1){
        document.getElementById('experiment_div').style.display = 'block';
    }
    if(path_location_excl_select.selectedIndex != -1){
        document.getElementById('pathology_location_div').style.display = 'block';
    }
    if(path_location_incl_select.selecteIndex != '-1'){
       //alert(path_location_excl_select.selectedIndex);
        document.getElementById('pathology_location_div').style.display = 'block';
    }
    //alert('loc ' + path_type_excl_select.selectedIndex + ' ' + path_type_incl_select.selectedIndex  )
    //if(path_type_excl_select.selectedIndex == -1){
    if(path_type_excl_select.selectedIndex != -1){
        document.getElementById('pathology_type_div').style.display = 'block';
    }
    if(path_type_incl_select.selecteIndex != -1 ){
       document.getElementById('pathology_type_div').style.display = 'block';
    }
}

    
//~ Replaces the inner html of a select box with the results of an SQL query
function selectOptions( selectID, table, select_column, select_by, select_by_value)
    {
    var selectBox = document.getElementById( selectID );
    selectBox.innerHTML = '<option value=Any> Any </option>';
    var ajaxRequest = new XMLHttpRequest();
    var GETrequest = '?table=' + table + "&select_column=" + select_column + "&select_by=" + select_by + "&select_by_value=" + select_by_value;

    // Places the SQL result in DOM when ajax is done retrieving it from PHP
	ajaxRequest.onreadystatechange = function()
        {
		if(ajaxRequest.readyState == 4)
            {
            selectBox.innerHTML = '<option value=Any> Any </option>';
			selectBox.innerHTML += ajaxRequest.responseText;
		    }
	    };
    
    //request a page
    ajaxRequest.open("GET", "scripts/selectBoxQuery.php" + GETrequest , true);
    ajaxRequest.send(null); 
    }

function search_load(){
    loadHiddenDivs( ['cobalt_fractionated_details', 'cobalt_chronic_details', 'dog_details'] );
    if( loadSelections(document.getElementById('search_form'))  == 'no cookie' )
        { 
        formReset(document.getElementById('search_form')) ;
        }
    
    //~ really terrible hack to ensure that the dynamically loaded organ sub category reloads
    var organ_type = document.getElementById('organ_types');
    selectOptions( 'organ_select', 'organ_types', 'organ', 'organ_type', organ_type.value);
    document.getElementById("submit").value = 'search';
}
                                    
function checkPresent(elid, errorMessage)
    {
    var element = document.getElementById( elid );
    if( element.value == '')
        {
        alert(errorMessage);
        return false;
        }
    else
        {
        return true;
        }
    }
    
function checkSame( elid, matching_elid, errorMessage )
    {
    var element = document.getElementById( elid );
    var matching_element = document.getElementById( matching_elid );
    if( element.value != matching_element.value )
        {
        alert(errorMessage);
        return false;
        }
    else
        {
        return true;
        }
    }
    

function validate_email( elid, errorMessage )
    {
    var element = document.getElementById( elid );
    var value = element.value;
    var at_postion = value.indexOf("@");
    var dot_postition = value.lastIndexOf(".");
    var distance = dot_postition - at_postion;
    if ( at_postion < 1 || distance < 2)
        {
        alert(errorMessage);
        return false;
        }
    else 
        {return true;}
    }
                                    
                                    
                                    