/**
 * this is the js version of previous PHP based visitrex
 * these function has to be implemented :
 *
 *  
 * 
How to use :
On client side, place this in the header :
(replace 12345 with your real account number)

<!--  START Visitrex -->
<script type = "text/javascript" src="http://halladesign-new.local/visitrex/js/visitrex.js"></script>
<script type = "text/javascript">
	Visitrex.trackVisit(12345);
</script>
<!--  END Visitrex -->

Last Update : 12/22/2009 6:23 AM
*/
				
		

var Visitrex = {
		xmlhttp:			null,
		doRequestValue : 	null,
		ipAddress:			'1.2.3.4',
		account_id : 		null, // to be passed through  client call in visitre.trackVisit(account_id)
		doGreeting : 		function() {alert('Hi');},
		getDomain : 		function() {
			return document.domain;
		},
	
	
		getDate : function() {
		   	var date = new Date();
		    var the_date =  date.getFullYear()+"-"+(date.getMonth()+1) +"-"+ date.getDate();
		    return the_date;
		},

		getTime: function() {
			var date = new Date();
			return date.toLocaleTimeString();
		},
		
		createXmlhttp : function() {
			var xmlhttp = null;
		
		    if (window.XMLHttpRequest){// code for all new browsers
			      xmlhttp=new XMLHttpRequest();
			  }
			else if (window.ActiveXObject){// code for IE5 and IE6
			      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			 }
			 return xmlhttp;
		},
		/*
		 * 
GET IP FUNCION HAS BEEN SKIPPED AS OF OCT 24TH 2009	

		getIp: function() {
		
			var url =  'http://jsonip.appspot.com/?callback=getip';
			
			
			var xmlhttp = this.createXmlhttp();
			var halla = null;
		    
			if (xmlhttp!=null){
			      xmlhttp.open("GET",url,true);
				  xmlhttp.send(null);
			      xmlhttp.onreadystatechange=function()
						{
							//alert('179 : ' + xmlhttp.readyState + xmlhttp.status);
						    if (xmlhttp.readyState==4){// 4 = "loaded"
						         if (xmlhttp.status==200){// 200 = OK
						             	//xmlhttp.data and shtuff
						            	// ...our code here...
						           
						          		halla = xmlhttp.responseText;
						           		alert(195 + ' ' + halla);
						           		
						           		
						        }else{
						            alert("Problem retrieving data");
						        }
						  }
						};
					      
					    	 
					      alert(xmlhttp.readyState + ' Halla = ' + halla);
			 }else{
			      alert("Your browser does not support XMLHTTP.");
			      return null;
			  }
		
		},
		*/
		
		
		getR: function() {
		  parameter = 'r';
		  var loc = location.search.substring(1, location.search.length);
		  var param_value = false;
		
		  var params = loc.split("&");
		  for (i=0; i<params.length;i++) {
		      param_name = params[i].substring(0,params[i].indexOf('='));
		      if (param_name == parameter) {
		          param_value = params[i].substring(params[i].indexOf('=')+1)
		      }
		  }
		  if (param_value) {
		     return  param_value;
		  } else {
		      
		      return ''; //Here determine return if no parameter is found
		  }
		
		},	
		
		
		
		
		getHostname: function() {
			var hn = window.location.hostname;
			return hn;
		},
		
		getPage: function() {
			return window.location.pathname;
		},
		
		getUrl: function() {
			return document.URL;
		}		,	
		
		
		
		
		getHttpReferer: function() {
			//return 'http://www.google.com/search?q=halladesign&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a';
			return document.referrer;
		},
		
		
		getInfo: function() {
			var str  = this.getIpAddress();
			str 	+= 		this.getHostname();
			str		+=		this.getHttpReferer();
			alert(str);
		},
		
		getSession: function() {
			return 'Visitrex';
		},
		
		getPath: function() {
						var path		=	'http://halladesign.com/visitrex/visits/track/?';
						path 			+=	'domain='+this.getDomain();
						path 			+= 	'&fdate='+ this.getDate();
						path 			+=	'&ftime='+this.getTime();
						//path 			+=	'&ip='+ this.ipAddress;
						path 			+=	'&r='+ this.getR('r');
						path 			+=  '&http_refer='+this.getHttpReferer();
						path 			+= 	'&hostname='+ this.getHostname();
						path 			+=	'&session='+ this.getSession();
						path			+= 	'&page='+ this.getPage();
						path 			+= 	'&the_url='+ this.getUrl();
						path 			+= 	'&accountId='+ this.account_id;
						path 			+= 	'&src=js';
						
						return path;
		},
		doGetRequest : function(url) {
			var this_xmlhttp = this.createXmlhttp();
			
			if (this_xmlhttp!=null){
			    
			      this_xmlhttp.onreadystatechange=this.readyState;
			    //  alert(url);
			      this_xmlhttp.open("GET",url,true);
			      this_xmlhttp.send(null);
			      //alert(this.getRequestValue);
			      return this.getRequestValue;
			 }else{
			      alert("Visitrex : Your browser does not support XMLHTTP.");
			      return null;
			  }
		},
		readyState : function(){
			alert('179 : ' + this.xmlhttp.readyState + this.xmlhttp.status);
		    if (this.xmlhttp.readyState==4){// 4 = "loaded"
		         if (this.xmlhttp.status==200){// 200 = OK
		             	//xmlhttp.data and shtuff
		            	// ...our code here...
		           
		          		this.getRequestValue = this.xmlhttp.responseText;
		           		alert(184 + ' ' + this.getRequestValue);
		        }else{
		            alert("Problem retrieving data");
		        }
		  }else {
		  	// no state
		  	//alert('No state');
		  }
		},
		trackVisit: function(account_id) {
			this.account_id = account_id;
			this.doGetRequest(this.getPath());
			
		}
		
}// eof visitrex object




