<%@ page language="C#" masterpagefile="~/MasterPage/MyspaceMasterPage.master" autoeventwireup="true" inherits="User_UserAddressEdit, DoodiiShop" title="Untitled Page" enableEventValidation="false" viewStateEncryptionMode="Never" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">

    <script type="text/javascript" src="/js/Format.js"></script>

    <script type="text/javascript" src="/js/Lib/AreaSelector2.js"></script>

    <script type="text/javascript" src="/js/Lib/PageNavigator.js"></script>

    <script src="/js/Lib/WindowsManager.js" type="text/javascript"></script>

    <style type="text/css">
        .tbl_1
        {
            width:100%;
        }
        .tbl_1 .htd
        {
            background-color:#E4FCDA;
               text-align:center;
        }
        .tbl_1 td
        {
            border:1px solid #61BD34;
            padding-left:5px;
        }
 .NewTable{
	
	border-collapse:collapse;
	font-size:12px; 
	color:#666;
}
    </style>
    <input id="hdId" type="hidden" runat="server" value="0" />
    <input id="hdAreaId" type="hidden" runat="server" />
    <table class="NewTable">
        <tr>
            <td class="htd" style="width: 80px">
                地址类型：</td>
            <td style="height: 50px;">
                <input type="radio" value="公司地址" name="rdname" runat="server" id="rdHide" />公司地址
                <input type="radio" value="家庭地址" name="rdname" runat="server" id="rdHide2" />家庭地址
                <input type="radio" value="其他" name="rdname" runat="server" id="rdOther" />其他 &nbsp;&nbsp;<span
                    id="ratype" class="f_annotate">请选择地址类型。</span>
                <input id="tbTypeName" type="text" maxlength="10" style="width: 200px; margin-top: 5px;"
                    runat="server" />
                <br />
                <span id="lblTypeNameMessage" runat="server" class="f_annotate"></span>
            </td>
        </tr>
        <tr>
            <td class="htd">
                楼宇/小区：</td>
            <td style="height: 50px;">
                <input id="tbArea" type="text" maxlength="20" style="width: 200px" readonly="readonly"
                    runat="server" />
                <br />
                <span id="lblAreaMessage" class="f_annotate"></span>
            </td>
        </tr>
        <tr>
            <td class="htd">
                精确地址：</td>
            <td style="height: 50px;">
                <input id="tbAddress" type="text" maxlength="20" style="width: 200px" runat="server" />
                <br />
                <span id="lblAddressMessage" class="f_annotate"></span>
            </td>
        </tr>
        <tr>
            <td class="htd">
                姓&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;名：</td>
            <td style="height: 50px;">
                <input id="tbContactPerson" type="text" maxlength="10" style="width: 200px;" runat="server" />
                <br />
                <span id="lblContactPersonMessage" class="f_annotate"></span>
            </td>
        </tr>
        <tr>
            <td class="htd">
                手&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;机：</td>
            <td style="height: 75px;">
                <input id="tbContactMobile" type="text" maxlength="11" style="width: 100px" runat="server" />
                <br />
                <span id="lblContactTelMessage" class="f_annotate"></span>
            </td>
        </tr>
        <tr>
            <td class="htd">
                固定电话：</td>
            <td style="height: 75px;">
                <input id="tbContactPhone" type="text" maxlength="8" style="width: 100px" runat="server" />
                <span>分机：</span>
                <input id="tbContactSubPhone" runat="server" type="text" maxlength="6" style="width: 60px" />
                <br />
                <span id="lblContactPhoneMessage" class="f_annotate"></span>
            </td>
        </tr>
        <tr>
            <td colspan="2" style="padding-left: 260px;">
                <a class="Style_Input2" id="btnSubmit" href="javascript:void(0)">提&nbsp;交</a>
                <a class="All_Input" id="btnBackToList" href="javascript:void(0)" style="font-family: 宋体; display:none;
                    background-position: 0px -170px; width: 35px; margin-left: 8px;"><span class="Input_span">
                        取消</span><span class="Input_span1" style="background-position: -629px -170px;"></span></a>
            </td>
        </tr>
    </table>

    <script type="text/javascript">
    var Id=0;
    var areaId = 0;
    var DeliverAddress_ShopId = 0;
    var DeliverAddress_objAreaSelector2=null;
    var TruetypeName="";
    $(document).ready( function(){
        Id = parseInt($("#<%=hdId.ClientID%>").val());
        if (Id == 0)
        {
            alert("地址记录不存在！");
            window.parent.PopUpWindow.Close();
        }
        areaId = parseInt($("#<%=hdAreaId.ClientID%>").val());
        addressAppendInit()
    });
    
    function addressAppendInit()
    {
         DeliverAddress_objAreaSelector2 = new AreaSelector2({
            onRegionChangeFunc: DeliverAddress_AreaSelector_onRegionChange,
            onPageChangeFunc: DeliverAddress_AreaSelector_onPageChange,
            onKeywordChangeFunc: DeliverAddress_AreaSelector_onKeywordChange,
            onSelectAreaFunc: DeliverAddress_AreaSelector_onSelectArea,
            onLettersChangeFunc:DeliverAddress_AreaSelector_onLettersChange,
            onClosedFunc:Deliver_onClose,            
            offsetX: 0,
            offsetY: -5 ,
            pageStyle:1,
            //divToolbarDomContent:"<span style='color:#666; float:left; line-height:32px;'>&nbsp;&nbsp;如果楼宇太多，不方便查找您可以直接在这里搜索。</span><a class='All_Input1' style='width:108px; padding-left:12px; margin-left:10px; margin-top:4px;' href='javascript:void(0);' onclick='DeliverAddress_objAreaSelector2.close();'><span class='Input_span'>我想先去店里逛逛</span><span class='Input_span2'></span></a>"
            divToolbarDomContent:""
            });     
        bindEvents();
    }
    
    function bindEvents()
    {
        setMessage("TypeName", 0);
        setMessage("ContactPerson", 0);
        setMessage("ContactTel", 0);
        setMessage("ContactPhone", 0);
        setMessage("Area", 0);
        setMessage("Address", 0);
        $("#<%=tbTypeName.ClientID%>").bind("focus", function(){ setMessage("TypeName", 0); });
        $("#<%=tbTypeName.ClientID%>").bind("blur", function(){ checkTypeName(); });
        $("#<%=tbContactPerson.ClientID%>").bind("focus", function(){ setMessage("ContactPerson", 0); });
        $("#<%=tbContactPerson.ClientID%>").bind("blur", function(){ checkContactPerson(); });
        $("#<%=tbContactPhone.ClientID%>").bind("focus", function(){ setMessage("ContactTel", 0); });
        $("#<%=tbContactPhone.ClientID%>").bind("keypress", function(e){ return filtrateTelChar(e); });
        $("#<%=tbContactPhone.ClientID%>").bind("blur", function(){ checkContactPhone(); });
        $("#<%=tbContactSubPhone.ClientID%>").bind("focus", function(){ setMessage("ContactPhone", 0); });
        $("#<%=tbContactSubPhone.ClientID%>").bind("keypress", function(e) { return filtrateTelChar(e); });
        $("#<%=tbContactSubPhone.ClientID%>").bind("blur", function(){ checkContactPhone(); });
        $("#<%=tbContactMobile.ClientID%>").bind("focus", function(){ setMessage("ContactTel", 0); });
        $("#<%=tbContactMobile.ClientID%>").bind("keypress", function(e) { return filtrateTelChar(e); });
        $("#<%=tbContactMobile.ClientID%>").bind("blur", function(){ checkContactTel(); });
        $("#<%=tbArea.ClientID%>").bind("focus", function(){ DeliverAddress_objAreaSelector2.show();checkRadio(); });
        $("#<%=tbAddress.ClientID%>").bind("focus", function(){ setMessage("Address", 0); });
        $("#<%=tbAddress.ClientID%>").bind("blur", function(){ checkAddress(); });
        $("#btnBackToList").bind("click", function(){ window.location='/User/UserAddressList.aspx'; });
        $("#btnSubmit").bind("click", function(){ submitForm(); });
        $("#<%=rdOther.ClientID %>").bind("click",function(){functionOther();checkRadio()});
        $("#<%=rdHide.ClientID %>").bind("click",function(){hideOther();checkRadio()});
        $("#<%=rdHide2.ClientID %>").bind("click",function(){hideOther();checkRadio()});
    }
 //楼宇选择控件JS
    function DeliverAddress_AreaSelector_onRegionChange(index)
    {
        DeliverAddress_getAreaList();
    }
    function DeliverAddress_AreaSelector_onPageChange(index)
    {
        DeliverAddress_getAreaList(index);
    }
    function DeliverAddress_AreaSelector_onKeywordChange(keyword)
    {
        DeliverAddress_getAreaList();
    }
    function DeliverAddress_AreaSelector_onSelectArea(objAreaInfo)
    {   
        areaId = objAreaInfo.areaId;
        $("#<%=tbArea.ClientID %>").val(objAreaInfo.areaName);
        DeliverAddress_objAreaSelector2.hide();
        ///隐藏遮照
        //vWindowsManager.HideDialog();
         
    }
    function DeliverAddress_AreaSelector_onLettersChange(letter)
    {
       DeliverAddress_getAreaList();
    }
    function Deliver_onClose()
    {
        //vWindowsManager.HideDialog();
    }
    function DeliverAddress_getAreaList(pageIndex)
    {     
         if (pageIndex!=null)
            DeliverAddress_objAreaSelector2.pageIndex = pageIndex;
        else
            DeliverAddress_objAreaSelector2.pageIndex = 1;  
         User_UserAddressEdit.GetSuggestAreaList(
            -1,
            -1,
            -1,
            DeliverAddress_objAreaSelector2.keyword,
            DeliverAddress_objAreaSelector2.letter,
            DeliverAddress_objAreaSelector2.pageIndex,
            DeliverAddress_objAreaSelector2.pageSize,
            DeliverAddress_ShopId,
            DeliverAddress_getAreaList_Callback
        );
    }
    
    function DeliverAddress_getAreaList_Callback(res)
    {
        
        DeliverAddress_objAreaSelector2.clearAreaResult();
        var ret = res.value;
        if (ret == null) return;
        for (var i = 0; i < ret.Items.length; i++)
        {
            var areaName = ret.Items[i].Name;
            var areaId = parseInt(ret.Items[i].Id);
            DeliverAddress_objAreaSelector2.appendArea(areaId, areaName);
        }
        DeliverAddress_objAreaSelector2.showPage(ret.PageInfo.TotalPage);
        if (ret.Items.length == 0)
        {
            if ($.trim(DeliverAddress_objAreaSelector2.keyword) != "")
            {
                var s = "";
            }
            else
            {
                var s = "当前区域暂时没有楼宇/小区。";
            }
            DeliverAddress_objAreaSelector2.setMessage(s);
        }
        
    }




     function functionOther()
        {
           $("#<%=tbTypeName.ClientID %>").show();
           $("#<%=lblTypeNameMessage.ClientID%>").show();
        }
        function hideOther()
        {
           if($("#<%=tbTypeName.ClientID %>").hide())
           {}
           else
           {$("#<%=tbTypeName.ClientID %>").hide()}
           if($("#<%=lblTypeNameMessage.ClientID %>").hide())
           {}
           else
           {$("#<%=lblTypeNameMessage.ClientID %>").hide();}
        }
        function checkRadio()
        {
            var TypeName="";
            var flag=0;
            if($("#<%=rdOther.ClientID %>").attr("checked"))
            {
                TypeName=$("#<%=tbTypeName.ClientID%>").val();
                flag=1;
            }
            else
            {
                if($("#<%=rdHide.ClientID %>").attr("checked"))
                {
                    TypeName=$("#<%=rdHide.ClientID%>").val();
                    flag=2;
                }
                else if($("#<%=rdHide2.ClientID %>").attr("checked"))
                {
                    TypeName=$("#<%=rdHide2.ClientID%>").val();
                    flag=2;
                }
                
            }
            if(TypeName=="")
            {
               if(flag==0)
               { 
                setMessage("Radio", -1, "请选择地址类型！");return false;
               }
               else if(flag==1)
               {
                 setMessage("TypeName", -1, "请填写地址类型！");return false; 
               }     
            }
            else
            { 
              TruetypeName=TypeName;
              setMessage("Radio", 0, "请选择地址类型。");
              return true;
            }
        }
    //setMessage
    //<state>0:normal, 1:correct, -1:error;
    function setMessage(type, state, text)
    {
        if (type == "TypeName")
        {
            if (state == 0)
            {
                $("#<%=lblTypeNameMessage.ClientID %>").html("请填写地址类型。");
                $("#<%=lblTypeNameMessage.ClientID %>").attr("class", "f_annotate");
                return;
            }
            if (state == -1)
            {
                $("#<%=lblTypeNameMessage.ClientID %>").html(text);
                $("#<%=lblTypeNameMessage.ClientID %>").attr("class", "f_import");
                return;
            }
        }
        if(type=="Radio")
            {
               if (state == 0)
                {
                    $("#ratype").html("请选择地址类型。");
                    $("#ratype").attr("class", "f_annotate");
                    return;
                }
                if (state == -1)
                {
                    $("#ratype").html(text);
                    $("#ratype").attr("class", "f_import");
                    return;
                }
            }
        if (type == "ContactPerson")
        {
            if (state == 0)
            {
                $("#lblContactPersonMessage").html("请如实填写您的信息，以便送餐员方便找到您，长度在10个汉字以内。");
                $("#lblContactPersonMessage").attr("class", "f_annotate");
                return;
            }
            if (state == -1)
            {
                $("#lblContactPersonMessage").html(text);
                $("#lblContactPersonMessage").attr("class", "f_import");
                return;
            }
        }
        if (type == "ContactTel")
        {
            if (state == 0)
            {
                $("#lblContactTelMessage").html("请填写您的手机号码以便送餐时方便联系您。");
                $("#lblContactTelMessage").attr("class", "f_annotate");
                return;
            }
            if (state == -1)
            {
                $("#lblContactTelMessage").html(text);
                $("#lblContactTelMessage").attr("class", "f_import");
                return;
            }
        }
        if(type=="ContactPhone")
        {
            if (state == 0)
            {
                $("#lblContactPhoneMessage").html("请填写您的固定电话以便送餐时方便联系您。");
                $("#lblContactPhoneMessage").attr("class", "f_annotate");
                return;
            }
            if (state == -1)
            {
                $("#lblContactPhoneMessage").html(text);
                $("#lblContactPhoneMessage").attr("class", "f_import");
                return;
            }
        }
        if(type=="Contact")
        {
            if (state == 0)
            {
                $("#lblContactTelMessage").html("");
                $("#lblContactTelMessage").attr("class", "f_annotate");
                $("#lblContactPhoneMessage").html("");
                $("#lblContactPhoneMessage").attr("class", "f_annotate");
                return;
            }
            if (state == -1)
            {
                $("#lblContactTelMessage").html("与固定电话两者间至少填一项。");
                $("#lblContactTelMessage").attr("class", "f_import");
                $("#lblContactPhoneMessage").html("与手机号码两者间至少填一项。");
                $("#lblContactPhoneMessage").attr("class", "f_import");
                return;
            }
        
        }
        if (type == "Area")
        {
            if (state == 0)
            {
                $("#lblAreaMessage").html("选择您最近楼宇/小区，如果没有可以联系店主确认是否可以送达。");
                $("#lblAreaMessage").attr("class", "f_annotate");
            }
            if (state == -1)
            {
                $("#lblAreaMessage").html(text);
                $("#lblAreaMessage").attr("class", "f_import");
                return;
            }
        }
        if (type == "Address")
        {
            if (state == 0)
            {
                $("#lblAddressMessage").html("例如：10幢1号101室、4幢A单元101室、2号楼10楼7A室。");
                $("#lblAddressMessage").attr("class", "f_annotate");
            }
            if (state == -1)
            {
                $("#lblAddressMessage").html(text);
                $("#lblAddressMessage").attr("class", "f_import");
                return;
            }
        }
    }
    
    //checks
    function checkTypeName()
    {
        if ($.trim($("#<%=tbTypeName.ClientID%>").val()) == "")
        {
            setMessage("TypeName", -1, "请填写地址类型！");
            return false;
        }
        setMessage("TypeName", 0);
        return true;
    }
    function checkContactPerson()
    {
        if ($.trim($("#<%=tbContactPerson.ClientID%>").val()) == "")
        {
            setMessage("ContactPerson", -1, "请填写联系人名称！");
            return false;
        }
        else
        {
            setMessage("ContactPerson", 0);
            return true;
        }
    }
    function filtrateTelChar(e)
    {
        var evt = window.event || e;
	    var keyCode = evt.keyCode || evt.which;
	    if (keyCode < 48 || keyCode > 57)
	    {
	        if (keyCode != 8 && keyCode != 46 && keyCode != 37 && keyCode != 39)
	            return false;
	        else
	            return true;
	    }
	    return true;
    }
    function checkContactPhone()
    {
        
        var phone = $("#<%=tbContactPhone.ClientID %>").val();
        var subPhone = $("#<%=tbContactSubPhone.ClientID %>").val();
        if (phone == "" && subPhone != "")
        {
            setMessage("ContactPhone", -1, "请填写总机号码！");
            return false;
        } if (phone != "")
        {
            var pattern = /^[0-9]{8}$/;
            if (pattern.test(phone) == false)
            {
                setMessage("ContactPhone", -1, "请填写8位的固定电话号码！");
                return false;
            }
        }
        setMessage("ContactPhone", 0);
        return true;
    }
    function checkContact()
    {
        var phone = $("#<%=tbContactPhone.ClientID %>").val();
        var mobile = $("#<%=tbContactMobile.ClientID %>").val();
        if (phone == "" && mobile == "")
        {
            setMessage("Contact", -1, "固定电话和手机至少要填其中一个！");
            return false;
        }
        setMessage("Contact", 0);
        return true;
    }
    function checkContactTel()
    {
        var mobile = $("#<%=tbContactMobile.ClientID %>").val();
        if (mobile != "")
        {
            var pattern = /^[0-9]{11}$/;
            if (pattern.test(mobile) == false)
            {
                setMessage("ContactTel", -1, "请填写11位的手机号码！");
                return false;
            }
        }
        setMessage("ContactTel", 0);
        return true;
    }
    function checkArea()
    {
        if (areaId == 0)
        {
            setMessage("Area", -1, "请选择送餐楼宇/小区！");
            return false;
        }
        else
        {
            setMessage("Area", 0);
            return true;
        }
    }
    function checkAddress()
    {
        if ($.trim($("#<%=tbAddress.ClientID%>").val()) == "")
        {
            setMessage("Address", -1, "请填写精确地址！");
            return false;
        }
        else
        {
            setMessage("Address", 0);
            return true;
        }
    }
    
    function checkForm()
    {
        var result = true;
        if (checkContactPerson() == false) result = false;
        if (checkContact() == false) result = false;
        else
        {
            if (checkContactTel() == false) result = false;
            if (checkContactPhone() == false) result = false;
        }
        if (checkArea() == false) result = false;
        if (checkAddress() == false) result = false;
        return result;
    }
    
    //submit
    function submitForm()
    {
        if (checkForm() == false || checkRadio()==false) return;
        var TypeName = $("#<%=tbTypeName.ClientID%>").val();
        var ContactPerson = $("#<%=tbContactPerson.ClientID%>").val();
        var ContactPhone = $("#<%=tbContactPhone.ClientID%>").val();
        var ContactSubPhone = $("#<%=tbContactSubPhone.ClientID%>").val();
        if (ContactSubPhone != "") ContactPhone = ContactPhone + "-" + ContactSubPhone;
        var ContactMobile = $("#<%=tbContactMobile.ClientID%>").val();
        var AreaId = areaId;
        var Address = $("#<%=tbAddress.ClientID%>").val();
        if (window.confirm("您确定要保存此信息吗？") == false) return;
        $("#btnBackToList").attr("disabled", true);
        $("#btnSubmit").attr("disabled", true);
        User_UserAddressEdit.SaveAddressEdit(Id, TruetypeName, ContactPerson, ContactPhone, ContactMobile, AreaId, Address, submitForm_Callback);
    }
    
    function submitForm_Callback(res)
    {
        $("#btnBackToList").attr("disabled", false);
        $("#btnSubmit").attr("disabled", false);
        var ret = res.value;
        if (ret == null && typeof(ret) != "object")
        {
            alert("保存失败，请尝试再次提交！");
            return;
        }
        if (ret == false)
        {
            alert("保存失败，请尝试再次提交！");
            return;
        }
        alert("保存成功！");
        window.location='/User/UserAddressList.aspx';
    }
    </script>

</asp:Content>
