// common.js
// Summary: JavaScript include that provides common web and browsing functionality
// Remarks: All variables and methods are prefixed by "cmn_" to prevent clashing of names
// with other scripts


//-----------------------------------------
// BEGIN: Path management

// Local variables
var cmn_rootWebPath = "/MDHOWSBS/";
var cmn_absoluteRootWebPath = "localhost/MDHOWSBS/";
var cmn_rootLinkWebPath = "/MDHOWSBS/";

// Summary: Sets web path to root of site following the server name in the Url.
// Client side scripts can use this value to format correct path whether the 
// site is running either under root domain name "/" or within a virtual directory
function cmn_setRootWebPath (rootWebPath)
{
	cmn_rootWebPath = rootWebPath;
}


// Summary: Gets web path to root of site
function cmn_getRootWebPath ()
{
	return cmn_rootWebPath;
}
// Summary: Sets web path to root of site following the server name in the Url.
// Client side scripts can use this value to format correct path whether the 
// site is running either under root domain name "/" or within a virtual directory
function cmn_setLinkRootWebPath (rootWebPath)
{
	cmn_rootLinkWebPath = rootWebPath;
}


// Summary: Gets web path to root of site
function cmn_getLinkRootWebPath ()
{
	return cmn_rootLinkWebPath;
}


// Summary: Sets full web path, including server name, to root of site
function cmn_setFullRootWebPath (fullRootWebPath)
{
	cmn_fullRootWebPath = fullRootWebPath;
}

// Summary: Gets full web path, including server name, to root of site
function cmn_getFullRootWebPath ()
{
	return cmn_fullRootWebPath;
}

// Summary: Gets web path to a file based on a site-root relative path
function cmn_getWebPath (path)
{
	return cmn_combineWebPath(cmn_getRootWebPath(), path);
}

// Summary: Gets full web path, including server name to a file within the
// site based on a site-root relative path
function cmn_getFullWebPath (path)
{
	return cmn_combineWebPath("http://" + cmn_getFullRootWebPath(), path);
}

// Summary: Gets absolute URL, including server name to a file within the
// site based on a site-root relative path
function cmn_getFullWebPathSecure (path)
{
	return cmn_combineWebPath("https://" + cmn_getFullRootWebPath(), path);
}

// Summary: Combines 2 parts of a web path into a valid path, removing
// any double forward slashes
function cmn_combineWebPath(path1, path2)
{
	var result;
		
	result = path1.charAt(path1.length - 1) != "/" 
		? path1 + "/" : path1;
	result += path2.charAt(0) == "/"
		? path2.substring(1) : path2;
	return result;
}

// END: Path management
//-----------------------------------------


//-----------------------------------------
// BEGIN: DOM manipulation

function cmn_showElement(elementId)
{
	var el = document.getElementById(elementId);
	el.style.display = "block";
}

function cmn_hideElement(elementId)
{
	var el = document.getElementById(elementId);
	el.style.display = "none";
}

// END: DOM manipulation
//-----------------------------------------


//-----------------------------------------
// BEGIN: Event firing

function cmn_attachEvent(obj,evt,fnc,useCapture){
	if (!useCapture) useCapture=false;
	if (obj.addEventListener)
	{
		obj.addEventListener(evt,fnc,useCapture);
		return true;
	}
	else if (obj.attachEvent)
	{
		return obj.attachEvent("on"+evt,fnc);
	}
	else
	{
		cmn_attachEvent2(obj,evt,fnc);
		obj['on'+evt]=function(){ cmn_fireEvent(obj,evt) };
	}
} 

//The following are for browsers like NS4 or IE5Mac which don't support either
//attachEvent or addEventListener
function cmn_attachEvent2(obj,evt,fnc){
	if (!obj.myEvents) obj.myEvents={};
	if (!obj.myEvents[evt]) obj.myEvents[evt]=[];
	var evts = obj.myEvents[evt];
	evts[evts.length]=fnc;
}
function cmn_fireEvent(obj,evt){
	if (!obj || !obj.myEvents || !obj.myEvents[evt]) return;
	var evts = obj.myEvents[evt];
	for (var i=0,len=evts.length;i<len;i++) evts[i]();
}

// END: Event firing
//-----------------------------------------



//-----------------------------------------
// BEGIN: Cookie management

var cmn_acceptsCookies = false;
if(document.cookie == '') {
    document.cookie = 'cmn_acceptsCookies=yes'; // Try to set a cookie.
    if(document.cookie.indexOf('cmn_acceptsCookies=yes') != -1) {
	cmn_acceptsCookies = true; 
    }// If it succeeds, set variable
} else { // there was already a cookie
  cmn_acceptsCookies = true;
}


function cmn_setCookie (name, value, hours, path, domain, secure) {
    if (cmn_acceptsCookies) { // Don't waste your time if the browser doesn't accept cookies.
	var not_NN2 = (navigator && navigator.appName 
		       && (navigator.appName == 'Netscape') 
		       && navigator.appVersion 
		       && (parseInt(navigator.appVersion) == 2))?false:true;

	if(hours && not_NN2) { // NN2 cannot handle Dates, so skip this part
	    if ( (typeof(hours) == 'string') && Date.parse(hours) ) { // already a Date string
		var numHours = hours;
	    } else if (typeof(hours) == 'number') { // calculate Date from number of hours
		var numHours = (new Date((new Date()).getTime() + hours*3600000)).toGMTString();
	    }
	}
	document.cookie = name + '=' + escape(value) + ((numHours)?(';expires=' + numHours):'') + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:'') + ((secure && (secure == true))?'; secure':''); // Set the cookie, adding any parameters that were specified.
    }
} // cmn_setCookie


function cmn_readCookie(name) {
    if(document.cookie == '') { // there's no cookie, so go no further
	return false; 
    } else { // there is a cookie
	var firstChar, lastChar;
	var theBigCookie = document.cookie;
	firstChar = theBigCookie.indexOf(name);	// find the start of 'name'
	var NN2Hack = firstChar + name.length;
	if((firstChar != -1) && (theBigCookie.charAt(NN2Hack) == '=')) { // if you found the cookie
	    firstChar += name.length + 1; // skip 'name' and '='
	    lastChar = theBigCookie.indexOf(';', firstChar); // Find the end of the value string (i.e. the next ';').
	    if(lastChar == -1) lastChar = theBigCookie.length;
	    return unescape(theBigCookie.substring(firstChar, lastChar));
	} else { // If there was no cookie of that name, return false.
	    return false;
	}
    }	
} // cmn_readCookie

function cmn_killCookie(name, path, domain) {
  var theValue = cmn_readCookie(name); // We need the value to kill the cookie
  if(theValue) {
      document.cookie = name + '=' + theValue + '; expires=Fri, 13-Apr-1970 00:00:00 GMT' + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:''); // set an already-expired cookie
  }
} 

// END: Cookie management
//-----------------------------------------


//-----------------------------------------
// BEGIN: Popup management

function cmn_openPageInParentWindow(url) { 
  if (window.opener && !window.opener.closed)
    window.opener.document.location.href = url;
} 

 
function cmn_openPopup(URL,newWin,top,left) 
{
    cmn_openPopupSized(URL,newWin,top,left,730,500)  
}
 
 function cmn_openPopupSized(URL,newWin,top,left,width,height) 
{
  day = new Date();
  eval(newWin + " = window.open(URL, '" + newWin +
  "', 'toolbar=0,scrollbars=1,location=0,statusbar=0," +
  "menubar=0,resizable=1,width=" + width + ",height=" + height + ",left = " + top + ",top = " + left + "');");
 }
 
// END: Popup management
//-----------------------------------------


//-----------------------------------------
// BEGIN: Link management
 
// Summary: Replaces url on anchor element with a JavaScript function
// Remarks: Allows a HTML link to function as a normal link that is replaced with enhanced
// scripting functionality if JavaScript enabled. 
// Remarks: Note that using onclick="myfunction();return false" 
// to prevent navigation to a link's destination href is not supported in FireFox
// Parameters:
// elementId: id of the anchor element to update
// js: code to add to the href attribute after the "javascript: " protocol
function cmn_setAnchorHrefScript (elementId, js)
{
    var el;
    
    if (document.getElementById)
    {
        el = document.getElementById(elementId);
        
        if (el != null)
        {
            el.setAttribute("href","javascript:void(" + js + ")");
            el.setAttribute("target","");
        }
    }
}

// Summary: shortcut method to reduce HTML output in lists etc
function cmn_setAHS (elementId, js)
{
    cmn_setAnchorHrefScript (elementId, js);
    
}

// END: Link management
//-----------------------------------------


//-----------------------------------------
// BEGIN: Default button management

// Track whether default button has been fired
var cmn_defaultFired = false;

// Summary: Triggers click of a button with a specific id when the enter key is pressed
// Remarks: This is based on the standard ASP.Net 2.0 WebResource JavaScript that is used by ASP Panel
// control to provide default button functionality. It was extracted to give more control over the HTML output // (ASP.Net Panels output invalid XHTML)
function cmn_FireDefaultButton(event, target) 
{
	if (!cmn_defaultFired && event.keyCode == 13 && !(event.srcElement && (event.srcElement.tagName.toLowerCase() == "textarea"))) 
	{
	    var defaultButton;
        if (document.all)
        {
            defaultButton = document.all[target];
        }
        else if (document.getElementById)
        {
            defaultButton = document.getElementById(target);
        }
        
        if (defaultButton && typeof(defaultButton.click) != "undefined") 
        {
			cmn_defaultFired = true;
            defaultButton.click();
            event.cancelBubble = true;
            if (event.stopPropagation) event.stopPropagation();
            return false;
        }
    }
    return true;
}

// END: Default button management
//-----------------------------------------

//-----------------------------------------
// BEGIN: Querystring manipulation

//********************************************************************
//********************************************************************
// Client-side access to querystring name=value pairs
//	Version 1.2.2
//	30 Mar 2005
//	Adam Vandenberg

//********************************************************************
function Querystring(qs) // optionally pass a querystring to parse
//********************************************************************
{
	this.params = new Object()
	this.get=Querystring_get
	
	if (qs == null)
		qs=location.search.substring(1,location.search.length)

	if (qs.length == 0) return

// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ')
// parse out name/value pairs separated via &
	var args = qs.split('&')
	
// split out each name=value pair
	for (var i=0;i<args.length;i++)
	{
		var pair = args[i].split('=')
		name = unescape(pair[0])

		var value
		if (pair.length == 2)
			value = unescape(pair[1])
		else
			value = name
		
		this.params[name] = value
	}
}

//********************************************************************
function Querystring_get(key, default_) // 'default' defaults to null
//********************************************************************
{
	// This silly looking line changes UNDEFINED to NULL
	if (default_ == null) default_ = null;
	
	var value=this.params[key]
	if (value==null){value=default_}
	
	return value
} 

// END: Querystring manipulation
//-----------------------------------------


//-----------------------------------------
// BEGIN: Input controls

function cmn_disableButton(buttonId)
{
	var el = document.getElementById(buttonId);
	el.disabled = true;
}

// END: Input controls
//-----------------------------------------
document.write('<style type="text/css">.javascripted { visibility:visible; }</style>');