var mapsForm = new WebForm();
var f; //global form variable
var ddMap = null;
var customPlace = null;

/* START VALIDATION ROUTINE ***********/
function mapForm_onSubmit() {
    SW.domWidget.inputLabels.removeLabel(yuiDom.get("customAddress"));
    document.getElementById('disambiguationResults').style.display = "none";
    ddMap = new VEMap("drivingDirectionsMap");
    ddMap.LoadMap();
    ddMap.ShowMessageBox=false;
    f = document.forms['mapsForm'];
    mapsForm.resetErrors();

    if (checkBusRules(f) == 'blankForm') {mapsForm.addError("blankFormErrorDiv")}
    if (checkBusRules(f) == 'airportAddress') {mapsForm.addError("airportCodeLabel", "fromStreetAddressLabel", "airportAddressErrorDiv")}
    if (checkBusRules(f) == 'airportCodeInvalid') {mapsForm.addError("airportCodeLabel", "airportCodeErrorDiv")}
    if (mapsForm.hasErrors()) {
        mapsForm.showErrors();
        return false;
    } else {
        dirlocation = (f.customAddress.value != '') ? f.customAddress.value : f.airportCode.value;
        this.ddMap.Find(null,dirlocation, null, null, null, null, null, null, false, false, this.getLatLong);
        return false;
    }
}
/* END VALIDATION ROUTINE ***********/
function checkBusRules(f) {
    var airportCode = f.airportCode.value;
    var street = f.customAddress.value;
    var objRegExp = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
    var airCodeRegExp = /[a-zA-Z]{3}/;

    if ((airportCode == null || airportCode == '') && (street == null || street == '')) {
        return "blankForm";
    } else {
        if (airportCode != null && airportCode != '') {
            if ((street != null && street != '')) {
                return "airportAddress";
            } else {
                if (!airCodeRegExp.test(airportCode)) {return "airportCodeInvalid";}
            }
        }
    }
}

function getLatLong(veShapeLayer, veFindResult, vePlace, moreResults, errorMsg) {
    document.getElementById('disambiguationResults').style.display = "none";
    if (vePlace != null) {
        if (vePlace.length > 1) {
            var results = disambiguityMessage + ":<br/>";
            var highConfidenceMatches = new Array();
            for (x = 0; x < vePlace.length; x++) {
                if (vePlace[x].MatchConfidence == VEMatchConfidence.High) highConfidenceMatches.push(vePlace[x]);
                else results += "<a href=\"javascript:this.ddMap.Find(null,'" + vePlace[x].Name + "', null, null, null, null, null, null, false, false, getLatLong)\">" + vePlace[x].Name + "</a><br/>";
            }
            if (highConfidenceMatches.length == 0) {
                document.getElementById('disambiguationResults').innerHTML = results;
                document.getElementById('disambiguationResults').style.display = "block";
                return false;
            } else if (highConfidenceMatches.length == 1) {
                customPlace = highConfidenceMatches[0];
            }
        } else if (vePlace.length == 1) {
            customPlace = vePlace[0];
        }
        f.lat.value = customPlace.LatLong.Latitude;
        f.long.value = customPlace.LatLong.Longitude;
        f.name.value = f.customAddress.value;
        f.customAddress.disabled = true;
        f.airportCode.disabled = true;
        if (document.getElementById('disambiguationResults').style.display != "block") {
//            f.submit();
            checkDirections();
        }
        return false;
    } else {
       yuiDom.get('directionsAddressError').style.display='block';
        return false;
    }
}

function checkDirections() {
        var latLongArray = new Array();
        var routeOptions = new VERouteOptions();
        latLong = new VELatLong(customPlace.LatLong.Latitude,customPlace.LatLong.Longitude);
        pushPinLatLong= new VELatLong(f.propLat.value,f.propLong.value);
        routeOptions.RouteCallback = validateDirections;
        routeOptions.RouteMode = VERouteMode.Driving;
        latLongArray.push(latLong);
        latLongArray.push(pushPinLatLong);
        this.ddMap.GetDirections(latLongArray,routeOptions);

}

function validateDirections(veRoute) {
    if(veRoute.Distance > 0  && veRoute.Time > 0) {
        f.validDirections.value = true;
    } else {
        f.validDirections.value = false;
    }
    f.submit();

}
