//REVOLVING DOOR SCRIPT
//(C)2002, by Brett Brewer, All rights reserved.
//
//For support and information, go to www.brettbrewer.com.
//This script may be modified for your purposes, but not redistributed or resold without
//express written consent. I have made this script available at a very low price, so please 
//don't steal it.
//
//This script will preserve the cartID from SoftCart so that users may leave
//and reenter the store without losing their cart items. 
//
//It was written by Brett Brewer. If you need custom JavaScripts, Jscripts, PHP scripts
//or custom behaviors for DreamweaverMX, go to www.brettbrewer.com and send me a message.
//
//BEFORE YOU INSTALL THIS SCRIPT:
//Please note, that in order for this script to work, users must arrive
//at your page via a standard HTTP web link of some sort. You cannot use this script on pages
//that utilize a javascript navigation menu such as a form-based pulldown menus that 
//use "onChange" events to trigger javascript navigation functions
//or other types of javascript navigation methods. However, this should work fine on sites that use
//dynamic menus that create real web links. To tell if your navigation uses standard links
//simply right-click (option-click for Mac users) on one of your menu links and select "Open Link in New Window".
//If the link fails, chances are pretty good that this script won't work for you. Otherwise, read on.
//
//
//
//STEP 1: Set the following variable equal to your SoftCart directory. This should be identical to the value
//        in your sidedoor.js file. Make sure to include the leading slash to specify the path from your root
//        web directory to the softcart executable.
//

var SCPath = "/cgi-local/SoftCart.100.exe";

//
//STEP 2: Set the following variable equal to the name of your store (same as name used in your store administrator).
//

var config = "scstore";

//
//STEP 3: Specify the rootPath variable as your complete domain in format "www.yourdomain.com"
//

var rootPath = "www.danielpendley.com";

//
//STEP 4:
//Here we define a regular expression so we can later search for a referring URL 
//that looks like a valid SoftCart URL. It just defines a pattern to look for
//that will identify your SCPath. You basically just enclose your SCPath in a pair 
//of slashes and then preceed any other slashes or periods with a backward slash. 
//The trailing "i" makes it a case-insensitive search.If you
//can't figure this out, just contact me via the web address above and I'll be happy to send you one for your
//particular SCpath. Here are some regular expression examples for various typical SCPaths:
//
//if your SCPath = "/cgi-local/SoftCart.exe" then your
//var myRE = /\/cgi-local\/SoftCart\.exe/i;
//
//if your SCPath = "/cgi-bin/SoftCart.exe" then your
//var myRE = /\/cgi-bin\/SoftCart\.exe/i;
//

var myRE = /\/cgi-local\/SoftCart\.100\.exe/i;

//
//STEP 5: Save this script in the root web directory of your site.
//
//STEP 6: Add the following line to the <HEAD> section of each of your non-SoftCart pages. 
//        <script language="JavaScript" src="/revolvingdoor.js" type="text/JavaScript"></script>
//


//DO NOT TOUCH ANYTHING BELOW THIS LINE UNLESS YOU KNOW YOUR JAVASCRIPT.
//______________________________________________________________________

var cartID;
var theArray;
var referringURL = document.referrer;
	
//Here, we make sure there actually is a referring address. If the user enters the page through a form
//selection menu or a javascript "Jump Menu" such as those in Dreamweaver, this will not work because 
//there is no referrer set in such a link. If you use this script on such a page, you must add some 
//javascript to the jump menu to set the referrer equal to whatever you need

if(referringURL!=""){
	
//Then we verify that the referring page URL contained a Softcart ID and if so, we extract the cartID
//which we assume is the third parameter delimited by a "+". This is where we need that regular expression
//we defined earlier. It basically says, if we see the SoftCart directory and .exe file in our referring URL
//and the url does not start with the word "file" and there is a "?" in the URL denoting URL parameters which will
//hopefully contain a cart id, then we go ahead and mess with the URL.

	if((referringURL.search(myRE) != -1) && (referringURL.substring(0,4) != "file") && ( referringURL.indexOf("?") != -1)){
		
		//Split the referring URL into its components and save them in an array called "theArray".
		theArray=referringURL.split("+");
	
		//The cartID should hopefully be the third string in the zero-based array.
		cartID=theArray[2];
	
		//Here we replace the current URL with a URL that contains the cartID as a parameter.
		//If you have additional URL parameters to preserve, then chances are, you
		//can figure out how to modify this script to preserve them too. 
		
		window.location.replace(location.pathname + "?cartID=" + cartID);

	}
	
	//Now we check to see if the referring URL contains a preserved cartID parameter from another non-SoftCart page
	//Note that this time, we actually search for a literal string "cartID" rather than a regular expression. Since
	//SoftCart doesn't use a URL parameter called cartID within the store, we know that if the referrer has the word
	//"cartID" in the URL, then it must be a preserved cart from another non-SoftCart page. Not that it matters.
	
	else if((referringURL.search("cartID") != -1) && (referringURL.substring(0,4) != "file") && ( referringURL.indexOf("?") != -1)){
		
		//Split the referring URL into its components, saving them in theArray.
		//Since we are just splitting it at the beginning of the URL parameters,
		//this should preserve as many parameters as there happen to be. In fact, you could
		//get rid of the first two conditions in the if clause above and just look for the "?"
		//denoting the the presence of any URL parameters at all. We don't really care what they are. 
		
		theArray=referringURL.split("?");
	
		//Now all the URL parameters should be in the second element of the zero-based array, so we
		//just add them all back. The fact that we are calling this variable cartID here is of no
		//consequence and probably obscures the logic a bit.
		
		cartID=theArray[1];
	
		//Replace the current URL with a URL that contains the rescued URL parameters.
		
		window.location.replace(location.pathname + "?" + cartID);

	}
}





/*example of a refferring URL:
http://www.realestate-school.com/cgi-local/SoftCart.100.exe/online-store/scstore/scpages/showcart.html?P+scstore+qzqh5275ffc420c4+1044974841 */
// actual URL begins at position 7