function URLEncode(str) {
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";
	var plaintext = str;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";	// x-www-urlencoded, rather than 
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert("Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted.");
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	}
	return encoded;
}

function rn() {
  return Math.floor(Math.random()*999999999);
}

function toggleChk(elem) {
  elem = $(elem);
  if (elem) {
    if (elem.checked) { elem.checked = false; }
    else { elem.checked = true; }
  }
}

function toggleElem(elem) {
  elem = $(elem);
  if (elem) {
    if (elem.style.display == "none") {
      elem.setStyle('display','block');
    }
    else {
      elem.setStyle('display','none');
    }
  }
}

function init_event_calendar(icmd,mm,yy) {
  cx = "";
  if (mm) {
    if (!icmd) { icmd = ""; }
    if (!yy) { yy = ""; }
    cx = "&command="+icmd+"&month="+mm+"&year="+yy;
  }
  new Request({
	url: '/ajax/calendar.php?rn='+rn()+cx,
	onSuccess: 	function(txt) {
	  if (txt.indexOf('ERROR') >= 0) {
	    $('mycal').set('html','Calendar unavailable at this time.');
	  }
	  else {
	    $('mycal').set('html',txt);
	  }
	}
  }).send();
}
