
/**
* @author finy
*/

var intval = null;
/*载入js*/
function loadjs(file,jsId)
{
  var scriptTag = document.getElementById(jsId);
  var head = document.getElementsByTagName('head').item(0);
  if(scriptTag) head.removeChild(scriptTag);
  script = document.createElement('script');
  script.src = file;
  script.type = 'text/javascript';
  script.id = jsId;
  head.appendChild(script);

}
/*删除js*/
function unsetJs(jsId)
{
  var scriptTag = document.getElementById(jsId);
  var head = document.getElementsByTagName('head').item(0);
  if(scriptTag) scriptTag.parentNode.removeChild(scriptTag);
  //alert(document.getElementsByTagName('head').item(0).innerHTML);
}

function byId(ids){
  return document.getElementById(ids);
}

function byTag(tag){
  return document.getElementsByTagName(tag);
}

function byName(nm){
  return document.getElementsByName(nm);
}

function bind(func, args) {
  return function() {
    func.apply(null, args);
  };
}

var count;

/*使body不可用*/
function disableBody(){
  byTag("body")[0].leftMargin=0;
  byTag("body")[0].rightMargin=0;
  byTag("body")[0].bottomMargin=0;
  dv=createDiv("disableDiv","#006699","0px","0px","absolute","100%","","2");
  dv.style.top=dv.style.left=dv.style.margin=dv.style.padding="0px";
  if (document.body.clientHeight<=window.document.body.offsetHeight)
  {
    var wh = window.document.body.offsetHeight + "px";
    if (window.innerHeight)
    {
      var wh = window.innerHeight + "px";
    }
  }
  else
  {
    var wh = window.document.body.offsetHeight + "px";
  }
  dv.style.height = wh;
  document.body.appendChild(dv);
  count = 100;
  intval = setInterval("setEffect(dv,0,30,10,'opacity',intval)",2);
}

/*创建一个div对象*/
function createDiv(dvid,bgcolor,lft,tp,pos,wdth,hgt,zindex){
  var newdv = document.createElement("div");
  if(dvid!=""){
    newdv.id = dvid;
  }
  if(bgcolor!=""){
    newdv.style.backgroundColor=bgcolor;
  }
  if(lft!=""){
    newdv.style.left = lft;
  }
  if(tp!=""){
    newdv.style.top = tp;
  }
  if(pos!=""){
    newdv.style.position = pos;
  }
  if(wdth!=""){
    newdv.style.width = wdth;
  }
  if(hgt!=""){
    newdv.style.height = hgt;
  }
  if(zindex!=""){
    newdv.style.zIndex = zindex;
  }
  return newdv;
}

/*从body中删除对象*/
function removeObjByBody(objid){
  document.body.removeChild(byId(objid));
}

/*设置透明度*/
function setEffect(obj,flag,end,step,efect,intval){
  if(flag == 0){
    tmp = count-step;
    if(tmp<=end){
      clearInterval(intval);
    }
  }else{
    tmp = count+step;
    if(tmp>=end){
      clearInterval(intval);
    }
  }
  if(efect=="opacity"){
    obj.style.filter = "alpha(opacity="+count+")";
  }else if(efect=="size"){
    obj.innerHTML=(334+count/2);
    obj.style.left = (334+count/2)+"px";
    obj.style.width = count+"px";
  }
  count=tmp;
}

/*使body可用*/
function enableBody(){
  removeObjByBody("disableDiv");
  removeObjByBody("newdv");
}

/*正在载入*/
function loading(){
  disableBody();
  dvs = createDiv("newdv","#ffffff","","","absolute","200px","10px","3");
  dvs.style.top=document.body.clientTop+250;
  dvs.style.border="3px solid #006699";
  dvs.style.left = document.body.clientWidth/3;
  document.body.appendChild(dvs);
  dvs.style.textAlign="center";
  dvs.style.padding="10px";
  dvs.innerHTML="<img src='images/loading.gif' style=\"\"> loading...";
}

/*构造ajax对象*/
function AjaxObject()
{
  var request = null;
  try
  {
    request = new ActiveXObject("Msxml2.XMLHTTP");
  }
  catch (e)
  {
    try
    {
      request = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (oc)
    {
      request = null;
    }
  }

  if (!request && typeof XMLHttpRequest != "undefined")
  {
    try
    {
      request =  new XMLHttpRequest();
    }
    catch (fa)
    {
      alert("抱歉，您的浏览器不支持这个功能，请选择IE 6.0或FireFox浏览器。")
      request = null;
    }
  }
  return request;
}

/*
function InitRequest()
{
var C_req = null;
try{
C_req = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e){
try{
C_req = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(oc){
C_req = null;
}
}
if (!C_req && typeof XMLHttpRequest != "undefined"){
try{
C_req = new XMLHttpRequest();
}
catch(fa){
alert("对不起!您的浏览器不支持该功能,请使用Internet Explorer 6.0或FireFox浏览器!");
C_req = null;
}
}
return C_req;
}*/

/*处理请求*/
function PostRequest(url, data, dataType,mid)
{
  var AjaxObj = AjaxObject();
  if (AjaxObj != null)
  {
    AjaxObj.onreadystatechange = function ()
    {
      if (AjaxObj.readyState == 4)
      {
        ProcessAjaxData(AjaxObj.responseText,dataType,mid);
      }
    };
    AjaxObj.open("POST", url, true);
    AjaxObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    AjaxObj.send(data);
  }
}

function ProcessAjaxData(data,typ,mid)
{
  switch(typ){
    case 0:
    enableBody();
    document.getElementById("com_models").innerHTML=data;
    break;
    case 1:
    document.getElementById("compare_models").innerHTML="";
    document.getElementById("compare_models").innerHTML=data;
    break;
  }
}
function getData(val,type){
  loading();
  if(type == "seris"){//系列
    PostRequest("get_model.php","seris="+val,0,"");
  }else if(type == "model"){//型号
    PostRequest("get_model.php","model="+val,0,"");
  }
}

/*增加/减少比较项*/
function manage_item(mid,typ){
  var tmp = document.getElementById("select_mids").value;
  //alert("start "+tmp);
  //alert(tmp);
  var vl="";
  if(typ == 1){
    if(tmp.indexOf(",")!=-1){
      tmp_ary = tmp.split(",");
      for(var j=0;j<tmp_ary.length;j++){
        if(tmp_ary[j] == mid){
          return false;
        }
      }
      //alert(tmp_ary.length);
      if(tmp_ary.length>=6){
        alert("最多比较6款车型 ^_^");
        return false;
      }
    }
    if(mid!=0){
      if(parseInt(tmp) == 0){
        document.getElementById("select_mids").value=mid;
        vl = "mids="+mid;
      }else{
        document.getElementById("select_mids").value=tmp+","+mid;
        vl = "mids="+tmp+","+mid;
      }
    }else{
      vl = "mids="+tmp;
    }
  }else if(typ == -1){
    tmp_ary2 = tmp.split(",");
    for(var i=0;i<tmp_ary2.length;i++){
      if(tmp_ary2[i] == mid){
        continue;
      }else{
        vl=vl+tmp_ary2[i]+",";
      }
    }
    var tp = vl.substr(0,vl.length-1);
    if(tp == ""){
      document.getElementById("select_mids").value="0";
    }else{
      document.getElementById("select_mids").value=tp;
    }
    vl = "mids="+tp;
  }
  //alert(vl);
  //alert("end "+vl);
  PostRequest("get_compare.php",vl,1,mid);
}

/*提交比较*/
function sub_compare(){
  var tmp = document.getElementById('select_mids').value;
  tmp = tmp.split(",");
  var saveval="";
  for(var i=0;i<tmp.length;i++){
    if((tmp[i] == 0) ||(saveval.indexOf(tmp[i])!=-1)){
      continue;
    }else{
      saveval = saveval+","+tmp[i];
    }
  }
  saveval = saveval.substr(1,saveval.length);
  location.href='model_compare2.php?compid='+saveval;
}

