﻿$(function(){
    var uname=readCookie("uname");
//    if(uname!="")
//    {
//        $(".d1").html("<div style=\"float:left; margin-top:10px; margin-left:70px\"><font size=\"-1\" color=\"darkblue\">欢迎你:"+uname+"</font></div><div style=\"float:left; margin-top:10px; margin-left:70px\"><a href=\"#\"><font size=\"-1\" color=\"darkblue\" id=\"homepage\">设为首页</font></a></div><div style=\"float:left; margin-top:10px;margin-left:20px\"><a href=\"#\"><font size=\"-1\" color=\"darkblue\" id=\"bbs\">进入论坛</font></a></div><div style=\"float:left; margin-top:10px;margin-left:20px\"><a href=\"#\"><font size=\"-1\" color=\"darkblue\" id=\"shoucang\">加入收藏</font></a></div><div style=\"float:left; margin-top:10px;margin-left:20px\"><span id=\"exit\" style=\"cursor:pointer\"><font size=\"-1\" color=\"darkblue\">退出</font></span></div>");
//    }
//    else
//    {
//        $(".d1").html("<div class=\"style3\"><font size=\"-1\" color=\"darkblue\">用户名：</font></div><div style=\"float:left; margin-top:6px\"><input  type=\"text\" id=\"username\" style=\"width:80px; height:15px;\"/></div><div style=\"float:left; margin-top:10px\"><font size=\"-1\" color=\"darkblue\">密码：</font></div><div style=\"float:left; margin-top:6px\"><input  type=\"password\" style=\"width:80px; height:15px\" id=\"password\" /></div><div style=\"float:left; margin-top:6px; margin-left:25px\"><img src=\"/images/登陆.jpg\" id=\"login\" /></div><div style=\"float:left; margin-top:6px; margin-left:33px\"><img src=\"/images/注册.jpg\" id=\"regist\" /></div><div style=\"float:left; margin-top:10px; margin-left:70px\"><a href=\"#\"><font size=\"-1\" color=\"darkblue\" id=\"homepage\">设为首页</font></a></div><div style=\"float:left; margin-top:10px;margin-left:20px\"><a href=\"#\"><font size=\"-1\" color=\"darkblue\" id=\"bbs\">进入论坛</font></a></div><div style=\"float:left; margin-top:10px;margin-left:20px\"><a href=\"#\"><font size=\"-1\" color=\"darkblue\" id=\"shoucang\">加入收藏</font></a></div>");
//    }
    if(uname!="")
    {
        $("#user_home_login").html("");    
        $(".d1").html("<font size=\"-1\" color=\"darkblue\">欢迎你:"+uname+"</font><span id=\"exit\" style=\"cursor:pointer\"><font size=\"-1\" color=\"darkblue\">退出</font></span>");
    }
    else
    {
        $("#user_home_login").html("用户名：<input name=\"textfield\" type=\"text\" class=\"txt_top2\" id=\"username\" />  密码：<input id=\"password\" name=\"textfield2\" type=\"password\" class=\"txt_top2\" /> <img src=\"/images/index_r2_c5.gif\" width=\"25\" height=\"25\" align=\"absmiddle\" id=\"login\"  style=\"cursor:pointer\"/><a href=\"/Register.aspx\" target=\"_blank\">[注册]</a>");
        $(".d1").html("");
    }
    //$("#user_home_login").load("getuserstate.aspx");
    $("#Factory").change(function(){
        $.post("/Middle.aspx",{FactoryId:$("#Factory").val(),Action:"selectBrand"},function(data){
           $("#Brand").html(data);  
        })
    })
    $("#Brand").change(function(){
        $.post("/Middle.aspx",{FactoryId:$("#Factory").val(),BrandId:$("#Brand").val(),Action:"selectModel"},function(data){
           $("#Model").html(data);
        })
    })
    
   $("#search").click(function(){
       if($("#Factory").val()!="0"&$("#Brand").val()!="0"&$("#Model").val()!="0")
       {
            //window.open("/ImgModel.aspx?Factoryid="+$("#Factory").val()+"&BrandId="+$("#Brand").val()+"&ModelId="+$("#Model").val());
            $.post("/Middle.aspx",{ModelId:$("#Model").val(),Action:"b"},function(data){
               window.open("/Model/"+data);
            })
       }
       else
        alert("请选择完整信息！");
   })
   $("#regist").click(function(){
      window.open("/Register.aspx");
   })
    $("#Province").change(function(){
         $.post("/Middle.aspx",{Province:$("#Province").val(),Action:"a"},function(data){
           $("#City").html(data);    
        })
    })
    $("#City").change(function(){
         $.post("/Middle.aspx",{City:$("#City").val(),Action:"selectArea"},function(data){
            if(data.length<59)
            {
                $("#area").hide();
            }
            else
                $("#area").show();    
            $("#Area").html(data);
            
        })
    })
    $("#login").click(function(){
        $.post("/checkusername.aspx",{uname:$("#username").val(),upass:$("#password").val(),Action:"login"},function(data){
        if(data.indexOf("登陆成功")!=-1)
        {
           var arr=new Array(2);
           arr=data.split(",");
           alert(arr[0]);
           writeCookie("uname",$("#username").val(),2);
           window.location=arr[1];
        }
        else
        {
            alert(data);
           $("#username").val("");
           $("#password").val("");
        }
        })
    })
    $("#exit").click(function(){
        $.post("/Exit.aspx",function(data){
            var arr=new Array(2);
            arr=data.split(",");
            alert(arr[0]);
            writeCookie("uname","",2);
            window.location=arr[1];
        })
    })
    $("#homepage").click(function(){
        setHomepage();
    })
    $("#shoucang").click(function(){
        AddToFavorite();
    })
     $("#bbs").click(function(){
        window.open("http://bbs.imgcar.com/");
    })
})
//写入cookie
function writeCookie(name, value, hours)
{
  var expire = "";
  if(hours != null)
  {
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
  }
  document.cookie = name + "=" + escape(value) + expire+";path=/";
}
//读取cookie
function readCookie(name)
{
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    { 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}
//设置首页
function setHomepage(){   
   if (document.all)
   {   
        document.body.style.behavior='url(#default#homepage)';   
        document.body.setHomePage(document.URL);   
   }
   else if (window.sidebar)
   {   
        if(window.netscape)
        {   
             try{    
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");    
             }catch (e)
             {    
                alert( "该操作被浏览器拒绝，如果想启用该功能，请在地址栏内输入 about:config,然后将项 signed.applets.codebase_principal_support 值该为true" );    
             }   
        }    
        var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components. interfaces.nsIPrefBranch);   
        prefs.setCharPref('browser.startup.homepage',document.URL);   
   }   
}
//加入收藏
function AddToFavorite()   
{   
   if (document.all)
   {   
       window.external.addFavorite(document.URL,document.title);   
   }
   else if (window.sidebar)
   {   
       window.sidebar.addPanel(document.title, document.URL, "");   
   }   
}    