﻿
var Domin = "http://cache.ucar.cn/";
/*隔行换色*/
var ChangeColor = function(delegateId, tagetNodeName, className) {
    var self = this;
    if (!(self instanceof ChangeColor)) {
        self = new ChangeColor(delegateId, tagetNodeName, className);
    }
    self.getItem(delegateId);
    self.bindEvents(tagetNodeName, className);
}
ChangeColor.prototype = {
    getItem: function(id) {
        this.container = document.getElementById(id);
    },
    bindEvents: function(tagetNodeName, className) {
        this.container.onmouseover = function(e) {
            var e = e || window.event, target = e.target || e.srcElement;
            do {

                if (target.nodeName && target.nodeName.toLowerCase() === tagetNodeName) {
                    break;
                }
            }
            while (target = target.parentNode);
            if (target === null)
                return;
            if (target.className.length > 0) {
                target.className += ' ' + className;
            }
            else {
                target.className = className;
            }
        }
        this.container.onmouseout = function(e) {
            var e = e || window.event, target = e.target || e.srcElement;
            do {
                if (target.nodeName && target.nodeName.toLowerCase() === tagetNodeName) {
                    break;
                }
            }
            while (target = target.parentNode);
            if (target === null)
                return;
            if (target.className.indexOf(className) > -1) {
                target.className = target.className.replace(className, '');
            }
        }
    }
}

var getTime = function() {
    return Date.now ? Date.now() : +new Date();
}
/*
* 简单动画函数
*/
var anim = function(ele, prop, targetPos, duration, fn) {
    var distance, startTime, looping;
    if (typeof targetPos !== undefined && (targetPos instanceof Array) && targetPos.length >= 2) {
        var from = parseInt(targetPos[0], 10), to = parseInt(targetPos[1], 10);
        distance = to - from;
        currProp = from;
        startTime = getTime();
        var timer = setInterval(loop, 10);
    }
    if (typeof targetPos === 'number') {
        var currProp = parseInt(getStyle(ele, prop), 10);
        distance = targetPos - currProp;
        startTime = getTime();
        var timer = setInterval(loop, 0);
    }
    function loop() {
        var lostTime = getTime() - startTime;
        currValue = lostTime / duration * distance;
        ele.style[prop] = Math.abs(currProp + currValue) + 'px';
        if (lostTime >= duration) {
            clearInterval(timer);
            ele.style[prop] = currProp + distance + 'px';
            fn && fn();
        }
    }
    return arguments.callee;
}
var hisBrandid = parseInt($("hdbrandid").value);  //品牌Id
var hisSerialId = parseInt($("hdserialid").value); //车系Id
var hisBasicid = parseInt($("hdcarbasicid").value); //车型Id
var ProCity = new Ucar_City_Select("selPro", "selCity");
ProCity.defultProvinceText = "全国";
ProCity.defultProvinceValue = "-1";
ProCity.defultProvinceId = "quanguo";
ProCity.isUseCityInit = true;
ProCity.isUseDisable = true;
ProCity.cityHiddenName = "hdctid";
ProCity.provinceHiddenName = "hdpvid";
ProCity.initProvinceSelect();
if (!isNaN(hisBrandid) && hisBrandid > 0) //初始化车系
{
    OnitSerialByBrand(hisBrandid);
}
else {
    $("li_chexi").className = "xilie disable";
}
if (!isNaN(hisSerialId) && hisSerialId > 0)//初始化车型
{
    OnitCarBasicByCsId(hisSerialId);
}
else {
    $("li_chexing").className = "chexing disable";
}
//隔行换色
if ($("buycar_list1")) {
    ChangeColor('buycar_list1', 'tr', 'hover_color');
}
if ($("buycar_list2")) {
    ChangeColor('buycar_list2', 'tr', 'hover_color');
}
if (parseInt($("hdpvid").value) > 0) {
    $("selPro").className = "provinces selected";
}
if (parseInt($("hdctid").value) > 0) {
    $("selCity").className = "cities selected";
}
//排序初始化
$("selSort").value = $("hdsort").value + "," + $("hddirection").value;
//列表与大图切换初始化
if ($("hdlisttype").value == 1)//大图
{
    $("btnList").className = "btn_listb";
    $("btnPic").className = "btn_bpic";
}
else {
    $("btnList").className = "btn_list";
    $("btnPic").className = "btn_bpicb";
}
if ($("hdishaspic").value == 1)//是否有图
{
    $("btnIsHasPic").className = "btn_imgCar_cur"
}
else {
    $("btnIsHasPic").className = "btn_imgCar"
}
//按城市(obj省份/城市选择下拉对象) 查询(type:1,按城市，2按省份)
var _serialid = $F('hdserialid');
var _mainbrandid = $F('hdbrandid');
var _brandid = $F('hdbdid');
var _cityid = $F('hdctid');
var _prvid = $F('hdpvid');
var _ageid = $F('hdageid');
var _priceid = $F('hdpriceid');
var _exhaust = $F('hdExhaust');
var _carlevl = $F('hdCarLevl');
var _sourceid = $F('hdsourcetype');
var _serialspell = $F('hdSerialSpell');
var _brandspell = $F('hdBrandSpell');
var _mbrandspell = $F('hdMbrandSpell');
var _areaname = $F('hdareaname');
var _lowprice = $F('hdLowPrice');
var _highprice = $F('hdHighPrice');
function AreaQuery(obj, type) {
    var url = window.location.href.toLowerCase();
    var key;
    var keyvalue;
    if (type == 1) {
        _cityid = obj.options[obj.selectedIndex].value;
        key = "ctid";
        keyvalue = _cityid;
        if (_cityid ==0) {
            key = "pvid";
            keyvalue = _prvid;
        }
    }
    else {
        _prvid = obj.options[obj.selectedIndex].value;
        key = "pvid";
        keyvalue = _prvid;
    }
    url = RemoveURLParam(url, "page");
    var newURL = ProceCityUrl(url, obj, key, keyvalue);
    newURL = GetCarListRetUrl(newURL);
    newURL = RemoveURLParam(newURL, "notbh");
    window.location.href = newURL;
}

var ProceCityUrl = function(url, obj, keys, keyvalue) {
    var cityname = obj.options[obj.selectedIndex].id;
    var regp = new RegExp("ca/[\\d*]/");
    if (regp.match(url)) {
        return url.replace(regp, cityname + "/pa" + _ageid + "esc/");
    }
    var rega = new RegExp("price/[\\d]+-[\\d]+/");
    if (rega.match(url)) {
        url = url.replace(rega, cityname + "/p" + _priceid + "aesc/");
        if (_priceid == 0 && _highprice > 0) {
            if (url.indexOf("?")>-1) {
                return url + "&lp=" + _lowprice + "&hp=" + _highprice;
            }
            return url + "?lp=" + _lowprice + "&hp=" + _highprice;
        }
        return url;
    }
    var regc = new RegExp("cc/[\\d*]/");
    if (regc.match(url)) {
        return url.replace(regc, cityname + "/paesc" + _carlevl + "/");
    }
    var rege = new RegExp("exhaust/[\\d*]");
    if (rege.match(url)) {
        return url.replace(rege, cityname + "/pae" + _exhaust + "sc/");
    }
    if (url.indexOf('geren') > -1) {
        return url.replace("geren", cityname + "/paes1c");
    }
    if (url.indexOf("shangjia") > -1) {
        return url.replace("shangjia", cityname + "/paes2c");
    }
    if (url.indexOf("renzheng") > -1) {
        return url.replace("renzheng", cityname + "/paes3c");
    }
    if (url.match("p(\\d*)a(\\d*)e(\\d*)") == null) {
        var reg;
        if (url.indexOf("carlist.aspx") > -1) {
            //需要修改
            reg = new RegExp("/buycar/([\\w\-]+[\?]?.*)");
        }
        else {
            //只替换参前面的内容，保留？后的附属参数
            reg = new RegExp("/buycar/([\\w\-].*)/");
        }
        if (_serialid < 1 && _mainbrandid < 1) {
            if (reg.match(url)) {
                url = url.replace(url.match(reg)[1], cityname + "/");
            }
        }
        else if (_serialid > 0) {
            url = url.replace(reg, "/buycar/" + cityname + "-" + _serialspell + "/");
        }
        else if (_brandid > 0) {
            url = url.replace(reg, "/buycar/" + cityname + "-" + _brandspell + "/");
        }
        else if (_mainbrandid > 0) {
            url = url.replace(reg, "/buycar/" + cityname + "-" + _mbrandspell + "/");
        }
    }
    else {
        var urlp;
        var key;
        if (keys == "ctid") {
            key = "f";
        }
        if (keys == "pvid") {
            key = "t";
        }
        if (_serialid < 1 && _mainbrandid < 1 && _brandid < 1) {
            var reg = new RegExp(".*/buycar/([\\w\-]+)/([\\w\-]*)/");
            if (reg.match(url) && url.match(reg).length > 1) {
                urlp = url.match(reg)[1];
                url = url.replace(url.match(reg)[1], cityname);
            }
        }
        else {
            var reg1 = new RegExp(".*/buycar/([\\w\-]+)/([\\w\-]*)/([\\w\-]*)/");
            if (reg1.match(url) && url.match(reg1).length > 3) {
                urlp = url.match(reg1)[3];
                if (key == "f") {
                    //省份替换城市
                    urlp = urlp.replace("t", "f");
                }
                if (key == "t") {
                    //城市替换省份
                    urlp = urlp.replace("f", "t");
                }
                url = url.replace(url.match(reg1)[3], "@@");
                var rd = new RegExp(key + "-?\\d*");
                urlp = urlp.replace(rd, key + keyvalue);
                url = url.replace("@@", urlp);
            }
        }

    }
    return url;
};
var GetCarListRetUrl = function(url) {
    var arr = new Array("pvid", "ctid", "areaname", "carlevelid", "bid", "pid", "sid", "spell", "sourceid", "ageid", "priceid", "exhaust");
    var key;
    var n = url.indexOf("?");
    var m = 0;
    if (n > -1)//如果有参数且有这个key参数 对现有参数进行替换
    {
        var query;
        query = url.substring(n + 1);
        var querys = query.split('&');
        //将链接包含数组的参数替换为空。
        for (var j = 0; j < querys.length; j++) {
            m = querys[j].indexOf("=");
            key = querys[j].substring(0, m);
            for (var i = 0; i < arr.length; i++) {
                if (arr[i].indexOf(key) > -1) {
                    if (_serialid > 0 || _mainbrandid > 0 || _brandid > 0) {
                        url = this.Parakey(key, url)
                    }
                    url = url.replace(querys[j], "");
                }
            }
        }
    }
    var reg = new RegExp("\\?&+");
    url = url.replace(reg, "?");
    var regs = new RegExp("\\?@|\\?&+@|&+@|&$|\\?$");
    url = url.replace(regs, "");
    url = url.replace(/@/g, "");
    var regl = new RegExp("//$");
    url = url.replace(regl, "/");
    return url;
};
/// 参数代表的重写字符标识
var Parakey = function(chars, url) {
    var _parakey;
    switch (chars) {
        //价格                                                                                                
        case "priceid":
            _parakey = "p" + _priceid;
            url = url.replace("p@", _parakey);
            break;
        //车龄                                                                                                
        case "ageid":
            _parakey = "a" + _ageid;
            url = url.replace("a@", _parakey);
            break;
        //排气量                                                                                                
        case "exhaust":
            _parakey = "e" + _exhaust;
            url = url.replace("e@", _parakey);
            break;
        //车辆类型（大，中，小，mpv，suv，皮卡等）                                                                                               
        case "carlevelid":
            _parakey = "c" + _carlevl;
            url = url.replace("c@", _parakey);
            break;
        //车源类型（个人，商家，认证）                                                                                                
        case "sourceid":
            _parakey = "s" + _sourceid;
            url = url.replace("s@", _parakey);
            break;
        //城市                                                                                                
        case "ctid":
            _parakey = "f" + _cityid;
            url = url.replace("f@", _parakey);
            break;
        //省份                                                                                                
        case "pvid":
            _parakey = "t" + _prvid;
            url = url.replace("t@", _parakey);
            break;
        default:
            break;
    }
    return url;
};


//按车源来源类型
function carSourceQuery(type) {
    if (isNaN(type)) {
        return;
    }
    var hisType = $("hdsourcetype").value;
    if (type == hisType) {
        return;
    }
    var url = window.location.href;
    var newURL = SetURLParam(url, "sourceid", type);
    newURL = RemoveURLParam(newURL, "spid");
    newURL = RemoveURLParam(newURL, "tvaid");
    newURL = RemoveURLParam(newURL, "notbh");
    newURL = SetURLParam(newURL, "page", "1");
    window.location.href = newURL;
}
//变速箱
function GearTypeQuery(type) {
    if (isNaN(type)) {
        return;
    }
    var hisType = $("hdcargeartype").value;
    if (type == hisType) {
        return;
    }
    var url = window.location.href;
    var newURL = SetURLParam(url, "boxid", type);
    newURL = RemoveURLParam(newURL, "notbh");
    newURL = SetURLParam(newURL, "page", "1");
    window.location.href = newURL;
}
//颜色选择查询
function CarColorQuery(colorsid) {
    var hiscolorsid = $("hdColorsid").value;
    if (hiscolorsid == colorsid)
        return;
    var url = window.location.href;
    var newURL = SetURLParam(url, "corlorsid", colorsid);
    newURL = RemoveURLParam(newURL, "notbh");
    newURL = SetURLParam(newURL, "page", "1");
    window.location.href = newURL;
}
function MiliQuery(mili, miliDesc) {
    var hisMiliId = $("hdMili").value;
    if (hisMiliId == mili) {
        return;
    }
    $("vmt").innerHTML = miliDesc;
    $("vmtList").style.display = 'none';
    var url = window.location.href;
    var newURL = SetURLParam(url, "miliid", mili);
    newURL = RemoveURLParam(newURL, "notbh");
    newURL = SetURLParam(newURL, "page", "1");
    window.location.href = newURL;
}
//显示方式，列表和大图
function TransListType(type) {
    if (isNaN(type)) {
        return;
    }
    var hisType = $("hdlisttype").value;
    if (type == hisType) {
        return;
    }
    var url = window.location.href;
    var newURL = SetURLParam(url, "listtype", type);
    window.location.href = newURL;
}
//有图与无图切换
function HasPicQuery(type) {
    if (isNaN(type)) {
        return;
    }
    var hisType = $("hdishaspic").value;
    if (type == hisType) {
        return;
    }
    var url = window.location.href;
    var newURL = SetURLParam(url, "ispic", type);
    newURL = SetURLParam(newURL, "page", "1");
    newURL = RemoveURLParam(newURL, "notbh");
    window.location.href = newURL;
}
//有无图切换
function transPic() {
    var hisType = $("hdishaspic").value;
    var type = (hisType == 0 ? 1 : 0);
    var url = window.location.href;
    var newURL = SetURLParam(url, "ispic", type);
    newURL = RemoveURLParam(newURL, "notbh");
    newURL = SetURLParam(newURL, "page", "1");
    window.location.href = newURL;
}
//分页方式变化
function PageTrans(type) {
    if (isNaN(type)) {
        return;
    }
    var hisType = $("hdpagesize").value;
    if (type == hisType) {
        return;
    }
    var url = window.location.href;
    var newURL = SetURLParam(url, "pagecounttype", type);
    newURL = SetURLParam(newURL, "page", "1");
    window.location.href = newURL;
}
//下一页
function NextPage() {
    var pageIndex = $("hdPageIndex").value;
    var pageCount = $("hdPageCount").value;
    if (isNaN(pageIndex) || pageIndex == pageCount) {
        return
    }
    pageIndex = parseInt(pageIndex) + 1;
    var url = window.location.href;
    var newURL = SetURLParam(url, "page", pageIndex);
    window.location.href = newURL;
}
//前一页
function PrePage() {
    var pageIndex = $("hdPageIndex").value;
    if (isNaN(pageIndex) || pageIndex == 1) {
        return
    }
    pageIndex = parseInt(pageIndex - 1);
    var url = window.location.href;
    var newURL = SetURLParam(url, "page", pageIndex);
    window.location.href = newURL;
}
//排序
function SortChange(obj) {
    var temp = obj.value.split(',');
    Sort(temp[0], temp[1]);
}
//排序
function Sort(orderid, direction) {
    var url = window.location.href;
    var newURL = SetURLParam(url, "orderid", orderid);
    newURL = SetURLParam(newURL, "direction", direction);
    newURL = SetURLParam(newURL, "page", "1");
    window.location.href = newURL;
}
//打开品牌选择框
function open_brand() {
    DynamicOpen_Div($('pinpai_content'), $('dpa_pinpai'), $('pinpai_cnt'), 614, 355);
}
function close_brand() {
    $('pinpai_content').style.display = 'none';
}
//动态打开品牌，车系，车型选择框
function DynamicOpen_Div(contentDivObj, dpaDivObj, cntDivObj, divWidth, divHeight) {
    contentDivObj.style.display = 'block';
}
//动态关闭品脾，车系，车型选择框
function DynamicClose_Div(dpaDivObj, cntDivObj, divWidth, divHeight) {
    anim(cntDivObj, 'width', [divWidth, 0], 400, function() {
        cntDivObj.style.display = 'none';
        dpaDivObj.style.display = 'none';
    });
    anim(cntDivObj, 'height', [divHeight, 0], 400);
}
//品牌选择
function selectBrand(brandid, brandName, brandspell) {

    if (hisBrandid == brandid) {
        close_brand();
        return;
    }
    $("li_pinpai").innerHTML = brandName;
    close_brand();
    var url = window.location.href.toLowerCase();
    url = RemoveURLParam(url, "page");
    var newURL = ProceSerialUrl(url, "brand/" + brandspell, 1);
    newURL = GetCarListRetUrl(newURL);
    newURL = RemoveURLParam(newURL, "carid");
    newURL = RemoveURLParam(newURL, "notbh");
    window.location.href = newURL;
}
//根据主品牌初始化系列
function OnitSerialByBrand(brandid) {
    var url = "/ajax/carinfojs.ashx";
    var pars = "rand=" + Math.random() + "&carbrandid=" + brandid + "&type=1";
    var myAjax = new Ajax.Request(
                        url,
                        { method: 'get', parameters: pars, onComplete: OnitSerial }
                        );
}
//根据Ajax请求的系列填充系列
function OnitSerial(request) {
    var text = request.responseText;
    eval(text);
    $("xilie_cnt").innerHTML = ""; //系列列表Div
    $("chexing_cnt").innerHTML = "";
    var html = "<iframe class=\"shim\"></iframe>";
    var SeraialList = "";
    if (ucar_carserial != null && ucar_carserial.length > 0) {
        var LastGroupName = "";
        var indexnum;
        for (var i = 0; i < ucar_carserial.length; i++) {
            if (ucar_carserial[i].GroupName != LastGroupName) {
                if (LastGroupName != "") {
                    SeraialList += "</dl>";
                }
                LastGroupName = ucar_carserial[i].GroupName;

                SeraialList += "<dl class=\"brand_list1\"><dt>" + LastGroupName + "</dt>"
            }
            indexnum = ucar_carserial[i].Text.indexOf("|");
            if (indexnum > -1) {
                SeraialList += "<dd><a id=\"" + ucar_carserial[i].Text.substring(indexnum + 1) + "\" href=\"javascript:selectSerial(" + ucar_carserial[i].Value + ",'" + ucar_carserial[i].Text.substring(0, indexnum) + "','" + ucar_carserial[i].Text.substring(indexnum + 1) + "')\">" + ucar_carserial[i].Text.substring(0, indexnum) + "</a></dd>";
            }
            else {
                SeraialList += "<dd><a href=\"javascript:selectSerial(" + ucar_carserial[i].Value + ",'" + ucar_carserial[i].Text + "','')\">" + ucar_carserial[i].Text + "</a></dd>";
            }
        }
        SeraialList += "</dl>";
    }
    $("xilie_cnt").innerHTML = html + SeraialList;
}
//打开车系选择器
function open_chexi() {
    if (hisBrandid <= 0) {
        return;
    }
    DynamicOpen_Div($('chexi_content'), $("dpa_chexi"), $("xilie_cnt"), 200, 360);
}
//关闭车系选择下拉框
function close_chexi() {
    $('chexi_content').style.display = 'none';
}
//处理品牌，车系选择时链接地址
var ProceSerialUrl = function(url, aidvalue, type) {
    var paramu = "";
    var keys;
    if (_cityid > 0) {
        paramu = "/p@a@e@s@f" + _cityid + "/";
    }
    else if (_prvid >= -1) {
        paramu = "/p@a@e@s@t" + _prvid + "/";
    }
    _serialspell = aidvalue;
    var regp = new RegExp("ca/[\\d*]/");
    if (regp.match(url)) {
        return url.replace(regp, _serialspell + "/pa" + _ageid + "est0/");
    }
    var rega = new RegExp("price/[\\d]+-[\\d]+/");
    if (rega.match(url)) {

        url = url.replace(rega, _serialspell + "/p" + _priceid + "aest0/");
        if (_priceid == 0 && _highprice > 0) {
            if (url.indexOf("?")>-1) {
                return url + "&lp=" + _lowprice + "&hp=" + _highprice;
            }
            return url + "?lp=" + _lowprice + "&hp=" + _highprice;
        }
        return url;
    }
    var rege = new RegExp("exhaust/[\\d*]");
    if (rege.match(url)) {
        return url.replace(rege, _serialspell + "/pae" + _exhaust + "st0/");
    }
    if (url.indexOf('geren') > -1) {
        return url.replace("geren", _serialspell + "/paes1t0/");
    }
    if (url.indexOf("shangjia") > -1) {
        return url.replace("shangjia", _serialspell + "/paes2t0/");
    }
    if (url.indexOf("renzheng") > -1) {
        return url.replace("renzheng", _serialspell + "/paes3t0/");
    }
    if (url.match("p(\\d*)a(\\d*)e(\\d*)") == null) {
        var reg;
        if (url.indexOf("carlist.aspx") > -1) {
            //需要修改
            reg = new RegExp("/buycar/([\\w\-]+[\?]?.*)");
        }
        else {
            //只替换参前面的内容，保留？后的附属参数
            reg = new RegExp("/buycar/([\\w\-].*)/");
        };
        //只有地区和车系/品牌
        if (_areaname != "") {
            if (type == 1) {
                url = url.replace(reg, "/buycar/" + _areaname + "-" + _serialspell.substring(6) + "/");
            }
            else if (type == 0) {
                url = url.replace(reg, "/buycar/" + _areaname + "-" + _serialspell.substring(7) + "/");
            }
        }
        else {
            url = url.replace(reg, "/buycar/" + _serialspell + paramu);
        }
    }
    else {
        var reg = new RegExp(".*/buycar/([\\w\-]+)/([\\w\-]*)/");
        var urlp;
        var u1;
        if (reg.match(url) && url.match(reg).length > 2) {
            u1 = url.match(reg)[1];
            var u2 = url.match(reg)[2];
            if (url.indexOf('brand') < 0 && url.indexOf('serial') < 0) {
                url = url.replace(u1, _serialspell);
                var reg11 = new RegExp("c\\d*/");
                if (_cityid > 0) {
                    url = url.replace(reg11, "f@/");
                    keys = "ctid";
                }
                else if (_prvid >= -1) {
                    url = url.replace(reg11, "t@/");
                    keys = "pvid";
                }
                url = this.Parakey(keys, url)
            }
            else {
                url = url.replace(u2, "@@");
                if (_cityid > 0) {
                    url = url.replace("t0", "f" + _cityid);
                }
                else if (_prvid > 0) {
                    url = url.replace("t0", "t" + _prvid);
                }
            }
        }
        else {
            var reg1 = new RegExp(".*/buycar/([\\w\-]+)/([\\w\-]*)/([\\w\-]*)/");
            if (reg1.match(url) && url.match(reg1).length > 3) {
                url = url.replace(url.match(reg1)[3], "@@");
            }
        }
        if (type == 1) {
            url = url.replace("@@", _serialspell.substring(6));
        }
        if (type == 0) {
            url = url.replace("@@", _serialspell.substring(7));
        }
        if (type == 1) {
            url = url.replace("serial", "brand");
        }
        if (type == 0) {
            url = url.replace("brand", "serial");
        }
    }
    return url;
};
//选择车系，异步刷新车型列表
function selectSerial(serialId, SerialName, serialspell) {
    if (hisSerialId == serialId) {
        close_chexi();
        return;
    }
    $("li_chexi").innerHTML = SerialName;
    close_chexi();
    var url = window.location.href.toLowerCase();
    url = RemoveURLParam(url, "page");
    if (serialspell != "") {
        var newURL = ProceSerialUrl(url, "serial/" + serialspell, 0);
        newURL = GetCarListRetUrl(newURL);
    }
    newURL = RemoveURLParam(newURL, "carid");
    newURL = RemoveURLParam(newURL, "notbh");
    window.location.href = newURL;
}
//根据车系ID初始换车型
function OnitCarBasicByCsId(serialId) {
    var url = "/ajax/carinfojs.ashx"
    var pars = "rand=" + Math.random() + "&carserialid=" + serialId;
    var myAjax = new Ajax.Request(
                        url,
                        { method: 'get', parameters: pars, onComplete: OnitCarBasic }
                        );
}
//初始化车型
function OnitCarBasic(request) {
    var text = request.responseText;
    eval(text);
    $("chexing_cnt").innerHTML = ""; //车型列表Div
    var html = "<iframe class=\"shim\"></iframe>";
    var BasicList = "";
    if (ucar_carbasicinfo != null && ucar_carbasicinfo.length > 0) {
        var LastGroupName = "";
        for (var i = 0; i < ucar_carbasicinfo.length; i++) {
            if (ucar_carbasicinfo[i].GroupName != LastGroupName) {
                if (LastGroupName != "") {
                    BasicList += "</dl>";
                }
                LastGroupName = ucar_carbasicinfo[i].GroupName;

                BasicList += "<dl class=\"brand_list1\"><dt>" + LastGroupName + "</dt>"
            }
            BasicList += "<dd><a href=\"javascript:selectCarBasic(" + ucar_carbasicinfo[i].Value + ",'" + ucar_carbasicinfo[i].Text + "')\">" + ucar_carbasicinfo[i].Text + "</a></dd>";
        }
        BasicList += "</dl>";
    }
    $("chexing_cnt").innerHTML = html + BasicList;
}
function open_chexing() {
    if (hisBrandid <= 0 || hisSerialId <= 0) {
        return;
    }
    DynamicOpen_Div($('chexing_content'), $('dpa_chexing'), $('chexing_cnt'), 200, 360);
}
//关闭车型
function close_chexing() {
    $('chexing_content').style.display = 'none';
}
//选择车型   
function selectCarBasic(BasicId, BasicName) {

    if (hisBasicid == BasicId) {
        close_chexing();
        return;
    }
    $("li_chexing").innerHTML = BasicName;
    close_chexing();
    var newURL = window.location.href;
    newURL = RemoveURLParam(newURL, "notbh");
    newURL = SetURLParam(newURL, "carid", BasicId);
    newURL = SetURLParam(newURL, "page", "1");
    window.location.href = newURL;
}

/*车辆对比start*/
lastScrollY = 0;
function heartBeat() {
    var diffY;
    var oCompare = document.getElementById("div_id");
    if (!oCompare)
        return;
    if (document.documentElement && document.documentElement.scrollTop)
        diffY = document.documentElement.scrollTop;
    else if (document.body)
        diffY = document.body.scrollTop
    percent = .1 * (diffY - lastScrollY);
    if (percent > 0) percent = Math.ceil(percent);
    else percent = Math.floor(percent);
    document.getElementById("div_id").style.top = parseInt(document.getElementById("div_id").style.top) + percent + "px";
    lastScrollY = lastScrollY + percent; ;
}
window.setInterval("heartBeat()", 10);

function Show(divID1, divID2) {
    document.getElementById(divID1).style.display = "none";
    document.getElementById(divID2).style.display = "block";
}
function GetUrlLink(UcarID, carSource1L) {
    if (carSource1L == 1) {
        return "/buycar/CarInfo.aspx?UcarID=" + UcarID;
    }
    else if (carSource1L == 2) {
        return "/buycar/ucartransdetailpage.aspx?UcarID=" + UcarID;
    }
    return "#";
}
function CreateCompare(liId, BrandName, UcarType, UcarSerialNumber, UserID) {

    var Url = GetUrlLink(liId, UcarType);
    var ul = $("ulCompare");
    var li = document.createElement("li");
    li.setAttribute("id", liId);
    ul.appendChild(li);
    var listr = "<span" + " onclick='RemoveCompare(" + "\"" + liId + "\"" + ",\"" + BrandName + "\"" + ",\"" + UcarType + "\"" + ",\"" + UcarSerialNumber + "\"" + ",\"" + UserID + "\"" + ");' style=\"cursor: pointer;\"/>&nbsp;&nbsp;</span>";
    listr += "<a href='" + Url + "' title='" + BrandName + "' target=\"_blank\" class=\"mc_cola\">" + BrandName + "</a>";
    $(liId).update(listr);

    var cookieCompare = GetCompare();
    cookieCompare += ("%" + liId + "@" + BrandName + "@" + UcarType + "@" + UcarSerialNumber + "@" + UserID);
    SetCompare(cookieCompare);
    if ($("dt" + liId)) {
        $("dt" + liId).className = 'xshb';
    }
}

function RemoveCompare(LiId, name, UcarType, UcarSerialNumber, UserID) {
    var strtemp = "%" + LiId + "@" + name + "@" + UcarType + "@" + UcarSerialNumber + "@" + UserID

    var cookieCompare = GetCompare();
    SetCompare(cookieCompare.replace(strtemp, ""));

    var objLi = $(LiId);
    if (objLi != null) {
        $(LiId).remove();
    }
    if ($("ckx" + LiId) != null) {
        $("ckx" + LiId).checked = false;
    }
}

function Display() {
    //2011-01-18 add by shihb 当点击显示未审核车源停留上次浏览位置
    //start
    var pnotbathc = $("carlist1_pagerNotBatch");
    var pforlist = $("carlist1_pagerForListBottom");
    var picpforlist = $("carpiclist1_pagerForListBottom");
    var picnotbathc = $("carpiclist1_pagerNotBatch");
    var buycarlist3 = $("buycar_list3");
    var divnotbathc = $("divnotBatch");
    var hidpagecount = $("hidpagec");
    var hidnotbh = $("hidnotbh");
    var url = window.location.href;
    var p = getargv(url, "page")
    var b = getargv(url, "notbh")
    //是否为最后一页
    if ($("hidlastpage") != null && $("hidlastpage").value == "1") {
        if (hidnotbh != null && hidnotbh.value == "1") {
            if (pnotbathc != null) {
                pnotbathc.style.display = '';
            }
            if (pforlist != null) {
                pforlist.style.display = 'none';
            }
            if (picnotbathc != null) {
                picnotbathc.style.display = '';
            }
            if (picpforlist != null) {
                picpforlist.style.display = 'none';
            }
        }
        else {
            if (pforlist != null) {
                pforlist.style.display = '';
            }
            if (pnotbathc != null) {
                pnotbathc.style.display = 'none';
            }
            if (picnotbathc != null) {
                picnotbathc.style.display = 'none';
            }
            if (picpforlist != null) {
                picpforlist.style.display = '';
            }
        }
    }

    if (hidnotbh != null && hidnotbh.value == "1") {


        if (buycarlist3 != null) {
            buycarlist3.style.display = '';
        }
        if (divnotbathc != null) {
            divnotbathc.style.display = '';
        }
        if ($("buycar_list2") != null) {
            $("buycar_list2").style.display = '';
        }
    }
    else {
        if (pnotbathc != null) {
            pnotbathc.style.display = 'none';
        }
    }
    //当有参数notbh if (!isNaN(p))
    if (hidpagecount != null && !isNaN(p) && !isNaN(b)) {
        if (parseInt(p) != hidpagecount.value) {
            if (parseInt(b) == 1) {
                if ($("hidpagecount").value == 15 || $("hidpagecount").value == 30) {
                    if (divnotbathc != null) {
                        divnotbathc.style.display = '';
                    }
                }
                else {
                    if (divnotbathc != null) {
                        divnotbathc.style.display = 'none';
                    }
                }
            }
        }
    }
    //end
    var show = GetCompare().split("%");
    if (show.length > 0) {
        SetCompare("");
        for (i = 1; i < show.length; i++) {
            var para = show[i].split("@");
            CreateCompare(para[0], para[1], para[2], para[3], para[4])

            if ($("ckx" + para[0]) != null) {
                $("ckx" + para[0]).checked = true;
                $("dt" + para[0]).className = 'xshb';
            }
            Show('i1', 'i2');
        }
    }
}
// 显示未审核车源(点击显示未审核车源事件)
function ShowNotBatchCar() {
    if ($("hidpagecount").value == 15 || $("hidpagecount").value == 30) {
        notBatchQuery();
    }
    if ($("buycar_list3") != null) {
        $("buycar_list3").style.display = '';
    }
    if ($("buycar_list2") != null) {
        $("buycar_list2").style.display = '';
    }
    if ($("divnotBatch") != null) {
        $("divnotBatch").style.display = '';
    }
    if ($("carlist1_pagerForListBottom") != null) {
        $("carlist1_pagerForListBottom").style.display = "none";
    }
    if ($("carlist1_pagerNotBatch") != null) {
        $("carlist1_pagerNotBatch").style.display = '';
    }
    if ($("carlist1_divnotBatchCar") != null) {
        $("carlist1_divnotBatchCar").style.display = "none";
    }
    if ($("carpiclist1_pagerForListBottom") != null) {
        $("carpiclist1_pagerForListBottom").style.display = "none";
    }
    if ($("carpiclist1_pagerNotBatch") != null) {
        $("carpiclist1_pagerNotBatch").style.display = '';
    }
    if ($("carpiclist1_divnotBatchCar") != null) {
        $("carpiclist1_divnotBatchCar").style.display = 'none';
    }
    if (document.all) {
        if ($("a001") != null) {
            $("a001").click();
        }
    }
    else {  //firefox下面执行
        if ($("a001") != null) {
            var evt = document.createEvent("MouseEvents");
            evt.initEvent("click", true, true);
            $("a001").dispatchEvent(evt);
            window.location.href = window.location.href + "#a001";
        }
    }
    if ($("hidpagecount").value == 15 || $("hidpagecount").value == 30) {
        notBatchQuery();
    }
}
function notBatchQuery() {
    var url = window.location.href;
    var p = getargv(url, "page")
    var newURL = RemoveURLParam(url, "page");
    if (!isNaN(p)) {
        newURL = SetURLParam(newURL, "page", parseInt(p) + 1);
    }
    newURL = SetURLParam(newURL, "notbh", "1");
    window.location.href = newURL;
}
function ValidateAndAddCompare(liId, BrandName, UcarType, UcarSerialNumber, UserID) {

    var str = GetCompare();
    //需要检查是否已存在相同项 以及是否超出了个数
    var show = str.split("%");
    if (show.length > 1) {
        for (i = 1; i < show.length; i++) {
            var para = show[i].split("@");
            if (para[0] == liId) {
                alert("此车源已经添加到了对比窗口中！");
                return;
            }
        }
    }
    if (show.length < 5) {
        CreateCompare(liId, BrandName, UcarType, UcarSerialNumber, UserID);
    }
    else {
        alert("最多只能选择四种车源进行对比！");
        objCkx = $("ckx" + liId);
        if (objCkx != null) {
            objCkx.checked = false;
        }
        return;
    }
}
function ToCompare() {
    var s = "/buycar/carcompare.aspx?ucarid=";
    var str = GetCompare();
    var show = str.split("%");
    var url = "";
    if (show.length > 2) {
        for (i = 1; i < show.length; i++) {
            var para = show[i].split("@");
            url += "," + para[0];
        }
    }
    else {
        alert("最少选择两种车源进行对比！");
        return;
    }
    var i = url.indexOf(",");
    url = s + url.substring(i + 1);
    window.open(url);
}

function CompareChange(obj, liId, BrandName, UcarType, UcarSerialNumber, UserID) {
    if (obj.checked == true) {
        ValidateAndAddCompare(liId, BrandName, UcarType, UcarSerialNumber, UserID);
    }
    else {
        RemoveCompare(liId, BrandName, UcarType, UcarSerialNumber, UserID);
    }
}

function SetCompare(value) {
    SetCookie("Ucar_Compare", value, getExpDate(0, 1, 0), "/", null, null);
}
function GetCompare() {
    var Compare = GetCookie("Ucar_Compare");
    if (Compare == null) {
        return "";
    }
    else {
        return Compare;
    }
}

function SetCookie(name, value, expires, path, domain, secure) {
    document.cookie = name + "=" + escape(value) +
	        ((expires) ? "; expires=" + expires : "") +
	        ((path) ? "; path=" + path : "") +
	        ((domain) ? "; domain=" + domain : "") +
	        ((secure) ? "; secure" : "");
}
//--- 获取cookie
function GetCookie(sName) {
    var aCookie = document.cookie.split("; ");
    for (var j = 0; j < aCookie.length; j++) {
        var aCrumb = aCookie[j].split("=");
        if (escape(sName) == aCrumb[0])
            return unescape(aCrumb[1]);
    }
    return null;
}
//--- 删除cookie
function DelCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = GetCookie(name);
    document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
function getExpDate(days, hours, minutes) {
    var expDate = new Date();
    if (typeof days == "number" && typeof hours == "number" &&
	        typeof hours == "number") {
        expDate.setDate(expDate.getDate() + parseInt(days));
        expDate.setHours(expDate.getHours() + parseInt(hours));
        expDate.setMinutes(expDate.getMinutes() + parseInt(minutes));
        return expDate.toGMTString();
    }
}
/*车辆对比end*/

/*收藏记录start*/

/*收藏记录End*/
function AddCollectionViewHistory() {

    var radioList = document.getElementsByName("radioSearch");
    if (radioList != null) {
        for (var i = 0; i < radioList.length; i++) {
            if (radioList[i].checked) {
                AddCollection(radioList[i].value, radioList[i].title);
                return;
            }
        }
        alert("请选择要收藏的项");
    }
}
/*邮件订阅开始*/

var iTimeoutID;
function BookReadMail(id) {
    var mail = $(id);
    var divBookRead = $('divBookRead');
    divBookRead.style.display = "block";
    var _EmailNumFilter = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
    if (mail.value == null || mail.value == '') {
        $('divBookRead').update("请填写Email!");
        iTimeoutID = window.setTimeout("know(iTimeoutID)", 4000);
    }
    else if (!_EmailNumFilter.test(mail.value)) {
        divBookRead.update("Email格式不正确!");
        iTimeoutID = window.setTimeout("know(iTimeoutID)", 4000);
    }
    else {
        $('divBookRead').update("正在处理请稍候...");
        var search = document.location.search == '' ? '?bt=1' : document.location.search;
        var url = "/ajax/bookReadCarSource.ashx" + search + "&bt=1&type=add&ism=1&mail=" + mail.value + "&url=" + window.location.href.replace(/\&/g, '$') + "&suiji=" + Math.round(Math.random() * 10000);
        new Ajax.Request(url, { method: "get", onComplete: updatedivBookRead, evalScripts: true, asynchronous: true });
    }
}

function updatedivBookRead(request) {
    var text = '';
    if (request.responseText == "1")//成功
    {
        text = '感谢您使用优卡网车源订阅服务!';
    }
    else if (request.responseText == "0")//重复
    {
        text = '您已订阅,不能重复订阅!';
    }
    else if (request.responseText == "-1")//失败
    {
        text = '抱歉，您的订阅失败,请重试!';
    }
    $("divBookRead").style.display = "block";
    $("divBookRead").update(text);
    $('txtMail').value = '';
    iTimeoutID = window.setTimeout("know(iTimeoutID)", 4000);
}
function know() {
    $("divBookRead").style.display = "none";
    window.clearTimeout(iTimeoutID)
}
function CreateRSS() {
    var search = $F('ucCarlist_hdurlsearch') == '' ? '' : '?' + $F('ucCarlist_hdurlsearch');
    window.open('CreateRSS.ashx' + search);
}
/*邮件订阅结束*/

/*浏览历史记录Start*/
function HistorySearchView() {
    var strDiv = "";
    var cookievalue = eval('[' + GetCookie('LastSearchView') + ']');

    if (cookievalue == null) {
        $('divRecently').style.display = 'none';
        return;
    }
    for (var i = 0; i < cookievalue.length; i++) {
        if (cookievalue[i]) {
            strDiv += "<li>";
            strDiv += "<input type=\"radio\" title='" + unescape(cookievalue[i].title) + "' value=\"" + cookievalue[i].href + "\" name=\"radioSearch\" />";
            strDiv += "<a href=\"" + cookievalue[i].href + "\" title='" + unescape(cookievalue[i].title) + "' target=\"_blank\">";
            strDiv += unescape(cookievalue[i].title);
            strDiv += "</a>"
            strDiv += "</li>";
        }
    }
    $('ulserrched_condition').update(strDiv);
}

function WriteSearchHistory() {
    var title = $('sptitle').innerHTML;
    if (title == null || title == '' || title == '全部车源' || title == '没有搜索的结果') {
        return;
    }
    title = escape(title);
    var link = window.location.href;


    var cookieList = GetCookie('LastSearchView');
    var cookievalue = '{href:\'' + window.location.href + '\',title:\'' + title + '\'}';

    if (cookieList == null) {
        SetCookie('LastSearchView', cookievalue);
    }
    else {
        var temp = null;
        temp = cookievalue;
        var oldcookievalue = eval('[' + GetCookie('LastSearchView') + ']');
        var count = 0;
        for (var i = 0; i < oldcookievalue.length; i++) {
            if ((eval('[' + cookievalue + ']')[0].title != oldcookievalue[i].title) && count < 3) {
                temp += "," + '{title:\'' + oldcookievalue[i].title + '\',href:\'' + oldcookievalue[i].href + '\'}';
                count++;
            }
        }
        DelCookie('LastSearchView');
        SetCookie('LastSearchView', temp);
    }
}
WriteSearchHistory();
HistorySearchView();
/*浏览历史记录end*/


