// --------------------------------------------------------------------- --->
// name:    utilities.js                                                 --->
// author:  todd@hampson.us                                              --->
// date:    2008-12-01                                                   --->
// purpose: CrystalLoft                                                  --->
// mods:    2008-12-01 - create for CrystalLoft                          --->
// --------------------------------------------------------------------- --->

// global vars

var min=9;
var med=12;
var max=16;

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 addToCart (id, txt)
{
  if (confirm(' Add item ' + txt + ' (' + id + ') to cart?') == true)
  {
    document.mainform.itemid.value = id;
    document.mainform.action.value = "search";
    document.mainform.submit();
  }
}

function confirmClearCart ()
{
  if (confirm('Are you SURE you want to clear your cart?\nThis cannot be undone!') == true)
  {
    document.mainform.cartaction.value = "clear";
    document.mainform.submit();
  }
}

function setFilename (fn)
{
  document.mainform.filename.value = fn;
  null;
}

function popupWindow (url,name,opt) // correct order
{
  // alert ('in popupWindow');
  if (opt == '' || opt == 'default')
  {
    opt = 'height=500,width=700,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 increaseFontSize()
{
  var p = document.getElementsByTagName('*');
  for(i=0; i < p.length; i++ )
  {
    if (p[i].style.fontSize)
    {
      var s = parseInt(p[i].style.fontSize.replace("px",""));
    }
    else
    {
      var s = med;
    }
    if (s < max ) { s += 1; } p[i].style.fontSize = s+"px";
  }
}

function decreaseFontSize()
{
  var p = document.getElementsByTagName('*');
  for (i=0; i<p.length; i++)
  {
    if (p[i].style.fontSize)
    {
      var s = parseInt(p[i].style.fontSize.replace("px",""));
    }
    else
    {
      var s = med;
    }
    if( s!=min && s < max) {s -= 1;}p[i].style.fontSize = s+"px";
  }   
}

function setFontSize (size)
{
  location.href='setcss.cfm?size='+size+'&mode=set';
}

function increaseFontSizeNew (size)
{
  if (size == 16)
  {
    alert('Sorry, you cannot increase the font size beyond this point.');
  }
  else
  {
    location.href='setcss.cfm?size='+size+'&mode=increase';
  }
}

function decreaseFontSizeNew (size)
{
  if (size == 10)
  {
    alert('Sorry, you cannot decrease the font size any further.');
  }
  else
  {
    location.href='setcss.cfm?size='+size+'&mode=decrease';
  }
}


function closeWindowOnEnter () // close window on enter key
{
  if (event.keyCode == 13) { self.close(); }
} 

function resizeWindow(w, h) // for single image
{
  if ( document.images[0] )
  {
    window.resizeTo(document.images[0].width+w,document.images[0].height+h);
  }
  self.focus();
}

// end of utilities
