/********************************************************************************
	Name		    -->cookie_Script.js
	Purpose		    -->all function required to handle the cookie
	Date Of Creation    -->02-04-07
	Date Of Modification-->
/********************************************************************************/

function setVisitorID()
{

	/**********************************************************************************************
		Function Name	--> setVisitorID
		Purpose		--> To Check for cookie and create it
		Input 		--> nome
		Ouput		--> none
		Called By	--> index.html
	***********************************************************************************************/

   if (Get_Cookie('VisitorID'))						// if the cookie is already set then
   {
       	   var VisitorID = Get_Cookie('VisitorID');		 	// get the time when the user login
	   var currTime=new Date();
	   currTime=currTime.getTime();					// get the current time
	   var difference=(currTime-VisitorID)/1000;			//get the difference between the login time and current time

   	   var tmp_flt_diffHours=difference/3600;
	  //if the last visit is more then 2 hours then update the time to current value
	  	if(tmp_flt_diffHours>2)
	   {
			Delete_Cookie('VisitorID');			//delete the existing cookie and create the new cookie
			Set_Cookie('VisitorID',new Date());
	   }
   }
   else									//if user enter the page first time then set a cookie
   {
   	expire_time=new Date();
   	one_day= 1000 * 60 * 60 * 24;					//cookie will expire after 1 day
   	expire_time.setTime(expire_time.getTime()+ one_day);
       Set_Cookie('VisitorID',new Date(),expire_time);
   }
}


function Set_Cookie(name,value,expires,path,domain,secure)
{
	/**********************************************************************************************
		Function Name	--> Set_Cookie
		Purpose		--> Function to set a cookie
		Input 		--> name	-> name of cookie
				    value	-> value of cookie
				    expires	->expire value of cookie
				    domain	->domain name
		Ouput		--> none
		Called By	--> setVisitorID()
	***********************************************************************************************/
 	time=value.getTime();

       var cookieString = name + "=" +escape(time) +
       ( (expires) ? ";expires=" + expires.toGMTString() : "") +
       ( (path) ? ";path=" + path : "") +
       ( (domain) ? ";domain=" + domain : "") +
       ( (secure) ? ";secure" : "");
	    document.cookie = cookieString;


}



function Get_Cookie(name)
{
	/**********************************************************************************************
		Function Name	--> Get_Cookie
		Purpose		--> Function to check wether cookie already exists or not
		Input 		--> name	-> name of cookie
		Ouput		--> none
		Called By	--> setVisitorID()
	***********************************************************************************************/

	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) &&( name != document.cookie.substring( 0, name.length) ) )
	{
		return null;
	}
	if ( start == -1 )
	return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 )
	end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}


function Delete_Cookie(name)
{
	/**********************************************************************************************
		Function Name	--> Delete_Cookie
		Purpose		--> Function to delete the cookie
		Input 		--> name	-> name of cookie
		Ouput		--> none
		Called By	--> setVisitorID()
	***********************************************************************************************/

	if ( Get_Cookie( name ) ) document.cookie = name + "=" +
	";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
function read_cookie(frmName)
{

	/**********************************************************************************************
		Function Name	--> read_cookie
		Purpose		--> Function to get the value of cookie
		Input 		--> none
		Ouput		--> none
		Called By	--> ideascatlog.php
	***********************************************************************************************/
	 var tmp_arr_cookie=document.cookie.split(';');
	 var nameEQ="VisitorID=";
	 for(i=0;i<tmp_arr_cookie.length;i++)
	 {
		 var tmp_str_nameValue=tmp_arr_cookie[i];
		 var c=tmp_str_nameValue.indexOf(nameEQ);
		 var len=c+nameEQ.length;

		 if(c!=-1)
		 {

			 tmp_str_dateTime=tmp_str_nameValue.substring(len,tmp_str_nameValue.length);

			 tmp_str_currDateTime=new Date();

			 tmp_str_currDateTime=tmp_str_currDateTime.getTime();

			 var difference= (tmp_str_currDateTime-tmp_str_dateTime)/1000;

			 var tmp_flt_diffHours=difference/3600;
			 var tmp_int_diffHours=parseInt(tmp_flt_diffHours);
			 diff_hours=tmp_flt_diffHours-tmp_int_diffHours;

			 var tmp_flt_diffMins= diff_hours*60;
			 tmp_int_diffMins=parseInt(tmp_flt_diffMins);
			 var diff_mins=tmp_flt_diffMins-tmp_int_diffMins;



			 difference_secs=parseInt(diff_mins*60);

			 if(tmp_int_diffHours<10)
			 {
			 	tmp_int_diffHours="0"+tmp_int_diffHours;
			 }
			 if(tmp_int_diffMins<10)
 			 {
			 	tmp_int_diffMins="0"+tmp_int_diffMins;
			 }
			 if(difference_secs<10)
			 {
			 	difference_secs="0"+difference_secs;
			 }
			 tmp_str_logonTime=tmp_int_diffHours+":"+tmp_int_diffMins+":"+difference_secs;

			 eval('document.'+frmName+'.Time_Spent_on_site.value=tmp_str_logonTime');

				break;
		}
	}

}