var _cookie_domain = ".outerbanksrentals.com";
var _ga_url = "http://www.guestadvantage.com/";
var _chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
function getSID()
{ 
	_sid = getCookie('GASID');
	if (_sid == null)
	{ 
		_sid = "";
		for(i=0;i<20;i++)
		{
			_sid += _chars.charAt(Math.floor(Math.random() * 62.0));
		}
		_sid += makeTimestamp(new Date());
		setCookie('GASID',_sid);
	}
	return _sid;
}	 

function makeTimestamp(myDatetime)
 {
   var year = "" + myDatetime.getYear();
   var month = myDatetime.getMonth();
   var day = myDatetime.getDay();
   var hour = myDatetime.getHours();
   var minute = myDatetime.getMinutes();
   var second = myDatetime.getSeconds();
   var temp = year.substr(year.length-2,2);
   temp += ((month < 10) ? "0" : "") + month;
   temp += ((day < 10) ? "0" : "") + day;
   temp += ((hour < 10) ? "0" : "") + hour;
   temp += ((minute < 10) ? "0" : "") + minute;
   temp += ((second < 10) ? "0" : "") + second;
   return temp;
}

// Sets cookie values. Expiration date is optional

//
function setCookie(name, value, expire)
{

   if(expire==null)
   {
  		expire = new Date();
   	expire.setTime(expire.getTime() + 1000*60*60*24*365);
   }
   document.cookie = name + "=" + escape(value)
   	+ "; path=/"
   	+ "; domain=" + _cookie_domain
   	+ "; expires=" + expire.toGMTString();
}

function getCookie(name)
{
   var search = name + "=";
   if (document.cookie.length > 0)
   { // if there are any cookies
      offset = document.cookie.indexOf(search) ;
      if (offset != -1)
      { // if cookie exists 
         offset += search.length;
         // set index of beginning of value
         end = document.cookie.indexOf(";", offset);
         // set index of end of cookie value
         if (end == -1) 
            end = document.cookie.length;
         return unescape(document.cookie.substring(offset, end));
      } 
   }
   else
   	return null;
}

function generateGAURL(parmname,parmvalue,URLImpParm,objID)
{
  var strloc = location.href;
  var strPos = strloc.indexOf(parmname);
  var IFrameObj = getElementFromID(objID);
  var SessID = getSID();
  if(strPos == -1)
    {
      //set the iframe src with the login url
      var GAURL = "http://www.guestadvantage.com/members/login.html?" + URLImpParm + "&SESSIONID=" + SessID;
    }
  else
    {
      strPos = strPos + parmname.length + 1;
      var endPos = strloc.indexOf("&",strPos);
      if(endPos == -1){endPos = strloc.length;}
      if(strloc.substring(strPos,endPos) == parmvalue)
        {
          //set iframe src with file that awards points
          var GAURL = "http://www.guestadvantage.com/points/process-points.html?" + URLImpParm + "&SESSIONID=" + SessID;
        }
      else
        {
          //set the iframe src with the login url
          var GAURL = "http://www.guestadvantage.com/members/login.html?" + URLImpParm + "&SESSIONID=" + SessID;
        }

    }

  var ResInfo = getCookie("UserInfo");
  GAURL = GAURL + "&" + ResInfo;
  IFrameObj.src = GAURL;
}

function getElementFromID(ID)
{
  if(document.layers)   //NN 4+
    return document.layers[ID];
  else if(document.all) //IE 4
    return document.all[ID];
  else
    return document.getElementById(ID); //new browsers
}

