// <script>
//
// ********************
//
// $Workfile: menu.js $  $Revision: 3 $
// $Modtime: 9/28/09 2:53p $
//
// $NoKeywords: $
//*************************



// Copyright (C) 2005 Ilya S. Lyubinskiy. All rights reserved.
// Technical support: http://www.php-development.ru/
//
// YOU MAY NOT
// (1) Remove or modify this copyright notice.
// (2) Distribute this code, any part or any modified version of it.
//     Instead, you can link to the homepage of this code:
//     http://www.php-development.ru/javascripts/menu.php.
//
// YOU MAY
// (1) Use this code on your website.
// (2) Use this code as a part of another product provided that
//     its main use is not creating javascript menus.
//
// NO WARRANTY
// This code is provided "as is" without warranty of any kind, either
// expressed or implied, including, but not limited to, the implied warranties
// of merchantability and fitness for a particular purpose. You expressly
// acknowledge and agree that use of this code is at your own risk.




function at_display(x)
{
  var win = window.open();
  for (var i in x) win.document.write(i+' = '+x[i]+'<br>');
}

// ----- DropDown Control ------------------------------------------------------

var at_timeout = 200;

// ----- Show Aux -----

function at_show_aux(parent, child)
{
  var p = document.getElementById(parent);
  var c = document.getElementById(child);

  p.className        = "active";

  if (c.offsetWidth <= 0)
  {
    c.style.position   = "absolute";
    c.style.visibility = "visible";
    c.style.display    = "block";
  }

  var direction = undefined;
  if (p.parentNode && p.parentNode["at_position"] == "x")
    direction = p.parentNode["at_direction"];

  var top   = (c["at_position"] == "y") ?  p.offsetHeight +3 : 0;
  var left1 = (c["at_position"] == "x") ?  p.offsetWidth +0 : 0;
  var left2 = (c["at_position"] == "x") ? -c.offsetWidth +0: 0;
  var left3 = (c["at_position"] == "x") ?  p.offsetWidth +10 : 0;

  for (; p; p = p.offsetParent)
  {
    if (p.style.position != 'absolute')
    {
      left1 += p.offsetLeft;
      left2 += p.offsetLeft;
      top   += p.offsetTop;
    }
    left3 += p.offsetLeft;
  }

  if (direction)
  {
    left = (direction == 'left') ? left1 : left2;
    c['at_direction'] = direction;
  }
  else
  {
    left = (left3+c.offsetWidth == document.body.offsetWidth) ? left1 : left2;
    c['at_direction'] = (left3+c.offsetWidth == document.body.offsetWidth) ? 'right' : 'right';
  }

  c.style.position   = "absolute";
  c.style.visibility = "visible";
  c.style.display    = "block";
  c.style.top        = top +'px';
  c.style.left       = left+'px';
}

// ----- Hide Aux -----

function at_hide_aux(parent, child)
{
  document.getElementById(parent).className        = "parent";
  document.getElementById(child ).style.visibility = "hidden";
  document.getElementById(child ).style.display    = "block";
}

// ----- Show -----

function at_show(e)
{
  var p = document.getElementById(this["at_parent"]);
  var c = document.getElementById(this["at_child" ]);

  at_show_aux(p.id, c.id);

  clearTimeout(c["at_timeout"]);
}

// ----- Hide -----

function at_hide()
{
  var c = document.getElementById(this["at_child"]);

  c["at_timeout"] = setTimeout("at_hide_aux('"+this["at_parent"]+"', '"+this["at_child" ]+"')", at_timeout);
}

// ----- Attach -----

function at_attach(parent, child, position)
{
  p = document.getElementById(parent);
  c = document.getElementById(child );

  p["at_child"]    = c.id;
  c["at_child"]    = c.id;
  p["at_parent"]   = p.id;
  c["at_parent"]   = p.id;
  c["at_position"] = position;

  p.onmouseover = at_show;
  p.onmouseout  = at_hide;
  c.onmouseover = at_show;
  c.onmouseout  = at_hide;
}

// ----- DropDown Menu ---------------------------------------------------------

// ----- Build Aux -----

function dhtmlmenu_build_aux(parent, child, position)
{
  document.getElementById(parent).className = "parent";

  document.write('<div class="vert_menu" id="'+parent+'_child">');

  var n = 0;
  for (var i in child)
  {
    if (i == '-')
    {
      document.getElementById(parent).href = child[i];
      continue;
    }

    if (typeof child[i] == "object")
    {
      document.write('<a onclick="alert1();" class="parent" id="'+parent+'_'+n+'">'+i+'</a>');
      dhtmlmenu_build_aux(parent+'_'+n, child[i], "x");
    }
    else
    {
      if (is_customer_area(child[i])) {
         document.write('<a onclick="javascript:customer_alert(\'' + child[i]  + '\'); return false;" id="'+parent+'_'+n+'" href="'+child[i]+'">'+i+'</a>');
      } else {
         document.write('<a onclick="alert3();" id="'+parent+'_'+n+'" href="'+child[i]+'">'+i+'</a>');
      }
    }
    n++;
  }

  document.write('</div>');

  at_attach(parent, parent+"_child", position);
}

// ----- Build -----

function dhtmlmenu_build(menu)
{
  for (var i in menu) dhtmlmenu_build_aux(i, menu[i], "y");
}



// ====================================================================
// ----- Added by SFM to pop a dialog when going to customer area -----
// ====================================================================


// Test the url for certain patterns that indicate the url is in customer area.
// Return 1 if in customer area; 0 otherwise.
function is_customer_area(url) {
var customer = 0;
   if (url) {
      //$RegExp.$1 = "";
      var pattern1 = /(\/support\/m)/;
      var pattern2 = /(\/discus\/messages\/76)/;
      if (pattern1.test(url)) {
       customer = 1;
      }
      if (pattern2.test(url)) {
         customer = 1;
      }
   }
   return(customer > 0);
}

// =========================================================================
// An alert from one type of menu structure element; this type is not of any
// interest for testing customer area access.
// =========================================================================
function alert1() {
//  alert("Menu heading item selected");
}

// =========================================================================
// An alert from another type of menu structure element; this type is not of any
// interest for testing customer area access.
// =========================================================================
function alert3() {
//  alert("Child Menu item selected 3");
}

// =========================================================================
// An alert from a menu item that might be associated with a customer area.
// Test the url again and conditionally display a popup message.  Use a
// redirect to load the requested url.
// =========================================================================
function customer_alert(url) {
    //  This function warns customer that an additional access level is required.
    //  We only do this if cookies are enabled and set a cookie so the warning only appears once.
    //
    //  We use window.location = url to perform a redirect.

   var cookies = new Object();
   var cookieName = "CustomerAccessPolicyPopup";

   document.cookie = "cookiesenabled=yes";
   extractCookies(cookies);
   //alert("Child Menu item in customer area selected 2 " + url + cookies["cookiesenabled"]);
   if (!cookies["cookiesenabled"] ) {
      window.location = url;
      //alert ("cookies not enabled");
      return false;
   }
   if (cookies[cookieName]) {
      window.location = url;
      //alert ("has seen warning");
      return false;
   }

   if (url) {
      var pattern1 = /(\/support\/m)/;
      var pattern2 = /(\/discus\/messages\/76)/;
      if (pattern1.test(url)) {
       customer = 1;
      }
      if (pattern2.test(url)) {
         customer = 1;
      }
   }
   if (customer > 0) {
      var accessMessage = getAccessMessage();
      var response = confirm(accessMessage);
      setCookie( cookieName, 'true', '1', '/', '', '' );
      if ( ! response) {
         window.location = "/support/open/register_customer.html";
         //alert("Note:  If you do not have a current product Reaction Design License, you will not be able to sign up for customer access.");
         return false;
      } else {
         window.location = url;
         return false;
      }
   }
  return false;
}

// =========================================================================
// Build the popup message separately just to avoid visual clutter above
// =========================================================================
function getAccessMessage(msg) {
   msg = "";
   msg += "Access to customer support areas of our web site now requires an additional sign-up step.\n\n";
   msg += "To sign up, you will need your license number and organization name.\n\n";
   msg += "If you have already completed this step, click the OK button to continue to the page you requested. \n\n";
   msg += "Otherwise, click the Cancel button and the customer sign-up page will appear.  ";
   msg += "\n\n\n \n";
   return msg;
}


// =========================================================================
// Create a hash object of cookie-name=cookie-value
// from Powell and Schneider - JavaScript, the Complete Reference
// =========================================================================
function extractCookies(cookies) {
   var name, value;
   var beginning, middle, end;
   for (name in cookies) {
      //cookies = new Object();
      break;
   }
   beginning = 0;
   while (beginning < document.cookie.length) {
      middle = document.cookie.indexOf('=', beginning);
      end = document.cookie.indexOf(';', beginning);
      if (end == -1) {
         end = document.cookie.length;
      }
      if (middle > end || (middle == -1)) {
         name = document.cookie.substring(beginning, end);
         value = "";
      } else {
         name = document.cookie.substring(beginning, middle);
         value = document.cookie.substring(middle+1, end);
      }
      cookies[name] = unescape(value);
      beginning = end + 2;
   }
}

// =========================================================================
// Set a cookie value with option parameters
// from somewhere
// =========================================================================
function setCookie( name, value, expires, path, domain, secure )
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	/*
	if the expires variable is set, make the correct
	expires time, the current script below will set
	it for x number of days, to make it for hours,
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24 * 365;
	}
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}


