// this routine runs on every page, and locates SPAN elements with a class of phone_number
// where found, it then formats the innerText of the phone number to a single, consistent
// format


addListener(this, "load", function() { formatPhoneNumbers(); });
addListener(document, "load", function() { formatPhoneNumbers(); });

function highlight() {
    if (window.event) {
        // get the cell/table row DOM object that triggered the event
        var strEventType = window.event.type || event.type;
        blnRemoveHighlight = strEventType == 'mouseout';
        var objCell = window.event.target || event.srcElement;
        objRow = objCell.parentNode;
        // An image, link or other containing element will skew this, as they become the 
        // event source, rather than the cell.  Traverse up the heirarchy until we can 
        // find the actual cell and row
        while (objRow.tagName != 'TR' && typeof objRow.parentNode == 'object') {
            objCell = objRow;
            objRow = objCell.parentNode;
        }
        // Make sure we were able to retrieve the correct		
        if (objRow.tagName == 'TR') {

            for (var intCellIndex = 0; intCellIndex < objRow.cells.length; intCellIndex++) {
                if (blnRemoveHighlight) {
                    objRow.cells[intCellIndex].style.backgroundColor = '';
                    objRow.cells[intCellIndex].style.color = '';
                } else {
                    objRow.cells[intCellIndex].style.backgroundColor = 'rgb(200,205,255)';
                    objRow.cells[intCellIndex].style.color = 'white';
                }
            }
            if (!blnRemoveHighlight) {
                // make sure we unhighlight when mouse moves off:
                if (typeof objCell.onmouseout == 'function') {
                    //objCell.onmouseout();
                    //objCell.highlight(true);
                } else {
                    objCell.onmouseout = highlight;
                }
            }
        } else {
            // unexpected tag ...

        }
    }
}


function formatPhoneNumbers() {
	var enumSpans = getElementsByClassName('phone_number');
	for (var intIndex = 0; intIndex < enumSpans.length; intIndex ++ ) {
		var strPhoneNo = enumSpans[intIndex].innerText;
		if (strPhoneNo.length > 0 ) {
			enumSpans[intIndex].innerText = '(' + strPhoneNo.substring(0,3) + ') ' + 
				strPhoneNo.substring(3,6) + '-' + strPhoneNo.substring(6,10)
			if ( strPhoneNo.length > 10 ) {
				enumSpans[intIndex].innerText = enumSpans[intIndex].innerText + ' x ' + strPhoneNo.substring(10)
			}
		}
		enumSpans[intIndex].className = '';
	}
}



function addListener(element, event, listener, bubble) {
  if(element.addEventListener) {
    if(typeof(bubble) == "undefined") bubble = false;
    element.addEventListener(event, listener, bubble);
  } else if(this.attachEvent) {
    element.attachEvent("on" + event, listener);
  }
}

function showHideDiv (objDiv) {
	if (objDiv) {
		if (objDiv.style.visibility != 'visible') {
			objDiv.style.display = 'block';
			objDiv.style.visibility = 'visible';
		} else {
			objDiv.style.display = 'none';
			objDiv.style.visibility = 'hidden';
		}
	}
}

// open a pdf document in a new window
// URL can be local or www
function getPdfDocument( strUrlToPdf ) {
	var pdfWindow = window.open( "viewpdf.asp?pdf=" + strUrlToPdf, "PBCFRPDF","channelmode=no,directories=no,height=600,width=800,location=yes,menubar=no,status=no,toolbar=no")
}

function getPptDocument( strUrlToPpt ) {
	var pptWindow = window.open( "viewppt.asp?ppt=" + strUrlToPpt, "PBCFRPPT","channelmode=no,directories=no,height=600,width=800,location=yes,menubar=no,status=no,toolbar=no")
}

function getMap( strAddressInfo, strMapTitle ) {
	var mapWindow = window.open("mapping.asp?title=" + strMapTitle + "&addr=" + strAddressInfo, "PBCFRMAP","channelmode=no,directories=no,height=360,width=525,location=no,menubar=no,status=no,toolbar=no,resizable=yes");
	mapWindow.focus();
}

function showStatus (strStatusText ) {
	window.status = strStatusText;
}

function setClickableFocus( obj ) {
	obj.style.className = 'clickablehover'
}

function getElementsByClassName(strClass, strTag, objContElm) {
  strTag = strTag || "*";
  objContElm = objContElm || document;    
  var objColl = objContElm.getElementsByTagName(strTag);
  if (!objColl.length &&  strTag == "*" &&  objContElm.all) objColl = objContElm.all;
  var arr = new Array();                              
  var delim = strClass.indexOf('|') != -1  ? '|' : ' ';   
  var arrClass = strClass.split(delim);    
  for (var i = 0, j = objColl.length; i < j; i++) {                         
    var arrObjClass = objColl[i].className.split(' ');   
    if (delim == ' ' && arrClass.length > arrObjClass.length) continue;
    var c = 0;
    comparisonLoop:
    for (var k = 0, l = arrObjClass.length; k < l; k++) {
      for (var m = 0, n = arrClass.length; m < n; m++) {
        if (arrClass[m] == arrObjClass[k]) c++;
        if ((delim == '|' && c == 1) || (delim == ' ' && c == arrClass.length)) {
          arr.push(objColl[i]); 
          break comparisonLoop;
        }
      }
    }
  }
  return arr; 
}

// To cover IE 5 Mac lack of the push method
Array.prototype.push = function(value) {this[this.length] = value; };

function URLEncode(strEnodeText)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = strEnodeText;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for
	return encoded;
};

function URLDecode( strTextToDecode )
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var encoded = strTextToDecode;
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
  return plaintext;
};

