﻿/*
============================
PROPERTY LIGHTBOX FUNCTIONS
============================
*/

function StartLightBoxMortgageCalc(visistatdid, propertyaddress) {
    InsertExtraVisistat(visistatdid, propertyaddress);

    $('#propertymortgage-thankyou').hide();
    $('#propertymortgage-info').find('.lightboxtopwrapper').show();
    $('#propertymortgage-info').find('.lightboxform').show();
    $('#propertymortgage-lightbox').show().fadeTo(300, 0.6, function () { startLightBox2extraproperty('#propertymortgage-lightbox', '#propertymortgage-info', '#propertymortgage-wrapper'); });

}

function StartLightBoxRequestFeedback(visistatdid, propertyaddress) {
    InsertExtraVisistat(visistatdid, propertyaddress);

    $('#propertyfeedbackrequest-thankyou').hide();
    $('#propertyfeedbackrequest-info').find('.lightboxtopwrapper').show();
    $('#propertyfeedbackrequest-info').find('.lightboxform').show();
    $('#propertyfeedbackrequest-lightbox').show().fadeTo(300, 0.6, function () { startLightBox2extraproperty('#propertyfeedbackrequest-lightbox', '#propertyfeedbackrequest-info', '#propertyfeedbackrequest-wrapper'); });
}

function StartLightBoxFeedbackRating(visistatdid, propertyaddress) {
    InsertExtraVisistat(visistatdid, propertyaddress);

    $('#propertyfeedbackrateform-thankyou').hide();
    $('#propertyfeedbackrateform-info').find('.lightboxtopwrapper').show();
    $('#propertyfeedbackrateform-info').find('.lightboxform').show();
    $('#propertyfeedbackrateform-lightbox').show().fadeTo(300, 0.6, function () { startLightBox2extraproperty('#propertyfeedbackrateform-lightbox', '#propertyfeedbackrateform-info', '#propertyfeedbackrateform-wrapper'); });
}


//finish loading up the show and setup the close button
function startLightBox2extraproperty(divtoclose, divtoshow, wrapper) {
    $(wrapper).show().fadeTo(300, 1.0);

    $(divtoshow).show();
    $(wrapper).find('a.close').click(
	    function () {
	        $(wrapper).fadeTo(300, 0.0, function () { closeLightboxextraproperty(divtoclose, wrapper); });
	    }
	);
}

function closeLightboxextraproperty(divtoclose, wrapper) {
    closeJsError_PropertyUtils();
    $(divtoclose).fadeTo(300, 0.0, function () {
        $(wrapper).hide();
        $(divtoclose).hide();
    });
}

function constructErrorMsg_PropertyUtils(errormsg, addme) {
    if (errormsg != '') {
        errormsg = errormsg + ', ';
    }
    errormsg = errormsg + addme;

    return errormsg;
}

function showJsError_PropertyUtils(errormsg) {
    if (errormsg != '') {
        $('.formerror').html(errormsg);
    }
    $('#lightbox-messaging').show().animate({ top: "0" }, { duration: 800, easing: 'easeOutExpo' }).animate({ top: "-50px" }, { duration: 300, easing: 'easeInExpo' });

    setTimeout('$("#lightbox-messaging a.closeerror").fadeTo(800, 1.0);', 1200);

    $('#lightbox-messaging a.closeerror').click(
		   function () {
		       closeJsError_PropertyUtils();
		   }
	    );
}

function OnPropertyAjaxError(arg, msg) {
    showJsError_PropertyUtils(msg);
}

function closeJsError_PropertyUtils() {
    $("#lightbox-messaging a.closeerror").fadeTo(800, 0.0);
    $('#lightbox-messaging').animate({ top: "-300px" }, { duration: 800, easing: 'easeInExpo', complete: hideErrorMessaging_PropertyUtils });
}

function hideErrorMessaging_PropertyUtils() {
    $('#lightbox-messaging').hide();
}


function ThankYouPropertyUtils(title, msg, thankyoudivid, infodivid) {
    $(thankyoudivid).find('h2').html(title);
    $(thankyoudivid).find('p').html(msg);

    $(infodivid).find('.lightboxtopwrapper, .lightboxform').hide('slow', function () { $(thankyoudivid).show('slow'); });
}

/*
============================
MORTGAGE CALCULATOR FUNCTIONS
============================
*/

function CalculateMonthlyPayment() {
    var interest, term, formula, payment, amount;

    interest = $('#propertymortgage-info').find('#interest').val();
    interest = interest.replace('%', '');
    interest = interest.replace(',', '');

    term = $('#propertymortgage-info').find('#length').val();

    amount = $('#propertymortgage-info').find('#principal').val();
    amount = amount.replace('$', '');
    amount = amount.replace(',', '');

    var errorMsg = '';

    if (isNumeric(amount) == false) {
        errorMsg = appendErrorMsgComma(errorMsg, R.Common.InvalidPrice);
    }

    if (isNumeric(interest) == false) {
        errorMsg = appendErrorMsgComma(errorMsg, R.Common.InvalidInterest);
    }

    if (isNumeric(term) == false) {
        errorMsg = appendErrorMsgComma(errorMsg, R.Common.InvalidTerm);
    }

    interest = interest / 100;
    term = term * 12;
    formula = (Math.pow((1 + interest / 12), term) * interest) / (12 * (Math.pow((1 + interest / 12), term) - 1));
    payment = amount * formula;
    payment = Math.round(payment);

    $('#propertymortgage-info').find('#payment').val(payment)
}



/*
============================
FEEDBACK REQUEST FUNCTIONS
============================
*/

function SendFeedbackRequest() {

    var vFields, pName, form, errorMsg = '';

    //enter in the field id's to be validated
    vFields = ["feedback_name", "feedback_company", "feedback_phone", "feedback_email"];
    //enter in the display names for each field in the same order as above
    pName = [R.Common.YourName, R.Common.YourCompany, R.Common.YourPhone, R.Common.YourEmail];

    errorMsg = validate(false, vFields, pName);

    form = $('#feedback-request-form');

    if (errorMsg == 'success') {

        $.post(
            form.attr('action'),
            form.serialize(),
            function (d) {
                if (d.Success) {
                    closeJsError_PropertyUtils();
                    ThankYouPropertyUtils(R.Common.Success, d.Message, '#propertyfeedbackrequest-thankyou', '#propertyfeedbackrequest-info');
                }
                else {
                    showJsError_PropertyUtils(d.Message);
                }
            }
        );

    }
    else {
        showJsError_PropertyUtils(errorMsg);
    }
}



/*
============================
FEEDBACK RATING FUNCTIONS
============================
*/

function SendFeedbackRating() {

    var form = $('#feedback-rating-form');

    $.post(
        form.attr('action'),
        form.serialize(),
        function (d) {
            if (d.Success) {
                closeJsError_PropertyUtils();
                $('#propertyfeedbackrateform-wrapper').hide('slow');
            }
            else {
                showJsError_PropertyUtils(d.Message);
            }
        }
    );
}


/*
============================
LIGHTBOXES
============================
*/

$(document).ready(function () {
    $('#add-info').rpslightbox({ preload: setAddInfoCallback, canceled: addInfoCallback });
    $('#map-property').rpslightbox({ preload: setMapCallback, canceled: mapCallback });
    $('#virtual-tour').rpslightbox({ preload: setVirtualTourCallback, canceled: virtualTourCallback });
});

function setMapCallback() {
    totalaccesscallback = mapCallback;
    return isTotalAccessCookieSet();
}

function setVirtualTourCallback() {
    totalaccesscallback = virtualTourCallback;
    return isTotalAccessCookieSet();
}

function setAddInfoCallback() {
    totalaccesscallback = addInfoCallback;
    return isTotalAccessCookieSet();
}

function addInfoCallback() {
    var vid = '';

    //DID is the visistat ID that is inserted in the tracking code at the
    // footer of the page. If it exists, we use it
    try { vid = DID; }
    catch (e) { }

    StartLightBoxAddInfo(DID, R.Common.PropertyDetailsAdditionalInfo + $('body h2').text());
}

function mapCallback() {
    window.location = getWindowLocation() + "/Map";
}

function virtualTourCallback() {
    window.location = getWindowLocation() + "/VirtualTour";
}

function getWindowLocation() {
    var loc = window.location.href;
    loc = loc.replace(/#.*$/, '');
    return loc;
}

function isTotalAccessCookieSet() {
    var val = readCookieSubkey("TotalAccess", "Access");
    return (val == "true");
}

function submitTotalAccessForAddInfo() {
    closeMessage();

    showAddInfoProcessing();

    var name = $('#pmi-name').val();
    var email = $('#pmi-email').val();
    var phone = $('#pmi-phone').val();
    var comments = $('#pmi-comments').val();
    var addr = $('#pmi-address').val();
    var ticks = $('#pmi-id').val();
    var propertyId = $('#pmi-propid').val();
    var buyerseller = "buyer";
    var extravals = 'addrinfo=' + escape(addr) + '&trackingfield1=Property%20Detail&trackingfield2=' + escape(addr);

    submitTotalAccessGlobalFinal(name, email, phone, comments, buyerseller, extravals, ticks, true, propertyId, submitTotalAccessForAddInfoCallback, submitTotalAccessForAddInfoCallbackError);
}

function showAddInfoProcessing() {
    if ($('#property-moreinfo-form .actions').find('img.ajax-loader').length == 0)
        $('#property-moreinfo-form .actions').append('<img src="/img/ajax-loader-bar.gif" class="ajax-loader" alt="processing" style="width:220px; display:block; margin:auto;" />');

    $('#property-moreinfo-form .actions').find('img.ajax-loader').show();
}

function submitTotalAccessForAddInfoCallbackError(arg) {
    $('#property-moreinfo-form .actions').find('img.ajax-loader').fadeOut(500);
}

function submitTotalAccessForAddInfoCallback(arg) {
    $('#property-moreinfo-form').fadeOut(500, function () {
        $('#property-moreinfo-result').find('.processed').show();
        $('#property-moreinfo-form .actions').find('img.ajax-loader').hide();
        $('#property-moreinfo-result').fadeIn(500);
    });
}
