﻿function CoMIT_CreateCookie(name,value,days) 
    {
 
    if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
    }
    else 
        var expires = "";
            
    document.cookie = name+"="+value+expires+"; path=/";
 
    }
 
function CoMIT_ReadCookie(name) 
    {
 
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) 
    {
    var c = ca[i];
    while (c.charAt(0)==' ') { c = c.substring(1,c.length) };
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
 
    return null;
    
    }
 
function CoMIT_EraseCookie(name) 
    {
    
    createCookie(name,"",-1);
    
    }
 
var xhttpIncident = null;;
 
var dt = new Date();
 
var ashxTarget = null;
    
function HandlerOnIncident()
{
 
if (xhttpIncident.readyState==4)
{   
processResponse(xhttpIncident);  
}
 
}
 
function HandlerNonIEOnIncident()
{
if (xhttpIncident.readyState==4)
{   
processNonIEResponse(xhttpIncident);  
}
 
}
 
function sendXmlHttpIncidentRequest(sQueryString, sHttpMethod, bAsynchronousRequest, bRefreshCache)
{

if (document.all)  // IE browser
{
xhttpIncident = new ActiveXObject("Msxml2.XMLHTTP"); 
    xhttpIncident.onreadystatechange = HandlerOnIncident;        
}
else  
{
xhttpIncident = new XMLHttpRequest(); 
    xhttpIncident.onreadystatechange = HandlerNonIEOnIncident;        
}
    
 
//ashxTarget = 'CoMIT_IncidentAlertHandler.ashx?z=' + dt.getTime();
//xhttpIncident.open(sHttpMethod, ashxTarget, bAsynchronousRequest, bRefreshCache);

ashxTarget = 'CallService.ashx?z=' + dt.getTime();
xhttpIncident.open("GET", ashxTarget, bAsynchronousRequest, bRefreshCache);

xhttpIncident.send("");
 
}
 
function cancelRequest()
{
 
xhttpIncident.onreadystatechange = function () {};
xhttpIncident.abort();
xhttpIncident = null;
}
 
 
//**************************************************************************************************************** 
function processNonIEResponse(myXhttp)
{ 


    try{
    
    
            if (myXhttp.responseXML.getElementsByTagName("CoMIT_IncidentAlert")[0].getElementsByTagName("summary")[0].getElementsByTagName("IncidentCount")[0].textContent=="0")
            {  

                // In case we're down to zero and a message is still displayed.
                if (xGetElementById("divDUCOMIncidentAlert")!=null) {       
                          
                        document.body.removeChild(xGetElementById("divDUCOMIncidentAlert"));  
 
                }
 
            }
            else
            {
 
                cookieCoMIT_IncidentId = CoMIT_ReadCookie("CoMIT_IncidentId");
 
                cookieCoMIT_IncidentDate = CoMIT_ReadCookie("CoMIT_IncidentDate");
 
                DisplayIncidentAlert = true;
 
                if ((cookieCoMIT_IncidentId) && (cookieCoMIT_IncidentDate))
                {
 
                    if (cookieCoMIT_IncidentId==myXhttp.responseXML.getElementsByTagName("CoMIT_IncidentAlert")[0].getElementsByTagName("details")[0].getElementsByTagName("IncidentLevel0")[0].getElementsByTagName("IncidentId")[0].textContent)
                    {
 
                        DisplayIncidentAlert = ( cookieCoMIT_IncidentDate != myXhttp.responseXML.getElementsByTagName("CoMIT_IncidentAlert")[0].getElementsByTagName("details")[0].getElementsByTagName("IncidentLevel0")[0].getElementsByTagName("IncidentEditDate")[0].textContent);
                    
                    }
                    else
                    {
                    
                        DisplayIncidentAlert = true;
                    
                    }
                    
                }
                
                if (DisplayIncidentAlert)
                {
            
                    COMITAddIncidentAlert(myXhttp.responseXML.getElementsByTagName("CoMIT_IncidentAlert")[0].getElementsByTagName("details")[0].getElementsByTagName("IncidentLevel0")[0].getElementsByTagName("IncidentContent")[0].textContent, myXhttp.responseXML.getElementsByTagName("CoMIT_IncidentAlert")[0].getElementsByTagName("details")[0].getElementsByTagName("IncidentLevel0")[0].getElementsByTagName("IncidentLevel")[0].textContent);
 
                    CoMIT_CreateCookie('CoMIT_IncidentId',myXhttp.responseXML.getElementsByTagName("CoMIT_IncidentAlert")[0].getElementsByTagName("details")[0].getElementsByTagName("IncidentLevel0")[0].getElementsByTagName("IncidentId")[0].textContent,1); 
 
                    CoMIT_CreateCookie('CoMIT_IncidentDate',myXhttp.responseXML.getElementsByTagName("CoMIT_IncidentAlert")[0].getElementsByTagName("details")[0].getElementsByTagName("IncidentLevel0")[0].getElementsByTagName("IncidentEditDate")[0].textContent,1); 
            
                }
                
            }
            
        }
        catch (ex)
        {
        }
        
        xhttpIncident = null;
        
        nextInterval = 60000;
 
//setTimeout("sendXmlHttpIncidentRequest('CoMIT_IncidentAlertHandler.ashx', 'GET', true, true);", nextInterval);
setTimeout("sendXmlHttpIncidentRequest('CallService.ashx', 'GET', true, true);", nextInterval);
 
}
 
 
//**************************************************************************************************************** 
function processResponse(myXhttp) {
        try {

            if (myXhttp.responseXML.selectSingleNode("CoMIT_IncidentAlert").selectSingleNode("summary").selectSingleNode("IncidentCount").text=="0")
            {
                // In case we're down to zero and a message is still displayed.
                if (xGetElementById("divDUCOMIncidentAlert")!=null) {       
                          
                        document.body.removeChild(xGetElementById("divDUCOMIncidentAlert"));  
 
                }
 
            }
            else
            {
                cookieCoMIT_IncidentId = CoMIT_ReadCookie("CoMIT_IncidentId");
 
                cookieCoMIT_IncidentDate = CoMIT_ReadCookie("CoMIT_IncidentDate");
 
                DisplayIncidentAlert = true;
 
                if ((cookieCoMIT_IncidentId) && (cookieCoMIT_IncidentDate))
                {

                    if (cookieCoMIT_IncidentId==myXhttp.responseXML.selectSingleNode("CoMIT_IncidentAlert").selectSingleNode("details").selectSingleNode("IncidentLevel0").selectSingleNode("IncidentId").text)
                    {
 
                        DisplayIncidentAlert = ( (cookieCoMIT_IncidentDate != myXhttp.responseXML.selectSingleNode("CoMIT_IncidentAlert").selectSingleNode("details").selectSingleNode("IncidentLevel0").selectSingleNode("IncidentEditDate").text));
                    
                    }
                    else
                    {
                    
                        DisplayIncidentAlert = true;
                    
                    }
                    
                }
                
                if (DisplayIncidentAlert)
                {
        
                    COMITAddIncidentAlert(myXhttp.responseXML.selectSingleNode("CoMIT_IncidentAlert").selectSingleNode("details").selectSingleNode("IncidentLevel0").selectSingleNode("IncidentContent").text, myXhttp.responseXML.selectSingleNode("CoMIT_IncidentAlert").selectSingleNode("details").selectSingleNode("IncidentLevel0").selectSingleNode("IncidentLevel").text);
 
                    CoMIT_CreateCookie('CoMIT_IncidentId',myXhttp.responseXML.selectSingleNode("CoMIT_IncidentAlert").selectSingleNode("details").selectSingleNode("IncidentLevel0").selectSingleNode("IncidentId").text,1); 
 
                    CoMIT_CreateCookie('CoMIT_IncidentDate',myXhttp.responseXML.selectSingleNode("CoMIT_IncidentAlert").selectSingleNode("details").selectSingleNode("IncidentLevel0").selectSingleNode("IncidentEditDate").text,1); 
       
                }
                
            }
            
        }
        catch (ex)
        {
        }
        
        xhttpIncident = null;
        
        nextInterval = 60000;
 
//setTimeout("sendXmlHttpIncidentRequest('CoMIT_IncidentAlertHandler.ashx', 'GET', true, true);", nextInterval);
 setTimeout("sendXmlHttpIncidentRequest('CallService.ashx', 'GET', true, true);", nextInterval);

    }
    
//**************************************************************************************************************** 
function COMITAddIncidentAlert(strMessage, intIncidentLevel, strAlertId, dtAlertEditDate) 
{  
    if (xGetElementById("divDUCOMIncidentAlert")!=null) {       
	    document.body.removeChild(xGetElementById("divDUCOMIncidentAlert"));  
    }

    switch (intIncidentLevel)
    {
	case "1":
	    strIncidentColor = "#00ff00";
	    strTitle = "GREEN ALERT";
	    break;
	case "2":
	    strIncidentColor = "#ffdf32";
	    strTitle = "YELLOW ALERT";
	    break;
	default:
	    strIncidentColor = "#ff0000";
	    strTitle = "RED ALERT";
	    break;
    }

    divIncidentAlert = document.createElement('div');        
    divIncidentAlert.setAttribute('id',"divDUCOMIncidentAlert");        
    divIncidentAlert.style.visibility= "visible";         
    divIncidentAlert.style.width = "510px";         
    divIncidentAlert.style.paddingLeft = "0px";        
    divIncidentAlert.style.paddingRight="0px";         
    divIncidentAlert.style.paddingTop = "0px";         
    divIncidentAlert.style.paddingBottom = "0px";         
    divIncidentAlert.style.zIndex = "150";          
    divIncidentAlert.style.backgroundColor = "#ffffff";         
    divIncidentAlert.style.borderWidth = "1px";         
    divIncidentAlert.style.color = "#001d50";         
    divIncidentAlert.style.borderStyle = "solid";         
    divIncidentAlert.style.borderBottomColor = strIncidentColor;
    divIncidentAlert.style.borderLeftColor= strIncidentColor;
    divIncidentAlert.style.borderTopColor = strIncidentColor;    
    divIncidentAlert.style.borderRightColor = strIncidentColor;      
    divIncidentAlert.style.position = "absolute";        

    divIncidentAlertH1 = document.createElement('h1');
    divIncidentAlertH1.style.backgroundColor = strIncidentColor;
    divIncidentAlertH1.style.padding = "5px";
    divIncidentAlertH1.style.margin = "0";
    divIncidentAlertH1.style.fontFamily = "Georgia, Times, serif";
    divIncidentAlertH1.style.fontSize = "large";
    divIncidentAlertH1.style.color = "#ffffff";
    divIncidentAlertH1.style.letterSpacing = "4px";
    divIncidentAlertH1.style.textAlign = "center";
    divIncidentAlertH1.style.textTransform = "uppercase";
    divIncidentAlertH1.innerHTML = strTitle;

    divIncidentAlertMessage = document.createElement('div');
    divIncidentAlertMessage.style.fontFamily = "Arial, Helvetica, sans-serif";
    divIncidentAlertMessage.style.fontSize = "medium";
    divIncidentAlertMessage.style.padding = "5px";
    divIncidentAlertMessage.style.lineHeight = "140%";
    divIncidentAlertMessage.innerHTML = strMessage;

    divIncidentAlert.appendChild(divIncidentAlertH1); 
    divIncidentAlert.appendChild(divIncidentAlertMessage);


	spanIncidentAlertOK = document.createElement('div');
	spanIncidentAlertOK.style.width="510px";
	spanIncidentAlertOK.style.textAlign = "center";
	btnIncidentAlertOK = document.createElement('input');
	btnIncidentAlertOK.type = "button";
	btnIncidentAlertOK.setAttribute('id',"btnIncidentAlertOK");        
	btnIncidentAlertOK.value = "Hide Alert";
	btnIncidentAlertOK.style.margin = "5px";
	btnIncidentAlertOK.onclick = function () { xHide(xGetElementById('divDUCOMIncidentAlert'));};
	spanIncidentAlertOK.appendChild(btnIncidentAlertOK);
	divIncidentAlert.appendChild(spanIncidentAlertOK);
	document.body.appendChild(divIncidentAlert);     

	client_height = xClientHeight(); 
	client_width = xClientWidth();        
	xLeft(divIncidentAlert, (client_width/2)-300);         
	xTop(divIncidentAlert, xScrollTop(document) + 75);

	window.onscroll = function()
	{
	    if (xGetElementById("divDUCOMIncidentAlert")!=null) 
	    {        
		if (xGetElementById("divDUCOMIncidentAlert").style.visibility=="visible") 
		{        
		    xTop(xGetElementById("divDUCOMIncidentAlert"), xScrollTop(document) + 75);                
		}
	    }
	}   


}


//setTimeout("sendXmlHttpIncidentRequest('http://www.drexelmed.edu/CoMIT_IncidentAlertHandler.ashx', 'GET', true, true);", 2000);
setTimeout("sendXmlHttpIncidentRequest('CallService.ashx', 'GET', true, true);", 2000);
