
function NotificationCB(a) {

 // init code run upon New
    var req;
	var id=a.id;
	var eContainer=$(a.container);
	var confFunc = a.confFunc;
	var url="/notify/exist/?" + a.eventIDstg;
	var key=a.eventIDstg+a.eventdesc;
	var eventdesc=a.eventdesc;
	var val=0;
	var CheckBoxID="watchcb" + a.id;
	var cb='<input type="checkbox" name="' + CheckBoxID + '" id="' + CheckBoxID + '">' + a.chkboxLabel + ' ';
	eContainer.innerHTML=cb;

	// onClick toggles watch on/off
	document.getElementById(CheckBoxID).onclick = function() {
			var wcb = document.getElementById(CheckBoxID);
			wcb.disabled=true;  // because window.open is async, we can get multiple clicks. so let popup form re-enable
			if (wcb.checked) {
				SetWatch(1);
			} else {
				// they are un-checking, so delete watch
				SetWatch(0);
			}
		};
	
	var XMLHttpFactories = [
		function () {return new XMLHttpRequest()},
		function () {return new ActiveXObject("Msxml2.XMLHTTP")},
		function () {return new ActiveXObject("Msxml3.XMLHTTP")},
		function () {return new ActiveXObject("Microsoft.XMLHTTP")}
	];

	/*
	function createXMLHTTPObject() {
		var xmlhttp = false;
		for (var i=0;i<XMLHttpFactories.length;i++) {
			try {
				xmlhttp = XMLHttpFactories[i]();
			}
			catch (e) {
				continue;
			}
			break;
		}
		return xmlhttp;
	}
	*/
	
    function AjaxRequest() {
      if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
      } else if (window.ActiveXObject) {
        return new ActiveXObject("Microsoft.XMLHTTP");
      }
    }

    function processRequest () { // gotWatch
      if (req.readyState == 4) {
        if (req.status == 200) {
			eContainer.style.display="inline"; // or ""
		 // If the EXISTS div in the repsonse contains "1" then the watch record exists
		    var rsp=req.responseText;
			var stg="EXISTS>";
			var i;
			i=rsp.search(new RegExp( stg, "i" ));
			if (i != -1) {
				rc=(rsp.slice(i+stg.length)).substr(0,1);
				if (rc == 1)
					document.getElementById(CheckBoxID).checked=true;
			}
			
			// try to run any javascript in the html returned
			//var x = eContainer.getElementsByTagName("script");   
			//for(var i=0;i<x.length;i++)  
			//{  
			//	eval(x[i].text);  
			//} 
			
        }
      }
    }
	
    this.GetWatch = function() {
	  req=null; 
	  req = AjaxRequest();
      req.onreadystatechange = processRequest; 
	  // stupid IE will cache the GET so add cache buster (already done)
      req.open("GET", url+ "&randNum=" + new Date().getTime(), true);
      req.send(null);
    }

    function SetWatch(v) {
	  val=v;
	  if (val)
		url='/notify/add/?' + key;
	  else
		// they are un-checking, so delete watch
		url='/notify/delete/?' + key;
	  req=null; 
	  req = AjaxRequest();
      req.onreadystatechange = WatchSet; 
	  // stupid IE will cache the GET so add cache buster (already done)
      req.open("GET", url+ "&randNum=" + new Date().getTime(), true);
      req.send(null);
    }
	
    function WatchSet () {
      if (req.readyState == 4) {
        if (req.status == 200) {
			var wcb = document.getElementById(CheckBoxID);
			wcb.disabled=false;  // because window.open is async, we can get multiple clicks. so let popup form re-enable
			confFunc(val,eventdesc);			
        }
      }
    }
	
}


// this is a simple function-shortcut to avoid using lengthy document.getElementById
function ae$(a) { return document.getElementById(a); }
 
// This is a main ae_prompt function, it saves function callback and sets up dialog
function ae_prompt(q, a) {
	ae$('aep_prompt').innerHTML = q;
	//ae$('aep_text').value = a;
	ae$('aep_ovrl').style.display = ae$('aep_ww').style.display = '';
	//ae$('aep_text').focus();
	//ae$('aep_text').select();
}
 
// This function is called when user presses OK(m=0) or Cancel(m=1) button
// in the dialog. You should not call this function directly.
function ae_clk(m) {
	// hide dialog layers 
	ae$('aep_ovrl').style.display = ae$('aep_ww').style.display = 'none';
}

function ConfirmationPopup(val,text) {
// Display results in a modal dialog
	var sHeader;
	// H3 error in IE
	//if (val) {
	//	sHeader="<H3>Notification Set</H3>"
	//} else {
	//	sHeader="<H3>Notification Removed</H3>"
	//}
	if (val) {
		sHeader="<B>Notification Set</B><BR>"
	} else {
		sHeader="<B>Notification Removed</B><BR>"
	}
	var i;
	i=text.search(new RegExp( "=", "i" ));
	if (i != -1) 
		bdy=text.substr(i+1);
    ae_prompt( sHeader+'<SMALL>'+bdy+'</SMALL>', ''); 
}
