/*
 * application wide js
 * requires /media/prototype.js
 * requires /media/mm.js
 * requires /media/real_commerce.css
 */

/*
 * BUTTON Hovers
 *
 * handles IE6 failing to hover non <a> elements
 * for consistency and to avoid having to detect the browser
 * and for a less complicated stylesheet we use the same code
 * for all browser rather than relying on button:hover pseudo classes
 */
function handleButtonHover(event)
{
  button = $(Event.findElement(event, 'button'));
  if (typeof button == 'undefined') {
    return;
  }
  
  if (button.tagName &&   /* need it for IE6 */
      (button.match('button.rc') ||
       button.match('button.rcHover') ||
       button.match('button.rcSml') ||
       button.match('button.rcSmlHover') ||
       button.match('button.rcLrg') ||
       button.match('button.rcLrgHover')))
  {
    span = button.getElementsBySelector('span').shift();
    if (!span) 
    {
      alert('no span found! you must have one for sliding doors to work!');
      return;
    }

    if (button.className.endsWith('Hover'))
    {
      button.className = button.className.sub('Hover', ''); 
    } 
    else
    {
      button.className = button.className + 'Hover'; 
    }
  } 
}

/*
 * IE6 <button> click fix
 *
 * IE6 submits all the <button> elements in a form
 * stop it doing this by disabling all but the clicked one
 *
 */
function handleButtonClick(event)
{
  button = $(Event.findElement(event, 'button'));
  if (typeof button == 'undefined') {
    return;
  }

  if (button.tagName == 'BUTTON')
  {
    form = $(button.form);
    allbuttons = form.getElementsBySelector('button');
    for (var i = 0; i < allbuttons.length; i++)
    {
      if (allbuttons[i] != button)
      {
        allbuttons[i].disabled=true;
      }
    }
    /* proceed as normal with click */
    return true;
  } 
}

/* 
 * register the necessary event handler, but not until the DOM has loaded 
 * becuase we need the <body> element to already exist when we resgister the
 * click event to it
 */
Event.observe(window, 'load', function() {
  Event.observe(document.body, 'mouseover', handleButtonHover);
  Event.observe(document.body, 'mouseout', handleButtonHover);
  Event.observe(document.body, 'click', handleButtonClick);
});

/*
 * End of BUTTON Hovers
 */

/*
 * Returns the current mouse position
 * as an object.
 */
function getMousePos(e)
{
  var IE = document.all ? true : false;

  if (IE)
  {
    var mousePos = {
      x: event.clientX + document.documentElement.scrollLeft,
      y: event.clientY + document.documentElement.scrollTop
    };
  }
  else
  {
    var mousePos = {
      x: e.pageX,
      y: e.pageY
    };
  }

  return mousePos;
}

/**
 * Creates a popup window
 * based on mouse position.
 */
function popup(e, url, width, height)
{
  var mouse = getMousePos(e);
  
  if (!width) { width = 400; }
  if (!height) { height = 200; }
  
  WindowObjectReference = window.open(url, 'Information',"menubar=no,location=no,resizable=yes,scrollbars=yes,left=" + (mouse.x - 300) + ",top=" + (mouse.y - 150) + ",width=" + width +",height=" + height);
}

function getCookie( name ) {
  var start = document.cookie.indexOf( name + "=" );
  var len = start + name.length + 1;
  if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
    return null;
  }
  if ( start == -1 )
  {
    return null;
  }
  var end = document.cookie.indexOf( ";", len );
  if ( end == -1 ) end = document.cookie.length;
  return unescape(document.cookie.substring( len, end ));
}

function setCookie( name, value, expires, path, domain, secure ) {
  var today = new Date();
  today.setTime( today.getTime() );
  if ( expires ) {
    expires = expires * 1000 * 60 * 60 * 24;
  }
  var expires_date = new Date( today.getTime() + (expires) );
  document.cookie = name+"="+escape( value ) +
    ( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) + //expires.toGMTString()
    ( ( path ) ? ";path=" + path : "" ) +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
}

function deleteCookie( name, path, domain ) {
  if ( getCookie( name ) ) document.cookie = name + "=" +
                             ( ( path ) ? ";path=" + path : "") +
                             ( ( domain ) ? ";domain=" + domain : "" ) +
                             ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function checkForPrompt(obj_form, fieldname, default_value, error_message)
{
  if (obj_form[fieldname].value == default_value || obj_form[fieldname].value == '')
  {  
    alert(error_message); 
    return false;
  }
}
function clearEmptyPrompt(obj_textbox, default_prompt)
{
  if (obj_textbox.value == default_prompt)
  {
    obj_textbox.value = '';
  }
}

/**
* Returns the value of the selected radio button in the radio group, null if
* none are selected, and false if the button group doesn't exist
*
* @param {radio Object} or {radio id} el
* OR
* @param {form Object} or {form id} el
* @param {radio group name} radioGroup
*/
function $RF(el, radioGroup) {
    if($(el).type && $(el).type.toLowerCase() == 'radio') {
        var radioGroup = $(el).name;
        var el = $(el).form;
    } else if ($(el).tagName.toLowerCase() != 'form') {
        return false;
    }

    var checked = $(el).getInputs('radio', radioGroup).find(
        function(re) {return re.checked;}
    );
    return (checked) ? $F(checked) : null;
}
