<!--
function Querystring(qs) { // optionally pass a querystring to parse

 this.params = new Object();

 this.get=Querystring_get;

 if (qs == null)

  qs=location.search.substring(1,location.search.length );

 if (qs.length == 0) return;

 qs = qs.replace(/\+/g, ' ');

 var args = qs.split('&');

  // parse out name/value pairs separated via &

 for (var i=0;i<args.length;i++) {

  var value;

  var pair = args[i].split('=');

  var name = unescape(pair[0]) ;

  if (pair.length == 2)

   value = unescape(pair[1]);

  else

   value = name;

  this.params[name] = value;
 }
}

function Querystring_get(key, default_)
{
 // This silly looking line changes UNDEFINED to NULL

 if (default_ == null) default_ = null;

 var value=this.params[key];

 if (value==null) value=default_; 

 return value;
}



// Function to generate the correct URL from the BrowseInside URL so that it can be passed to newsstand / bookreader.
// Also cookies the machine based on user id.
function insideview()
{
   var qs2 = new Querystring();

   // Grab the pguid variable from the query string.
   var v1 = qs2.get("pguid");

   // Grab the user's id from the query string.
   var v2 = qs2.get("user");

   // Grab the ISBN from the query string.
   var v3 = qs2.get("isbn");  

   var guidToUse;

   // START OF COOKIE PROCEDURE.
   // We were provided with a user id, so lets cookie the machine.
   // This will be hardcoded until user credentials are implemented.
   if (v2 == '12345') 
   {

	   var cookieValue = "";

	   var name = 'BI';

	   var search = name + "=";

	   var value_set ="N";

	   if(document.cookie.length > 0)
	   { 
		   offset = document.cookie.indexOf(search);

		   if (offset != -1)
		   { 
			 offset += search.length;

			 end = document.cookie.indexOf(";", offset);

			 if (end == -1) end = document.cookie.length ;

			 cookieValue = unescape(document.cookie.substring(offset, end));

			 v2 = cookieValue;

			 value_set = "Y";
		   }    
		} 

		if (value_set != "Y")

		{
		   // cookie not found - set it

		   var today = new Date();

		   today.setTime( today.getTime () );

		   expires = 365 * 1000 * 60 * 60 * 24;

		   var expires_date = new Date( today.getTime() + (expires) );   

		   cookieValue = Math.floor(Math.random()*100000001);

		   document.cookie = name + "=" + escape(cookieValue) + "; expires=" + expires_date; 

		   v2 = cookieValue;
		}
	 } 
	 // END OF COOKIE PROCEDURE.

   
   // The basic newsstand URL. All attributes will need to be appended to this.
   var root;
   root = "http://software.newsstand.com/bookrdr/live/Reader.swf?pguid=";
   
   var url;

   if (v1 == '1003' || v1 == null) // The GUID was not passed in (or in some older implementations was passed in as '1003' directly.
   {
        // This means the WIDGET called the URL. We need to pass in the GUID '1002' and the ISBN.
		guidToUse = '1003';
		url = root + guidToUse + "&isbn=" + v3;
   }
   else
   {
        // This means the BUTTON called the URL. We need to pass in the iternal corporate GUID, the user ID (currently hard coded), and the ISBN.
		guidToUse = '9870526125713272';
		url = root + guidToUse + "&user=" + v2 + "&isbn=" + v3;
   }
   return(url);
}
-->
