﻿$(function() {

    $("#NewAccount").dialog({
        bgiframe: true,
        autoOpen: false,
        modal: true,
        width: 390,
        height: 400,
        closeText: '',
        resizable: false,
        buttons: {
            'Create an account': function() {
                var bValid = true;
                var fname = $("#na_tbfname"),
			    lname = $("#na_tblname"),
			    email = $("#na_tbemail"),
			    phone = $("#na_tbphone"),
                allFields = $([]).add(fname).add(lname).add(email).add(phone),
                tips = $("#na_validateTips");
                allFields.removeClass('ui-state-error');

                if (AgentId == "") {
                    updateTips("AgentId is invalid or missing.", tips);
                    bValid = false;
                }

                bValid = bValid && checkLength(fname, "first name", 3, 16, tips);
                bValid = bValid && checkLength(email, "email", 6, 80, tips);
                bValid = bValid && checkRegexp(email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. ui@onboard.com", tips);

                if (bValid) {
                    updateTips("Creating new account...", tips);
                    z57('newAccount');

                    setTimeout(function() { execPreviousAction(tips) }, timeout);

                  
                }
            }
        }
    });


    $("#EmailListing").dialog({
        bgiframe: true,
        autoOpen: false,
        modal: true,
        width: 390,
        height: 560,
        closeText: '',
        resizable: false,
        buttons: {
            'Email Listing': function() {
                var bValid = true;
                var name = $("#el_tbname"),
			    email = $("#el_tbemail"),
			    email1 = $("#el_tbemail1"),
			    email2 = $("#el_tbemail2"),
			    email3 = $("#el_tbemail3"),
                //email4 = $("#el_tbemail4"),
			    message = $("#el_tbmessage"),
                allFields = $([]).add(name).add(email).add(email1).add(email2).add(email3).add(message),
                tips = $("#el_validateTips");
                allFields.removeClass('ui-state-error');

                if (AgentId == "") {
                    updateTips("AgentId is invalid or missing.", tips);
                    bValid = false;
                }

                if (bValid == true) {
                    if (listingId == "") {
                        updateTips("ListingId is invalid or missing.", tips);
                        bValid = false;
                    }
                }

                if (bValid == true) {
                    if (MLSListingId == "") {
                        updateTips("MLSListingId is invalid or missing.", tips);
                        bValid = false;
                    }
                }

                bValid = bValid && checkLength(name, "name", 3, 32, tips);
                bValid = bValid && checkLength(email, "email", 6, 80, tips);
                bValid = bValid && checkLength(message, "message", 6, 150, tips);
                bValid = bValid && checkRegexp(email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. ui@onboard.com", tips);

                if (email1.val().length > 0)
                    bValid = bValid && checkRegexp(email1, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. ui@onboard.com", tips);

                if (email2.val().length > 0)
                    bValid = bValid && checkRegexp(email2, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. ui@onboard.com", tips);

                if (email3.val().length > 0)
                    bValid = bValid && checkRegexp(email3, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. ui@onboard.com", tips);


                if (bValid) {
                    updateTips("Sending email...", tips);
                    z57('emailListing');

                    setTimeout(function() { showStatusMessage("#EmailListing", tips) }, timeout);

                  
                }
            }
        }
    });

    $("#SchedShowing").dialog({
        bgiframe: true,
        autoOpen: false,
        modal: true,
        width: 400,
        height: 440,
        closeText: '',
        resizable: false,
        buttons: {
            'Schedule a Showing': function() {
                var bValid = true;
                var fname = $("#ss_tbfname"),
			    lname = $("#ss_tblname"),
			    email = $("#ss_tbemail"),
                allFields = $([]).add(fname).add(lname).add(email),
                tips = $("#ss_validateTips");
                allFields.removeClass('ui-state-error');

                if (AgentId == "") {
                    updateTips("AgentId is invalid or missing.", tips);
                    bValid = false;
                }

                if (bValid == true) {
                    if (listingId == "") {
                        updateTips("ListingId is invalid or missing.", tips);
                        bValid = false;
                    }
                }

                if (bValid == true) {
                    if (MLSListingId == "") {
                        updateTips("MLSListingId is invalid or missing.", tips);
                        bValid = false;
                    }
                }

                bValid = bValid && checkLength(fname, "first name", 3, 16, tips);
                bValid = bValid && checkLength(lname, "last name", 3, 16, tips);
                bValid = bValid && checkLength(email, "email", 6, 80, tips);
                bValid = bValid && checkRegexp(email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. ui@onboard.com", tips);

                if (bValid) {
                    updateTips("Sending request...", tips);
                    z57('schedShowing');

                    setTimeout(function() { showStatusMessage("#SchedShowing", tips) }, timeout);

                  
                }
            }
        }
    });

    $("#InfoRequest").dialog({
        bgiframe: true,
        autoOpen: false,
        modal: true,
        width: 400,
        height: 440,
        closeText: '',
        resizable: false,
        buttons: {
            'Submit': function() {
                var bValid = true;
                var fname = $("#ir_tbfname"),
		        lname = $("#ir_tblname"),
		        email = $("#ir_tbemail"),
                allFields = $([]).add(fname).add(lname).add(email),
                tips = $("#ir_validateTips");
                allFields.removeClass('ui-state-error');

                if (AgentId == "") {
                    updateTips("AgentId is invalid or missing.", tips);
                    bValid = false;
                }

                if (bValid == true) {
                    if (listingId == "") {
                        updateTips("ListingId is invalid or missing.", tips);
                        bValid = false;
                    }
                }

                if (bValid == true) {
                    if (MLSListingId == "") {
                        updateTips("MLSListingId is invalid or missing.", tips);
                        bValid = false;
                    }
                }

                bValid = bValid && checkLength(fname, "first name", 3, 16, tips);
                bValid = bValid && checkLength(lname, "last name", 3, 16, tips);
                bValid = bValid && checkLength(email, "email", 6, 80, tips);
                bValid = bValid && checkRegexp(email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. ui@onboard.com", tips);

                if (bValid) {
                    updateTips("Sending request...", tips);
                    z57('infoRequest');

                    setTimeout(function() { showStatusMessage("#InfoRequest", tips) }, timeout);
                   
                }
            }
        }
    });

    $("#SaveSearch").dialog({
        bgiframe: true,
        autoOpen: false,
        modal: true,
        width: 380,
        height: 25,
        closeText: '',
        resizable: false
    });

    $("#SaveListing").dialog({
        bgiframe: true,
        autoOpen: false,
        modal: true,
        width: 380,
        height: 25,
        closeText: '',
        resizable: false
    });

    function checkRegexp(o, regexp, n, divtip) {

        if (!(regexp.test(o.val()))) {
            o.addClass('ui-state-error');
            if (divtip != null)
                updateTips(n, divtip);
                
            return false;
        } else {
            return true;
        }

    }

    function checkLength(o, n, min, max, divtip) {

        if (o.val().length > max || o.val().length < min) {
            o.addClass('ui-state-error');
            if (divtip != null)
                updateTips("Length of " + n + " must be between " + min + " and " + max + ".", divtip);
                
            return false;
        } else {
            return true;
        }
    }

    function updateTips(t, o) {
        //tips = $(divtip);
        o.text(t).effect("highlight", {}, 1500);
    }

    function execPreviousAction(tips) {
      //  alert('2 = ' + statusCode);
        if (statusCode == 0) {
            updateTips("* All fields are required", tips);
            $("#NewAccount").dialog('close');

            // Check previous action then execute
            if (previousAction == 'saveSearch') {
                saveSearchListing("#SaveSearch", previousAction);
            }
            else if (previousAction == 'saveListing') {
                saveSearchListing("#SaveListing", previousAction);
            }

        } else
            updateTips("The attempt to " + actionTemp + " failed.  Please try again later.", tips);

    }

    function showStatusMessage(dialog, tips) {

        if (statusCode == 0) {
            updateTips("* All fields are required", tips);
            $(dialog).dialog('close');
        } else
            updateTips("The attempt to " + actionTemp + " failed.  Please try again later.", tips)
    }

});

function saveSearchListing(id, action) {

    var bSuccess = true;
    actionTemp = action;
    previousAction = action;

   // alert('1 = ' + UId);
    
    if (UId == '') {
        // show create account
        bShowDialog = false;
        actionTemp = "newAccount";

        if (action == 'saveSearch') {
            $("#NewAccount").dialog('option', 'title', 'Property Organizer Sign-up');
        } else if (action == 'saveListing') {
            $("#NewAccount").dialog('option', 'title', 'Register Page');
        }

        $("#NewAccount").dialog('open');
        bSuccess = false;
        return;
    } else {
    // to be implemented.. UID validation
       // bShowDialog = true;
    }

    if (action == 'saveSearch') {
        $("#SaveSearch").dialog('open');
        document.getElementById('SaveSearchText').innerHTML = "Saving search...";
        z57(action);
        setTimeout(function() { showSaveMessage(action) }, timeout2);
    } else if (action == 'saveListing') {
        $("#SaveListing").dialog('open');
        document.getElementById('SaveListingText').innerHTML = "Saving listing...";
        z57(action);
        setTimeout(function() { showSaveMessage(action) }, timeout2);
    }

    //reset
    bShowDialog = true;
}

function showSaveMessage(action) {
    if (action == 'saveSearch') {

        if (statusCode == 0) {
            document.getElementById('SaveSearchText').innerHTML = "Search has been successfully saved!";
            $("#SaveSearch").dialog('close');
        } else {
            document.getElementById('SaveSearchText').innerHTML = "The attempt to " + actionTemp + " failed.  Please try again later.";
        }
        
//        if (bShowDialog == true || statusCode != 0) {
//            $("#SaveSearch").dialog('open');
//        }

    } else if (action == 'saveListing') {

        if (statusCode == 0) {
            document.getElementById('SaveListingText').innerHTML = "Listing has been successfully saved!";
            $("#SaveListing").dialog('close');
        } else {
            document.getElementById('SaveListingText').innerHTML = "The attempt to " + actionTemp + " failed.  Please try again later.";
        }
        
//        if (bShowDialog == true || statusCode != 0) {
//            $("#SaveListing").dialog('open');
//        }

    }
}

function openEmailAlerts() {
    document.getElementById('EmailAlerts').style.display = '';
    document.getElementById('lblDescription').style.display = 'none';
    document.getElementById('btnEmailAlerts').style.display = 'none';
    document.getElementById('AddressMLS').style.display = 'none';
}

function closeEmailAlerts() {
    document.getElementById('EmailAlerts').style.display = 'none';
    document.getElementById('lblDescription').style.display = '';
    document.getElementById('btnEmailAlerts').style.display = '';
    document.getElementById('AddressMLS').style.display = '';
}

function initEmailAlerts() {
    var bValid = true;
    var fname = $("#ea_tbfname"),
	    lname = $("#ea_tblname"),
	    email = $("#ea_tbemail"),
	    phone = $("#ea_tbphone"),
        allFields = $([]).add(fname).add(lname).add(email).add(phone);
        allFields.removeClass('ui-state-error');

    if (AgentId == "") {
        document.getElementById("ListingResult").className = 'Error';
        document.getElementById('ListingResult').innerHTML = "AgentId is missing. Please try again later.";
        hideProgressBar();
        bValid = false;
    }

    bValid = bValid && checkLength2(fname, "first name", 3, 16, null);
    bValid = bValid && checkLength2(email, "email", 6, 80, null);
    bValid = bValid && checkRegexp2(email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. ui@onboard.com", null);

    if (bValid) {
        z57('newAccount');

        setTimeout(function() { callSaveSearch() }, timeout);
    } else {
        bContinue = false;
        hideProgressBar();
    }   
    
}

function checkRegexp2(o, regexp, n, divtip) {

    if (!(regexp.test(o.val()))) {
        o.addClass('ui-state-error');
        return false;
    } else {
        return true;
    }

}

function checkLength2(o, n, min, max, divtip) {

    if (o.val().length > max || o.val().length < min) {
        o.addClass('ui-state-error');
        return false;
    } else {
        return true;
    }
}

function callSaveSearch() {
   // alert('4 = ' + statusCode);

    if (statusCode == 0) {
        z57("saveSearch");
        EmailAlerts = '';
        setTimeout(function() { showEmailAlertsMessage() }, timeout);

    } else {
        document.getElementById("ListingResult").className = 'Error';
        document.getElementById('ListingResult').innerHTML = "The attempt to " + actionTemp + " failed.  Please try again later.";
        hideProgressBar();
        bContinue = false;
    }
}

function showEmailAlertsMessage() {

   // alert('5 = ' + statusCode);
    
    if (statusCode == 0) {
        bContinue = true;
    } else {
        document.getElementById("ListingResult").className = 'Error'; 
        document.getElementById('ListingResult').innerHTML = "The attempt to " + actionTemp + " failed.  Please try again later.";
        hideProgressBar();
        bContinue = false;
    }
}
