var runOffers = false;
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].toLowerCase() == variable) {
	  return pair[1];
    }
  }
  return null; 
}

function createCookie(name,value,days,domain) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	if(domain){
		var domainValue = "; domain="+domain+";";
	}else{
		var domainValue ="";
	}
	document.cookie = name+"="+value+expires+"; path=/"+domainValue;
}

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 getLeadSource() { 
    var ld = readCookie("ld"); 
    return (ld != false && ld != null) ? ld: '';
} 

function getPromotionCode() {
    var pc = readCookie("pc");
    return (pc != false && pc != null) ? pc: '';
}

function linkWrapper(href){
       var ldsrc = getLeadSource();      
       if(ldsrc.match(new RegExp("3B.*"))){
               var addLd = ldsrc.match(new RegExp("3B.*"));
               href = href + '&ld=WBABundle' + addLd;
       }else{
	    href = href + '&ld=WBABundle';
       }	
       document.location = href;
} 

   function trackBox(href){
       var ldsrc = getLeadSource();
       var soa = new RegExp("Selling-on-Amazon");
       var wba = new RegExp("WebStore-for-eCommerce-Business");
       var bundle = new RegExp("WebStore-Multi-Channel");
 
       ldsrc = ldsrc.match(new RegExp(".*3B")); 	
 
       if(href.match(soa)){
       		createCookie("ld", ldsrc+"soa", 30);
       }else if(href.match(wba)){
		createCookie("ld", ldsrc+"wba", 30);
       }else if(href.match(bundle)){
		createCookie("ld", ldsrc+"bundle", 30);
       }
       //alert(getLeadSource());
       document.location = href;	
   }
   
   
var ld = getQueryVariable("ld");
var pc = getQueryVariable("pc");
var adver = getQueryVariable("adver");
var type = getQueryVariable("type");
var ref= getQueryVariable("ref") ;  // Hydra hack for tracking SEM   
//-------DO NOT REMOVE; THESE ARE REPORT SUITE IDS--------
function getAccount(){
var hn = window.location.hostname;
if(hn.indexOf("webstore.amazon") >=0){
return 'amznwebstoreprod';}
return 'amznwebstoredev';
}
var s_account = getAccount();
//---------------------------------------------------------



if (ld && ld != null && ld != '') {
	createCookie("ld", ld, 30);
}else{
	var refURL = document.referrer;
	//var testRE = new RegExp("desktop\.amazon\.co","i");
	//var testRE = new RegExp("((webstoreoffers|desktop|webstore)\.amazon\.co|amazonservices\.com)","i");
	var testRE = new RegExp("(http://(webstoreoffers|webstore)\.amazon\.co|http://www\.amazonservices\.com|(desktop|integ)\.amazon\.co)","i");
	var newld = '';
	//Preserve lead when surfing internal pages 
	if( !refURL || !refURL.match(testRE)){
		var googleRE = new RegExp("www\.google\.","i");
		var yahooRE = new RegExp("\.search\.yahoo\.co","i");
		var msnRE = new RegExp("\.search\.(live|msn)\.co","i");
		var aolRE = new RegExp("(\.search\.aol\.co|aolsearch)","i");
		var askRE = new RegExp("\.ask\.co","i");
		var altaRE = new RegExp("\.altavista\.co","i");
		var lycosRE = new RegExp("\.lycos\.co","i");
	
		var azRE = new RegExp("AZMMSubNav");
		
		//Malformed lead source from AZMMSubNav
		var currentURL = window.location.search.substring(1);
		if(currentURL.match(azRE)){
       		        newld = "AZMMSubNav";
       		}else{
			//Natural Search Leads
			if(refURL.match(googleRE)){
				newld = "NSGoogle";
			}else{
				if(refURL.match(yahooRE)){
					newld = "NSYahoo";
				}else{
					if(refURL.match(msnRE)){
						newld = "NSMsn";
					}else{
						if(refURL.match(aolRE)){
							newld = "NSAol";
						}else{
							if(refURL.match(askRE) || refURL.match(altaRE) || refURL.match(lycosRE)){
								newld = "NSOther";
							}
						}
					}
				}
			}
		}
		createCookie("ld", newld, 30);
 	}
	//Only if no lead and referral URL is not an internal site URL
	if(newld == '' && !refURL.match(testRE)){
        	if(refURL && refURL != '' && getLeadSource() == ''){
                        var domain = refURL.match( /:\/\/([^\/:]+)/ );
			//Prefix "EL" for External Links
			if(domain){
				newld = domain[1]?"EL-"+domain[1]:'';
			}
		}else{
			newld = getLeadSource();
		}
		createCookie("ld", newld, 30);
        }
}


if (pc && pc != null && pc != '') {
                createCookie("pc", pc, 30);
}


createCookie("referrer", document.referrer, 30);

function trafficFilter(){
	var randomnumber=Math.ceil(Math.random()*100);
	var offer = 0;
	if(randomnumber <= pilotTraffic){
		//then choose pilot between 1-2;
		//offer = Math.ceil(Math.random()*2);
		offer = 2;
	}
	return offer;
}
function getOffer(){
	var lead = getLeadSource();
	var offer = offerMap[lead];
	if(typeof offer == "undefined"){
		//look for wild cards
		for(x in offerMap){
			var wildCard = x.indexOf("*");
			var tempX = x;
			var tempLead = lead;
			if(wildCard > -1){
				tempX = x.replace(/\*/,"");
			}
			if(wildCard == 0){
				tempLead = lead.substring((lead.length-tempX.length),lead.length);
			}else if(wildCard > 0){
				tempLead = lead.substring(0,wildCard);
			}
			if(tempX == tempLead){
				offer = offerMap[x];
			}
		}
	}
	var leadCookie = readCookie("wacOfferLead");
	var offerCookie = readCookie("wacOffer");
	var updateOffer = 0;
	var updateOfferLead = lead;
	if(lead){
		if(lead == leadCookie){
			updateOffer = offerCookie;
			updateOfferLead = leadCookie;
		}else{
			if(typeof offer == "undefined"){
				//if offer from offerMap does not exist run traffic test
				updateOffer = trafficFilter();
			}else if(offer == "include"){
				//if offer from offerMap returns "include" run traffic test
				updateOffer = trafficFilter();
			}else if(offer == "exclude"){
				//if offer from offerMap returns "exclude" do not show pilots
				updateOffer = 0;
			}else{
				//else use offer from offerMap
				updateOffer = offer;
			}
			updateOfferLead = lead;
		}
	}else{
		if(offerCookie){
			updateOffer = offerCookie;
			updateOfferLead = leadCookie;
		}else{
			updateOffer = trafficFilter()
			updateOfferLead = lead;
		}
	}
		if(updateOffer == 1) updateOffer = 2; //handle old cookies
		createCookie("wacOffer",updateOffer,30,"amazon.com");
		createCookie("wacOfferLead",updateOfferLead,30,"amazon.com");
		return updateOffer;
	
}
function pickRandomOffer(){
	var offer = 0
	if(runRandom){
		offer = Math.floor(Math.random()*3);
	}
	return offer;
}
function displayOffer(validPage,offer){
	if(offer > 0){
		if(validPage){
			//remove sell on amazon link
			//$("#leftnav .new").parent().addClass("hide");
			$("#leftnav .new").text("Setup Assistance");
			$("#leftnav .new").attr("href","/Setup-Assistance-for-WebStore-by-Amazon/");
			$("#leftnav .new").attr("class","newOffer");
			//Footer disclaimer
			swapOffer(".disclaimerInfo",offer,
					[
					 'Show original content',
					 '<div>*Offer includes one month of subscription fees for WebStore by Amazon (you will also be charged a referral fee for items that sell).  Offer limited to new subscriptions only.  Other limitations apply.  After the trial period we will automatically charge your credit card the monthly subscription fee at the beginning of every month thereafter.<br/><br/>** The Setup Assistance package is provided by an independent third party solution provider.  You must accept the Setup Assistance offer within 7 days of registering or the offer will expire.</div>',
					 '<div>*Free offer applies to Setup Assistance only (you will still be charged a monthly subscription fee and a referral fee for items that sell).  Offer limited to new subscriptions only.  You must accept the Setup Assistance offer within 7 days of registering or the offer will expire.  Other limitations apply.  The Setup Assistance package is provided by an independent third party solution provider.</div>'
					 ]
			);
			//signup banner
			swapOffer(".banner a[name='SignUpNow']",offer,
					[
					 'Show original content',
					 '<img src="http://g-ecx.images-amazon.com/images/G/01/webstore/wac/images/signupBanner_1._V220830311_.gif" alt="WebStore by Amazon & FREE* Setup Assistance" />',
					 '<img src="http://g-ecx.images-amazon.com/images/G/01/webstore/wac/images/signupBanner_2._V220830310_.gif" alt="WebStore by Amazon & FREE* Setup Assistance" />'
					 ]
			);
			//home page
			swapOffer("#homeOffers",offer,
					[
					 'Show original content',
					 '<div class="offer_1">'+
			   	  		'<div class="col1"><img src="http://g-ecx.images-amazon.com/images/G/01/webstore/wac/images/offer_branded_websites.jpg" alt="Your own branded ecommerce website"/></div>'+
			   	  		'<div class="col2"><a href="/WebStore-Pricing/"><img src="http://g-ecx.images-amazon.com/images/G/01/wba/bundle/blank._V261124187_.gif" alt="Build an ecommerce website powered by Amazon\'s technology. Limited time offer includes 1 month free* trial and setup assistance to launch your site. Learn more."/></a></div>'+
			   	  		'<div class="col3"><a href="https://sellercentral.amazon.com/gp/webstore-seller/registration/login.html/?ie=UTF8&tokenID=webstore"><img src="http://g-ecx.images-amazon.com/images/G/01/wba/bundle/blank._V261124187_.gif" alt="1st Month Free* Sign Up Now"/></a></div>'+
			   	  	'</div>'+
                    '<p>&nbsp;</p><div id="lightbulbs-img"><img src="http://g-ecx.images-amazon.com/images/G/01/webstore/wac/images/lightBulbs.jpg" alt="light bulbs"/></div>',
				   	 '<div class="offer_2">'+
			   	  		'<div class="col1"><img src="http://g-ecx.images-amazon.com/images/G/01/webstore/wac/images/offer_branded_websites.jpg" alt="Your own branded ecommerce website"/></div>'+
			   	  		'<div class="col2"><a href="/WebStore-Pricing/"><img src="http://g-ecx.images-amazon.com/images/G/01/wba/bundle/blank._V261124187_.gif" alt="Build an ecommerce website powered by Amazon\'s technology. Limited time offer : get FREE* Setup Assistance. Learn more."/></a></div>'+
			   	  		'<div class="col3"><a href="https://sellercentral.amazon.com/gp/webstore-seller/registration/login.html/?ie=UTF8&tokenID=webstore"><img src="http://g-ecx.images-amazon.com/images/G/01/wba/bundle/blank._V261124187_.gif" alt="Sign Up Now"/></a></div>'+
			   	  	'</div>'+
                    '<p>&nbsp;</p><div id="lightbulbs-img"><img src="http://g-ecx.images-amazon.com/images/G/01/webstore/wac/images/lightBulbs.jpg" alt="light bulbs"/></div>',
					 ]
			);
            moveDiv('#sh_webinar',313,80);
            moveDiv('#sh_webinar_banner',313,80);
            moveDiv('#sh_lead',313,475);
            moveDiv('#sh_lead_banner',313,475);
			//Pricing page
			swapOffer("#offerPricing",offer,
					[
					 'Show original content',
					 
					 '<div class="offer_1">'+
			    		'<div class="offerName">'+
			    			'<strong>WebStore by Amazon</strong><br/>'+
			    			'<p>'+
			    				'Limited time offer includes 1 month free* trial and setup assistance** to launch your site.'+
			    			'</p>'+
			    		'</div>'+
			    		'<div class="offerText">'+
						'<p>'+
							'<strong>WebStore Features Include:</strong>'+
						'</p>'+
						'<ul>'+
							'<li>Credit card processing and fraud protection</li>'+	
							'<li>Drive traffic by automatic submission to search engines</li>'+
							'<li>Amazon\'s <a href="http://www.amazon.com/gp/help/customer/display.html?nodeId=537868" target="_blank">A-to-Z Guarantee</a> on all qualified  purchases</li>'+
							'<li>Millions of Amazon.com shoppers pre-registered to shop on your site</li>'+
							'<li>Amazon\'s proven marketing techniques including product reviews and best sellers available  on your site</li>'+
							'<li>Opportunity to earn additional revenue by advertising Amazon products</li>'+
							'<li>Create multiple WebStores under one account </li>'+
						'</ul>'+
						'<p>'+
							'<strong>Setup Assistance is provided by an independent third party solution provider and includes:</strong>'+
						'</p>'+
						'<ul>'+
						'<li>Domain name set-up assistance</li>'+
						'<li>Assistance choosing a theme</li>'+
						'<li>Assistance loading your logo</li>'+
						'<li>Assistance loading your content</li>'+
						'<li>Assistance loading up to 5 products</li>'+
						'<li>Assistance creating up to 10 categories or sub-categories</li>'+
						'<li>Assistance configuring your WebStore settings</li>'+
						'<li>Publishing and launching your WebStore</li>'+
						'</ul>'+
						'<a href="/Setup-Assistance-for-WebStore-by-Amazon/">Learn More</a>'+
						'</div>'+
			    		'<div class="offerPrice">'+
			    			'<span class="dollar">$59</span>'+
				   	  		'<span class="cents">99</span>'+
				   	  		'<span class="interval">month</span>'+
				   	  		'<ul class="extras">'+
				   	  		'<li>49.99 Set Up Fee**</li>'+
				   	  		'<li>7% referral fee<br/>(Includes credit card fees)</li>'+
				   	  		'</ul>'+
				   	  	'</div>'+
				   	  	'<div class="offerSignup">'+
				   	  		'<a href="https://sellercentral.amazon.com/gp/webstore-seller/registration/login.html/?ie=UTF8&tokenID=webstore"><img src="http://g-ecx.images-amazon.com/images/G/01/webstore/wac/images/btn_signup_cl.gif" alt="Sign up now"/></a><br/>'+
				   	  		'<a class="seeHowItWorks" onclick="window.open(\'http://g-ec2.images-amazon.com/images/G/01/webstore/wac/images/webstore_how_it_works_new.gif\', \'howitworks_wba_win\', \'width=500, height=200, resizable=1, scrollbars=1\'); return false" href="">See how it works</a>'+
				   	  	'</div>'+
				   	  	'<div class="stars">'+
							'<img class="trial" src="http://g-ecx.images-amazon.com/images/G/01/webstore/wac/images/star_setup_youget_white._V223752595_.gif"/>'+
							'<img class="launch" src="http://g-ecx.images-amazon.com/images/G/01/webstore/wac/images/star_trial_youget_white._V223752301_.gif"/>'+
						'</div>'+
			    	'</div>',
			    	
			    	
			    	'<div class="offer_2">'+
		    		'<div class="offerName">'+
		    			'<strong>WebStore by Amazon</strong><br/>'+
		    			'<p>'+
		    				'For a limited time get<br/>'+
		    				'<b>FREE*</b> Setup Assistance ($99.99 value)'+
		    			'</p>'+
		    		'</div>'+
		    		'<div class="offerText">'+
					'<p>'+
						'<strong>WebStore Features Include:</strong>'+
					'</p>'+
					'<ul>'+
						'<li>Credit card processing and fraud protection</li>'+	
						'<li>Drive traffic by automatic submission to search engines</li>'+
						'<li>Amazon\'s <a href="http://www.amazon.com/gp/help/customer/display.html?nodeId=537868" target="_blank">A-to-Z Guarantee</a> on all qualified  purchases</li>'+
						'<li>Millions of Amazon.com shoppers pre-registered to shop on your site</li>'+
						'<li>Amazon\'s proven marketing techniques including product reviews and best sellers available  on your site</li>'+
						'<li>Opportunity to earn additional revenue by advertising Amazon products</li>'+
						'<li>Create multiple WebStores under one account </li>'+
					'</ul>'+
					'<p>'+
						'<strong>FREE* Setup Assistance is provided by an independent third party solution provider and includes:</strong>'+
					'</p>'+
					'<ul>'+
					'<li>Domain name set-up assistance</li>'+
					'<li>Assistance choosing a theme</li>'+
					'<li>Assistance loading your logo</li>'+
					'<li>Assistance loading your content</li>'+
					'<li>Assistance loading up to 5 products</li>'+
					'<li>Assistance creating up to 10 categories or sub-categories</li>'+
					'<li>Assistance configuring your WebStore settings</li>'+
					'<li>Publishing and launching your WebStore</li>'+
					'</ul>'+
					'<a href="/Setup-Assistance-for-WebStore-by-Amazon/">Learn More</a>'+
					'</div>'+
		    		'<div class="offerPrice">'+
			   	  		'<span class="dollar">$59</span>'+
			   	  		'<span class="cents">99</span>'+
			   	  		'<span class="interval">month</span>'+
			   	  		'<span class="extras">+ 7% Referral Fee</span>'+
			   	  	'</div>'+
			   	  	'<div class="offerSignup">'+
			   	  		'<a href="https://sellercentral.amazon.com/gp/webstore-seller/registration/login.html/?ie=UTF8&tokenID=webstore"><img src="http://g-ecx.images-amazon.com/images/G/01/webstore/wac/images/btn_signup_cl.gif" alt="Sign up now"/></a><br/>'+
			   	  		'<a class="seeHowItWorks" onclick="window.open(\'http://g-ec2.images-amazon.com/images/G/01/webstore/wac/images/webstore_how_it_works_new.gif\', \'howitworks_wba_win\', \'width=500, height=200, resizable=1, scrollbars=1\'); return false" href="">See how it works</a>'+
			   	  	'</div>'+
			   	  	'<div class="stars">'+
						'<img class="launch" src="http://g-ecx.images-amazon.com/images/G/01/webstore/wac/images/star_setup_free_white._V223437858_.gif"/>'+
					'</div>'+
		    	'</div>'
					 ]
			);
		}
		$("#leftnav .newOffer,.disclaimerInfo,.banner,#homeOffers,#offerPricing").css("display","block");
	}
}
function swapOffer(selector,offer,swapHTML){
	if(typeof swapHTML[offer] == "undefined"){
		return false;
	}else{
		$(selector).html(swapHTML[offer]);
	}
}
function moveDiv(selector, top, left) {
    var div = $(selector);
    if (div) {
        div.css('top', ''+top+'px');
        div.css('left', ''+left+'px');
    }
}
var offer = 0;
if(typeof runOffers == "undefined"){
	throw "Could not find runOffers, weights.js is missing.";
	}else if(runOffers == true){
		offer = getOffer();
		if(typeof dontTouch  == "undefined" ){
			throw "DontTouch table is missing";
		}else{
			var url = window.location+"";
			url = url.split("/");
			var dir = url[3];
			var validPage = true;
			for(i in dontTouch){
				if(dontTouch[i] == dir){
					validPage = false;
				}
			}
			if(!validPage && offer > 0){
				window.location = "/";
			}
			
			if(typeof $ != "undefined"){
				if(validPage && offer > 0){
					document.write('<style type="text/css">'+
						'#leftnav .new{display:none;}'+
						'.disclaimerInfo{display:none;}'+
						'.banner{display:none;}'+
						'#homeOffers{display:none;}'+
						'#offerPricing{display:none;}'+
					'</style>');
					$(document).ready(function(){
						displayOffer(validPage,offer);
					});
				}
			}
		}
	}else{
		//turn off pilot program
		var offerCookie = readCookie("wacOffer");
		if(offerCookie){
			createCookie("wacOffer","",-1,"amazon.com");
		}
}
