/////////////////////////////
// Prototypes
/////////////////////////////

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

/////////////////////////////
// Common Functions
/////////////////////////////
var popupAllowed;

function getSiteElement(elemID) {
	return document.getElementById(elemID);
}

function showSiteElement(elemID,dispStyle) {
	if( !dispStyle ) { dispStyle = "block"; }
	var elem = getSiteElement(elemID);
	
	if( elem ) {
		elem.style.display = dispStyle;
	}
	else {
		alert("Unable to show '"+elemID+"'  - Element is NULL");
	}
}

function hideSiteElement(elemID) {
	var elem = getSiteElement(elemID);
		
	if( elem ) {
		elem.style.display = "none";
	}
	else {
		alert("Unable to hide '"+elemID+"'  - Element is NULL");
	}

}

function showBusyIndicator(promptMsg) {
	var glassPane = getSiteElement("busyGlassPane");
	
	if( glassPane ) {
		
		var prompt = getSiteElement("busyPrompt");
		if( prompt ) {
			prompt.innerHTML = ( promptMsg ) ? promptMsg : "Please Wait ...";	
		}

		glassPane.style.display = "block";
	}
}

function hideBusyIndicator() {
	var glassPane = getSiteElement("busyGlassPane");
	
	if( glassPane ) {
		glassPane.style.display = "none";
	}
}

function getElementsByClassName(classname, node) {
	if(!node) node = document.getElementsByTagName("body")[0];
	var a = [];
	var re = new RegExp('\\b' + classname + '\\b');
	var els = node.getElementsByTagName("*");
	for(var i=0,j=els.length; i<j; i++)
		if(re.test(els[i].className))a.push(els[i]);
	return a;
}

function resetScroll() {
	//alert("Reset Scroll!");
	top.scrollTo(0,0);
}

function registerButtons(clsName,btnFldr,imgSfx) {
	var imgs = document.getElementsByTagName("img");

	for( i=0; i<imgs.length; i++ ) {
	    if( imgs[i].className == clsName ) {
		    imgs[i].src = btnFldr + "/" + imgs[i].name + imgSfx;
		    imgs[i].style.border = "none";
		    
		    //alert("Registering: "+imgs[i].src);
		
		    imgs[i].onmouseover = function() {
			    this.src = btnFldr + "/" + this.name + "_over" + imgSfx;
		    }
		
		    imgs[i].onmouseout = function() {
			    this.src = btnFldr + "/" + this.name + imgSfx;
		    }
		}
	}
}

function selectNavigation(cbo) {
	var URL = cbo.options[cbo.selectedIndex].value;
	
	if( URL == "#" ) {
		cbo.selectedIndex = 0;
	}
	
	window.location.href = URL;	
}

function loadPageContent(pg,args) {
	var loc = pg+".html";

	if( args != null ) {
		loc = loc + "?" + args;
	}

	top.frames['PageContent'].location = loc;
}

function loadSiteSidebar(nm) {
	top.frames['SiteSidebar'].location = "SB62_SiteSidebar.html?"+nm;
}

function openPhotoGallery(nm) {
	//alert("Opening Photo Gallery '"+nm+"'");
	var galleryWindow = window.open("PhotoGallery.html?"+nm, "GalleryWindow", "width=1024px,height=768px");
	galleryWindow.focus(); 
}

function openPopupWindow(url,name,width,height) {
	var popWindow = window.open(url, name, "width="+width+",height="+height);
	popWindow.focus(); 
}

function getAbsoluteDivs() {
    var arr = new Array();
    var all_divs = document.body.getElementsByTagName("DIV");
    var j = 0;

    for( i = 0; i < all_divs.length; i++ ) {
        if( all_divs.item(i).style.position=='absolute') {
            arr[j] = all_divs.item(i);
            j++;
        }
	}

    return arr;
}

function bringToFront(idx) {
    if( !document.getElementById || !document.getElementsByTagName )
        return;
        
    //alert("bringToFront("+idx+")");

    var obj = document.getElementById('DIV'+idx);
    
    var divs = getAbsoluteDivs();
    var max_index = 0;
    var cur_index;

    // Compute the maximal z-index of
    // other absolute-positioned divs
    for( i = 0; i < divs.length; i++ ) {
        var item = divs[i];
        if( item == obj || item.style.zIndex == '')
            continue;

        cur_index = parseInt(item.style.zIndex);
        if( max_index < cur_index ) {
            max_index = cur_index;
        }
    }

    obj.style.zIndex = max_index + 1;
}

function sendToBack(idx) {
    if( !document.getElementById || !document.getElementsByTagName )
        return;
        
    //alert("sendToBack("+idx+")");

    var obj = document.getElementById('DIV'+idx);
    var divs = getAbsoluteDivs();
    var min_index = 999999;
    var cur_index;

    if( divs.length < 2 )
        return;

    // Compute the minimal z-index of
    // other absolute-positioned divs
    for( i = 0; i < divs.length; i++ ) {
        var item = divs[i];
        if( item == obj )
            continue;

        if( item.style.zIndex == '') {
            min_index = 0;
            break;
        }

        cur_index = parseInt(item.style.zIndex);
        if( min_index > cur_index ) {
            min_index = cur_index;
        }
    }

    if( min_index > parseInt(obj.style.zIndex) ) {
        return;
    }

    obj.style.zIndex = 1;

    if( min_index > 1 )
        return;

    var add = min_index == 0 ? 2 : 1;

    for( i = 0; i < divs.length; i++ ) {
        var item = divs[i];
        if( item == obj )
            continue;

        item.style.zIndex += add;
    }
}

function getAjaxRequest() {
    var xmlHttp=null;

    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer
        try {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    
    return xmlHttp;
}

function sendAjaxRequest(url,callback,asXML) { 
    //alert("sendAjaxRequest("+url+")  XML? "+asXML);
    
    var xmlHttp = getAjaxRequest();

    if ( xmlHttp == null ) {
        alert ("ERROR: Your browser does not support AJAX!");
        return;
    } 

    xmlHttp.onreadystatechange=function() {
        if ( xmlHttp.readyState == 4 ) { 
            //alert("Response is Ready!");

    	    // invoke the callback (if defined)
    	    if( typeof callback != 'undefined' && callback != '' ) {
                funcRef = window[callback];
                if( 'function' == typeof funcRef ) {
                    funcRef(asXML ? xmlHttp.responseXML : xmlHttp.responseText);
	        }
    	    }
        }
   }

   var timestamp = new Date();
   var uniqueURL = url+ (url.indexOf("?") > 0 ? "&" : "?")+ "timestamp="+ timestamp.getTime();
   xmlHttp.open("GET",uniqueURL, true);
   xmlHttp.send(null);
}

function parseScript(_source) {
		var source = _source;
		var scripts = new Array();
		
		// Strip out tags
		while(source.indexOf("<script") > -1 || source.indexOf("</script") > -1) {
			var s = source.indexOf("<script");
			var s_e = source.indexOf(">", s);
			var e = source.indexOf("</script", s);
			var e_e = source.indexOf(">", e);
			
			// Add to scripts array
			scripts.push(source.substring(s_e+1, e));
			// Strip from source
			source = source.substring(0, s) + source.substring(e_e+1);
		}
		
		// Loop through every script collected and eval it
		for(var i=0; i<scripts.length; i++) {
			try {
				eval(scripts[i]);
			}
			catch(ex) {
				// do what you want here when a script fails
			}
		}
		
		// Return the cleaned source
		return source;
	}

function ahah(url, target) {
	//alert("ahah - Target: "+target);
	
	document.getElementById(target).innerHTML = 'Fetching data...';
  
    var xmlHttp = getAjaxRequest();
    var tgt = target;

    if ( xmlHttp == null ) {
        alert ("ERROR: Your browser does not support AJAX!");
        return;
    } 
  
    xmlHttp.onreadystatechange=function() {
		if (xmlHttp.readyState == 4) { // only if req is "loaded"
			if (xmlHttp.status == 200) { // only if "OK"
				alert(xmlHttp.responseText);
				document.getElementById(tgt).innerHTML = parseScript(xmlHttp.responseText);
			}
			else {
				document.getElementById(tgt).innerHTML=" AHAH Error:\n"+ xmlHttp.status + "\n" +xmlHttp.statusText;
			}
		}
	}

	var timestamp = new Date();
	var uniqueURL = url+ (url.indexOf("?") > 0 ? "&" : "?")+ "timestamp="+ timestamp.getTime();
	xmlHttp.open("GET",uniqueURL, true);
	xmlHttp.send(null);  
}  

function loadDivPage(target, url) {
	//alert("loadDivPage");
	ahah(url,target);
	return false;
}

function loadPageContent(url) {
	alert("loadPageContent: " + url);
	loadDivPage('PageContent',url);
}

function checkPopup(winObj,title) {
	popupOK = true;
	if( winObj == null || typeof(winObj) == "undefined" ) {
		popupOK = false;
		alert("The " + title + " window is currently being blocked!\n\nPlease allow popup windows for this site.");
	}
	return popupOK;
}


function 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 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 eraseCookie(name) {
	createCookie(name,"",-1);
}

function cookiesEnabled() {
	var cookieNm = "chkcookie";
	var tmpcookie = new Date();
			
	createCookie(cookieNm,tmpcookie.getTime());
	
	var enabled = readCookie(cookieNm);
	
	if( enabled ) {
		eraseCookie(cookieNm);
	}
	
	return enabled;
}
