function addEvent(objObject, strEventName, fnHandler)
{   
   if(objObject)
   {      
      if(objObject.addEventListener)
      {       
         objObject.addEventListener(strEventName, fnHandler, false);
      }
      else if(objObject.attachEvent) // IE
      {         
         objObject.attachEvent("on" + strEventName, fnHandler);
      }
   }
   else
   {
      //alert("Script Error:\n\nThere is no object to attach the function {" + fnHandler.name + "} to the {on" + strEventName + "} event.");
   }
}
function getEventSrc ( e )
{
   if(!e) e = window.event; // IE

   if(e.originalTarget)
   {
      
      return e.originalTarget;
   }
   else if(e.srcElement) // IE
   {
      return e.srcElement;
   }
}
function supportsDynamicEvents()
{
   return document.getElementById && (window.attachEvent || window.addEventListener);
}

function openPopupWindow(winURL, winName, winWidth, winHeight)
{

   if(window.outerWidth)
   {
      var xMax = window.outerWidth, yMax = window.outerHeight;
   }
   else if(screen.width)
   {
      var xMax = screen.width, yMax = screen.height;   
   }
   else if(document.layers)
   {
      var xMax = window.outerWidth, yMax = window.outerHeight;
   }
   else
   {
      var xMax = 640, yMax=480;
   }

   var xOffset = (xMax - winWidth) / 2, yOffset = (yMax - winHeight) / 2;

   var child = window.open(winURL, winName, 'width=' + winWidth + ',height=' + winHeight + ',screenX=' + xOffset + ',screenY=' + yOffset+' , top=' + yOffset + ',left=' + xOffset + ',scrollbars=1');
   
    if (child == null)
    {
	    alert("Can not create popup window.\n\nPlease ensure that your browser is allowing popups for this site.");
    }	
}

function openPopupWindowNoScroll(winURL, winName, winWidth, winHeight)
{
   if(window.outerWidth)
   {
      var xMax = window.outerWidth, yMax = window.outerHeight;
   }
   else if(screen.width)
   {
      var xMax = screen.width, yMax = screen.height;   
   }
   else if(document.layers)
   {
      var xMax = window.outerWidth, yMax = window.outerHeight;
   }
   else
   {
      var xMax = 640, yMax=480;
   }

   var xOffset = (xMax - winWidth) / 2, yOffset = (yMax - winHeight) / 2;

   var child = window.open(winURL, winName, 'width=' + winWidth + ',height=' + winHeight + ',screenX=' + xOffset + ',screenY=' + yOffset+' , top=' + yOffset + ',left=' + xOffset + ',scrollbars=false');
    
    if (child == null)
    {
	    alert("Can not create popup window.\n\nPlease ensure that your browser is allowing popups for this site.");
    }	
}


// chkFocus ensures that the current window always has focus
// The skipcycle flag is used to indicate that an object within
// the current window has focus therefore the window should not
// try to obtain focus or else the object on the window would lose focus
function chkFocus()
{
   if(!skipcycle)
   {
      window.focus();
   }
   mytimer = setTimeout("chkFocus()", 250);
}
function skipTrue()
{
   skipcycle = true;
}
function skipFalse()
{
   skipcycle = false;
}
function closeWindow()
{
   self.close();
}				
function openGlossary()
{   
   openPopupWindow("Glossary.aspx", "openGlossary", 300, 250);
}
//replaces the < and > chars with their entity replacements.
function EscapeMarkup( rawString )
{
	return rawString.replace(/</g, "&lt;").replace( />/g,"&gt;");
}
// datetime parsing and formatting routimes. modify them if you wish other datetime format
function str2dt (str_datetime)
{
   var re_date = /^([012]?\d|3[01])-([Jj][Aa][Nn]|[Ff][Ee][bB]|[Mm][Aa][Rr]|[Aa][Pp][Rr]|[Mm][Aa][Yy]|[Jj][Uu][Nn]|[Jj][u]l|[aA][Uu][gG]|[Ss][eE][pP]|[oO][Cc][Tt]|[Nn][oO][Vv]|[Dd][Ee][Cc])-(\d{4})$/;
   if (!re_date.exec(str_datetime))
   {
		return alert("Invalid date format: "+ str_datetime);
   }
	
	//alert(RegExp.$3);
	
	var monthNumber = getMonthNumber(RegExp.$2);
	
	return (new Date (RegExp.$3, monthNumber - 1, RegExp.$1));
}
function dt2dtstr (dt_datetime)
{
   theDay = dt_datetime.getDate();
   if(theDay < 10)
   {
      strDay = "0" + theDay;
   }
   else
   {
      strDay = theDay;
   }
   theMonth = (dt_datetime.getMonth() + 1);
   if(theMonth < 10)
   {
      strMonth = "0" + theMonth;
   }
   else
   {
      strMonth = theMonth;
   }   
   return (new String(strDay + "-" + getMonthName(strMonth) + "-" + dt_datetime.getFullYear()));
}

function getMonthName(monthNumber)
{
    
    switch (monthNumber)
    {
        case '01':
            return "Jan";
        case '02':
            return "Feb";
        case '03':
            return "Mar";
        case '04':
            return "Apr";
        case '05':
            return "May";
        case '06':
            return "Jun";
        case '07':
            return "Jul";
        case '08':
            return "Aug";
        case '09':
            return "Sep";
        case 10:
            return "Oct";
        case 11:
            return "Nov";
        case 12:
            return "Dec";          
    }
}

function getMonthNumber(monthName)
{
    switch (monthName)
    {
        case 'Jan':
            return 1;
        case 'Feb':
            return 2;
        case 'Mar':
            return 3;
        case 'Apr':
            return 4;
        case 'May':
            return 5;
        case 'Jun':
            return 6;
        case 'Jul':
            return 7;
        case 'Aug':
            return 8;
        case 'Sep':
            return 9;
        case 'Oct':
            return 10;
        case 'Nov':
            return 11;
        case 'Dec':
            return 12;          
    }
}

function IsIE()
{
	var agt=navigator.userAgent.toLowerCase();
	var re = /\bmsie\b/gi;
	return re.test( agt );
}

// Attach mouseover and mouseout events to all images that have an helptext attribute.
function AttachHelpEvents()
{
	for ( var index = 0 ; ++index != document.images.length; )
	{
		var img = document.images[index];	
							
		if ( img.getAttribute ( "helptext" ) )
		{				   
			addEvent ( img, "mouseover", displayHelpText );
			addEvent ( img, "mouseout" , hideHelpText );
		}		
	}
}

function displayHelpText(objEvent)
{          					      
   var objImage = getEventSrc(objEvent); 
   var imageID = objImage.id;         
   // DIV used to hold the display help text
   objHelpBox = document.getElementById('help-box');
   // Used to display help text when help icons are moused over
   objHelpContainer = document.getElementById('help-text');       
   objHelpContainer.innerHTML = "";   
   objHelpContainer.innerHTML = unescape(objImage.getAttribute("helptext"));
   objHelpBox.style.display = "block";
}

function hideHelpText()
{
   objHelpBox = document.getElementById('help-box');
   objHelpBox.style.display = "none";             
}

//compare two strings
function equals( str1, str2 )
{
	str1 = trim( str1 ).toLowerCase();
	str2 = trim( str2 ).toLowerCase();
	return str1 == str2;
}

function MainExceptionHandler(e)
{
   alert("The following error has occured:\n\n" + e);
}

function SelectSingleNodeValue(domDocument, xpath)
{
	if(domDocument == null || xpath == null) return '';
	var node = domDocument.selectSingleNode(xpath);
	if(node == null) return '';
	return node.nodeValue;
}

// Implementation of loadXML for non-IE browsers
if(!IsIE()) {
	Document.prototype.loadXML = function (s) {
	      
		// parse the string to a new doc   
		var doc2 = (new DOMParser()).parseFromString(s, "text/xml");
		      
		// remove all initial children
		while (this.hasChildNodes())
			this.removeChild(this.lastChild);
		         
		// insert and import nodes
		for (var i = 0; i < doc2.childNodes.length; i++) {
			this.appendChild(this.importNode(doc2.childNodes[i], true));
		}
	}
}

// Implementation of selectNodes and selectSingleNodes for Non-IE browsers
if( document.implementation.hasFeature("XPath", "3.0") )
{
   // prototying the XMLDocument
   XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
   {
      if( !xNode ) { xNode = this; } 
      var oNSResolver = this.createNSResolver(this.documentElement)
      var aItems = this.evaluate(cXPathString, xNode, oNSResolver, 
                   XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
      var aResult = [];
      for( var i = 0; i < aItems.snapshotLength; i++)
      {
         aResult[i] =  aItems.snapshotItem(i);
      }
      return aResult;
   }

   // prototying the Element
   Element.prototype.selectNodes = function(cXPathString)
   {
      if(this.ownerDocument.selectNodes)
      {
	     return this.ownerDocument.selectNodes(cXPathString, this);
      }
      else{throw "For XML Elements Only";}
   }
   
   // prototying the XMLDocument
   XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
   {
      if( !xNode ) { xNode = this; } 
      var xItems = this.selectNodes(cXPathString, xNode);
      if( xItems.length > 0 )
      {
         return xItems[0];
      }
      else
      {
         return null;
      }
   }
   
   // prototying the Element
   Element.prototype.selectSingleNode = function(cXPathString)
   {	
      if(this.ownerDocument.selectSingleNode)
      {
         return this.ownerDocument.selectSingleNode(cXPathString, this);
      }
      else{throw "For XML Elements Only";}
   }
}

function transfer ( objEventSrc )
{   
   var strSource = objEventSrc.getAttribute ( "srcId" );
   var strDestination = objEventSrc.getAttribute ( "destId" );       
   var objSource;
   var objDestination = document.getElementById ( strDestination );   
     
   if ( strSource == null )
   {    
      objSource = objEventSrc;      
   }      
   else
   {
      objSource = document.getElementById ( strSource );
   }   
   
   transferSelected ( objSource, objDestination );
}

function transferSelected ( fromObject, toObject )
{
   for ( var i=0; i<fromObject.options.length; i++ )
   {  
      if ( fromObject.options[i].selected )	  
      {
         addOption ( toObject, fromObject.options[i].text, fromObject.options[i].value );
      }
   }
   
   deleteSelected ( fromObject );   
}

function deleteSelected ( object )
{
   for ( var i = object.options.length - 1; i >- 1; i-- )
   {
      if ( object.options[i].selected )
      {
         deleteOption ( object, i );
      }
   }
}

function selectAll ( objSelectAll )
{   
	if (objSelectAll != null)
	{
		for ( var i=0; i<objSelectAll.options.length; i++ )
		{
			objSelectAll.options[i].selected = true;      
		}	
	}	
}

function addOption ( object, text, value )
{
   var defaultSelected = false;
   var selected = false;
   var optionName = new Option ( text, value, defaultSelected, selected );
   object.options[object.length] = optionName;
   object.options[object.length-1].title = text;   
}

function deleteOption ( object, index )
{
   object.options[index] = null;
}

function trim(str)
{
    str = this != window ? this : str;
    return str.replace(/^\s+/g, "").replace(/\s+$/g, "");
}

// Adds trim functionality to String object
String.prototype.trim = trim;

function createXmlDocument()
{
    if(IsIE())
        return new ActiveXObject("Microsoft.XMLDOM");
    else
        return document.implementation.createDocument("", "", null);
}

function serializeXmlDocument(xmlDocument)
{
    if(IsIE())
    {
        return xmlDocument.xml;
    }
    else
    {
        var xmlSerializer = new XMLSerializer();
        return xmlSerializer.serializeToString(xmlDocument);
    }
}