function toggleDisplay (item, forceDisplay) {
var f = document.getElementById (item);
  if (f.style.display == 'block' && (!forceDisplay)) {
    f.style.display = 'none';
  } else {
    f.style.display = 'block';

    // enable focus in first non-hidden input field
    var fs = f.getElementsByTagName('input');
    for (i=0; i<fs.length; i++) {
      if (fs[i].type != 'hidden') {
        fs[i].focus();
	return;
      }
    }
  }
}

function toggleView (item) {
var f = document.getElementById (item);
  if (f.style.display == 'none') {
    f.style.display = '';
  } else {
    f.style.display = 'none';
  }
}

function toggleColors (item, colorA, colorB) {
var e = document.getElementById (item);
var s = e.style;
  if (s.color == colorB) { // sometimes easy but rgb encoding complex
    s.color = colorA;
    e.recordedColor = colorA;
  } else if (e.recordedColor && e.recordedColor == colorB) {
  // 'tis overkill to use dojo for this
  // } else if ( dojo &&
  //             dojo.gfx.color.extractRGB(s.color).toString()
  //             == dojo.gfx.color.extractRGB(colorB).toString()) {
    s.color = colorA;
    e.recordedColor = colorA;
  } else {
    s.color = colorB;
    e.recordedColor = colorB;
  }
}

function toggleWipe (item) {
if (!dojo) {
  toggleView (item);
  return;
}
var f = document.getElementById (item);
var d = 300;
  if (f.style.display == 'none') {
    dojo.lfx.html.wipeIn (item, d).play();
    // dojo.html.setOpacity (dojo.byId (item), 0.5);
  } else {
    dojo.lfx.html.wipeOut (item, d).play();
  }
}


function windowWidth () {
  if( typeof( window.innerWidth ) == 'number' ) {
  // Non-IE
    return window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
  // IE 6+ in 'standards compliant mode'
    return document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
  // IE 4 compatible
    return document.body.clientWidth;
  }
}

function moveForm (e, item) {
var f = document.getElementById (item);
  if (f.style.display != 'block') {
  // get mouse position
    var posx = 0;
    var posy = 0;
    if (!e) var e = window.event;
    if (e.pageX || e.pageY) {
      posx = e.pageX;
      posy = e.pageY;
    } else if (e.clientX || e.clientY) {
      posx = e.clientX + document.body.scrollLeft
             + document.documentElement.scrollLeft;
      posy = e.clientY + document.body.scrollTop
             + document.documentElement.scrollTop;
    }
    // reasonably align form's left border horizontally with click
    // f.style.left = (posx - 16) + 'px';
    f.style.right = Math.max(windowWidth() - posx - 48, 0) + 'px';
    f.style.top = (posy + 16) + 'px';
  }
}

function moveEmailForm (e) {
var f = document.getElementById ('emailForm');
  if (f.style.display != 'block') {
  // get mouse position
    var posx = 0;
    var posy = 0;
    if (!e) var e = window.event;
    if (e.pageX || e.pageY) {
      posx = e.pageX;
      posy = e.pageY;
    } else if (e.clientX || e.clientY) {
      posx = e.clientX + document.body.scrollLeft
             + document.documentElement.scrollLeft;
      posy = e.clientY + document.body.scrollTop
             + document.documentElement.scrollTop;
    }
    // reasonably align form's right border horizontally with click
    f.style.right = (windowWidth() - posx - 48) + 'px';
  }
}

function fieldNotEmpty (dataInput, dataId, dataSpec) {
  if (dataInput.length > 0) {
    return true;
  } else {
    alert ('Missing ' + dataId + '.' 
           + ((dataSpec!='')?(' Must be ' + dataSpec + '.'):''));
    return false;
  }
}

function fieldMatches (dataInput, validRE, dataId, dataSpec) {
 if (dataInput.match (validRE)) {
   return true;
 } else {
   alert (dataId + ' is invalid.'
          + ((dataSpec!='')?(' Must be ' + dataSpec + '.'):''));
   return false;
 }
}

function fieldSelected (dataInput, dataSpec) {
  for (var i=0; i< dataInput.length; i++) {
    if ((dataInput[i].type == 'radio') && dataInput[i].checked) {
      // alert (dataInput[i].value + ' checked');
      return true;
    }
  }
  alert ('Please select at least one ' + dataSpec + '.');
  return false;
}


function validateVotingForm (theForm) {
  return (
    fieldSelected (theForm.getElementsByTagName('input'), 'candidate')
    && false
  );
}

function validateEmailForm (theForm) {
  return (
    fieldNotEmpty (theForm.to.value,
                   'destination address', 'provided')
  &&
    fieldNotEmpty (theForm.from.value,
                   'your address', 'provided')
  &&
    fieldMatches (theForm.to.value, /^.+@.+\..+$/,
                  'The address of your friend', 'an email address')
  &&
    fieldMatches (theForm.from.value, /^.+@.+\..+$/,
                  'Your address', 'an email address')
  );
}

function validateTipsForm (theForm) {
  theForm.hej.value = 'ave';
  return (
    fieldNotEmpty (theForm.comments.value.replace(/^\s*|\s*$/g,""),
                   'tips message', 'something')
  );
}

function saluteInteractiveForm (x) {
  document.write('<');
  document.write('in'+'put type="hi'+'dden" na'+'me="hej" v'+'alue="'+x+'" /'+'>');
}

