﻿//-----------------------------FIND ADDRESS - GEOCODE ----------------------------
    var zones = new Array();
    var walkdist = new Array();
    
    //This is an array of all the notransportation zones that are in the shape
    //file.  This is to double check that the schools zone actually exists,
    //if not, the user will be directed to transporation for determination
    //of whether transportaiton is provided.  Should call the service here
    //instead of hard coding array.
    var allZones = new Array("194", "195", "197", "199", "201", "203", "205", "206", "207", "208", "209",
                    "210", "213", "214", "215", "216", "217", "218", "219", "220", "221", "222", "223", "224",
                    "225", "226", "227", "228", "230", "231", "233", "235", "236", "237", "238", "239", "240",
                    "241", "242", "243", "244", "245", "246" ,"247", "248", "249", "250", "251", "252", "253",
                    "254", "255", "256", "257", "258", "259", "260", "261", "262", "263", "264", "265", "266",
                    "267", "269", "270", "271", "274", "275", "276", "277", "278", "279", "280", "281", "282",
                    "283", "284", "285", "287", "288", "289", "290", "291", "292", "293", "294", "295", "296",
                    "297", "298", "299", "401", "405", "406", "407", "408", "409", "410", "411", "412", "413",
                    "414", "415", "416", "417", "418", "419", "423", "424", "426", "450", "451", "452", "455",
                    "456", "457", "458", "459", "473", "475", "491", "602", "605", "682");

   
    function findAddress(address) {
     //Clear the map
     //gmap.clearOverlays();
     //Display all schools with red markers first
     //showAllSchools();  
     
     //Add Denver, CO to address, in order for Google to Geocode properly
        address = address + " Denver, CO";
    
      if (geocoder) {
        geocoder.getLocations(
//        geocoder.getLatLng(
          address,
          function(point) {
            document.getElementById("didyoumean").innerHTML = "";
            if (!point) {
              alert(address + " not found");
            } else {
              

              //the following makes a white icon that will be used to mark the address
            //Also checks to see how many results...
              
              if (point.Placemark.length > 1) {
              //There are more than one result....
              //will place "did you mean" code here
                document.getElementById("didyoumean").innerHTML = "Did you mean: ";
                //loop through the results
                for (var i=0; i<point.Placemark.length; i++){
                    var p = point.Placemark[i].Point.coordinates;
                    document.getElementById("didyoumean").innerHTML += "<br>" + (i+1) + ": <a href='javascript:place(" + p[1] + "," + p[0] + ")'>" + point.Placemark[i].address + "</a>";
                }
              }
              else {
              var p = point.Placemark[0].Point.coordinates;
              var coord = new GLatLng(p[1],p[0]);
              gmap.setCenter(coord, 14);  
//              var coord = point;            
//              gmap.setCenter(point, 14);
              var iconOptions = {}; 
              iconOptions.width = 32; 
              iconOptions.height = 32; 
              iconOptions.primaryColor = "#FFFFFF"; 
              iconOptions.cornerColor = "#AAAAAA"; 
              iconOptions.strokeColor = "#000000"; 
              var icon = MapIconMaker.createMarkerIcon(iconOptions);
              //mark the address location with the white marker
              marker = new GMarker(coord, {icon:icon, title:address});
//              marker = new GMarker(point, {icon:icon, title:address});
              gmap.addOverlay(marker);

                transportationzones = new esri.arcgis.gmaps.Query();
                //see if we are in the transportation or no transportation zones
                transportationzones.queryGeometry = coord;
                transportationzones.outFields = [ "SCHOOL_ID", "WALKDIST" ];
                transportationzonetask.execute(transportationzones, false, tranzonecallback );
                
                
                neighborhood = new esri.arcgis.gmaps.Query();
                //set neighborhood school search parameters
                neighborhood.queryGeometry = coord;
                neighborhood.outFields = "*";
                neighborschooltask.execute(neighborhood, false, myneighborhoodcallback);

                                                
              text1 = "<b>" + address + "</b><br />Your neighborhood schools follow: <br /><i>Click on the school name to pan to that location</i><br />";
              }     
            }
          }
          
        ); 
      }
    }
    
    function place(lat,lng) {
    //This function places the marker when there were more than one option
    var coord = new GLatLng(lat,lng);
    gmap.setCenter(coord, 14);              
              
              var iconOptions = {}; 
              iconOptions.width = 32; 
              iconOptions.height = 32; 
              iconOptions.primaryColor = "#FFFFFF"; 
              iconOptions.cornerColor = "#AAAAAA"; 
              iconOptions.strokeColor = "#000000"; 
              var icon = MapIconMaker.createMarkerIcon(iconOptions);
              //mark the address location with the white marker
              marker = new GMarker(coord, {icon:icon});
              gmap.addOverlay(marker);

                transportationzones = new esri.arcgis.gmaps.Query();
                //see if we are in the transportation or no transportation zones
                transportationzones.queryGeometry = coord;
                transportationzones.outFields = [ "SCHOOL_ID", "WALKDIST" ];
                transportationzonetask.execute(transportationzones, false, tranzonecallback );
                
                
                neighborhood = new esri.arcgis.gmaps.Query();
                //set neighborhood school search parameters
                neighborhood.queryGeometry = coord;
                neighborhood.outFields = "*";
                neighborschooltask.execute(neighborhood, false, myneighborhoodcallback);

                                                
              text1 = "<br />Your neighborhood schools follow: <br /><i>Click on the school name to pan to that location</i><br />";
    
    }

    function myneighborhoodcallback(fset) {
        //Okay, here's the deal... this is very poorly written, all these if/then/else
        //statements.  But I had to throw this in last minute, when I realized there
        //was something I missed.  This really needs to be written... to be more service
        //oriented.  This code is embarassing.  
        fe = fset.features;
        if (fe.length > 0) {
            attributes = fe[0].attributes;
            var transk, trans1, trans2, trans3, trans4, trans5, trans6;
            var trans7, trans8, trans9, trans10, trans11, trans12, largeWindow;
            var i_k, i_1, i_2, i_3, i_4, i_5, i_6, i_7, i_8, i_9, i_10, i_11, i_12;
            //First check is to see if the school has a published "no Tran zone"
            //If there is a zone for the school, then set i_grade=y (as in, YES the 
            //"notransportation zone" exists for that school.  Otherwise
            //user will need to contact transportation to see if bussing is provided.

            //First check to see if in district...
            if (attributes["ELEM_NUM"] == 0) {
                i_k = "o";
                i_1 = "o";
                i_2 = "o";
                i_3 = "o";
                i_4 = "o";
                i_5 = "o";
                i_6 = "o";
                i_7 = "o";
                i_8 = "o";
                i_9 = "o";
                i_10 = "o";
                i_11 = "o";
                i_12 = "o";

                var tabs = [];
                label = "District";
                text = "<div style='height:260px; width:280px'>";
                text = text + "<br />According to our records, you are outside of our district and in ";
                text = text + attributes["ELM_SCHOOL"] + ".<br />";
                text = text + "<br />Contact the <a href='http://soco.dpsk12.org/'>";
                text = text + "School of Choice</a> office for options to ";
                text = text + "attend a Denver Public School.  We have provided phone numbers to neighboring districts on the next tab for your convenience.<br />";

                tabs.push(new GInfoWindowTab(label, text));
                label = "Numbers"
                text = "<div style='height:260px; width:280px'>";                              
                text = text + "Adams County 14 - (303) 289-3949<br />";
                text = text + "Adams-Arapahoe 28J - (303) 344-8060<br />";
                text = text + "Boulder Valley RE2 - (303) 447-1010<br />";
                text = text + "Brighton 27J - (303) 659-4820<br />";
                text = text + "Cherry Creek 5 - (303) 773-1184<br />";
                text = text + "Douglas County RE1 - (303) 688-3195<br />";
                text = text + "Elizabeth C-1 - (303) 646-4441<br />";
                text = text + "Englewood 1 - (303) 761-7050<br />";
                text = text + "Jefferson County R-1 - (303) 982-6500<br />";
                text = text + "Littleton 6 - (303) 347-3300<br />";
                text = text + "Mapleton 1 - (303) 853-1000<br />";
                text = text + "Northglenn-Thornton 12 - (303) 410-8914<br />";
                text = text + "Sheridan 2 - (303) 761-8640<br />";
                text = text + "St. Vrain Valley RE 1J - (303) 449-4978<br />";
                text = text + "Westminster 50 - (303) 428-3511<br />";
                tabs.push(new GInfoWindowTab(label, text));
                marker.openInfoWindowTabsHtml(tabs);
            }


            else {
                for (var j in allZones) {
                    if (attributes["School_K"] == allZones[j]) {
                        i_k = "y";
                    }
                    else if (i_k != "y") {
                        transk = " - Contact <a href='http://transportation.dpsk12.org'>Transportation Department</a> to see if eligible.";
                        i_k = "c";

                    }
                    if (attributes["School_1"] == allZones[j]) {
                        i_1 = "y";
                    }
                    else if (i_1 != "y") {
                        trans1 = " - Contact <a href='http://transportation.dpsk12.org'>Transportation Department</a> to see if eligible.";
                        i_1 = "c";

                    }
                    if (attributes["School_2"] == allZones[j]) {
                        i_2 = "y";
                    }
                    else if (i_2 != "y") {
                        trans2 = " - Contact <a href='http://transportation.dpsk12.org'>Transportation Department</a> to see if eligible.";
                        i_2 = "c";
                    }
                    if (attributes["School_3"] == allZones[j]) {
                        i_3 = "y";
                    }
                    else if (i_3 != "y") {
                        trans3 = " - Contact <a href='http://transportation.dpsk12.org'>Transportation Department</a> to see if eligible.";
                        i_3 = "c";
                    }
                    if (attributes["School_4"] == allZones[j]) {
                        i_4 = "y";
                    }
                    else if (i_4 != "y") {
                        trans4 = " - Contact <a href='http://transportation.dpsk12.org'>Transportation Department</a> to see if eligible.";
                        i_4 = "c";
                    }
                    if (attributes["School_5"] == allZones[j]) {
                        i_5 = "y";
                    }
                    else if (i_5 != "y") {
                        trans5 = " - Contact <a href='http://transportation.dpsk12.org'>Transportation Department</a> to see if eligible.";
                        i_5 = "c";
                    }
                    if (attributes["School_6"] == allZones[j]) {
                        i_6 = "y";
                    }
                    else if (i_6 != "y") {
                        trans6 = " - Contact <a href='http://transportation.dpsk12.org'>Transportation Department</a> to see if eligible.";
                        i_6 = "c";

                    }
                    if (attributes["School_7"] == allZones[j]) {
                        i_7 = "y";
                    }
                    else if (i_7 != "y") {
                        trans7 = " - Contact <a href='http://transportation.dpsk12.org'>Transportation Department</a> to see if eligible.";
                        i_7 = "c";
                    }
                    if (attributes["School_8"] == allZones[j]) {
                        i_8 = "y";
                    }
                    else if (i_8 != "y") {
                        trans8 = " - Contact <a href='http://transportation.dpsk12.org'>Transportation Department</a> to see if eligible.";
                        i_8 = "c";
                    }
                    if (attributes["School_9"] == allZones[j]) {
                        i_9 = "y";
                    }
                    else if (i_9 != "y") {
                        trans9 = " - Contact <a href='http://transportation.dpsk12.org'>Transportation Department</a> to see if eligible.";
                        i_9 = "c";
                    }
                    if (attributes["School_10"] == allZones[j]) {
                        i_10 = "y";
                    }
                    else if (i_10 != "y") {
                        trans10 = " - Contact <a href='http://transportation.dpsk12.org'>Transportation Department</a> to see if eligible.";
                        i_10 = "c";
                    }
                    if (attributes["School_11"] == allZones[j]) {
                        i_11 = "y";
                    }
                    else if (i_11 != "y") {
                        trans11 = " - Contact <a href='http://transportation.dpsk12.org'>Transportation Department</a> to see if eligible.";
                        i_11 = "c";
                    }
                    if (attributes["School_12"] == allZones[j]) {
                        i_12 = "y";
                    }
                    else if (i_12 != "y") {
                        trans12 = " - Contact <a href='http://transportation.dpsk12.org'>Transportation Department</a> to see if eligible.";
                        i_12 = "c";
                    }
                }
                //Check to see if there are any school numbers in the zones array...
                //So if the "no tran zone exists" (ie, i_grade = y), yet the address 
                //is not within any of them (ie, zones<length <1)
                //then transportation is provided.  Then set I_gradelevel = c
                //as in COMPLETE - no more checks necessary.  
                if (zones.length < 1) {
                    if (i_k == "y") {
                        transk = " - Transportation is provided";
                        i_k = "c";
                    }
                    if (i_1 == "y") {
                        trans1 = " - Transportation is provided";
                        i_1 = "c";
                    }
                    if (i_2 == "y") {
                        trans2 = " - Transportation is provided";
                        i_2 = "c";
                    }
                    if (i_3 == "y") {
                        trans3 = " - Transportation is provided";
                        i_3 = "c";
                    }
                    if (i_4 == "y") {
                        trans4 = " - Transportation is provided";
                        i_4 = "c";
                    }
                    if (i_5 == "y") {
                        trans5 = " - Transportation is provided";
                        i_5 = "c";
                    }
                    if (i_6 == "y") {
                        trans6 = " - Transportation is provided";
                        i_6 = "c";
                    }
                    if (i_7 == "y") {
                        trans7 = " - Transportation is provided";
                        i_7 = "c";
                    }
                    if (i_8 == "y") {
                        trans8 = " - Transportation is provided";
                        i_8 = "c";
                    }
                    if (i_9 == "y") {
                        trans9 = " - Transportation is provided*";
                        i_9 = "c";
                    }
                    if (i_10 == "y") {
                        trans10 = " - Transportation is provided*";
                        i_10 = "c";
                    }
                    if (i_11 == "y") {
                        trans11 = " - Transportation is provided*";
                        i_11 = "c";
                    }
                    if (i_12 == "y") {
                        trans12 = " - Transportation is provided*";
                        i_12 = "c";
                    }
                }
                // First check to see if indicator (i_gradelevel) is complete.
                // If it is complete, skip this section. 
                // Check to see if school numbers are in the zones array...
                // Because if they are, then the address falls within the
                // "no transportation zone" and student must walk, or parents must
                // provide transportation.

                if (i_k != "c") {
                    for (var j in zones) {
                        if (attributes["School_K"] == zones[j] && walkdist[j] == 0.9) {
                            transk = " - Transportation is not provided";
                            break;
                        }
                        else {
                            transk = " - Transportation is provided";
                        }
                    }
                }
                if (i_1 != "c") {
                    for (var j in zones) {
                        if (attributes["School_1"] == zones[j] && walkdist[j] == 0.9) {
                            trans1 = " - Transportation is not provided";
                            break;
                        }
                        else {
                            trans1 = " - Transportation is provided";
                        }
                    }
                }
                if (i_2 != "c") {
                    for (var j in zones) {
                        if (attributes["School_2"] == zones[j] && walkdist[j] == 0.9) {
                            trans2 = " - Transportation is not provided";
                            break;
                        }
                        else {
                            trans2 = " - Transportation is provided";
                        }
                    }
                }
                if (i_3 != "c") {
                    for (var j in zones) {
                        if (attributes["School_3"] == zones[j] && walkdist[j] == 0.9) {
                            trans3 = " - Transportation is not provided";
                            break;
                        }
                        else {
                            trans3 = " - Transportation is provided";
                        }
                    }
                }
                if (i_4 != "c") {
                    for (var j in zones) {
                        if (attributes["School_4"] == zones[j] && walkdist[j] == 0.9) {
                            trans4 = " - Transportation is not provided";
                            break;
                        }
                        else {
                            trans4 = " - Transportation is provided";
                        }
                    }
                }
                if (i_5 != "c") {
                    for (var j in zones) {
                        if (attributes["School_5"] == zones[j] && walkdist[j] == 0.9) {
                            trans5 = " - Transportation is not provided";
                            break;
                        }
                        else {
                            trans5 = " - Transportation is provided";
                        }
                    }
                }
                if (i_6 != "c") {
                    for (var j in zones) {
                        if (attributes["School_6"] == zones[j] && walkdist[j] == 2.25) {
                            trans6 = " - Transportation is not provided";
                            break;
                        }
                        else {
                            trans6 = " - Transportation is provided";
                        }
                    }
                }
                if (i_7 != "c") {
                    for (var j in zones) {
                        if (attributes["School_7"] == zones[j] && walkdist[j] == 2.25) {
                            trans7 = " - Transportation is not provided";
                            break;
                        }
                        else {
                            trans7 = " - Transportation is provided";
                        }
                    }
                }
                if (i_8 != "c") {
                    for (var j in zones) {
                        if (attributes["School_8"] == zones[j] && walkdist[j] == 2.25) {
                            trans8 = " - Transportation is not provided";
                            break;
                        }
                        else {
                            trans8 = " - Transportation is provided";
                        }
                    }
                }
                if (i_9 != "c") {
                    for (var j in zones) {
                        if (attributes["School_9"] == zones[j] && walkdist[j] == 3.15) {
                            trans9 = " - Transportation is not provided";
                            break;
                        }
                        else {
                            trans9 = " - Transportation is provided*";
                        }
                    }
                }
                if (i_10 != "c") {
                    for (var j in zones) {
                        if (attributes["School_10"] == zones[j] && walkdist[j] == 3.15) {
                            trans10 = " - Transportation is not provided";
                            break;
                        }
                        else {
                            trans10 = " - Transportation is provided*";
                        }
                    }
                }
                if (i_11 != "c") {
                    for (var j in zones) {
                        if (attributes["School_11"] == zones[j] && walkdist[j] == 3.15) {
                            trans11 = " - Transportation is not provided";
                            break;
                        }
                        else {
                            trans11 = " - Transportation is provided*";
                        }
                    }
                }
                if (i_12 != "c") {
                    for (var j in zones) {
                        if (attributes["School_12"] == zones[j] && walkdist[j] == 3.15) {
                            trans12 = " - Transportation is not provided";
                            break;
                        }
                        else {
                            trans12 = " - Transportation is provided*";
                        }
                    }
                }

                // This is building the window that appears at the address bubble with all the neighborhood schools: 
                var tabs = [];
                label = "ECE-2nd";
                text = "<div style='height:190px; width:380px'>";
                text = text + text1 + "<strong>Preschool</strong>: <b><a href='#' onclick='showInfoWindow(" + attributes["SCHL_EC"] + ")'>" + namefromnumber(attributes["School_ECE"]) + "</a></b> - Transportation is not applicable<br />";
                text = text + "<strong>Kindergarten</strong>: <b><a href='#' onclick='showInfoWindow(" + attributes["SCHL_K"] + ")'>" + namefromnumber(attributes["School_K"]) + "</a></b>" + transk + "<br />";
                text = text + "<strong>1st Grade</strong>: <b><a href='#' onclick='showInfoWindow(" + attributes["SCHL_1"] + ")'>" + namefromnumber(attributes["School_1"]) + "</a></b>" + trans1 + "<br />";
                text = text + "<strong>2nd Grade</strong>: <b><a href='#' onclick='showInfoWindow(" + attributes["SCHL_2"] + ")'>" + namefromnumber(attributes["School_2"]) + "</a></b>" + trans2 + "<br />";
                tabs.push(new GInfoWindowTab(label, text));
                label = "3rd-5th"
                text = "<div style='height:190px; width:380px'>";
                text = text + text1;
                text = text + "<strong>3rd Grade</strong>: <b><a href='#' onclick='showInfoWindow(" + attributes["SCHL_3"] + ")'>" + namefromnumber(attributes["School_3"]) + "</a></b>" + trans3 + "<br />";
                text = text + "<strong>4th Grade</strong>: <b><a href='#' onclick='showInfoWindow(" + attributes["SCHL_4"] + ")'>" + namefromnumber(attributes["School_4"]) + "</a></b>" + trans4 + "<br />";
                text = text + "<strong>5th Grade</strong>: <b><a href='#' onclick='showInfoWindow(" + attributes["SCHL_5"] + ")'>" + namefromnumber(attributes["School_5"]) + "</a></b>" + trans5 + "<br /></div>";
                tabs.push(new GInfoWindowTab(label, text));
                label = "6th-8th"
                text = "<div style='height:190px; width:380px'>";
                text = text + text1 + "6th Grade: <b><a href='#' onclick='showInfoWindow(" + attributes["SCHL_6"] + ")'>" + namefromnumber(attributes["School_6"]) + "</a></b>" + trans6 + "<br />";
                text = text + "7th Grade: <b><a href='#' onclick='showInfoWindow(" + attributes["SCHL_7"] + ")'>" + namefromnumber(attributes["School_7"]) + "</a></b>" + trans7 + "<br />";
                text = text + "8th Grade: <b><a href='#' onclick='showInfoWindow(" + attributes["SCHL_8"] + ")'>" + namefromnumber(attributes["School_8"]) + "</a></b>" + trans8 + "<br />";
                tabs.push(new GInfoWindowTab(label, text));
                label = "9th-12th"
                text = "<div style='height:190px; width:380px'>";
                text = text + text1 + "9th Grade: <b><a href='#' onclick='showInfoWindow(" + attributes["SCHL_9"] + ")'>" + namefromnumber(attributes["School_9"]) + "</a></b>" + trans9 + "<br />";
                text = text + "10th Grade: <b><a href='#' onclick='showInfoWindow(" + attributes["SCHL_10"] + ")'>" + namefromnumber(attributes["School_10"]) + "</a></b>" + trans10 + "<br />";
                text = text + "11th Grade: <b><a href='#' onclick='showInfoWindow(" + attributes["SCHL_11"] + ")'>" + namefromnumber(attributes["School_11"]) + "</a></b>" + trans11 + "<br />";
                text = text + "12th Grade: <b><a href='#' onclick='showInfoWindow(" + attributes["SCHL_12"] + ")'>" + namefromnumber(attributes["School_12"]) + "</a></b>" + trans12 + "<br />";
                text = text + "*Transportation for high school aged students is by means of RTD bus pass.  <a href='http://www.rtd-denver.com'>Click here for RTD Trip Planner</a></div>";

                tabs.push(new GInfoWindowTab(label, text));
                marker.openInfoWindowTabsHtml(tabs);
            }
        }
    }
    
    
    function tranzonecallback(fset){
    //Build an array of transportation zones address is within.

    for (var i=0; i<fset.features.length; i = i + 1)
        {
        zones[i] = fset.features[i].attributes["SCHOOL_ID"];
        walkdist[i] = fset.features[i].attributes["WALKDIST"];
        }
    return zones,walkdist;
    }
    
   
//-----------------------------END FIND ADDRESS - GEOCODE ----------------------------
    