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 init_splash() {
  $('spl1').setStyles({'display':'block','opacity':0});
  $('spr1').setStyles({'display':'block','opacity':0});
  $('spl2').setStyles({'display':'block','opacity':0});
  $('spr2').setStyles({'display':'block','opacity':0});
  new Fx.Morph($('spl1'), {}).start({'opacity':[0,1]});
  new Fx.Morph($('spr1'), {}).start({'opacity':[0,1]});
  $('spl').addEvent("mouseover",function(){
    splashFunc('spl2',true);
  });
  $('spl').addEvent("mouseout",function(){
    splashFunc('spl2',false);
  });
  $('spr').addEvent("mouseover",function(){
    splashFunc('spr2',true);
  });
  $('spr').addEvent("mouseout",function(){
    splashFunc('spr2',false);
  });
}

splashEvents = new Array();
splashEvents['spl2'] = false;
splashEvents['spr2'] = false;

function splashFunc(elem,tgl) {
  trg = $(elem);
  if (splashEvents[elem] == false) {
    splashEvents[elem] = true;
    if (tgl) { // fade in
      new Fx.Morph(trg, {onComplete:function(){ splashEvents[elem] = false; }}).start({'opacity':[0,1]});
    }
    else { // fade out
      new Fx.Morph(trg, {onComplete:function(){ splashEvents[elem] = false; }}).start({'opacity':[1,0]});
    }
  }
}

function chkQform() {
  ff = eval("document.qform");
  if (ff.quote.value == "") {
    alert("Please enter a quote.");
	ff.quote.focus();
    return false;
  }
  else {
    ff.subtn.value = "Saving...";
    ff.subtn.disabled = true;
    return true;
  }
}

function delete_quote(id) {
  var kk = confirm("Are you sure you want to delete this quote?");
  if (kk) {
    window.location = "?action=delete&id="+id;
  }
}

function chkMetaForm() {
  ff = eval("document.metaform");
  if (ff.ipage_title.value == "") {
    alert("Please enter a title for the Browser Bar.");
    ff.ipage_title.focus();
    return false;
  }
  else if (ff.iprimary_title.value == "") {
    alert("Please enter a primary title for this page.");
    ff.iprimary_title.focus();
    return false;
  }
  else {
    ff.subtn.value = "Saving...";
    ff.subtn.disabled = true;
    return true;
  }
}

ipageOptions = false;
function togglePageOptions() {
  gc = $('pageOptionsFS').getCoordinates();
  elemHeight = gc['height'];
  if (!ipageOptions) {
    $('pageOptionsLink').set('html','(-) Hide Page Options');
    new Fx.Morph($('page_options'), {unit:'px'}).start({'height':[0,elemHeight]});
    ipageOptions = true;
  }
  else {
    $('pageOptionsLink').set('html','(+) Show Page Options');
    new Fx.Morph($('page_options'), {unit:'px'}).start({'height':[elemHeight,0]});
    ipageOptions = false;
  }
}

function chkContactForm() {
  ff = eval("document.ff");
  if (ff.ifirst_name.value == "") {
    alert("Please enter your first name.");
    ff.ifirst_name.focus();
    return false;
  }
  else if (ff.ilast_name.value == "") {
    alert("Please enter your last name.");
    ff.ilast_name.focus();
    return false;
  }
//  else if (ff.iphone.value == "") {
//    alert("Please enter your phone.");
//    ff.iphone.focus();
//    return false;
//  }
  else if (ff.iemail.value.indexOf('@') <= 0) {
    alert("Please enter a valid email address.");
    ff.iemail.focus();
    ff.iemail.select();
    return false;
  }
  else if (ff.imessage.value == "") {
    alert("Please enter a message.");
    ff.imessage.focus();
    return false;
  }
  else if (ff.recaptcha_response_field.value == "") {
    alert("Please enter the words you see.");
    ff.recaptcha_response_field.focus();
    return false;
  }
  else {
    ff.subtn.value = "Sending...please wait";
    ff.subtn.disabled = true;
  	return true;
  }
}

function viewContactForms(page_id) {
  MOOdalBox_I.open(
    "/cms/contactForms.php?page_id="+page_id+"&rn="+rn(),
    "VIEW CONTACT FORMS",
    "750 450"
  );
}

function delete_message(id,page_id) {
  var kk = confirm("Are you sure you want to delete this message from the archives? This action will be permanent.");
  if (kk) {
    window.location = "?action=delete&id="+id+"&page_id="+page_id;
  }
}
