﻿// calendar overlay
var dom = (document.getElementById) ? true : false;
var ns5 = ((navigator.userAgent.indexOf("Gecko")>-1) && dom) ? true: false;
var ie5 = ((navigator.userAgent.indexOf("MSIE")>-1) && dom) ? true : false;
var ns4 = (document.layers && !dom) ? true : false;
var ie4 = (document.all && !dom) ? true : false;
var nodyn = (!ns5 && !ns4 && !ie4 && !ie5) ? true : false;

// resize fix for ns4
var origWidth, origHeight;
if (ns4) {
	origWidth = window.innerWidth; origHeight = window.innerHeight;
	window.onresize = function() { if (window.innerWidth != origWidth || window.innerHeight != origHeight) history.go(0); }
}

// avoid error of passing event object in older browsers
if (nodyn) { event = "nope" }

var tipFollowMouse= false;	
// Be sure to set tipWidth wide enough for widest image
var tipWidth= 300;
var offX= 5;	// how far from mouse to show tip
var offY= 5; 
var tipFontFamily= "Verdana, arial, helvetica, sans-serif";
var tipFontSize= "8pt";
var tipFontColor= "#000000";
var tipBgColor= "#DDECFF"; 
var tipBorderColor= "#000080";
var tipBorderWidth= 3;
var tipBorderStyle= "ridge";
var tipPadding= 4;
var tooltip, tipcss;

function initTip() {
	if (nodyn) return;
	tooltip = (ns4)? document.tipDiv.document: (ie4)? document.all['tipDiv']: (ie5||ns5)? document.getElementById('tipDiv'): null;
	if (tooltip != null)
	{
	    tipcss = (ns4)? document.tipDiv: tooltip.style;
	    if (ie4||ie5||ns5) {	// ns4 would lose all this on rewrites
//		    tipcss.width = tipWidth+"px";
//		    tipcss.fontFamily = tipFontFamily;
//		    tipcss.fontSize = tipFontSize;
//		    tipcss.color = tipFontColor;
//		    tipcss.backgroundColor = tipBgColor;
//		    tipcss.borderColor = tipBorderColor;
//		    tipcss.borderWidth = tipBorderWidth+"px";
//		    tipcss.padding = tipPadding+"px";
//		    tipcss.borderStyle = tipBorderStyle;
	    }
	    if (tooltip&&tipFollowMouse) {
		    if (ns4) document.captureEvents(Event.MOUSEMOVE);
		    document.onmousemove = trackMouse;
	    }
	}
}

window.onload = onLoad;

function onLoad()
{
    initTip();
    buildEventsTemplate();
}

var t1,t2;	// for setTimeouts
var tipOn = false;	// check if over tooltip link
function doTooltip(evt,txtEvents) {
	if (!tooltip) return;
	if (t1) clearTimeout(t1);	if (t2) clearTimeout(t2);
	tipOn = true;

    // clears tip	
	t2=setTimeout("tipcss.visibility='hidden'",100);
	
	if (ns4) {
		var tip = txtEvents;
		tooltip.write(tip);
		tooltip.close();
	} else if (ie4||ie5||ns5) {
		var tip = txtEvents;
	 	tooltip.innerHTML = tip;
	}
	if (!tipFollowMouse) positionTip(evt);
	else t1=setTimeout("tipcss.visibility='visible'",100);
}

var mouseX, mouseY;
function trackMouse(evt) {
	mouseX = (ns4||ns5)? evt.pageX: window.event.clientX + document.body.scrollLeft;
	mouseY = (ns4||ns5)? evt.pageY: window.event.clientY + document.body.scrollTop;
	if (tipOn) positionTip(evt);
}

function positionTip(evt) {
	if (!tipFollowMouse) {
		mouseX = (ns4||ns5)? evt.pageX: window.event.clientX + document.body.scrollLeft;
		mouseY = (ns4||ns5)? evt.pageY: window.event.clientY + document.body.scrollTop;
	}
	
	// tooltip width and height
	var tpWd = (ns4)? tooltip.width: (ie4||ie5)? tooltip.clientWidth: tooltip.offsetWidth;
	var tpHt = (ns4)? tooltip.height: (ie4||ie5)? tooltip.clientHeight: tooltip.offsetHeight;
	
	// document area in view (subtract scrollbar width for ns)
	var winWd = (ns4||ns5)? window.innerWidth-20+window.pageXOffset: document.body.clientWidth+document.body.scrollLeft;
	var winHt = (ns4||ns5)? window.innerHeight-20+window.pageYOffset: document.body.clientHeight+document.body.scrollTop;
	
	// check mouse position against tip and window dimensions
	// and position the tooltip 
	if ((mouseX+offX+tpWd)>winWd) 
		tipcss.left = (ns4)? mouseX-(tpWd+offX): mouseX-(tpWd+offX)+"px";
	else tipcss.left = (ns4)? mouseX+offX: mouseX+offX+"px";
	
	//tipcss.top = (ns4)? winHt-(tpHt+offY): winHt-(tpHt+offY)+"px";
	if ((mouseY+offY+tpHt)>winHt)
	    if (ns4)
	    {
	        tipcss.top = winHt-(tpHt+offY);
	        
	        if (tipcss.top < 50)
		    {
		        tipcss.top = 50;
		    }
	    }
	    else
	    {
	        if (winHt-(tpHt+offY) < 50)
	        {
	            tipcss.top = "50px";
	        }
	        else
	        {
	            tipcss.top = winHt-(tpHt+offY)+"px"
	        }
	    }
	else
	{
	    //tipcss.top = (ns4)? mouseY+offY: mouseY+offY+"px";
	    if (ns4)
	    {
	        tipcss.top = mouseY+offY;
	    }
	    else
	    {
            tipcss.top = mouseY+offY+"px";
	    }
	}
	
	if (!tipFollowMouse) t1=setTimeout("tipcss.visibility='visible'",100);
}

function hideTip() {
	if (!tooltip) return;
	t2=setTimeout("tipcss.visibility='hidden'",100);
	tipOn = false;
}

// ajax for paging
function getXmlHTTP()
{
    var http_request = false;

    if (window.XMLHttpRequest) 
    { // Mozilla, Safari, ...
    
        http_request = new XMLHttpRequest();
        
        if (http_request.overrideMimeType) 
        {
            http_request.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) 
    { // IE
        try 
        {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) 
        {
            try 
            {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }
    
    return http_request;
}

// post back to the server including the parameters from grid
function buildEventsTemplate(param)
{
    // div that will store the returned data
    var rdiv = document.getElementById("eventResultDiv");
    
    if (rdiv != null)
    {
        rdiv.innerHTML = "";
        
        // default parameter
        var thePage = window.location.pathname + window.location.search + "?ajaxPaging=true";
        
        // add the parameter pass to this method to the existing variable
        // &curIndex is for to supply the control the correct index for paging
        if (param != "")
        {
            thePage += param;    
        }

        // get ajax object
        var xmlReq = getXmlHTTP();
        
        if (xmlReq != null)
        {
            xmlReq.onreadystatechange = function() { processEventsTemplate(xmlReq); };
                
            // post to the page
            xmlReq.open('GET', thePage, true);
            xmlReq.send(null);
        }
    }
}

function processEventsTemplate(xmlReq)
{
    // get browser type
    var browser = new BrowserDetect();
    
    // div for results
    var rdiv = document.getElementById("eventResultDiv");
    
    if (browser.isNSCompatible)
    {
        // workaround for firefox
        // clear multiple instances of the results div
        var aryDiv = document.getElementsByName("eventResultDivName");
        
        // remove elements from results div
        for (var i = 0; i < aryDiv.length; i++)
        {
            aryDiv[i].innerHTML = "";
        }
    }
    
    // loading
    if (xmlReq.readyState == 1)
    {
        rdiv.innerHTML = "Loading...";
    }
    
    // ready state is complete
    if (xmlReq.readyState == 4)
    {
        // HTTP server response
        // 200 = OK
        if (xmlReq.status == 200)
        {
            // returned value from page
            // events control - renders a table generated by a template
            var retVal = xmlReq.responseText;
            var startPosition = retVal.indexOf('<div id=\'eventResultDiv\' name=\'eventResultDivName\'></div>');
            var endPosition = retVal.indexOf('<div id=\'endResultDiv\'></div>')
            retVal = retVal.substring(startPosition, endPosition);

            try
            {
                // tries to set the returned value to the div
                // error will occur with IE
                rdiv.innerHTML = "";
                rdiv.innerHTML = retVal;
            }
            catch (e)
            {
                // wraps a div around the div supplied in the control
                // bug in IE (workaround)
                var wrappingDiv = document.createElement('div');
                wrappingDiv.innerHTML = "";
                wrappingDiv.innerHTML = retVal;
                rdiv.appendChild(wrappingDiv);
            }
        }
        else
        {
            // there was a problem with the request
            rdiv.innerHTML = "Error retrieving data";
        }
    }
}

function showNext()
{
    var hidRecordCount = parseInt(document.getElementById("hidRecordCount").value);
    var hidCurrentIndex = parseInt(document.getElementById("hidCurrentIndex").value);
    var hidPageSize = parseInt(document.getElementById("hidPageSize").value);
    
    if ((hidCurrentIndex + hidPageSize) < hidRecordCount)
    {
        hidCurrentIndex = hidCurrentIndex + hidPageSize;
    }
    
    // build parameter
    var param = "&curIndex=" + hidCurrentIndex;
    
    // build new results
    buildEventsTemplate(param);
}

function showPrevious()
{
    var hidRecordCount = parseInt(document.getElementById("hidRecordCount").value);
    var hidCurrentIndex = parseInt(document.getElementById("hidCurrentIndex").value);
    var hidPageSize = parseInt(document.getElementById("hidPageSize").value);
    
    hidCurrentIndex = hidCurrentIndex - hidPageSize;
    
    if (hidCurrentIndex < 0)
    {
        hidCurrentIndex = 0;
    }

    // build parameter
    var param = "&curIndex=" + hidCurrentIndex;
    
    // build new results
    buildEventsTemplate(param);
}

function showLast()
{
    var hidRecordCount = parseInt(document.getElementById("hidRecordCount").value);
    var hidCurrentIndex = parseInt(document.getElementById("hidCurrentIndex").value);
    var hidPageSize = parseInt(document.getElementById("hidPageSize").value);    

    var intMod = 0;

    intMod = hidRecordCount % hidPageSize;
    
    if (intMod > 0)
    {
        hidCurrentIndex = hidRecordCount - intMod;
    }
    else
    {
        hidCurrentIndex = hidRecordCount - hidPageSize;
    }
    
    // build parameter
    var param = "&curIndex=" + hidCurrentIndex
    
    // build new results
    buildEventsTemplate(param);
}

function showFirst()
{
    // build parameter
    var param = "&curIndex=0";
    
    // build new results
    buildEventsTemplate(param);
}

// Browser Detect  v2.1.6
// documentation: http://www.dithered.com/javascript/browser_detect/index.html
// code by Chris Nott (chris[at]dithered[dot]com)

function BrowserDetect() {
   var ua = navigator.userAgent.toLowerCase(); 

   // browser engine name
   this.isGecko       = (ua.indexOf('gecko') != -1 && ua.indexOf('safari') == -1);
   this.isAppleWebKit = (ua.indexOf('applewebkit') != -1);

   // browser name
   this.isKonqueror   = (ua.indexOf('konqueror') != -1); 
   this.isSafari      = (ua.indexOf('safari') != - 1);
   this.isOmniweb     = (ua.indexOf('omniweb') != - 1);
   this.isOpera       = (ua.indexOf('opera') != -1); 
   this.isIcab        = (ua.indexOf('icab') != -1); 
   this.isAol         = (ua.indexOf('aol') != -1); 
   this.isIE          = (ua.indexOf('msie') != -1 && !this.isOpera && (ua.indexOf('webtv') == -1) ); 
   this.isMozilla     = (this.isGecko && ua.indexOf('gecko/') + 14 == ua.length);
   this.isFirebird    = (ua.indexOf('firebird/') != -1);
   this.isNS          = ( (this.isGecko) ? (ua.indexOf('netscape') != -1) : ( (ua.indexOf('mozilla') != -1) && !this.isOpera && !this.isSafari && (ua.indexOf('spoofer') == -1) && (ua.indexOf('compatible') == -1) && (ua.indexOf('webtv') == -1) && (ua.indexOf('hotjava') == -1) ) );
   
   // spoofing and compatible browsers
   this.isIECompatible = ( (ua.indexOf('msie') != -1) && !this.isIE);
   this.isNSCompatible = ( (ua.indexOf('mozilla') != -1) && !this.isNS && !this.isMozilla);
   
   // rendering engine versions
   this.geckoVersion = ( (this.isGecko) ? ua.substring( (ua.lastIndexOf('gecko/') + 6), (ua.lastIndexOf('gecko/') + 14) ) : -1 );
   this.equivalentMozilla = ( (this.isGecko) ? parseFloat( ua.substring( ua.indexOf('rv:') + 3 ) ) : -1 );
   this.appleWebKitVersion = ( (this.isAppleWebKit) ? parseFloat( ua.substring( ua.indexOf('applewebkit/') + 12) ) : -1 );
   
   // browser version
   this.versionMinor = parseFloat(navigator.appVersion); 
   
   // correct version number
   if (this.isGecko && !this.isMozilla) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('/', ua.indexOf('gecko/') + 6) + 1 ) );
   }
   else if (this.isMozilla) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('rv:') + 3 ) );
   }
   else if (this.isIE && this.versionMinor >= 4) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('msie ') + 5 ) );
   }
   else if (this.isKonqueror) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('konqueror/') + 10 ) );
   }
   else if (this.isSafari) {
      this.versionMinor = parseFloat( ua.substring( ua.lastIndexOf('safari/') + 7 ) );
   }
   else if (this.isOmniweb) {
      this.versionMinor = parseFloat( ua.substring( ua.lastIndexOf('omniweb/') + 8 ) );
   }
   else if (this.isOpera) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('opera') + 6 ) );
   }
   else if (this.isIcab) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('icab') + 5 ) );
   }
   
   this.versionMajor = parseInt(this.versionMinor);                  
}