// JavaScript Document

/***************************************************************
 * function toggle(id, msgOff, msgOn)
 *
 * Takes the id of a <div> and sets the style display
 * equal to "block" or "none"
 *
 * If L does not exist, display should be set to "none;"
 * some browsers may issue a warning.
 ***************************************************************/

function toggle(id, msgOff, msgOn) {
        var L = document.getElementById("L"+id);
        var I = document.getElementById("I"+id);
        if (L.style.display == "" || L.style.display == "none") {
                L.style.display = "block";
                I.src = "/images/up.png";
                document.getElementById("spanblurb"+id).innerHTML = msgOn;
        } // end IF
        else {
                L.style.display = "none";
                I.src = "/images/down.png";
                document.getElementById("spanblurb"+id).innerHTML = msgOff;
        } // end ELSE
} // end FUNCTION toggle(id)

function getEmployeeDiv() {

 	 var id="all";
  	id = gup("id");
	

	if (id===null || id==null || id=="") {
		id=="all";
	}
	
	if (id=="all" || id=="" || id===null) {
		return;
	}
	else {
	    
		var myConsultant = document.getElementById(id).innerHTML;
		document.getElementById("allConsultants").style.display = "none";
		document.getElementById('oneConsultant').innerHTML = ('<div id="' + id + '" class="consultant">' + myConsultant + '</div>');
	}
		
} // end FUNCTION getEmployeeDiv

function getEmployee(rt, id) {
	rt=eval(rt);
	alert(rt);

	if (id===null || id=="") {
		id=="all";
	}

	alphaArray = new Array();
	idArray = new Array();

		for(var i=0;i < rt.length; i++) {
			strTo = "<h3>" + rt.first[i].value;
			strTo += " " + rt.last[i].value + "</h3>";
			strTo += "<p>" + rt.title[i].value + "<br />";
			strTo += "Telephone: " + rt.telephone[i].value + ", extension " + rt.extension[i].value + "<br /> ";
			strTo += "Fax / E-Fax: " + rt.fax[i].value + " / "  + rt.efax[i].value;
			email = rt.id[i].value + "@madisonpension.com";
			strTo += "<br />Email: <a href=\"mailto:" + email + "\">" + email + "</a></p>";
			
			if (rt.id[i].value == id) {
				document.write(strTo);
				strTo = "";
			}
			else if (id == "all") {
				//document.write(strTo);
				alphaArray[rt.last[i].value] = strTo;
				idArray.push(rt.last[i].value);
			}
		} // end FOR
		
		if (id=="all") {
			// sort the array, then output
			idArray.sort();
			for (i=0; i<idArray.length; i++) {
				document.write(alphaArray[idArray[i]]);
				if (i != (idArray.length) -1) 
					document.write("<hr />");
			}
		} 
	

} // end FUNCTION getEmployee

function getEmployees(id) {
    var xmlHttpReq = false;
    var self = this;
    var rt;
    var strURL = 'mps-json.js';

   if (window.XMLHttpRequest) { // Mozilla/Safari
                xmlHttpReq = new XMLHttpRequest();
    } // if mozilla
    else if (window.ActiveXObject) { // IE
                xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    } // if msie

    try {
                alert('opening ' + strURL);
                xmlHttpReq.open("GET", strURL, true);
                //alert('successfully opened file');
        } // end try

        catch (e) {
                alert(e);
    } // end catch    xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    	xmlHttpReq.onreadystatechange = function() {
        if (xmlHttpReq.readyState == 4) {
                getEmployee(xmlHttpReq.responseText, id);
		alert('finished');
        } // if 4 / ready
    } //  onreadystatechange

        // clear
   xmlHttpReq.send(null);

} // end FUNCTION getEmployees 


/* from netlobo -- thanks! */
function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
 
  if( results == null )
    return "";
  else
    return results[1];
}
