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('','','','','','','','','','','','','',
  '','','','','','','','','','','','','');



/**********************
  Begin DropDownMenu stuff
  ****************************/
var timeout     = 500;
var closetimer	= 0;
var ddmenuitem  = 0;
var ddmenulink  = 0;

// show menu items
function mopen(id)
{	
  // determine the menu link id
  var aid = '';
  switch (id)
  {
    case 'm1':  aid = 'a1'; break;
    case 'm2':  aid = 'a2'; break;
    case 'm3':  aid = 'a3'; break;
    case 'm4':  aid = 'a4'; break;
    case 'm5':  aid = 'a5'; break;
    case 'm6':  aid = 'a6'; break;
    case 'm7':  aid = 'a7'; break;
  } // end switch on id
  
	// cancel close timer
	mcancelclosetime();

	// close old layer
	if (ddmenuitem) ddmenuitem.style.visibility = 'hidden';
	// reset menu text color
	if (ddmenulink) ddmenulink.style.color = '#2e6a31';  // HP_GREEN

	// get new layer and show it
	ddmenuitem = document.getElementById(id);
	ddmenuitem.style.visibility = 'visible';
	// set text color of menu
	ddmenulink = document.getElementById(aid);
	ddmenulink.style.color = '#ffffff';  
}

// hide menu items
function mclose()
{
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
	
	// reset menu text color
	if(ddmenulink) ddmenulink.style.color = '#2e6a31'; // HP_GREEN
}

// go close timer
function mclosetime()
{
	closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime()
{
	if(closetimer)
	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

// close layer when click-out
document.onclick = mclose; 

/*****************************
  End DropDownMenu stuff
  ******************************/
  
  
  

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;
  
  var rm = new Array();
  rm[0] = 'Name/Title';
  rm[1] = 'URL';
  
  var len = rf.length;
  for (var i = 0; i < len; i++)
  {
    field = rf[i];
    if (field.value == '')
    {
      alert ('Please enter the ' + rm[i]);
      rf[i].focus();
      return false;
    }
  }
    
  return ok;
}


function doKeywords(list, field)
{
  buf = '';
  len = list.length;
  for (var i = 0; i < len; i++)
  {
    if (buf.length > 0) { buf += ','; }
    buf += list.options[i].value;
  }
  field.value = buf;
  return true;
}


function addKeywords(fromname, toname)
{
  fromlist = eval('document.forms[0].' + fromname);
  tolist = eval('document.forms[0].' + toname);
  
  if (fromlist.selectedIndex == -1) // nada
  { 
    alert('Please select a keyword to add it'); 
    return true; 
  }
  
  fromlen = fromlist.length;
  
  for (var i = 0; i < fromlen; i++) 
  {
    if (fromlist.options[i].selected)
    {
      f = fromlist.options[i];
      f.selected = false;
      if (checkDupeOption(f, tolist))
      {
        o = new Option(f.text, f.value, false, false) ;
        tolist.add(o, null);
      }
    }
  }
  
  //tolist.selectedIndex = -1;
  //fromlist.selectedIndex = -1;
  
  return true;
}

function checkDupeOption(opt, list)
{
  len = list.length;
  
  for (var i = 0; i < len; i++) 
  {
    topt = list.options[i];
    if (topt.text == opt.text)
    { return false; }
  }
  return true;
}



function removeKeywords(fromname)
{
  fromlist = eval('document.forms[0].' + fromname);
  
  if (fromlist.selectedIndex == -1) // nada
  { 
    alert('Please select a keyword to remove it'); 
    return true; 
  }
  
  fromlen = fromlist.length;
  
  for (var i = 0; i < fromlen; i++) 
  {
    if (fromlist.options[i].selected)
    {
      fromlist.options[i].selected = false;
      fromlist.remove(i);
    }
  }
  fromlist.selectedIndex = -1;  
  return true;
}


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(action)
{
  selectDefault(action); 
  return;
} // end bodyLoad()


function selectDefault(action)
{
  switch (action)
  {
    case 'loginform':
    case 'login':
      document.login.username.focus();
      document.login.username.select();
      break;
      
    default:
      if (document.forms.length == 0)
      { break; }
      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;
        }
      }
      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(); }
}


function validateDateField(field, name, required)
{
  var yearMin = 1900, yearMax = new Date().getFullYear() + 5;
  var monthMin = 1, monthMax = 12;
  var dayMin = 1, dayMax = 31;

  if (debug) { return true; }
  if ( ! required ) { required = false; } else { required = true; }
  if (field.value == '')
  {
    if (! required)
    { return true; }
    else
    { 
      barf('The ' + name + ' must be provided, as a date in the format: yyyy-mm-dd', field); 
      return false;
    }
  } // no value provided

  // ok, a value was provided, let's check it out
  val = field.value;
  len = val.length;
  if (len != 10)
  {
      barf('The ' + name + ' must be provided, as a date in the format: yyyy-mm-dd', field);
      return false;
  }
  parts = val.split('-');
  if (parts.length != 3)
  {
      barf('The ' + name + ' must be provided, as a date in the format: yyyy-mm-dd', field);
      return false;
  }
  // 08 is a problem for some reason, so hack it
  if (parts[1] == '08') { parts[1] = '8'; }
  if (parts[2] == '08') { parts[2] = '8'; }

  year = parseInt(parts[0]);
  month = parseInt(parts[1]);
  day = parseInt(parts[2]); 
  if (isNaN(year) || isNaN(month) || isNaN(day))
  {
      barf('The ' + name + ' must be provided, as a date in the format: yyyy-mm-dd', field);
      return false;
  }
  if (year <= yearMin || year >= yearMax)
  {
      barf('The ' + name + ' year must be between ' + yearMin + ' and ' + yearMax, field);
      return false;
  }
  if (month <= monthMin || month >= monthMax)
  {
      barf('The ' + name + ' month must be between ' + monthMin + ' and ' + monthMax, field);
      return false;
  }
  if (day <= dayMin || day >= dayMax)
  {
      barf('The ' + name + ' day must be between ' + dayMin + ' and ' + dayMax, field);
      return false;
  }
 
  return true;

} // end validateDateField()

function barf(msg, field)
{
  alert(msg); 
  field.focus();
  field.select();
  return;  
} // end barf()









