﻿// javascript for menus
function cvMenu( oMenuDiv, oButtonDiv, iCloseDelay, iExitDelay, iXoff, iYoff )
{
	
	this.oMenuDiv = oMenuDiv;
	this.oButtonDiv = oButtonDiv;
	this.iCloseDelay = iCloseDelay;
	this.iExitDelay = iExitDelay;
	this.iXoff = iYoff;
	this.iYoff = iXoff;
	this.bOverButton = false;
	this.bOverMenu = false;
	this.oTimerID = 0;
	this.oShadow = null;

	this.close = function()
	{
		if(this.oTimerID > 0) clearTimeout(this.oTimerID);
		this.oTimerID=0;
		this.bOverMenu = false;
		this.bOverButton = false;
		this.oMenuDiv.style.display = 'none';
	}

	this.enterbutton = function()
	{
		this.bOverButton = true;
		if(this.oTimerID > 0) clearTimeout(this.oTimerID);
			this.oTimerID=0;
	
		this.oMenuDiv.style.display = 'block';
	
		var o = oButtonDiv.offsetTop + this.yoff; 
		if ( o < 0 )
			o=0;
		this.oMenuDiv.style.top = o;

		o = oButtonDiv.offsetLeft + this.xoff; 
		if ( o < 0 )
			o=0;
		this.oMenuDiv.style.left = o;
		
	}
	this.exitbutton = function()
	{
		if( this.bOverButton && !this.bOverMenu )
		{
			this.oTimerID = setTimeout(this.close,this.CloseDelay);
		}
	}
	this.entermenu = function()
	{
		this.bOverMenu=true;
		this.bOverButton=false;
		if(this.oTimerID > 0) clearTimeout(this.oTimerID);
		this.oTimerID=0;
	}
	
	this.exitmenu = function()
	{
		this.bOverMenu=false;
		if(this.oTimerID > 0) clearTimeout(this.oTimerID);
		this.oTimerID = setTimeout(this.close,this.ExitDelay); 
	}
	
}


function clkEditButton(vMenu, oBut, oMenu)
{
	vMenu.bOverButton = true;

	if(vMenu.oTimerID > 0) clearTimeout(vMenu.oTimerID);
		vMenu.oTimerID=0;
	
	oMenu.style.display = 'block';
	
	var o = oBut.offsetTop + vMenu.yoff; 
	if ( o < 0 )
		o=0;
	oMenu.style.top = o;

	var o = oBut.offsetLeft + vMenu.xoff; 
	if ( o < 0 )
		o=0;

	oMenu.style.left = o;

	//alert( vMenu.shadow);
	
	if( vMenu.shadow )
	{
		if( !vMenu.oShadow )
		{
			vMenu.oShadow = document.createElement("DIV");

			//alert( vMenu.oShadow );
			
			with(vMenu.oShadow)
			{
				id = 'shadow';
				with(style)
				{
					position = 'absolute';
					display = 'block';
					visibility = 'visible';
					left = (parseInt(oMenu.style.left)+4) + 'px';
					top = (parseInt(oMenu.style.top)+4) + 'px';
					width = '100px';
					height = '100px';
					zindex = 2001;
					margin= '0';
					padding= '0px 0px 0px 0px';
					overflow = 'hidden';
					backgroundColor = '#000000';
					filter= 'alpha(opacity=25)';
					MozOpacity = 0.25;
				}
			}

			divs = document.getElementsByTagName("div");
			if( divs['oAllScreen'] != null )
			{
				divs['oAllScreen'].appendChild(vMenu.oShadow);
			}
		}
		with (vMenu.oShadow)
		{
			style.height = oMenu.offsetHeight + 'px';
			style.width = oMenu.offsetWidth + 'px';
			style.display = 'block';
		}
	}

	
}


function exitButton(vMenu)
{
	if( vMenu.bOverButton && !vMenu.bOverMenu )
	{
		vMenu.oTimerID = setTimeout(vMenu.CloseFunc,vMenu.CloseDelay);
	}
}
function CloseMenu()
{
	CloseMenuCommon(cvEditMenu,document.getElementById("divEditMenu"))
}
function CloseWfMenu()
{
	CloseMenuCommon(cvWfMenu,document.getElementById("divWorkFlowMenu"))
}
function CloseEmailMenu()
{
	CloseMenuCommon(cvEmailMenu,document.getElementById("divEmailMenu"))
}
function CloseSearchMenu()
{
//	alert('Close')
	CloseMenuCommon(cvSearchMenu,document.getElementById("divSearchMenu"));
}
function CloseMenuCommon(vMenu,oMenu)
{
	if(vMenu.oTimerID > 0) clearTimeout(vMenu.oTimerID);
	vMenu.oTimerID=0;
	vMenu.bOverMenu = false;
	vMenu.bOverButton = false;
	oMenu.style.display = 'none';
	if(vMenu.oShadow)
		vMenu.oShadow.style.display = 'none';
}

function EnterMenu(vMenu)
{
//	alert('Open')
	vMenu.bOverMenu=true;
	vMenu.bOverButton=false;
	if(vMenu.oTimerID > 0) clearTimeout(vMenu.oTimerID);
	vMenu.oTimerID=0;

}
function ExitMenu(vMenu)
{
	vMenu.bOverMenu=false;
	if(vMenu.oTimerID > 0) clearTimeout(vMenu.oTimerID);
	vMenu.oTimerID = setTimeout(vMenu.CloseFunc,vMenu.ExitDelay); 
}



function civicapostwindow(sPath)
{
	document.wfpost.action=sPath;
	document.wfpost.submit();
}

/* Create a new XMLHttpRequest object to talk to the Web server */
var xmlHttp = false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
  try {
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (e2) {
    xmlHttp = false;
  }
}
@end @*/

if (!xmlHttp && typeof XMLHttpRequest != 'undefined')
{
  xmlHttp = new XMLHttpRequest();
}

function TestArray(value) 
{
  var s = typeof value;    
  if (s === 'object') 
  {        
		if (value) 
		{
			if (value instanceof Array) 
			{                
				s = 'array';
			}        
		} 
		else 
		{            
			s = 'null';
		}    
	}    
	return s;
}

function Test()
{
  var q = document.getElementById("q").value;
  var r = document.getElementById("r");
	alert(q);
	r.innerText = q;
}

var aSearchObjs = [];
var aSearchUrls = [];
var aSearchNew = [];
var iSearchIndx = -1;

function CheckSearchDropdown()
{
	//alert( iSearchIndx );
	if( iSearchIndx >= 0 ) 
	{
		if( aSearchNew[iSearchIndx] == 1 )
			window.open(aSearchUrls[iSearchIndx]);
		else
			window.location = aSearchUrls[iSearchIndx];
		return false;
	}
	else
		return true;
}

function SearchKeyDown(e)
{
//	alert(e.type)
	
	if(document.all) // IE
	{
		keynum = e.keyCode;
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which;
  }

//  sInner = 'iSearchIndx = ' + iSearchIndx + '<br>' ;
//	sInner += 'aSearchObjs = ' + aSearchObjs.length + '<br>' ;					 
//  document.getElementById("sample").innerHTML = sInner; 

	// down ?
	if( keynum == 40 && iSearchIndx<(aSearchObjs.length-1))
	{
		if( iSearchIndx >= 0 )
			SearchElementBlur(aSearchObjs[iSearchIndx]);
		iSearchIndx++;
		SearchElementFocus(aSearchObjs[iSearchIndx]);
	}
	// up ?
	if( keynum == 38 && iSearchIndx>=0)
	{
		SearchElementBlur(aSearchObjs[iSearchIndx]);
		iSearchIndx--;
		if( iSearchIndx >= 0 )
			SearchElementFocus(aSearchObjs[iSearchIndx]);
	}
}

function SearchElementKeyDown(e,iIndx)
{
	var keynum;

	if(aSearchObjs.length == 0 ) 
		return;
	
	if(document.all) // IE
	{
		keynum = e.keyCode;
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which;
  }
  
	if( keynum == 40 && iIndx<aSearchObjs.length)
	{
		if(aSearchObjs[iIndx+1])
		{
			var aScrl = getScrollXY();
			var sSize = alertSize();
			
			aSearchObjs[iIndx+1].focus();
			
			//alert( 'ScrlX: ' + aScrl[0] + ' ScrlY: ' + aScrl[1] + '\nw: ' + sSize[0] + ' h: ' + sSize[1] + '\nmenu: ' + document.getElementById('divSearchMenu').offsetTop + '\nLink: ' + aSearchObjs[iIndx+1].offsetTop );
			
			
		}
	}
	else if( keynum == 38 )
	{
		if(iIndx>0)
		{
			if(aSearchObjs[iIndx-1])
				aSearchObjs[iIndx-1].focus();
		}
		else
		{
			document.getElementById('searchquery').focus();
		}
	}
	
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}

function SearchChanged(e,el,oMenu)
{
	var keynum;
	
	if(e.type == 'keyup')
	{
		if(document.all) // IE
		{
			keynum = e.keyCode;
		}
		else if(e.which) // Netscape/Firefox/Opera
		{
			keynum = e.which;
		}
		
		if((keynum==13) || (keynum>36 && keynum<41))
			return;
	}
	
  aSearchObjs = [];
  aSearchLinks = [];
	
  var q = el.value;
  // Only go on if there are values for the field
  if ((q == null) || (q == ""))
  {
  
		var r = document.getElementById("divSearchMenuOptions");
		if(r)
		{
			r.innerText = q;
			CloseMenuCommon(cvSearchMenu,document.getElementById("divSearchMenu"))
		}

		return;
  }

  // Build the URL to connect to
  var url = "/inc/rx.asp?q=" + escape(q);

  // Open a connection to the server
  xmlHttp.open("GET", url, true);

	iSearchIndx = -2;

  // Setup a function for the server to run when it's done
  xmlHttp.onreadystatechange = updatePage;

  // Send the request
  xmlHttp.send(null);
  
//  sInner = 'iSearchIndx = ' + iSearchIndx + '<br>';
  
//  document.getElementById("sample").innerHTML = sInner; 
  
}

function updatePage() 
{
  if (xmlHttp.readyState == 4) 
  {
    var response = xmlHttp.responseText;
		var r = document.getElementById("divSearchMenuOptions");
		var s ='';

	  if ( (response != null) && (response != ""))
	  {
			var s = '';
			var oPair;
			
			var aList = response.split("~~")
			
			for( oPair=0; oPair<aList.length; oPair++)
			{
				if( s=='') s= '<ul>' ; 
				
				var oLnk = aList[oPair].split("~")
				oLnk[2] = escape(oLnk[2]).replace(/http%3A/i,"http:")
				
				s+= '<li>';
				s+= '<a id="srch" name="' + oPair + '" onkeyDown="SearchElementKeyDown(event,' + oPair + ');" onfocus="EnterMenu(cvSearchMenu); SearchElementFocus(this);" onblur="SearchElementBlur(this);" ';
				s+= 'href="' + oLnk[2] + '"';
				if( oLnk[3] == '1' )
				{
					s+= ' target="_blank"';
				}
				s+= '>';
				
				s+= oLnk[1];
				s+= '</a>'; 
				s+= '</li>'; 
				aSearchUrls[oPair] = oLnk[2];
				aSearchNew[oPair] = oLnk[3];
			}
			if( s!='') s+= '<ul>'; 
	  }
	  r.innerHTML = s;
	  
	  //alert(s);
	  
	  if(s=='')
	  {
			// close the menu
			CloseMenuCommon(cvSearchMenu,document.getElementById("divSearchMenu"))
			iSearchIndx = -2;
	  }
	  else
	  {
			var oSrch = document.anchors;
			if( oSrch )	
			{
				var j=0;
				
				for(i=0;i<oSrch.length;i++)
				{
					if(oSrch[i].id == 'srch')
					{
						aSearchObjs[j++] = oSrch[i];
					}
				}
			}	
			iSearchIndx = -1;
			// open the menu
	  	clkEditButton(cvSearchMenu, document.getElementById("SiteSearch"),document.getElementById("divSearchMenu") );
	  }
  }

}

function SearchElementFocus(o)
{
	o.className="focus";
}

function SearchElementBlur(o)
{
	o.className="";
}

function SearchFocus()
{
	// open the menu
	if( aSearchObjs )
	{
		//alert( aSearchObjs.length );
		if( aSearchObjs.length > 0 )
			clkEditButton(cvSearchMenu, document.getElementById("SiteSearch"),document.getElementById("divSearchMenu") );
	}
}

function alertSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [ myWidth, myHeight];
  //window.alert( 'Width = ' + myWidth + '\nHeight = ' + myHeight );
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  //window.alert( 'X ' + scrOfX + '\nY ' + scrOfY  );
  return [ scrOfX, scrOfY ];
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------

function CheckForNavbuilder()
{
	var win = window.open("", '_navbuilder');

	try
	{
		if ( win.location.href.indexOf('/civica/dynacon/sb/default.asp') != -1  )
		{ 
			win.focus();
		} 
		else
		{
			win.focus();
			frmNavBuilder.submit();
		}
	}
	catch(e)
	{
		alert('The Navbuilder appears to be open on another site!');
		win.focus();
	}	
}