﻿/// <reference name="MicrosoftAjax.js"/>
/*!
*   My Feedback(Jquery Plugin)
*
* Copyright (c) 2009 Andy Huang
* Date: Sunday, February 01, 2009
* Version: V1.0
*/
(function($) {
    //浮动层开始-----------------
    $.fn.panel = function(options) {
        //获取传入的参数
        var opts = $.extend({}, $.fn.panel.defaults, options);
        var ie6 = false; //判断ie6
        if ($.browser.msie && jQuery.browser.version < 7) {
            ie6 = true;
            try {
                document.execCommand("BackgroundImageCache", false, true);
            } catch (err) { }
        }
        //执行
        return this.each(function() {
            $this = $(this); //全局对象,整个浮动层
            var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
            //层定位,可改写你喜欢定位的位置(结合scroll()事件)
            var loc = { right: o.right + "px", bottom: o.bottom + "px" };
            $this.css({ "z-index": "9999", "position": "fixed" }).css(loc);
            //解决ie6 层定位的bug
            if (ie6) {
                resize(); //位置
                scroll(); // 滚动
            }

            //创建留言板和绑定事件
            createDiv();
            bindButtonEvent();

        });
    };
    function resize() {
        $(window).resize(function() {
            ie6style();
        });
    }
    function scroll() {
        $this.css("position", "absolute");
        $(window).scroll(function() {
            ie6style();
        });
    }
    function ie6style() {
        $(window).scroll(function() {
            //unkown code,who can help me?
            $this.css({ "bottom": "0px" });
            $this.css({ "right": "0px", "bottom": "1px" });
        });
    }
    //浮动层结束------------------
    //留言板
    function createDiv() {
          var str="<div class=\"iwlw_but\" id='lw_Small' style='float:right'>";
          str+="<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
          str+=" <tr>";
          str+="<td>欢迎您给我们留言</td>";
          str+="<td><a href=\"#flashTop\" id='leaveWord' title='点击进行留言'><img src=\"leaveWordInfo/MessageStyle/img/new_but_ilw11.jpg\" width=\"13\" height=\"12\" border=\"0\"/></a></td>";
          str+="</tr>";
         str+="</table>";
         str+="</div>";
         //留言板头部
          str+="<table width=\"434\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" id='lw_bigTitle' style='display:none'>";
          str+="<tr>";
          str+="<td width=\"434\" height=\"32\" align=\"right\" style=\"background: url(leaveWordInfo/MessageStyle/img/new_main_top.gif) no-repeat; padding-right:8px;\"><a href=\"#\" id='jquery-x-tool-close'><img src=\"leaveWordInfo/MessageStyle/img/close1.gif\" width=\"17\" height=\"17\" border=\"0\" /></a></td>";
          str+="</tr>";
          str+="</table>";
          //留言板主体
          str+="<table width=\"434\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" bgcolor=\"#ffffff\" id='lw_Body' style='display:none'>";
          str+="<tr>";
          str+="<td width=\"5\" style=\"background:url(leaveWordInfo/MessageStyle/img/win_l.gif) repeat-y;\">&nbsp;</td>";
          str+=" <td>";
          str+="<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" style=\"background:url(leaveWordInfo/MessageStyle/img/new_text_bac.png) bottom repeat-x;\">";
          str+="<tr>";
          str+="<td colspan=\"2\" align=\"left\" style=\"padding-left:10px;\"><textarea  id=\"lw_Content\" style=\"width:404px; height:113px; border:#cccccc 1px solid; margin-top:8px;\"></textarea></td>";
          str+="</tr>";
          str+="<tr>";
          str+="<td width=\"47%\" height=\"30\" align=\"left\" style=\"padding-top:5px; padding-left:10px;\"><span class=\"p_no\">手机号：</span><input type=\"text\" class=\"p_text\" id=\"celephone\" style=\"width:120px;\" value=\"请输入手机号\" onmouseover=\"this.focus()\" onblur=\"if (value ==''){value='请输入手机号'}\" onfocus=\"this.select()\"  onclick=\"if(this.value=='请输入手机号')this.value=''\" /></td>";
          str+="<td width=\"53%\" align=\"right\" style=\"padding-right:10px;padding-top:5px;\"><span class=\"p_no\">邮箱：</span><input type=\"text\" class=\"p_text\" id=\"mail\" style=\"width:168px;\" value=\"请输入邮箱\" onmouseover=\"this.focus()\" onblur=\"if (value ==''){value='请输入邮箱'}\" onfocus=\"this.select()\"  onclick=\"if(this.value=='请输入邮箱')this.value=''\" /></td>";
          str+="</tr>";
          str+="<tr>";
          str+="<td colspan=\"2\" style=\"padding-left:2px; padding-left:10px;\"><span class=\"p_no\">公司名称：</span><input type=\"text\" class=\"p_text\" id=\"company\" style=\"width:335px;\" value=\"请输入公司名称\" onmouseover=\"this.focus()\" onblur=\"if (value ==''){value='请输入公司名称'}\" onfocus=\"this.select()\"  onclick=\"if(this.value=='请输入公司名称')this.value=''\" /></td>";
          str+="</tr>";
          str+="<tr>";
          str+=" <td height=\"5\"></td>";
          str+="</tr>";
          str+="<tr>";
          str+=" <td colspan=\"2\" style=\"padding-left:10px;\"><input type=\"submit\" class=\"gut_tj\" id=\"lw_Submit\" value=\"\" /><span class=\"gut_tj_rf\">（留言发送后，我们会及时的联系您）</span></td>";
          str+="</tr>";
          str+=" <tr><td height=\"5\"></td></tr>";
          str+="</table>";
          str+=" </td>";
          str+=" <td width=\"5\" align=\"left\" style=\"background:url(leaveWordInfo/MessageStyle/img/win_r.gif) repeat-y right;\">&nbsp;</td>";
          str+="</tr>";
          str+="</table>";
          //尾部
          str+="<table width=\"434\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" bgcolor=\"#F1FAFF\" id='big_Bottom' style='display:none'><tr><td width=\"8\" height=\"8\" style=\"background:url(leaveWordInfo/MessageStyle/img/win_lb.gif) repeat-y;\"></td><td height=\"8\" style=\"background:url(leaveWordInfo/MessageStyle/img/win_b.gif) repeat-x;\"></td><td width=\"8\" height=\"8\" style=\"background:url(leaveWordInfo/MessageStyle/img/win_rb.gif) repeat-y right;\"></td></tr></table>";
        $this.append(str);
    }
    //留言板事件
    function bindButtonEvent() {
        var messagebox = $("#lw_Content");  //留言框
        var cellphone=$("#celephone");
        var mail=$("#mail");
        var company=$("#company");
       //小窗口
       var smallDiv=$("#lw_Small");
        var cidValue=0;
        //大窗口
        var bigTitle=$("#lw_bigTitle");
        var bigBody=$("#lw_Body");
        var bigBottom=$("#big_Bottom");
        //关闭按钮事件
        $("#jquery-x-tool-close").click(function() {
            $this.hide();
        });
        //打开留言窗口
       $("#leaveWord").click(function() {
       bigTitle.show();
       bigBottom.show();
       bigBody.show();
       smallDiv.hide();
        })
        //确认按钮事件
        $("#lw_Submit").click(function() {
            var msg = $.trim(messagebox.val());
            var cellphoneV=$.trim(cellphone.val());
            var mailV=$.trim(mail.val());
            var companyV=$.trim(company.val());
            //判断留言情况
            if ( msg.length > 1000) {
            alert("您的留言不能超过1000个字符");
            return false;
            }
            if(msg.length==0)
            {
            alert("您的留言不能为空");
            return false;
            }
            //判断手机
            if(cellphoneV.length==0)
            {
            alert("请填写您的手机，方便我们联系！")
            return false;
            }
            else
            {
           var regCel=/^1([0-9]{1})[0-9]{9}$/;
            if(!regCel.test(cellphoneV))
            {
            alert("请填写正确格式的手机号！");
            return false;
            }
            }
            //判断邮箱
            if(mailV.length==0)
            {
            alert("请填写邮箱！");
            return false;
            }
            else
            {
           var recMail=/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/;
            if(!recMail.test(mailV))
            {
            alert("请填写正确格式的邮箱！");
            return false;
            }
            }
            //判断公司
            if(companyV.length==0)
            {
            alert("请填写公司名称！");
            return ;
            }
            sendmessage(); //完成后发送到服务器事件
            
        });

    }
    //服务器xmlhttp request事件
    function sendmessage() {
        var message = $("#lw_Content"); //消息框
        var cellphone=$("#celephone");
        var mail=$("#mail");
         var company=$("#company");
        var cidValue=0;
        var btn = $("#lw_Submit");            //确认按钮
        var option = {
            url: $.fn.panel.defaults.script,
            data: { Message:escape( message.val()),Cellphone:cellphone.val(),Mail:escape(mail.val()),Company:escape(company.val()),CIDValue:cidValue}, //发送服务器数据
            success: function(data, textStatus) {//成功事件
                switch (data) {
                    case "1":
                        alert("您已成功留言，我们会尽快查看！");
                         $this.hide();
                        break;
                        case"2":
                        alert("留言失败！");
                        break;
                        default:
                        break;
                }
            }
//            error: function(XMLHttpRequest, textStatus, errorThrown) {//发送失败事件
////                error.show().empty().text(textStatus);
//            }
        };
        //ajax 执行
        $.ajax(option);
    }

    //默认值
    $.fn.panel.defaults = {
        customerid: 0,
        left: 0,
        right: 0,
        bottom: 0,
        top: 0,
        title: '留言板',
        messagetip: '',
        script: 'leaveWordInfo/FastFeedBack.ashx'
    }

})(jQuery);
