var debug = false;
var filenameAllowedChars = 'abcdefghijklmnopqrstuvwxyz0123456789_\.\-/';
var fnbc = new Array(' ','~','`','!','@','#','\$','%','\^','&','*','(',')',
  '\"','\'',':',';','?','\\','\\','>','<', ',');
var fnrc = new Array('-','','','','','','','','','','','','',
  '','','','','','','','','','');
var actionAllowedChars = 'abcdefghijklmnopqrstuvwxyz0123456789';
var abc = new Array(' ','~','`','!','@','#','\$','%','\^','&','*','(',')',
  '\"','\'',':',';','?','\/','\\','>','<', ',', '\_', '-', '\.');
var arc = new Array('','','','','','','','','','','','','',
  '','','','','','','','','','','','','');



function goHome()
{
  window.location = '/home'
}


function startSplash()
{
  showJam();
}
function showJam()
{
  new Effect.Appear('splash-text-jam', { from: 0.0, to: 1.0, duration: 5.0, afterFinish: showComing });
}
function showComing()
{
  new Effect.Appear('splash-text-coming', { from: 0.0, to: 1.0, duration: 5.0 });
}

function splashImg()
{
  new Effect.Appear('splash-img', { from: 0.0, to: 1.0, duration: 13.0, afterFinish: showJam } );
}


/*
function flashComingSoon()
{
  new Effect.Pulsate('coming-soon', { pulses: 30, duration: 15.0 });
}
function showComingSoon()
{
  $('coming-soon').style.color = '#6f0d0d';
  new Effect.Pulsate('coming-soon', { pulses: 15, duration: 15.0 });
}
function splash()
{
  new Effect.Opacity('splash-bg', { from: 0.0, to: 1.0, duration: 3.0 });
}
function splashLines()
{
  new Effect.Opacity('splash-lines', { from: 0.0, to: 1.0, duration: 9.0, afterFinish: splashText } );
}
function splashText()
{
  new Effect.Appear('splash-text', { from: 0.0, to: 1.0, duration: 9.0 });
}
function splashLinesPulse()
{
  new Effect.Pulsate('splash-lines', { pulses: 3, duration: 1.5 });
}
*/


function startSound(snd) { soundManager.start(snd); }
function stopSound(snd) { soundManager.stop(snd); }


function fade(id, direction, duration, after)
{
  // params/defaults
  if (direction == null || direction == "")
  { direction = 'in'; }
  if (direction == 'in')
  { from = 0.0, to = 1.0; }
  else
  { from = 1.0, to = 0.0; }
  if (duration == null || duration == "")
  { duration = 3.0; }
  
  if (after == null || after == "")
  { obj = new Effect.Opacity(id, { from: from, to: to, duration: duration} ); }
  else
  { obj = new Effect.Opacity(id, { from: from, to: to, duration: duration, afterFinish: after} ); }
  return(obj);
}





function showStatus(msg)
{
  window.status = msg;
}


function checkForSave(e)
{

  // damn IE hack
  if (window.event) 
  { 
    e = window.event; 
    keyCode = e.keyCode;
  }
  else
  { keyCode = e.which; }
  
  // if not an 'S' or 's', return
  if (keyCode != 115 && keyCode != 83)
  { return; }
     
  // check for control key
  if (e.ctrlKey) 
  { 
    // see if there is a form
    if (document.forms[0]) 
    { 
      if (document.forms[0].name != 'searchbox')
      {
        document.forms[0].submit();
        e.preventDefault(); stopPropogation();
      }
    }    
  }
  return;
} // end checkForSave()


function confirmDelete(form, action, message)
{
  var msg = "Are you sure you want to permanently delete " + message + "?";
  if (confirm(msg))
  {
    form.action = action;
    return true; 
  } 
  return false;
}


function validateActionInput(field)
{
  var val = field.value;
  if (! validateAction(val))
  {
    alert('Valid actions contain only lowercase characters and digits');
    for (var i = 0; i < abc.length; i++)
    { 
      res = val.indexOf(abc[i]);
      while ( res != -1 )
      { 
        val = val.replace(abc[i], arc[i]); 
        res = val.indexOf(abc[i]);
      }
    }
    field.value = val;
    field.focus();
    return false;
  }
  return true;
}

function validateAction(val)
{
  
  // do a regex ^ match for allowed chars (case-insensitive)
  var regex = new RegExp('[^' + actionAllowedChars + ']', 'i');
  var res = regex.test(val);
  return(!res); 
} 


function validateFileNameInput(field)
{
  var val = field.value;
  if (! validateFileName(val))
  {
    alert('Valid filenames contain only characters, digits, dashes, ' +
      'underscores and periods.  You can use forward slash to indicate ' +
      'directory structure within the defined media directory.');
    for (var i = 0; i < fnbc.length; i++)
    { 
      res = val.indexOf(fnbc[i]);
      while ( res != -1 )
      { 
        val = val.replace(fnbc[i], fnrc[i]); 
        res = val.indexOf(fnbc[i]);
      }
    }
    field.value = val;
    field.focus();
    return false;
  }
  return true;
}

function validateFileName(val)
{
  
  // do a regex ^ match for allowed chars (case-insensitive)
  var regex = new RegExp('[^' + filenameAllowedChars + ']', 'i');
  var res = regex.test(val);
  return(!res); 
} 



function validateMediaForm(form)
{
  var ok = true;
  if (debug)
  { return true; }

  
  // check for required fields
  var rf = new Array();
  rf[0] = form.name;
  rf[1] = form.url;
  rf[2] = form.mediaFile;
  
  var rm = new Array();
  rm[0] = 'Display Name';
  rm[1] = 'File Name';
  rm[2] = 'File to Upload';
  
  var len = rf.length;
  for (var i = 0; i < len; i++)
  {
    field = rf[i];
    if (field.value == '')
    {
      alert ('Please enter your ' + rm[i]);
      rf[i].focus();
      return false;
    }
  }
    
  return ok;
}


function validateDigits(field, name, numDigits, lessOK)
{
  if (debug)
  { return true; }

  if (field.value == '') { return true; }
  
  if (! lessOK) { lessOK = false; } else { lessOK = true; }
  var val = field.value;
  var len = val.length;
  
  if (! isInt(val)) 
  {
    alert('The ' + name + ' must be numeric'); 
    field.focus();
    //field.select();
    return false;
  }
  
  if (len > numDigits)
  {
    alert('The ' + name + ' cannot be more than ' + numDigits + ' numeric digits'); 
    field.focus();
    //field.select();
    return false;
  }

  if (! lessOK && len != numDigits)
  {
    alert('The ' + name + ' must be exactly ' + numDigits + ' numeric digits'); 
    field.focus();
    //field.select();
    return false;
  }

  return true;
}



function isInt(value)
{
/*
  if (parseInt(value) != value)
  { return false; }
  else
  { return true; }
*/
  var validChars = "0123456789";
  var ok = true;
  var c;

 
  for (i = 0; i < value.length && ok == true; i++) 
  { 
    c = value.charAt(i); 
    if (validChars.indexOf(c) == -1) 
    { ok = false; }
  }
  return ok; 
}



function changeClass(newclass, item)
{
  item.className = newclass;  
}

function bodyLoad()
{
  selectDefault(); 
  return;
} // end bodyLoad()


function selectDefault()
{
      if (document.forms.length == 0)
      { return; }
      el = document.forms[0].elements.length;
      for (var i = 0; i < el; i++)
      {
        if (document.forms[0].elements[i].type != "hidden")
        {
          document.forms[0].elements[i].focus();
          if (document.forms[0].elements[i].type == "text")
          { document.forms[0].elements[i].select(); }
          break;
        }
      }

  return;
}


var popwin;
function popupWin(url, name, h, w, scroll, resize, utils) 
{
  var msg = '\nUSAGE: popupWin(<url>, <name>, <height>, <width>[, <scroll>, <resize>, <utils>])';
  
  // check for required params
  if (url == null || url == "")
  {
    alert("You must pass in a URL" + msg);
    return;
  }
  if (name == null || name == "")
  {
    alert("You must pass in a window name" + msg);
    return;
  }
  if (h == null || h == "")
  {
    alert("You must pass in a window height" + msg);
    return;
  }
  if (w == null || w == "")
  {
    alert("You must pass in a window width" + msg);
    return;
  }
 
  // set screen size/position
  var winl = (screen.width - w) / 2;
  var wint = (screen.height - h) / 2;
  var opts = 'height='+h+',width='+w+',top='+wint+',left='+winl;
  
  // if utils passed in, set a bunch of options
  if (utils != null && (utils == "yes" || utils == "true" || utils == "on" || utils == "1"))
  {
    opts += ",location=yes,menubar=yes,statusbar=yes";
  }
  else
  {
    opts += ",location=no,menubar=no,statusbar=no";
  }
  
  // scroll and resize are optional
  if (scroll != null && (scroll == "yes" || scroll == "true" || scroll == "on" || scroll == "1"))
  {
    opts += ",scrollbars=yes";
  }
  else
  {
    opts += ",scrollbars=no";
  }
  if (resize != null && (resize == "yes" || resize == "true" || resize == "on" || resize == "1"))
  {
    opts += ",resizable=yes";
  }
  else
  {
    opts += ",resizable=no";
  }
 
  popwin = window.open(url,name,opts);
  if (parseInt(navigator.appVersion) >= 4)
  { popwin.focus(); }
}


