


/*****************************************************************************/
var exit = false;
var was_refresh = false;
(function(){
  var c = document.cookie.split(/;\s*/);
  for( var i = 0; i < c.length; i++ )
  {
    var parts = c[i].split(/\=/);
    if( parts[0] == "lastpage" && unescape(parts[1]) == document.location.toString() )
      was_refresh = true;
  }// end for()
  
  // Now set the cookie:
//  document.cookie = "lastpage=" + escape(document.location.toString()) + "; path=/;";
})();


/*****************************************************************************/
function setup_a_tags()
{
  // Now make all onsite links prevent the popup:
  var a_tags = document.getElementsByTagName("A");
  var this_website = /^(https?\:\/\/.*?\/)/.exec( document.location.toString() )[1];
  for( var i = 0; i < a_tags.length; i++ )
  {
    if( a_tags[i].href.indexOf(this_website) == 0 )
    {
      a_tags[i].onclick = disable_popup;
    }
    else if( a_tags[i].href.indexOf("javascript:") == 0 )
    {
//      a_tags[i].onclick = disable_popup;
    }
    else if( a_tags[i].href.indexOf("http://") < 0 )
    {
      a_tags[i].onclick = disable_popup;
    }
    else if( a_tags[i].href.indexOf("http://www.emailfinder.com/") == 0 )
    {
      a_tags[i].onclick = disable_popup;
    }// endif()
  }// end for()
}// end setup_a_tags()


/*****************************************************************************/
var popup;
var exit_popup_link = '';
//window.onbeforeunload = function () {
//  if( exit )
//  {
//    if( popup )
//      popup.close();
//    popup = window.open('/exit-pop.asp?link=' + escape(exit_popup_link), 'popup', 'location=0,status=0,scrollbars=1,resizable=1,height=365,width=570' );
//  }// end if()
//};


/*****************************************************************************/
function disable_popup()
{
  var old_exit = exit;
  exit = false;
  window.setTimeout(function(){ exit = old_exit; }, 1000);
  return true;
}// end disable_popup()


/*****************************************************************************/
window.setTimeout(function(){
  var field = document.getElementById("custom-input");
  if( ! field ) field = document.getElementsByName("email")[0];
  try {
    do_email_field( field );
  }
  catch( e ) {
    // Do nothing:
  };
  try {
    cause_enter_key_to_submit_forms();
  }
  catch( e ) {
    // Do nothing:
  };
}, 500);


/*****************************************************************************/
function cause_enter_key_to_submit_forms()
{
  var inputs = document.getElementsByTagName("INPUT");
  for( var i = 0; i < inputs.length; i++ )
  {
    if( inputs[i].type.toString().toLowerCase() != "text" )
      continue;
    inputs[i].onkeypress = function(e) {
      e = e ? e : window.event;
      if( e.keyCode == 13 )
        return this.form.onsubmit();
    };
  }// end for()
}// end cause_enter_key_to_submit_forms()


/*****************************************************************************/
function do_email_field( field )
{
  field.value = "enter email address";
  field.style.color = "#CCCCCC";
  field.onfocus = function() {
    if( this.value == "enter email address" )
    {
      this.value = "";
      this.style.color = "";
    }// end if()
  };
  field.onblur = function() {
    if( this.value == "" )
    {
      this.value = "enter email address";
      this.style.color = "#CCCCCC";
    }// end if()
  };
}// end do_email_field( field )


/*****************************************************************************/
function validate_forward( form )
{
  var first_name = form.first_name.value;
  var last_name = form.last_name.value;

  // Trim leading and trailing whitespace:
  first_name = first_name.replace(/^\s+/, "").replace(/\s+$/, "");
  last_name  = last_name.replace(/^\s+/, "").replace(/\s+$/, "");

  // Make sure that a value has been entered:
  if( first_name == "" || last_name == "" )
  {
    alert("Please enter both First and Last names");
    return false;
  }// end if()

  var url = form.action + "?first_name=" + escape(first_name) + "&last_name=" + escape(last_name);
  if( form.city.value != "" )
  {
    url += "&city=" + form.city.value;
  }// end if()
  if( form.state.selectedIndex != 0 )
  {
    url += "&state=" + form.state.options[ form.state.selectedIndex ].value;
  }// end if()
  exit = false;
  top.document.location = url;
  return false;
}// end validate_forward( form )


/*****************************************************************************/
function validate_reverse( form )
{
  var val = form.email.value;
  val = val.replace(/^\s+/, "");
  val = val.replace(/\s+$/, "");

  var url = form.action + "?";
  if( val.length == 0 )
  {
    alert("Please enter a valid email address.");
    form.email.focus();
    return false;
  }// end if()
  url += "email=" + escape(form.email.value);

  if( ! /^[^@]+@[^@]+\.[^@]+$/.test( val ) )
  {
    alert("Please enter a valid email address.");
    form.email.focus();
    return false;
  }// end if()

  // All clear:
  top.document.location = url;
//  form.submit();
  return false;
}// end validate_reverse( form )


/*****************************************************************************/
// Handle /url/#cam=187 links:
if(window.addEventListener)
  window.addEventListener('load', handle_load, false);
else if(window.attachEvent)
  window.attachEvent('onload', handle_load);
else if( window.onload )
  window.onload = handle_load;

function handle_load()
{
//  var url = document.location.toString().indexOf("#cam=") > -1 ? document.location.toString() : top.location.toString();
  var url = /[\#\?&]cam\=/.test( document.location.toString() ) ? document.location.toString() : top.location.toString();
  if( /[\#\?&]cam\=/.test( url ) )
  {
    if( window.frames.length == 0 )
    {
      // I'm the iframe:
      // Set up the AJAX request to set some session variables:
      var httpOb =  new XMLHttpRequest();
      var cam_id = /[\#\?&]cam\=(\d+)/.exec( url )[1];
      httpOb.open("GET", "/handlers/mfes.set_cam?cam=" + cam_id, true);
      httpOb.onreadystatechange = function() {
        if( httpOb.readyState == 4 )
        {
          // Yay!
        }// end if()
      };
      httpOb.send( null );
    }// end if()
  }// end if()
}// end handle_load()


/*****************************************************************************/
var body_onBeforeUnload_handlers = [ ];
function body_onBeforeUnload()
{
  for( var i = 0; i < body_onBeforeUnload_handlers.length; i++ )
  {
    var func = body_onBeforeUnload_handlers[i];
    func();
  }// end for()
}// end body_onBeforeUnload()


