/*------------------------------------------------------------------------------------------------*\
  File:     general.js
  Abstract: contains general javascript functions for use throughout the application.
\*------------------------------------------------------------------------------------------------*/



/*------------------------------------------------------------------------------------------------*\
  Function:    display_dhtml_page_nav
  Description: function to display DHTML page navigation for lists. This function hides/shows the 
               current page - all of which exist in the page but are hidden with CSS. It also 
               updates other aspects of the pagination: the "viewing X-Y" section, the >> and << 
               links and 
  Assumption:  the PHP counterpart function with the same name has been called in the calling page. 
               That function sets up the 
  Parameters:  page         - the current page number
               total_pages  - the total number of pages
               num_per_page - the number of items listed per page
\*------------------------------------------------------------------------------------------------*/
function display_dhtml_page_nav(num_results, num_per_page, current_page)
{
  total_pages = Math.ceil(num_results / num_per_page);

  // hide/show the appropriate pages
  for (i=1; i<=total_pages; i++)
  {
    if (current_page == i)
    {
      document.getElementById("page_" + i).style.display = "block";
      document.getElementById("nav_page_" + i).innerHTML = "<b>" + i + "</b> ";
    }
    else
    {
      document.getElementById("page_" + i).style.display = "none";
      document.getElementById("nav_page_" + i).innerHTML = "<a href='javascript:display_dhtml_page_nav(" + 
        + num_results + ", " + num_per_page + ", " + i + ")'>" + i + "</a> ";
    }
  }
  
  // update the "Viewing X-Y" text 
  var tmp = (current_page - 1) * num_per_page;
  var max_end = tmp + num_per_page;
  var end = (max_end > num_results) ? num_results : max_end;
  var start = tmp + 1;
  
  document.getElementById("nav_viewing_num_start").innerHTML = start;
  document.getElementById("nav_viewing_num_end").innerHTML = end;
  
  
  // update the navigation links: <<
  if (current_page > 1)
  {
    previous_page = current_page - 1;
    document.getElementById("nav_previous_page").innerHTML = "<a href='javascript:display_dhtml_page_nav(" + 
      + num_results + ", " + num_per_page + ", " + previous_page + ")'>&lt;&lt;</a> ";
  }
  else
    document.getElementById("nav_previous_page").innerHTML = "&lt;&lt;";

  // >>
  if (current_page < total_pages)
  {
    next_page = current_page + 1;
    document.getElementById("nav_next_page").innerHTML = "<a href='javascript:display_dhtml_page_nav(" + 
      + num_results + ", " + num_per_page + ", " + next_page + ")'>&gt;&gt;</a> ";  
  }
  else
    document.getElementById("nav_next_page").innerHTML = "&gt;&gt;";
  
}



/*------------------------------------------------------------------------------------------------*\
  Function:    add_option
  Description: adds a new option to a select dropdown box.
  Parameters:  selectbox    - the select box object
               total_pages  - the display text of the new dropdown option
               value        - the value of the new dropdown option
\*------------------------------------------------------------------------------------------------*/
function add_option(selectbox, text_val, value)
{
  var new_option = new Option(text_val, value);
  var sel_length = selectbox.length;
  selectbox.options[sel_length] = new_option;
}

function delete_option(selectbox, ind)
{ 
  var sel_length = selectbox.length;
  if (sel_length > 0)
    selectbox.options[ind] = null;
}

function move_options(sel_from, sel_to)
{  
  var sel_length = sel_from.length;
  var sel_texts  = new Array();
  var sel_vals   = new Array();
  var sel_count = 0;

  var i;

  // find the selected Options in reverse order and delete them from the 'from' Select
  for (i=sel_length-1; i>=0; i--)
  {
    if (sel_from.options[i].selected)
    {
      // if there's no value, that means the lawyer is away. Don't move them.
      if (sel_from.options[i].value == "")
        continue;
      
      sel_texts[sel_count] = sel_from.options[i].text;
      sel_vals[sel_count]  = sel_from.options[i].value;
      delete_option(sel_from, i);
      sel_count++;
    }
  }

  // add the selected text/values in reverse order. This will add the Options to the 'to' Select
  // in the same order as they were in the 'from' Select
  for (i=sel_count-1; i>=0; i--)
    add_option(sel_to, sel_texts[i], sel_vals[i]);
}


// helper function used to select all options in a multi-select dropdown. This is used prior
// to submitting a form, to ensure the contents are passed along to the server. 
function select_all(el)
{
  // loop through each and select them (this passes them along to the server)
  for (i=0; i<el.length; i++)
    el[i].selected = true;

  return true;
}

function flevPopupLink(){// v1.2
var v1=arguments,v2=window.open(v1[0],v1[1],v1[2]), v3=(v1.length>3)?v1[3]:false;if (v3){v2.focus();}document.MM_returnValue=false;
}

function subrstr(str,nbr) {
   return str.substr(str.length-nbr);
   // return str.slice(-nbr)
}

function findObj(n, d) { 
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
  d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function openWindow(theURL,winName,features) {
  window.open(theURL,winName,features);
  return false;
}

/*********************************************************/
/* Open Search window                                    */
/*********************************************************/

function search_window(keyword, section, where)
{
	path = where+'?keyword='+keyword+'&section='+section;
	SearchWindow = window.open("","Search","toolbar=no,location=no,status=no,scrollbars=yes,width=600,height=500,innerheight=50,screenX=100,screenY=100,pageXoffset=100,pageYoffset=100,resizable=yes");          

	if (SearchWindow.frames.length == 0) 
	{
		SearchWindow = window.open(path,"Search","toolbar=no,location=no,status=no,scrollbars=yes,width=700,height=600,innerheight=50,screenX=100,screenY=100,pageXoffset=100,pageYoffset=100,resizable=yes");
	}
	else
	{
		SearchWindow.location.href = path;
		SearchWindow.focus();
	}
}


function textCounter(field,cntfield,maxlimit) {
if (field.value.length > maxlimit)
field.value = field.value.substring(0, maxlimit);
else
cntfield.value = maxlimit - field.value.length;
}

