//-----------------------------------------------------------
// GLOBAL FUNCTIONS
//-----------------------------------------------------------
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };
function getObj(name){
  return document.getElementById(name)
}
function findPos(obj){
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj == obj.offsetParent) {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
    }
    return [curleft,curtop];
}

//----------------------------------------------------
// Dynamically assign onmouseover events to nav2.
// This works but I haven't had time to make better
// images.  So it's not implemented.  All you have to do is uncomment the 
// addloadevent.
//----------------------------------------------------

function prepareNav2(){
  if( document.getElementById &&
      document.getElementsByTagName ){
    if( document.getElementById( 'nav2' ) ){
      var miceover = document.getElementById( 'nav2' );
      var links = miceover.getElementsByTagName( 'img' );
      for( var i=0; i < links.length; i++ ){
        links[i].onmouseover = function(){
          return rollover(this);
        };
        links[i].onmouseout = function(){
          return rollout(this);
        };        
      }
    }
  }
}
function rollover(id){
    var oversrc = id.src;
    var suffix = oversrc.substring(oversrc.lastIndexOf('.'));
    id.src = oversrc.substring(0,oversrc.lastIndexOf('.')) + "_over" + suffix;
}
function rollout(id){
    var oversrc = id.src;
    var suffix = oversrc.substring(oversrc.lastIndexOf('.'));
    var fred = oversrc.substring(0,oversrc.lastIndexOf('_')) + "" + suffix;
    id.src = fred;
}

//-----------------------------------------------------------
// Pop up new window
//------------------------------------------------------------

var popUpWin=0;
function popUpWindow(URLStr, left, top, width, height) {
  if(popUpWin) {
    if(!popUpWin.closed) popUpWin.close();
  }
  popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menub ar=no,scrollbars=yes,resizable=yes,copyhistory=no,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}

//-----------------------------------------------------------
// Pop up the "Build it Better" guarantee and 
// place it near the guarantee image in the banner.
//------------------------------------------------------------

function showG(){
    var obj = getObj('g');
    var id = getObj('guarantee');
    var pos = findPos(id);
    var x = pos[0];
    var y = pos[1];
    obj.style.display = 'block';
    obj.style.top = y + 75 + 'px';
    obj.style.left = x + 325 + 'px';
}
function hideG(){
    var obj = getObj('g');
    obj.style.display = 'none';
}

//-----------------------------------------------------------
// Pop up the "other services" descriptions onmouseover and
// give the table rows a bg color.  Show  "other services" 
// descriptions onclick.
//------------------------------------------------------------

function showG(name){
    var obj = getObj(name);
    obj.style.display = 'block';
}
function hideG(name){
    var obj = getObj(name);
    obj.style.display = 'none';
}
function prepareRoll(){
  if( document.getElementById && document.getElementsByTagName ){
    if( document.getElementById( 'tableOther' ) ){
      var miceover = document.getElementById( 'tableOther' );
      var links = miceover.getElementsByTagName( 'tr' );
      for( var i=0; i < links.length; i++ ){
        links[i].onmouseover = function(){
          return rollover(this);
        };
        links[i].onmouseout = function(){
          return rollout(this);
        };        
      }
    }
  }
}
function rollover(id){
    var rows = id.getElementsByTagName( 'td' );
    var cell1 = rows[0];
    var cell1a = cell1.className;
    var cell2 = rows[1];    
    showG(cell1a);    
    cell1.style.backgroundColor = "#C6D7EF";
    cell1.style.cursor = "pointer";
    cell2.style.backgroundColor = "#C6D7EF";
}
function rollout(id){
    var rows = id.getElementsByTagName( 'td' );
    var cell1 = rows[0];
    var cell1a = cell1.className;
    var cell2 = rows[1];
    hideG(cell1a);
    cell1.style.backgroundColor = "#fff";
    cell2.style.backgroundColor = "#fff";
}

//-----------------------------------------------------------
// Validation contact forms.
//------------------------------------------------------------ 

function checkFormData(frm){
    var sError = '';
    frm.SubmitButton.disabled=true;
    if (frm.Name.value.trim() == "") sError += " - Please enter your name\n";
    if (frm.Phone.value.trim() == "") sError += " - Please enter your phone number\n";
    if (frm.Email.value.trim() == "") sError += " - Please enter your email address\n";
    if (sError.length > 0){
        alert('Please correct the following errors:\n\n' + sError); 
        frm.SubmitButton.disabled=false; 
        return false; 
    }
    return true;
}

//----------------------------------------------------
// Functions inside the call to addLoadEvent will
// fire when the page loads.  If there is already
// a window.onload function on the page, then the
// addLoadEvent function will create a brand new function
// so that there is no conflict.
//----------------------------------------------------

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } 
  else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
/*
addLoadEvent(function(){
   prepareNav2();
});
*/

