﻿/*
* Ajax Functions:
*   This library is for all of the semi and full fledged ajax functionality
*   including on the fly actions without reloading the page, and sometimes
*   things that just look ajaxy (animations and the such)
*/

function TBShowLoading() {
    var divToLoad = '<div id="divLoadingContent" style="display:none"><div><img src="/images/loadingAnimation.gif" alt="Loading..." /></div></div>';
    $("body").append(divToLoad);
    tb_show("", "#TB_inline?inlineId=divLoadingContent&height=50&width=90&modal=true", null);
}

function TBShowError(errMessage) {
    var ErrValidationLightBox = "<div id='tbshowerror' style='display:none;'>" +
    "<div class='validationWrapper'>" +
    "<div class='CloseWrapper'>" +
    "<img src='/images/closeBtn.gif' alt='close' width='54' height='8' border='0' onclick='tb_remove();' /></div>" +
    "<div class='ErrorMessageWrapper'>" +
    "<p>" + errMessage + "</p></div></div>";
    $("body").append(ErrValidationLightBox);
    setTimeout("tb_show('Errors!', '#TB_inline?height=300&width=400&modal=true&inlineId=tbshowerror', null);", 200);
}

function TBShowMessage(message) {
    var MessageLightBox = "<div id='tbshowmessage' style='display:none;'>" +
    "<div class='validationWrapper'>" +
    "<div class='CloseWrapper'>" +
    "<img src='/images/closeBtn.gif' alt='close' width='54' height='8' border='0' onclick='tb_remove();' /></div>" +
    "<div class='ErrorMessageWrapper'>" +
    "<p style='color: #666666 !important;'>" + message + "</p></div></div>";
    $("body").append(MessageLightBox);
    setTimeout("tb_show('', '#TB_inline?height=300&width=400&modal=true&inlineId=tbshowmessage', null);", 200);
}

function TBShowConfirm(confMessage, isParent) {
    if (!isParent)
        isParent = "no"
    var ConfValidationLightBox = "<div id='tbshowconfirm' style='display:none;'>" +
    "<div class='validationWrapper'>" +
    "<div class='CloseWrapper'>" +
    "<img src='/images/closeBtn.gif' alt='close' width='54' height='8' border='0' onclick='tb_remove();" +
    (isParent == "no" ? "" : "parent.tb_remove(); parent.location = parent.location;") + "' /></div>" +
    "<div class='ConfirmationMessageWrapper'>" +
    "<p>" + confMessage + "</p></div></div>";
    $("body").append(ConfValidationLightBox);
    tb_show('Confirm!!', '#TB_inline?height=300&width=400&modal=true&inlineId=tbshowconfirm', null);
}

function TBShowConfirmEmail(confMessage, isParent) {
    if (!isParent)
        isParent = "no"
    var ConfValidationLightBox = "<div id='tbshowconfirm' style='display:none;'>" +
    "<div class='validationWrapper'>" +
    "<div class='CloseWrapper'>" +
    "<img src='/images/closeBtn.gif' alt='close' width='54' height='8' border='0' onclick='tb_remove();" +
    (isParent == "no" ? "" : "parent.tb_remove(); parent.location = parent.location;") + "' /></div>" +
    "<div class='ConfirmationMessageWrapper'>" +
    "<p>" + confMessage + "</p>"   + 
        "<script type='text/javascript'>" +
        "var axel = Math.random() + '';" +
        "var a = axel * 10000000000000;" +
        "document.write('<img src='https://ad.doubleclick.net/activity;src=2299045;type=resid062;cat=email194;ord=' + a + '?' width='1' height='1' alt=''/>');" +
        "</script>" +
        "<noscript>" +
        "<img src='https://ad.doubleclick.net/activity;src=2299045;type=resid062;cat=email194;ord=1?' width='1' height='1' alt=''>" +
        "</noscript>" +
    "</div></div>";
    $("body").append(ConfValidationLightBox);
    tb_show('Confirm!!', '#TB_inline?height=300&width=400&modal=true&inlineId=tbshowconfirm', null);
    
}


function TBShowConfirm2(confMessage, isParent, url) {
    if (!isParent)
        isParent = "no"
    if (!url)
        url = parent.location;
    var ConfValidationLightBox = "<div id='tbshowconfirm' style='display:none;'>" +
    "<div class='validationWrapper'>" +
    "<div class='CloseWrapper'>" +
    "<img src='/images/closeBtn.gif' alt='close' width='54' height='8' border='0' onclick='tb_remove();" +
    (isParent == "no" ? "" : "parent.tb_remove(); parent.location = '" + url + "'") + "' /></div>" +
    "<div class='ConfirmationMessageWrapper'>" +
    "<p>" + confMessage + "</p></div></div>";
    $("body").append(ConfValidationLightBox);
    tb_show('Confirm!!', '#TB_inline?height=300&width=400&modal=true&inlineId=tbshowconfirm', null);
}

function TBShowConfirm3(confMessage, isParent) {
    if (!isParent)
        isParent = "no"
    var ConfValidationLightBox = "<div id='tbshowconfirm' style='display:none;'>" +
    "<div class='validationWrapper'>" +
    "<div class='CloseWrapper'>" +
    "<img src='/images/closeBtn.gif' alt='close' width='54' height='8' border='0' onclick='tb_remove();" +
    (isParent == "no" ? "" : "parent.tb_remove(); parent.location = parent.location;") + "' /></div>" +
    "<div class='ConfirmationMessageWrapper'>" +
    "<p>" + confMessage + "</p>" +
        "<script type='text/javascript'>" +
        "var axel = Math.random() + '';" +
        "var a = axel * 10000000000000;" +
        "document.write('<img src='https://ad.doubleclick.net/activity;src=2299045;type=resid062;cat=savec614;ord=' + a + '?' width='1' height='1' alt=''/>');" +
        "</script>" +
        "<script type='text/javascript'>" +
                        "var inputArr = new Array();" +
                        "inputArr[0] = '_setAccount';" +
                        "inputArr[1] = 'UA-5418992-60';" +
                        "var _gaq = _gaq || [];" +
                        "_gaq.push(inputArr);" +
                        "_gaq.push(['_trackPageview', '/rentals/PropertyPage/SavedApartment/Success']);" +
                        "(function() {" +
                            "var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;" +
                            "ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';" +
                            "var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);" +
                        "})();" +
        "</script>" + 
        "<noscript>" +
        "<img src='https://ad.doubleclick.net/activity;src=2299045;type=resid062;cat=savec614;ord=1?' width='1' height='1' alt=''>" +
        "</noscript>" +
    "</div></div>";
    $("body").append(ConfValidationLightBox);
    tb_show('Confirm!!', '#TB_inline?height=300&width=400&modal=true&inlineId=tbshowconfirm', null);
}

function TBShowConfirm4(confMessage, isParent) {
    if (!isParent)
        isParent = "no"
    var ConfValidationLightBox = "<div id='tbshowconfirm' style='display:none;'>" +
    "<div class='validationWrapper'>" +
    "<div class='CloseWrapper'>" +
    "<img src='/images/closeBtn.gif' alt='close' width='54' height='8' border='0' onclick='tb_remove();" +
    (isParent == "no" ? "" : "parent.tb_remove(); parent.location = parent.location;") + "' /></div>" +
    "<div class='ConfirmationMessageWrapper'>" +
    "<p>" + confMessage + "</p>" +
        "<script type='text/javascript'>" +
        "var axel = Math.random() + '';" +
        "var a = axel * 10000000000000;" +
        "document.write('<img src='https://ad.doubleclick.net/activity;src=2299045;type=resid062;cat=savec614;ord=' + a + '?' width='1' height='1' alt=''/>');" +
        "</script>" +
        "<script type='text/javascript'>" +
                        "var inputArr = new Array();" +
                        "inputArr[0] = '_setAccount';" +
                        "inputArr[1] = 'UA-5418992-60';" +
                        "var _gaq = _gaq || [];" +
                        "_gaq.push(inputArr);" +
                        "_gaq.push(['_trackPageview', '/OnlineLeasing']);" +
                        "(function() {" +
                            "var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;" +
                            "ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';" +
                            "var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);" +
                        "})();" +
        "</script>" +
        "<noscript>" +
        "<img src='https://ad.doubleclick.net/activity;src=2299045;type=resid062;cat=savec614;ord=1?' width='1' height='1' alt=''>" +
        "</noscript>" +
    "</div></div>";
    $("body").append(ConfValidationLightBox);
    tb_show('Confirm!!', '#TB_inline?height=300&width=400&modal=true&inlineId=tbshowconfirm', null);
}

function SavedAptNotes(hmy, note) {
    TBShowLoading();
    $.get("/scripts/ajax.aspx", { call: "saveaptnote", hmy: hmy, notes: $('#' + note)[0].value },
        function(data) {
            if (data != "yes") {
                tb_remove();
                setTimeout("TBShowError(\"An error has occured in your submission\");", 500);
            }
            else {
                setTimeout("tb_remove();", 1000);
            }
        }
    );
}
function updateQuestion(email) {
    var q = $.ajax({ url: "/scripts/ajax.aspx", async: false, data: { call: "getquestion", user: email} }).responseText;
    return q;
}
function removeSavedApt(hmy) {
    TBShowLoading();
    $.get("/scripts/ajax.aspx", { call: "removeapt", hmy: hmy },
            function(data) {
                if (data != "yes") {
                    tb_remove();
                    setTimeout("TBShowError(\"An error has occured in your submission\");", 300);
                }
                else {
                    setTimeout("tb_remove();", 1000);
                    setTimeout("$(\"#divAptFullDetails" + hmy + "\").fadeOut(\"slow\");", 1250);
                }
            }
        );
}
function leaseApt(hmy, tb) {
    TBShowLoading();
    $.ajax({
        url: "/scripts/ajax.aspx",
        async: false,
        contentType: false,
        data: { call: "leaseapt", unitid: hmy, format: "json", jsoncallback: "jsonLeaseAptResponse", thickbox: tb },
        complete: function(jsonObject, textStatus) {
            tb_remove();
            //alert(jsonObject.responseText);
            var obj = eval(jsonObject.responseText);
            //alert(obj.result.response);
            switch (obj.result.response) {
                case "login":
                    if (!tb) {
                        setTimeout("tb_show(null, \"/ResidentCircle/LoginRegisterLightbox.aspx?action=lease&unitid=" + hmy + "&TB_iframe=true&height=400&width=756&modal=true\", null);", 300);
                    }
                    else {
                        $(document).location = 'LoginRegisterLightbox.aspx?action=lease&unitid=' + hmy;
                    }
                    break;
                case "no": tb_remove();
                    if (!tb)
                        setTimeout("TBShowError(\"An error has occured while submitting your information to the online leasing application. Please try again later.\");", 300);
                    else
                    //An error has occured while submitting your information to the online leasing application.  Please try again later.
                        setTimeout("TBShowError(\"Your user account was successfully registered, but an error has occured while submitting your information to the online leasing application. Please try again later.\");", 300);

                    break;
                default:
                    //setTimeout("tb_remove();window.open('" + obj.result.response + "', \"_blank\");", 1000);

                    {
//                        var inputArr = new Array();
//                        inputArr[0] = '_setAccount';
//                        inputArr[1] = 'UA-5418992-60';
//                        var _gaq = _gaq || [];
//                        _gaq.push(inputArr);
//                        _gaq.push(['_trackPageview', '/OnlineLeasing']);

//                        (function() {
//                            var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
//                            ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
//                            var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
                        //                        })();
                        TBShowConfirm4("You will be forwarded to Online leasing.");
                    }
                    {
                        window.open(obj.result.response, "_blank");
                        parent.tb_remove();
                    }
                    break;
            }
        }
    });
}

function saveApt(hmy, tb) {
    $.ajax({
        url: "/scripts/ajax.aspx",
        async: false,
        contentType: false,
        data: { call: "saveapt", unitid: hmy, format: "json", jsoncallback: "jsonLeaseAptResponse", thickbox: tb },
        complete: function(jsonObject, textStatus) {
            //alert(jsonObject.responseText);
            var obj = eval(jsonObject.responseText);
            //alert(obj.result.response);
            switch (obj.result.response) {
                case "login":
                    if (!tb) {
                        setTimeout("tb_show(null, \"/ResidentCircle/LoginRegisterLightbox.aspx?action=save&unitid=" + hmy + "&TB_iframe=true&height=400&width=756&modal=true\", null);", 300);
                    }
                    else {
                        $(document).location = 'LoginRegisterLightbox.aspx?action=save&unitid=' + hmy;
                    }
                    break;
                case "no": tb_remove();
                    if (!tb)
                        setTimeout("TBShowError(\"An error has occured while attempting to save your apartment. Please try again later.\");", 300);
                    else
                    //An error has occured while submitting your information to the online leasing application.  Please try again later.
                        setTimeout("TBShowError(\"Your user account was successfully registered, but an error has occured while attempting to save your apartment. Please try again later.\");", 300);

                    break;
                default:
                    //setTimeout("tb_remove();window.open('" + obj.result.response + "', \"_blank\");", 1000);
                    {
                        parent.tb_remove();
                        TBShowConfirm3("This apartment has been added to your profile.");
                    }
                    break;
            }
        }
    });
}

function doNothing() {
    return false;
}

function firstTimeLogin() {
    tb_show(null, "/ResidentCircle/FirstTimeLogin.aspx?TB_iframe=true&width=450&height=350&modal=true", null);
}

function sendApt(hmy) {
    setTimeout("tb_show(null, \"/Rentals/STAF.aspx?id=" + hmy + "&TB_iframe=true&height=345&width=575&modal=true\", null);", 300);
}