/* javascripts for Barrington Coffee v2 */
/* bh 2007 */



	function writeLinkBackFromThankYouPage()
	{
		// will create a Return to Shopping link (e.g. on the Thank You page) that takes you to either Joe or BCRC-proper shopping pages
		var linkToBCRC 		= '<a href="http:\/\/www.barringtoncoffee.com\/shop.html"> Barrington Coffee Roasting Company <\/a>';
		var linkToJoe  		= '<a href="http:\/\/www.barringtoncoffee.com\/joe.html"> Joe"s Products <\/a>';

		var acctCookieName 	= "merchantz";			
		var merchantzcookie = new String( readCookie(acctCookieName) );
		
		if( merchantzcookie.match( "skin=joe" ) )
		{
			document.write( linkToJoe );						
		}
		else
		{
			document.write( linkToBCRC );
		}
	}




	function writeContinueShoppingLink()
	{
		// will create a Continue Shopping link (e.g. in the cart) that takes you to either Joe or BCRC-proper shopping pages
		var linkToBCRC 		= '<a href="http:\/\/www.barringtoncoffee.com\/shop.html"> << Continue Shopping <\/a>';
		var linkToJoe  		= '<a href="http:\/\/www.barringtoncoffee.com\/joe.html"> << Continue Shopping <\/a>';

		var acctCookieName 	= "merchantz";			
		var merchantzcookie = new String( readCookie(acctCookieName) );
		
		if( merchantzcookie.match( "skin=joe" ) )
		{
			document.write( linkToJoe );						
		}
		else
		{
			document.write( linkToBCRC );
		}
	}



function writeLogoutLink()
{
	var htmlToInsert = '<div><a href="http:\/\/www.barringtoncoffee.com\/WebObjects\/Merchantz.woa\/wa\/Account\/Logout?store=1000021">logout<\/a><\/div>';
	var acctCookieName = "merchantz";
		
	if( cookiesAllowed() )
	{
		var merchantzcookie = new String( readCookie(acctCookieName) );
		if( merchantzcookie.match( "loggedin=true" ) )
		{
			document.write( htmlToInsert );						
		}
	}
}

function setSkin( skinName )
{
	// NOTE:
	// This skin-handling approach currently fails if users go to barringtoncoffee.com (without the 'www'),
	// so for the moment we're simply redirecting at the server side.
	// Anyone going to barringtoncoffee.com will be redirected to www.barringtoncoffee.com
	// Yay. But let's fix this anyway.
	
	var cookieName = "merchantz";
	var daysTillExpires = 2;
	
	// TO DO: we should trap for blank skinName arg here
	var skinPair = "skin=" + skinName;

	if( cookiesAllowed() )
	{
		// get current value of merchantz cookie
		var merchantzcookie = new String( readCookie( cookieName ) );
		
		// watch out for null values returned as strings
		if( !merchantzcookie.match('null') && merchantzcookie!="" )
		{
			var skinAdded = false;
			var newCookieValue = "merchantz=";
			
			// go through pairs looking for a skin setting
			var pairs = merchantzcookie.split( '&' );
			for( var i=0; i<pairs.length; i++ )
			{
				if( pairs[i].match( "skin" ) )
				{
					// skin arg is present, so just replace this particular pair
					pairs[i] = "skin=" + skinName;
					skinAdded = true;
				}
				
				// add ampersand if we're past the first pair
				if( i > 0 ) {
					newCookieValue += "&";}
				
				// add next pair onto string
				newCookieValue += pairs[i];
			}
			
			if( !skinAdded )
			{
				// skin value was not previously set, so we set it now
				newCookieValue += "&" + skinPair;				
			}
		}
		else
		{
			// cookie was basically blank or corrupt, so we just overwrite it
			newCookieValue = "merchantz=" + skinPair;
		}
		
		// write the cookie back
		document.cookie = newCookieValue + '; path=/';
		// alert( newCookieValue );
		//createCookie( cookieName, newCookieValue, daysTillExpires )
	}
	else
	{
		// what to do if cookies are not allowed?
		// alert( "" ); 
	}
	
}




/* ROLLOVER IMAGES */
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_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;
}

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];}
}
