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 chkimanage() {
  ff = eval("document.mms");
  if (ff.tagline.value == "") {
    alert("Please enter a tagline for your website.");
    ff.tagline.focus();
    return false;
  }
  else if ((ff.template_type.value == "0") && (ff.fname.value == "")) {
    alert("Please enter a first name for your contact information.");
    ff.fname.focus();
    return false;
  }
  else if ((ff.template_type.value == "0") && (ff.lname.value == "")) {
    alert("Please enter a last name for your contact information.");
    ff.lname.focus();
    return false;
  }
  else if ((ff.template_type.value == "1") && (ff.company.value == "")) {
    alert("Please enter a company name for your contact information.");
    ff.company.focus();
    return false;
  }
  else if ((ff.title_option.value == "1") && (ff.logo_file.value == "") && (ff.current_logo.value == "")) {
    alert("Please select a logo to upload.");
    ff.logo_file.focus();
    return false;
  }
  else if (ff.site_title.value == "") {
    alert("Please enter title for your website.");
    ff.site_title.focus();
    return false;
  }
  else if (ff.home_title.value == "") {
    alert("Please enter a title for your Home Page.");
    ff.home_title.focus();
    return false;
  }
  else if (ff.about_text.value == "") {
    alert("Please enter some content for your About Page.");
    ff.about_text.focus();
    return false;
  }
  else if (ff.about_title.value == "") {
    alert("Please enter a title for your About Page.");
    ff.about_title.focus();
    return false;
  }
  else if (ff.address.value == "") {
    alert("Please enter an address for your contact information.");
    ff.address.focus();
    return false;
  }
  else if (ff.city.value == "") {
    alert("Please enter a city for your contact information.");
    ff.city.focus();
    return false;
  }
//  else if (ff.state.value == "") {
//    alert("Please select a state for your contact information.");
//    ff.state.focus();
//    return false;
//  }
//  else if (ff.zip.value == "") {
//    alert("Please enter a zip for your contact information.");
//    ff.zip.focus();
//    return false;
//  }
  else if (ff.phone.value == "") {
    alert("Please enter a primary phone number for your contact information.");
    ff.phone.focus();
    return false;
  }
  else if (ff.email.value.indexOf('@') <= 0) {
    alert("Please enter a valid email address for your contact information. This is hidden from visitors.");
    ff.email.focus();
    ff.email.select();
    return false;
  }
  else if ((ff.new_pass.value != "") && (ff.old_pass.value == "")) {
    alert("Please enter your old password.");
    ff.old_pass.focus();
    return false;
  }
  else if ((ff.new_pass.value != "") && (ff.new_pass_repeat.value == "")) {
    alert("Please repeat your new password to confirm.");
    ff.new_pass_repeat.focus();
    return false;
  }
  else if ((ff.new_pass.value != "") && (ff.new_pass_repeat.value != ff.new_pass.value)) {
    alert("Your passwords did not match. Please re-enter your new password.");
    ff.new_pass_repeat.value = "";
    ff.new_pass.focus();
    ff.new_pass.select();
    return false;
  }
  else {
    ff.subtn0.value = "saving...";
    ff.subtn1.value = "saving...";
    ff.subtn2.value = "saving...";
    ff.subtn3.value = "saving...";
    ff.subtn0.disabled = true;
    ff.subtn1.disabled = true;
    ff.subtn2.disabled = true;
    ff.subtn3.disabled = true;
    return true;
  }
}

function chk_site_live() {
  if (document.mms.site_live.checked == false) {
    var kk = confirm("Are you sure you want to change your account to Inactive? Your website will no longer appear to visitors upon saving changes to this form.");
    if (kk == false) {
      document.mms.site_live.checked = true;
    }
  }
}

function init_manage_form() {
  ff = eval("document.mms");
  if (ff.title_option.value == "1") {
//    $('title_text').setStyle('display','none');
    $('title_logo').setStyle('display','');
  }
  else {
//    $('title_text').setStyle('display','');
    $('title_logo').setStyle('display','none');
  }
  
  if (ff.template_type.value == "1") {
    $('fn').setStyle('display','none');
    $('ln').setStyle('display','none');
    $('cm').setStyle('display','');
  }
  else {
    $('fn').setStyle('display','');
    $('ln').setStyle('display','');
    $('cm').setStyle('display','none');
  }
}

function preview_skin() {
  Slimbox.open("/images/example_skin"+$('template_skin').value+".jpg", "TEMPLATE SKIN PROOF");
}

function site_types() {
  alert("Our website template system is designed to support an Individual Producer and an Office/Group. The difference is the Office/Group configuration gives you the ability to manage multiple contacts (or 'team members').");
}

function ot_alert() {
  if ($('ot_alrt')) {
    if ($('template_type').value == '1') {
      $('ot_alrt').setStyle('display','');
    }
    else {
      $('ot_alrt').setStyle('display','none');
    }
  }
}

function chkTeamForm() {
  if ($('myteam_fname').value == "") {
    alert("Please enter a first name.");
    $('myteam_fname').focus();
    return false;
  }
  else if ($('myteam_lname').value == "") {
    alert("Please enter a last name.");
    $('myteam_lname').focus();
    return false;
  }
  else if ($('myteam_email').value.indexOf('@') <= 0) {
    alert("Please enter a valid email address.");
    $('myteam_email').focus();
    $('myteam_email').select();
    return false;
  }
  else {
    $('myteam_subtn').value = "saving...please wait";
    $('myteam_subtn').disabled = true;
    return true;
  }
}

function chkDeleteTeam() {
  var kk = confirm("Are you sure you want to delete this team member?");
  if (kk) {
    return true;
  }
  else {
    return false;
  }
}

function chkF() {
  ff = eval("document.f");
  if (ff.name.value == "") {
    alert("Please enter your name.");
    ff.name.focus();
    return false;
  }
  else if (ff.url.value == "") {
    alert("Please enter the web address you are trying to view.");
    ff.url.focus();
    return false;
  }
  else if (ff.rmagent.value == "") {
    alert("Please enter the name of the Reverse Mortgage originator or company you are trying to find.");
    ff.rmagent.focus();
    return false;
  }
  else if (ff.email.value.indexOf('@') <= 0) {
    alert("Please enter a valid email address.");
    ff.email.focus();
    ff.email.select();
    return false;
  }
  else if (ff.phone.value == "") {
    alert("Please enter your phone number.");
    ff.phone.focus();
    return false;
  }
  else {
    ff.subtn.value = "Please wait...";
    ff.subtn.disabled = true;
    return true;
  }
}

function update_home_header_preview() {
  $('home_header_image_preview').set('html','<img src="'+$('home_header_image_dir').value+$('home_header_image').value+'" />');
}

function chkCustomPage() {
  ff = eval("document.cpf");
  if (ff.page_title.value == "") {
    alert("Please enter a page title.");
    ff.page_title.focus();
    return false;
  }
  else if (ff.page_desc.value == "") {
    alert("Please enter a page description.");
    ff.page_desc.focus();
    return false;
  }
  else if (ff.link_text.value == "") {
    alert("Please enter link text.");
    ff.link_text.focus();
    return false;
  }
  else if (tinyMCE.getInstanceById('page_content').getBody().innerHTML.replace(/(<([^>]+)>)/ig,"").trim() == "") {
    alert("Please enter content for your page.");
    return false;
  }
  else {
    ff.subtn1.value = "Saving...";
    ff.subtn2.value = "Saving...";
    ff.subtn1.disabled = true;
    ff.subtn2.disabled = true;
    return true;
  }
}

function custom_page_edit(page_id) {
  ff = eval("document.cpf");
  ff.action.value = "edit";
  ff.page_id.value = page_id;
  ff.submit();
}

function custom_page_delete(page_id) {
  ff = eval("document.cpf");
  var kk = confirm("Are you sure you want to delete this custom page?");
  if (kk) {
    ff.action.value = "delete";
    ff.page_id.value = page_id;
    ff.submit();
  }
}

function visit_my_page(pid,islock) {
  ff = eval("document.mpf");
  if (islock) {
    var pw = prompt("Please enter the password to access this page.");
    if (pw) {
      ff.password.value = pw;
      ff.page_id.value = pid;
      ff.submit();
    }
  }
  else {
    ff.page_id.value = pid;
    ff.submit();
  }
}