// -------------------------------------------
//  *************  FUNCTIONS   *************
// --------------------------------------------

// ------------------------------------------------------------------
// 
// name:      | getX
// purpose:   | returns the intersection of elements from 2 arrays
// input:     | two array references
// returns:   | array containing intersection of input arrays
//
// ------------------------------------------------------------------

function getX( array1ref, array2ref )
{
    var retArray = new Array(); 
    
    for ( var o=0; o < array1ref.length; o++ )
    {
  	for ( var i=0; i < array2ref.length; i++ )
  	{
            if ( array1ref[o] == array2ref[i] )
            {
                retArray[retArray.length] = array1ref[o];
            }
  	}
    }

    return retArray;	
}


// ---------------------------------------------------------------------
// 
// name:      | getMX
// purpose:   | returns the intersection of elements from more than 2 arrays
// input:     | references to the unique category arrays
// returns:   | array containing the intersection of the uniqued arrays
//
// ---------------------------------------------------------------------

function getMX()
{
    var retXArray = new Array();

    retXArray = getX( getMX.arguments[0], getMX.arguments[1] );

    if ( getMX.arguments.length > 2 )
    {
  	for ( var x=2; x < getMX.arguments.length; x++ )
  	{
            retXArray = getX( retXArray, getMX.arguments[x] );

  	}
    }

    return retXArray;
}


// ---------------------------------------------------------------------
// 
// name:      | concatArrays
// purpose:   | takes two arrays and concats the second onto the first
// input:     | two array references
// returns:   | array
//
// ---------------------------------------------------------------------

function concatArrays( arrayref1, arrayref2 )
{	
    for ( var i=0; i < arrayref2.length; i++ )
    {	
        arrayref1[arrayref1.length] = arrayref2[i];
    }
}


// ---------------------------------------------------------------------
// 
// name:      | uniqArrays
// purpose:   | takes an array and returns an array of the unique values
// input:     | an array ref
// returns:   | array
//
// ---------------------------------------------------------------------

function uniqArrays(arrayref)
{
  var localhash = new Object;
  var returnarray = new Array();

  for ( var a=0; a < arrayref.length; a++ )
  {
      if ( ! localhash[arrayref[a]] )
      {
          localhash[arrayref[a]] = "";
          returnarray[returnarray.length] = arrayref[a];
      }
  }
  
  return returnarray;
}


// --------------------------------------------------------------------
// 
// name:      | setRegions
// purpose:   | finds the common set of regions corresponding to matched colls
// input:     | reference to the form to update
// returns:   | the regintersection array is the list of valid regions
//
// ------------------------------------------------------------------


function setRegions( db_form )
{
    var numberchecked = 0;
    var checkednames = new Array();
    var regintersection = new Array(); // the intersection of the region values
    var ele = "";	
    
    // loop through all the form elements
    
    for ( var i = 0; i < db_form.elements.length; i++ ) 
    {
        ele = db_form.elements[i];
        if ( ele.type == "checkbox" )
        {
            if ( ele.checked )
            {
                checkednames[numberchecked] = getRegions( eval( ele.value ) );	
                numberchecked++;
            }
        }
        
    }
    
    if ( numberchecked == 0)
    {
        regintersection[0] = defaultItem;
    }
    
    if ( numberchecked == 1)  
    {
        regintersection = checkednames[0];
    }
    
    if ( numberchecked == 2)  
    {
        regintersection = getX( checkednames[0],checkednames[1] );
    }

    if ( numberchecked > 2) 
    { 
        regintersection = getX( checkednames[0],checkednames[1]);
        for ( var u = 2; u < numberchecked; u++)
        {
            regintersection = getX( regintersection,checkednames[u]);
        }
    }

    setQueryRegionOptions( regintersection );

    return;
}

// ------------------------------------------------------------------
// 
// name:      | getRegions
// purpose:   | grabs the valid regions values
//            | from each selected collection data array, and returns the 
//            | region list in an array
// input:     | a collection array reference.
// returns:   | an array of region values
//
// ------------------------------------------------------------------

function getRegions( collarrayref )
{
    var regsArray = new Array();

    for ( var i = 0; i < collarrayref.length; i++ )
    {
        regsArray[regsArray.length] = collarrayref[i];	
    }

    return regsArray;
}


// ------------------------------------------------------------------
// 
// name:      | setQueryRegionOptions
// purpose:   | rebuilds the options inside the region select elements
// input:     | takes the region intersection array from setRegions
// returns:   | none
//
// ------------------------------------------------------------------

function setQueryRegionOptions( rgnxArray )
{		
    parent.document.search.rgn.options.length = 0;

    for ( var i=0; i < rgnxArray.length; i++)
    {
        parent.document.search.rgn.options[i] = new Option( rgnxArray[i], rgnxArray[i] );
        if ( i == 0 ) 
        {
            parent.document.search.rgn.options[i].selected = true;
        }
    }
        
    if ( rgnxArray.length == 0 )
    {
        // This is an error condition in the colldb
        parent.document.search.rgn.options[0] = new Option( "ERROR: No common regions", "full text" );
        parent.document.search.rgn.options[0].selected = true;
    }

    // To rebuild the select correctly
    if ( navigator.appname == "Netscape" )
    {
        history.go(0);
    }

}

// ---------------------------------------------------------------------
// 
// name:      | hasValue
// purpose:   | see if value is in given array
// input:     | arrayref, value
// returns:   | true/false
//
// ---------------------------------------------------------------------

function hasValue( arr, val )
{
    for ( var i=0; i < arr.length; i++ )
    {
        if ( arr[i] == val )
        {
            return true;
        }
    }

    return false;
}


// ---------------------------------------------------------------------
// 
// name:      | getColls
// purpose:   | checks the option value for a given select and if that
//            | value exists in the collection.selectname array, sets
//            | a flag in the checked colls array
// colled by: | any change in the categories select elements
// input:     | takes form reference /  array names colls[] checkedColls[]
//              and dynamically constructed collection.selectname array names
// returns:   | checked array of collections
//            | which is passed on to setColCkList
//
// ---------------------------------------------------------------------

function getColls( db_form )
{
    // build and initialize the checkedColls array.  
    var checkedColls = new Array();
    for ( var i=0; i < colls.length; i++ )
    {
        checkedColls[i] = new Array();
        checkedColls[i][0] = colls[i]; 
        checkedColls[i][1] = false;  // not checked by default
    }

    // for each coll see if it supports any checked option for each select if so
    // turn it's check mark on
    for ( var i=0; i < colls.length; i++ )
    {
        var markThisColl = true;

        for ( var j=0; j < db_form.elements.length; j++ ) 
        {
            var ele = db_form.elements[j];
            if ( ( ele.name == "genre" ) || ( ele.name == "period") ||
                 ( ele.name == "language" ) || ( ele.name == "gender" ) )
            {
                // build array name holding data for a given coll about
                // this select menu
                var arrCollSelect = eval( ele.name + colls[i] );  
                var collHasThisOption = false;
 
                // if option is checked set flag if option is in
                // the given collection
                for ( var k=0; k < ele.options.length; k++ )
                {
                    if ( ele.options[k].selected ) 
                    {
                        if ( ( ele.options[k].value == "All" ) ||
                             ( hasValue( arrCollSelect, ele.options[k].value ) ) )
                        {
                            collHasThisOption = true;
                        }
                    }
                }

                markThisColl &= collHasThisOption;
            }
        }

        checkedColls[i][1] = markThisColl;
    }

    // update the checklist .
    setCollCheckList( parent.document.search, checkedColls ); 

    // update the region pulldown based on the checked items in the
    // collection list
    setRegions( parent.document.search );

    return;
} 


// --------------------------------------------------------------------------------------
// 
// name:      | setCollCheckList
// purpose:   | updates the form containing the list of collections 
//            | by checking the checkboxes of those colls that match the
//            | characteristics selected in the collsbyparam form
// input:     | reference to the form to update and list of matching colls
// returns:   | updates the proper checkboxes
//
// --------------------------------------------------------------------------------------

function setCollCheckList( collListForm, checkedArray )
{
    var ele = "";
    
    // loop through all the form elements
    for ( var i=0; i < collListForm.elements.length; i++ ) 
    {
        ele = collListForm.elements[i];
        if (ele.type == "checkbox")
        {
            for ( var r=0; r < checkedArray.length; r++ )
            {
                if ( checkedArray[r][0] == ele.value )
                {
                    ele.checked = checkedArray[r][1];
                    break;
                }
            }
        }
    }
}


// ------------------------------------------------------------------
// 
// name:      | PageInit
// purpose:   | perform onLoad functions per page
// colled by: | onLoad handler
// calls:     | none
// input:     | none
// returns:   | none
//
// ------------------------------------------------------------------
function PageInit( pagetype )
{
    clearSelectOptions( );
    
    if ( pagetype == 'extended' )
    {
        setRegions( parent.document.search );
    }
}

// ------------------------------------------------------------------
// 
// name:      | clearSelectOptions
// purpose:   | rip out dummy OPTIONS needed to display dropdown menu 
//            | correctly in Netscape 4.x
// colled by: | PageInit
// calls:     | none
// input:     | none
// returns:   | none
//
// ------------------------------------------------------------------

function clearSelectOptions( )
{
	var ele = "";
	for (var i = 0; i < parent.document.search.elements.length; i++) 
	{
		ele = parent.document.search.elements[i];
		if (ele.type == "select-one")
		{

		  while ( ele.options[0].text.indexOf( "___" ) != -1 )
		    {
		      ele.options[0] = null;
		    }
  		}
  	}
}






// ------------------------------------------------------------------
// 
// name:      | formValidate
// purpose:   | Checks to see if at least one collection is selected;
//            | sends an alert if not.
// colled by: | onsubmit attribute event handler in ext search form
// calls:     | none
// input:     | none
// returns:   | none
//
// ------------------------------------------------------------------

function formValidate (theForm)
{
	var cbswitch = 0;
	for (var i=0; i < theForm.elements.length; i++)
	{
		if ((theForm.elements[i].type == 'checkbox') && (theForm.elements[i].name == 'c'))
		{
			if (theForm.elements[i].checked == true) { cbswitch = 1 }
		}
	}
	
	if (cbswitch == 0)
	{
		alert('You must sellect at least one collection to search');
		return false;
	}
	if (cbswitch == 1) { return true }
}




// ------------------------------------------------------------------
// 
// name:      | checkAllRecords
// purpose:   | used to check all the collection checkboxes 
//            | 
// colled by: | Clicking the "check all collections" button on 
//	      | the search form
// calls:     | none
// input:     | form reference
// returns:   | none
//
// ------------------------------------------------------------------

function checkAllRecords(theForm)
{
	var ele = "";
	for (var i = 0; i < theForm.elements.length; i++) 
	{
		ele = theForm.elements[i];
		if ((ele.type == "checkbox") && (ele.name == 'c'))
		{
			ele.checked = true;
  		}
  	}
}


// ------------------------------------------------------------------
// 
// name:      | clearAllRecords
// purpose:   | used to uncheck all the collection checkboxes 
//            | 
// colled by: | Clicking the "uncheck all collections" button on 
//	      | the search form
// calls:     | none
// input:     | form reference
// returns:   | none
//
// ------------------------------------------------------------------

function clearAllRecords(theForm)
{
	var ele = "";
	for (var i = 0; i < theForm.elements.length; i++) 
	{
		ele = theForm.elements[i];
		if ((ele.type == "checkbox") && (ele.name == 'c'))
		{
			ele.checked = false;
  		}
  	}
}
