//------------------------------------START SPECIAL OFFERS--------------------------

//get the Text contained by the cart links ie the product names.
function getCartProductText(){
	//get the cart product links
	var arrCartProductLinkElements=getElements(document, "a", "cartProductLink")
	//get their respective text
	var arrCartElementText = [];
	for(var i=0; i<arrCartProductLinkElements.length; i++)
	{
		arrCartElementText.push(arrCartProductLinkElements[i].childNodes.item(0).data);
	}
	return arrCartElementText;
}

//Get a WOSID if exists (needed when not using cookies.)
function getWOSID(aURL){
	re=/wosid=([^\&]*)/;
	if(re.exec(aURL))
	{
		return RegExp.$1;
	}
	return;
}
//Get the appId if it exists (needed when not using cookies.)
function getAppID(aURL){
	re=/\/(\d+)?\/?wa/;
	if(re.exec(aURL))
	{
		return RegExp.$1;
	}
	return;
}

function writeCartOffers(){
	var offers=initOffers();
	if(offers.length==0)return;
	
	for(var index=0; index<offers.length; index++) {
		var offer=offers[index];
		if(offer.length==8||offer.length==9)
		{
			 if(writeCartOffer(offer)) return;
		}
		
	}
	
}

function writeCartOffer(offer){
	var catId=offer[0];
	var prodId=offer[1];
	var offerImage=offer[2];
	var offerHeadline=offer[3];
	var offerText=offer[4];
	var invalidRegex=offer[5];
	var triggerRegex=offer[6];
	var offerPrice=offer[7];
	var saveValue=offer[8];
	if(canShowOffer(invalidRegex,triggerRegex)) 
	{
		var triggerPosition=stringArrayContains(getCartProductText(),triggerRegex)
		var triggerProductText=getCartProductText()[triggerPosition];
		var addToCartLink=getCartLink(prodId,catId);
		//open the offer table
		document.write("<table CELLSPACING=0 CELLPADDING=0 style='width:588px;border:1px solid #3AA267;margin:10px 4px 10px 5px;float:left'>");
		document.write("<tr><td style='text-align:center;padding:4px;color:#000;background-color:#d6e9f7'>");
		document.write("Customers who bought a "+triggerProductText+" also bought:");
		document.write("<\/td><\/tr>");
		document.write("<tr><td>");	
		document.write("<img style='float:left;height:118px;margin-right:2px' alt 'Checkout Offer' src='/ires/53/WW/wwProductImages/"+offerImage+"'>");
		document.write("<p>");
		document.write("<DIV CLASS=productListDescriptionDiv>"+offerHeadline+"</DIV>");
		document.write("<DIV CLASS=productListTextDiv>"+offerText+"</DIV>");
		document.write("<\/p>");
		document.write("<SPAN style='#999'>ONLY<\/SPAN> <SPAN>&#163;"+offerPrice+"<\/SPAN>");
		document.write("<a class='productListAddToBasketLink' style='display:inline' href='"+addToCartLink+"'>Add To Cart<\/a>");
		if(saveValue)
			{
				document.write("<br><SPAN style='color:red;padding-top:2px;'>SAVE<\/SPAN> <SPAN>&#163;"+saveValue+"<\/SPAN>");
			}
		document.write("<\/p><\/td><\/tr><\/table>");
		return true;
	}
return false;	
		
}

//write the special offer
function getCartLink(prodId,catId){
	//get the wosid and appid we may need them.
	var woSID=getWOSID(location.href);
	var appID=getAppID(location.href);
	var addToCartLink="";
	if(woSID)
	{
		//we have a WSID so we don't have cookies
		addToCartLink="/" + appID +"/wa/4/40/"+catId+"/"+prodId+"/mpp?wosid="+woSID;
	}
	else
	{
		addToCartLink="/wa/4/40/"+catId+"/"+prodId+".html";

	}
	return addToCartLink;
}

// we can present the offer......
function canShowOffer(invalidText,triggerRegex){
	//get the text of the items in the cart. 
	var cartLinkTextArray=getCartProductText();
	//No product  means no offer!
	if(cartLinkTextArray.length==0) return false;
	//If it contains the invalid text then return false.
	if(stringArrayContains(cartLinkTextArray,invalidText)!=-1) 
	{
		return false;
	}
	else
	{
		if(stringArrayContains(cartLinkTextArray,triggerRegex)!=-1)
		{
			return true;
		}
		return false;
	}

}

//Does the array passed contain the regex, rtn true or false.
function stringArrayContains(array, regex){
	if(array.length==0)return -1;
	
	for (var i = 0; i < array.length; i++) 
	{
		if(regex.test(array[i]))return i;
	}
	return -1;
}
function initOffers(){
	var offers=new Array();
	//--1---Chameau Boot Bag---------------
	/*var offer=new Array(8);
	offer[0]="1017";
	offer[1]="32249";
	offer[2]="l-chameau-boot-bag.jpg";
	offer[3]="Le Chameau Wellington Boot Bag";
	offer[4]="Wellington Boot Bag from Le Chameau";
	offer[5]=/Boot Bag.?/i;
	offer[6]=/Chasseur.?|Chasseurnord.?|Veganord.?/i;
	offer[7]="16.98";
	offers.push(offer);*/
	//--2----Hunter Boot Bag--------------
	/*var offer=new Array(8);
	offer[0]="1017";
	offer[1]="30713";
	offer[2]="hunter-boot-bag_sml.jpg";
	offer[3]="Hunter Wellington Boot Bag";
	offer[4]="Wellington Boot Bag from Hunter";
	offer[5]=/Boot Bag.?/i;
	offer[6]=/Royal.?|Sovereign.?|Abernethy.?|Galloway.?|Classic.?|Original.?|Huntress.?|Hunter.?/i;
	offer[7]="17.99";
	offers.push(offer);*/
	//--3---WW Boot Bag---------------
	/*var offer=new Array(8);
	offer[0]="1017";
	offer[1]="31852";
	offer[2]="ww-boot-bag-sml.jpg";
	offer[3]="Wellington Boot Bag";
	offer[4]="Nylon waterproof wellington boot bag with mesh breather to allow natural drying.";
	offer[5]=/Boot Bag.?/i;
	offer[6]=/Neoprene.?|Parcours.?|Benylsport.?/i;
	offer[7]="14.98"
	offers.push(offer);*/
	//--4---Chameau Wood Boot Jack---------------
	/*var offer=new Array(8);
	offer[0]="1464";
	offer[1]="32247";
	offer[2]="l-wooden-boot-jack.jpg";
	offer[3]="Le Chameau Wooden Boot Jack";
	offer[4]="Wooden Boot Jack with rubber jaw.";
	offer[5]=/Boot Jack.?/i;
	offer[6]=/Vega.?|All Tracks.?/i;
	offer[7]="13.98";
	offers.push(offer);*/
	//--5---Chameau Alloy Boot Jack---------------
	var offer=new Array(8);
	offer[0]="1464";
	offer[1]="32248";
	offer[2]="l-ali-boot-jack.jpg";
	offer[3]="Le Chameau Alloy Boot Jack";
	offer[4]="Smart Alloy Boot Jack with Le Chameau Logo.";
	offer[5]=/Boot Jack.?/i;
	offer[6]=/Vierzonord.?/i;
	offer[7]="15.98";
	offers.push(offer);
	//--6---Plastic Boot Jack---------------
	var offer=new Array(8);
	offer[0]="1464";
	offer[1]="30987";
	offer[2]="plastic-boot-jack-sml.jpg";
	offer[3]="Plastic Boot Jack";
	offer[4]="Light-weight boot jack with rubber protective sleeve to prevent damage to boots.  Ideal to carry in car or in your boot bag.";
	offer[5]=/Boot Jack.?/i;
	offer[6]=/Cotton.?/i;
	offer[7]="9.49"
	offers.push(offer);
	//--7---Wooden Boot Jack---------------
	var offer=new Array(8);
	offer[0]="1464";
	offer[1]="30986";
	offer[2]="wooden-boot-jack-sml.jpg";
	offer[3]="Wooden Boot Jack";
	offer[4]="Strong Wooden Boot Jack";
	offer[5]=/Boot Jack.?/i;
	offer[6]=/Neo-Abernethy.?|Neo-Galloway.?|Neo-Classic.?/i;
	offer[7]="12.98"
	offers.push(offer);
	
	//--8---WW Boot Bag Offer---------------
	var offer=new Array(9);
	offer[0]="1251";
	offer[1]="30670";
	offer[2]="ww-boot-bag-sml.jpg";
	offer[3]="-- SPECIAL OFFER --";
	offer[4]="Nylon waterproof wellington boot bag with mesh breather to allow natural drying.";
	offer[5]=/Boot Bag.?/i;
	offer[6]=/Neoprene.?|Parcours.?|Benylsport.?|Royal.?|Sovereign.?|Abernethy.?|Galloway.?|Classic.?|Original.?|Huntress.?|Hunter.?/i;
	offer[7]="4.89"
	offer[8]="9.77"
	offers.push(offer);
	
	//--9---Chameau Wood Boot Jack Offer---------------
	/*var offer=new Array(8);
	offer[0]="1251";
	offer[1]="44725";
	offer[2]="l-wooden-boot-jack.jpg";
	offer[3]="-- SPECIAL OFFER --";
	offer[4]="Wooden Boot Jack with rubber jaw.";
	offer[5]=/Boot Jack.?/i;
	offer[6]=/Vega.?|All Tracks.?/i;
	offer[7]="9.99";
	offer[8]="3.99"
	offers.push(offer);*/

	//--10---Chameau Boot Bag Offer---------------
	/*var offer=new Array(8);
	offer[0]="1251";
	offer[1]="44726";
	offer[2]="l-chameau-boot-bag.jpg";
	offer[3]="-- SPECIAL OFFER --";
	offer[4]="Wellington Boot Bag from Le Chameau";
	offer[5]=/Boot Bag.?/i;
	offer[6]=/Chasseur.?|Chasseurnord.?|Veganord.?|Vega.?|All Tracks.?/i;
	offer[7]="13.98";
	offer[8]="3.00"
	offers.push(offer);*/

	//--2----Hunter Boot Bag Offer--------------
	/*var offer=new Array(8);
	offer[0]="1251";
	offer[1]="44724";
	offer[2]="hunter-boot-bag_sml.jpg";
	offer[3]="-- SPECIAL OFFER --";
	offer[4]="Wellington Boot Bag from Hunter";
	offer[5]=/Boot Bag.?/i;
	offer[6]=/Royal.?|Sovereign.?|Abernethy.?|Galloway.?|Classic.?|Original.?|Huntress.?|Hunter.?/i;
	offer[7]="14.99";
	offer[8]="3.00"
	offers.push(offer);*/


	return offers;

}

//-------------------------------------END SPECIAL OFFERS--------------------------


