﻿var pageHeight;

// general
function addEvent(obj,evType,fn){ 
	if (obj.addEventListener){obj.addEventListener(evType, fn, false); return true;}
 	else if(obj.attachEvent){var r=obj.attachEvent("on"+evType, fn);return r;}
 	else return false; 
}


function externalLinks() { 
	if (!document.getElementsByTagName) return; 
	var anchors = document.getElementsByTagName("a"); 
	for (var i=0; i<anchors.length; i++) { 
		var anchor = anchors[i]; 
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") anchor.target = "_blank"; 
	}
}
function genEmail(user,domain,suffix,text) {
	mailto = user+'@'+domain+'.'+suffix;
	text = (text) ? text : mailto;
	document.write('<a href="mailto:'+mailto+'">'+text+'</a>');
}
function innerHTML(obj,val){document.getElementById(obj).innerHTML=val;}
function loadURL(target,newwin){if(newwin)window.open(target,'');else document.location.href=target;}
function txtCt(obj,maxlimit){if(obj.value.length>maxlimit)obj.value=obj.value.substring(0,maxlimit);}
function showObj(obj){if(document.getElementById(obj))document.getElementById(obj).style.display='block';}
function hideObj(obj){if(document.getElementById(obj))document.getElementById(obj).style.display='none';}
function newWin(url,id,w,h,tb,st,sb,re) {
	var x=screen.availWidth/2-w/2;
	var y=screen.availHeight/2-h/2;
	var newWin = window.open(url,id,'width='+w+',height='+h+',left='+((x>0)?x:0)+',top='+((y>0)?y:0)+',toolbar='+(tb?tb:'no')+',status='+(st?st:'no')+',scrollbars='+(sb?sb:'no')+',resizable='+(re?re:'no')+'');
	newWin.focus();
}
function getQueryVariable(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) return pair[1];
	}
	return null;
}

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 var 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 eraseCookie(name){createCookie(name,"",-1);}

function fixPNG(obj) {
	if (browser.is_ie && !browser.is_ie7up && obj.src.indexOf('.png') != -1) {
		var imgID = (obj.id) ? "id='" + obj.id + "' " : "";
		var imgClass = (obj.className) ? "class='" + obj.className + "' " : "";
		var imgAlt = "alt='" + obj.alt.replace(/'/g, '&#39;') + "' ";
		var imgTitle = (obj.title) ?  "title='" + obj.title.replace(/'/g, '&#39;')  + "' " : "";
		var imgStyle = "display:inline-block;" + obj.style.cssText;
		var strNewHTML = "<span " + imgID + imgClass + imgTitle
		+ " style=\"" + "width:" + obj.width + "px; height:" + obj.height + "px;" + imgStyle + ";"
		+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
		+ "(src=\'" + obj.src + "\', sizingMethod='crop');\"></span>";
		obj.outerHTML = strNewHTML;
  }
}
function fixPNGimgs() {
	if(!document.getElementsByTagName)return; 
	var imgs=document.getElementsByTagName("img"); 
	for(var i=0; i<imgs.length; i++){var img=imgs[i];if(img.getAttribute("rel")=="png")fixPNG(img);}
}

addEvent(window, 'load', externalLinks);
addEvent(window, 'load', fixPNGimgs);

/**************************************************
Expands and/or Collapses the divs in the right 
content area on the Contact us page.
**************************************************/
function showHideContent(id)
  {
    var elem = document.getElementById(id);
    if (elem) 
    {
      // Collapse Current Div
      if (document.getElementById(id).style.display == 'block') 
      {
        document.getElementById(id + 'img').src = '../images/contact/arrow.gif';
        elem.style.display = 'none';
      } 
      // Expand New Div
      else
      {
        // collapse all expandable divs
        for (i=1;i<=5;i++)
        {
            document.getElementById('collapse' + i + 'img').src = '../images/contact/arrow.gif';
            document.getElementById('collapse' + i).style.display = 'none';
        }
        // expand the desired div
        document.getElementById(id + 'img').src = '../images/contact/arrow_expanded.gif';
        elem.style.display = 'block';
      }
    }
  } 
  
/**************************************************
Get the Y position of the supplied object
**************************************************/
function getY(object)
{
    var y = 0;
    while(object != null) 
    {
        y = y + object.offsetTop;
        object = object.offsetParent;
    }
    return y;
}

/**************************************************
Get the X position of the supplied object
**************************************************/
function getX(object)
{
    var x = 0;
    while(object != null) 
    {
        x = x + object.offsetLeft;
        object = object.offsetParent;
    }
    return x;
}

/**************************************************
This function finds the window height that is
currently visible.
**************************************************/
function getVisibleHeight()
{
    var height = 0;
        
    /* Internet Explorer */
    if(document.documentElement.clientHeight && document.documentElement) 
    {
        height = document.documentElement.clientHeight;
    } 
    
    /* Non Internet-Explorer Browsers - Safari, Firefox, Opera, Chrome */
    else if(typeof(window.innerHeight) == 'number') 
    {
        height = window.innerHeight;
    } 
    
    /* If no match occurs, set a value anyway (shouldn't happen , here as a precaution) */
    else {
        height = 2000;
    }
    
  return height;
}

/**************************************************
This function finds the window height based on the 
current scrolled position
**************************************************/
function findHeight()
{
    var fade = document.getElementById('fade');
    var visibleHeight = getVisibleHeight();
    
  	var body = document.body;
	var doc = document.documentElement;

	var elements = doc.offsetHeight? body: doc ;
	var contentHeight = Math.max(elements.scrollHeight, elements.offsetHeight) + 150;
	
	/* Compare the content height with the visible height, if the
	visible height is greater than the content height, use that value. */
	if (contentHeight < visibleHeight)
	{
	    fade.style.height = visibleHeight + 'px';
	}else {
	    fade.style.height = contentHeight + 'px';
	}
}

/**************************************************
This function toggles on the map modal popup on 
the Marketers/Distributors Results Page.
**************************************************/
var initialized = false;

function openMap(street, city, state, zip, link){

    /* Variables */
    var modal = document.getElementById('modal');
    var fade = document.getElementById('fade');
     
    /* Get Total Height Including Scroll */
    findHeight();
    
    /* Create an iframe if one already isn't present */
    if (initialized == false){
        modal.innerHTML ='<div style="position:relative"><iframe id="mapframe" src="map/blank.htm" class="map" frameborder="0" border="0" style="position:relative;"></iframe></div>';
        initialized = true;
    }
    var iframe = document.getElementById('mapframe');
           
    /* Turn on and Position container div */
    var g = getY(link) - 90;
    var h = getX(link) + 20;
         
    fade.style.display = 'block';
        
    modal.style.display = 'block';
    modal.style.top = g + 'px';
    modal.style.left = h + 'px';
        
    /* Populate the iframe with the current map */
    var url = 'map/map.htm?street=' + street + '&city=' + city + '&state=' + state + '&zip=' + zip;
    iframe.src=url;
   
}

/**************************************************
This function toggles off the map modal popup on 
the Marketers/Distributors Results Page.
**************************************************/
function closeMap(){
    var modal = document.getElementById('modal');
    var fade = document.getElementById('fade');
    modal.style.display = 'none';
    fade.style.display = 'none';
}

/**************************************************
This function toggles table row colors from gray to
white. This is used on the Brands/Products Pages for
the TDS sheet tables
**************************************************/

function setRowColors(id){ 
 if(document.getElementsByTagName){  
   var table = document.getElementById(id);   
   var rows = table.getElementsByTagName("tr");   
   for(i = 0; i < rows.length; i++){           
     if(i % 2 == 0){ 
       rows[i].className = "product_listing_tr_gray"; 
     }else{ 
       rows[i].className = "product_listing_tr_white"; 
     }       
   } 
 } 
}

/**************************************************
This function returns the number of TDS results found. 
This is used on the Brands/Products Pages for
the TDS sheet tables
**************************************************/

function getReportNumbers(numTables){ 
    var count = 0;
    for (i=1;i<=numTables;i++)
    {
        if(document.getElementsByTagName){  
            var tableID = 'Table' + i;
            var table = document.getElementById("Table" + i);   
            var rows = table.getElementsByTagName("tr");  
            count = count + rows.length;
        } 
    }
    document.getElementById("numbers").innerHTML = count; 
    
}

/**************************************************
This function helps a user figure out how to save
the page as a bookmark.
**************************************************/

function bookmark(){
	var url = window.document.location;
	var title = document.title;
	if ((BrowserDetect.browser == "Safari") && (BrowserDetect.OS == "Mac")){
		alert("Press APPLE-D to save a bookmark of this page in Safari.)");
	}else if ((BrowserDetect.browser == "Safari") && (BrowserDetect.OS == "Windows")){
		alert("Press CTRL-D to save a bookmark of this page in Safari.");
	}else if ((BrowserDetect.browser == "Explorer") && (BrowserDetect.OS == "Windows")) {
        window.external.AddFavorite(url,title);
    }else if ((BrowserDetect.browser == "Opera") && (BrowserDetect.OS == "Windows")){
		alert("Press CTRL-T to save a bookmark of this page in Opera.");
	}else if ((BrowserDetect.browser == "Opera") && (BrowserDetect.OS == "Mac")){
		alert("Press APPLE-T to save a bookmark of this page in Opera.");
	}else if ((BrowserDetect.browser == "Firefox") && (BrowserDetect.OS == "Mac")){
		alert("Press APPLE-D to save a bookmark of this page in Firefox.");
	}else if ((BrowserDetect.browser == "Firefox") && (BrowserDetect.OS == "Windows")){
		alert("Press CTRL-D to save a bookmark of this page in Firefox.");
	}else if ((BrowserDetect.browser == "Chrome") && (BrowserDetect.OS == "Windows")){
		alert("Press CTRL-D to save a bookmark of this page in Chrome.");
	}else{
		alert("Press CTRL-D to save a bookmark of this page.");
	}
  }

/**************************************************
This function clears the default text out of the 
about search box when clicked.
**************************************************/

function clearAboutSearchBox(myField)
{
    if (myField.value == "Enter search term" ){
        myField.value = "";
    }
}

    

  