/* Path for this file: /content/dam/nissan/codepack/nissan/components/pages/layout/mainlayout.js/jcr:content/renditions/original */
// these were pulled from main JS file into mainlayout.js
/*
jQuery(document).ready(function() {
    if(jQuery("#frameBg").size()>0){
        //jQuery("#frameBg").height(jQuery("#frameContent").height()+20);
    }
    jQuery("#frameContent").click(function(){
        //jQuery("#frameContent").html(jQuery("#frameContent").html()+"<br /><br />");
        //resizeMe(jQuery("#frameContent").height());
    });    
});
function resizeMe(hgt){
    if(jQuery("#frameBg").size()>0){
        jQuery("#frameBg").height(hgt+20);
        if(jQuery("#frameContent").height() + 50 > jQuery("#pageBg").height()){
            //jQuery("#pageBg").height(jQuery("#pageBg").height()+20);
        }
    }
}
*/

jQuery(document).ready(function() {
    if(jQuery.browser.msie && parseInt(jQuery.browser.version) === 6) jQuery('#frameBg + #frameContent').addClass('frameContentHasBg');
 
    if(jQuery("#frameContent").height() + 50 > jQuery("#pageBg").height()){         
            jQuery("#pageBg").css("margin-bottom","50px");
        }
    
});


/* cpo.js */
var zipHasFocus = false;

function ShowZipToolTip() {
    if (jQuery('#zipToolTip')) {
        jQuery("#zipToolTip").fadeIn("slow");
        if (!zipHasFocus) {
            jQuery('#zipcode').focus();
            jQuery('#zipcode').select();
        }
        setTimeout('HideZipToolTip()', 2000);
    }
}

function HideZipToolTip() {
    if (jQuery('zipToolTip')) {
        jQuery("#zipToolTip").fadeOut("slow");
    }
}

function getIEVersion() {
    var version = 0
    if (navigator.appVersion.indexOf("MSIE") != -1) {
        temp = navigator.appVersion.split("MSIE")
        version = parseFloat(temp[1])
    }
    return version
}

function isIE() {
    var version = 0
    if (navigator.appVersion.indexOf("MSIE") != -1) {
        temp = navigator.appVersion.split("MSIE")
        version = parseFloat(temp[1])
    }
    return (version > 0);
}

function writeZipInput(zipInput) {
    zipHasFocus = false;
    if (zipInput.value == '') {
        zipInput.maxLength = "20";
        if (location.pathname.match("us/es") != null) {
            zipInput.value = 'Entra el codigo postal';
        } else {
            zipInput.value = 'Enter Zip Code';
        }
    }
}

function clearZipInput(zipInput) {
    zipHasFocus = true;
    if (zipInput.value == 'Enter Zip Code' || zipInput.value == 'Entra el codigo postal') {
        zipInput.value = '';
        zipInput.maxLength = "5";
    }
    zipInput.select();
}

function checkZip() {
    if (!jQuery('#zipcode')) {
        return false;
    }

    if (jQuery('#zipcode').val().length < 5) {
        ShowZipToolTip();
        return false;
    }

    var validZip = /^[0-9]{1,5}$/;
    if (validZip.test(jQuery('#zipcode').val())) {
        return true;
    } else {
        ShowZipToolTip();
        return false;
    }
}

function onlyNumbers(evt, zipInput) {
    zipInput.maxLength = "5";

    var e = evt;

    if (isIE()) var charCode = e.which || e.keyCode;
    else var charCode = e.which;

    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        return false;
    }

    if (charCode == 13) {
        zipHasFocus = false;
        searchThis();
        return false;
    }

    HideZipToolTip();

    return true;
}

function submitZip() {
    var zip = jQuery('#zipcode').value;
    if (checkZip()) {
        crmEvent6();
        hideThis("gMain");
        showThis("search_results");
    }
}

// adjust position of zip tooltip
function adjustZipToolTipPosition() {
    if (getIEVersion() < 7) {
        if (jQuery('zipToolTip')) {
            //$('#zipToolTip').style.top = 120;
        }
    }
}

//SR-18968
//Redirecting to Pidget/testgateway based on URL
function pidgetURL() {
    var currentLocation = window.location;

    if ((String(currentLocation).indexOf("newdev") != -1) || (String(currentLocation).indexOf("qa1") != -1) || (String(currentLocation).indexOf("qa2") != -1) || (String(currentLocation).indexOf("stage") != -1)) liveURL = false;
    else liveURL = true;
    return liveURL;
}
//end of SR-18968
addLoadEvent(adjustZipToolTipPosition);

/* cpo_cookie.js */
/*Cookie based - More Advance search page*/
var alerts = false;

var zipCode = '';
var ZipCode = '';
var distance = '';
var bodystyle_val;
var bodystyle_val_2 = 'Any';
var start_year = '';
var end_year = '';
var limit = '';
var transmission = '';
var mileage = '';
var fuel = '';
var ext_color = 'red';
var engine = '';
var drive = '';
var only_photo = new Boolean();
var fuel = '';
var vehicles1;
var vehicles2;
var vehicles3;
var min_price = "";
var max_price = "";
var features = new Array();
var feature0 = '';
var feature1 = '';
var feature2 = '';
var feature3 = '';
var feature4 = '';
var feature5 = '';
var feature6 = '';
var feature7 = '';
var feature8 = '';
var feature9 = '';

var t = false; /*Boolean to check whether zipcode is entered or not*/

function setBodyStyle(bodyStyle) {
    bodystyle_val_2 = bodyStyle;
}

function setCookie() {

    if (alerts) alert("Entered setCookie()");

    var form = document.search_form;
    var expiredays = '1';

    zipCode = document.getElementById("zipcode").value;
    ZipCode = document.getElementById("zipcode").value;
    distance = form.distance.value;
    vehicles1 = getValueofSelect(form.vehicle_option_1);
    vehicles2 = getValueofSelect(form.vehicle_option_2);
    vehicles3 = getValueofSelect(form.vehicle_option_3);
    start_year = form.start_year.value;
    end_year = form.end_year.value;
    limit = form.limit_sel.value;
    transmission = '';
    mileage = form.mileage_sel.value;
    fuel = form.fuel_sel.value;
    ext_color = form.ext_color_sel.value;
    engine = form.engine_sel.value;
    drive = form.drive_sel.value;
    only_photo = form.only_photo.checked;
    
    if (form.model_featues[0].checked)
        feature0 = form.model_featues[0].value;

    if (form.model_featues[1].checked) 
        feature1 = form.model_featues[1].value;

    if (form.model_featues[2].checked) 
        feature2 = form.model_featues[2].value;

    if (form.model_featues[3].checked) 
        feature3 = form.model_featues[3].value;

    if (form.model_featues[4].checked) 
        feature4 = form.model_featues[4].value;

    if (form.model_featues[5].checked) 
        feature5 = form.model_featues[5].value;

    if (form.model_featues[6].checked) 
        feature6 = form.model_featues[6].value;

    if (form.model_featues[7].checked) 
        feature7 = form.model_featues[7].value;

    if (form.model_featues[8].checked) 
        feature8 = form.model_featues[8].value;

    if (form.model_featues[9].checked) 
        feature9 = form.model_featues[9].value;

    for (var i = 0, count = 0; i < form.model_featues.length; i++)
    if (form.model_featues[i].checked) {
        features[count] = form.model_featues[i].value;
        count++;
    }

    for (var i = 0; i < form.bodystyle.length; i++)
    if (form.bodystyle[i].checked) bodystyle_val = form.bodystyle[i].value;
    else {
        if (bodystyle_val == 'Any' && (vehicles1 != 'Any' || vehicles2 != 'Any' || vehicles3 != 'Any')) {
            bodystyle_val = bodystyle_val_2;
        }
    }

    for (var i = 0; i < form.transmission.length; i++)
    if (form.transmission[i].checked) transmission = form.transmission[i].value;

    var ExpireDate = new Date();
    ExpireDate.setTime(ExpireDate.getTime() + (1 * 24 * 3600 * 1000));
    document.cookie = "name=form_cookie" + "=" + ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());

    if (zipCode.length < 5) zipCode = 'Enter Zip Code';

    document.cookie = 'zipCode=' + zipCode + ';';
    document.cookie = 'ZipCode=' + ZipCode + ';';
    document.cookie = 'distance=' + distance + ';';
    document.cookie = 'bodystyle_val=' + bodystyle_val + ';';
    document.cookie = 'start_year=' + start_year + ';';
    document.cookie = 'end_year=' + end_year + ';';
    document.cookie = 'limit=' + limit + ';';
    document.cookie = 'transmission=' + transmission + ';';
    document.cookie = 'ext_color=' + ext_color + ';';
    document.cookie = 'mileage=' + mileage + ';';
    document.cookie = 'engine=' + engine + ';';
    document.cookie = 'drive=' + drive + ';';
    document.cookie = 'fuel=' + fuel + ';';
    document.cookie = 'only_photo=' + only_photo + ';';
    document.cookie = 'vehicles1=' + vehicles1 + ';';
    document.cookie = 'vehicles2=' + vehicles2 + ';';
    document.cookie = 'vehicles3=' + vehicles3 + ';';
    document.cookie = 'features=' + features + ';';
    document.cookie = 'feature0=' + feature0 + ';';
    document.cookie = 'feature1=' + feature1 + ';';
    document.cookie = 'feature2=' + feature2 + ';';
    document.cookie = 'feature3=' + feature3 + ';';
    document.cookie = 'feature4=' + feature4 + ';';
    document.cookie = 'feature5=' + feature5 + ';';
    document.cookie = 'feature6=' + feature6 + ';';
    document.cookie = 'feature7=' + feature7 + ';';
    document.cookie = 'feature8=' + feature8 + ';';
    document.cookie = 'feature9=' + feature9 + ';';

    if (limit.indexOf("Any") != -1) {
        min_price = "";
        max_price = "Any";
    }
    else if (limit.indexOf("to") != -1) {
        min_price = limit.substr(1, limit.indexOf(" to ") - 1);
        max_price = limit.substr(limit.indexOf(" to ") + 4, limit.length);
    }
    else if (limit.indexOf("+") != -1) {
        min_price = limit.substr(1, limit.indexOf(" +") - 1);
        max_price = "";
    }
    else if (limit.indexOf("Under") != -1) {
        min_price = "0";
        max_price = limit.substr(limit.indexOf("$") + 1, limit.length);
    }

    min_price = min_price.replace(",", "");
    max_price = max_price.replace(",", "");

    var t = validateCPOZipCode(document.search_form);

    if (alerts) alert("Exit setCookie()");

    if (t) {
        searchResultsURL();
    }

}

function searchResultsURL() {
    if (alerts) alert("Entered searchResults()");

    var url = '/apps/cpo/searchResults?zipCode=' + escape(zipCode) + '&distance=' + escape(distance) + '&bodystyle=' + escape(bodystyle_val);

    if (document.search_form.vehicle_option_2.disabled) {
        url += '&vehicles1=' + escape(vehicles1) + '&vehicles2=&vehicles3=';
    }
    else if (document.search_form.vehicle_option_3.disabled) {
        url += '&vehicles1=' + escape(vehicles1) + '&vehicles2=' + escape(vehicles2) + '&vehicles3=';
    }
    else url += '&vehicles1=' + escape(vehicles1) + '&vehicles2=' + escape(vehicles2) + '&vehicles3=' + escape(vehicles3);

    url += '&start_year=' + escape(start_year) + '&end_year=' + escape(end_year) + '&min_price=' + escape(min_price) + '&max_price=' + escape(max_price) + '&transmission=' + escape(transmission) + '&fuel=' + escape(fuel) + '&ext_color=' + escape(ext_color) + '&mileage=' + escape(mileage) + '&engine=' + escape(engine) + '&drive=' + escape(drive) + '&only_photo=' + escape(only_photo) + '&feature0=' + escape(feature0) + '&feature1=' + escape(feature1) + '&feature2=' + escape(feature2) + '&feature3=' + escape(feature3) + '&feature4=' + escape(feature4) + '&feature5=' + escape(feature5) + '&feature6=' + escape(feature6) + '&feature7=' + escape(feature7) + '&feature8=' + escape(feature8) + '&feature9=' + escape(feature9) + '&features=' + escape(features);

    window.location.href = url;

    if (alerts) alert("Exit searchResults()");
}

function backTosearchResults() {
    var searchURL = location.search.substr(location.search.indexOf("&&") + 2, location.search.length);

    window.location = "/apps/cpo/searchResults?" + searchURL;
}

function getCookie(NameOfCookie) {
    begin = document.cookie.indexOf(NameOfCookie + "=");
    if (begin != -1) {
        begin += NameOfCookie.length + 1;
        end = document.cookie.indexOf(";", begin);
        if (end == -1) end = document.cookie.length;
        return unescape(document.cookie.substring(begin, end));
    }
    return null;
}

function getValueofSelect(arr) {
    for (var i = 0; i < arr.length; i++)
    if (arr[i].selected) {
        return (arr[i].value);
    }
}

function checkCookie() {
    if (alerts) alert("Entered checkCookie()");

    if (getCookie('distance')) {
        if (alerts) alert("Cookie is present in the page");
        zipCode = getCookie('zipCode');
        distance = getCookie('distance');
        bodystyle_val = getCookie('bodystyle_val');
        vehicles = getCookie('vehicles');
        start_year = getCookie('start_year');
        end_year = getCookie('end_year');
        limit = getCookie('limit');
        transmission = getCookie('transmission');
        mileage = getCookie('mileage');
        ext_color = getCookie('ext_color');
        engine = getCookie('engine');
        drive = getCookie('drive');
        fuel = getCookie('fuel');
        features = getCookie('features');
        only_photo = getCookie('only_photo');
        vehicles1 = getCookie('vehicles1');
        vehicles2 = getCookie('vehicles2');
        vehicles3 = getCookie('vehicles3');

        showCookie();
    }
    else if (getCookie('vehicles1')) {
        document.getElementById("zipcode").value = getCookie('zipCode');
        vehicles1 = getCookie('vehicles1');
        selectThis("vehicle_option_1", vehicles1, "select");
        bodystyle_val = getCookie('bodystyle_val');
        selectThis(document.search_form.bodystyle, bodystyle_val, 'radio');
    }
    else {
        document.getElementById("zipcode").value = getCookie('zipCode');
    }

    if (alerts) alert("Leaving checkCookie()");
}

function showCookie() {

    if (alerts) alert("Entered showCookie()");

    var form = document.search_form;

    document.getElementById("zipcode").value = zipCode;
    form.distance.value = distance;

    only_photo = only_photo.indexOf('true') != -1 ? true : false;
    form.only_photo.checked = only_photo;

    selectThis(form.bodystyle, bodystyle_val, 'radio');
    setData(bodystyle_val);

    if (vehicles1 != "Any") {
        selectThis("vehicle_option_1", vehicles1, "select");
    }

    if (vehicles2 != "Any") {
        document.getElementById("vehicle_option_2").disabled = false;
        selectThis("vehicle_option_2", vehicles2, "select");
    }

    if (vehicles3 != "Any") {
        document.getElementById('vehicle_option_2').disabled = false;
        selectThis("vehicle_option_3", vehicles3, "select");
    }

    selectThis('years_from', start_year, 'select');
    selectThis('years_to', end_year, 'select');
    selectThis('limit_sel', limit, 'select');
    selectThis('ext_color_sel', ext_color, 'select');
    selectThis('mileage_sel', mileage, 'select');
    selectThis('fuel_sel', fuel, 'select');
    selectThis('engine_sel', engine, 'select');
    selectThis('drive_sel', drive, 'select');

    if (features != null) if (features.length > 0) {
        features = features.split(",");
        for (var f = 0; f < form.model_featues.length; f++)
        for (var ff = 0; ff < features.length; ff++)
        if (form.model_featues[f].value.indexOf(features[ff]) != -1) form.model_featues[f].checked = true;
    }

    selectThis(form.transmission, transmission, 'radio');

    if (alerts) alert("Exit showCookie()");
}

function selectThis(str, value, type) {
    var sel;

    if (type.indexOf('select') != -1) sel = document.getElementById(str);
    else sel = str;

    for (var i = 0; i < sel.length; i++)
    if ((sel[i].value).indexOf(value) != -1 && sel[i].value.length == value.length) if (type.indexOf('select') != -1) sel[i].selected = true;
    else sel[i].checked = true;
}

function Delete_Cookie() {
    var cookie_date = new Date(); // current date & time
    cookie_date.setTime(cookie_date.getTime() - 1);
    document.cookie = "name=form_cookie; expires=" + cookie_date.toGMTString();

}

function searchThis(obj) {
    setThisCookie();

    if (typeof obj != "undefined" && obj != null) setOverSrc(obj);
    var t = checkZip();

    if (t) {
        zipCode = document.getElementById("zipcode").value;
        vahicles1 = "Any";
        vehicles1Text = "Any";
        bodyStyleText = "";
        if (typeof document.default_search != "undefined" && typeof document.default_search.vehicle_option != "undefined") {
            vahicles1 = document.default_search.vehicle_option.value;
            vehicles1Text = document.default_search.vehicle_option.options[document.default_search.vehicle_option.selectedIndex].text;
            bodyStyleText = document.default_search.bodystyle.value;
        }
        window.location.href = '/apps/cpo/searchResults?zipCode=' + escape(zipCode) + '&distance=50%20Miles&bodystyle=' + escape(bodyStyleText) + '&vehicles1=' + escape(vahicles1) + '&vehicles2=&vehicles3=&start_year=Any&end_year=Any&min_price=Any&max_price=' + escape("Any") + '&transmission=Any&ext_color=' + escape("Any") + '&mileage=Any&fuel=Any&engine=Any&drive=Any&only_photo=false&vehiclesText=' + escape(vehicles1Text) + '&features=Any&feature0=&feature1=&feature2=&feature3=&feature4=&feature5=&feature6=&feature7=&feature8=&feature9=' + '&next=CPO.PFA_Search';

    }
}

function checkForCookie(obj) {
    if (getCookie('zipCode')) {
        zipCode = getCookie('zipCode');
        vehicles1 = getCookie('vehicles1');
        bodystyle_val = getCookie('bodystyle_val');

        showThisCookie();
    }
}

function showThisCookie() {
    document.getElementById("zipcode").value = zipCode;
    selectThis('vehicle_option', vehicles1, 'select');
}

function setThisCookie() {
    zipCode = document.getElementById("zipcode").value;
    ZipCode = document.getElementById("zipcode").value;

    document.cookie = 'zipCode=' + zipCode + ';';
    document.cookie = 'ZipCode=' + ZipCode + ';';
    if (typeof document.default_search != "undefined" && typeof document.default_search.vehicle_option != "undefined" && document.default_search.vehicle_option != null) document.cookie = 'vehicles1=' + document.default_search.vehicle_option.options[document.default_search.vehicle_option.selectedIndex].text + ';';
    document.cookie = 'bodystyle_val=' + document.default_search.bodystyle.value;
}


function setThistoCookie(attr, value) {
    document.cookie = attr + '=' + value + ';';
}


/*Validating zipcode*/
function validateCPOZipCode() {
    if (alerts) alert("Entered validateCPOZipCode()");

    if (!jQuery('zipcode')) {
        jQuery('zipcode').focus();
        alert('Please enter your Zip Code.');
        return false;
    }

    if (jQuery('zipcode').value.length < 5) {
        jQuery('zipcode').focus();
        alert('Please enter valid Zip Code');
        return false;
    }

    var validZip = /^[0-9]{1,5}$/;
    if (validZip.test(jQuery('zipcode').value)) {
        return true;
    } else {
        jQuery('zipcode').focus();
        alert('Please enter valid Zip Code')
        return false;
    }

    if (alerts) alert("Exit validateCPOZipCode()");
}
window.onunload = Delete_Cookie;

/* popup_cpo.js */
var popupCheck = '';
var events;

jQuery(document).ready(function () {
    jQuery().mouseover(function (e) {
        events = e;
    });
})

function showPopup(obj, popup, position) {
    if (popupCheck.length < 1) {
        popupCheck = popup;
    }
    else {
        hidePopup(popupCheck);
        popupCheck = popup;
    }
    var x = events.pageX;
    var y = events.pageY;
    var element = document.getElementById(popup);
    element.style.display = "block";
    var screenX = jQuery(".gBody").width(); //screen.width;
    var screenY = screen.height;
    var bHeight = jQuery(".gBody").height();

    switch (position) {
    case "above":
        y -= (document.getElementById(popup).scrollHeight);
        break;
    case "below":
        y += 10;
        break;
    case "leftside":
        x -= document.getElementById(obj).scrollWidth;
        break;
    case "rightside":
        x += document.getElementById(obj).scrollWidth;
        break;
    case "above1":
        y = (document.getElementById(popup).scrollHeight) - 150;
        x = (document.getElementById(popup).scrollLeft) + 250;
        break;
    case "cpodisclaimer1":
        y = (document.getElementById(popup).scrollHeight) - 110;
        x -= 420;
        break;
    case "cpodisclaimer2":
        y = (document.getElementById(popup).scrollHeight) - 110;
        x -= 320;
        break;
    case "cpodisclaimer3":
        y = (document.getElementById(popup).scrollHeight) - 110;
        x -= 250;
        break;
    case "cpodisclaimer4":
        y = (document.getElementById(popup).scrollHeight) + 220;
        x += 250;
        break;
    }

    var eHeight = document.getElementById(popup).scrollHeight;
    var eWidth = document.getElementById(popup).scrollWidth;

    if (x < x / 4) x = x / 4;
    if (x + eWidth + 20 > screenX) x = screenX - eWidth - 50;

    if (bHeight < eval(y + document.getElementById(popup).scrollHeight)) {
        y = bHeight - document.getElementById(popup).scrollHeight - 50;
    }

    if (x < 0) x = 10;
    if (y < 0) y = 10;

    element.style.top = y + "px";
    element.style.left = x + "px";
}

function hidePopup(id) { 
    document.getElementById(id).style.display = "none";
    return;
}

/* cpo-page-functions.js */
function sendFormData() {
    var vehicleName = document.getElementById('vehicle_option').options[document.getElementById('vehicle_option').selectedIndex].text;
    if (vehicleName == 'Any Vehicle') {
        vehicleName = "Any";
    }

    document.getElementById('vehicle').value = vehicleName;
    getBodyStyle();
    if (document.getElementById("zipcode").value.length == 5) {
        setThisCookie();
    }
    document.default_search.submit();
}

//To set the bodyStyle
function getBodyStyle() {
    var selIndex = "";
    var bodystyle = "";

    /* The original list looks like it was dynamic; 
     * there doesn't seem to be a reason for it to be.
     * Converted to static until proven otherwise. */
    var vehicleBodyList = new Array(
        new Array("Altima Coupe","coupe"),
        new Array("Altima Sedan","sedan"),
        new Array("Armada","suv"),
        new Array("Cube","wagon"),
        new Array("Frontier","trucks"),
        new Array("GT-R","coupe"),
        new Array("Maxima","sedan"),
        new Array("Murano","suv"),
        new Array("Pathfinder","suv"),
        new Array("Quest","vans"),
        new Array("Rogue","suv"),
        new Array("Sentra","sedan"),
        new Array("Titan","trucks"),
        new Array("Versa Hatchback","hatch"),
        new Array("Versa Sedan","sedan"),
        new Array("Xterra","suv"),
        new Array("350Z Coupe","coupe"),
        new Array("370Z Coupe","coupe"),
        new Array("350Z Roadster","convert"),
        new Array("370Z Roadster","convert")
    );

    var vehicleName = document.getElementById('vehicle_option').options[document.getElementById('vehicle_option').selectedIndex].text;

    if (vehicleName == 'Any Vehicle' || vehicleName == 'Todos los vehiculos') {
        vehicleName = "Any";
        bodystyle = "Any";
    } else {
        for (i = 0; i < vehicleBodyList.length; i++) {
            if (vehicleBodyList[i][0] == vehicleName) {
                selIndex = i;
            }
        }
        bodystyle = vehicleBodyList[selIndex][1];
    }
    document.getElementById('bodystyle').value = bodystyle;
    setBodyStyle(bodystyle);
}

//This function is used to get list of distinct vehicles from an array containing duplicate entries.


function getDistinctVehicles(vehicles) {
    var vehicleList = new Array("all");
    var index = 0;
    for (var a = 0; a < vehicles.length; a++) {
        var flag = false;
        for (var b = 0; b < vehicleList.length; b++) {
            if (vehicles[a] == vehicleList[b]) {
                flag = true;
            }
        }
        if (!flag) {
            vehicleList[index++] = vehicles[a];
        }
    }
    return vehicleList;
}


// This function is used to get a list of years for each item.The item can be a vehicle or model.
//The resultant array is a 2D array in the format [[itemName,[list_of_years]]]


function getDistinctItems(ItemList, yearList) {
    var modelList = new Array();
    var tempModels = new Array();
    for (var a = 0; a < ItemList.length; a++) {
        temp = new Array();
        var count = 0;
        for (var b = 0; b < yearList.length; b++) {
            if (ItemList[a] == yearList[b][0]) {
                flag = false;
                for (var c = 0; c < temp.length; c++) {
                    if (yearList[b][1] == temp[c]) flag = true;
                }
                if (!flag) temp[count++] = yearList[b][1];
            }
        }
        tempModels = [ItemList[a], temp];
        modelList[a] = tempModels;

    }

    return modelList;
}

function populateVehicles() {
    var vehicles_txt = '';
    var vehicleCodeList = new Array(
        new Array("350Z Coupe","350Z",""),
        new Array("350Z Coupe","350Z",""),
        new Array("370Z Coupe","370Z",""),
        new Array("370Z Coupe","370Z",""),
        new Array("350Z Coupe","350Z",""),
        new Array("350Z Coupe","350Z",""),
        new Array("350Z Coupe","350Z",""),
        new Array("350Z Roadster","350Z",""),
        new Array("350Z Roadster","350Z",""),
        new Array("350Z Roadster","350Z",""),
        new Array("350Z Roadster","350Z",""),
        new Array("370Z Roadster","370Z",""),
        new Array("Altima Coupe","ALTIMA",""),
        new Array("Altima Coupe","ALTIMA",""),
        new Array("Altima Coupe","ALTIMA",""),
        new Array("Altima Sedan","ALTIMA",""),
        new Array("Armada","PATHARMADA",""),
        new Array("Armada","PATHARMADA",""),
        new Array("Altima Sedan","ALTIMA",""),
        new Array("Altima Sedan","ALTIMA",""),
        new Array("Altima Sedan","ALTIMA",""),
        new Array("Altima Sedan","ALTIMA",""),
        new Array("Altima Sedan","ALTIMA",""),
        new Array("Altima Sedan","ALTIMA",""),
        new Array("Armada","PATHARMADA",""),
        new Array("Armada","PATHARMADA",""),
        new Array("Armada","PATHARMADA",""),
        new Array("Armada","PATHARMADA",""),
        new Array("Armada","PATHARMADA",""),
        new Array("Cube","CUBE",""),
        new Array("Cube","CUBE",""),
        new Array("Frontier","FRONTI",""),
        new Array("Frontier","FRONTI",""),
        new Array("Frontier","FRONTI",""),
        new Array("Frontier","FRONTI",""),
        new Array("Frontier","FRONTI",""),
        new Array("Frontier","FRONTI",""),
        new Array("Frontier","FRONTI",""),
        new Array("GT-R","GTR",""),
        new Array("GT-R","GT_R",""),
        new Array("Maxima","MAX",""),
        new Array("Maxima","MAX",""),
        new Array("Maxima","MAX",""),
        new Array("Maxima","MAX",""),
        new Array("Maxima","MAX",""),
        new Array("Maxima","MAX",""),
        new Array("Maxima","MAX",""),
        new Array("Murano","MURANO",""),
        new Array("Murano","MURANO",""),
        new Array("Murano","MURANO",""),
        new Array("Murano","MURANO",""),
        new Array("Murano","MURANO",""),
        new Array("Pathfinder","PATH",""),
        new Array("Pathfinder","PATH",""),
        new Array("Pathfinder","PATH",""),
        new Array("Pathfinder","PATH",""),
        new Array("Pathfinder","PATH",""),
        new Array("Murano","MURANO",""),
        new Array("Murano","MURANO",""),
        new Array("Quest","QUEST",""),
        new Array("Quest","QUEST",""),
        new Array("Quest","QUEST",""),
        new Array("Quest","QUEST",""),
        new Array("Quest","QUEST",""),
        new Array("Pathfinder","PTH",""),
        new Array("Pathfinder","PTH",""),
        new Array("Rogue","ROGUE",""),
        new Array("Quest","QST",""),
        new Array("Rogue","ROGUE",""),
        new Array("Sentra","SENTRA",""),
        new Array("Sentra","SENTRA",""),
        new Array("Sentra","SENTRA",""),
        new Array("Sentra","SENTRA",""),
        new Array("Sentra","SENTRA",""),
        new Array("Rogue","ROGUE",""),
        new Array("Sentra","SENTRA",""),
        new Array("Sentra","SENTRA",""),
        new Array("Titan","TITAN",""),
        new Array("Titan","TITAN",""),
        new Array("Titan","TITAN",""),
        new Array("Titan","TITAN",""),
        new Array("Titan","TITAN",""),
        new Array("Titan","TITAN",""),
        new Array("Titan","TITAN",""),
        new Array("Versa Hatchback","VERSA",""),
        new Array("Versa Hatchback","VERSA",""),
        new Array("Versa Hatchback","VERSA",""),
        new Array("Versa Sedan","VERSA",""),
        new Array("Versa Hatchback","VERSA",""),
        new Array("Versa Sedan","VERSA",""),
        new Array("Versa Sedan","VERSA",""),
        new Array("Versa Sedan","VERSA",""),
        new Array("Xterra","XTERRA",""),
        new Array("Xterra","XTERRA",""),
        new Array("Xterra","XTERRA",""),
        new Array("Xterra","XTERRA",""),
        new Array("Xterra","XTERRA",""),
        new Array("Xterra","XTERRA",""),
        new Array("Xterra","XTERRA","")
    );

    var vehicles = new Array(
        "350Z Coupe",
        "350Z Coupe",
        "370Z Coupe",
        "370Z Coupe",
        "350Z Coupe",
        "350Z Coupe",
        "350Z Coupe",
        "350Z Roadster",
        "350Z Roadster",
        "350Z Roadster",
        "350Z Roadster",
        "370Z Roadster",
        "Altima Coupe",
        "Altima Coupe",
        "Altima Coupe",
        "Altima Sedan",
        "Armada",
        "Armada",
        "Altima Sedan",
        "Altima Sedan",
        "Altima Sedan",
        "Altima Sedan",
        "Altima Sedan",
        "Altima Sedan",
        "Armada",
        "Armada",
        "Armada",
        "Armada",
        "Armada",
        "Cube",
        "Cube",
        "Frontier",
        "Frontier",
        "Frontier",
        "Frontier",
        "Frontier",
        "Frontier",
        "Frontier",
        "GT-R",
        "GT-R",
        "Maxima",
        "Maxima",
        "Maxima",
        "Maxima",
        "Maxima",
        "Maxima",
        "Maxima",
        "Murano",
        "Murano",
        "Murano",
        "Murano",
        "Murano",
        "Pathfinder",
        "Pathfinder",
        "Pathfinder",
        "Pathfinder",
        "Pathfinder",
        "Murano",
        "Murano",
        "Quest",
        "Quest",
        "Quest",
        "Quest",
        "Quest",
        "Pathfinder",
        "Pathfinder",
        "Rogue",
        "Quest",
        "Rogue",
        "Sentra",
        "Sentra",
        "Sentra",
        "Sentra",
        "Sentra",
        "Rogue",
        "Sentra",
        "Sentra",
        "Titan",
        "Titan",
        "Titan",
        "Titan",
        "Titan",
        "Titan",
        "Titan",
        "Versa Hatchback",
        "Versa Hatchback",
        "Versa Hatchback",
        "Versa Sedan",
        "Versa Hatchback",
        "Versa Sedan",
        "Versa Sedan",
        "Versa Sedan",
        "Xterra",
        "Xterra",
        "Xterra",
        "Xterra",
        "Xterra",
        "Xterra",
        "Xterra"
    );

    var vehicleList = new Array();
    var vehicle_cd = new Array();
    vehicleList = getDistinctVehicles(vehicles);
    vehicle_cd = getDistinctItems(vehicleList, vehicleCodeList);
    for (var a = 0; a < vehicle_cd.length; a++) {
        vehicles_txt += '<option id="' + vehicle_cd[a][0] + '" value="' + vehicle_cd[a][1] + '">' + vehicle_cd[a][0] + '</option>';
    }
    document.getElementById('vehicleSpan').innerHTML = '<select id=vehicle_option name=vehicle_option><option id="Any" value="Any">Any Vehicle</option>' + vehicles_txt + '</select>';
}

var mIndex=0;
function setOptionValue()
{   
    var orgValue = document.getElementById('vehicle_option').value;
    var i = orgValue.split("_");
    document.getElementById('vehicle_option').options[document.getElementById('vehicle_option').selectedIndex].value = i[0];
}

function saveIndex()
{
    mIndex = document.getElementById('vehicle_option').selectedIndex;
}

function rollbackValue()
{
    document.getElementById('vehicle_option').options[mIndex].value = document.getElementById('vehicle_option').options[mIndex].value + '_' + mIndex;    
}
jQuery(function(){
    jQuery('#vehicle_option').change(function(){
        rollbackValue();
        setOptionValue();
        saveIndex();
    })
});


