var today = new Date();
today.setTime( today.getTime() );
today.setHours(today.getHours()+1);
today.setMinutes(0);
today.setSeconds(0);
var ser_directory;


function AdspayTrackVisiters(ser_dir)
{
	
 ser_directory=ser_dir;
window.setInterval('UpdateAdspayAnalytics()',5000);


}



function UpdateAdspayAnalytics()
{
	
	
	
	var url=ser_directory;//"http://valiyapalli.com/adspay_renamed/adspay_adserver/";

	var get_ad_id=Get_Cookie('adspay_adid');
    var ids="";
	var clk="";
	var ids_pos="";
	var expr="";
	var dur="";
	
	//alert(get_ad_id);
	
	if(get_ad_id !=null)
	{
		ids_pos=get_ad_id.split("_");
		ids=ids_pos[0];
		clk=ids_pos[1];
		expr=ids_pos[2];
		dur=ids_pos[3];
		
		var expires_date =	expr;
			
	//	alert(dur);
		
	}

	else
	{
		expires = 12 * 1000 * 60 * 60 ;
		var expires_date = new Date( today.getTime() + (expires) );
		
		
	}

p=utf8_encode(document.location.href);
//alert(url+"statisticscode-execution.php?loc="+p+"&coo="+ids+"&clicks="+clk+"&dur="+dur+"&t"+new Date().getTime());
 // document.getElementById('adspay_script').src = url+"statisticscode-execution.php?loc="+p+"&coo="+ids+"&clicks="+clk+"&dur="+dur+"&t"+new Date().getTime();


var old = document.getElementById('adspay_script');
   if (old != null) {
     old.parentNode.removeChild(old);
     delete old;
   }
   var head = document.getElementsByTagName("head")[0];
   var script = document.createElement('script');
   script.id = 'adspay_script';
   script.type = 'text/javascript';
   script.src = url+"statisticscode-execution.php?loc="+p+"&coo="+ids+"&clicks="+clk+"&dur="+dur;
   head.appendChild(script);

//alert(document.getElementById('adspay_script').src);


										
								
										 											 
				


	
}


function LoadStatisticsData(data)
{
	
	var get_ad_id=Get_Cookie('adspay_adid');
  
	
	var ids_pos="";
	var expr="";
    var expires_date;
	
	
	
	if(get_ad_id !=null)
	{
		ids_pos=get_ad_id.split("_");
		
		
		expr=ids_pos[2];
		
		
		 expires_date =	expr;
			

		
	}

	else
	{
		expires = 12 * 1000 * 60 * 60 ;
		 expires_date = new Date( today.getTime() + (expires) );
		
		
	}
	
	                                                                                   var da="";
																															
																						if(data.dss != -1)
																						{
																						if( get_ad_id !=null)
																						{
																										
																								
																							if(data.dur !="")
																							document.cookie = "adspay_adid="+escape(get_ad_id)+escape(data.dur)+";expires=" + expires_date;
																																						 
																						}
																						 
																						 else
																						 {
																							
																						  da=data.ad_id+"_"+data.click_id+"_"+expires_date+"_"+data.dur ; 
																						  Set_Cookie( 'adspay_adid',da, expires_date) ;
																						 }
																						 
																						}										 
															
	
	
}
	




function	utf8_encode  (string) 
{
	string = string.replace(/\r\n/g,"\n");
	var utftext = "";

	for (var n = 0; n < string.length; n++) 
	{

		var c = string.charCodeAt(n);

		if (c < 128) 
		{
			utftext += String.fromCharCode(c);
		}
		else if((c > 127) && (c < 2048)) 
		{
			utftext += String.fromCharCode((c >> 6) | 192);
			utftext += String.fromCharCode((c & 63) | 128);
		}
		else 
		{
			utftext += String.fromCharCode((c >> 12) | 224);
			utftext += String.fromCharCode(((c >> 6) & 63) | 128);
			utftext += String.fromCharCode((c & 63) | 128);
		}

	}

	return escape(utftext);
}
 
function Get_Cookie( name )
 {
	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 Set_Cookie( name, value, expires_date, path, domain, secure ) 
{	




//alert(expires_date);
	document.cookie = name + "=" +escape( value ) +

		 ";expires=" + expires_date.toGMTString()  + //expires.toGMTString()

		( ( path ) ? ";path=" + path : "" ) + 

		( ( domain ) ? ";domain=" + domain : "" ) +

		( ( secure ) ? ";secure" : "" );

}

/*

function delete_cookie ( cookie_name )
{
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

*/

