// This function was used and done this way to not only NOT break the AJAX and calling of it but also to be able
// to some how reset the saving of Email, upon coming in, had to use one of its controls unload events.
function buildInitialFunstionality() {
    window.document.getElementById('ctl00_ContentPlaceHolder_PasswordTextBox').onunload = cancelPwdAndEmailSaving();
}
function cancelPwdAndEmailSaving() {
    // Cancel passwords and email being saved.
    document.getElementById('ctl00_ContentPlaceHolder_PasswordTextBox').value = "";
    document.getElementById('ctl00_ContentPlaceHolder_ConfirmPasswordTextBox').value = "";

    if (isValidEmail(document.getElementById('ctl00_ContentPlaceHolder_ExtraInformationTextBox').value) == true) {
        // Its an email address in the Extra information coming from browser saving it, so clear it.
        document.getElementById('ctl00_ContentPlaceHolder_ExtraInformationTextBox').value = "";
    }
}

function displayEmailError(email, errorLabelName) {
    //alert(email);
    if (isValidEmail(email) == false) {
        document.getElementById(errorLabelName).innerHTML = "Invalid email entered.";
        return false;
    }
    else {
        document.getElementById(errorLabelName).innerHTML = "";
        return true;
    }
}

function isValidEmail(emailAddress) {
    var emailRegex = "^[\\w-_\.+]*[\\w-_\.]\@([\\w]+\\.)+[\\w]+[\\w]$";
    var regx = new RegExp(emailRegex);
    return regx.test(emailAddress);
}
function setAutocomplete(suburbControl, postcodeControl, stateControl, precinctControl, action) {
    if (action == "getSuburbPrecinctList") {
        $("#" + precinctControl).autocomplete({
            source: function(request, response) {
                $.ajax({
                    url: '/AutoPostcodeLookup/PostcodeLookup.ashx',
                  dataType: "json",
                  data: {
                    term : request.term,
                    ACTION : action
                  },
                  success: function(data) {
                    response(data);
                  }
                });
            },minLength: 3,
            //define select handler  
                select: function(e, ui) {
                    if (ui.item.value != "No matches") {
                        var entityParts = ui.item.value.split(',');
                        var suburb = entityParts[1];
                        var postcode = entityParts[2];
                        var state = entityParts[3];
                        var precinct = entityParts[0];
                        $("#" + suburbControl).val(suburb);
                        $("#" + postcodeControl).val(postcode);
                        $("#" + stateControl).val(state);
                        $("#" + precinctControl).val(precinct);
                    }
                    else {
                        $("#" + suburbControl).val("");
                        $("#" + postcodeControl).val("");
                        $("#" + stateControl).val("Please select a State");
                        $("#" + precinctControl).val();
                    }
                    return false;
                }
            });
    }
    else if (action == "getSuburbList") {
        $("#" + suburbControl).autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: '/AutoPostcodeLookup/PostcodeLookup.ashx',
                    dataType: "json",
                    data: {
                        term: request.term,
                        ACTION: action,
                        suburbPrecinct: $("#" + precinctControl).val()
                    },
                    success: function (data) {
                        response(data);
                    }
                });
            },minLength: 3,
            //define select handler  
                select: function(e, ui) {
                    if (ui.item.value != "No matches") {
                        var entityParts = ui.item.value.split(',');
                        var suburb = entityParts[0];
                        var postcode = entityParts[1];
                        var state = entityParts[2];
                        $("#" + suburbControl).val(suburb);
                        $("#" + postcodeControl).val(postcode);
                        $("#" + stateControl).val(state);
                    }
                    else {
                        $("#" + suburbControl).val("");
                        $("#" + postcodeControl).val("");
                        $("#" + stateControl).val("Please select a State");
                    }
                    return false;
                }
            });

    }
}

