// --------------------------------------------------------------------- --->
// name:    utilities.js                                                 --->
// author:  todd@hampson.us                                              --->
// date:    2010-04-06                                                   --->
// purpose: HRM Utilities                                                --->
// mods:    2008-12-01 - create for HRM                                  --->
// --------------------------------------------------------------------- --->

// global vars

var IE = document.all?true:false;
if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMouseXY;
var tempX = 0;
var tempY = 0;

var screenWidth,screenHeight;
if (self.innerHeight) // all except Explorer
{
  screenWidth = self.innerWidth;
  screenHeight = self.innerHeight;
}
else if (document.documentElement && document.documentElement.clientHeight)
// Explorer 6 Strict Mode
{
  screenWidth = document.documentElement.clientWidth;
  screenHeight = document.documentElement.clientHeight;
}
else if (document.body) // other Explorers
{
  screenWidth = document.body.clientWidth;
  screenHeight = document.body.clientHeight;
}

function getMouseXY(e)
{
  if (IE)
    { // grab the x-y pos.s if browser is IE
      tempX = event.clientX + document.body.scrollLeft;
      tempY = event.clientY + document.body.scrollTop;
    }
  else
  {  // grab the x-y pos.s if browser is NS
  tempX = e.pageX;
  tempY = e.pageY;
  }  
  if (tempX < 0){tempX = 0;}
  if (tempY < 0){tempY = 0;}
  if (document.mainform)
  {
    try {
      document.mainform.xcoord.value = tempX;
      document.mainform.ycoord.value = tempY;
      // alert('in getMouseXY: x = ' + tempX + ', y = ' + tempX);
    } catch(error) {
      ;
    }
  }
  return true;
}

function notAvailable ()
{
  alert('Sorry, this function is currently not available.');
}

function resetSearch()
  {
    document.mainform.searchcode.value = '';
    document.mainform.searchtext.value = '';
}

function confirmSearch()
{
  if ( document.mainform.searchcode.value == '' && document.mainform.searchtext.value == '' )
  {
    if ( confirm ('This is a very general selection. Do you wish to continue?') == true )
    {
      document.mainform.action.value = 'search';
      document.mainform.submit();
    }
  }
  else
  {
    document.mainform.action.value = 'search';
    document.mainform.submit();
  }
}

function setFilename (fn)
{
  document.mainform.filename.value = fn;
  null;
}

function popupWindow (url,name,opt) // correct order
{
  // alert ('in popupWindow');
  if (newwin) {alert ('newwin exists'); newwin.close();}
  if (opt == '' || opt == 'default')
  {
    opt = 'height=500,width=600,resizable=1,scrollbars=1,toolbar=0,menubar=0,titlebar=0';
  }
  var newwin = window.open(url,name,opt);
  newwin.focus();
}

function helpWindow (n) // correct order
{
  opt = 'height=600,width=600,resizable=1,scrollbars=1,toolbar=0,menubar=0,titlebar=0';
  var newwin = window.open('SFHelp.cfm?fn='+n,'HelpText',opt);
  newwin.focus();
}

function blurThis()
{
  alert('Sorry, this item is read-only.');
  window.focus();
}

function printWindow()
{
  window.print();
  return false;
}

function refresh ()
{
  history.go(0);
}

function reload ()
{
  history.go(0);
}

function changeOrder(col)
{
  document.mainform.orderby.value = col;
  document.mainform.submit();
}

function changeOrderAlt(col) // alternate ASC/DESC
{
  document.mainform.orderby.value = col;
  if (document.mainform.orderby.value == document.mainform.lastorderby.value)
  {
    if (document.mainform.ascdesc.value == "ASC")
    {
      document.mainform.ascdesc.value = "DESC";
    }
    else
    {
      document.mainform.ascdesc.value = "ASC";
    }
  }
  if ( document.mainform.rq ) {document.mainform.rq.value = 'no';};
  document.mainform.submit();
}

function clearSort()
{
  if ( confirm('Are you sure you want to clear this sort?') == true )
  {
    document.mainform.orderby.value = "";
    document.mainform.ascdesc.value = "";
    document.mainform.orderbylist.value = "";
    document.mainform.submit();
  }
}

function confirmThis (act,txt)
{
  if (!txt) {txt = act;}
  if ( confirm ('Are you sure you want to ' + txt + '?') == true )
  {
    document.mainform.action.value = act;
    document.mainform.submit();
  }
}

function closeWindow ()
{
  window.close();
}

function confirmCloseWindow ()
{
  if ( confirm ('Are you sure you want to close this window?') == true )
  {
    window.close();
  }
}

function confirmDisplay ()
{  
  document.mainform.action.value = 'display';
  document.mainform.submit();
}

function confirmImport ()
{
  if ( confirm ('Are you sure you want import this data?') == true )
  {
    document.mainform.action.value = 'import';
    document.mainform.submit();
  }
}

function confirmDelete (id)
{
  alert('Delete function is not available.');
  //if ( confirm ('Are you sure you want to delete this record?') == true )
  //{
  //  document.mainform.id.value = id;
  //  document.mainform.action.value = 'delete';
  //  document.mainform.submit();
  //}
}

function confirmUpdate (id)
{
  if ( confirm ('Are you sure you want to update this record?') == true )
  {
    document.mainform.id.value = id;
    document.mainform.action.value = 'update';
    document.mainform.submit();
  }
}

function confirmEmail (userid, addr)
{
  if ( confirm ('Send an e-mail for ' + userid + ' to ' + addr + '?') == true )
  {
    mailto:addr;
  }
}

function confirmEmailPassword (userid, addr)
{
  msg = '';
  if ( confirm ('E-mail password and/or message for ' + userid + ' to ' + addr + '?') == true )
  {
    var msg = prompt("Enter special message or leave blank","");
    if (msg == null) {msg = '';}
    window.location.href='adminretrievepwd.cfm?username=' + userid + '&msg=' + msg ;
  }
}

function confirmCancel (thisloc)
{
  if (!thisloc) { thisloc = 'SFMain.cfm'; }
  if ( confirm ('Are you sure you want to cancel? All information on this form will be lost.') == true )
  {
    window.location.href = thisloc;
  }
}

function unconfirmedUpdate (id)
{
    document.mainform.id.value = id;
    document.mainform.action.value = 'update';
    document.mainform.submit();
}

function refreshParent ()
{
   window.opener.location.href = window.opener.location.href;
}

function closeWindowOnBlur () // close window on blur
{
  alert('closing window on blur'); self.close();
} 

function resizeWindow(h,w) // for the media window
{
  if ( document.getElementById("mediaobject") )
  {
    //alert('Resizing window');
    window.resizeTo(document.getElementById("mediaobject").width+w,document.getElementById("mediaobject").height+h);
  }
  self.focus();
}

// end of utilities

