popups = {};

function setGridLocation (formName,
                          row,
                          col)
{

  var f = window.document[formName];

  if (f != this.undefined)
  {

     var r = f.nRow;
     var oldr;

     if (r != this.undefined)
     {

        oldr = r.value;
        r.value = row;

     }

     var c = f.nCol;
     var oldc;

     if (c != this.undefined)
     {

        oldc = c.value;
        c.value = col;

     }

     var img = window.document['img' + row + col];

     if (img != this.undefined)
     {

        img.src = '/imgs/true.gif';

     }

     var ind = 'img' + oldr + oldc;
     var oimg = window.document[ind];

     if (oimg != this.undefined)
     {

        oimg.src = '/imgs/spacer1x1.gif';

     }

  }

}

function availableWarning (checkbox)
{

  if ((checkbox.checked)
      &&
      (!checkbox.defaultChecked)
     )
  {

     window.alert ("Please note!  Once you save your changes your\npage will be available to users of the site.");

  } 

  if (!checkbox.checked)
  { 

     window.alert ("Please note!  Once you save your changes your\npage WILL NOT be available to users of the site.");

  }

}

function openSmallPopup (url,
                         target,
                         extraFeatures)
{

 var features = 'height=250,width=450,scrollbars,resizable';

 return baseOpenPopup (url,
                       target,
                       features,
                       extraFeatures);

}

function baseOpenPopup (url,
                        target,
                        features,
                        extraFeatures)
{

  if (extraFeatures != this.undefined)
  {

     features = features + ',' + extraFeatures;

  }

  window.open (url,
               target,
               features).focus ();

  return false;   

}

function openPopup (url,
                    target,
                    extraFeatures)
{

  var features = 'height=555,width=585,scrollbars,resizable';

  return baseOpenPopup (url,
                        target,
                        features,
                        extraFeatures);

}

function openPopup2 (url,
                     target)
{

  return openPopup (url,
                    target,
                    'location,status');

}

function setRowBackgroundOff (element)
{

  element.className = 'nR';

}

function setRowBackgroundOn (element)
{

  element.className = 'hlR';

}

function getRadioButtonValue (form,
                              name)
{

  for (var i = 0; i < form[name].length; i++)
  {

      if (form[name][i].checked)
      {

         return form[name][i].value;

      }

  }

  return '';

}

function toggleView (elId,
		     imgId)
{
  
      var e = $(elId); 

      if (e == undefined)
      {

	 return;

      }

      if (isVisible (elId))
      {

	// Set it hidden.
	setHidden (elId,
	           imgId);

      } else {

	// Set it visible.
	setVisible (elId,
                    imgId);

      }

}

function setHidden (elId,
		    imgId)
{

    if (elId instanceof Array)
    {

        for (var i = 0; i < elId.length; i++)
        {

          setHidden (elId[i],
                     imgId);

        }

        return;

    }

    var e = $(elId); 

    if (e == undefined)
   {

       return;
       
   }

    var imge = $(imgId);
    
    e.style.visibility = 'hidden';
    e.style.display = 'none';

    if (imge != undefined)
    {

        imge.src = '/imgs/plus.gif';

    }

}

function setVisible (elId,
                     imgId)
{

    if (elId instanceof Array)
    {

        for (var i = 0; i < elId.length; i++)
        {

          setVisible (elId[i],
                      imgId);

        }

        return;

    }

    var e = $(elId); 

    if (e == undefined)
   {

       return;

   }
            
    e.className = e.className.replace (/hidden/g, '');
            
    e.style.visibility = 'visible';
    e.style.display = '';
    
    var imge = $(imgId);

    if (imge)
   {

        imge.src = '/imgs/minus.gif';

   }

}

function isVisible (id)
{

  var e = $(id);
  
  var st = $getComputedStyle (e, 'visibility');

  return st != 'hidden';

}

function $getComputedStyle (oElm, strCssRule)
{
    
    var strValue = "";
    
    if (oElm == null)
    {
        
        return strValue;
        
    }
    
    if ((document.defaultView)
        &&
        (document.defaultView.getComputedStyle)
       )
    {
    
        strValue = document.defaultView.getComputedStyle (oElm, "").getPropertyValue(strCssRule);
    
    } else if (oElm.currentStyle) {
        
        strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1)
                                                   {
    
                                                        return p1.toUpperCase();
    
                                                    });
        
        strValue = oElm.currentStyle[strCssRule];

    }

    return strValue;

}

String.prototype.trim = function ()
{

    return this.replace(/^\s*|\s*$/g,'');

}

String.prototype.endsWith = function (v)
{

    var s = this.length - v.length;
    
    if (s < 0)
    {
      
        return false;
    
    }
    
    return (this.lastIndexOf (v, s) == s);
}

function $ (id)
{
    
    if (!id)
    {
        
        return null;
        
    }
    
    if (id.id)
    {
        
        return id;
        
    }
    
    return window.document.getElementById (id);
}

/*
 * Return a form with the specified name.
 * Equivalent of doing:
 *    window.document.form[name]
 *    
 * @param name The form name.
 * @return The form.
 */
function $f (name)
{
        
    return window.document.forms[name];
}

/*
 * Return an item from a form.
 * Equivalent of doing:
 *    window.document.form[form][name]
 *
 * @param form The form name.
 * @param name The name of the form element.
 * @return The form element.
 */
function $fi (form,
              name)
{   
    return $f(form)[name];
}

function doAjaxCall (url,
		     contextObj,
		     onCompleteFunction)
{

    doPostAjaxCall (url,
		    contextObj,
		    onCompleteFunction,
		    null);

}

function doPostAjaxCall (url,
                         contextObj,
                         onCompleteFunction,
                         content,
                         async)
{

    var xmlHttp;

    // Create a new XMLHttpRequest object to talk to the Web server 
    try 
    {

        xmlHttp = new XMLHttpRequest();

    } catch (trymicrosoft) {

        try 
        {

            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");

        } catch (othermicrosoft) {

            try 
            {

                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");

            } catch (failed) {

                xmlHttp = false;

            }  

        }

    }

    if (!xmlHttp)
    {

        return null;

    }

    if (onCompleteFunction)
    {

        var func = function (xmlHttp, contextObj)
        {

            return (
                    function ()
                    {

                        if (xmlHttp.readyState == 4)
                        {

                            onCompleteFunction.call (contextObj,
                                                     xmlHttp);
                            
                        }

                    });

        };

        xmlHttp.onreadystatechange = func (xmlHttp, contextObj);

    }

    if (async == null)
    {

        async = true;

    }

    if (!content)
    {

        var pref = '?';

        if (url.indexOf ('?') != -1)
        {
            
            pref = '&';
            
        }
        
        url = url + pref + '_ajax=true';

        xmlHttp.open ("GET", 
                      url, 
                      async);

    } else {

        content = content + '&_ajax=true';

        xmlHttp.open ("POST",
                      url,
                      async);

        xmlHttp.setRequestHeader ('Content-type',
                                  'application/x-www-form-urlencoded');

    }

    // Send the request
    xmlHttp.send (content);  

    return xmlHttp;

}

function createPopup (parent,
                      id,
                      className,
                      html)
{
  
    var noid = false;
  
    if (!id)
    {

        id = getUniquePopupId ();
        noid = true;
        
    }
 
    // See if we already have the popup.
    if (popups[id])
    {

        // Just return.
        return id;
        
    }
        
    if ((html)
        &&
        (html.innerHTML)
       )
    {
        
        html = html.innerHTML;
        
    }
        
    // Create a new div.
    var ndiv = window.document.createElement ("div");
    ndiv.id = id;
    ndiv.onmouseover = function () {clearHideIn (parent);};
    ndiv.onmouseout = function () {hideIn (id, parent, 500);};
    
    if (!className)
    {
        
        className = 'popup';
        
    }
    
    ndiv.className = className;
    ndiv.style.visibility = 'hidden';
    ndiv.style.display = 'none';
    
    // Add to our divs.
    popups[id] = ndiv;
    
    if (!$('popupcontainer'))
    {
        
        // Create the container.
        var pdiv = window.document.createElement ('div');
        pdiv.id = 'popupcontainer';
        
        window.document.body.appendChild (pdiv);
        
    }
    
    $('popupcontainer').appendChild (ndiv);

    if (!html)
    {
        
        html = '';
        
    }

    ndiv.innerHTML = '<table class="layout" cellpadding="0" cellspacing="0"><tr><td class="top"><img alt="*" src="/imgs/popup-topleft.png" /></td><td class="fill top"><img alt="*" height="17" src="/imgs/popup-topcentre.png" width="100%" /></td><td class="top"><img alt="*" src="/imgs/popup-topright.png" /></td></tr><tr><td class="top hfill"><img alt="*" width="17" class="hfill" src="/imgs/popup-centreleft.png" /></td><td class="fill" style="background-color: white;"><div class="' + className + '-content" id="' + id + '_content">' + html + '</div></td><td class="top hfill"><img alt="*" width="23" class="hfill" src="/imgs/popup-centreright.png" /></td></tr><tr><td class="bottom"><img alt="*" src="/imgs/popup-bottomleft.png" /></td><td class="fill bottom"><img alt="*" height="25" src="/imgs/popup-bottomcentre.png" class="fill" /></td><td class="bottom"><img alt="*" src="/imgs/popup-bottomright.png" /></td></tr></table>';

    if (parent)
    {
        
        if (typeof parent == 'string')
        {
            
            parent = $(parent);
            
        }
        
        if (parent)
        {
        
            if (!parent.id)
            {
        
                parent.id = id + '_parent';
            
            }
        
            parent.onmouseout = function () { clearHideIn (this); hideIn (id, this, 500); };
            
        }
        
    }               
      
    return id;
            
}

function showProductPopup (key,
                           parent)
{
    
    parent.___timeout_id = setTimeout (function ()
				       {

                                           if (parent.___has_found)
                                           {
                                            
                                              showChild (parent,
                                                         'productpopup' + key);
                                            
                                              return;
                                            
                                           }

                                           doAjaxCall ('/admin/orders/ajax-view.html?id=' + key,
                                                       null,
                                                       function (xmlHttp)
                                                       {
                                                        
                                                           parent.___has_found = true;
                                                           
                                                           createPopup (parent,
                                                                        'productpopup' + key,
                                                                        null,
                                                                        xmlHttp.responseText);
                                                           
                                                           showChild (parent,
                                                                      'productpopup' + key);
                                                           
                                                           parent.___has_found = true;
                                                        
                                                       });
					    
					},
					666
				       );
    
    
}

function clearHideIn (el)
{

    if (el instanceof Array)
    {

      for (var i = 0; i < el.length; i++)
      {

        clearHideIn (el[i]);

      }

      return;

    }

    if (!el.id)
    {

        el = $(el);
        
    }

    clearTimeout (el.___timeout_id);

}

function hideIn (id,
                 el,
                 timeout)
{

    el.___timeout_id = setTimeout (function () 
                                   {

                                       if (id instanceof Array)
                                       {

                                           for (var i = 0; i < id.length; i++)
                                           {

                                               setHidden (id[i]);

                                           }

                                       } else {

                                           setHidden ($(id));

                                       }

                                   }, 
                                   timeout);

    return el.___timeout_id;

}

function showChild (parent,
                    child)
{

    var c = $(child);
    
    // Draw offscreen so we can get the height/width.
    c.style.position = "absolute";
    c.style.top = '-10000px';
    c.style.left = '-10000px';
    c.style.visibility = 'visible';
    c.style.display = '';
            
    parent = $(parent);

    var pos = getPopupPosition (parent,
                                child);

    c.style.position   = "absolute";
    c.style.top        = pos.top + 'px';
    c.style.left       = pos.left + 'px';
    c.style.visibility = "visible";
    //c.style.zIndex     = 1000;
    c.style.display    = '';

}

function getPopupPosition (parent,
                           child)
{
  
    var c = $(child);
  
    var child_width = c.offsetWidth;
    var child_height = c.offsetHeight;
    
    if (!parent.id)
    {
        
        parent = $(parent);
        
    }
    
    var left = 0;
    var top = 0;
    var height = 0;
    var screenX = 0;

    if (isIE ()) 
    {

        screenX = window.document.body.offsetWidth + 10;

    } else {

        screenX = window.outerWidth;
        
    }
            
    var elpos = getElementPosition (parent);

    top = elpos.top;
    left = elpos.left;

    top = top + parent.offsetHeight - 2;

    if ((child_width + left) > screenX)
    {

        left = screenX - child_width - 30;

    } 

    height = window.document.body.offsetHeight;

    if ((height > 0)
        &&
       ((top + child_height) > height)
       )
    {

        top = top - child_height - parent.offsetHeight;
        
    }      
    
    if (top < 0)
    {
        
        top = 10;
        left = left + 30;
        
    }
    
    return {
      
      top : top,
      left : left
        
    };
    
}

function getElementPosition (elemID,
                             parent) 
{

    if (typeof elemID == 'string')
    {
        
        var offsetTrail = $(elemID);

    } else {
        
        var offsetTrail = elemID;
        
    }
    
    var scrollLeft = offsetTrail.scrollLeft;
    var scrollTop = offsetTrail.scrollTop;
    
    var offsetLeft = 0;
    var offsetTop = 0;
    
    while (offsetTrail) 
    {

        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;

        offsetTrail = offsetTrail.offsetParent;
        
        if ((parent)
            &&
            (offsetTrail)
            &&
            (offsetTrail.id)
            &&
            (offsetTrail.id == parent.id)
           )
        {
            
            break;
            
        }
        
    }

    return {left:offsetLeft, top:offsetTop, scrollLeft: scrollLeft, scrollTop: scrollTop};

}

function isIE ()
{
        
    return window.navigator.appName == 'Microsoft Internet Explorer';
        
}

function selectText (el)
{

    if (window.document.selection)
    {

    } else {

        s = window.getSelection();
        
        var r = document.createRange();
        
        r.setStartBefore(el);
        r.setEndAfter(el);
        s.addRange(r);
    
    }

}
