/*
 * Returns the current mouse position
 * as an object.
 */
function getMousePos(e)
{
  var IE = document.all ? true : false;

  if (IE)
  {
    var mousePos = {
      x: event.clientX + document.documentElement.scrollLeft,
      y: event.clientY + document.documentElement.scrollTop
    };
  }
  else
  {
    var mousePos = {
      x: e.pageX,
      y: e.pageY
    };
  }

  return mousePos;
}

/*
 * Creates a popup window
 * based on mouse position.
 */
function popup(e, url, width, height)
{
  var mouse = getMousePos(e);
  
  if (!width) { width = 400; }
  if (!height) { height = 200; }
  
  WindowObjectReference = window.open(url, 'Information',"menubar=no,location=no,resizable=yes,scrollbars=yes,left=" + (mouse.x - 300) + ",top=" + (mouse.y - 150) + ",width=" + width +",height=" + height);
}

function getCookie( name ) {
  var start = document.cookie.indexOf( name + "=" );
  var len = start + name.length + 1;
  if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
    return null;
  }
  if ( start == -1 )
  {
    return null;
  }
  var end = document.cookie.indexOf( ";", len );
  if ( end == -1 ) end = document.cookie.length;
  return unescape(document.cookie.substring( len, end ));
}

function setCookie( name, value, expires, path, domain, secure ) {
  var today = new Date();
  today.setTime( today.getTime() );
  if ( expires ) {
    expires = expires * 1000 * 60 * 60 * 24;
  }
  var expires_date = new Date( today.getTime() + (expires) );
  document.cookie = name+"="+escape( value ) +
    ( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) + //expires.toGMTString()
    ( ( path ) ? ";path=" + path : "" ) +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
}

function deleteCookie( name, path, domain ) {
  if ( getCookie( name ) ) document.cookie = name + "=" +
                             ( ( path ) ? ";path=" + path : "") +
                             ( ( domain ) ? ";domain=" + domain : "" ) +
                             ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function checkForPrompt(obj_form, fieldname, default_value, error_message)
{
  if (obj_form[fieldname].value == default_value || obj_form[fieldname].value == '')
  {  
    alert(error_message); 
    return false;
  }
}
function clearEmptyPrompt(obj_textbox, default_prompt)
{
  if (obj_textbox.value == default_prompt)
  {
    obj_textbox.value = '';
  }
}


/* ------------------------------------------------
 * Functions to pre-populate and empty input boxes
 * ------------------------------------------------
 */

  function clearInput(field, defaultValue){
  if (field.value == defaultValue) {
     field.value = '';
   }
  } 
  
  function fillInput(field, defaultValue){
   if (field.value == '') {
     field.value = defaultValue;
   }
  }  

/* ----------------------------------------------------------------------
 * Functions to verify that search box is not submitted default or blank
 * ----------------------------------------------------------------------
 */

  function checkSearchForm(form){  
   if (($('#frm_keywords').val() == '')||($('#frm_keywords').val() == 'Search')) {
     alert("Please enter a keyword to look for");
     return false;
   }else{
     return true;
   }
  }    

/* -----------------------------------------------
 * Functions to run as soon as the page is loaded
 * -----------------------------------------------
 */

  $(document).ready(function(){
    if($('#per_page_select').length > 0){
      $('#per_page_select').sSelect();
      }
  });