// main javascript functions for  LS landscapes



/* ***********************************************************
Example 4-5 (DHTMLapi.js)
"Dynamic HTML:The Definitive Reference"
by Danny Goodman
Published by O'Reilly & Associates  ISBN 1-56592-494-0
http://www.oreilly.com
Copyright 1998 Danny Goodman.  All Rights Reserved.
************************************************************ */
// DHTMLapi.js custom API for cross-platform
// object positioning by Danny Goodman (http://www.dannyg.com)

// Global variables
var isNav, isIE
var coll = ""
var styleObj = ""
if (parseInt(navigator.appVersion) >= 4) {
  if (navigator.appName == "Netscape") {
    isNav = true
  } else {
    isIE = true
    coll = "all."
    styleObj = ".style"
  }
}

// Convert object name string or object reference
// into a valid object reference
function getObject(obj) {
  var theObj
  if (typeof obj == "string") {
    theObj = eval("document." + coll + obj + styleObj)
  } else {
    theObj = obj
  }
  return theObj
}

// Positioning an object at a specific pixel coordinate
function shiftTo(obj, x, y) {
  var theObj = getObject(obj)
  if (isNav) {
    theObj.moveTo(x,y)
  } else {
    theObj.pixelLeft = x
    theObj.pixelTop = y
  }
}

// Moving an object by x and/or y pixels
function shiftBy(obj, deltaX, deltaY) {
  var theObj = getObject(obj)
  if (isNav) {
    theObj.moveBy(deltaX, deltaY)
  } else {
    theObj.pixelLeft += deltaX
    theObj.pixelTop += deltaY
  }
}

// Setting the z-order of an object
function setZIndex(obj, zOrder) {
  var theObj = getObject(obj)
  theObj.zIndex = zOrder
}

// Setting the background color of an object
function setBGColor(obj, color) {
  var theObj = getObject(obj)
  if (isNav) {
    theObj.bgColor = color
  } else {
    theObj.backgroundColor = color
  }
}

// Setting the visibility of an object to visible
function show(obj) {
  var theObj = getObject(obj)
  theObj.visibility = "visible"
}

// Setting the visibility of an object to hidden
function hide(obj) {
  var theObj = getObject(obj)
  theObj.visibility = "hidden"
}

// Retrieving the x coordinate of a positionable object
function getObjectLeft(obj)  {
  var theObj = getObject(obj)
  if (isNav) {
    return theObj.left
  } else {
    return theObj.pixelLeft
  }
}

// Retrieving the y coordinate of a positionable object
function getObjectTop(obj)  {
  var theObj = getObject(obj)
  if (isNav) {
    return theObj.top
  } else {
    return theObj.pixelTop
  }
}

// Utility function returns the available content width space in browser window
function getInsideWindowWidth(){
  if (isNav) {
    return window.innerWidth
  } else {
    return document.body.clientWidth
  }
}

// Utility function returns the available content height space in browser window
function getInsideWindowHeight() {
  if (isNav) {
    return window.innerHeight
  } else {
    if(document.body.clientHeight>document.documentElement.clientHeight) {
      return document.body.clientHeight }
    else{
      return document.documentElement.clientHeight
    }
  }
}


// ***********************************************************
// from http://developer.apple.com/internet/webcontent/styles.html


// Copyright © 2001 by Apple Computer, Inc., All Rights Reserved.
//
// You may incorporate this Apple sample code into your own code
// without restriction. This Apple sample code has been provided "AS IS"
// and the responsibility for its operation is yours. You may redistribute
// this code, but you are not permitted to redistribute it as
// "Apple sample code" after having made changes.

// ugly workaround for missing support for selectorText in Netscape6/Mozilla
// call onLoad() or before you need to do anything you would have otherwise used
// selectorText for.
var ugly_selectorText_workaround_flag = false;
var allStyleRules;
// code developed using the following workaround (CVS v1.15) as an example.
// http://lxr.mozilla.org/seamonkey/source/extensions/xmlterm/ui/content/XMLTermCommands.js
function ugly_selectorText_workaround() {
  if((navigator.userAgent.indexOf("Gecko") == -1) ||
     (ugly_selectorText_workaround_flag)) {
    return; // we've already been here or shouldn't be here
  }
  var styleElements = document.getElementsByTagName("style");

  for(var i = 0; i < styleElements.length; i++) {
    var styleText = styleElements[i].firstChild.data;
    // this should be using match(/\b[\w-.]+(?=\s*\{)/g but ?= causes an
    // error in IE5, so we include the open brace and then strip it
    allStyleRules = styleText.match(/\b[\w-.]+(\s*\{)/g);
  }

  for(var i = 0; i < allStyleRules.length; i++) {
    // probably insufficient for people who like random gobs of
    // whitespace in their styles
    allStyleRules[i] = allStyleRules[i].substr(0, (allStyleRules[i].length - 2));
  }
  ugly_selectorText_workaround_flag = true;
}


// setStyleById: given an element id, style property and
// value, apply the style.
// args:
//  i - element id
//  p - property
//  v - value
//
function setStyleById(i, p, v) {
  var n = document.getElementById(i);
  n.style[p] = v;
}

// getStyleById: given an element ID and style property
// return the current setting for that property, or null.
// args:
//  i - element id
//  p - property
function getStyleById(i, p) {
  var n = document.getElementById(i);
  var s = eval("n.style." + p);

  // try inline
  if((s != "") && (s != null)) {
    return s;
  }

  // try currentStyle
  if(n.currentStyle) {
    var s = eval("n.currentStyle." + p);
    if((s != "") && (s != null)) {
      return s;
    }
  }

  // try styleSheets
  var sheets = document.styleSheets;
  if(sheets.length > 0) {
    // loop over each sheet
    for(var x = 0; x < sheets.length; x++) {
      // grab stylesheet rules
      var rules = sheets[x].cssRules;
      if(rules.length > 0) {
        // check each rule
        for(var y = 0; y < rules.length; y++) {
          var z = rules[y].style;
          // selectorText broken in NS 6/Mozilla: see
          // http://bugzilla.mozilla.org/show_bug.cgi?id=51944
          ugly_selectorText_workaround();
          if(allStyleRules) {
            if(allStyleRules[y] == i) {
              return z[p];
            }
          } else {
            // use the native selectorText and style stuff
            if(((z[p] != "") && (z[p] != null)) ||
               (rules[y].selectorText == i)) {
              return z[p];
            }
          }
        }
      }
    }
  }
  return null;
}

// setStyleByClass: given an element type and a class selector,
// style property and value, apply the style.
// args:
//  t - type of tag to check for (e.g., SPAN)
//  c - class name
//  p - CSS property
//  v - value
var ie = (document.all) ? true : false;

function setStyleByClass(t,c,p,v){
  var elements;
  if(t == '*') {
    // '*' not supported by IE/Win 5.5 and below
    elements = (ie) ? document.all : document.getElementsByTagName('*');
  } else {
    elements = document.getElementsByTagName(t);
  }
  for(var i = 0; i < elements.length; i++){
    var node = elements.item(i);
    for(var j = 0; j < node.attributes.length; j++) {
      if(node.attributes.item(j).nodeName == 'class') {
        if(node.attributes.item(j).nodeValue == c) {
          eval('node.style.' + p + " = '" +v + "'");
        }
      }
    }
  }
}

// getStyleByClass: given an element type, a class selector and a property,
// return the value of the property for that element type.
// args:
//  t - element type
//  c - class identifier
//  p - CSS property
function getStyleByClass(t, c, p) {
  // first loop over elements, because if they've been modified they
  // will contain style data more recent than that in the stylesheet
  var elements;
  if(t == '*') {
    // '*' not supported by IE/Win 5.5 and below
    elements = (ie) ? document.all : document.getElementsByTagName('*');
  } else {
    elements = document.getElementsByTagName(t);
  }
  for(var i = 0; i < elements.length; i++){
    var node = elements.item(i);
    for(var j = 0; j < node.attributes.length; j++) {
      if(node.attributes.item(j).nodeName == 'class') {
        if(node.attributes.item(j).nodeValue == c) {
          var theStyle = eval('node.style.' + p);
          if((theStyle != "") && (theStyle != null)) {
            return theStyle;
          }
        }
      }
    }
  }
  // if we got here it's because we didn't find anything
  // try styleSheets
  var sheets = document.styleSheets;
  if(sheets.length > 0) {
    // loop over each sheet
    for(var x = 0; x < sheets.length; x++) {
      // grab stylesheet rules
      var rules = sheets[x].cssRules;
      if(rules.length > 0) {
        // check each rule
        for(var y = 0; y < rules.length; y++) {
          var z = rules[y].style;
          // selectorText broken in NS 6/Mozilla: see
          // http://bugzilla.mozilla.org/show_bug.cgi?id=51944
          ugly_selectorText_workaround();
          if(allStyleRules) {
            if((allStyleRules[y] == c) ||
               (allStyleRules[y] == (t + "." + c))) {
              return z[p];
            }
          } else {
            // use the native selectorText and style stuff
            if(((z[p] != "") && (z[p] != null)) &&
               ((rules[y].selectorText == c) ||
                (rules[y].selectorText == (t + "." + c)))) {
              return z[p];
            }
          }
        }
      }
    }
  }

  return null;
}

// setStyleByTag: given an element type, style property and
// value, and whether the property should override inline styles or
// just global stylesheet preferences, apply the style.
// args:
//  e - element type or id
//  p - property
//  v - value
//  g - boolean 0: modify global only; 1: modify all elements in document
function setStyleByTag(e, p, v, g) {
  if(g) {
    var elements = document.getElementsByTagName(e);
    for(var i = 0; i < elements.length; i++) {
      elements.item(i).style[p] = v;
    }
  } else {
    var sheets = document.styleSheets;
    if(sheets.length > 0) {
      for(var i = 0; i < sheets.length; i++) {
        var rules = sheets[i].cssRules;
        if(rules.length > 0) {
          for(var j = 0; j < rules.length; j++) {
            var s = rules[j].style;
            // selectorText broken in NS 6/Mozilla: see
            // http://bugzilla.mozilla.org/show_bug.cgi?id=51944
            ugly_selectorText_workaround();
            if(allStyleRules) {
              if(allStyleRules[j] == e) {
                s[p] = v;
              }
            } else {
              // use the native selectorText and style stuff
              if(((s[p] != "") && (s[p] != null)) &&
                 (rules[j].selectorText == e)) {
                s[p] = v;
              }
            }

          }
        }
      }
    }
  }
}

// getStyleByTag: given an element type and style property, return
// the property's value
// args:
//  e - element type
//  p - property
function getStyleByTag(e, p) {
  var sheets = document.styleSheets;
  if(sheets.length > 0) {
    for(var i = 0; i < sheets.length; i++) {
      var rules = sheets[i].cssRules;
      if(rules.length > 0) {
        for(var j = 0; j < rules.length; j++) {
          var s = rules[j].style;
          // selectorText broken in NS 6/Mozilla: see
          // http://bugzilla.mozilla.org/show_bug.cgi?id=51944
          ugly_selectorText_workaround();
          if(allStyleRules) {
            if(allStyleRules[j] == e) {
              return s[p];
            }
          } else {
            // use the native selectorText and style stuff
            if(((s[p] != "") && (s[p] != null)) &&
               (rules[j].selectorText == e)) {
              return s[p];
            }
          }

        }
      }
    }
  }

  // if we don't find any style sheets, return the value for the first
  // element of this type we encounter without a CLASS or STYLE attribute
  var elements = document.getElementsByTagName(e);
  var sawClassOrStyleAttribute = false;
  for(var i = 0; i < elements.length; i++) {
    var node = elements.item(i);
    for(var j = 0; j < node.attributes.length; j++) {
      if((node.attributes.item(j).nodeName == 'class') ||
         (node.attributes.item(j).nodeName == 'style')){
         sawClassOrStyleAttribute = true;
      }
    }
    if(! sawClassOrStyleAttribute) {
      return elements.item(i).style[p];
    }
  }
}






/* *********************************************************** */

// mta stuff


// function to get the height of an element by using its ID
// (e.g.   <div class=menu2 id=menuitem2> becomes getHeightOfID("menuitem2")

function getHeightOfId(ID){
  if(arguments.length!=1)alert("Wrong number of arguments to getHeightOfID()");
  var ns6=document.getElementById&&!document.all
  var ie=document.all

  if((!ie)&&(!ns6)){
    var AlertStr="Alert you appear to be running an outdated browser. ";
    AlertStr=AlertStr+"Please upgrade to a more recent browser. ";
    AlertStr=AlertStr+"Some things on this site will not work properly with your current browser.";
    alert(AlertStr);
  }

  if(ns6){
    return(document.getElementById(ID).offsetHeight);
  }

  if(ie){
    return(eval("document.all." + ID + ".clientHeight"));
  }

  return(50);
}

// almost identical, but using offsetheight for the IE return as clientheight sometimes returns 0

function getHeightOfId_OH(ID){
  if(arguments.length!=1)alert("Wrong number of arguments to getHeightOfID_OH()");
  var ns6=document.getElementById&&!document.all
  var ie=document.all

  if((!ie)&&(!ns6)){
    var AlertStr="Alert you appear to be running an outdated browser. ";
    AlertStr=AlertStr+"Please upgrade to a more recent browser. ";
    AlertStr=AlertStr+"Some things on this site will not work properly with your current browser.";
    alert(AlertStr);
  }

  if(ns6){
    return(document.getElementById(ID).offsetHeight);
  }

  if(ie){
    return(eval("document.all." + ID + ".offsetHeight"));
  }

  return(50);
}



// procs fo allow pages wrongly called outside of frames to reposition themselves

function BreakOutOfFrames(){
  if (window != top) top.location.href = location.href;
}



// function to force very soft reloading of pages when browser resizes

function onResizeProc(){history.go(0);}


// proc to set innerhtml of an element

function setInnerHTML(ID,str){
  var ns6=document.getElementById&&!document.all
  var ie=document.all
  var objfound;

  if((!ie)&&(!ns6)){
    var AlertStr="Alert you appear to be running an outdated browser. ";
    AlertStr=AlertStr+"Please upgrade to a more recent browser. ";
    AlertStr=AlertStr+"Some things on this site will not work properly with your current browser.";
    alert(AlertStr);
  }

  if(ns6){
    objfound=document.getElementById(ID);
  }

  if(ie){
    objfound=eval("document.all." + ID);
  }
  objfound.innerHTML=str;
}


// proc to get innerHTML of an element
function getInnerHTML(ID){
  if(arguments.length!=1)alert("getInnerHTML needs 1 argument");

  var ns6=document.getElementById&&!document.all
  var ie=document.all

  if(ie){
    return(eval("document.all." + ID + ".innerHTML"));
    }
  else{
    if(ns6){
      return(document.getElementById(ID).innerHTML);
    }
  }
}







// writing cookies


function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}



// function to get the width of an element by using its ID
// (e.g.   <div class=menu2 id=menuitem2> becomes getWidthOfID("menuitem2")

function getWidthOfID(ID){
  if(arguments.length!=1)alert("Wrong number of arguments to getWidthOfID()");
  var ns6=document.getElementById&&!document.all
  var ie=document.all

  if((!ie)&&(!ns6)){
    var AlertStr="Alert you appear to be running an outdated browser. ";
    AlertStr=AlertStr+"Please upgrade to a more recent browser. ";
    AlertStr=AlertStr+"Some things on this site will not work properly with your current browser.";
    alert(AlertStr);
  }

  if(ns6){
    return(document.getElementById(ID).offsetWidth);
  }

  if(ie){
    return(eval("document.all." + ID + ".clientWidth"));
  }

  return(50);
}






// function to hide email addresses from robots

var atstr="@";
var str1="in";
var str2="fo";
var str3="ls-l";
var str4="ands";
var str5="cape";
var str6="s.c";
var str7="o.u";
var str8="k";

function blat(){
  document.write("<a href='mailto:" + str1 + str2 + atstr + str3 + str4 + str5 + str6 + str7 + str8 + "'>");
  document.write(str1 + str2 + atstr + str3 + str4 + str5 + str6 + str7 + str8 + "</a>");
}


// LS Landscapes stuff
var lspace;
var rspace;
var minlspace=110;

 function compute_lrspace(){
   var sparespace=getInsideWindowWidth()-50-700; // allow for margins on body and width of middle column (700)
   lspace=Math.round(sparespace*0.56);
   if(lspace<minlspace)lspace=minlspace;   
   rspace=sparespace-lspace;
   if(rspace<0)rspace=0;
 }



function lspacer(){
  compute_lrspace();
  document.write("<img src='img/5x5.gif' height=10 width=" + lspace + ">");
}


// if there is an argument to this, the height of the rspacer is set to this
// (this means it's possible to set the height to be very small, for the
// landscapes page

function rspacer(){
  compute_lrspace();
  var rspacerheight=getInsideWindowHeight()-20;
  if(rspacerheight<400)rspacerheight=400;
  if(arguments.length>0)rspacerheight=arguments[0];
  document.write("<img src='img/5x5.gif' height=" + rspacerheight + " width=" + rspace + ">");
}





// mouseovers

// function to turn visibility on and off for a given element
// syntax: setvisibility([element id] [visibility status (hidden|visible)]))

function setvisibility(element,status){
//alert("setvisibility: element " + element + ", status: " + status);
  if(arguments.length!=2)alert("two arguments needed: [element id] [visibility status (hidden|visible)]");
  var ns6=document.getElementById&&!document.all
  var ie=document.all
  var ele;

  if(ie){
    ele=(eval("document.all." + element));
    }
  else{
    if(ns6){
      ele=(document.getElementById(element));
    }
  }
  ele=ele.style;
  ele.visibility=status;
}


function faboutover(){
  setvisibility("fabout","visible");
}


function faboutout(){
  setvisibility("fabout","hidden");
}

function flandscapingover(){
  setvisibility("flandscaping","visible");
}

function flandscapingout(){
  setvisibility("flandscaping","hidden");
}

function fmaintenanceover(){
  setvisibility("fmaintenance","visible");
}

function fmaintenanceout(){
  setvisibility("fmaintenance","hidden");
}

function fshedsover(){
  setvisibility("fsheds","visible");
}


function fshedsout(){
  setvisibility("fsheds","hidden");
}

function fprofover(){
  setvisibility("fprof","visible");
}

function fprofout(){
  setvisibility("fprof","hidden");
}

function fcontactover(){
  setvisibility("fcontact","visible");
}

function fcontactout(){
  setvisibility("fcontact","hidden");
}




// innerpages
var changestrings=new Array();
changestrings["home"]="Return to the LS Landscapes home page<br>&nbsp;";
changestrings["about"]="The LS Landscapes team, and what we do<br>&nbsp;";
changestrings["landscaping"]="Information on our landscaping work, implementing designs and planting gardens<br>with references from clients, and photos&nbsp;";
changestrings["maintenance"]="Let us take care of your routine garden maintenance<br>&nbsp;";
changestrings["shed"]="We also supply and erect high-quality sheds<br>&nbsp;";
changestrings["prof"]="We&#146;re members of the Guild of Master Craftsmen, and the Marshalls Register of Approved&nbsp;Landscape&nbsp;Contractors&nbsp;and&nbsp;Driveway&nbsp;Installers ";
changestrings["contact"]="Why not see what we can do for you?<br>&nbsp;";


function setchangestr(str){
//alert(changestrings[str]);
  setInnerHTML("change",changestrings[str]);
}

function clearchangestr(){
  setInnerHTML("change","&nbsp;<br>&nbsp;");
}

