var thisYear = new Date()
chkYear = thisYear.getUTCFullYear() - 1;

imageURL = "";
displayRating = "";
function getAggregateRating(ar){
var arv = parseFloat(ar);
    if (ar >=4.75 && ar <=5){
         imageURL="5";
      }else if(ar >4.25 && ar <4.75){
         imageURL="4_5";
      }else if(ar >=3.75 && ar <=4.25){
         imageURL="4";
      }else if(ar >3.25 && ar <3.75){
         imageURL="3_5";
      }else if(ar >=2.75 && ar <=3.25){
         imageURL="3";
      }else if(ar >2.25 && ar <2.75){
         imageURL="2_5";
      }else if(ar >=1.75 && ar <=2.25){
         imageURL="2";
      }else if(ar >1.25 && ar <1.75){
         imageURL="1_5";
      }else if(ar >=1 && ar <=1.25){
         imageURL="1";
    }
    displayRating = arv.toFixed(1);
}

function printNoBodySingleTile(styleId, year, make, model, tileId, photoURL, priceLow, priceHigh,
                               hwyMpgLow, hwyMpgHigh, cityMpgLow, cityMpgHigh, modelId, bodyType, rowID,
                               isHybrid,minPrice, maxPrice,AggregateRating,TotalRatingCount,thistrim,lcat) {
    headerStringMake = global_util.truncateString("" + make + "", 15);
    headerStringModel = global_util.truncateString("" + model + "", 15);
    priceLow = global_util.remTrailingZeros(priceLow);
    priceHigh = global_util.remTrailingZeros(priceHigh);
    var msrpConvertLow = global_util.formatCurrency(priceLow);
    var msrpConvertHigh = global_util.formatCurrency(priceHigh);
    var tileString = "";
    //TODO remove this once we've found a way to effectively search hybrid/bodystyle combos
    isHybrid = "n";
    var hybrid = isHybrid;
    if (hybrid == "") {
        hybrid = "n";
    } else {
        hybrid = "y";
    }
    var imgURL = "";
    if(photoURL != null)
    {
        imgURL = photoURL.replace(/\s+/, "");
    }
    if (thistrim == "undefined" || thistrim == null) {
        thistrim = "";
    }
    tileString += "<div class=\"results-container\">";
    tileString += "<div class=\"results-details\">";
    tileString += "<table class=\"results-table\" cellspacing=\"0\">";
    tileString += "<tr><td class=\"top-left\"></td>";
    tileString += "<td class=\"mid-section-top\"><div id=\"results-header-container\"><h4 title=\"" + make + ' ' + model + "\" class=\"results-headers\">" + headerStringMake + "<br/>"+ headerStringModel+"</h4></div></td>";    tileString += "<td class=\"top-right\"></td></tr>";
    tileString += "<tr><td colspan=\"3\" class=\"content-section\">";
    tileString += "<div class=\"results-details\" onClick=\"window.location='/research/car-models/" + year + "-" +
                  make.replace(/[^a-zA-Z0-9\.~_-]/g,"-") + "-" + model.replace(/[^a-zA-Z0-9\.~_-]/g,"-") +
                  "/" + modelId + "-" + isHybrid + ( bodyType != null ? "-" + bodyType : "" ) + "/" +
                  thistrim.replace(/[^a-zA-Z0-9\.~_-]/g,"-") + 
                  "~model.jsp?ad_min_price=" + minPrice
            + "&ad_max_price=" + maxPrice + "&sortBy=priceASC&rdpage=FRMGEN&lcat=" + lcat + "';\">";
    tileString += "<div id='p_" + tileId + "' class=\"photo\">";
    if (imgURL != "") {
        tileString += "<img src='http://images.autotrader.com/scaler/121/61/" + imgURL + "' />";
    } else {
        tileString += "<img src='/img/research/shared/image_not_available_121x61.gif' />";
    }
    tileString += "</div>";
    tileString += "<div id='y_" + tileId + "' class=\"year\"><h6>" + year + "</h6></div>";
    tileString += "<div id='m_" + tileId + "' class=\"msrp\">";
    if (year >= chkYear) {
        tileString += "<h6>MSRP</h6>";
    } else {
        tileString += "<h6>Price</h6>";
    }
    tileString += "<ul><li>" + msrpConvertLow + "&nbsp;-&nbsp;" + msrpConvertHigh + "&nbsp;</li></ul>";
    tileString += "</div>";
    tileString += "<div class=\"MPG\" id='mp_" + tileId + "'>";
    tileString += "<h6>Fuel Mileage</h6>";
    tileString += "<ul>";
    if (hwyMpgLow != "" && hwyMpgHigh != "") {
        tileString += "<li>Hwy: " + hwyMpgLow + "&nbsp;-&nbsp;" + hwyMpgHigh + " MPG</li>";
    } else {
        tileString += "<li>Hwy: N/A</li>";
    }
    if (cityMpgLow != "" && cityMpgHigh != "") {
        tileString += "<li>&nbsp; City: " + cityMpgLow + "&nbsp;-&nbsp;" + cityMpgHigh + " MPG</li>";
    } else {
        tileString += "<li>&nbsp; City: N/A</li>";
    }
    tileString += "</ul>";
    tileString += "</div>";
    tileString += "<div id='r_" + tileId + "' class=\"consumer-rating\">";
    if(null!= AggregateRating && AggregateRating>0 && null!=TotalRatingCount && TotalRatingCount>0) {
        tileString += "<h6>Avg. Consumer Rating</h6>";
        getAggregateRating(AggregateRating);
        if(TotalRatingCount==1){
        tileString += "<span style=\"position:relative;left:-2px;\"><img src=\"/img/research/reviews/stars0"+imageURL+".gif\"> "+displayRating+" of 5.0</span><br/>Based on "+TotalRatingCount+" review</div>";
        }else {
            tileString += "<span style=\"position:relative;left:-2px;\"><img src=\"/img/research/reviews/stars0"+imageURL+".gif\"> "+displayRating+" of 5.0</span><br/>Based on "+TotalRatingCount+" reviews</div>";
        }
    }else{
        tileString += "<div style=\"background-color:transparent;width:1px;height:26px;\">&nbsp;&nbsp;&nbsp;&nbsp;</div><br/></div>";
    }
    tileString += "</div><!--results-details-->";
    tileString += "<div class=\"compare-list\" id='cl_" + tileId + "' >";
    tileString += "<input type=\"checkbox\" id='checkbox_" + tileId + "' onClick=\"CB.addToCompare(this,'" + tileId + "','" + styleId + "','" + year + "','" + make + "','" + model + "','" + imgURL + "')\" />";
    tileString += "<label for='checkbox_" + tileId + "' class=\"pointer\" > Add to compare list</label>";
    tileString += "</div>";
    tileString += "</td></tr>";
    tileString += "<tr>";
    tileString += "<td id=\"bl_" + tileId + "\" class=\"bottom-left\"></td>";
    tileString += "<td id=\"msb_" + tileId + "\" class=\"mid-section-bottom\"></td>";
    tileString += "<td id=\"br_" + tileId + "\" class=\"bottom-right\"></td>";
    tileString += "</tr></table>";
    tileString += "</div></div>";
    return tileString;
}

// function printNoBodySingleTile
function printBodySingleTile(styleId, year, make, model, tileId, photoURL, priceLow, priceHigh, hwyMpgLow, hwyMpgHigh, cityMpgLow, cityMpgHigh, modelId, bodyStyleId, bodyType, rowID, isHybrid, minPrice, maxPrice,AggregateRating,TotalRatingCount,adBodyType) {
    headerStringMake = global_util.truncateString("" + make + "", 15);
    headerStringModel = global_util.truncateString("" + model + "", 15);
    priceLow = global_util.remTrailingZeros(priceLow);
    priceHigh = global_util.remTrailingZeros(priceHigh);
    var msrpConvertLow = global_util.formatCurrency(priceLow);
    var msrpConvertHigh = global_util.formatCurrency(priceHigh);
    var tileString = "";
    //TODO remove this once we've found a way to effectively search hybrid/bodystyle combos
    isHybrid = "n";
    var hybrid = isHybrid;
    if (hybrid == "") {
        hybrid = "n";
    } else {
        hybrid = "y";
    }
    var imgURL = "";
    if(photoURL != null)
    {
        imgURL = photoURL.replace(/\s+/, "");
    }
    tileString += "<div class=\"results-container\">";
    tileString += "<div class=\"results-details\">";
    tileString += "<table class=\"results-table\" cellspacing=\"0\">";
    tileString += "<tr><td class=\"top-left\"></td>";
    tileString += "<td class=\"mid-section-top\"><div id=\"results-header-container\"><h4 title=\"" + make + ' ' + model + "\" class=\"results-headers\">" + headerStringMake + "<br/>"+ headerStringModel+"</h4></div></td>";
    tileString += "<td class=\"top-right\"></td></tr>";
    tileString += "<tr><td colspan=\"3\" class=\"content-section\">";
    tileString += "<div class=\"results-details\" onClick=\"window.location='/research/model_info/model-overview.jsp?show=model&modelId=" + modelId +"&styleId="+styleId+ "&hybrid=" + isHybrid + "&ad_min_price=" + minPrice + "&ad_max_price=" + maxPrice + "&body_style=" + bodyType + "&ad_body_style=" + adBodyType + "';\">";
    tileString += "<div id='p_" + tileId + "' class=\"photo\">";
    if (imgURL != "") {
        tileString += "<img src='http://images.autotrader.com/scaler/121/61/" + imgURL + "' />";
    } else {
        tileString += "<img src='/img/research/shared/image_not_available_121x61.gif' />";
    }
    tileString += "</div>";

    tileString += "<div id='y_" + tileId + "' class=\"year\"><h6>" + year + "</h6></div>";
    if (bodyType == "") {
        tileString += "<div id='m_" + tileId + "' class=\"msrp\">";
    } else {
        tileString += "<div id='b_" + tileId + "' class=\"bodytypeOn\"><h6>" + bodyType + "</h6></div>";
        tileString += "<div id='m_" + tileId + "' class=\"msrpOn\">";
    }
    ;
    if (year >= chkYear) {
        tileString += "<h6>MSRP</h6>";
    } else {
        tileString += "<h6>Price</h6>";
    }
    tileString += "<ul><li>" + msrpConvertLow + "&nbsp;-&nbsp;" + msrpConvertHigh + "&nbsp;</li></ul>";
    tileString += "</div>";
    if (bodyType == "") {
        tileString += "<div class=\"MPG\" id='mp_" + tileId + "'>";
    } else {
        tileString += "<div class=\"MPGON\" id='mp_" + tileId + "'>";
    }
    tileString += "<h6>Fuel Mileage</h6>";
    tileString += "<ul>";
    if (hwyMpgLow != "" && hwyMpgHigh != "") {
        tileString += "<li>Hwy: " + hwyMpgLow + "&nbsp;-&nbsp;" + hwyMpgHigh + " MPG</li>";
    } else {
        tileString += "<li>Hwy: N/A</li>";
    }
    if (cityMpgLow != "" && cityMpgHigh != "") {
        tileString += "<li>&nbsp; City: " + cityMpgLow + "&nbsp;-&nbsp;" + cityMpgHigh + " MPG</li>";
    } else {
        tileString += "<li>&nbsp; City: N/A</li>";
    }
    tileString += "</ul>";
    tileString += "</div></div>";
    tileString += "<div id='r_" + tileId + "' class=\"consumer-rating\">";
    if(null!= AggregateRating && AggregateRating>0 && null!=TotalRatingCount && TotalRatingCount>0) {
        tileString += "<h6>Avg. Consumer Rating</h6>";
        getAggregateRating(AggregateRating);
        if(TotalRatingCount==1){
        tileString += "<span style=\"position:relative;left:-2px;\"><img src=\"/img/research/reviews/stars0"+imageURL+".gif\"> "+displayRating+" of 5.0</span><br/>Based on "+TotalRatingCount+" review</div>";
        }else {
            tileString += "<span style=\"position:relative;left:-2px;\"><img src=\"/img/research/reviews/stars0"+imageURL+".gif\"> "+displayRating+" of 5.0</span><br/>Based on "+TotalRatingCount+" reviews</div>";
        }
    }else{
       tileString += "<div style=\"background-color:transparent;width:1px;height:26px;\">&nbsp;&nbsp;&nbsp;&nbsp;</div><br/></div>";
    }
    tileString += "<div class=\"compare-list\" id='cl_" + tileId + "' >";
    tileString += "<input type=\"checkbox\" id='checkbox_" + tileId + "' onClick=\"CB.addToCompare(this,'" + tileId + "','" + styleId + "','" + year + "','" + make + "','" + model + "','" + imgURL + "')\" />";
    tileString += "<label for='checkbox_" + tileId + "' class=\"pointer\">Add to compare list</label>";
    tileString += "</div>";
    tileString += "</td></tr>";
    tileString += "<tr>";
    tileString += "<td id=\"bl_" + tileId + "\" class=\"bottom-left\"></td>";
    tileString += "<td id=\"msb_" + tileId + "\" class=\"mid-section-bottom\"></td>";
    tileString += "<td id=\"br_" + tileId + "\" class=\"bottom-right\"></td>";
    tileString += "</tr></table>";
    tileString += "</div></div>";
    return tileString;
}
//function printBodySingleTile


function printNoBodyMultipleTile(styleId, minYear, maxYear, make, model, tileId, photoURL, priceLow, priceHigh, hwyMpgLow, hwyMpgHigh, cityMpgLow, cityMpgHigh, modelId, genId, bodyType, rowID, isHybrid, minPrice, maxPrice,AggregateRating,TotalRatingCount,adBodyType) {
    headerStringMake = global_util.truncateString("" + make + "", 15);
    headerStringModel = global_util.truncateString("" + model + "", 15);
    priceLow = global_util.remTrailingZeros(priceLow);
    priceHigh = global_util.remTrailingZeros(priceHigh);
    var msrpConvertLow = global_util.formatCurrency(priceLow);
    var msrpConvertHigh = global_util.formatCurrency(priceHigh);
    var tileString = "";
    //TODO remove this once we've found a way to effectively search hybrid/bodystyle combos
    isHybrid = "n";
    var hybrid = isHybrid;
    if (hybrid == "") {
        hybrid = "n";
    } else {
        hybrid = "y";
    }
    var imgURL = "";
    if(photoURL != null)
    {
        imgURL = photoURL.replace(/\s+/, "");
    }
    tileString += "<div class=\"results-container\">";
    tileString += "<div class=\"results-details\">";
    tileString += "<table class=\"results-table\" cellspacing=\"0\">";
    tileString += "<tr><td class=\"top-left\"></td>";
    tileString += "<td class=\"mid-section-top\"><div id=\"results-header-container\"><h4 title=\"" + make + ' ' + model + "\" class=\"results-headers\">" + headerStringMake + "<br/>"+ headerStringModel+"</h4></div></td>";
    tileString += "<td class=\"top-right\"></td></tr>";
    tileString += "<tr><td colspan=\"3\" class=\"content-section\">";
    tileString += "<div class=\"results-details\" onClick=\"showLayer(" + genId + ",'generation_" + tileId + "'," + tileId + ", " + minYear + "," + maxYear + "," + rowID + ", '" + hybrid + "', '" + minPrice + "', '" + maxPrice + "', '" + bodyType + "', '" + imgURL + "','" + adBodyType +"'); return false;\">";
    tileString += "<div id='p_" + tileId + "' class=\"photo\">";
    if (imgURL != "") {
        tileString += "<img src='http://images.autotrader.com/scaler/121/61/" + imgURL + "' />";
    } else {
        tileString += "<img src='/img/research/shared/image_not_available_121x61.gif' />";
    }
    tileString += "</div>";
    tileString += "<div id='y_" + tileId + "' class=\"year\"><h6>" + minYear + "&nbsp;-&nbsp;" + maxYear + "</h6></div>";
    tileString += "<div id='m_" + tileId + "' class=\"msrp hidden\">";
    if ((minYear >= chkYear) && (maxYear >= chkYear)) {
        tileString += "<h6>MSRP</h6>";
    } else {
        tileString += "<h6>Price Range</h6>";
    }
    tileString += "<ul><li>" + msrpConvertLow + "&nbsp;-&nbsp;" + msrpConvertHigh + "&nbsp;</li></ul>";
    tileString += "</div>";
    tileString += "<div class=\"MPG hidden\" id='mp_" + tileId + "'>";
    tileString += "<h6>Fuel Mileage</h6>";
    tileString += "<ul>";
    if (hwyMpgLow != "" && hwyMpgHigh != "") {
        tileString += "<li>Hwy: " + hwyMpgLow + "&nbsp;-&nbsp;" + hwyMpgHigh + " MPG</li>";
    } else {
        tileString += "<li>Hwy: N/A</li>";
    }
    if (cityMpgLow != "" && cityMpgHigh != "") {
        tileString += "<li>&nbsp; City: " + cityMpgLow + "&nbsp;-&nbsp;" + cityMpgHigh + " MPG</li>";
    } else {
        tileString += "<li>&nbsp; City: N/A</li>";
    }
    tileString += "</ul>";
    tileString += "</div></div>";
    tileString += "<div id=\"vx_" + tileId + "\" class=\"view-years\">";
    tileString += "<div id='r_" + tileId + "' class=\"consumer-rating hidden\">";
     if(null!= AggregateRating && AggregateRating>0 && null!=TotalRatingCount && TotalRatingCount>0) {
       tileString += "<h6>Avg. Consumer Rating</h6>";
        getAggregateRating(AggregateRating);
        if(TotalRatingCount==1){
        tileString += "<span style=\"position:relative;left:-2px;\"><img src=\"/img/research/reviews/stars0"+imageURL+".gif\"> "+displayRating+" of 5.0</span><br/>Based on "+TotalRatingCount+" review</div>";
        }else {
            tileString += "<span style=\"position:relative;left:-2px;\"><img src=\"/img/research/reviews/stars0"+imageURL+".gif\"> "+displayRating+" of 5.0</span><br/>Based on "+TotalRatingCount+" reviews</div>";
        }
    }else{
        tileString += "<div style=\"background-color:transparent;width:1px;height:26px;\">&nbsp;&nbsp;&nbsp;&nbsp;</div><br/></div>";
    }
    tileString += "<div id=\"v_" + tileId + "\" class=\"viewallyears\">";
    tileString += "<span class=\"viewallyears\" id=\"v_" + tileId + "\"><a href=\"#\" onClick=\"showLayer(" + genId + ",'generation_" + tileId + "'," + tileId + ", " + minYear + "," + maxYear + "," + rowID + ",'" + hybrid + "', '" + minPrice + "', '" + maxPrice + "', '" + bodyType + "', '" + imgURL + "'); return false;\">View specific years</a></span>";
    tileString += "</div><div id=\"generation_" + tileId + "\" style=\"display: none;\" class=\"UltraPop genrow" + rowID + "\">";
    tileString += "</div>";
    /*************************************************************************************************************/
    tileString += "</td></tr>";
    tileString += "<tr>";
    tileString += "<td id=\"bl_" + tileId + "\" class=\"bottom-left\"></td>";
    tileString += "<td id=\"msb_" + tileId + "\" class=\"mid-section-bottom\"></td>";
    tileString += "<td id=\"br_" + tileId + "\" class=\"bottom-right\"></td>";
    tileString += "</tr></table>";
    tileString += "</div></div>";
    return tileString;
}
// function printNoBodyMultipleTile


function printBodyMultipleTile(styleId, minYear, maxYear, make, model, tileId, photoURL, priceLow, priceHigh, hwyMpgLow, hwyMpgHigh, cityMpgLow, cityMpgHigh, modelId, bodyStyleGenId, bodyType, rowID, isHybrid, minPrice, maxPrice,AggregateRating,TotalRatingCount,adBodyType) {
    headerStringMake = global_util.truncateString("" + make + "", 15);
    headerStringModel = global_util.truncateString("" + model + "", 15);
    priceLow = global_util.remTrailingZeros(priceLow);
    priceHigh = global_util.remTrailingZeros(priceHigh);
    var msrpConvertLow = global_util.formatCurrency(priceLow);
    var msrpConvertHigh = global_util.formatCurrency(priceHigh);
    var tileString = "";
    //TODO remove this once we've found a way to effectively search hybrid/bodystyle combos
    isHybrid = "n";
    var hybrid = isHybrid;
    if (hybrid == "") {
        hybrid = "n";
    } else {
        hybrid = "y";
    }
    var imgURL = "";
    if(photoURL != null)
    {
        imgURL = photoURL.replace(/\s+/, "");
    }
    tileString += "<div class=\"results-container\">";
    tileString += "<div class=\"results-details\">";
    tileString += "<table class=\"results-table\" cellspacing=\"0\">";
    tileString += "<tr><td class=\"top-left\"></td>";
    tileString += "<td class=\"mid-section-top\"><div id=\"results-header-container\"><h4 title=\"" + make + ' ' + model + "\" class=\"results-headers\">" + headerStringMake + "<br/>"+ headerStringModel+"</h4></div></td>";
    tileString += "<td class=\"top-right\"></td></tr>";
    tileString += "<tr><td colspan=\"3\" class=\"content-section\">";
    tileString += "<div class=\"results-details\" onClick=\"showBodyStyleLayer(" + bodyStyleGenId + ",'generation_" + tileId + "'," + tileId + ", " + minYear + ", " + maxYear + ", " + rowID + ",'" + hybrid + "', '" + minPrice + "', '" + maxPrice + "', '" + bodyType + "', '" + imgURL + "', '" + adBodyType + "'); return false;\">";
    // tileString += "<div class=\"results-details\" onClick=\"window.location='/research/model_info/model-overview.jsp?show=model&styleId=" + styleId + "&modelId=" + modelId + "&hybrid=" + isHybrid + "&min_price=" + minPrice + "&max_price=" + maxPrice + "';\">";
    tileString += "<div id='p_" + tileId + "' class=\"photo\">";
    if (imgURL != "") {
        tileString += "<img src='http://images.autotrader.com/scaler/121/61/" + imgURL + "' />";
    } else {
        tileString += "<img src='/img/research/shared/image_not_available_121x61.gif' />";
    }
    tileString += "</div>";
    tileString += "<div id='y_" + tileId + "' class=\"year\"><h6>" + minYear + "&nbsp;-&nbsp;" + maxYear + "</h6></div>";
    if (bodyType == "") {
        tileString += "<div id='m_" + tileId + "' class=\"msrp hidden\">";
    } else {
        tileString += "<div id='b_" + tileId + "' class=\"bodytypeOn hidden\"><h6>" + bodyType + "</h6></div>";
        tileString += "<div id='m_" + tileId + "' class=\"msrpOn hidden\">";
    }
    if ((minYear >= chkYear) && (maxYear >= chkYear)) {
        tileString += "<h6>MSRP</h6>";
    } else {
        tileString += "<h6>Price</h6>";
    }
    tileString += "<ul><li>" + msrpConvertLow + "&nbsp;-&nbsp;" + msrpConvertHigh + "&nbsp;</li></ul>";
    tileString += "</div>";
    if (bodyType == "") {
        tileString += "<div class=\"MPG hidden\" id='mp_" + tileId + "'>";
    } else {
        tileString += "<div class=\"MPGON hidden\" id='mp_" + tileId + "'>";
    }
    tileString += "<h6>Fuel Mileage</h6>";
    tileString += "<ul>";
    if (hwyMpgLow != "" && hwyMpgHigh != "") {
        tileString += "<li>Hwy: " + hwyMpgLow + "&nbsp;-&nbsp;" + hwyMpgHigh + " MPG</li>";
    } else {
        tileString += "<li>Hwy: N/A</li>";
    }
    if (cityMpgLow != "" && cityMpgHigh != "") {
        tileString += "<li>&nbsp; City: " + cityMpgLow + "&nbsp;-&nbsp;" + cityMpgHigh + " MPG</li>";
    } else {
        tileString += "<li>&nbsp; City: N/A</li>";
    }
    tileString += "</ul>";
    tileString += "</div></div>";
    tileString += "<div id=\"vx_" + tileId + "\" class=\"view-years\">";
    tileString += "<div id='r_" + tileId + "' class=\"consumer-rating hidden\">";
    if(null!= AggregateRating && AggregateRating>0 && null!=TotalRatingCount && TotalRatingCount>0) {
        tileString += "<h6>Avg. Consumer Rating</h6>";
        getAggregateRating(AggregateRating);
        if(TotalRatingCount==1){
        tileString += "<span style=\"position:relative;left:-2px;\"><img src=\"/img/research/reviews/stars0"+imageURL+".gif\"> "+displayRating+" of 5.0</span><br/>Based on "+TotalRatingCount+" review</div>";
        }else {
            tileString += "<span style=\"position:relative;left:-2px;\"><img src=\"/img/research/reviews/stars0"+imageURL+".gif\"> "+displayRating+" of 5.0</span><br/>Based on "+TotalRatingCount+" reviews</div>";
        }
    }else{
       tileString += "<div style=\"background-color:transparent;width:1px;height:26px;\">&nbsp;&nbsp;&nbsp;&nbsp;</div><br/></div>";
    }
    tileString += "<div id=\"v_" + tileId + "\" class=\"viewallyears\">";
    tileString += "<span class=\"viewallyears\" id=\"v_" + tileId + "\"><a href=\"#\" onClick=\"showBodyStyleLayer(" + bodyStyleGenId + ",'generation_" + tileId + "'," + tileId + ", " + minYear + "," + maxYear + "," + rowID + ",'" + hybrid + "', '" + minPrice + "', '" + maxPrice + "', '" + bodyType + "', '" + imgURL + "','" + AggregateRating + "', '" + TotalRatingCount + "'); return false;\">View specific years</a></span>";
    tileString += "</div><div id=\"generation_" + tileId + "\" style=\"display: none;\"  class=\"UltraPop genrow" + rowID + "\">";
    tileString += "</div>";
    /************************************************************************************************************/
    tileString += "</td></tr>";
    tileString += "<tr>";
    tileString += "<td id=\"bl_" + tileId + "\" class=\"bottom-left\" ></td>";
    tileString += "<td id=\"msb_" + tileId + "\" class=\"mid-section-bottom\"></td>";
    tileString += "<td id=\"br_" + tileId + "\" class=\"bottom-right\"></td>";
    tileString += "</tr></table>";
    tileString += "</div>";
    return tileString;
}
// function printBodyMultipleTile

function handleUltrapop(tileId){
    //Added to re-initialize the iframe for this new div
    _UltraPop_init();
    var genRow = "generation_"+tileId;
    $(genRow).toggleExclusive();
}

function getGenerationData(genId, tileId, id, startYear, endYear, isHybrid, minPrice, maxPrice, bodyType, photoURL, adBodyType,AggregateRating,TotalRatingCount) {

    var callback = function(results) {
        displayGenerationData(genId, tileId, id, startYear, endYear, results, isHybrid, minPrice, maxPrice, "",photoURL,adBodyType,AggregateRating,TotalRatingCount);
        handleUltrapop(tileId);
    };
    //call wrapper function instead to get NML entrypoint URL and pass in to displayGenerationData
    //ModelSearchUtil.searchByGenId(genId, isHybrid, callback)
    MIBadgeWrapper.searchByGenIdgetGenerationsTileBadge(genId, isHybrid, endYear, callback);
}

function getBodyStyleGenerationData(bodyStyleGenId, tileId, id, startYear, endYear, isHybrid, minPrice, maxPrice, bodyType, photoURL,adBodyType,AggregateRating,TotalRatingCount) {

    var callback = function(results) {
        displayGenerationData(bodyStyleGenId, tileId, id, startYear, endYear, results, isHybrid, minPrice, maxPrice, bodyType, photoURL,adBodyType,AggregateRating,TotalRatingCount);
        handleUltrapop(tileId);
    };
    //call wrapper function instead to get NML entrypoint URL and pass in to displayGenerationData
    //ModelSearchUtil.searchByBodyStyleGenId(bodyStyleGenId, isHybrid, callback)
    MIBadgeWrapper.searchByBodyStyleGenIdgetGenerationsTileBadge(bodyStyleGenId, isHybrid, endYear, callback);
}


function DivSetVisible(state)
  {
   var DivRef = document.getElementById('mainPopUpDiv');
   var IfrRef = document.getElementById('DivShim');
   if(state)
   {
    DivRef.style.zIndex = "50";
    DivRef.style.position = "absolute";
    IfrRef.style.width = DivRef.offsetWidth;
    IfrRef.style.height = DivRef.offsetHeight;
    IfrRef.style.top = DivRef.style.top;
    IfrRef.style.left = DivRef.style.left;
    IfrRef.style.zIndex = "-1";
    IfrRef.style.display = "block";
    //IfrRef.style.filter="progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)";
   }
   else
   {
    DivRef.style.display = "none";
    IfrRef.style.display = "none";
   }
  }

function displayGenerationData(genId, tileId, id, startYear, endYear, results, isHybrid, minPrice, maxPrice, bodyType, photoURL, adBodyType, AggregateRating,TotalRatingCount) {
    var chkYear = thisYear.getUTCFullYear() - 1;
    var tiles = results.modelSearchResults.tiles;
    //TODO remove this once we've found a way to effectively search hybrid/bodystyle combos
    isHybrid = "n";
    var hybrid = isHybrid;
    if (hybrid == "") {
        hybrid = "n";
    } else {
        hybrid = "y";
    }
    var imgURL = "";
    if(photoURL != null)
    {
        imgURL = photoURL.replace(/\s+/, "");
    }
    var genInfo = "";
    genInfo += "<div id=\"generation-makes\">";
    genInfo += "<table id=\"generation-makes-table\" cellspacing=\"0\">";
    genInfo += "<tr>";
    genInfo += "<th>" + tiles[0].make + " " + tiles[0].model + "<br />" + startYear + "-" + endYear + "</th>";
    genInfo += "</tr>";
    genInfo += "<tr>";
    if (imgURL != "") {
        genInfo += "<td><img src='http://images.autotrader.com/scaler/121/61/" + imgURL + "' /></td>";
    } else {
        genInfo += "<td><img src='/img/research/shared/image_not_available_121x61.gif' /></td>";
    }
    genInfo += "</tr>";
    genInfo += "</table>";
    genInfo += "</div>";
    genInfo += "<div id=\"generation-listings\">";
    genInfo += "<div class=\"clearfix\">";
    genInfo += "<div id=\"display-layer-sentence\">These are the years for this model generation that match your search.</div>";
    genInfo += "<div id=\"close-generation-layer\"><a href=\"#\" onClick=\"hidePopupLayer('generation_" + tileId + "'); return false;\">Close</a></div>";
    genInfo += "</div>";
    genInfo += "<table id=\"generation-listings-table\" cellspacing=\"0\">";
    genInfo += "<tr>";
    genInfo += "<th class=\"generation-year\">Year</th>";
    if (endYear >= chkYear) {
        genInfo += "<th>MSRP</th>";
    } else {
        genInfo += "<th>Price</th>";
    }
    genInfo += "<th>Fuel Mileage<br/>(Hwy./City)</th>";
    genInfo += "<th>Avg. Consumer Rating</th>";
    genInfo += "<th>Vehicle<br />Details</th>";
    genInfo += "<th>Select up to 4<br />to Compare</th>";
    genInfo += "</tr>";
    var a = [];
    var s = "";
    for (var i = 0; i < tiles.length; i++) {
        var t = tiles[i];
        var checkbox_id = tileId + "_" + i;
        var isCarSelected = "";
        genInfo += "<tr class=\"" + ((i % 2 == 0) ? "odd-row" : "even-row") + "\">";
        genInfo += "<td class=\"generation-year\">" + tiles[i].year + "</td>";
        var pl = defaultString(t.priceLow);
        var ph = defaultString(t.priceHigh);
        var priceLow = global_util.remTrailingZeros(pl);
        var priceHigh = global_util.remTrailingZeros(ph);
        var msrpConvertLow = global_util.formatCurrency(priceLow);
        var msrpConvertHigh = global_util.formatCurrency(priceHigh);
        a = [msrpConvertLow, " - ", msrpConvertHigh];
        s = a.join("");
        genInfo += "<td class=\"generation-price\">" + s + "</td>";
        var hl = t.hwyLow;
        var hh = t.hwyHigh;
        var cl = t.cityLow;
        var ch = t.cityHigh;
        var h = getMPG(hl, hh);
        var c = getMPG(cl, ch);
        a = [h, " /", "<br/>", c, " MPG"];
        s = a.join("");
        genInfo += "<td class=\"generation-mpg\">" + s + "</td>";
        var aggratingpopup = tiles[i].AggregateRating;
        var TotalRatingCount = tiles[i].TotalRatingCount;
        if( (TotalRatingCount != null) && (TotalRatingCount > 0) && (aggratingpopup != null) && (aggratingpopup != 0) )
        {
             getAggregateRating(aggratingpopup);
             genInfo += "<td class=\"generation-rating\">";
             genInfo += "<img src=\"/img/research/reviews/stars0";
             genInfo += imageURL;
             genInfo += ".gif\"> ";
             genInfo += displayRating;
             genInfo += " of 5.0  (";
             genInfo += TotalRatingCount;
             genInfo += ")";
             genInfo += "</td>";
        }
        else
        {
             genInfo += "<td class=\"generation-rating\">";
             genInfo += "&nbsp;";
             genInfo += "</td>";
        }
        genInfo += "<td class=\"generation-link\"><a href=\"/research/car-models/" + tiles[i].year + "-" + tiles[i].make.replace(/[^a-zA-Z0-9\.~_-]/g,"-") + "-" + tiles[i].model.replace(/[^a-zA-Z0-9\.~_-]/g,"-") + "/" + tiles[i].modelId + ( tiles[i].styleId != null ? "-" + tiles[i].styleId : "" )+( isHybrid != "y" ? "-" + isHybrid : "" ) + ( bodyType != null ? "-" + bodyType : "" ) + "/" + tiles[i].style.replace(/[^a-zA-Z0-9\.~_-]/g,"-") + "~model.jsp?ad_min_price=" + minPrice + "&ad_max_price=" + maxPrice + "&sortBy=priceASC&rdpage=FRMGEN\">View details</a></td>";
        for(var j=0;j<compareList.length;j++)
        {
             if(compareList[j].styleId == tiles[i].styleId)
             {
                  isCarSelected = " checked='checked' ";
                  break;
             }
        }
        genInfo += "<td class=\"generation-compare\"><input type=\"checkbox\" id='checkbox_" + checkbox_id + "' " + isCarSelected + " name=\" \" value=\" \" onClick=\"CB.addToCompare(this,'" + checkbox_id + "','" + tiles[i].styleId + "','" + tiles[i].year + "','" + tiles[i].make + "','" + tiles[i].model + "','" + tiles[i].photo + "')\" /></td>";
        genInfo += "</tr>";
    }
    genInfo += "</table>";
    genInfo += "<img src=\"/no_cache/ac/model_info_view_all_years.asis\" width=\"1\" height=\"1\" border=\"0\">";
    genInfo += "</div>";
    genInfo += "<br class=\"clear\" />";
    //add NML entry point and parameter to be passed in
    //get the URL passed in
    if(results.badgeBean != null && results.badgeBean.url != null) {
        var nmlUrl = results.badgeBean.url;
        if (nmlUrl != null) {
            //change the link href and model name text
            var templateNml = new Template( unescape($F("misrp-gentile-nml")));
            var nmlData = {link: nmlUrl, model: tiles[0].model};
            Object.toJSON(nmlData);
            var nmlEL = templateNml.evaluate(nmlData);
            genInfo += nmlEL;
        }
    }

    document.getElementById(id).innerHTML = genInfo;
}

/**
 * Gets a formatted MPG string.
 * @param lo the low number.
 * @param hi the high number.
 * @return a formatted MPG string.
 * Examples:
 * - lo=null and hi=20   :  20
 * - lo=10   and hi=null :  10
 * - lo=10   and hi=20   :  10 - 20
 * - lo=null and hi=null :  N/A
 */
function getMPG(lo, hi)
{
    // Clean the strings.
    var l = defaultString(lo);
    var h = defaultString(hi);
    // Test for both parameters being null.
    if( (l == "") && (h == "") )
    {
        return "-";
    }
    // Test for lo is null and hi is not.
    if(l == "")
    {
        return h;
    }
    // Test for hi is null and lo is not.
    if(h == "")
    {
        return l;
    }
    // If none of the above conditions match, then neither one is null.
    var a = [l, " - ", h];
    var s = a.join("");
    return s;
}

/**
 * protects against a null input String
 * @param s the String to check, may be null
 * @return the passed in String, or the empty String if it was null
 */
function defaultString(s) {
    return (null == s) ? "" : s;
}
