﻿// JScript 文件
// eService公共脚本类
// Dev. 追梦客
// QQ. 16991200
// 此类在使用过程中要使用jQuery，请在之前添加jQuery脚本注册
//====================AjaxForm Option =============================
//            var options = { 
//                target:       '#send_result',   // target element(s) to be updated with server response 
//                //beforeSubmit:  showRequest,  // pre-submit callback 
//                success:function (response,status){
//                    alert(response)
//                },  // post-submit callback 
//                dataType:     "json",
//                url:           _this.Urls.SaleServAjaxHandler
//                // other available options: 
//                //url:       url         // override for form's 'action' attribute 
//                //type:      type        // 'get' or 'post', override for form's 'method' attribute 
//                //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
//                //clearForm: true        // clear all form fields after successful submit 
//                //resetForm: true        // reset the form after successful submit 
//         
//                // $.ajax options can be used here too, for example: 
//                //timeout:   3000 
//====================AjaxForm Option End =============================
    var eService = {
        Urls:{
            UpdateArticleVisities:"/Scripts/UpdateArticleVisities.html",//文章点击数更新地址
            SaleServAjaxHandler:"/SaleServ/AjaxHandler.html",
            HumanResourceAjaxHandler:"/HumanResource/AjaxHandler.html",
            UserAjaxHandler:"/User/AjaxHandler.html",
            OnlineTenderAjaxHandler:"/OnlineTender/AjaxHandler.html"
        }
    };
    eService.UpdateArticleVisities = function (id,show){
        //id 文章ID
        //show 显示访问数的元素ID
        
        $.post(this.Urls.UpdateArticleVisities,{"id":id},function (data){
            $("#"+show).text(data);
        });
    }
    eService.UpdateOnlineTenderVisities = function (id,show){
        //id 文章ID
        //show 显示访问数的元素ID
        
        $.post(this.Urls.OnlineTenderAjaxHandler,{ac:"UpdateVisities","id":id},function (data){
            $("#"+show).text(data);
        });
    }
    eService.SetFontSize = function(target,font_size){
		//设置指定指定区域的字体大小，并自动设置行间距
		var _box = document.getElementById(target);
		if(_box)
		{
			_box.style.fontSize = font_size+"px";
			_box.style.lineHeight = (font_size * 1.5)+"px";
		}
	}
    eService.PrintPage = function(obj){
		//打印页面
		window.print();
	}
	eService.GetBrowser = function (){
	    //获得浏览器类型
	    if(navigator.userAgent.indexOf("MSIE")>0)return 1; 
		if(navigator.userAgent.indexOf("Firefox")>0)return 2; 
		return 0; 
	}
	eService.AddToFavorite = function (url,title){
	    //添加到收藏夹
		switch(this.GetBrowser()){
		   case 1:
		   	
			window.external.AddFavorite(url,title);
		   break;
		   case 2:
			window.sidebar.addPanel(title, url, "");
		   break;
		   case 0:
			alert("加入收藏失败，您使用的浏览器不支持这个功能");
		   break;
		}
	}
	eService.SetForHomePage = function (sender){
	    //设为首页
		try{
			sender.style.behavior="url(#default#homepage)";
			sender.setHomePage(location.hostname);
		}catch(ex){
			//TODO Nothing
			//alert(ex)
		}
		return false;
	}
	eService.GetStock = function (id,t){
		//加载股市行情信息
		if(t!=null){
	        $("#"+id).load("/Scripts/GetStock.ashx",{type:t,r:Math.random()},null);
	    }else{
	        $("#"+id).load("/Scripts/GetStock.ashx",{r:Math.random()},null);
	    }
	}
	eService.SetCookie = function (sName, sValue, timeKeep)
    {
        //设置COOKIES值
        //timeKeep单位：小时
	    var now=new Date();
	    var expireTime= new Date(now.valueOf()+timeKeep*60000*60);
	    document.cookie = sName + "=" + escape(sValue) + "; path=/; expires=" + expireTime.toGMTString() + ";";
    }
    eService.GetCookie = function (sName)
    {
        //获取指定名称的COOKIE项的值
	    var aCookie = document.cookie.split("; ");
	    for (var i=0; i < aCookie.length; i++)
	    {
		    var aCrumb = aCookie[i].split("=");
		    if (sName == aCrumb[0]) 
			    return unescape(aCrumb[1]);
	    }
	    return null;
    }
    eService.GetCurrentDateTime = function ()
    {
        //获取当前时间
        var date = new Date();
        var current=new String("");
        current += date.getFullYear()+"-";
        current += date.getMonth() + 1+"-";
        current += date.getDate()+" ";
        current += date.getHours()+":";
        current += date.getMinutes()+":";
        current += date.getSeconds();
        return current;
    }
    eService.GuestBookSubmit = function (fm)
    {
        $("#PostState").html("正在提交");
        //留言薄提交
        var data = {
            Code:$("#Code",fm).val(),
            NickName:$("#NickName",fm).val(),
            Mobile:$("#Mobile",fm).val(),
            Phone:$("#Phone",fm).val(),
            Email:$("#Email",fm).val(),
            Address:$("#Address",fm).val(),
            Title:$("#Title",fm).val(),
            Content:$("#Content",fm).val()
        }
        $.post(fm.action,data,function (rep){
            
            eval("var json="+rep+";")
            if(json.State=="OK"){
                 $("#PostState").html("<span style=\"color:blue;\">您的留言已成功提交！</span>");
                for(var s in data)
                {
                    $("#"+s+"Error").html("");
                }
            }else{
                $("#PostState").html("<span style=\"color:red;\">提交的留言数据不正确！</span>");
                for(var s in data)
                {
                    $("#"+s+"Error").html("");
                }
                for(var s in json.Errors)
                {
                    $("#"+s+"Error").html("<span style=\"color:red;\">"+json.Errors[s]+"</span>");
                }
            }
        });
        return false;
    }
    eService.GoUrl = function (url)
    {
       var _rg = /^(http:\/\/|ftp:\/\/|https:\/\/)/ig;
		if(_rg.test(url))
		{
			var _popup = open(url,"_blank","");
			if(_popup == null)
			{
				location.href = url;	
			}
		}
    }
    eService.CreateHomeStationSearchForm = function (){
        //创建首页经销商服务商查询的表单
        var _this = this;
        $(document).ready(
            function (){
                 $("#st_b1").bind("click",function (){
                    $("#station_search_form").attr("action","/SaleServ/SaleStation/List.html");
                    $("#linkStationNear").attr("href","/SaleServ/SaleStation/List.html");
                 });
                 $("#st_b2").bind("click",function (){
                    $("#station_search_form").attr("action","/SaleServ/ServStation/List.html");
                    $("#linkStationNear").attr("href","/SaleServ/ServStation/List.html");
                 });
                 //创建车型
                 $.getJSON(_this.Urls.SaleServAjaxHandler,{ac:"GetAllCarModel"},function(json){
                    for(var i=0;i<json.length;i++)
                    {
                        $("select[name='model']").get(0).options.add(new Option(json[i].Name,json[i].ID))
                    }
                 });
                 //创建省份
                 $.getJSON(_this.Urls.SaleServAjaxHandler,{ac:"GetProvince"},function(json){
                    for(var i=0;i<json.length;i++)
                    {
                        $("select[name='province']").get(0).options.add(new Option(json[i].Name,json[i].Name))
                    }
                 });
                 //绑定事件
                 $("select[name='province']").bind("change",function (s){
                    $("select[name='city']").get(0).length = 1;
                    $.getJSON(_this.Urls.SaleServAjaxHandler,{ac:"GetCity",province: $("select[name='province']").get(0).value},function(json){
                       for(var i=0;i<json.length;i++)
                       {
                           $("select[name='city']").get(0).options.add(new Option(json[i].Name,json[i].Name))
                       }
                    });
                 })
            }
        );      
        
    }
    eService.CreateFittingSearchForm = function (){
        //创建配件查询的表单
        var _this = this;
        $(document).ready(
            function (){
                 //创建省份
                 $.getJSON(_this.Urls.SaleServAjaxHandler,{ac:"GetCarBrand"},function(json){
                    for(var i=0;i<json.length;i++)
                    {
                        $("select[name='brand']").get(0).options.add(new Option(json[i].Name,json[i].ID))
                    }
                 });
                 //绑定事件
                 $("select[name='brand']").bind("change",function (){
                    $("select[name='model']").get(0).length = 1;
                    $.getJSON(_this.Urls.SaleServAjaxHandler,{ac:"GetCarModel",brand: $("select[name='brand']").get(0).value},function(json){
                       for(var i=0;i<json.length;i++)
                       {
                           $("select[name='model']").get(0).options.add(new Option(json[i].Name,json[i].ID))
                       }
                    });
                 })
            }
        ); 
        
    }
    eService.CreateStationSearchForm = function (){
        //创建经销商服务商查询的表单
        var _this = this;
        $(document).ready(
            function (){
                 //创建省份
                 $.getJSON(_this.Urls.SaleServAjaxHandler,{ac:"GetProvince"},function(json){
                    for(var i=0;i<json.length;i++)
                    {
                        $("select[name='province']").get(0).options.add(new Option(json[i].Name,json[i].Name))
                    }
                 });
                 //绑定事件
                 $("select[name='province']").bind("change",function (){
                    $("select[name='city']").get(0).length = 1;
                    $.getJSON(_this.Urls.SaleServAjaxHandler,{ac:"GetCity",province: $("select[name='province']").get(0).value},function(json){
                       for(var i=0;i<json.length;i++)
                       {
                           $("select[name='city']").get(0).options.add(new Option(json[i].Name,json[i].Name))
                       }
                    });
                 })
            }
        ); 
    }
    
    eService.GetFaqAnswer = function (faq){
        //获取在线服务里faq内容
        if($("#answer_"+faq).html().length==0)
        {
            $.getJSON(this.Urls.SaleServAjaxHandler,{ac:"GetFaqAnswer",id:faq},function(json){
                $("#answer_"+json.ID).html(json.Content);
            }); 
        }
        $("#answer_"+faq).css("display",$("#answer_"+faq).css("display")=="none"?"":"none");
    }
    eService.CreateHRButton = function (invite){
        //创建简介详情中的按钮
        var _this = this;
        $(document).ready(function (){
            $.getJSON(_this.Urls.UserAjaxHandler,{ac:"GetUserInfo",r:Math.random()},function (json){
                if(json.Userid == -1)
                {
                    $(".invite_detail_button").html("<ul><li><a href=\"/User/UserLogin.html\" class=\"login\"><span>会员登录</span></a></li><li><a href=\"/User/UserRegister.html\" class=\"reg\"><span>注册会员</span></a></li></ul><div class=\"clear\"></div>")
                }else{
                    $(".invite_detail_button").html("<ul><li><a href=\"javascript:eService.AddInviteToResume("+invite+")\"><span>添加到应聘列表</span></a></li><li><a href=\"/User/Resume/UpdateUserInfo.html\"><span>更新个人简历</span></a></li></ul><div class=\"clear\"></div>")
                }
                
            })
        })
    }
    eService.AddInviteToResume = function (invite){
        //添到职位信息到用户的应聘职位列表中
        var _this = this;
        $.post(_this.Urls.UserAjaxHandler,{ac:"AddInviteToResume",inviteid:invite},function (data){
//            if(json == "OK")
//            {
//                alert("该职位已成功添加入您的应聘列表");
//            }else{
                alert(data);
//            }
        },"text");
    }
    eService.CreateUserRegisterForm = function (){
        //用户登录
        var _this = this;
        $(document).ready(function (){
             var options = { 
                    target:        '#registerResult',   // target element(s) to be updated with server response 
                    //beforeSubmit:  showRequest,  // pre-submit callback 
                    success:function (response,status){
                        if(response == "OK"){
                            $("#registerResult").css("color","");
                            $("#registerResult").html("您已成功注册成为本站用户");
                        }else{
                             $("#registerResult").css("color","#F00");
                        }
                    },
                    url:_this.Urls.UserAjaxHandler
                }; 
             
               $('#userRegForm').ajaxForm(options);
        });
    }
    eService.CreateUserLoginForm = function (){
        //用户登录
        var _this = this;
        $(document).ready(function (){
             var options = { 
                    target:        '#loginResult',   // target element(s) to be updated with server response 
                    //beforeSubmit:  showRequest,  // pre-submit callback 
                    success:function (response,status){
                        if(response[0] == "OK"){
                            $("#loginResult").css("color","");
                            $("#loginResult").html("您已成功登录系统");
                            location.href = response[1];                              
                        }else{
                             $("#loginResult").css("color","#F00");
                             $("#loginResult").html(response[1]);
                        }
                    },
                    url:_this.Urls.UserAjaxHandler,
                    dataType:   "json"
                }; 
             
                // bind form using 'ajaxForm' 
                $('#userLoginForm').ajaxForm(options);
        });
    }
    eService.UserLoginOut = function (){
        //用户登出
        var _this = this;
        $.post(_this.Urls.UserAjaxHandler,{ac:"UserLoginOut"},function (data){
            if(data == "OK")
            {
                location.reload(true);
            }
        },"text");
    }

