     
var _ms_XMLHttpRequest_ActiveX = ""; // Holds type of ActiveX to instantiate

if (!window.Node || !window.Node.ELEMENT_NODE) {
    var Node = { ELEMENT_NODE: 1, ATTRIBUTE_NODE: 2, TEXT_NODE: 3, CDATA_SECTION_NODE: 4, ENTITY_REFERENCE_NODE: 5,
                  ENTITY_NODE: 6, PROCESSING_INSTRUCTION_NODE: 7, COMMENT_NODE: 8, DOCUMENT_NODE: 9, DOCUMENT_TYPE_NODE: 10, 
    		  DOCUMENT_FRAGMENT_NODE: 11, NOTATION_NODE: 12 };
}

// From prototype.js @ www.conio.net | Returns an object reference to one or more strings
// ignore the fact that there are no arguments to this method -- javascript doesn't care how many you send (not strongly typed)
// The method checks the actual # of arguments -- returns a single object or an array
function $() {
    var elements = new Array();

    for (var i = 0; i < arguments.length; i++) {
        var element = arguments[i];

        if (typeof element == 'string')
            element = document.getElementById(element);

        if (arguments.length == 1)
            return element;

        elements.push(element);
    }

    return elements;
}


// Method to get text from an XML DOM object
function getTextFromXML( oNode, deep ) {
    var s = "";
    var nodes = oNode.childNodes;

    for (var i = 0; i < nodes.length; i++) {
        var node = nodes[i];

        if (node.nodeType == Node.TEXT_NODE) {
            s += node.data;
        } else if (deep == true && (node.nodeType == Node.ELEMENT_NODE || node.nodeType == Node.DOCUMENT_NODE
                                       || node.nodeType == Node.DOCUMENT_FRAGMENT_NODE)) {
            s += getTextFromXML(node, true);
        };
    }

    ;
    return s;
}




// If you plan on doing anything outside of North America, then you'd better encode the things you pass back and forth
// the escape() method in Javascript is deprecated -- should use encodeURIComponent if available
function encode( uri ) {
    if (encodeURIComponent) {
        return encodeURIComponent(uri);
    }

    if (escape) {
        return escape(uri);
    }
}

function decode( uri ) {
    uri = uri.replace(/\+/g, ' ');

    if (decodeURIComponent) {
        return decodeURIComponent(uri);
    }

    if (unescape) {
        return unescape(uri);
    }

    return uri;
}



/*
 * AJAXRequest: An encapsulated AJAX request. To run, call
 * new AJAXRequest( method, url, async, process, data )
 *
 */

function executeReturn( AJAX ) {
    if (AJAX.readyState == 4) {
        if (AJAX.status == 200) {
	    if ( AJAX.responseText ) {
		    eval(AJAX.responseText);
	    }
	}
    }
}


function AJAXRequest( method, url, data, process, async, dosend) {
    // self = this; creates a pointer to the current function
    // the pointer will be used to create a "closure". A closure
    // allows a subordinate function to contain an object reference to the
    // calling function. We can't just use "this" because in our anonymous
    // function later, "this" will refer to the object that calls the function 
    // during runtime, not the AJAXRequest function that is declaring the function
    // clear as mud, right?
    // Java this ain't
    
    var self = this;

    // check the dom to see if this is IE or not
    if (window.XMLHttpRequest) {
	// Not IE
        self.AJAX = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
	// Hello IE!
        // Instantiate the latest MS ActiveX Objects
        if (_ms_XMLHttpRequest_ActiveX) {
            self.AJAX = new ActiveXObject(_ms_XMLHttpRequest_ActiveX);
        } else {
	    // loops through the various versions of XMLHTTP to ensure we're using the latest
	    var versions = ["Msxml2.XMLHTTP.7.0", "Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP",
                        "Microsoft.XMLHTTP"];

            for (var i = 0; i < versions.length ; i++) {
                try {
		    // try to create the object
		    // if it doesn't work, we'll try again
		    // if it does work, we'll save a reference to the proper one to speed up future instantiations
                    self.AJAX = new ActiveXObject(versions[i]);

                    if (self.AJAX) {
                        _ms_XMLHttpRequest_ActiveX = versions[i];
                        break;
                    }
                }
                catch (objException) {
                // trap; try next one
                } ;
            }

            ;
        }
    }
    
    // if no callback process is specified, then assing a default which executes the code returned by the server
    if (typeof process == 'undefined' || process == null) {
        process = executeReturn;
    }

    self.process = process;

    // create an anonymous function to log state changes
    self.AJAX.onreadystatechange = function( ) {
        self.process(self.AJAX);
    }

    // if no method specified, then default to GET
    if (!method) {
        method = "GET";
    }

    method = method.toUpperCase();

    if (typeof async == 'undefined' || async == null) {
        async = true;
    }

    self.AJAX.open(method, url, async);

    if (method == "POST") {
        self.AJAX.setRequestHeader("Connection", "close");
        self.AJAX.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        self.AJAX.setRequestHeader("Method", "POST " + url + "HTTP/1.1");
    }

    // if dosend is true or undefined, send the request
    // only fails is dosend is false
    // you'd do this to set special request headers
    if ( dosend || typeof dosend == 'undefined' ) {
	    if ( !data ) data=""; 
	    self.AJAX.send(data);
    }
    return self.AJAX;
}


<!--------------------------------- BEGIN "GET FORM VALUES" ------------------------------------>

// Clears each select that was passed to this method
// uses Javascripts dynamic argument capability--method isn't declared with args, they are looped through
// in the code using the built-in arguments array
function clearSelect() {
    for (var i = 0; i < arguments.length; i++) {
        var element = arguments[i];

        if (typeof element == 'string')
            element = document.getElementsByName(element)[0];

        if (element && element.options) {
            element.options.length = 0;
            element.selectedIndex = -1;
        }
    }
}


function getCities( initialVal ) {
//    alert('here');
    return new AJAXRequest("GET", '?action=wantJS&want=cities&initial=' + encode(initialVal));
}

function getDistricts( initialVal ) {
    return new AJAXRequest("GET", '?action=wantJS&want=districts&initial=' + encode(initialVal));
}

/*function getNeighborhoods(initialVal) {
    return new AJAXRequest("GET", '?action=wantJS&want=neighborhoods&initial=' + encode(initialVal));
}

function updateNeighborhood( newVal ) {
    return new AJAXRequest("GET", '?action=wantJS&update=neighborhoood&newVal=' + encode(newVal));
}*/

function updateCity( newVal ) {
    return new AJAXRequest("GET", '?action=wantJS&update=city&newVal=' + encode(newVal));
}


function nameChange(target) {
	 customElement = document.getElementsByName('rename' + target)[0];
	 customElement.className = 'show';
}


nextRowNum = 1;

function addNewRow(numRows) {
	 bigTable = document.getElementById('BIGTABLE');

	 for (i = 1; i <= numRows; i++) {
	     oldrow = document.getElementById('CLONEstuff' + i);
	     neuro = bigTable.insertRow(bigTable.rows.length - 2);

	     for (j = 0; j < oldrow.cells.length; j++) {
	     	 oldcell = oldrow.cells[j];
	     	 newcell = neuro.insertCell(neuro.cells.length);

		 newcell.colSpan = oldcell.colSpan;
		 newcell.rowSpan = oldcell.rowSpan;

		 newcell.innerHTML = oldcell.innerHTML.split('CLONE').join(nextRowNum + 'n');
	     }
	 }

	 nextRowNum++;
	 return false;

}


//
// call changedSel(target) or changedSel(target, DONTdoAJAXlookup)
//
function changedSel() {
	 // adapted to handle target with a number after it (e.g. "schoolDistrict331").
	 // this is to assist with tasks like updateNeighborhoods

    if (changedSel.arguments.length < 1) return;
    target = changedSel.arguments[0];


    element = document.getElementsByName(target + "_txtwe_")[0];

    customElement = document.getElementsByName(target + "_custom")[0];

    if (element.selectedIndex == element.options.length - 1) {
       customElement.className = 'show';

    } else {
      customElement.className = 'hide';
      document.getElementsByName(target + "_txtex_")[0].value = '';

      // a second argument with a true value means do not do Ajax lookup!
      if (changedSel.arguments.length > 1 && changedSel.arguments[1]) return;

      shortTarget = target.substring(0, 4);
      if (shortTarget != 'elem' && shortTarget != 'midd' &&
      	  shortTarget != 'high' && element.selectedIndex != 0) {  // 0 is empty value	

	 URLstring = "?action=wantJS&update=" + target + "&newVal=" + 
    	       			  encode(element.options[element.selectedIndex].value);

      	 if (shortTarget == "scho") {     // short for schoolDistrict

	    // check to see if there is an associated number--
	    maybeNum = target.length != 14 ? target.substring(14) : "";

       	    ce = document.getElementsByName('elementarySchool' + maybeNum)[0];
	    if (ce && ce.saveStuff) { URLstring += "&elem=" + ce.saveStuff; ce.saveStuff = ''; }
       	    ce = document.getElementsByName('middleSchool' + maybeNum)[0];
	    if (ce && ce.saveStuff) { URLstring += "&mid=" + ce.saveStuff; ce.saveStuff = ''; }
       	    ce = document.getElementsByName('highSchool' + maybeNum)[0];
	    if (ce && ce.saveStuff) { URLstring += "&high=" + ce.saveStuff; ce.saveStuff = ''; }

       	 } else if (shortTarget == "city") {

	    maybeNum = target.length != 4 ? target.substring(4) : "";
	    ce = document.getElementsByName('schoolDistrict' + maybeNum + '_txtwe_')[0];
	    if (ce) { URLstring += "&dist=" + encode(ce.options[ce.selectedIndex].value); }
	 }

     	 return new AJAXRequest("GET", URLstring);
      }
    }
}

function setVal(target, value) {
	 element = document.getElementsByName(target)[0];

	 for (i = 0; i < element.options.length; i++) {
	     if (element.options[i].value == value) {
	     	element.selectedIndex = i;
		return;
	     }
	 }

	 element.saveStuff = value;
}


function testMap() {
    city = document.getElementsByName('city_txtwe_')[0];
    city = city.options[city.selectedIndex].value;
    if (city == 'other') city = document.getElementsByName('city_txtex_')[0];
    state = document.getElementsByName('state_txtwe_')[0];
    state = state.options[state.selectedIndex].value;
    if (state == 'other') state = document.getElementsByName('state_txtex_')[0];

    address = document.getElementsByName('address_str_')[0].value;
    zip = document.getElementsByName('zip_str_')[0].value;

    line = address;
    if (address) line += ', ';
    line += city;
    if (city) line += ', ';
    line += state;
    line += ' ';
    line += zip;

    line = encode(line);
    window.open('http://maps.google.com/maps?oi=map&q=' + line, 'mapWindow');

}
