//	To store the xmlhttpobject for Ajax request
var xmlHttp;

//	This boolean variable tells whether the Ajax request should happen or not
var stoptracking=false;

//	This has been declared jus for the sake namespacing
var com = {};
com.heymath  ={};

//	This function gathers the required information and makes a Ajax call to store those information into a database
com.heymath.LessonTracker=function(param_featureID, param_itemID, param_source, param_urlDomain)
{

		if (stoptracking)
		{			
			return;
		}
		
		var useragent = navigator.userAgent;
		var refer = get_refer();
		
		var featureID = param_featureID;
		if(featureID == undefined || featureID == null || featureID == 'null')
			featureID = get_featureID();
		
		var itemID = param_itemID;
		if(itemID == undefined || itemID == null || itemID == 'null')
			itemID = get_itemID();
		
		var source = param_source;
		if(source == undefined || source == null || source == 'null')
			source = get_source();
		
// postdata contains the data in a concatenated format				
		var postdata="useragent="+useragent+"&";
		postdata+="url1="+location.href+"&";
		postdata+="ref_url="+refer+"&";
		postdata+="featureID="+featureID+"&";
		postdata+="itemID="+itemID+"&";
		postdata+="source="+source;
	
		
		
//	GetXmlHttpObject() function is called which returns the XmlHttpObect		
		xmlHttp=GetXmlHttpObject();

		if (xmlHttp==null)
		{
			  alert ("Your browser does not support AJAX!");
			  return;
		}
		try
		{
			if(param_urlDomain == undefined || param_urlDomain == null || param_urlDomain == 'null')
				param_urlDomain = "";
				
			var url = param_urlDomain + "/servlet/lessontracking.LessonTracking";	//	The servlet to which the information is sent
			
			xmlHttp.open("POST",url,true);	
			
			xmlHttp.onreadystatechange = function(){};	
			
			xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			
			xmlHttp.setRequestHeader("Content-length", postdata.length);
			
			xmlHttp.setRequestHeader("Connection", "close");
			
			xmlHttp.send(postdata);

			return;
			
			
		}
		catch(err)
		{
				//alert(err.description);
				return;
		}


//	This function creates and returns the XmlHttpRequest depending upon the browser support
function GetXmlHttpObject()
{
		var xmlHttp=null;
	try
	  {
		  // Firefox, Opera 8.0+, Safari
		  xmlHttp=new XMLHttpRequest();
	  }
	catch (e)
	  {
		  // Internet Explorer
		  try
			{
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}
		  catch (e)
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
	  }
	return xmlHttp;
}

//	This function fetches values from all the featureId tags and returns them as a concatenated string
function get_featureID()
{
	var featureID=null;
	var fidList = document.getElementsByTagName("nhm_featureID");
	for (var i=0;i<fidList.length;i++)
		{ 	if(featureID==null)	
			{
				featureID = fidList[i].getAttribute("value");
			}
			else
			{
				featureID += ","+fidList[i].getAttribute("value");
			}
  		}
		return featureID;
}

//	This function fetches values from all the itemId tags and returns them as a concatenated string
function get_itemID()
{
		var itemID=null;
		var itemidList = document.getElementsByTagName("nhm_itemID");
		for (var i=0;i<itemidList.length;i++)
		{ 	if(itemID==null)	
			{
				itemID = itemidList[i].getAttribute("value");
			}
			else
			{
				itemID += ","+itemidList[i].getAttribute("value");
			}
  		}
		return itemID;
}

//	This returns the referer URL
function get_refer()
{
		var refer = encodeURI(document.referrer);
		if(refer.length<=0)
		{
			refer="0";
		}

		return refer;
}

//This function returns the sourcename through which the lessons are accessed.
function get_source()
{
	
	var sourceList=document.getElementsByTagName("nhm_source");
	var source1="null";
	try
	{
		source1=sourceList[0].getAttribute("value");
	}
	catch(err)
	{

	}
	return source1;
}

};

