﻿$(document).ready(function(){  
    var listingFile = jQuery("#featuredList").val();
    var listingStyle = jQuery("#featuredStyle").val();
    var agentID = jQuery("#agentid").val();
    if(listingFile.length > 0 && listingStyle.length > 0){
        jQuery.post("/include/ajax/ajaxproxy.aspx?url=http://prudentialcaliforniare.com/prucal/Supplements/featured/request.aspx",{list:listingFile,style:listingStyle,agentid:agentID},function(data){
            jQuery("#featuredListing").empty().append(data.replace(/\/Large/g,'/Small'));
            jQuery("#featuredListing .lux-price").each(function(i){
                var dPrice = jQuery(this).text();
                var cPrice = formatPrice(dPrice);
                jQuery(this).text(cPrice);
            });
        });
    }
});

function formatPrice(price){
    var formattedPrice = "";
    var noDecimal = price.substring(0,price.length - 3);
    if(noDecimal.length == 6){
        formattedPrice = "$" + noDecimal.substring(0,3) + "," + noDecimal.substring(3,6);
    }else{
        var partModulus = noDecimal.length%3;
        var firstPricePart = noDecimal.substring(0,partModulus);
        var lastPricePart = noDecimal.substring(partModulus,noDecimal.length);
        formattedPrice = "$" + firstPricePart;
        for(var i=0;i<lastPricePart.length;i=i+3){
            formattedPrice += "," + lastPricePart.substring(i,i+3);
        }
    }
    return formattedPrice;
}