addcart=false;
formElements = 0;
var cartData = [];

var r = new Date();
reff = document.referrer; 

if(reff.toLowerCase().indexOf("powerwf.com")<0)
        addCookie("REFURL_"+r.getTime(),reff);

var d = new Date();  
    addCookie("PREVURL_"+d.getTime(),document.location)  


setTimeout("delayedProcessing()",3500);


function delayedProcessing()
{
    eval('__sfga()');    
}


String.prototype.trim = function(){return this.replace(/(^\s*)|(\s*$)/g,"")}


   function getDetails(detailsURL)
   {
     if(!addcart){document.location=detailsURL}else{addcart=false;}
   }
   function addCartPWF(btnClicked)
   {
     addcart=true;
     showCart();
     
     var cartForm = document.forms["CART_FORM"];  
     var cartTable = document.getElementById("GOOGLECART");
     formElements = cartData.length;

     var productContainer = btnClicked.parentNode.parentNode.parentNode;
     
     var prodTitle = "";
     var prodCode = "";
     var prodPrice = "";
     var prodQTY = 1; 		     
     var prodImgURL = "";
     var prodMaintenance = "";
     var prodMaintenanceCode = "";
      		      		      		       
     
    theChildNode = findChildElement(productContainer,"product-title");
    if(theChildNode) {

        prodTitle = theChildNode.innerHTML.trim(); 	
    }
    
    theChildNode = findChildElement(productContainer,"product-price");
    if(theChildNode)
    { 		 
        prodPrice = theChildNode.innerHTML.trim();       
    }
    
    theChildNode = findChildElement(productContainer,"product-attr-id");
    if(theChildNode)
    { 		 
        prodCode = theChildNode.innerHTML.trim(); 
        prodMaintenanceCode = prodCode+"-MNT";	                    
    }
    
    theChildNode = findChildElement(productContainer,"product-maint");
    if(theChildNode)
    { 		 
        prodMaintenance = theChildNode.innerHTML.trim();       
    } 		            
    theChildNode = findChildElement(productContainer,"product-image");
    if(theChildNode)
    { 	 	
        prodImgURL = theChildNode.getAttribute("src").trim();                     
    }

     if(cartTable.innerHTML.indexOf(prodTitle)<0)	    
        {
           ++formElements; 
           updateArray(formElements, prodTitle, prodCode, prodPrice, prodQTY, prodImgURL, prodMaintenance, prodMaintenanceCode);
           addCartRow(cartTable,formElements, prodTitle, prodCode, prodPrice, prodQTY, prodImgURL, prodMaintenance, prodMaintenanceCode);
           
        }
        else
        {
           //just update qty.           
           //skip header row and alternate rows.
           for(var row = 1; row < cartTable.rows.length; row+=2)
           {
              theRow = cartTable.rows[row];
              if((theRow.innerHTML)&&(theRow.innerHTML.indexOf(prodTitle)>=0))
              { 						
                theRow.setAttribute("class","highlight");
                theID = getCartID(prodTitle);  	                               	                	                		                
                qtyInput = document.getElementById("shopping-cart.items.item-"+theID+".quantity"); 		                
                qty = 1+parseInt(qtyInput.value); 
                qtyInput.value = qty;
                var extPriceCol = document.getElementById("extprice-"+theID); 
                extPriceCol.innerHTML="$"+(qty*parseFloat(prodPrice.substring(1)))+".00";
                updateArray(theID, prodTitle, prodCode, prodPrice, qty, prodImgURL, prodMaintenance, prodMaintenanceCode)
                
                ++theID;
                qtyMntInp = document.getElementById("shopping-cart.items.item-"+theID+".quantity");
                qtyMntInp.value = qty;
                qtyMntInpD = document.getElementById("shopping-cart.items.item-"+theID+".quantity-dis");
                qtyMntInpD.value = qty;
                var extMntPriceCol = document.getElementById("extmntprice-"+theID); 
                extMntPriceCol.innerHTML="$"+(qty*parseFloat(prodMaintenance.substring(1)))+".00";
                
                mntRow = cartTable.rows[row+1];
                mntRow.setAttribute("class","highlight");
                
                
                break;
              }     		                       
           }
        }   
        setTimeout("removeHighlights();",2200);
   }
   
   function updateMaint(product,maintID,theID, prodTitle, prodCode, prodPrice, prodImgURL, prodMaintenance, prodMaintenanceCode)
   {
      maintInp = document.getElementById(maintID);
      maintInp.value = product.value;
      maintInpDis = document.getElementById(maintID+"-dis")
      maintInpDis.value = product.value;   
      updateArray(theID, prodTitle, prodCode, prodPrice, product.value, prodImgURL, prodMaintenance, prodMaintenanceCode);  
         
      var extPriceCol = document.getElementById("extprice-"+theID); 
                extPriceCol.innerHTML="$"+(maintInp.value*parseFloat(prodPrice.substring(1)))+".00";                
      var extMntPriceCol = document.getElementById("extmntprice-"+(1+theID)); 
                extMntPriceCol.innerHTML="$"+(maintInp.value*parseFloat(prodMaintenance.substring(1)))+".00";    
   }
             
   function removeHighlights()
   {
        var cartTable = document.getElementById("GOOGLECART");
        for(row in cartTable.rows)
        {
            if(cartTable.rows[row].className == "highlight")
                cartTable.rows[row].className = "";
        }
   }
   
   function toggleCart(obj)
   {

       var theCart = document.getElementById("display-cart");
        if(theCart.style.display != "none")
        {                    
            theCart.style.visibility = "hidden";
            theCart.style.display = "none";
            obj.style.classname = "page_item page-item-4567 parent";
        }
        else
        {
            theCart.style.visibility = "visible";
            theCart.style.display = "block";
            obj.style.classname = "page_item page-item-4567 parent active";
        }
   }
   
   function showCart()
   {
       var theCart = document.getElementById("display-cart");
        theCart.style.visibility = "visible";
        theCart.style.display = "block";
   }
   
   function addCartRow(cartTable, formElements, prodTitle, prodCode, prodPrice, prodQTY, prodImgURL, prodMaintenance, prodMaintenanceCode)
   {
        if(prodQTY <= 0)
            return;
            
	   newLine = cartTable.insertRow(-1); 	 	                	                             
       newMntLine = cartTable.insertRow(-1);
       newLine.setAttribute("style","border:solid 1px #cccccc;");
       newMntLine.setAttribute("style", "border:solid 1px #cccccc;");  	                             
       
       newImg = newLine.insertCell(0);
       newImg.setAttribute("align", "center");
       newImg.setAttribute("width", "50");
       prodImg = document.createElement("IMG");
       prodImg.setAttribute("src", prodImgURL);
       prodImg.setAttribute("height", "50");
       prodImg.setAttribute("width", "50");
       newImg.appendChild(prodImg);             
       newCodeInput = document.createElement("INPUT");
       newCodeInput.setAttribute("name","shopping-cart.items.item-"+formElements+".item-description");
       newCodeInput.setAttribute("value",prodCode);    
       newCodeInput.setAttribute("type","hidden");   
       newImg.setAttribute("Rowspan","2");                           
       newImg.appendChild(newCodeInput);

       newTitle = newLine.insertCell(1);
       newTitle.setAttribute("style", "border-left:1px solid #cccccc");
       newTitle.appendChild(document.createTextNode(prodTitle));
       newTitle.setAttribute("class", "cart-black");	                    
       newTitleInput = document.createElement("INPUT");
       newTitleInput.setAttribute("name","shopping-cart.items.item-"+formElements+".item-name");
       newTitleInput.setAttribute("value",prodTitle);
       newTitleInput.setAttribute("type","hidden");
       newTitle.appendChild(newTitleInput);

       newQTY = newLine.insertCell(2);
       newQTY.setAttribute("class", "cart-black");	
       newQTYInput = document.createElement("INPUT");
       newQTYInput.setAttribute("name", "shopping-cart.items.item-" + formElements + ".quantity");
       newQTYInput.setAttribute("value", prodQTY);
       newQTYInput.setAttribute("maxlength", "3");
       newQTYInput.setAttribute("size", "2");
       newQTYInput.setAttribute("onchange", "updateMaint(this,'shopping-cart.items.item-" + (formElements + 1) + ".quantity'," + formElements + ",'" + prodTitle + "','" + prodCode + "','" + prodPrice + "','" + prodImgURL + "','" + prodMaintenance + "','" + prodMaintenanceCode + "');");
       newQTYInput.setAttribute("id", "shopping-cart.items.item-" + formElements + ".quantity");
       newQTY.appendChild(newQTYInput);

       newPrice = newLine.insertCell(3);
       newPrice.setAttribute("class", "cart-black");
       newPrice.appendChild(document.createTextNode(prodPrice));
       newPrice.setAttribute("align","right");	                    
       newPriceInput = document.createElement("INPUT");
       newPriceInput.setAttribute("name","shopping-cart.items.item-"+formElements+".unit-price");
       var unitPrice=parseFloat(prodPrice.substring(1));
	   newPriceInput.setAttribute("value",unitPrice);
       newPriceInput.setAttribute("type","hidden");
       newPrice.appendChild(newPriceInput);
       
       newCurrencyInput = document.createElement("INPUT");
       newCurrencyInput.setAttribute("name","shopping-cart.items.item-"+formElements+".unit-price.currency");
       newCurrencyInput.setAttribute("value","USD");
       newCurrencyInput.setAttribute("type","hidden");
       newPrice.appendChild(newCurrencyInput);
       
       newDigiInput = document.createElement("INPUT");
       newDigiInput.setAttribute("name","shopping-cart.items.item-"+formElements+".digital-content.display-disposition");
       newDigiInput.setAttribute("value","OPTIMISTIC");
       newDigiInput.setAttribute("type","hidden");
       newPrice.appendChild(newDigiInput);
       
       newEmailInput = document.createElement("INPUT");
       newEmailInput.setAttribute("name","shopping-cart.items.item-"+formElements+".digital-content.email-delivery");
       newEmailInput.setAttribute("value","true");
       newEmailInput.setAttribute("type","hidden");
       newPrice.appendChild(newEmailInput);

       extPriceCell = newLine.insertCell(4);
       extPriceCell.setAttribute("class", "cart-black");
       extPrice=(unitPrice*prodQTY);
       extPriceCell.appendChild(document.createTextNode("$"+extPrice+".00"));
       extPriceCell.setAttribute("align","right");
       extPriceCell.setAttribute("style","border-left:1px solid #cccccc");
       extPriceCell.setAttribute("id","extprice-"+formElements);
       
       ++formElements;

       newMntTitle = newMntLine.insertCell(0);
       newMntTitle.setAttribute("style", "border-left:1px solid #cccccc");
       newMntTitle.setAttribute("class", "cart-black");
       newMntTitle.appendChild(document.createTextNode("Maintenance - " + prodTitle));	                    
       newMntTitleInput = document.createElement("INPUT");
       newMntTitleInput.setAttribute("name","shopping-cart.items.item-"+formElements+".item-name");
       newMntTitleInput.setAttribute("value","Maintenance - " + prodTitle);
       newMntTitleInput.setAttribute("type","hidden");
       newMntTitle.appendChild(newMntTitleInput);

       newMntQTY = newMntLine.insertCell(1);
       newMntQTY.setAttribute("class", "cart-black");
       newMntQTYInputD = document.createElement("INPUT");
       newMntQTYInputD.setAttribute("name", "shopping-cart.items.item-" + formElements + ".quantity-dis");
       newMntQTYInputD.setAttribute("value", prodQTY);
       newMntQTYInputD.setAttribute("maxlength", "3");
       newMntQTYInputD.setAttribute("size", "2");
       newMntQTYInputD.setAttribute("disabled", "true");
       newMntQTYInputD.setAttribute("id", "shopping-cart.items.item-" + formElements + ".quantity-dis");
       newMntQTY.appendChild(newMntQTYInputD);
       newMntQTYInput = document.createElement("INPUT");
       newMntQTYInput.setAttribute("name", "shopping-cart.items.item-" + formElements + ".quantity");
       newMntQTYInput.setAttribute("value", prodQTY);
       newMntQTYInput.setAttribute("maxlength", "3");
       newMntQTYInput.setAttribute("size", "2");
       newMntQTYInput.setAttribute("type", "hidden");
       newMntQTYInput.setAttribute("id", "shopping-cart.items.item-" + formElements + ".quantity");
       newMntQTY.appendChild(newMntQTYInput);

       newMntCodeInput = document.createElement("INPUT");
       newMntCodeInput.setAttribute("name", "shopping-cart.items.item-" + formElements + ".item-description");
       newMntCodeInput.setAttribute("value", prodMaintenanceCode);
       newMntCodeInput.setAttribute("type", "hidden");
       newMntQTY.appendChild(newMntCodeInput);

       newMntPrice = newMntLine.insertCell(2);
       newMntPrice.setAttribute("class", "cart-black");
       newMntPrice.appendChild(document.createTextNode(prodMaintenance));	 
       newMntPrice.setAttribute("align","right");	                   
       newMntPriceInput = document.createElement("INPUT");
       newMntPriceInput.setAttribute("name","shopping-cart.items.item-"+formElements+".unit-price");
       var unitMntPrice = parseFloat(prodMaintenance.substring(1));
	   newMntPriceInput.setAttribute("value",unitMntPrice);
       newMntPriceInput.setAttribute("type","hidden");
       newMntPrice.appendChild(newMntPriceInput);
       
       newMntCurrencyInput = document.createElement("INPUT");
       newMntCurrencyInput.setAttribute("name","shopping-cart.items.item-"+formElements+".unit-price.currency");
       newMntCurrencyInput.setAttribute("value","USD");
       newMntCurrencyInput.setAttribute("type","hidden");
       newMntPrice.appendChild(newMntCurrencyInput);
       
       newMntDigiInput = document.createElement("INPUT");
       newMntDigiInput.setAttribute("name","shopping-cart.items.item-"+formElements+".digital-content.display-disposition");
       newMntDigiInput.setAttribute("value","OPTIMISTIC");
       newMntDigiInput.setAttribute("type","hidden");
       newMntPrice.appendChild(newMntDigiInput);
       
       newMntEmailInput = document.createElement("INPUT");
       newMntEmailInput.setAttribute("name","shopping-cart.items.item-"+formElements+".digital-content.email-delivery");
       newMntEmailInput.setAttribute("value","true");
       newMntEmailInput.setAttribute("type","hidden");
       newMntPrice.appendChild(newMntEmailInput);

       extMntPriceCell = newMntLine.insertCell(3);
       extMntPriceCell.setAttribute("class", "cart-black");
       extMntPrice=(unitMntPrice*prodQTY);
       extMntPriceCell.appendChild(document.createTextNode("$"+extMntPrice+".00"));
       extMntPriceCell.setAttribute("align","right");
       extMntPriceCell.setAttribute("style","border-left:1px solid #cccccc");
       extMntPriceCell.setAttribute("id","extmntprice-"+formElements);
   }
   
   function findChildElement(baseNode,theClassName)
   {
        if((baseNode.className)&&(baseNode.className.indexOf(theClassName) >= 0))
        {                    
			return baseNode;
		}
		else
		{
			for(childIterator in baseNode.childNodes)
			{
				aNode = findChildElement(baseNode.childNodes[childIterator],theClassName);
				if(aNode)
					return aNode;
			}
		}
		return null;
   }
   
   function findInputsById(partialID)
   {
		var matchingNodes = [];
		var allNodes = document.getElementsByTagName("INPUT");
        for(nodeIterator in allNodes)
		{
				aNode = allNodes[nodeIterator];
				if((aNode.id)&&(aNode.id.indexOf(partialID)>=0))
					matchingNodes.Push(aNode);
		}
		
		return matchingNodes;
   }
  
   function updateArray(rowNum, prodTitle, prodCode, prodPrice, prodQTY, prodImgURL, prodMaintenance, prodMaintenanceCode)
   {
		if(prodQTY >0)
			cartData[rowNum] = prodTitle+"~"+prodCode+"~"+prodPrice+"~"+prodQTY+"~"+prodImgURL+"~"+prodMaintenance+"~"+prodMaintenanceCode;
		else
			cartData[rowNum] = "";
   }
   
   function getCartID(prodTitle)
   {
		for(row=0;row < cartData.length;++row)
        {  
			if((cartData[row])&&(cartData[row].indexOf(prodTitle)>=0))
			{					   
				return row;
			}
		}
   }
   
   function saveCart()
   {                                    
        for(row=0;row < cartData.length;++row)
        {  
           rowData = cartData[row];
           if(rowData)         
				addCookie("GOOGLECART-"+row,rowData);  
		   else
		        crumbleCookie("GOOGLECART-"+row,rowData); 
        }                              
   }
   
   function readCart()
   {    
   
        var cartTable = document.getElementById("GOOGLECART");
        
        for(row = 0; row < 50;++row)
        {
			savedData = getCookie("GOOGLECART-"+row);    
			            
			if(savedData)
			{
			   cartData[row]=savedData;
			   realData = cartData[row].split("~");					   
			   addCartRow(cartTable,row, realData[0],realData[1],realData[2],realData[3],realData[4],realData[5],realData[6]);
			}					
        }                
        formElements = cartData.length; 
        setTimeout("removeHighlights();",2200);
   }
   
   function addCookie(c_name,value)
   {
        var exdate=new Date();
        exdate.setDate(exdate.getDate()+7);
        document.cookie=c_name+ "=" +escape(value)+";path=/;expires="+exdate.toGMTString();
   }
   
   function getCookie(c_name)
    {               
        if (document.cookie.length>0)
        {
          c_start=document.cookie.indexOf(c_name + "=");
          if (c_start!=-1)
            {
            c_start=c_start + c_name.length+1;
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) c_end=document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end));
            }
        }
        return null;
    }
    
    function crumbleCookie( name ) 
    {                 
        document.cookie = name + "=;expires=Fri, 14-Mar-1980 00:00:01 GMT";
    }   
    
    function cleanForm()
    {
       purchaseTracking();
       saveCart();      //Save Current state
       deleteTable();   //Whack everything to get a clean state
       readCart();      //Restore clean version
       if(formElements == 0)
       {
            alert("Add products before clicking 'Checkout'");
            return false;
       }       
       var cartForm = document.forms["CART_FORM"];       
       return true;
    }
    
    function deleteTable()
    {
        var table = document.getElementById('GOOGLECART'); 
        var rows = table.rows; 
        while(rows.length>1) 
            table.deleteRow(rows.length-1); 
    }
    function emptyCart()
    {
         deleteTable();
         removeCartCookies();
         cartData = [];
    }
    
    function removeCartCookies()
    {
        for(row = 0; row < 50;++row)
       {
            crumbleCookie("GOOGLECART-"+row);
       }
    }
    
    function purchaseTracking()
    {
       document.getElementById("tracking").src = "http://www.googleadservices.com/pagead/conversion/1035872310/?label=9ZH2CPq43AEQttD47QM&amp;guid=ON&amp;script=0";       
    }
    function downloadTracking()
    {
       document.getElementById("tracking").src = "http://www.googleadservices.com/pagead/conversion/1035872310/?label=pRe0CIK43AEQttD47QM&amp;guid=ON&amp;script=0";       
    }
    
    window.onload = readCart;
    window.onbeforeunload = saveCart;     

//////////////////////////////////////

var trackingCode = false;
var mstag = "none";
var googleTrack = "none";

if (QS.length > 2)
{
  trackingCode = true;
  //MS Tagging
  mstag = '<script type="text/javascript"> if (!window.mstag) mstag = {loadTag : function(){},time : (new Date()).getTime()};</script> <script id="mstag_tops" type="text/javascript" src="//flex.atdmt.com/mstag/site/839b0534-9d12-460e-a5d4-c6b2954a8599/mstag.js"></script> <script type="text/javascript"> mstag.loadTag("analytics", {dedup:"1",domainId:"1146534",type:"1",actionid:"32884"})</script> <noscript> <iframe src="//flex.atdmt.com/mstag/tag/839b0534-9d12-460e-a5d4-c6b2954a8599/analytics.html?dedup=1&domainId=1146534&type=1&actionid=32884" frameborder="0" scrolling="no" width="1" height="1" style="visibility:hidden;display:none"> </iframe> </noscript>';
  
  //Google Tracking
  googleTrack = '<script type="text/javascript">'
+'/* <![CDATA[ */'
+'var google_conversion_id = 1035872310;'
+'var google_conversion_language = "en";'
+'var google_conversion_format = "3";'
+'var google_conversion_color = "ffffff";'
+'var google_conversion_label = "2bdbCILrmwIQttD47QM";'
+'var google_conversion_value = 0;'
+'/* ]]> */'
+'</script>'
+'<script type="text/javascript" src="http://www.googleadservices.com/pagead/conversion.js">'
+'</script>'
+'<noscript>'
+'<div style="display:inline;">'
+'<img height="1" width="1" style="border-style:none;" alt="" src="http://www.googleadservices.com/pagead/conversion/1035872310/?label=2bdbCILrmwIQttD47QM&amp;guid=ON&amp;script=0"/>'
+'</div>'
+'</noscript>';
    
  document.writeln(mstag);
  document.writeln(googleTrack);

}






