﻿var surveyUrl = "http://www.surveygizmo.com/s3/popup/650449/0dd1438cfe5c";

$(document).click(function (e) {
    if (isALink(e)) {
        hackFromClick = true;
        if (isSectionUnitDetails(document.location.href)) {
            if (IsLeftPageUnitDetails(e)) {
                decCookie(cookieUnitDetails);
                if (displayExitSurvey()) {
                    ShowExitSurvey();
                    return false;
                }
            }
        }
        else {
            if (displayExitSurvey()) {
                ShowExitSurvey();
                return false;
            }
        }
    }
});

//this hack is used to not pass throught "onbeforeunload" when clicked..
var hackFromClick = false;

var cookieUnitDetails = "survey_unitdetails";

window.onload = function () {
    if (isSectionUnitDetails(document.location.href)) {
        incCookie(cookieUnitDetails);
    }
}

window.onbeforeunload = function () {
    if (!hackFromClick) {
        if (isSectionUnitDetails(document.location.href)) {
            decCookie(cookieUnitDetails);
        }
        if (displayExitSurvey()) {
            ShowExitSurveyAlternative();
        }
    }
    hackFromClick = false;
}


function IsLeftPageUnitDetails(e) {
    var targ = getTarget(e);
    return (targ.parentNode.toString().toUpperCase().indexOf("LEASING/DEFAULT.ASPX") > 0
    || targ.parentNode.toString().toUpperCase().indexOf("RENTALS") > 0);
}


//this method is called from flash
function onExitSurvey(currentUrl, nextUrl) {
    if (isSectionFloorPlans(currentUrl) && isSectionFloorPlans(nextUrl)) return;
    if (displayExitSurvey()) {
        ShowExitSurvey();
    }
}

function verifyTakenSurvey(surveyName) {
    var cookie = getCookie(surveyName);
    return cookie != "";
}

function verifyPopupSurvey() {
    counter = getCookie(cookieUnitDetails);
    if (counter == "") counter = "0";
    if (window.opener != null) {
        //the current window is the popup secundary.
        if (!window.opener.closed) {
            return counter != "0" || isSectionAvailabilities(window.opener.location.href);
        }
        else {
            return counter != "0";
        }
    }
    else {
        //the current window is the main.
        return counter != "0";
    }
}

function displayExitSurvey() {
    currentUrl = document.location.href;
    if (isSectionSurvey(currentUrl)) {
        var cookieName = "onexit_survey";
        if (!verifyTakenSurvey(cookieName) && !verifyPopupSurvey()) {
            setCookie(cookieName, true, 365);
            return true;
        }
    }
    return false;
}

function hideExitSurvey() {
    document.getElementById('sg-popup').style.display = 'none';
    return false;
}

function ShowExitSurvey() {
    var sg_div = document.createElement("div");
    sg_div.innerHTML = "<p style='margin:0;'><span>&nbsp;</span></p>";
    sg_div.innerHTML += "<p style='margin:0;'><span>We would greatly appreciate your taking a short survey about your experience on our website.</span></p>";
    sg_div.innerHTML += "<p style='margin:0;'><span>&nbsp;</span></p>";
    sg_div.innerHTML += "<p style='margin:0;'><a href=\"#\" style='color:#000;text-decoration:underline;' onclick=\"return openSurvey('" + encodeURI(surveyUrl) + "');\">Click here to start the survey</a></p>";
    sg_div.innerHTML += "<p style='margin:0;'><span>&nbsp;</span></p>";
    sg_div.innerHTML += "<p style='margin:0;'><a href=\"#\" style='color:#000;text-decoration:underline;' onclick=\"hideExitSurvey();\">Click here to decline the survey</a</p>";
    sg_div.innerHTML += "<p style='margin:0;'><span>&nbsp;</span></p>";
    sg_div.innerHTML += "<p style='margin:0;'><span>Thank you in advance for your time and comments.</span></p>";
    sg_div.innerHTML += "<p style='margin:0;'><span>&nbsp;</span></p>";

    sg_div.id = "sg-popup";
    sg_div.style.position = "absolute";
    sg_div.style.width = "467px";
    sg_div.style.top = "100px";
    sg_div.style.left = "400px";
    sg_div.style.backgroundColor = "#ffffff";
    sg_div.style.borderColor = "#000000";
    sg_div.style.borderStyle = "solid";
    sg_div.style.borderWidth = "1px";
    sg_div.style.padding = "0cm 5.4pt 0cm 5.4pt";
    sg_div.style.fontSize = "10.5pt";
    sg_div.style.color = "#000000";
    sg_div.style.fontFamily = "Calibri", "sans-serif";
    sg_div.style.zIndex = "1001";
    document.body.appendChild(sg_div);
}

function ShowExitSurveyAlternative() {
    var sharedObject = document.createElement("div");
    sharedObject.innerHTML = "<h1>You have been selected for a survey</h1><p>We appreciate your feedback!</p><p><a href=\"" + encodeURI(surveyUrl) + "\" onclick=\"window.close();\">Please click here start it now.</a> </p> <a href=\"javascript:window.close();\">No, thank you.</a> ";

    sharedObject.innerHTML = "<p style='margin:0;'><span>&nbsp;</span></p>";
    sharedObject.innerHTML += "<p style='margin:0;'><span>We would greatly appreciate your taking a short survey about your experience on our website.</span></p>";
    sharedObject.innerHTML += "<p style='margin:0;'><span>&nbsp;</span></p>";
    sharedObject.innerHTML += "<p style='margin:0;'><a style='color:#000;text-decoration:underline;' href=\"" + encodeURI(surveyUrl) + "\" onclick=\"window.close();\">Click here to start the survey</a></p>";
    sharedObject.innerHTML += "<p style='margin:0;'><span>&nbsp;</span></p>";
    sharedObject.innerHTML += "<p style='margin:0;'><a href=\"javascript:window.close();\" style='color:#000;text-decoration:underline;'>Click here to decline the survey</a</p>";
    sharedObject.innerHTML += "<p style='margin:0;'><span>&nbsp;</span></p>";
    sharedObject.innerHTML += "<p style='margin:0;'><span>Thank you in advance for your time and comments.</span></p>";
    sharedObject.innerHTML += "<p style='margin:0;'><span>&nbsp;</span></p>";

    sharedObject.id = "sg-popup";
    sharedObject.style.width = "100%";
    sharedObject.style.backgroundColor = "#ffffff";
    sharedObject.style.padding = "0cm 5.4pt 0cm 5.4pt";
    sharedObject.style.fontSize = "10.5pt";
    sharedObject.style.color = "#000000";
    sharedObject.style.fontFamily = "Calibri", "sans-serif";

    if (window.showModalDialog) {
        var retValue = showModalDialog("/modalwindow.htm", sharedObject, "dialogWidth:467px; dialogHeight:170px; center:yes;status:no;scroll:no;resizable:no;");
    }
    else {
        // for similar functionality in Opera, but it's not modal!
        var modal = window.open("/modalwindow.htm", null, "width=600,height=250,left=300,modal=yes,alwaysRaised=yes", null);
        modal.dialogArguments = sharedObject;
    }
}

function openSurvey(surveyUrl) {
    window.open(surveyUrl, "_blank", "width=600,height=500,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=yes,resizable=yes");
    return hideExitSurvey();
}

function setCookie(c_name, value, exdays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString() + "; path=/");
    document.cookie = c_name + "=" + c_value;
}

function getCookie(c_name) {
    var i, x, y;
    var ARRcookies = document.cookie.split(";");
    for (i = 0; i < ARRcookies.length; i++) {
        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|\s+$/g, "");
        if (x == c_name) {
            return unescape(y);
        }
    }
    return "";
}


function removeBlanks(v) {
    return v.replace(' ', '');
}

function isALink(e) {
    var targ = getTarget(e);
    return (targ.tagName == "A" ||
        (targ.parentElement && targ.parentElement.tagName == "A") ||
        (targ.parentNode && targ.parentNode.tagName == "A"));
}

function getTarget(e) {
    var targ;
    if (!e) var e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode;

    return targ;
}

function incCookie(cookieName) {
    counter = getCookie(cookieName);
    if (counter == "") counter = 0;
    setCookie(cookieName, parseInt(counter) + 1, 365);
}
function decCookie(cookieName) {
    counter = parseInt(getCookie(cookieName));
    setCookie(cookieName, counter - 1, 365);
}

function isSectionAvailabilities(currentUrl) {
    return currentUrl.toUpperCase().indexOf("AVAILABILITIES") > 0;
}
function isSectionFloorPlans(currentUrl) {
    return currentUrl.toUpperCase().indexOf("FLOORPLANS") > 0;
}
function isSectionUnitDetails(currentUrl) {
    return currentUrl.toUpperCase().indexOf("UNITDETAILSPOPUP.ASPX") > 0;
}
function isSectionSurvey(currentUrl) {
    return isSectionAvailabilities(currentUrl) || isSectionFloorPlans(currentUrl) || isSectionUnitDetails(currentUrl);
}
