
var formDateLabels = {date1:"The beginning date",date2:"The ending date"};

function isaPosNum( s ) 
{
    return ( parseInt( s ) > 0 );
}

function dateCheck( item, min, max )
{
    var pluginDate = item.name;
        
    if ( ! isaPosNum( item.value ) )
    { 
        alert( formDateLabels[pluginDate] + 
               " must be a positive number between" + 
               min + " and " + max );
        return false;
    }
    
    if ( parseInt( item.value ) < min )
    { 
        alert( formDateLabels[pluginDate] + 
               " must be greater than " + 
               min + " and less than " + 
               max + "\n\nPlease re-enter it" );
        return false;
    }
    
    if ( parseInt( item.value ) > max )
    { 
        alert( formDateLabels[pluginDate] + 
               " must be greater than " + 
               min + " and less than " + 
               max + "\n\nPlease re-enter it" );
        return false;
    }
    
    return true;
}

function validateAndSubmit( theform, min, max, fieldsList  )
{
	// At least one searchterm must be provided
	var lNoInput = true;
	
	/* Argument "fieldsList" is of variable length and is supposed
	to contain a list of names of inputs of which at least one must
	have a value. */
	for (var i=3; i<arguments.length; i++) {
		// remove 'direct search' instruction from textfield
		if (arguments[i].value == "direct search") arguments[i].value = '';
		if (arguments[i].value.replace(/(^\s*)|(\s*$)/g, "") != '') lNoInput = false;
	}
	
	if (lNoInput) {
		alert ("Please enter at least one search term.");
		return false;
	}

	// Check validity of date interval
	if ( theform.date1 && theform.date2 ) {
	
	    if ( dateCheck( theform.date2, min, max ) ) {
	        if ( dateCheck( theform.date1, min, max ) ) {
	            return true;
	        }
	        else {
	            return false;
	        }
	    }
	    else {
	        return false;
	    }
	}
	else return true;	
}

// uncheckAllRecords.
// take a FORM object as an argument, and set all the checkbox objects
// in that form to not "checked", return false

function uncheckAllRecords( theform )
{
  var ele = "";

  for ( var i = 0; i < theform.elements.length; i++)
  {
      ele = theform.elements[i];
      
      if (ele.type == "checkbox")
      {
          ele.checked = false;
      }
  }

  return false;
}

/* Open index popup */
function openIndex(oInput,index,url) {

	/* append arguments to URL */
	var s
	if (url.indexOf("?") != -1) s=";"; else s="?";
	url = url + s + "field=" + escape(oInput.name) + ";index=" + escape(index);
	if (oInput.value) url = url + ";start=" + escape(oInput.value);
	indicesWindow = window.open( url, "indices", "scrollbars=yes,resizable=yes,width=450,height=400" );
	indicesWindow.focus();
}


function validateAndSubmitExpert( theform, min, max, fieldsList  )
{
	// At least one searchterm must be provided
	var lNoInput = true;
	
	/* Argument "fieldsList" is of variable length and is supposed
	to contain a list of names of inputs of which at least one must
	have a value. */
	for (var i=3; i<arguments.length; i++) {
		// remove 'direct search' instruction from textfield
		alert( i + ":" + arguments[i] );
		if (arguments[i].value == "direct search") arguments[i].value = '';
		if (arguments[i].value.replace(/(^\s*)|(\s*$)/g, "") != '') lNoInput = false;
	}
	
	if (lNoInput) {
		alert ("Please enter at least one search term.");
		return false;
	}

	// Check validity of date interval
	if ( theform.date1 && theform.date2 ) {
	
	    if ( dateCheck( theform.date2, min, max ) ) {
	        if ( dateCheck( theform.date1, min, max ) ) {
	            return true;
	        }
	        else {
	            return false;
	        }
	    }
	    else {
	        return false;
	    }
	}
	else return true;	
}



