var dom = (document.getElementById) ? true : false;
var ie4 = (document.all && !dom) ? true : false;
var ie5 = (document.all && dom) ? true : false;
var ns4 = (document.layers && !dom) ? true : false;
var ns5 = ((navigator.userAgent.indexOf("Gecko")>-1) && dom) ? true : false;
var ns6 = (!document.all && dom) ? true : false;
var nodyn = (!ns5 && !ns4 && !ie4 && !ie5) ? true : false;
if (nodyn) { event = "nope" }

if(ie4)
	document.getElementById = function(id) {return document.all[id];}
else if(!document.getElementById && !document.all)
  document.getElementById = function(id) {return {style:{}};}

String.prototype.trim = function() // trims white space off both ends of a string
{
  return (this.replace(/^\s*/, "")).replace(/\s*$/, "");
}

function HidePopUpBox()
{
  var oDiv = document.getElementById("PopUpBox");
  
  if (oDiv)
  {
    oDiv.style.visibility = "hidden";
  }
}

function ShowPopUpBox(evt, HTML, CssClass)
{
  var oDiv = document.getElementById("PopUpBox");
  
  if (oDiv)
  {
    var offX = -15;
    var offY = 15;
    var mousePosition = new XYCoords();
    
    mousePosition = getEventCoords(evt);
    oDiv.style.left = (mousePosition.x+offX)+'px';
    oDiv.style.top = (mousePosition.y+offY)+'px';
    oDiv.innerHTML = (HTML != null ? unescape(HTML) : '');
    oDiv.style.visibility = "visible";
    oDiv.className = (CssClass != null) ? CssClass : '';
  }
}

XYCoords = function (x,y)
{ 
  this.x = x || 0;
  this.y = y || 0;	
  return this;
}

function getWindowSize()
{
  var WindowSize = new XYCoords();
  
  if (typeof( window.innerWidth ) == 'number')
  {
    // Non-IE
    WindowSize.x = window.innerWidth;
    WindowSize.y = window.innerHeight;
  }
  else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight) )
  {
    // IE 6+ in 'standards compliant mode'
    WindowSize.x = document.documentElement.clientWidth;
    WindowSize.y = document.documentElement.clientHeight;
  }
  else if (document.body && (document.body.clientWidth || document.body.clientHeight))
  {
    // IE 4 compatible
    WindowSize.x = document.body.clientWidth;
    WindowSize.y = document.body.clientHeight;
  }
  return WindowSize;
}

function getScrollXY()
{
  var scrOfX = 0, scrOfY = 0;
  
  if (typeof( window.pageYOffset ) == 'number' )
  {
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	}
	else if (document.body && (document.body.scrollLeft || document.body.scrollTop))
	{
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	}
	else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
	{
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
}

function getEventCoords(e)
{
  var coords = new XYCoords();
  
  if (!e)
  {
    e = window.event;
  }
  
  if (!e || (typeof(e.pageX) != 'number' && typeof(e.clientX) != 'number'))
  {
    return coords;
  }
  
	if (typeof(e.pageX) == 'number')
	{
	  coords.x = e.pageX;
	  coords.y = e.pageY;
	}
	else
	{
		coords.x = e.clientX;
		coords.y = e.clientY;
		
		if (!((window.navigator.userAgent.indexOf('Opera') + 1 ) || (window.ScriptEngine && ScriptEngine().indexOf( 'InScript') + 1 && !document.createElement) || window.navigator.vendor == 'KDE'))
		{
			if (document.documentElement && (document.documentElement.scrollTop || document.documentElement.scrollLeft))
			{
				coords.x += document.documentElement.scrollLeft;
				coords.y += document.documentElement.scrollTop;
			}
			else if (document.body && (document.body.scrollTop || document.body.scrollLeft))
			{
				coords.x += document.body.scrollLeft;
				coords.y += document.body.scrollTop;
			}
		}
	}
  return coords;
}

function StartClock(idImg, stime)
{
  var d = new Date();
  var ltime = d.getTime();
  document.clockDiff = (ltime - stime);
  document.clockID = null;
  document.clockRunning = false;
  document.clockImg = idImg;
  document.lMinutes = -1;
  UpdateClock();
}

function StopClock()
{
	if (document.clockRunning)
	{
	  clearTimeout(clockID);
  }
	document.clockRunning = false;
}

function UpdateClock()
{
	var ltime = new Date(); 
	var ltime_unix = ltime.getTime();
	ltime = new Date(ltime_unix - document.clockDiff);
	
	var lHours = ltime.getHours();
	var lMinutes = ltime.getMinutes();
	var sHours = ltime.getUTCHours();
	var sMinutes = ltime.getUTCMinutes();
	
  var oIMG = document.getElementById(document.clockImg);
	if (oIMG && lMinutes != document.lMinutes)
	{
    oIMG.alt = 'UTC Time: ' + fmtTime(sHours) + ':' + fmtTime(sMinutes) + ' Local Time: ' + fmtTime(lHours) + ':' + fmtTime(lMinutes);
    document.lMinutes = lMinutes;    
	}
	var wtime = ltime_unix - document.clockDiff;
	clockID = setTimeout('UpdateClock()',1000 - (wtime % 1000));
	clockRunning = true;
}

function fmtTime(t)
{
  return (t < 10) ? ('0' + t) : t;
}

function addToFavorites()
{
  if (window.external)
    window.external.AddFavorite('http://www.yardandgroom.com', 'YardandGroom.com');
  else 
    alert("Sorry! Your browser doesn't support this function.");
}

function goURL(url,target)
{
  if (target == null || target == "_self")
  {
    window.location.href=url;
  }
  if (target == "_blank")
  {
    window.open(url);
 }
}

function openWin(url, name, scrollbars, resizeable, width, height)
{
  getWin(url, name, scrollbars, resizeable, width, height);
}

function getWin(url, name, scrollbars, resizeable, width, height)
{
  return window.open(url, name, 'menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars='+ scrollbars +',width='+ width +',height='+ height +',resizable=' + resizeable);
}

function PrintIt()
{
  if (window.print)
  {
    window.print();
  } else
  {
    msg = "Javascript cannot print from your browser. To print this page, select the menu item File - Print at the top of this page.";
    alert(msg);
  }
  return false ;
}

function setFocus(id)
{
  var oControl = document.getElementById(id);
  if (oControl)
  {
    oControl.focus();
  }
}

function onTextBoxFocus(oInput, maxLength)
{
  if (oInput)
  {
    oInput.style.backgroundColor = '#FFFFFF';
    if (oInput.type == 'text' || oInput.type == 'textarea' || oInput.type == 'password') {
      if (maxLength >= 1)
      {
        if (oInput.onkeydown == null)
        {
          oInput.onkeydown = (function(){textCounter(oInput, maxLength)});
          oInput.onkeyup = (function(){textCounter(oInput, maxLength)});
        }
        textCounter(oInput, maxLength);
      }
    }
  }
}

function onTextBoxBlur(oInput)
{
  if (oInput)
  {
    oInput.style.backgroundColor = '#FFFFFF';
    window.status = '';
  }
}

function textCounter(oInput, maxLength)
{
  if (oInput)
  {
    if (oInput.value.length > maxLength) // if too long...trim it!
    {
      oInput.value = oInput.value.substring(0, maxLength);
    }
    
    var spanUsed = document.getElementById(oInput.id + '_CharsUsed')
    var spanLeft = document.getElementById(oInput.id + '_CharsLeft')
    
    if (spanUsed)
    {
      spanUsed.innerHTML = oInput.value.length;
    }
    if (spanLeft)
    {
      spanLeft.innerHTML = (maxLength - oInput.value.length);
    }
  }
}

function getConfirm()
{
  return (confirm('Are you sure?'));
}

function setCookie (name, value, lifespan, access_path)
{

  var cookietext = name + "=" + escape(value);

  if (lifespan != null)
  {
    var today=new Date();
    var expiredate = new Date();
    expiredate.setTime(today.getTime() + 1000*60*60*24*lifespan);
    cookietext += "; expires=" + expiredate.toGMTString();
  }

  if (access_path != null)
  {
    cookietext += "; PATH="+access_path;
  }

  document.cookie = cookietext;
  return null;
}


function setDatedCookie(name, value, expire, access_path)
{
  var cookietext = name + "=" + escape(value) + ((expire == null) ? "" : ("; expires=" + expire.toGMTString()));

  if (access_path != null)
  {
    cookietext += "; PATH="+access_path;
  }
  
  document.cookie = cookietext;
  return null;
}

function getCookie(Name)
{
  var search = Name + '=';
  var CookieString = document.cookie;
  var result = null;
  
  if (CookieString.length > 0)
  {                
    offset = CookieString.indexOf(search);
    if (offset != -1)
    {
      offset += search.length;
      end = CookieString.indexOf(';', offset);
      if (end == -1)
      {
        end = CookieString.length;
      }
      result = unescape(CookieString.substring(offset, end));
    } 
  }
  return result;
}

function deleteCookie(Name, Path)
{
  setCookie(Name,"Deleted", -1, Path);
}

function Querystring(qs)
{
  // optionally pass a querystring to parse
  this.params = new Object();
  this.get = Querystring_get;
	
  if (qs == null)
  {
    qs = location.search.substring(1,location.search.length);
  }
  
  if (qs.length == 0) return;

  // Turn <plus> back to <space>
  // See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
  qs = qs.replace(/\+/g, ' ');
  var args = qs.split('&'); // parse out name/value pairs separated via &
	
	// split out each name=value pair
  for (var i=0; i<args.length; i++)
  {
    var value;
    var pair = args[i].split('=');
    var name = unescape(pair[0]);

    if (pair.length == 2)
      value = unescape(pair[1])
    else
      value = name;
		
    this.params[name] = value;
  }
}

function Querystring_get(key, default_)
{
	// This silly looking line changes UNDEFINED to NULL
  if (default_ == null) default_ = null;
	
  var value = this.params[key];
  if (value == null) value = default_;
	
	return value;
}

function selectItem(sel, item)
{
  if (sel && item)
  {
    for (var i = 0; i < sel.options.length; i++)
    {
      if (sel[i].value == item)
      {
        sel[i].selected = true;
        return true;
      }
    }
  }
  return false;
}

function getPriceRangeValue(minPrice, maxPrice)
{
  if (minPrice && maxPrice) return minPrice + '-' + maxPrice;
  if (minPrice) return minPrice + '-';
  if (maxPrice) return '0-' + maxPrice;
  return null;
}

