///////////////////////////////////////////////////////////////////////////////
//Author: ARC World Wide

//Date: 10/10/2004

//Modified Date: 11/30/2004

//Purpose: 

//Set a new cookie on User's computer if the cookie doesnot exist.
//Read the cookie and cookieid if cookie file does exist
//Set a date when the cookie will expire and new cookie will be set

//Code Version:2.0

///////////////////////////////////////////////////////////////////////////////

	 
function readID() 
	{
		var expDays = 10*365; // number of days the cookie should last
		var expDate = new Date();
		var pageexpDate= new Date();
		var rightNow = new Date();
		
		//This is where actual expire date is set
		expDate.setTime(expDate.getTime() +  (24 * 60 * 60 * 1000 * expDays)); 
		pageexpDate.setTime(pageexpDate.getTime()+  (24 * 60 * 60 * 5 * 1));
		var id = GetCookie('cadillac'); //Check if the cookie exist or not
		if (id == null || id == "no id") 
		{
			var pageCount=0
			var visitCount = 0
			var randomNumber=Math.floor((Number(new Date())-Math.floor(Math.random()*999999999999)))+'-'+Math.floor(Math.random()*99999999999)+'-'+Math.floor(Number(new Date())) // Unique random number which will be used a cookie id
			//return randomnumber;
			//the first parameter is the name of the cookieid used in iPlanet webserver to caputer the id value
			//the second parameter is the actual random cookieid that gets generated when users visits the page for the first time
			//the third parameter is the expiry date when the cookie will expire
			SetCookie('cadillac', randomNumber, expDate);
			SetCookie('When',rightNow.getTime(),expDate);
			SetCookie('VisitCount',parseInt(visitCount+1),expDate);
			if (document.referrer != null && document.referrer.indexOf('cadillac') == -1)
			{	
				SetCookie('Referrer',document.referrer,expDate);
			}
			SetCookie('PageCount',parseInt(pageCount+1),pageexpDate);
		}
		else
		{
			//If the PageCount cookie is expired initialize the variable with default value
			if(GetCookie('PageCount')=='NaN' || GetCookie('PageCount')== null)
			{
				var pageCount=0	
			}
			else
			{
				var pageCount=GetCookie('PageCount');
			}
				var visitCount=GetCookie('VisitCount');
				var randomNumber=GetCookie('randomNumber');
				SetCookie('cadillac', randomNumber, expDate);
				SetCookie('When',rightNow.getTime(),expDate);
				
			//Set this cookie on only first page of the site visit
			if (parseInt(pageCount)==0)
			{
				SetCookie('VisitCount',parseInt(visitCount)+1,expDate);			
			}		
				SetCookie('PageCount',parseInt(pageCount)+1,pageexpDate);
			if (document.referrer != null && document.referrer.indexOf('cadillac') == -1)
			{	
				SetCookie('Referrer',document.referrer,expDate);
			}	
				
			//Following value should be Tool:Model:CompleteStatus. 
			//This is the first tool that visitor used and this value should neverchange
			//SetCookie('FirstMeasTool','');											
			//This is the first tool that visitor used and this value should nevercahge
			//if(GetCookie('FirstMeasTool')=='NaN' || GetCookie('FirstMeasTool')== null)
			//SetCookie('FirstMeasTool','');
			
			//This is the first Model visitor viewed in the Gallery	
				SetCookie('FirstMeasModel','');
				
			//Following value should be Tool:Model:CompleteStatus. 
			//This current tool that visitor used and this value should change
				//SetCookie('CurrentMeasTool','');
				
			//This is the first Model visitor viewed in the Gallery		
				SetCookie('CurrentMeasModel','');
		}
	}
//////////////////////////////////////////////////////
//Get the value of the cookie id
//////////////////////////////////////////////////////
function getCookieVal (offset) 
	{  
		var endstr = document.cookie.indexOf (";", offset);  
		if (endstr == -1)    
		endstr = document.cookie.length;  
		return unescape(document.cookie.substring(offset, endstr));
	}
//////////////////////////////////////////////////////////////////////////////////////
//Read the cookie and make sure it matches the name of the cookie
//It accepts the "name" parameter which is basically the name of the cookieid("northern")
//////////////////////////////////////////////////////////////////////////////////////
function GetCookie (name) 
	{  
		var arg = name + "=";  
		var alen = arg.length;  //This is the length of name of the cookieid
		var clen = document.cookie.length;  
		var i = 0;  
		while (i < clen)
		{    
			var j = i + alen;    
			if (document.cookie.substring(i, j) == arg)      
			return getCookieVal (j);     
			i =	document.cookie.indexOf(" ", i) + 1;    
			if (i == 0) break;   
		}  
			return null;
	}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Set the new cookie
//the first parameter is the name of the cookieid used in iPlanet webserver to caputer the id value
//the second parameter is the actual random cookieid that gets generated when users visits the page for the first time
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	function SetCookie (name, value) 
	{  
		var argv = SetCookie.arguments; 
		var argc = SetCookie.arguments.length;  
		//var expires = new Date();
		var expires = (argc > 2) ? argv[2] : null;  
		var path = (argc > 3) ? argv[3] : null;  
		var domain = (argc > 4) ? argv[4] : null;  
		var secure = (argc > 5) ? argv[5] : false;  
		document.cookie = name + "=" + escape (value) + 
		((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
		//the path is set as same on all the pages so that every page can share the same cookie
		("; path=" + "/") +  
		((domain == null) ? "" : ("; domain=" + domain)) +    
		((secure == true) ? "; secure" : "");
	}
readID();