/*
 contains functions for handling ambiguities for maps and routes
 requires that common.js be included
*/

// General

//functions used to output an ambigious address form
//takes ambiguities in the form a location collection
//and a prefix - orig or dest or "" for location

function mq_output_ambiguities(form, locations, prefix) {
   if (locations.count > 0 && mq_ParamExists(locations.getAt[0]) &&
      mq_ParamExists(locations.getAt[0].country) && locations.getAt[0].country.length) {
      createHiddenInput (form, prefix + 'country', prefix + 'country', locations.getAt[0].country);
   }

   mqAmbAddrRow(form, locations, prefix);
   mqAmbCityRow(form, locations, prefix);
   mqAmbStateZipRow(form, locations, prefix);
}

function mqAmbAddrRow(form, locations, prefix) {
    if (locations.status >= 1100 && locations.status < 1120) {
       // createFormSelect (form, prefix + 'addr', 'error', 'Address or Intersection:', prefix + 'address', locations.getAt, 'address');

      var county = (locations.getAt[0])? (locations.getAt[0]).county: "";
   createHiddenInput (form, prefix + 'county', prefix + 'county', county);
   createHiddenInput (form, prefix + 'addr', prefix + 'address', locations.getAt[0].address);

        var div = createDiv (form, 'row', '');
        var label = div.appendChild (document.createElement ('label'));
        label.htmlFor = prefix + 'addr';
        label.appendChild (document.createTextNode ('Address or Intersection:'));
        div.appendChild (document.createElement ('br'));
        var span = createSpan (div, 'error', '');
        var select = span.appendChild (document.createElement ('select'));
    select.name = prefix + 'UnparsedAddress';

        for (x=0; x<locations.count; x++) {
            var option = select.appendChild (document.createElement ('option'));
            var value  = locations.getAt[x].city + "|";
                value += locations.getAt[x].county + "|";
                value += locations.getAt[x].stateProvince + "|";
                value += locations.getAt[x].latitude + "|";
                value += locations.getAt[x].longitude + "|";
                value += locations.getAt[x].postalCode + "|";
                value += locations.getAt[x].address;
            option.value = value;
            var desc  = locations.getAt[x].address;
            option.appendChild (document.createTextNode (desc));
        }
        select.onchange = function () { mqparseSelect(this.options[this.selectedIndex].value, prefix) };

    } else {
        if (locations.count > 0 && mq_ParamExists(locations.getAt[0]) && locations.getAt[0].address) {
            var value = locations.getAt[0].address;
        } else if (mq_ParamExists(av.parameters) && eval("av.parameters."+prefix+"address")) {
            var value = eval("av.parameters."+prefix+"address");
        } else {
            var value = '';
        }
        if (locations.status >= 1000 && locations.status <= 1011) {
            var spanClass = 'error';
        } else {
            var spanClass = '';
        }
        createFormInput (form, prefix + 'addr', spanClass, 'Address or Intersection:', 'text', prefix + 'address', value, '30', '80');
    }
}

function mqAmbCityRow(form, locations, prefix) {
    if(locations.status >= 1130 && locations.status < 1140) {
        createHiddenInput (form, prefix + 'city', prefix + 'city', locations.getAt[0].city);
        createHiddenInput (form, prefix + 'state', prefix + 'stateProvince', locations.getAt[0].stateProvince);
       var county = (locations.getAt[0])? (locations.getAt[0]).county: "";
    createHiddenInput (form, prefix + 'county', prefix + 'county', county);
        var div = createDiv (form, 'row', '');
        var label = div.appendChild (document.createElement ('label'));
            label.htmlFor = prefix + 'city';
            label.appendChild (document.createTextNode ('City or Town:'));
        div.appendChild (document.createElement ('br'));
        var span = createSpan (div, 'error', '');
        var select = span.appendChild (document.createElement ('select'));

        for (x=0; x<locations.count; x++) {
            var option = select.appendChild (document.createElement ('option'));
            var value  = locations.getAt[x].city + "|";
                value += locations.getAt[x].county + "|";
                value += locations.getAt[x].stateProvince + "|";
                value += locations.getAt[x].latitude + "|";
                value += locations.getAt[x].longitude;
            option.value = value;
            var desc  = locations.getAt[x].city + " (" + locations.getAt[x].county + "), " + locations.getAt[x].stateProvince;
            option.appendChild (document.createTextNode (desc));
        }
    select.name = prefix + "UnparsedCity";
        select.onchange = function () { mqparseSelect(this.options[this.selectedIndex].value, prefix) };
    } else {
        if (locations.count > 0 && mq_ParamExists(locations.getAt[0]) && locations.getAt[0].city) {
            var value = locations.getAt[0].city;
        } else {
            var value = '';
        }
        if (locations.status == 1030 || locations.status == 1031) {
            var spanClass = 'error';
        } else {
            var spanClass = '';
        }
        createFormInput (form, prefix + 'city', spanClass, 'City or Town:', 'text', prefix + 'city', value, '30', '80');
    }
}

//function used to populate hidden inputs based on city/state selection from dropdown
function mqChangeCityInfo(value, prefix) {
    var valText = value.split("|");
    var city    = valText[0];
    var county  = valText[1];
    var state   = valText[2];
    document.getElementById (prefix + 'city').value          = city;
    document.getElementById (prefix + 'state').value        = state;
    document.getElementById (prefix + 'county').value        = county;
}


//function used to populate hidden inputs based on city/state selection from dropdown
function mqparseSelect(value, prefix) {
    var valText = value.split("|");
     if(valText[0]) document.getElementById (prefix + 'city').value     = valText[0];
     if(valText[1]) document.getElementById (prefix + 'county').value   = valText[1];
     if(valText[2]) document.getElementById (prefix + 'state').value    = valText[2];
     //if(valText[3]) document.getElementById (prefix + 'latitude').value     = valText[3];
     //if(valText[4]) document.getElementById (prefix + 'longitude').value    = valText[4];
     if(valText[5]) document.getElementById (prefix + 'zip').value      = valText[5];
     if(valText[6]) document.getElementById (prefix + 'address').value  = valText[6];
}

function mqAmbStateZipRow(form, locations, prefix) {
    var div = createDiv (form, 'row', '');
    if(!(locations.status >= 1130 && locations.status < 1142)) {
        var sLabel = div.appendChild (document.createElement ('label'));
            sLabel.htmlFor = prefix + 'state';
            sLabel.appendChild (document.createTextNode ('State:'));
    }
    var zLabel = div.appendChild (document.createElement ('label'));
        zLabel.htmlFor = prefix + 'zip';
        zLabel.appendChild (document.createTextNode ('Zip Code:'));
    div.appendChild (document.createElement ('br'));
    if(!(locations.status >= 1130 && locations.status < 1142)) {
        if(locations.status >= 1140 && locations.status < 1150) {

           var county = (locations.getAt[0])? (locations.getAt[0]).county: "";
        createHiddenInput (form, prefix + 'county', prefix + 'county', county);
        createHiddenInput (form, prefix + 'state', prefix + 'stateProvince', locations.getAt[0].stateProvince);

            var sSpan = div.appendChild (document.createElement ('span'));
                sSpan.className = 'error';
            var sSelect = sSpan.appendChild (document.createElement ('select'));
                sSelect.name = prefix + 'UnparsedStateProvince';
            for (x=0; x<locations.count; x++) {
                var option = sSelect.appendChild (document.createElement ('option'));
                var value  = "|" + locations.getAt[x].stateProvince + "||" + locations.getAt[x].latitude;
            value += "|" + locations.getAt[x].longitude;
                 option.value = value;
        option.appendChild (document.createTextNode (locations.getAt[x].stateProvince));
            }
        sSelect.onchange = function () { mqparseSelect(this.options[this.selectedIndex].value, prefix) };
        } else {
            if (locations.getAt[0].stateProvince) {
                var value = locations.getAt[0].stateProvince;
            } else {
                var value = '';
            }
            createInput (div, prefix + 'state', 'text', prefix + 'stateProvince', value, '4', '2');
        }
    }
    if (locations.status >= 1120 && locations.status < 1130) {

           var county = (locations.getAt[0])? (locations.getAt[0]).county: "";
        createHiddenInput (form, prefix + 'county', prefix + 'county', county);
        createHiddenInput (form, prefix + 'zip', prefix + 'postalCode', locations.getAt[0].postalCode);

        var zSpan = div.appendChild (document.createElement ('span'));
                zSpan.className = 'error';
            var zSelect = zSpan.appendChild (document.createElement ('select'));
                zSelect.name = prefix + 'UnparsedPostalCode';
            for (x=0; x<locations.count; x++) {
                var option = zSelect.appendChild (document.createElement ('option'));
                var value  = "|" + locations.getAt[x].stateProvince + "||" + locations.getAt[x].latitude;
            value += "|" + locations.getAt[x].longitude + "|" + locations.getAt[x].postalCode;
                 option.value = value;
                option.appendChild (document.createTextNode (locations.getAt[x].postalCode));
            }
        zSelect.onchange = function () { mqparseSelect(this.options[this.selectedIndex].value, prefix) };

    } else {
        if (locations.getAt[0].postalCode) {
            var value = locations.getAt[0].postalCode;
        } else {
            var value = '';
        }
        if (locations.status >= 1020 && locations.status <= 1022) {
            var spanClass = 'error';
        } else {
            var spanClass = '';
        }
        createInput (div, prefix + 'zip', 'text', prefix + 'postalCode', value, '15', '10');
    }
}

//function used to return the status text for a given status number
function getGeoStatusTxt(loc, statusCode, button) {
   var strStatus = "Cannot find";

   //geocoding return codes
   //non exact single match
   if (eval(statusCode) >= 1000 && eval(statusCode) < 1200) {

      if(mq_ParamExists(loc.address) && loc.address.length)
      {
         strStatus += " ";
         strStatus += loc.address;
         strStatus += ",";
      }

      if(mq_ParamExists(loc.city) && loc.city.length)
      {
         strStatus += " ";
         strStatus += loc.city;

      }

      if(mq_ParamExists(loc.stateProvince) && loc.stateProvince.length)
      {
         strStatus += " ";
         strStatus += loc.stateProvince;
      }

      if(mq_ParamExists(loc.postalCode) && loc.postalCode.length)
      {
         strStatus += " ";
         strStatus += loc.postalCode;
      }

      strStatus += ". ";
   }

   //single string return codes
   switch (eval(statusCode))
   {
      //No Location Matches
      case 1901 :
         return "Cannot identify this location.  Please provide additional information or modify the information your entered.";
         break;
      //Invalid State
      case 1902 :
         return "Invalid state abbreviation.  Please enter a valid state abbreviation and click the \"" + button + "\" button.";
         break;
      //Invalid Country Code
      case 1903 :
         return "Mapping is unavailable for the country specified.";
         break;
      //Invalid Inputs
      case 9000 :
         return "Cannot identify this location without additional information.  Please enter at least a city and state or postal code.";
         break;
      //Single Non-Exact Address Match
      case 1000 :
         strStatus+="Found a similar address.  Click the \"" + button + "\" button to map this address, or modify the information you entered.";
         return strStatus;
         break;
      //Multiple Non-Exact Address Matches
      case 1100 :
         strStatus+="Found similar addresses.  Please select one and click the \"" + button + "\" button.";
         return strStatus;
         break;
      //Single Non-Exact Street Block Match
      case 1010 :
         strStatus+="Found a similar address.  Click the \"" + button + "\" button to map this address, or modify the information you entered.";
         return strStatus;
         break;
      //Multiple Non-Exact Street Block Matches
      case 1110 :
         strStatus+="Found similar addresses.  Please select one and click the \"" + button + "\" button.";
         return strStatus;
         break;
      //Single Exact Street Block Match
      case 1011 :
         strStatus+="Found a similar address.  Click the \"" + button + "\" button to map this address, or modify the information you entered.";
         return strStatus;
         break;
      //Multiple Exact Street Block Matches
      case 1111 :
         strStatus+="Found similar addresses.  Please select one and click the \"" + button + "\" button.";
         return strStatus;
         break;
      //Single Zip+4 Fallback from Address
      case 1020 :
         strStatus+="Click the \"" + button + "\" button to map the ZIP+4 code, or modify the information you entered.";
         return strStatus;
         break;
      //Multiple Zip+4 Fallback from Address
      case 1120 :
         strStatus+="Found multiple ZIP+4 codes.  Please select one and click the \"" + button + "\" button.";
         return strStatus;
         break;
      //Single Zip+2 Fallback from Address
      case 1021 :
         strStatus+="Click the \"" + button + "\" button to map the ZIP+2 code, or modify the information you entered.";
         return strStatus;
         break;
      //Multiple Zip+2 Fallback from Address
      case 1121 :
         strStatus+="Found multiple ZIP+2 codes.  Please select one and click the \"" + button + "\" button."
         return strStatus;
         break;
      //Single Zip Fallback from Address
      case 1022 :
         strStatus+="Click the \"" + button + "\" button to map the ZIP code, or modify the information you entered.";
         return strStatus;
         break;
      //Multiple Zip Fallback from Address
      case 1122 :
         strStatus+="Found multiple ZIP codes.  Please select one and click the \"" + button + "\" button.";
         return strStatus;
         break;
      //Multiple Zips US
      case 1123 :
         return "Found multiple ZIP codes.  Please select one and click the \"" + button + "\" button.";
         break;
      //Single Fallback from Address to City
      case 1030 :
         strStatus+="Click the \"" + button + "\" button to map the city, or modify the information you entered.";
         return strStatus;
         break;
      //Multiple Fallback from Address to City
      case 1130 :
         strStatus+="Found multiple cities.  Please select one and click the \"" + button + "\" button.";
         return strStatus;
         break;
      //Single Fallback from Zip to City
      case 1031 :
         strStatus="Cannot find";
         if(mq_ParamExists(loc.postalCode) && loc.postalCode.length)
         {
            strStatus += " ";
            strStatus += loc.postalCode;
         }
         strStatus+=".  Click the \"" + button + "\" button to map the city, or modify the information you entered.";
         return strStatus;
         break;
      //Multiple Fallback from Zip to City
      case 1131 :
         strStatus="Cannot find";
         if(mq_ParamExists(loc.postalCode) && loc.postalCode.length)
         {
            strStatus += " ";
            strStatus += loc.postalCode;
         }
         strStatus+=".  Found multiple cities.  Please select one and click the \"" + button + "\" button.";
         return strStatus;
         break;
      //Multiple City Match No Fallback
      case 1132 :
         return "Found multiple cities.  Please select one and click the \"" + button + "\" button."
         break;

      //Single Fallback from Address to State
      case 1040 :
         strStatus+="Click the \"" + button + "\" button to map the state, or modify the information you entered.";
         return strStatus;
         break;
      //Multiple Fallback from Address to State
      case 1140 :
         return "Found multiple states.  Please select one and click the \"" + button + "\" button.";
         break;
      //Single Fallback from Zip to State
      case 1041 :
         strStatus="Cannot find";
         if(mq_ParamExists(loc.postalCode) && loc.postalCode.length)
         {
            strStatus += " ";
            strStatus += loc.postalCode;
         }
         strStatus+=".  Click the \"" + button +"\" button to map the state, or modify the information you entered.";
         return strStatus;
         break;
      //Multiple Fallback from Zip to State
      case 1141 :
         strStatus="Cannot find";
         if(mq_ParamExists(loc.postalCode) && loc.postalCode.length)
         {
            strStatus += " ";
            strStatus += loc.postalCode;
         }
         strStatus+=".  Found multiple states.  Please select one and click the \"" + button + "\" button.";
         return strStatus;
         break;
      //Multiple State Match No Fallback
      case 1142 :
         return "Found multiple states.  Please select one and click the \"" + button + "\"p button."
         break;
      //Single Fallback from PostalCode to Country
      case 1050 :
         return "Could not find the postal code specified.";
         break;
   }

   return "An Unknown error occurred";
}


function mq_route_geocode_check() {

   mq_geocode_check("orig");
   mq_geocode_check("dest");

}

function mq_geocode_check (pre)  {

   var ambig = "document.ambiguities." + pre;
   var city;
   var stateProvince;
   var postalCode;
   var lat;
   var lng;


   if(eval("document.ambiguities." + pre + "latitude") != undefined) {
      document.ambiguities.removeChild(eval("document.ambiguities." + pre + "latitude"));
   }
   if(eval("document.ambiguities." + pre + "longitude") != undefined) {
      document.ambiguities.removeChild(eval("document.ambiguities." + pre + "longitude"));
   }

   if(eval(ambig + "UnparsedAddress") != undefined) {
      var dirtyFlag = false;
      var valText = (eval(ambig + "UnparsedAddress").value).split("|");
      if(valText[0]) city = valText[0];
      if(valText[1]) stateProvince = valText[2];
      if(valText[3]) lat = valText[3];
      if(valText[4]) lng = valText[4];
      if(valText[5]) postalCode = valText[5];
      if((eval(ambig + "city").value != city) || (eval(ambig + "stateProvince").value != stateProvince)
            || (eval(ambig + "postalCode").value != postalCode)) {
         dirtyFlag = true;
      }
      if(!dirtyFlag) {
          if(lat) createHiddenInput (document.ambiguities, pre + 'latitude', pre + 'latitude', lat);
          if(lng) createHiddenInput (document.ambiguities, pre + 'longitude', pre + 'longitude', lng);
      }

   }

   if(eval(ambig + "UnparsedCity") != undefined) {
      var dirtyFlag = false;
      var valText = (eval(ambig + "UnparsedCity").value).split("|");
      if(valText[3]) lat = valText[3];
      if(valText[4]) lng = valText[4];
      if((eval(ambig + "address").value != "") || (eval(ambig + "postalCode").value != "")) {
         dirtyFlag = true;
      }
      if(!dirtyFlag) {
          if(lat) createHiddenInput (document.ambiguities, pre + 'latitude', pre + 'latitude', lat);
          if(lng) createHiddenInput (document.ambiguities, pre + 'longitude', pre + 'longitude', lng);
      }
   }

}

