// Custom functions to be available site-wide should go here

// processes obfuscated email addresses, preventing 
// spam harvesters from obtaining the address
function SafeEmail( myEmail ){
	var pieces = myEmail.split("?");
	var suffix = ( pieces.length == 2 ) ? '?' + pieces[1] : '';
	
	var emailAddrs = pieces[0].split(',');
	var emailAddrStr = '';
	for(var i=0;i<emailAddrs.length;i++){
		// Set the e-mail address.
		var oSwaps = emailAddrs[i].match( /^([^\:\:]+)::(.+)/ ) ;
		if ( oSwaps ){
			emailAddrs[i] = oSwaps[2] + '@' + oSwaps[1];
		}
		emailAddrStr += ( emailAddrStr == '' ) ? emailAddrs[i] : ',' + emailAddrs[i];
	}

	document.location = "mailto:" + emailAddrStr + suffix;

}

// open a pop-up window
function popWindow(theUrl, flags ){
	var myFlags = 'toolbar=0,location=0,top=50,left=100,directories=0,status=1,menubar=0,scrollbars=1,resizable=1,width=570,height=600';
	if( flags != null && flags != ""){
		myFlags = flags;
	}
	var theWin=window.open(theUrl,theUrl,myFlags);
	theWin.focus();	
}


// loads the result of fetching the specified URL into the object specified
function loadDynamicContent( theURL, resID ){
	try {
		document.getElementById(resID).innerHTML = "<br><span class='SubSubHead'>LOADING...</span><br><img src='/img/loading.gif'><br>"; 
		ajaxRequest(theURL, resID);
	} catch(e) { 
		alert(e.Description);
	}
}


function getProxiedAddress( theURL ){
    return "/Resources/BCC/proxy.aspx?" + escape(theURL);
}

// performs an AJAX query
function ajaxRequest(url, res) {
	if(!window.ajaxRequests){window.ajaxRequests = Object();}
	var theTime = (new Date()).getTime();
	while(ajaxRequests[theTime]){
		theTime -= 1;
	}
	var theFunction = "var xmlhttp = ajaxRequests['" + theTime + "'];"
	theFunction += "var resDiv = document.getElementById('" + res + "');"
	theFunction += "if( (xmlhttp.readyState == 4) && (xmlhttp.status == 200) ) {";
	theFunction += "resDiv.innerHTML = xmlhttp.responseText;";
	theFunction += "}";
	
	ajaxRequests[theTime] = window.XMLHttpRequest ? new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP"); 
	ajaxRequests[theTime].onreadystatechange = new Function( theFunction );
	ajaxRequests[theTime].open("GET", url); 
	ajaxRequests[theTime].setRequestHeader('referer',document.location.href);
	ajaxRequests[theTime].send(null); 

}



