﻿// JScript File
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function getHTTPObject() {
  var xmlhttp = false;
  if (window.XMLHttpRequest) {
   xmlhttp = new XMLHttpRequest();
  } else if(window.ActiveXObject) {
   try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (e) {
    try {
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {
     xmlhttp = false;
    }
   }
  }
  return xmlhttp;
 };
 
addLoadEvent(setEvent);
	
function setEvent() {    
    //make sure we have all of the following..."event name", 
    //"event context" and "recipient id" (current user id)
	if(!document.getElementById("EventName") || !document.getElementById("EventContext") || !document.getElementById("RecipientID")) return false;	  
		//alert('Event Set');
        var theEventName = document.getElementById("EventName");						
		theEventName.onchange = function(){execEvent();}		                    	    	    
	                            
	return true;
}

function execEvent() {    
    var theEventName = document.getElementById("EventName");	
    var theEventContext = document.getElementById("EventContext");	
    var theRecipientID = document.getElementById("RecipientID");	   
	
	var xmlhttp = getHTTPObject();
	//PATH to your handler page
	//search root is passed in from the .aspx page for fully qualifing the site root
	xmlhttp.open("POST", searchRoot + "AjaxHandlers/EventHandler.aspx", true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	var params = "EventName=" + theEventName.value + "&EventContext=" + theEventContext.value + "&RecipientID=" + theRecipientID.value;
	xmlhttp.send(params);	
	//alert(theEventName.value  + ' event for ' + theRecipientID.value  + ', description: ' + theEventContext.value + ' completed.');
	return true;	
}
