function URLEncode(URL)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					
	var HEX = "0123456789ABCDEF";

	var plaintext = URL;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
};

/**********************************************************************************
 *  Name:       gateway.js
 *  Author:   Praveen Shenoy
 *  Date:   10/20/03
 *  Description: Redirect to gateway page
 *  Copyright:   
 *
 *
 *  Version History: 1.0 First Draft
 *        2.0 Include all variables returned by Gateway in local cookie   
 *
 **********************************************************************************/

<!--
var brandId="SV";   // Your brand identifier. Coordinate with your content manager to get the correct id.
var serverDomain= "landingpage2.smirnoffice.com"; // change this when you switch from environment to environment
var gatewayUrl = "http://" + serverDomain + "/?BrandId=" + brandId + "&RefUrl=" + URLEncode(window.location.href);
var localCookieName="allowAccess";
var localCookieValue="yes";
var siteName = "smirnoffice.com";

/*
 * cookies returned by gateway
 * RhRemDetails = "0";  // remember details
 * RhLanguage   = "en";  //language
 * RhFlashEnabled = "N"; //flash enabled - Y/N
 * RhReferer
 * RhCountry
 * RhYear
*/

var lcRhRemDetails   = "RhRemDetails";
var lcRhLanguage     = "RhLanguage";
var lcRhFlashEnabled = "RhFlashEnabled";
var lcRhReferer      = "RhReferer";
var lcRhCountry      = "RhCountry";
var lcRhYear         = "RhYear";


/*
 name - name of the cookie
 value - value of the cookie
 [expires] - expiration date of the cookie (defaults to end of current session)
 [path] - path for which the cookie is valid (defaults to path of calling document)
 [domain] - domain for which the cookie is valid (defaults to domain of calling document)
 [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
 * an argument defaults when it is assigned null as a placeholder
 * a null placeholder is not required for trailing omitted arguments
*/
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
 
  document.cookie = curCookie;
 
}

/*
 name - name of the desired cookie
 * return string containing value of specified cookie or null if cookie does not exist
*/
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

/*
 name - name of the cookie
 [path] - path of the cookie (must be same as path used to create cookie)
 [domain] - domain of the cookie (must be same as domain used to create cookie)
 * path and domain default if assigned null or omitted if no explicit argument proceeds
*/
function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" + 
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-1900 00:00:01 GMT";
  }
}

/*
 date - any instance of the Date object
 * hand all instances of the Date object to this function for "repairs"
*/
function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

function isRedirectReqd(userAgent)
{
 var arrRobots = new Array();
 arrRobots[0]  = "googlebot";   //'google 
 arrRobots[1]  = "atomz";   //'atomz search engine 
 arrRobots[2]  = "infoseek";  //'InfoSeek 
 arrRobots[3]  = "gulliver";  //
 arrRobots[4]  = "fido";    //
 arrRobots[5]  = "monster";   //
 arrRobots[6]  = "netcarta";   //
 arrRobots[7]  = "mindcrawler";  //
 arrRobots[8]  = "slurp";   //'search.msn.com; www.hotbot.com 
 arrRobots[9]  = "pageboy";   //
 arrRobots[10] = "lycos";  //
 arrRobots[11] = "spider";  //
 arrRobots[12] = "spyder" ;  //
 arrRobots[13] = "scooter";  //'altavista 
 arrRobots[14] = "vspider";  //
 arrRobots[15] = "webtrends";   //
 arrRobots[16] = "jeeves";   //
 arrRobots[17] = "fast";   //
 arrRobots[18] = "turnitinbot";  //
 arrRobots[19] = "webspider";  //
 arrRobots[20] = "architextspider";  //' Excite
 arrRobots[21] = "backrub";  //' Google experimental 
 arrRobots[22] = "lycos_spider";  //' lycos 
 arrRobots[23] = "zao";    //' zao 
 arrRobots[24] = "msiecrawler";   //
 arrRobots[25] = "google";  //' just in case
 
 userAgent=userAgent.toLowerCase();
 var redirect = true;
 var cookieAllowAccess="";
 var referer="";
 
 /////////////////////////////////////////////
 // Special case for testing in CoActive Environment
 // Added by Kevin Saffer
 // Date: 03/10/2004
 if (window.location.href.indexOf("ids") > 0 ) {
   redirect = false;
 }

 for(ctrRobots=0;ctrRobots<arrRobots.length;ctrRobots++)
 {
  if(userAgent.indexOf( arrRobots[ctrRobots].toLowerCase() ) >= 0 )
  {
   redirect = false; //a spider or index, found allow to pass through
   break;    // break from the loop
  }
 }

 if(redirect)
 {
  //check to see if you can from gateway page
  referer = (window.location.href).toLowerCase();

  if(referer.indexOf("rhremdetails=") >= 0)
  {
   //we have come to this page from the gateway
   //var mydate= new Date(Date() + 10);
   //setCookie - cookiename,value,expiration,path,domain,secure
                       
                        setCookie(localCookieName, localCookieValue, null, "/", siteName, false); //set your local cookies

   /*
   *  get the values returned by Gateway in local cookies
   */
   ourUrl = referer;
   ourUrl = ourUrl.replace("?","&");
   nameValue = ourUrl.split("&");
   for(loop=1;loop < nameValue.length;loop++) // neglect the 0th value as it contains our url
   {
    eval(nameValue[loop].replace("=","='")+"'"); // this will create variables with the names
   }
   if (ssyear > 0) rhyear = ssyear;
   if (gwCountry != null) rhcountry = gwCountry;
   
   setCookie(lcRhRemDetails, rhremdetails, null, null, null, false);
   setCookie(lcRhLanguage, rhlanguage, null, null, null, false);
   setCookie(lcRhFlashEnabled, rhflashenabled, null, null, null, false);
   setCookie(lcRhReferer, rhreferer, null, null, null, false);
   setCookie(lcRhCountry, rhcountry, null, null, null, false);
   setCookie(lcRhYear, rhyear, null, null, null, false);

   if (rhyear > 1900){
      if (rhremdetails == "true"){
         var exp = new Date(); 
         exp.setTime(exp.getTime() + (365*30*24*60*60*1000));
         setCookie("si_year", rhyear, exp, null, null, false);
      }
      else{
         setCookie("si_year", rhyear, null, null, null, false);
      }
   }
   // End Customization: Storing Year for UTC redirect
   
   redirect = false;  // no need to redirect
                        // Redirect to other country sites if necessary
                       
                }
  else
  { 
   //check and see if the cookies are present
   cookieAllowAccess = getCookie(localCookieName); 
   if(cookieAllowAccess)
   {
    cookieAllowAccess=cookieAllowAccess.toLowerCase();
    if(cookieAllowAccess.indexOf(localCookieValue) >=0)
    {
     redirect = false;
    }
   }

  }
  if(redirect)
  { 
   window.open(gatewayUrl,"_self");  // can use window.location.replace if you want to disable back feature
  }
 }

}
isRedirectReqd(navigator.userAgent);

