/*
 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');
    } else {
        if (locations.count > 0 && mq_ParamExists(locations.getAt[0]) && locations.getAt[0].address) {
            var value = locations.getAt[0].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 + 'county', prefix + 'county', locations.getAt[0].county);
        createHiddenInput (form, prefix + 'state', prefix + 'stateProvince', locations.getAt[0].stateProvince);

        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;
            option.value = value;
            var desc  = locations.getAt[x].city + " (" + locations.getAt[x].county + "), " + locations.getAt[x].stateProvince;
            option.appendChild (document.createTextNode (desc));
        }
        select.onchange = function () { mqChangeCityInfo(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 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:'));
    div.appendChild (document.createElement ('br'));
    if(!(locations.status >= 1130 && locations.status < 1142)) {
        if(locations.status >= 1140 && locations.status < 1150) {
            var sSpan = div.appendChild (document.createElement ('span'));
                sSpan.className = 'error';
            var sSelect = sSpan.appendChild (document.createElement ('select'));
                sSelect.id = prefix + 'state';
                sSelect.name = prefix + 'stateProvince';
            for (x=0; x<locations.count; x++) {
                var option = sSelect.appendChild (document.createElement ('option'));
                    option.value = locations.getAt[x].stateProvince;
                    option.appendChild (document.createTextNode (locations.getAt[x].stateProvince));
            }
        } 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 zSpan = div.appendChild (document.createElement ('span'));
                zSpan.className = 'error';
            var zSelect = zSpan.appendChild (document.createElement ('select'));
                zSelect.id = prefix + 'zip';
                zSelect.name = prefix + 'postalCode';
            for (x=0; x<locations.count; x++) {
                var option = zSelect.appendChild (document.createElement ('option'));
                    option.value = locations.getAt[x].postalCode;
                    option.appendChild (document.createTextNode (locations.getAt[x].postalCode));
            }
    } 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;
         strStatus += ",";
      }

      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.";
         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 occured";
}
