/* Menu Functions */

function mOver(obj) {                                        // change menu color on mouseover
  return true // disable
//  if (!obj.prevClass) {                                    // save current className

      obj.prevClass = obj.className;
//  }
  obj.className = "mOver";                                   // change to menuOver class

}

function mReset(obj) {                                       // reset menu color on mouseoff
  return true // disable
  if (obj.prevClass != null) { obj.className = obj.prevClass; } // restore prev className
}

/*
function mClick(obj) {   // mockup code for demo

  // remove "mOn" class from all menu options
  var mCells = document.all.tags("TD");
	for (i=0; i<mCells.length; i++) {
    if (mCells[i].className == "mOn") { mCells[i].className = "mOff"; }
  }

  // set ON color for clicked menu option
  obj.className = "mOn";
  obj.prevClass = "mOn";

}
*/


// <table border="0" width=38 height=38 cellspacing=0 cellpadding=0><tr><td align="center" valign="middle">
// <img src="http://www.w3.org/Icons/WWW/w3c_home" style="position: absolute; visibility: hidden" onload="constrainImageOnLoad(this, 38, 38)">
// </td></tr></table>

function constrainImageOnLoad(img, maxX, maxY) {
  window.status = window.status + " * ";
  if (maxX < 1) { maxX = 1; }
  if (maxY < 1) { maxY = 1; }

  // get height and width of image
  var x = img.width;
  var y = img.height;
  
  // couldn't get height and width?  try getAttribute() (works in IE)
//  if (x < 1) { x = img.getAttribute('width'); }
//  if (y < 1) { y = img.getAttribute('height'); }

  // still couldn't?  try waiting a moment
  window.status = window.status + x + ", ";
  if (x < 1) { alert('timeout'); setTimeout(function() { constrainImageOnLoad(img, maxX, maxY); }, 1000); return; }

  var scaleToFitX = maxX / x;
  var scaleToFitY = maxY / y;

  // choose smaller dimension
  var scale = (scaleToFitX < scaleToFitY) ? scaleToFitX : scaleToFitY;

  img.width  = parseInt((x * scale) + 0.5);        // round to nearest int
  img.height = parseInt((y * scale) + 0.5);        // round to nearest int

  img.style.visibility = 'visible';
  img.style.position = 'static';
}


function checkAll(chkval, field_prefix) {
  if (field_prefix == null) { field_prefix = ''; }
  if (typeof(field_prefix) == 'object') { field_prefix = field_prefix.name; }
  var field_prefix_regexp = new RegExp('^' + field_prefix);                     // \Q...\E would be nice!
  var myform = document.forms[0];
  var ecount  = myform.length;  // element count
  for (i=0; i < ecount; i++) {
    if (myform.elements[i].type == 'checkbox' && myform.elements[i].name.match(field_prefix_regexp)) {
      myform.elements[i].checked = chkval;
    }
  }
}

function dateChange(field) {
  var fieldPrefix = field.name.match(/^(.*)_(?:mon|day|year|hour|min|sec|ampm)$/)[1];
  if (fieldPrefix.length < 1) { alert("invalid field name passed to dateChange().  field.name must end in _mon, _day, _year, _hour, _min, _sec, or _ampm."); return false; }
  var fieldPrefixRegExp = new RegExp('^' + fieldPrefix);                      // \Q...\E would be nice!

  // if the user blanks out any field in a date, blank out the others
  if (field.selectedIndex == 0) {
    var myForm = field.form;
    var ecount = myForm.length;  // element count
    for (var i = 0; i < ecount; i++) {
      if (myForm.elements[i].type == 'select-one' && myForm.elements[i].name.match(fieldPrefixRegExp)) {
        myForm.elements[i].selectedIndex = 0;
      }
    }
  }
}

function popup(url, width, height, name) {
//  // figure our optimal window placement for popup dialog
//  var posX    = event.screenX;
//  var posY    = event.screenY + 20;
//  var screenW = screen.width;                                 // screen size
//  var screenH = screen.height - 20;                           // take taskbar into account
//  if (posX + width > screenW) { posX = posX - width - 40; }   // if mouse too far right
//  if (posY + height > screenH) { posY = posY - height - 80; } // if mouse too far down

  if (name == null) { name = 'popup'; }

  return window.open(url,name,'resizable=yes,location=no,scrollbars=yes,toolbar=no,menubar=no,directories=no,status=yes'
                              +',height='+height+',width='+width
//                            +',top='+posY+',left='+posX+',screenY='+posY+',screenX='+posX   // top,left for IE; screenX,screenY for NS
                           );
}

function browse(type, field_ref, extra_query_string) {
  // find the form which the field belongs to
  for (var form_number = 0; document.forms[form_number] != field_ref.form; form_number++) {
    if (form_number > document.forms.length) { alert('Cannot find element in document.forms[]'); return false; }
  }
  var field_name = 'forms[' + form_number +  '].' + field_ref.name;

  // open a popup window
  var sdir = escape(field_ref.value);
  popup('?action=browse_' + type + '&field_name=' + field_name + '&sdir=' + sdir + extra_query_string, 500, 350);
  return true;
}

function upload_popup(cgiurl) {
  var win = popup(null, 300, 350);
  win.document.open();
  win.document.writeln('<html><head><title>Upload Files</title></head>');
  win.document.writeln('<body>');
  win.document.writeln('<form action="' + cgiurl + '" method="post" enctype="multipart/form-data">');
  win.document.writeln('<input type="hidden" name="action" value="upload_files">');
  win.document.writeln('<table cellspacing=0 cellpadding=2>');
  win.document.writeln('<tr><td><font style="font-family: Arial, Helvetica, sans-serif; font-size: 13px;"><b>Upload Files</b></font></td></tr>');
  for (var num = 1; num <= 10; num++) {
    win.document.writeln('<tr><td><input type="file" name="upload_' + num + '"></td></tr>');
  }
  win.document.writeln('<tr><td align="right"><input type="submit" name="ok" value="  OK  "><input type="button" name="cancel" value="Cancel" onClick="window.close()"></td></tr>');
  win.document.writeln('</table>');
  win.document.writeln('</form>');
  win.document.writeln('</body></html>');
  win.document.close();
}


/* ------------------------------------------------------------------------- *\
  Function    : 
  Description : 
  Usage       : 
\* ------------------------------------------------------------------------- */

function forceValidDate(yearName,monName,dayName) {
  var dayObj = document.getElementsByName(dayName)[0];
  var year   = document.getElementsByName(yearName)[0].value;
  var mon    = document.getElementsByName(monName)[0].value;
  var day    = dayObj.value;

  // get days in month
  var monDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) { monDays[1] = "29"; } // check for leap year
  var daysInMonth = monDays[ mon-1 ];

  // remember selected Index
  var myIndex = dayObj.selectedIndex;

  // remove days
  while (dayObj.options[dayObj.length-1].value > daysInMonth) {
    dayObj.options[dayObj.length-1] = null;
  }
  
  // add days
  while (dayObj.options[dayObj.length-1].value < daysInMonth) {
    var nextDay = Number(dayObj.options[dayObj.length-1].value) + 1;
    dayObj.options[dayObj.length] = new Option(nextDay, nextDay);
  }

  // if no day selected, select highest day in month
  if (dayObj.options[myIndex] == null) { dayObj.options[dayObj.length-1].selected = true; }

}



function submitFormTargetBlank(thisButton, action) {

  var sourceForm = thisButton.form;

  var newWindow = popup();
  newWindow.document.write('<form action="' + thisButton.form.action + '" method="post">');   // multipart would be useless since we aren't uploading files anyways

  // Construct a bunch of hidden fields
  for (var i=0; i <= sourceForm.elements.length-1; i++) {
    var eobj = sourceForm.elements[i];                     // element object
    var type  = eobj.type
    var fname  = _htmlEncode(eobj.name);                // field name
    var fvalue = '';                                    // field value (defined below)

    // error checking
    if (fname.length == 0) { continue; }                 // skip blank names

    // for single pulldowns
    if (type == 'select-one') {
      var idx = eobj.options.selectedIndex;
      var txt = eobj.options[idx].text;                 // <option>text</option> value
      var val = eobj.options[idx].value;                // <option value="this">
      if (val.length) { fvalue = val; }                  // try and use value first
      else            { fvalue = txt; }
      fvalue = _htmlEncode(fvalue);
      newWindow.document.write('<input type="hidden" name="' + fname + '" value="' + fvalue + '">');
    }

    // for multiple pulldowns
    else if (type == 'select-multiple') {
      var sCount = 0;                                           // selected options count
      for (var idx=0; idx <= eobj.options.length-1; idx++) {    // o is option index
        if (eobj.options[idx].selected != true) { continue; }   // skip unselected values
        sCount += 1;                                            // add one to selected options counter        

        var txt = eobj.options[idx].text;                       // <option>text</option> value
        var val = eobj.options[idx].value;                      // <option value="this">
        if (val.length) { fvalue = val; }                        // try and use value first
        else            { fvalue = txt; }
        fvalue = _htmlEncode(fvalue);
        newWindow.document.write('<input type="hidden" name="' + fname + '" value="' + fvalue + '">');
      }
      if (sCount == 0) { 
        fvalue = _htmlEncode(fvalue);
        newWindow.document.write('<input type="hidden" name="' + fname + '" value="' + fvalue + '">');
      }
    }    

    // for checkboxes and radios
    else if (type == 'checkbox' || type == 'radio') {
      if (eobj.checked) {                                       // only show "checked" checkboxes
        fvalue = _htmlEncode(eobj.value);                       // field value
        newWindow.document.write('<input type="hidden" name="' + fname + '" value="' + fvalue + '">');
      }
    }

    // for submit buttons
    else if (type == 'submit' || type == 'button') {
      ; // do nothing
    }

    // for all other fields
    else {
      fvalue = _htmlEncode(eobj.value);                         // field value
      newWindow.document.write('<input type="hidden" name="' + fname + '" value="' + fvalue + '">');
    }
    // end

  }

  // and one final field for the caller's action
  newWindow.document.write('<input type="hidden" name="action" value="' + _htmlEncode(action) + '">');
  newWindow.document.write('</form></div>');
  var newForm = newWindow.document.forms[0];
  newForm.submit();
}


/* ------------------------------------------------------------------------- *\
  Function    : appendHTML
  Description : append HTML to end of element
  Usage       : appendHTML(element, newHTML);
\* ------------------------------------------------------------------------- */

function appendHTML(obj,html) {

  if (typeof obj == 'string') { obj = document.getElementById(obj); }
  if (obj == null) { return alert('setInnerHTML: Invalid object name or reference'); };

  if (useProprietaryFeatures) {
    if (obj.innerHTML != null) { obj.innerHTML += html; return; }
  }

  obj.appendChild( _createDocumentFragmentFromHTML(html) );
}

/* ------------------------------------------------------------------------- *\
  Function    : 
  Description : 
  Usage       : 
\* ------------------------------------------------------------------------- */



/* ------------------------------------------------------------------------- */



