﻿/* Macromedia Functions */
// Used by other MM_ scripts: MM_showHideLayers
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
// This function is used on the Greenview website!!
function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

/*
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_openBrWindow(theURL,winName,features) { //v2.0
  newwindow = window.open(theURL,winName,features);
  if (window.focus) {newwindow.focus()}
	return false;
}
*/

// Other
function isEmail(str) {
  // are regular expressions supported?
  var isSupp = 0;
  if (window.RegExp) {
    var tempStr="a";var tempReg=new RegExp(tempStr);
    if (tempReg.test(tempStr)){isSupp=1};
  }
  if (!isSupp) 
    return (str.indexOf(".")>2)&&(str.indexOf("@")>0);
  var r1=new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2=new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str)&&r2.test(str));
}

// Remove all spaces from a string
function removeSpaces(str) {
   var ret='';
   for (var i=0; i<str.length; i++){if (str.charAt(i)!=' ') ret+=str.charAt(i);}
   return ret;
}

// Check that a string contains only numbers
function isNumeric(sText){
    var ValidChars = "0123456789.,";var isNum=true;var Char;
    for (i=0; i<sText.length&&isNum==true; i++){Char=sText.charAt(i);if (ValidChars.indexOf(Char)==-1){isNum=false;}}
    return isNum;
}

//trim leading and trailing spaces to ensure user didn't enter all blanks
function trim(str) {
    while (str.charAt(str.length - 1)==" ") str=str.substring(0, str.length - 1);
    while (str.charAt(0)==" ") str=str.substring(1, str.length);
    return str;
}

function CommaFormatted(amt){
	var delim=","; /* replace comma if desired */
	var a=amt;
	var i=parseInt(a);
	if(isNaN(i)){return '';}
	i=Math.abs(i);
	var n=new String(i);var a=[];
	while(n.length>3) {var nn=n.substr(n.length-3);a.unshift(nn);n=n.substr(0,n.length-3);}
	if(n.length>0){a.unshift(n);}
	n=a.join(delim);
	amt=n; 
	return amt;
}

//clears default value from search box
 function clearBox(theText){if(theText.value==theText.defaultValue){theText.value=""}}

/* Lawn size calculation tools */
function calculateSquareFeet(lawnAcres){
	var decLawnSqFt=0;var decLawnAcres=0;
	if(trim(lawnAcres)=="") {window.alert("Please enter your an acre amount!");return false;}
	if(!isNumeric(eval(trim(lawnAcres)))) {
	    window.alert("You must enter a numeric value!");return false;
	} else {
	    decLawnAcres=eval(trim(lawnAcres));
	}
	decLawnSqFt=decLawnAcres/0.0000229568411386593;
	return CommaFormatted(decLawnSqFt);
}

/* Custom IndexTools Action Tracking */
function trackCalculateActionIndextools() {
    var trackObj = createITT();
    trackObj.ACTION = '05';
    trackObj.submit_action();
}
function trackAddProductIndextools(sku){
    var trackObj = createITT();
    trackObj.ACTION = '06';
    if (sku!=null){if (sku.length>0){trackObj._S_SKU=sku;}}
    trackObj.submit_action();
}
function trackChangeProductIndextools(sku){
    var trackObj = createITT();
    trackObj.ACTION = '07';
    if (sku!=null){if(sku.length>0){trackObj._S_SKU = sku;}}
    trackObj.submit_action();
}
function trackProductCrossSellClick() {
    //alert("Product");
    var trackObj=createITT();
    trackObj.ACTION='08';
    trackObj.submit_action();
}
function trackCartCrossSellClick() {
    //alert("Cart");
    var trackObj=createITT();
    trackObj.ACTION='09';
    trackObj.submit_action();
}

/* LOAD IMAGES FOR AJAX TOOLS */
var img1=new Image();img1.src='/images/ajax-loader-large.gif';

/*
 * THIS SET OF FUNCTIONS WAS REMOVED AS NOT USED ON NEW ABLECOMMERCE SITE, RL 12/18/2007
 * Script Name: ccvalidate.js
 * Written By:  Kevin King                    (Kevin@PrecisOnline.com)
 *              for Precision Solutions, Inc. (http://www.PrecisOnline.com)
 * Description: This script validates the basic accuracy of a credit card 
 *              number.  It expects the card type and card number to have 
 *              been entered/selected, and then the verifyCard function can 
 *              be called to verify the accuracy of the card number.
 */
 
 
/* pullquote function by Roger Johansson, http://www.456bereastreet.com/ */
var pullquote={
	init : function() {
	// Check that the browser supports the methods used
		if (!document.getElementById || !document.createElement || !document.appendChild) return false;
		var oElement, oPullquote, oPullquoteP, oQuoteContent, i, j;
	// Find all span elements with a class name of pullquote
		var arrElements=document.getElementsByTagName('span');
		var oRegExp=new RegExp("(^|\\s)pullquote(\\s|$)");
		for (i=0; i<arrElements.length; i++) {
	// Save the current element
			oElement=arrElements[i];
			if (oRegExp.test(oElement.className)) {
	// Create the blockquote and p elements
				oPullquote=document.createElement('blockquote');
				oPullquote.className=oElement.className
				oPullquoteP=document.createElement('p');
	// Insert the pullquote text
				for(j=0;j<oElement.childNodes.length;j++) {oPullquoteP.appendChild(oElement.childNodes[j].cloneNode(true));}
				oPullquote.appendChild(oPullquoteP);
	// Insert the blockquote element before the span elements parent element
				oElement.parentNode.parentNode.insertBefore(oPullquote,oElement.parentNode);
			}
		}
	}
};

// Pull-quotes as used on Preen.com
$(document).ready(function(){
//    $('span.pullquote').each(function(index) {
    $('.pullquote').each(function(index) {
	    var $P=$(this).parent('p');
	    $P.css('position','relative');
	    var $C=$(this).clone();
	    $C.addClass('pulledquote').removeClass('pullquote').find('span.drop').html('&hellip;').end().insertBefore($P);
	    var cTxt= $C.text();
	    $C.html(cTxt);
    }); 
});


// addEvent function from http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
/*
//Replaced by addLoadEvent by CSS Guy
function addPageEvent(obj,type,fn){
	if (obj.addEventListener)
		obj.addEventListener(type,fn,false);
	else if (obj.attachEvent)
	{
		obj["e"+type+fn]=fn;
		obj[type+fn]=function(){obj["e"+type+fn](window.event);}
		obj.attachEvent("on"+type,obj[type+fn]);
	}
}
*/

// CSS Guy Scripts
function addLoadEvent(func) {
  var oldonload=window.onload;
  if (typeof window.onload != 'function') {
    window.onload=func;
  } else {
    window.onload=function() {oldonload();func();}
  }
}

function insertAfter(newElement,targetElement){
  var parent=targetElement.parentNode;
  if (parent.lastChild==targetElement){parent.appendChild(newElement);}else{parent.insertBefore(newElement,targetElement.nextSibling);}
}

function faviconize() { 
  if (!document.getElementsByTagName) return false;
  if (!document.createElement) return false;
  var links = document.getElementsByTagName("a");
  	for (var j=0; j<links.length; j++) {
  		var hoststring = /^http:/;
  		var hrefvalue = links[j].getAttribute("href",2);
  		if (hrefvalue && hrefvalue.search(hoststring) != -1) {
		    var domain=hrefvalue.match(/(\w+):\/\/([^/:]+)(:\d*)?([^# ]*)/);
		    domain = RegExp.$2;
		    if (domain.search("greenviewfertilizer.com") == -1) {
		        var cue = document.createElement("img");
		        cue.className = "faviconimg";
		        var cuesrc = "http://"+domain+"/favicon.ico";
		        cue.setAttribute("src",cuesrc);
		        cue.onerror=function() {this.src="/images/icons/external.gif";}
		        insertAfter(cue,links[j]);
            }
		}
	}
}


/*------------------------------------------------------------------------------
    Function:       footnoteLinks()
    Author:         Aaron Gustafson (aaron at easy-designs dot net)
    Creation Date:  8 May 2005
    Version:        1.3
    Homepage:       http://www.easy-designs.net/code/footnoteLinks/
    License:        Creative Commons Attribution-ShareAlike 2.0 License
                    http://creativecommons.org/licenses/by-sa/2.0/
    Note:           This version has reduced functionality as it is a demo of the script's development
------------------------------------------------------------------------------*/
function footnoteLinks() {
    var containerID = 'primarycontent';
    var targetID = 'primarycontent';
    if (!document.getElementById || !document.getElementsByTagName || !document.createElement) return false;
    if (!document.getElementById(containerID) || !document.getElementById(targetID)) return false;

    var container=document.getElementById(containerID);
    var target=document.getElementById(targetID);
    var hDiv=document.createElement('br');
    addClass.apply(hDiv,['printOnly']);
    addClass.apply(hDiv,['newPage']);
    var h2=document.createElement('h2');
    addClass.apply(h2,['printOnly']);
    var h2_txt=document.createTextNode('Links');
    h2.appendChild(h2_txt);
    var coll=container.getElementsByTagName('*');var ol=document.createElement('ol');
    addClass.apply(ol,['printOnly']);
    var myArr=[];var thisLink;var num=1;var showLinks=0;
    for (var i=0; i<coll.length; i++) {
        if ( (coll[i].getAttribute('href') ||
              coll[i].getAttribute('cite')) &&
              coll[i].getAttribute('href') != '#' &&
              coll[i].getAttribute('class') != 'lblock' &&
              coll[i].getAttribute('class') != 'reflink' &&
              coll[i].parentNode.getAttribute('class') != 'modulenav'  &&
              coll[i].parentNode.parentNode.getAttribute('class') != 'modulenav'  &&
              coll[i].parentNode.parentNode.parentNode.getAttribute('class') != 'modulenav'  &&
              coll[i].parentNode.getAttribute('class') != 'crumbs' ) { 
              
            showLinks++;
            thisLink=coll[i].getAttribute('href') ? coll[i].href : coll[i].cite;
            var note=document.createElement('span');
            note.setAttribute('class','sup');
            addClass.apply(note,['printOnly']);
            var note_txt;
            var j=inArray.apply(myArr,[thisLink]);
            if (j||j===0){ // if a duplicate get the corresponding number from the array of used links
                note_txt=document.createTextNode(j+1);
            } else { // if not a duplicate
                var li=document.createElement('li');
                var li_txt=document.createTextNode(thisLink);
                li.appendChild(li_txt);
                ol.appendChild(li);
                myArr.push(thisLink);
                note_txt=document.createTextNode(num);
                num++;
            }
            note.appendChild(note_txt);
            if (coll[i].tagName.toLowerCase()=='blockquote'){
                var lastChild=lastChildContainingText.apply(coll[i]);
                lastChild.appendChild(note);
            } else {
                coll[i].parentNode.insertBefore(note, coll[i].nextSibling);
            }
        }
    }
    if (showLinks>0){
        target.appendChild(hDiv);
        target.appendChild(h2);
        target.appendChild(ol);
    }
    return true;
}

addLoadEvent(TJK_ToggleDL);
//addLoadEvent(pullquote.init);
addLoadEvent(setPopups);
addLoadEvent(faviconize);
addLoadEvent(footnoteLinks);