﻿/***************************************************************************
* Canadian Restaurant Supply Ltd.                                          *
* 100 - 2602 Enterprise Way                                                *
* Kelowna, BC  V1X 7Y5                                                     *
* @copyright 2007-2011 Canadian Restaurant Supply Ltd.                     *
* All Rights Reserved                                                      *
***************************************************************************/

// Delcare all Public variables here

var mousePos = null;
var winSize = null;

var ns4;
var op5;
var op6;
var mac;
var ie;
var mac_ie;

var specialNotices;
var specialFolder = 'http://www.crs-online.ca/images/box/';
var whichNotice;
var specialTimer;
var noticesOn = false;

var specialDelay = 10000; // milliseconds: 10000 milliseconds = 10 seconds.

// Functions and Subroutines here

function mouseMove(ev) {
  ev = ev || window.event;
  mousePos = mouseCoords(ev);
}

function mouseCoords(ev) {
  var newMouse = new Object();
  newMouse.x = null;
  newMouse.y = null;
  if (ev.pageX || ev.pageY) {
    newMouse.x = ev.pageX;
    newMouse.y = ev.pageY;
  } else {
    newMouse.x = ev.clientX + document.body.scrollLeft - document.body.clientLeft;
    newMouse.y = ev.clientY + document.body.scrollTop - document.body.clientTop;
  }
  return newMouse;
}

function windowResize( ) {
  winSize = {
    x: ( document.body.clientWidth ) ? document.body.clientWidth : window.innerWidth,
    y: ( document.body.clientHeight ) ? document.body.clientHeight : window.innerHeight
  }
}

function findPos(thisobj) {
  var obj = document.getElementById(thisobj);
  var posX = obj.offsetLeft;
  var posY = obj.offsetTop;
  while (obj.offsetParent) {
    posX += obj.offsetParent.offsetLeft;
    posY += obj.offsetParent.offsetTop;
    if (obj == document.getElementsByTagName('body')[0]) break; else obj = obj.offsetParent;
  }
  return [posX,posY];
}

// This function shows our tool-tips

function showTip( ) {
  var this_id = this.id;
  var tip = document.getElementById('tt_tooltip');
  if (tip) {
    var itip = document.getElementById(this_id);
    var newWidth = 300;
    var newInner = '';

/* All tooltips need to have a unique identifier; however, they can be used on more than one page. */

    switch (this_id) {
      case 'crshome' :
        newWidth = 260;
        newInner = '<b>Canadian Restaurant Supply Ltd.</b><br />Restaurant Supplier in Kelowna, BC, Canada';
        break;
      case 'advanced' :
        newWidth = 250;
        newInner = 'Clicking on this link will take you to<br />our online store&#39;s Advanced Search page.';
        break;
      case 'helplink' :
        newWidth = 180;
        newInner = 'Click this link if you<br />require additional information<br />about using this website.';
        break;
      case 'create' :
      case 'create2' :
        newWidth = 140;
        newInner = 'Click here to register<br />a new user account.';
        break;
      case 'logout' :
        newWidth = 140;
        newInner = 'Click here to Logout.';
        break;
      case 'settings' :
        newWidth = 200;
        newInner = 'Click here to view or change<br />your user settings and information.';
        break;
      case 'forgot' :
        newWidth = 200;
        newInner = 'Click here if you\'ve forgotten<br />your password or username.';
        break;
      case 'reset' :
        newWidth = 200;
        newInner = 'Click here to reset your password.';
        break;
      case 'senduser' :
        newWidth = 200;
        newInner = 'Click here to have your<br />Username send to your email address.';
        break;
      case 'viewcart' :
        newWidth = 160;
        newInner = 'You can view items in<br />your shopping cart here.';
        break;
      case 'login' :
        newWidth = 200;
        newInner = 'Click here to login, or<br />to register a new account, or<br />access your existing CRS account.';
        break;
      case 'login2' :
        newWidth = 160;
        newInner = 'Click here to Login.';
        break;
      case 'thispage' :
        newWidth = 160;
        newInner = 'This is the current page.';
        break;
      case 'homepage' :
        newWidth = 120;
        newInner = '<b>Home</b><br />This will take you<br />to our main page.';
        break;
      case 'productspage' :
        newWidth = 200;
        newInner = '<b>Products</b><br />Information about the types of<br />products we carry and our preferred<br />suppliers are on this page.';
        break;
      case 'clearancepage' :
        newWidth = 190;
        newInner = '<b>Clearance</b><br />This page has some current<br />clearance items on special.';
        break;
      case 'panddpage' :
        newWidth = 220;
        newInner = '<b>Planning &amp; Design</b><br />Go to this page to learn<br />about our professional commercial<br />kitchen design services.';
        break;
      case 'aboutpage' :
        newWidth = 210;
        newInner = '<b>About Us</b><br />A brief history of<br />Canadian Restaurant Supply Ltd.';
        break;
      case 'contactpage' :
        newWidth = 190;
        newInner = '<b>Contact Us</b><br />This page has information<br/ >on how to find us or contact<br />our various departments.';
        break;
      case 'careerspage' :
        newWidth = 190;
        newInner = '<b>Career Opportunities</b><br />Find out about our exciting<br />career opportunities with CRS.';
        break;
      case 'userconfirm' :
        newWidth = 170;
        newInner = 'Click here to confirm<br />the changes to your settings.';
        break;
      case 'userreset' :
        newWidth = 160;
        newInner = 'Click here to undo changes.';
        break;
      case 'usercancel' :
        newWidth = 160;
        newInner = 'Click here to cancel<br />changing your settings.';
        break;
      case 'captcha' :
        newWidth = 190;
        newInner = 'Type the letters you see in the<br />image into the box to the right.<br />This is for security purposes.<br />Click for new image.';
        break;
      case 'tableware' :
        newWidth = 200;
        newInner = '<b>Tableware &amp; Service Items</b><br />Click to browse our Online Store.';
        break;
      case 'smallware' :
        newWidth = 200;
        newInner = '<b>Kitchen Smallwares</b><br />Click to browse our Online Store.';
        break;
      case 'glassware' :
        newWidth = 200;
        newInner = '<b>Glasses &amp; Glass Dishes</b><br />Click to browse our Online Store.';
        break;
      case 'equipment' :
        newWidth = 200;
        newInner = '<b>Equipment &amp; Appliances</b><br />Click to browse our Online Store.';
        break;
      case 'barsvc' :
        newWidth = 200;
        newInner = '<b>Bar Service</b><br />Click to browse our Online Store.';
        break;
      case 'bgemenu' :
        newWidth = 200;
        newInner = '<b>Big Green Egg&reg</b><br />Click to browse our Online Store.';
        break;
      case 'flatware' :
        newWidth = 200;
        newInner = '<b>Flatware</b><br />Click to browse our Online Store.';
        break;
      case 'gourmet' :
        newWidth = 230;
        newInner = '<b>Gourmet Outfitters</b><br />Click to go to our retail store&#39;s website.';
        break;
      case 'redflyer' :
        newWidth = 160;
        newInner = 'View our current flyers<br />Online Now!';
        break;
      case 'biggreenegg' :
        newWidth = 200;
        newInner = '<b>Big Green Egg®</b><br />Click here for more information on<br />the &ldquo;World&#39;s Best Smoker &amp; Grill&rdquo;.';
        break;
      case 'request' :
        newWidth = 200;
        newInner = 'Click here to request a service<br />call from our sister company,<br /><b>CRS Refrigeration &amp; HVAC';
        break;
      case 'menucovers' :
        newWidth = 180;
        newInner = 'Click here for an online<br />quote of new Menu Covers<br />by The Menu Experts.';
        break;
      case 'browsestart' :
        newWidth = 210;
        newInner = 'Click here to start browsing our<br />Online Store at the main category.';
        break;
      case 'browse0' :
        newWidth = 180;
        newInner = 'Click here to browse our<br />Online Store by category.';
        break;
      case 'orderform' :
        newWidth = 220;
        newInner = 'Right-Click on this link and select<br />&quot;Save Target As...&quot; to download our<br />easy to use Fax Order Form.<br />You will need Adobe Acrobat Reader.';
        break;
      case 'proud' :
        newWidth = 190;
        newInner = 'Canadian Restaurant Supply Ltd.<br />is a wholly Canadian owned<br /> and operated company in B.C.';
        break;
      case 'clearance' :
        newWidth = 200;
        newInner = '<b>Clearance Specials</b><br />Click to view our some of<br />our featured Clearance Specials.';
        break;
      case 'emailpd' :
        newWidth = 190;
        newInner = 'Click this button to send<br />an email to the P&D department.';
        break;
      case 'e-mike' :
        newWidth = 150;
        newInner = 'Click to send an email<br />to Mike Otter.';
        break;
      case 'e-andy' :
        newWidth = 160;
        newInner = 'Click to send an email<br />to Andy Stefansson.';
        break;
      case 'e-brenda' :
        newWidth = 150;
        newInner = 'Click to send an email<br />to Brenda Otter.';
        break;
      case 'e-howard' :
        newWidth = 150;
        newInner = 'Click to send an email<br />to Howard Sisson.';
        break;
      case 'e-sandra' :
        newWidth = 150;
        newInner = 'Click to send an email<br />to Sandra Matlock.';
        break;
      case 'e-lisa' :
        newWidth = 150;
        newInner = 'Click to send an email<br />to Lisa Basham.';
        break;
      case 'e-dave' :
        newWidth = 150;
        newInner = 'Click to send an email<br />to Dave Mazza.';
        break;
      case 'e-beverley' :
        newWidth = 150;
        newInner = 'Click to send an email<br />to Beverley Wilson.';
        break;
      case 'e-kassy' :
        newWidth = 150;
        newInner = 'Click to send an email<br />to Kassy Otter.';
        break;
      case 'e-yvonne' :
        newWidth = 150;
        newInner = 'Click to send an email<br />to Yvonne Raulick.';
        break;
      case 'e-ron' :
        newWidth = 150;
        newInner = 'Click to send an email<br />to Ron Otter.';
        break;
      case 'e-kevinw' :
        newWidth = 150;
        newInner = 'Click to send an emai<br />to Kevin Wall.';
        break;
      case 'e-cliff' :
        newWidth = 150;
        newInner = 'Click to send an email<br />to Cliff Pierce.';
        break;
      case 'email1a' :
      case 'email1b' :
      case 'email1c' :
			case 'email1d' :
				newWidth = 200;
				newInner = 'Click to send Mike Otter an<br />email about this career opportunity.';
				break;
			case 'email2a' :
			case 'email2b' :
			case 'email2c' :
			case 'email2d' :
        newWidth = 200;
        newInner = 'Click to send Mike Otter an<br />email about this career opportunity.';
        break;
      case 'signup' :
        newWidth = 220;
        newInner = 'Click this button to sign-up<br />to receive updates, special notices,<br />and our Electronic Newsletter.';
        break;
      case 'bgeinquiry' :
        newWidth = 220;
        newInner = 'Click to send an email inquiry<br />about the <strong>Big Green Egg&reg;</strong>, including<br />up-coming product demonstrations.';
        break;
      case 'clickmap' :
        newWidth = 180;
        newInner = 'Click on this map to view<br />a larger version for our location.';
        break;
      case 'closemap' :
        newWidth = 150;
        newInner = 'Click on the map to<br />close this window.';
        break;
      case 'contactemail' :
        newWidth = 150;
        newInner = 'Click on the laptop to open<br />our email contact form.';
        break;
      case 'ourstaff' :
        newWidth = 180;
        newInner = 'Click to view Our Staff page.';
        break;
			case 'clearancefront' :
				newWidth = 180;
				newInner = 'Click to view Page 1<br />of our Clearance Flyer.';
				break;
			case 'clearanceback' :
			  newWidth = 180;
				newInner = 'Click to view Page 2<br />of our Clearance Flyer.';
				break;
			case 'clearancemidfront' :
			  newWidth = 180;
				newInner = 'Click to view Page 3<br />of our Clearance Flyer.';
				break;
			case 'clearancemidback' :
			  newWidth = 180;
				newInner = 'Click to view Page 4<br />of our Clearance Flyer.';
				break;
			case 'master-biltflyer':
				newWidth = 160;
				newInner = 'Click to view our<br/ >Master-Bilt Flyer.';
				break;
			case 'vollrathflyer':
			  newWidth = 160;
				newInner = 'Click to view our<br />Vollrath Flyer.';
				break;
			case 'pubfront':
				newWidth = 150;
				newInner = 'Click to view the front<br />of our CRS Pub Flyer 2010.';
				break;
			case 'pubback':
				newWidth = 150;
				newInner = 'Click to view the back<br />of our CRS Pub Flyer 2010.';
				break;
			case 'garlandfront':
				newWidth = 190;
				newInner = 'Click to view the Front of<br />our Garland Truckload Sale Flyer.';
				break;
			case 'garlandback':
				newWidth = 190;
				newInner = 'Click to view the Back of<br />our Garland Truckload Sale Flyer.';
				break;
			case 'grosfillexflyer':
				newWidth = 180;
				newInner = 'Click to view our<br />Grosfillex Spring 2010 Flyer.';
				break;
			case 'scotsmanflyer':
				newWidth = 180;
				newInner = 'Click to view our<br />Scotsman Ice Machine Flyer.';
				break;
			case 'emailattach':
				newWidth = 270;
				newInner = 'Click &quot;Browse&quot; to attach a file to this email.<br />Allowed file types are JPG, GIF, and BMP<br />images, or  TXT, XLS, DOC, and PDF documents.';
				break;
			case 'facebook':
				newWidth = 160;
				newInner = 'Find us on Facebook!';
				break;
      default:
        newWidth = 200;
        newInner = 'Help on this topic is<br />not available at this time.';
    }
		if (this_id.substr(0,12) == 'redflyerpage') {
			newWidth = 180;
			newInner = 'Click to view page ' + this_id.substr(12) + '<br />of our Flyer.';
		}

    tip.style.width = newWidth + 20 + 'px';
    tip.width = newWidth + 20 + 'px';

    var tip1 = document.getElementById('tt_tl');
    tip1.style.backgroundImage = 'url(/images/tooltips/topleft.gif)';
    tip1.style.width = 10 + 'px';
    tip1.style.height = 10 + 'px';
    tip1 = document.getElementById('tt_tm');
    tip1.style.backgroundImage = 'url(/images/tooltips/topmiddle.gif)';
    tip1.style.backgroundRepeat = 'repeat-x';
    tip1.style.height = 10 + 'px';
    tip1.style.width = newWidth + 'px';
    tip1 = document.getElementById('tt_tr');
    tip1.style.backgroundImage = 'url(/images/tooltips/topright.gif)';
    tip1.style.width = 10 + 'px';
    tip1.style.height = 10 + 'px';

    tip1 = document.getElementById('tt_l');
    tip1.style.backgroundImage = 'url(/images/tooltips/left.gif)';
    tip1.style.backgroundRepeat = 'repeat-y';
    tip1.style.width = 10 + 'px';
    tip1 = document.getElementById('tt_main');
    tip1.style.width = newWidth + 'px';
    tip1.innerHTML = newInner;
    tip1 = document.getElementById('tt_r');
    tip1.style.backgroundImage = 'url(/images/tooltips/right.gif)';
    tip1.style.backgroundRepeat = 'repeat-y';
    tip1.style.width = 10 + 'px';

    tip1 = document.getElementById('tt_bl');
    tip1.style.backgroundImage = 'url(/images/tooltips/bottomleft.gif)';
    tip1.style.width = 10 + 'px';
    tip1.style.height = 10 + 'px';
    tip1 = document.getElementById('tt_bm');
    tip1.style.backgroundImage = 'url(/images/tooltips/bottommiddle.gif)';
    tip1.style.backgroundRepeat = 'repeat-x';
    tip1.style.height = 10 + 'px';
    tip1.style.width = newWidth + 'px';
    tip1 = document.getElementById('tt_br');
    tip1.style.backgroundImage = 'url(/images/tooltips/bottomright.gif)';
    tip1.style.width = 10 + 'px';
    tip1.style.height = 10 + 'px';

    var iPos = findPos(this_id); // get the positon of the element that the tooltip is being displayed for
    var newLeft = iPos[0] + itip.offsetWidth;
    var newTop = iPos[1] + itip.offsetHeight;

// this line needs to be above positioning to allow for proper element.clientHeight values.
    tip.style.display = 'block';

// make sure the tooltip won't be above the available screen
    if (newTop <= 0) newTop = 10;
// make sure the tooltip won't be below the available screen or scroll
    if (newTop + tip.clientHeight - getScrollY() > browserHeight() - 1) newTop = browserHeight() - 1 - tip.clientHeight + getScrollY();
// make sure the tooltip won't be right of the available screen
    if (newLeft <= 0) newLeft = 10;
// make sure the tooltip won't be left of the available screen (do I need to add srolling?)
    if ((newLeft + (newWidth + 20) + 1) >= winSize.x - 1) newLeft = winSize.x - newWidth - 21;
//    if ((newLeft + (newWidth + 20) + 10) >= winSize.x - 1) newLeft = winSize.x - newWidth - 30;

// move the tooltip div to where we want it
    tip.style.top = newTop + 'px';
    tip.style.left = newLeft + 'px';
// make the tooltip div visible now
    tip.style.visibility = 'visible';
  }
}

// This function moves the tool-tips when our mouse moves
/* NOT USED
function moveTip( ) {
  var tip = document.getElementById('tt_' + this.id);
  
  var newTop = mousePos.y + 10;
  var newLeft = mousePos.x + 10;
  if (newTop < 0) newTop = mousePos.y + 20;
  if (newLeft < 0) newLeft = 0;
  if (mousePos.x + 10 >= winSize.x - 1) newLeft = winSize.x - tip.clientWidth - 2;
  tip.style.top = newTop;
  tip.style.left = newLeft;
}
*/
function hideTip( ) {
  var tip = document.getElementById('tt_tooltip');
  if (tip) {
    tip.style.display = 'none';
    tip.style.visibility = 'hidden';
  }
}

// functions to use with Email Form

function prepEForm() {
  var eform = document.getElementById('EmailForm');
  if (eform) {
    eform.style.top = '30px';
    eform.style.left = '30px';
    eform.style.width = '601px';
    eform.style.height = '437px';
  } else return false;
  var formborder = document.getElementById('formborder');
  if (formborder) {
    formborder.style.top = '30px';
    formborder.style.left = '30px';
    formborder.style.width = '601px';
    formborder.style.height = '437px';
  } else return false;

  var eform1 = document.getElementById('ef_tl');
  if (eform1) {
    eform1.style.backgroundImage = 'url(/images/border/topleft.gif)';
    eform1.style.width = '20px';
    eform1.style.height = '20px';
  }
  eform1 = document.getElementById('ef_tm');
  if (eform1) {
    eform1.style.backgroundImage = 'url(/images/border/top.gif)';
    eform1.style.backgroundRepeat = 'repeat-x';
    eform1.style.width = '561px';
    eform1.style.height = '20px';
  }
  eform1 = document.getElementById('ef_tr');
  if (eform1) {
    eform1.style.backgroundImage = 'url(/images/border/topright.gif)';
    eform1.style.width = '20px';
    eform1.style.height = '20px';
  }

  eform1 = document.getElementById('ef_l');
  if (eform1) {
    eform1.style.backgroundImage = 'url(/images/border/left.gif)';
    eform1.style.backgroundRepeat = 'repeat-y';
    eform1.width = '20px';
    eform1.height = '397px';
  }

  eform1 = document.getElementById('ef_r');
  if (eform1) {
    eform1.style.backgroundImage = 'url(/images/border/right.gif)';
    eform1.style.backgroundRepeat = 'repeat-y';
    eform1.style.width = '20px';
    eform1.style.height = '397px';
  }

  eform1 = document.getElementById('ef_bl');
  if (eform1) {
    eform1.style.backgroundImage = 'url(/images/border/bottomleft.gif)';
    eform1.style.width = '20px';
    eform1.style.height = '20px';
  }
  eform1 = document.getElementById('ef_bm');
  if (eform1) {
    eform1.style.backgroundImage = 'url(/images/border/bottom.gif)';
    eform1.style.backgroundRepeat = 'repeat-x';
    eform1.style.width = '561px';
    eform1.style.height = '20px';
  }
  eform1 = document.getElementById('ef_br');
  if (eform1) {
    eform1.style.backgroundImage = 'url(/images/border/bottomright.gif)';
    eform1.style.width = '20px';
    eform1.style.height = '20px';
  }

  eform1 = document.getElementById('ef_main');
  if (eform1) {
    eform1.style.width = '561px';
    eform1.style.height = '397px';
    eform1.style.textAlign = 'center';
  }
  return true;
}

function efvalidate() {
  var thisform = document.emailform;
  if (thisform.captcha.value == '') {
    alert('Please enter the Captcha code\nas displayed in the image.\nThis is for security and Anti-Robot purposes.');
    return false;
  }
  if (thisform.efname.value == '') {
    alert('Please enter your name.');
    return false;
  }
  if (thisform.efemail1.value == '') {
    alert('Please enter a valid email address.');
    return false;
  } else {
    var at = "@";
    var dot = ".";
    var str = thisform.efemail1.value;
    thisform.efemail1.value = Trim(str);
    var lat = str.indexOf(at);
    var lstr = str.length;
    var ldot = str.indexOf(dot);
    var invalidchars = new RegExp("[~`#$%^&*()=+,<>;:{}?\/\"'|]");
    var goodemail = new RegExp("^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$");
    var ipaddress = new RegExp("@*[0-9\.]{1,255}(?=[.])");
    if ((str.indexOf(at) == -1) || (str.indexOf(at) == 0) || (str.indexOf(at) == lstr)) {
      alert('Email Address Error: 0xE0000001');
      thisform.efemail2.value = '';
      thisform.efemail1.value = '';
      thisform.efemail1.focus();
      return false;
    }
    if ((str.indexOf(dot) == -1) || (str.indexOf(dot) == 0) || (str.indexOf(dot) == lstr)) {
      alert('Email Address Error: 0xE0000002');
      thisform.efemail2.value = '';
      thisform.efemail1.value = '';
      thisform.efemail1.focus();
      return false;
    }
    if ((str.indexOf(at,(lat + 1)) != -1) || (str.substring(lat-1,lat) == dot) || (str.substring(lat+1,lat+2) == dot)) {
      alert('Email Address Error: 0xE0000003');
      thisform.efemail2.value = '';
      thisform.efemail1.value = '';
      thisform.efemail1.focus();
      return false;
    }
    if ((str.indexOf(dot,(lat + 2)) == -1) || (str.indexOf(" ") != -1) || (str.lastIndexOf(dot) <= str.indexOf(at))) {
      alert('Email Address Error: 0xE0000004');
      thisform.efemail2.value = '';
      thisform.efemail1.value = '';
      thisform.efemail1.focus();
      return false;
    }
    if ((str.indexOf(at) > 64) || (str.substring(lat+1).length > 255)) {
      alert('Email Address Error: 0xE0000005');
      thisform.efemail2.value = '';
      thisform.efemail1.value = '';
      thisform.efemail1.focus();
      return false;
    }
    if (invalidchars.test(str)) {
      alert('Email Address Error: 0xE0000006');
      thisform.efemail2.value = '';
      thisform.efemail1.value = '';
      thisform.efemail1.focus();
      return false;
    }
    if (!goodemail.test(str)) {
      alert('Email Address Error: 0xE0000007');
      thisform.efemail2.value = '';
      thisform.efemail1.value = '';
      thisform.efemail1.focus();
      return false;
    }
    if (ipaddress.test(str)) {
      alert('Email Address Error: 0xE0000008');
      thisform.efemail2.value = '';
      thisform.efemail1.value = '';
      thisform.efemail1.focus();
      return false;
    }
    if (thisform.efemail1.value != thisform.efemail2.value) {
      alert('Email Addresses do not match.');
      thisform.efemail1.value = '';
      thisform.efemail2.value = '';
      thisform.efemail1.focus();
      return false;
    }
  }
  if (thisform.efcmd.value == 'quote') {
    if (thisform.efaddr1.value + thisform.efaddr2.value == '') {
		 alert('Please specify a street address.');
		 return false;
	 }
	 if (thisform.efcity.value == '') {
		 alert('Please specify a city.');
		 return false;
	 }
    if (thisform.efpost.value == '') {
		 alert('Please enter a Postal Code.');
		 return false;
	 }
  } else {
    if (thisform.efcomment.value == '') {
      alert('Message box is empty.');
      return false;
    }
  }
  thisform.submit();
  return true;
}

function efclear() {
  document.emailform.efname.value = '';
  document.emailform.efphone.value = '';
  document.emailform.effax.value = '';
  document.emailform.efemail1.value = '';
  document.emailform.efemail2.value = '';
  document.emailform.efcomment.value = '';
  return true;
}

// This function allows us to reference elements using their class attributes

document.getElementsByClassName = function(clsName) {
  var retVal = new Array();
  var elements = document.getElementsByTagName("*");
  for (var ycntr = 0; ycntr < elements.length; ycntr++) {
    if (elements[ycntr].className.indexOf(" ") >= 0) {
      var classes = elements[i].className.split(" ");
      for (var xcntr = 0; xcntr < classes.length; xcntr++) {
        if(classes[xcntr] == clsName) retVal.push(elements[ycntr]);
      }
    } else if (elements[ycntr].className == clsName) retVal.push(elements[ycntr]);
  }
  return retVal;
}

// This is what runs when the page loads to set everything up

window.onload = function() {
  sniffBrowsers();
  document.onmousemove = mouseMove;
  window.onresize = windowResize;
  var tooltips = document.getElementsByClassName('crstt');
  for (var xcntr = 0; xcntr < tooltips.length; xcntr++) {
    tooltips[xcntr].onmouseover = showTip;
    tooltips[xcntr].onmouseout = hideTip;
  }
  windowResize( );
  preloadimg();
	if (onWhichPage() != 'emailform') {
//		prepEForm();
		prepMap();
		initNotices();
	}
}

// This function closes the current open window.

function CloseOpenWindow() {
  var objWindow = window.open(location.href, "_self");
  objWindow.close();
}

// This function gives us the height of an element

function getElementHeight(Elem) {
  var xPos = -1;
  var elem;
  if (ns4) {
    elem = getObjNN4(document, Elem);
    return elem.clip.height;
  } else {
    if (document.getElementById) elem = document.getElementById(Elem);
    else if (document.all) elem = document.all[Elem];
    if (elem) {
      if (op5) {
        xPos = elem.style.pixelHeight;
      } else {
        xPos = elem.offsetHeight;
      }
    }
    return xPos;
  } 
}

function getObjNN4(obj,name) {
  var x = obj.layers;
  var foundLayer;
  for (var i=0; i < x.length; i++) {
    if (x[i].id == name) foundLayer = x[i];
    else if (x[i].layers.length) var tmp = getObjNN4(x[i], name);
    if (tmp) foundLayer = tmp;
  }
  return foundLayer;
}

function sniffBrowsers() {
  ns4 = document.layers;
  op5 = (navigator.userAgent.indexOf("Opera 5") != -1) || (navigator.userAgent.indexOf("Opera/5") != -1);
  op6 = (navigator.userAgent.indexOf("Opera 6") != -1) || (navigator.userAgent.indexOf("Opera/6") != -1);
  var agt = navigator.userAgent.toLowerCase();
  mac = (agt.indexOf("mac") != -1);
  ie = (agt.indexOf("msie") != -1); 
  mac_ie = mac && ie;
}

function browserHeight() {
  var myHeight = 0;
  if (typeof(window.innerHeight) == 'number') {
    //Non-IE
    myHeight = window.innerHeight;
  } else if(document.documentElement && document.documentElement.clientHeight) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if (document.body && document.body.clientHeight) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}

function browserWidth() {
  var myWidth = 0;
  if (typeof(window.innerWidth) == 'number') {
    //Non-IE
    myWidth = window.innerWidth;
  } else if (document.documentElement && document.documentElement.clientWidth) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && document.body.clientWidth) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
  return myWidth;
}

// This functions tells us how far left the page has been scrolled (horizontal)

function getScrollX() {
  var scrOfX = 0;
  if (typeof(window.pageXOffset) == 'number') {
    //Netscape compliant
    scrOfX = window.pageXOffset;
  } else if (document.body && document.body.scrollLeft) {
    //DOM compliant
    scrOfX = document.body.scrollLeft;
  } else if (document.documentElement && document.documentElement.scrollLeft) {
    //IE6 standards compliant mode
    scrOfX = document.documentElement.scrollLeft;
  }
  return scrOfX;
}

// This functions tells us how far down the page has been scrolled (vertical)

function getScrollY() {
  var scrOfY = 0;
  if (typeof(window.pageYOffset) == 'number') {
    //Netscape compliant
    scrOfY = window.pageYOffset;
  } else if (document.body && document.body.scrollTop) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
  } else if (document.documentElement && document.documentElement.scrollTop) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
  }
  return scrOfY;
}

// The following function performs rollover image swapping.
//  [which_id] is the id of the image (or base file name of image if [third_arg] is real id)
//  [which_state] = a|b|c
//  [third_arg] is either the file extension (with '.') or the real id of the image (without '.')
//
//  if [which_state] is the actual full file name with folders (includes '/'),
//  then [which_id] is the real id and no other arguments are required.

function swapimg(which_id,which_state,third_arg) {
  var newImage = '';
  var which_id_real;
  var which_type;
  var endResult = '';
  if (which_state.indexOf('/') != -1) {
    endResult = which_state;
    which_id_real = which_id;
  } else {
    newImage = '/images/menu/';
    if (arguments.length < 3) {
      which_id_real = which_id;
      which_type = '.jpg';
    } else {
      if (third_arg.substr(0,1) == '.') {
        which_type = third_arg;
        which_id_real = which_id;
      } else {
        which_type = '.jpg';
        which_id_real = third_arg;
      }
    }
    endResult = newImage + which_id + '_' + which_state + which_type;
  }
  var elem = document.getElementById(which_id_real);
  if (elem) elem.src = endResult;
  return true;
}

// New function to replace the one just above.
//
// assumes "filepath/filename_?.fileext";
// where ? = a or b (swapped).
function SwapImg(this_obj) {
	var image_id = this_obj.id;
	var image_src = this_obj.src;
	var image_folder = image_src.substring(0,image_src.lastIndexOf('/')); // SUBSTRING !!!
	var image_name = image_src.substring(image_src.lastIndexOf('/') + 1,image_src.lastIndexOf('_')); // SUBSTRING !!!
	var image_state = image_src.substr(image_src.lastIndexOf('_') + 1,1); // SUBSTR !!!
	var image_type = image_src.substr(image_src.lastIndexOf('.') + 1); // SUBSTR !!!
	this_obj.src = image_folder + '/' + image_name + '_' + (image_state == 'a' ? 'b' : 'a') + '.' + image_type;
	return true;
}

function showDiv(which_div) {
  var elem = document.getElementById(which_div);
  if (elem.style.visibility == 'hidden') {
    elem.style.visibility = 'visible';
    elem.style.display = 'inline';
  } else {
    elem.style.visibility = 'hidden';
    elem.style.display = 'none';
  }
  return true;
}

function onWhichPage() {
  var which_page = document.URL;
  var ycntr = 0;
  which_page = which_page.substr(which_page.lastIndexOf('/')+1);
  which_page = which_page.substr(0,which_page.indexOf('.'));
  return which_page;
}

function preloadimg() {
  if (!document.crsimages) document.crsimages = new Array();
  var images = new Array('equipment_b.jpg','equipment_c.jpg','tableware_b.jpg','tableware_c.jpg','glassware_b.jpg','glassware_c.jpg','smallware_b.jpg','smallware_c.jpg','tooltips/topleft.gif','tooltips/topmiddle.gif','tooltips/topright.gif','tooltips/left.gif','tooltips/right.gif','tooltips/bottomleft.gif','tooltips/bottommiddle.gif','tooltips/bottomright.gif','menu/bge_b.gif','menu/newsletter_b.jpg','newsletter_c.jpg');
  var which_page = onWhichPage();

  switch (which_page) {
    case "bge" :
    case "contact" :
    case "p_and_d" :
    case "ourstaff" :
    case "careers" :
      images.push('menu/email_b.jpg','menu/email_c.jpg','menu/efsend_b.jpg','menu/efsend_c.jpg','menu/efclear_b.jpg','menu/efclear_c.jpg','menu/efcancel_b.jpg','border/topleft.gif','border/top.gif','border/topright.gif','border/left.gif','border/right.gif','border/bottomleft.gif','border/bottom.gif','border/bottomright.gif');
      break;
    case "signup" :
      images.push('menu/signup_b.jpg','menu/signup_c.jpg');
      break;
    case "about" :
      images.push('menu/staff_b.jpg','menu/staff_c.jpg');
      break;
    default :
  }

  for(var xcntr in images) {
    ycntr = document.crsimages.push(new Image);
    document.crsimages[ycntr-1].src = '/images/' + images[xcntr];
  }
  return true;
}

function prepMap() {
  var map1 = document.getElementById('LocationMap');
  if (map1) {
    map1.top = '30px';
    map1.left = '30px';
    map1.width = '601px';
    map1.height = '411px';
  } else return false;

  var map1 = document.getElementById('lm_tl');
  if (map1) {
    map1.style.backgroundImage = 'url(/images/border/topleft.gif)';
    map1.style.width = '20px';
    map1.style.height = '20px';
  }
  map1 = document.getElementById('lm_tm');
  if (map1) {
    map1.style.backgroundImage = 'url(/images/border/top.gif)';
    map1.style.backgroundRepeat = 'repeat-x';
    map1.style.width = '561px';
    map1.style.height = '20px';
  }
  map1 = document.getElementById('lm_tr');
  if (map1) {
    map1.style.backgroundImage = 'url(/images/border/topright.gif)';
    map1.style.width = '20px';
    map1.style.height = '20px';
  }

  map1 = document.getElementById('lm_l');
  if (map1) {
    map1.style.backgroundImage = 'url(/images/border/left.gif)';
    map1.style.backgroundRepeat = 'repeat-y';
    map1.width = '20px';
    map1.height = '371px';
  }

  map1 = document.getElementById('lm_r');
  if (map1) {
    map1.style.backgroundImage = 'url(/images/border/right.gif)';
    map1.style.backgroundRepeat = 'repeat-y';
    map1.style.width = '20px';
    map1.style.height = '371px';
  }

  map1 = document.getElementById('lm_bl');
  if (map1) {
    map1.style.backgroundImage = 'url(/images/border/bottomleft.gif)';
    map1.style.width = '20px';
    map1.style.height = '20px';
  }
  map1 = document.getElementById('lm_bm');
  if (map1) {
    map1.style.backgroundImage = 'url(/images/border/bottom.gif)';
    map1.style.backgroundRepeat = 'repeat-x';
    map1.style.width = '561px';
    map1.style.height = '20px';
  }
  map1 = document.getElementById('lm_br');
  if (map1) {
    map1.style.backgroundImage = 'url(/images/border/bottomright.gif)';
    map1.style.width = '20px';
    map1.style.height = '20px';
  }

  map1 = document.getElementById('lm_main');
  if (map1) {
    map1.style.width = '561px';
    map1.style.height = '371px';
  }
}

function showMap() {
  var map1 = document.getElementById('LocationMap');
  if (map1) {
    map1.style.display = 'block';
    map1.style.position = 'fixed';
    map1.style.top = '50px';
    map1.style.left = '50px';
    map1.style.width = '601px';
    map1.style.height = '365px';
    map1.style.visibility = 'visible';
  }
}

function hideMap() {
  var map1 = document.getElementById('LocationMap');
  if (map1) {
    map1.style.display = 'none';
    map1.style.visibility = 'hidden';
  }
}


// The following functions are for general functionality of this website.

function setPointer(whichthis,statusMsg) {
  whichthis.style.cursor = 'pointer';
  if (statusMsg) window.status = statusMsg;
  return true;
}

function clearPointer(whichthis) {
  whichthis.style.cursor = 'arrow';
  window.status = '';
  return true;
}

function lTrim(sString) {
  while (sString.substring(0,1) == ' ') {
    sString = sString.substring(1, sString.length);
  }
  return sString;
}

function rTrim(sString) {
  while (sString.substring(sString.length-1, sString.length) == ' ') {
    sString = sString.substring(0,sString.length-1);
  }
  return sString;
}

function Trim(sString) {
  return rTrim(lTrim(sString));
}

// The following functions are used for the Special Notices window

function initNotices() {
	if (document.getElementById('noticeImage') && document.getElementById('noticeLink')) {
		if (!noticesOn) {
			// The following section sets up the Special Notices window's variables and timer functions
			// specialNotices is an array of the notices [filename, link, alt text].
			specialNotices = new Array(['crs10th.jpg','','Celebrating our 10th Anniversary!'],['flyer003.jpg','http://www.crs-online.ca/flyers/page.php?flyer=clearance','CRS Clearance Flyer 2010 Available Online Now!'],['signup001.gif','http://www.crs-online.ca/signup.php','Sign Up to receive Specials in your email!']);
			// preload images
			var aImages = new Array();
			for (xcntr = 0; xcntr < specialNotices.length; xcntr++ ) {
				aImages[xcntr] = new Image();
				aImages[xcntr].src = specialFolder + specialNotices[xcntr][0];
			}
			whichNotice = 0;
			// setup timer here.
			specialTimer = window.setInterval(changeNotice,specialDelay);
			noticesOn = true;
		}
	}
	return true;
}

function changeNotice() {
	if (noticesOn) {
		var imageId = document.getElementById('noticeImage');
		var linkId = document.getElementById('noticeLink');
		whichNotice++;
		if (whichNotice >= specialNotices.length) whichNotice = 0;
		if (linkId && imageId) {
			if (specialNotices[whichNotice][1] == '') {
				linkId.onclick = function(){return false;};
				linkId.href = '';
			} else {
				linkId.href = specialNotices[whichNotice][1];
				linkId.onclick = function(){return true;};
			}
			imageId.src = specialFolder + specialNotices[whichNotice][0];
			imageId.alt = specialNotices[whichNotice][2];
		} else {
			window.clearInterval(specialTimer);
			specialNotices = [];
			noticesOn = false;
		}
	}
	return true;
}

function uninitNotices() {
	if (noticesOn) {
		// kill timer
		window.clearInterval(specialTimer);
		// release array
		specialNotices = [];
		// close window
		noticesOn = false;
	}
	return true;
}

// The following functions are used in the CRS Online Store.

function makeCompany() {
  if (document.getElementsByName('new_user[company]')[0].value == '' || document.getElementsByName('new_user[company]')[0].value == document.getElementsByName('new_user[firstname]')[0].value + ' ' || document.getElementsByName('new_user[company]')[0].value == ' ' + document.getElementsByName('new_user[lastname]')[0].value) document.getElementsByName('new_user[company]')[0].value = document.getElementsByName('new_user[firstname]')[0].value + ' ' + document.getElementsByName('new_user[lastname]')[0].value;
  return true;
}

