﻿var yourAddrInput = null;
var theirAddrInput = null;
var findVenuesAddrInput = null;
var venueTypeDdl = null;
var ajaxReqCounter = 0;
var prb = null;
var valSummCont = null;
var valSumm = null;
var valRespHelper = null;

function ShowProcessingBox(finishedFunct) {
    valSummCont.innerText = new String();
    valSummCont.innerHTML = valSummCont.innerText;
    valSummCont.hide();
    
    if (valSumm) valSumm.resetErrorStyles();
    
    prb = new ProcessingRequestBox('Loading, please wait...');
    prb.show(finishedFunct);
}

function AddressBoxOnChange(sender) {
    if (!sender.isUpdated) sender.isUpdated = true;
}

function DefineSearch_CallBack(response) {            
    if (!response.error) {
        FinishedProcessingRequest();
    }            
}

function ProcessGeocodeAddrSuccess_CallBack(addr) {
    SetProfileAddress_Request(addr);
    FinishedProcessingRequest();
}

function ProcessGeocodeAddrFailed_CallBack(addr, inputControl) {
    var txt = ((inputControl.id.indexOf('YourAddress') > -1) ? 'Your' : ((inputControl.id.indexOf('TheirAddress') > -1) ? 'Their' : 'Find venue' ) ) + ' address hasn\'t been recognized';
    valRespHelper.AddMessage(new ValidationMessage(txt, inputControl, false));            
    inputControl.isUpdated = true;
    
    FinishedProcessingRequest();            
}

function SetProfileAddress_Request(addr) {
    // if the address has been geocoded and there was a match update the MHW profiles
    if (addr.geocoded && addr.foundMatch) {
        ajaxReqCounter++;
        
        Profile.ServiceManager.SetAddress(addr.addressType, addr.freeTxt, addr.latitude, addr.longitude, addr.streetAddress, addr.city, addr.postalCode, addr.country, SetProfileAddress_Callback);
    }
}
function SetProfileAddress_Callback(response) {
    if (!response.error) {
        FinishedProcessingRequest();
    }
}

function FinishedProcessingRequest() {
    ajaxReqCounter--;
    
    // after processing the last request
    if (ajaxReqCounter == 0) {
        if (valRespHelper.Passed) {
            // redirect to the results page
            window.location.href = '/results.aspx';
        } else {
            // hide the processing dialog
            prb.hide();
            
            // show the validation summary
            valSumm.showSummary(valRespHelper);
            valSummCont.show();
        }
    }
}

var nav = window.Event ? true : false;
        
if (nav) {
   //window.captureEvents(Event.KEYDOWN);
   window.onkeydown = NetscapeEventHandler_KeyDown;
}
else { document.onkeydown = MicrosoftEventHandler_KeyDown; }

function NetscapeEventHandler_KeyDown(e) {          
  if (e.which == 13 && e.target.type != 'textarea' && e.target.type != 'submit') {            
    SimClick(e.target.id);
    return false;           
  }
  return true;
}

function MicrosoftEventHandler_KeyDown() {            
    if (event.keyCode == 13 && event.srcElement.type != 'textarea' && event.srcElement.type != 'submit') {
        SimClick(event.srcElement.id);               
        return false;
    }
    return true;
}

function SimClick(o) {
    switch(o) {
        case yourAddrInput.id:
        case theirAddrInput.id:
            yourAddrInput.isUpdated = true;
            theirAddrInput.isUpdated = true;
            MeetHalfway();
        break;
        case findVenuesAddrInput.id:
            findVenuesAddrInput.isUpdated = true;
            FindVenues();
        break;
    }
}