// the user tracker consists of two parts. 
// setting the x & y coords in 



// this object is included
// at the bottom of everypage that 
// requires user tracking

function bswUserTrackerSetter(){

	// clear cookie to make sure we get new goods

	getBswBrowserInstance().setCookie("c","null");
	this.setHandler();	

}

bswUserTrackerSetter.prototype.setHandler = function(){

	// create an onclick handler for every 
	// anchor link on the site. If a onclick
	// event is already defined, modify the
	// onclick  to include the new functionality 

	var anchors = document.getElementsByTagName("A");

	var o = this;	
	for(a = 0;a < anchors.length;a++){
		
		if(anchors[a].onclick != null){
			
			// create object containing preexisting onlclick  functions....
			anchors[a].predefined = new Object({e:anchors[a].onclick});
			// redefine onclick event... 
			anchors[a].onclick = function(evt){			
				if(evt == null){					
					evt = event;				
				}
				o.plantCookie(o.mx(evt),o.my(evt));							
				this.predefined.e();
				return true;
			}		
		} else { 
				anchors[a].onclick = function(evt){
				if(evt == null){					
					evt = event;				
				}
				o.plantCookie(o.mx(evt),o.my(evt));
				return true;
			}
		}
		
	}
	
}
bswUserTrackerSetter.prototype.plantCookie = function (mouse_x,mouse_y){

	var toPlant = "";
		toPlant += "mx:" + mouse_x + ",";
		toPlant += "my:" + mouse_y + ",";
		toPlant += "cw:" + getBswBrowserInstance().getBaseX() + ",";
		toPlant += "ch:" + getBswBrowserInstance().getBaseY();
		getBswBrowserInstance().setCookie("c",toPlant);
	
	return true;
}


bswUserTrackerSetter.prototype.mx = function(evt){
	
			var x;
			
			if(x = evt.pageX){
				return x;				
			} else if (x = evt.clientX) {
				return x;		
			
			}
			
			return null;
}
	
bswUserTrackerSetter.prototype.my = function(evt){
	
			var y;
			
			if(y = evt.pageY){
				return y;				
			} else if (y = evt.clientY) {
				return y;		
			}
			
			return null;
}




bswUserTrackerPageRequest = new function(){


	this.site_root = null;
	this.isTracking = false;	
	this.saveTracking = true;
	

	this.xmlDoc = (new bswXML()).LoadXML("<page_request><server_controls/></page_request>");
	
	if(this.xmlDoc != null){		
		this.root = this.xmlDoc.documentElement;
		this.isTracking = true;
	}		
	this.setSiteRoot = function (s){
		this.site_root = s;
		this.createNode(this.root,"site_root",s);
	}	
	this.setUrl = function (dynamic_url){
		this.createNode(this.root,"request_url",escape(dynamic_url));
	}	
	this.setAlias = function (){
		this.createNode(this.root,"request_alias",escape(document.location));
	}	
	this.setBrowser = function (){
		this.createNode(this.root,"request_user_agent",escape(navigator.userAgent));
	}
	this.setUserId = function (id){
		this.createNode(this.root,"user_id",id);
	}	
	this.setSessionId = function (id){
		this.createNode(this.root,"session_id",id);
	}
	this.setRequestDate = function (date){
		this.createNode(this.root,"date",date);
	}
	this.setRequestTime = function (time){
		this.createNode(this.root,"time",time);
	}
	this.setGuid = function (guid){
		this.createNode(this.root,"user_guid",guid);
	}
	this.setTitle = function (){
		this.createNode(this.root,"page_title",escape(document.title));
	}	
	this.setUserHostAddress = function(s){
		this.createNode(this.root,"user_host_address",s);	
	}
	this.setUserHostName = function(s){
		this.createNode(this.root,"user_host_address",s);	
	}
	this.createNode = function (parent_node,name,value) {
		
		if(this.isTracking){	
			try{			
		
				var eleElement = this.xmlDoc.createElement(name);
					if(value != null){
						eleElement.appendChild(this.xmlDoc.createTextNode(value));
					}
					parent_node.appendChild(eleElement);
					return eleElement;
			} catch (e) {				
				alert(e);
			}
		}
	
	}
	this.addContentItem = function(request_id,control_id,view_type){
		if(this.isTracking){
			
			try{
				
					var scNode = this.xmlDoc.getElementsByTagName("server_controls")[0];	
					var controlNode = null;
					
					var cts = scNode.getElementsByTagName("server_control");
										
					for(c = 0; c < cts.length;c++){
						var cid = cts[c].getAttribute("id");
						if(cid == control_id){
							controlNode = cts[c];	
						}		
					}	
		
					if(controlNode == null){
						controlNode = this.createNode(scNode,"server_control",null);	
						controlNode.setAttribute("id",control_id);		
					}	
					
					// add item....
							
					var newContentItem = this.createNode(controlNode,"content_item",null);				
						
						this.createNode(newContentItem,"request_id",request_id);
						this.createNode(newContentItem,"view",view_type);		
					
				} catch (e){
				
					alert(e);
				}	
				
		}			
	}
	
	this.saveRequest = function(site_root,user_id,session_id,date,time_hour,time_minute,time_second,user_guid){	
		
			
		
		if(this.saveTracking){		
			
		//	this.setSiteRoot(site_root);
		//	this.setUserId(user_id);
		//	this.setSessionId(session_id);
		//	this.setRequestDate(date);
		//	this.setRequestTimeHour(time_hour);	
		//	this.setRequestTimeMinute(time_minute);
		//	this.setRequestTimeSecond(time_second);			
		//	this.setGuid(user_guid);	
		//	this.setTitle();		
			
			var xmlHttp = new bswXMLHttpRequest();
			var URL = this.site_root + "/tracker.aspx";
			var xmlString = "xml=" +  (new bswXMLSerializer()).xmlToString(this.xmlDoc);	

			try{
				if (xmlHttp != null)
				{	
					// open request connection
					xmlHttp.Open("POST", URL, false);
					xmlHttp.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
					// attempt to send request
					xmlHttp.SendPost(xmlString);
				}
			} catch (e){
				alert(e);
			}
		}
				
	}
	
	
	this.setReferrer = function(){
		
		if(this.isTracking){
			if(getBswBrowserInstance().getCookie("c") != null){
				
				var coords = getBswBrowserInstance().getCookie("c").split(",");
				
				try{
				
					if(coords.length > 1){
				
						var mx = coords[0].split(":")[1];
						var my = coords[1].split(":")[1];
						var cx = coords[2].split(":")[1];
						var cy = coords[3].split(":")[1];
						
						var newReferrer = this.createNode(this.root,"referrer",null);
						
						this.createNode(newReferrer,"url",escape(document.referrer));
						this.createNode(newReferrer,"mx",mx);
						this.createNode(newReferrer,"my",my);
						this.createNode(newReferrer,"cx",cx);
						this.createNode(newReferrer,"cy",cy);	
					}			
					
					
				} catch (e){
					
					// alert(e);
				
				}
			}
		}
		
		
	
	}
	this.setAlias();
	this.setReferrer();	
	this.setBrowser();
	
}




