var Profile_Edit ={
    _basicProfile : null,
    initialize : function(basicProfile,isVerified){
        this._basicProfile = basicProfile;        
        //'基本信息'
        //BasicInfo.initialize(this._basicProfile);
        //'个人信息'
        PersonalInfo.initialize(this._basicProfile);
        //'联系方式'
        Contacts.initialize(this._basicProfile,isVerified);
    },
    show : function(){
        //显示 '基本信息'
        //BasicInfo.showBasicInfo();
        //蒙恢效果div初始化
        //BasicInfo.basicInfoBackGray();
        //显示 '个人信息'
        PersonalInfo.showPersonalInfo();
        //蒙恢效果div初始化
        //PersonalInfo.personalInfoBackGray();
        //显示 '联系方式'
        //Contacts.showContactsInfo();
        //蒙恢效果div初始化
        //Contacts.contactsInfoBackGray();
        //password界面显示
        //$('password_labeldiv').style.display ="";
        //$('Password').style.display ="";
        $('password_edit').style.display ="";
        
    },
    //profile_edit.jsp 产生蒙恢效果的按钮事件  
    backGrayButtonClick : function(index){   
        switch(index){
   
            //'基本信息'蒙恢效果
            case 1 :
                BasicInfo.showEditBasicInfo();
                ////new BackGray().action('PersonalEdit', $('profileBasicInfo_backGray').innerHTML);
                //BasicInfo.basicInfoBackGray();
                BasicInfo.autocomplete_BasicInfo();
                //BasicInfo.autocomplete_Industry();
                BasicInfo.location_Message(true);
                break;
            //'个人信息'蒙恢效果
            case 2 :
                PersonalInfo.showEditPersonalInfo();
                ////new BackGray().action('PersonalInformationEdit', $('profilePersonalInfo_backGray').innerHTML);
                //PersonalInfo.personalFillDataBackGray();//填充蒙恢效果
                PersonalInfo.autocomplete_Personal();
                PersonalInfo.personalInfo_Message(true);
                PersonalInfo.skipNextText();
                break;
            //'联系方式'蒙恢效果
            case 3 :
                Contacts.showEditContactsInfo();
                ////new BackGray().action('EmailEdit', $('profileEmailInfo_backGray').innerHTML);
                Contacts.phone_Message(true);
                break;
            //'修改密码'蒙恢效果
            case 4 :
                Password.showEdit();//new BackGray().action('PasswordEdit', $('Template_profilePasswordInfo_backGray').innerHTML);
                Password.newPassword_Message(true);
                break;
        }
       
    }
 
};

//基本信息
var BasicInfo = {
    _otherName  : null,
    _name       : null,
    _gender     : null,
    _screenName : null,
    _location   : null,
    //  _industry   : null,
    //  _company   : null,
    //  _occupation   : null,
    _link_value : null,
    _addBasic_location : null,
    _addBasic_industry : null,

    initialize : function(basicProfile){
        if(basicProfile.otherName == null || basicProfile.otherName.trim().length == 0){
            this._otherName = "未填写";
        }else{
            this._otherName = basicProfile.otherName;
        }
        this._name = basicProfile.name;
        this._gender = basicProfile.gender;
        this._screenName = basicProfile.screenName;
        this._location = basicProfile.location;
        //  if(basicProfile.company == null || basicProfile.company == ""){
        //      this._company = "未填写";
        //  }else{
        //      this._company = basicProfile.company;
        //  }
        //  if(basicProfile.occupation == null || basicProfile.occupation == ""){
        //      this._occupation = "未填写";
        //  }else{
        //      this._occupation = basicProfile.occupation;
        //  }
        //  this._industry = basicProfile.industry;
        this._link_value = this._screenName;
    },
    _updateShowBasicInfo : function(){
        var obj = new Element("div",{
            "id": "edit_profile_div"
        });
        $('Personal').appendChild(obj);
        BasicInfo.showBasicInfo();
    },
    showBasicInfo : function(){
        $('basicinfo_labeldiv').style.display ="";
        $('edit_profile_div').insert(this._fillBasicTemplate());
    },
    hideEditBasicInfo : function(){
        $('profile_div_edit').style.display="none";
        $('edit_profile_div').style.display="";
    },
    _fillBasicTemplate : function(){
        var myObj = this._createBasicTemplate();
        var fillResult = myObj.evaluate(BasicInfo._basicFillData());
        return fillResult;
    },
    _createBasicTemplate : function(){
        var myObjHtml = $('Template_profileBasic_table').innerHTML.templateDecode();
        var myObj = new Template(myObjHtml);
        return myObj;
    },
    showEditBasicInfo : function(){
        $('edit_profile_div').style.display="none";
        $('profile_div_edit').style.display="";
        this._basicFillValue();
    },
    _basicFillValue : function(){
        var thiz = this;
        var bgender = null;
        if(BasicInfo._gender == 1)
            bgender = "男";
        else
            bgender = "女";
        var basicInfo_sp = $('profile_div_edit').select('select');
        basicInfo_sp.each(function(b){
            if(b.id == "gender"){
                if(BasicInfo._gender == 1 || bgender == "男"){
                    b.selected = "男";
                    b.value ="男";
                }else if(BasicInfo._gender == 0 || bgender == "女"){
                    b.selected = "女";
                    b.value ="女"
                }
            }
        });
        $("henglinkvalue").update(this._screenName) ;
        $("linkvalue").value = this._screenName;
        $("username").value = this._name;
        if(this._otherName ==  "未填写"){
            $("other_name").value = "";
        }else{
            $("other_name").value = this._otherName;
        }
        //    if(this._company == "未填写"){
        //       $("company").value = "";
        //    }else{
        //       $("company").value = this._company;
        //    }
        //    if(this._occupation == "未填写"){
        //        $("occupation").value = "";
        //    }else{
        //        $("occupation").value = this._occupation;
        //    }
        $("location").value = this._location;
    //$("industry").value = this._industry;
    },
    //基本信息要的数据
    _basicFillData : function(){
        var hash = new Hash();
        var thiz = this;
        var bgender = null;
        if(BasicInfo._gender == 1)
            bgender = "男";
        else
            bgender = "女";

        if($('profile_div_edit') != null){
            var basicInfo_sp = $('profile_div_edit').select('select');
            basicInfo_sp.each(function(b){
                if(b.id == "gender"){
                    if(BasicInfo._gender == 1 || bgender == "男"){
                        b.selected = "男";
                        b.value ="男";
                    }else if(BasicInfo._gender == 0 || bgender == "女"){
                        b.selected = "女";
                        b.value ="女"
                    }
                }
            });

        }else{

            var basicInfo_input1 = $('Template_PersonalEditSrc').select('input');
            basicInfo_input1.each(function(d){

                if(d.id == "username"){
                    d.setValue(thiz._name);
                }
                if(d.id == "other_name"){
                    d.setValue(thiz._otherName);
                }
                //        if(d.id == "company"){
                //            d.setValue(thiz._company);
                //        }
                //        if(d.id == "occupation"){
                //            d.setValue(thiz._occupation);
                //        }
                if(d.id == "location"){
                    d.setValue(thiz._location);
                }
            //        if(d.id == "industry"){
            //            d.setValue(thiz._industry);
            //        }
            });
        }

        hash.set("otherName",this._otherName);
        hash.set("name",this._name);
        hash.set("gender",bgender);
        hash.set("screenName",this._screenName);
        //  hash.set("company" , this._company);
        //  hash.set("occupation" , this._occupation);
        hash.set("location",this._location);
        //hash.set("industry" , this._industry);
        //hash.set("bid",Math.random()*1000);
        return hash;
    },
    
    //蒙恢效果div初始化其数据
    basicInfoBackGray : function(){
        
        var myObjHtml = $('Template_PersonalEditSrc').innerHTML.templateDecode();
        var myObj = new Template(myObjHtml);
 
        $('profileBasicInfo_backGray').update(myObj.evaluate(BasicInfo._basicFillData()));
    },
    //根据input有值同时改变screenName
    sameTimeChangeValue : function(){
      
        var basicInfo_input = $('profile_div_edit').select('input');
        basicInfo_input.each(function(b){
            if(b.id == "linkvalue")
                BasicInfo._link_value = b.getValue();
        });
        var basicInfo_span = $('profile_div_edit').select('span');
        basicInfo_span.each(function(b){
            if(b.id == "henglinkvalue")
                b.update(BasicInfo._link_value);
        });
    },
    //location的autocomplete
    autocomplete_BasicInfo : function(){
        var locationdiv  = null;
        var locationinput = null;
        var personal_div4 = $('profile_div_edit').select('div');
        personal_div4.each(function(d){
            if(d.id == "cityChoices_Location")
                locationdiv = d;
        });
        var personal_input4 = $('profile_div_edit').select('input');
        personal_input4.each(function(d){
            if(d.id == "location")
                locationinput = d;
        });
   
        this._addBasic_location = new RemoteAutocompleter(locationinput,locationdiv,'/Profile.do?action=GET_AUTOCOMP&categoryType=20&maxCount=10');
    },
    //industry的autocomplete
    autocomplete_Industry : function(){
        var companyndiv  = null;
        var companyinput = null;
        var personal_div4 = $('profile_div_edit').select('div');
        personal_div4.each(function(d){
            if(d.id == "companyChoices")
                companyndiv = d;
        });
        var personal_input4 = $('profile_div_edit').select('input');
        personal_input4.each(function(d){
            if(d.id == "company")
                companyinput = d;
        });
        this._addBasic_industry = new RemoteAutocompleter(companyinput,companyndiv,'/Profile.do?action=GET_AUTOCOMP&categoryType=40&maxCount=10');
    },
    //当城市input为空时,提供预设信息
    pre_Message : function(){
   
        var cityDiv = null;
        var personal_div14 = $('profile_div_edit').select('div');
        personal_div14.each(function(d){
            if(d.id == "cityChoices_Location")
                cityDiv = d;
        });
        if(cityDiv != null){
            //cityDiv.update('北京, 上海, 广州, 深圳, 天津, 青岛');
            new LocalAutocompleter.Local('location','cityChoices_Location');
        
        }
    
    
    },
    //检测screenName事件
    checkClick : function(){
        //清除messge
        BasicInfo._cleanMessage_BasicInfo();
        var Hlink = $("HengZhiLink");
        if(BasicInfo._link_value.strip().empty()){
            Hlink.style.display ="";
            Hlink.update("恒知链接不能为空!");
            Hlink.className = 'wrong_message';
            return;
        }
        if(BasicInfo._link_value.match(/[^A-Za-z0-9._-]/) != null || BasicInfo._link_value.length > 200){
            Hlink.style.display ="";
            Hlink.update("恒知链接只能包含数字,字母,.,_,-,长度不能超过200");
            Hlink.className = 'wrong_message';
            return;
        }

        
        new Ajax.Request('/Profile.do?action=CHECK_SCREEN_NAME',{
            parameters : {
                "screenName":BasicInfo._link_value
            },
            requestHeaders :{
                Accept :"application/json"
            },
            onSuccess:function(transport){
                var json = transport.responseText.evalJSON(true);
                if(json.RETURN){
                    var basicInfo_div = $('profile_div_edit').select('div');
                    basicInfo_div.each(function(d){
                        if(d.id == "HengZhiLink"){
                            d.style.display ="none";
                        }
                        if(d.id == "okMessagePersonal"){
                            d.style.display ="";
                            d.update("恒知链接可以使用!");
                            d.className = 'ok_message';
                        }
                    });
                }else{
                    var basicInfo_div2 = $('profile_div_edit').select('div');
                    basicInfo_div2.each(function(d){
                        if(d.id == "HengZhiLink"){
                            d.style.display ="";
                            d.update("恒知链接已经存在!");
                            d.className = 'wrong_message';
                            throw $break;
                        }
     
                    });
                }
            }.bind(this)
        });
    },
    //basicInfo提示信息
    location_Message : function(isOk){
        var contact_span1 = $('profile_div_edit').select('input');
        contact_span1.each(function(s){
            if(s.id =="location"){
                new Popup(s,"请输入拼音,中文或英文,恒知将自动提示");
            }
            if(s.id =="company")
                new Popup(s,"请输入拼音,中文或英文,恒知将自动提示");
            if(s.id =="other_name")
                new Popup(s,"您的其他称谓，如： 英文或昵称");
            if(s.id =="linkvalue")
                new Popup(s,"恒知链接名是您在恒知网的独有标识, 请选择字母和数字的组合");
        });
    },
    industry_Message : function(isOk){
        var contact_span1 = $('profile_div_edit').select('span');
        contact_span1.each(function(s){
            if(s.id =="industry_message")
                if(isOk)
                    s.style.display ="";
                else
                    s.style.display ="none";
        });
    },
    //保存事件点击
    basicInfoSaveEvent : function(){
        
        var username   = null;
        var gender     = null;
        var other_name = null;
        var location   = null;
        var industry   = null;
        var company   = null;
        var occupation   = null;
        var link_value = null;
        var locationId = null;
        var industryId = null;
        var hzlink = $("HengZhiLink");
        BasicInfo._cleanMessage_BasicInfo();
        var basicInfo_select = $('profile_div_edit').select('select');
        basicInfo_select.each(function(b){
            if(b.id == "gender"){
                if(b.getValue() == "男")
                    gender = 1;
                else if(b.getValue() == "请选择"){
                    gender = 100;
                }
                else if(b.getValue() == "女")
                    gender = 0;
            }
        });
        var basicInfo_input = $('profile_div_edit').select('input');
        basicInfo_input.each(function(b){
            if(b.id == "username")
                username = b.getValue();
            if(b.id == "other_name")
                other_name = b.getValue();
            if(b.id == "location"){
                location   = b.getValue();
                locationId = BasicInfo._addBasic_location.getId(location);
            }
            //    if(b.id == "industry"){
            //       industry   = b.getValue();
            //       industryId = BasicInfo._addBasic_industry.getId(industry);
            //    }
            //    if(b.id == "company"){
            //       company   = b.getValue();
            //    }
            //    if(b.id == "occupation"){
            //       occupation   = b.getValue();
            //    }
            if(b.id == "linkvalue"){
                BasicInfo._link_value = b.getValue();
                link_value  = b.getValue();
            }
        });

        if(gender == 100){
            hzlink.style.display ="";
            hzlink.className = 'wrong_message';
            hzlink.update("请选择性别");
            return;
        }
        if(BasicInfo._link_value.strip().empty() == false){
            if(BasicInfo._link_value.match(/[^A-Za-z0-9._-]/) != null || BasicInfo._link_value.length > 200){
                hzlink.style.display ="";
                hzlink.className = 'wrong_message';
                hzlink.update("恒知链接只能包含数字,字母,.,_,-,长度不能超过200");
                return;
            }
        }
        if(username.strip().empty()  ||  BasicInfo._link_value.strip().empty()){//|| industry.strip().empty()){
            hzlink.style.display ="";
            hzlink.className = 'wrong_message';
            if(username.strip().empty()){
                hzlink.update("姓名不能为空");
                return;
            }
            if(BasicInfo._link_value.strip().empty()){
                hzlink.update("恒知链接不能为空!");
                return;
            }

        }else{
            hzlink.update("");
            hzlink.style.display ="none";
        }

       
        var profileJson = new Hash();
        profileJson.set("name",username);
        profileJson.set("otherName",other_name);
        profileJson.set("gender",gender);
        profileJson.set("screenName",BasicInfo._link_value);
        profileJson.set("location",location);
        //    profileJson.set("industry",industry);
        //    profileJson.set("company",company);
        //    profileJson.set("occupation",occupation);
        if(locationId != null)
            profileJson.set("locationId",locationId);
        //    if(industryId != null)
        //      profileJson.set("industryId",industryId);
        //alert("location ="+locationId+"  industryId="+industryId);
        new Ajax.Request('/Profile.do?action=CHANGE_PROFILE',{
            parameters : {
                "profileJson":profileJson.toJSON()
            },
            requestHeaders :{
                Accept :"application/json"
            },
            onSuccess:function(transport){
                var json = transport.responseText.evalJSON(true);
                if(json.RETURN){
                    document.location.href="/profile";
                }else{
                    var basicInfo_div = $('profile_div_edit').select('div');
                    basicInfo_div.each(function(d){
                        if(d.id == "HengZhiLink"){
                            d.style.display ="";
                            d.update(json.MESSAGE);
                            d.className = 'wrong_message';
                            return;
                        }
                    });
                }
            }.bind(this)
        });
    },
    _cleanMessage_BasicInfo : function(){
        var basicInfo_div = $('profile_div_edit').select('div');
        basicInfo_div.each(function(d){ 
            if(d.id == "HengZhiLink")
                d.style.display ="none";
            if(d.id == "okMessagePersonal")
                d.style.display ="none";
        });
    }
};
//个人信息
var PersonalInfo ={
    _birthDay        : null,
    _birthPlace      : null,
    _addPersoal_city : null,
    _year            : null,
    _month           : null,
    _day             : null,
    initialize : function(basicProfile){
        var dateStr = PersonalInfo._changeDate(basicProfile.birthDay); //date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();
        this._birthDay = dateStr;
        this._birthPlace = basicProfile.birthPlace;
    },
    _updatePersonalData : function(birthDay,birthPlace){
        var dateStr = PersonalInfo._changeDate(birthDay);
        this._birthDay = dateStr ;
        this._birthPlace = birthPlace;
    },
    //把日期转换成2008-04-04这个样式
    _changeDate : function(birthDay){
        var date = new Date();
        var dateStr = ""; //date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();
        if(birthDay != null){
            date.setTime(birthDay);

            date = PersonalInfo.calcTime("+8",date);


            var moth = date.getMonth()+1;
            var day  = date.getDate();
            if(moth <10)
                moth = "0"+moth;
            if(date.getDate()<10)
                day = "0"+day;
            dateStr = date.getFullYear()+"--"+moth+"--"+day;
            PersonalInfo._year = date.getFullYear();
            PersonalInfo._month = moth;
            PersonalInfo._day   = day;
        }
        return dateStr;
    },
    calcTime : function(offset,date) {
        var utc = date.getTime() + (date.getTimezoneOffset() * 60000);
        var nd = new Date(utc + (3600000*offset));
        return  nd;
    }
    ,
    showPersonalInfo : function(){
        //$('personalinfo_labeldiv').style.display = "";
        $('edit_personal_info').insert(this._fillPersonalTemplate());
    //PersonalInfo._personalFillDataBackGray();
    },
    _fillPersonalTemplate : function(){
        var myObj = this._createPersonalTemplate();
        var fillResult = myObj.evaluate(PersonalInfo._personalFillData());
        return fillResult;
    },
    _createPersonalTemplate : function(){
        var myObjHtml = $('Template_ProfilePersonal_table').innerHTML.templateDecode();
        var myObj = new Template(myObjHtml);
        return myObj;
    },
    //创建template所要的数据
    _personalFillData : function(){
        var hash = new Hash();
        hash.set("birthDay",PersonalInfo._year+" 年 "+PersonalInfo._month+" 月 "+PersonalInfo._day+ " 日");
        if(PersonalInfo._year < 1500){
            hash.set("birthDay","未填写");
        }
        hash.set("birthPlace",this._birthPlace);
        if(this._birthPlace == null || this._birthPlace.trim().length==0 ){
            hash.set("birthPlace","未填写");
        }
        return hash;
    },
    //先删除页面上的旧数据，后显示新的数据
    _updatePersonalInfo : function(){
        var obj = new Element("div",{
            "id": "edit_personal_info"
        });
        $('PersonalInformation').appendChild(obj);
        PersonalInfo.showPersonalInfo();
    },
    //蒙恢效果div初始化其数据
    personalInfoBackGray : function(){
        var myObjHtml = $('Template_PersonalInformationEditSrc').innerHTML.templateDecode();
        var myObj = new Template(myObjHtml);
        $('profilePersonalInfo_backGray').update(myObj.evaluate(PersonalInfo.personalFillDataBackGray()));
    },
    showEditPersonalInfo : function(){
        $("personalInfoErrorMessage").style.display="none";
        $('edit_personal_info').style.display="none";
        $('personal_info_edit').style.display="";
        this._personalFillValue();
    },
    hideEditPersonalInfo : function(){
        $('personal_info_edit').style.display="none";
        $('edit_personal_info').style.display="";

        var personal_a14 = $('personal_info_edit').select('input');
        personal_a14.each(function(d){
            if(d.id =="birthYear" ){
                d.style.color='#999';
                d.value='yyyy'
            }
            if(d.id =="birthMonth"){
                d.style.color='#999';
                d.value='mm'
            }
            if(d.id =="birth_day" ){
                d.style.color='#999';
                d.value='dd'
            }
            if(d.id =="birthPlace"){
                d.setValue("");
            }
        });
    },
    autocomplete_Personal : function(){
        var citydiv  = null;
        var cityinput = null;
        var personal_div4 = $('personal_info_edit').select('div');
        personal_div4.each(function(d){
            if(d.id == "cityChoicess")
                citydiv = d;
        });
        var personal_input4 = $('personal_info_edit').select('input');
        personal_input4.each(function(d){
            if(d.id == "birthPlace")
                cityinput = d;
        });

        this._addPersoal_city = new RemoteAutocompleter(cityinput,citydiv,'/Profile.do?action=GET_AUTOCOMP&categoryType=20&maxCount=10');
    },
    skipNextText : function(){
        var year  = null;
        var month = null;
        var day   = null;
        var bplace = null;
        var personal_a5 = $('personal_info_edit').select('input');
        personal_a5.each(function(d){
            if(d.id =="birthYear"){
            }
            if(d.id =="birthMonth"){
                month = d;
            }
            if(d.id =="birth_day"){
                day = d;
            }
            if(d.id =="birthPlace")
                bplace = d;
        });
        if(year!= null){
            year.observe("keyup", function(e){
                if(!isNaN(year.value) && year.value.length >= 4){
                    month.focus();
                }
            });
        }
        if(month !=null){
            month.observe("keyup", function(e){
                if(!isNaN(month.value) && month.value.length >= 2){
                    day.focus();
                }
            });
        }
        if(day != null){
            day.observe("keyup", function(e){
                if(!isNaN(day.value) && day.value.length >= 2){
                    bplace.focus();
                }
            });
        }
    },
    _personalFillValue : function(){
        var birth = this._birthDay;
        var birthPlace = this._birthPlace;
        var birthYear ="";
        var birthMonth ="";
        var birth_day ="";
        if(birth !=""){
            var bb = birth.split("--");
            if(bb[1].length ==1)
                bb[1] ="0"+bb[1];
            if(bb[2].length == 1)
                bb[2] = "0"+bb[2];
            birthYear =bb[0];
            birthMonth =bb[1];
            birth_day =bb[2];
        }
  
        var personal_a4 = $('personal_info_edit').select('input');
        personal_a4.each(function(d){
            if(d.id =="birthYear" && birthYear > 1500){
                d.setValue(birthYear);
            }
            if(d.id =="birthMonth" && birthYear > 1500){
                d.setValue(birthMonth);
            }
            if(d.id =="birth_day" && birthYear > 1500){
                d.setValue(birth_day);
            }
            if(d.id =="birthPlace"){
                d.setValue(birthPlace);
            }
        });
    },
    //关于生日的处理 要生日字符串分成 年，月，日  然后把其放到<options>中
    personalFillDataBackGray : function(){
        var hash = new Hash();
        var birth = this._birthDay;
        var birthPlace = this._birthPlace;
        var birthYear ="";
        var birthMonth ="";
        var birth_day ="";
        if(birth !=""){
            var bb = birth.split("--");
            if(bb[1].length ==1)
                bb[1] ="0"+bb[1];
            if(bb[2].length == 1)
                bb[2] = "0"+bb[2];
            birthYear =bb[0];
            birthMonth =bb[1];
            birth_day =bb[2];
        }
        //alert("birthYear ="+birthYear+"birthMonth ="+birthMonth+"birth_day ="+birth_day);
        if($('personal_info_edit') != null){
            var personal_a4 = $('personal_info_edit').select('input');
            personal_a4.each(function(d){
                if(d.id =="birthYear"){
                    d.setValue(birthYear);
                }
                if(d.id =="birthMonth"){
                    d.setValue(birthMonth);
                }
                if(d.id =="birth_day"){
                    d.setValue(birth_day);
                }
                if(d.id =="birthPlace"){
                    d.setValue(birthPlace);
                }
            });
        }else{
      
            var personal_a5 = $('Template_PersonalInformationEditSrc').select('input');
            personal_a5.each(function(d){
                if(d.id =="birthYear"){
                    d.setValue(birthYear);
                }
                if(d.id =="birthMonth"){
                    d.setValue(birthMonth);
                }
                if(d.id =="birth_day"){
                    d.setValue(birth_day);
                }
                if(d.id =="birthPlace"){
                    d.setValue(birthPlace);
                }
            });//*/
        //$('birthYear').setValue(birthYear);
        //$('birthMonth').setValue(birthMonth);
        //$('birth_day').setValue(birth_day);
        }
        //hash.set("birthYear",birthYear);
        //hash.set("birthMonth",birthMonth);
        //hash.set("birth_day",birth_day);
        //hash.set("birthPlace",this._birthPlace);alert(this._birthPlace);
        return hash;
    },
    //PersonalInfo提示信息
    personalInfo_Message : function(isOk){
        var contact_span = $('personal_info_edit').select('input');
        contact_span.each(function(s){
            if(s.id =="birthPlace"){
                new Popup(s,"请输入拼音,中文或英文,恒知将自动提示");
            //  if(isOk){
            //    s.style.display ="";
            // }else{
            //      s.style.display ="none";
            // }
            }
        });
    },
    //保存事件的点击
    personalSaveEvent : function(){
        var birthYear = null;
        var birthMonth = null;
        var birth_day = null;
        var birthPlace = null;
        var locationId = null;
        var personalError = $("personalInfoErrorMessage");
        //取值
        PersonalInfo._cleanMessage_Personal();
        var personal_select = $('personal_info_edit').select('input');
        personal_select.each(function(p){
            if(p.id =="birthYear"){
                birthYear = p.getValue();
            }
      
            if(p.id == "birthMonth"){
                birthMonth = p.getValue();
            }
        
            if(p.id == "birth_day"){
                birth_day = p.getValue();
            }
        });
        //取值
        var personal_input = $('personal_info_edit').select('input');
        personal_input.each(function(p){
            if(p.id == "birthPlace"){
                birthPlace = p.getValue();
                locationId = PersonalInfo._addPersoal_city.getId(birthPlace);
            }
        
        });

        if(birthYear.strip() =="yyyy" && birthMonth.strip() =="mm" && birth_day.strip() =="dd"){
            birthYear = "1111";
            birthMonth = "11";
            birth_day = "11";
        }else{
            if(isNaN(birthYear) || birthYear.strip().length !=4 || isNaN(birthMonth) ||birthMonth.length >2 ||isNaN(birth_day) || birth_day.length > 2){
                personalError.style.display ="";
                personalError.update("请输入正确的日期格式");
                personalError.className = 'wrong_message';
                return;
            }
            if(birthYear < 1500){
                personalError.style.display ="";
                personalError.update("请输入正确的年份");
                personalError.className = 'wrong_message';
                return;
            }
            if(birthMonth > 12){
                personalError.style.display ="";
                personalError.update("请输入正确的月份");
                personalError.className = 'wrong_message';
                return;
            }
            if(birth_day >31){
                personalError.style.display ="";
                personalError.update("请输入正确的日期");
                personalError.className = 'wrong_message';
                return;
            }
        }
        var profileJson = new Hash();
        profileJson.set("birthYear",birthYear);
        profileJson.set("birthMonth",birthMonth);
        profileJson.set("birthDay",birth_day);
        profileJson.set("birthPlace",birthPlace.strip());
        if(locationId != null)
            profileJson.set("locationId",locationId);
        //alert("locationId ="+locationId);
        $('profile_edit_loading').show();
        new Ajax.Request('/Profile.do?action=CHANGE_PROFILE',{
            parameters : {
                "profileJson":profileJson.toJSON()
            },
            requestHeaders :{
                Accept :"application/json"
            },
            onSuccess:function(transport){
                var json = transport.responseText.evalJSON(true);
                if(json.RETURN){
                    //document.location.href="/profile/setting";
                    Password.modifyPassword();
                }else{
                    var personal_div = $('personal_info_edit').select('div');
                    personal_div.each(function(d){
                        if(d.id == "personalInfoErrorMessage"){
                            d.style.display ="";
                            d.update(json.MESSAGE);
                            d.className = 'wrong_message';
                            return;
                        }
                    });
                }
                $('profile_edit_loading').hide();
            }.bind(this)
        });
    },
    _cleanMessage_Personal : function(){
        var personal_div = $('personal_info_edit').select('div');
        personal_div.each(function(d){
            if(d.id == "personalInfoErrorMessage"){
                d.style.display ="none";
            }
        });
    }
};

//联系方式
var Contacts ={
    _phone      : null,
    _fixphone   : null,
    _email      : null,
    _isVerified : null,
    initialize : function(basicProfile,isVerified){
        if(basicProfile.phone == null){
            this._phone = "未填写";
        }else{
            this._phone = basicProfile.phone;
        }
        if(basicProfile.fixphone == null){
            this._fixphone = "未填写";
        }else{
            this._fixphone = basicProfile.fixphone;
        }
        this._email = basicProfile.email;
        this._isVerified = isVerified;
    },
    //  _updateContactData : function(phone,email){
    //  this._phone = phone;
    //  this._email = email;
    //  //thiz._contactsFillData();
    //  },
    showContactsInfo : function(){
        $('relationinfo_labeldiv').style.display ="";
        $('edit_email_info').insert(this._fillContactsTemplate());
    //this._contactFillValue();
    },
    _fillContactsTemplate : function(){
        var myObj = this._createContactsTemplate();
        var fillResult = myObj.evaluate(Contacts._contactsFillData());
        return fillResult;
    },
    _createContactsTemplate : function(){
        var myObjHtml = $('Template_ProfileEmail_table').innerHTML.templateDecode();
        var myObj = new Template(myObjHtml);
        return myObj;
    },
    _contactsFillData : function(){
        var hash = new Hash();
        if(this._isVerified){
            hash.set("phone",this._phone+" ( 已绑定 )");
        }else{
            hash.set("phone",this._phone+" ( <a href='/mobile_verify.jsp'>请确认</a> )");
        }
        hash.set("fixphone", this._fixphone);
        hash.set("email",this._email);
        return hash;
    },
    _contactFillValue : function(){
        $('email_text').value=this._email;
        if(this._phone == "未填写"){
            $('phone_my').value="";
        }else{
            $('phone_my').value=this._phone;
        }
        if(this._fixphone == "未填写"){
            $('fixphone_my').value="";
        }else{
            $('fixphone_my').value=this._fixphone;
        }
    },
    _updateContactsInfo : function(){
        var obj = new Element("div",{
            "id":"edit_email_info"
        });
        $('Registration').appendChild(obj);
        Contacts.showContactsInfo();
    },
    //蒙恢效果div初始化其数据
    contactsInfoBackGray : function(){
        var myObjHtml = $('Template_EmailEditSrc').innerHTML.templateDecode();
        var myObj = new Template(myObjHtml);
        $('profileEmailInfo_backGray').update(myObj.evaluate(Contacts._contactsFillData()));
    },
    showEditContactsInfo : function(){
      
        $("modifyContactDiv").style.display="none";
        $("edit_email_info").style.display="none";
        $("email_info_edit").style.display="";
        this._contactFillValue();
     
    },
    hideEditContactsInfo : function(){
        $("edit_email_info").style.display="";
        $("email_info_edit").style.display="none";
    },
    //清除message
    _cleanMessage_Contact : function(){
        var contact_div = $('email_info_edit').select('div');
        contact_div.each(function(d){
            if(d.id == "modifyContactDiv"){
                d.style.display ="none";
            }
        });
    },
    //phone提示信息
    phone_Message : function(isOk){
    
        var contact_input11 = $('email_info_edit').select('input');
        contact_input11.each(function(c){
            if(c.id =="phone"){
                new Popup(c,"未经您同意,恒知不会将您的联系方式透露给第三方");
            //if(isOk){
            //  c.style.display ="";
            // }else{
            //    c.style.display ="none";
            //}
            }
            if(c.id =="fixphone"){
                new Popup(c,"未经您同意,恒知不会将您的联系方式透露给第三方");
            //if(isOk){
            //  c.style.display ="";
            // }else{
            //    c.style.display ="none";
            //}
            }
        });
    },
    //得到错误提示的div对象
    _error_divObj : function(){
        var checkObj = null;
        var contact_span22 = $('email_info_edit').select('div');
        contact_span22.each(function(s){
            if(s.id =="modifyContactDiv")
                checkObj = s;
        });
        return checkObj;
    },
    //检查phone
    checkPhone : function(){
        var checkP = $("modifyContactDiv");
    
        var phoneNum = $("phone_my");
        if(phoneNum.value.empty()){
            checkP.update("请输入您的手机号码");
            checkP.style.display ="";
            checkP.className = 'wrong_message';
            return false;
        }
        if(phoneNum.value.length < 7 ){//|| !Contacts._isMobilePhone(p.value)){
            checkP.update('请输入正确的手机号码');
            checkP.style.display ="";
            checkP.className = 'wrong_message';
            return false;
        }
        return true;
    },
    _isMobilePhone : function(s){
        var flag;
        var mobilePhoneRegS=/^(013|015|8613|8615|13)\d{9}$/;
        if (!mobilePhoneRegS.exec(s))
            return false ;
        return true;
    },
    _isWords : function(s){
        var flag = true;
        var keyWordS=/^[A-Za-z0-9\u4e00-\u9fa5]+$/;
        if (!keyWordS.exec(s)) flag = false ;
        return flag ;
    },
    //检查email
    checkEmail : function(){
        var checkE = $("modifyContactDiv");
          
        var email_value = $("email_text");
        if(email_value.value.empty()){
            checkE.update("请输入您的邮箱地址");
            checkE.className = 'wrong_message';
            checkE.style.display ="";
            return false;
        }
        if(!Contacts._isSingleEmail(email_value.value)){
            checkE.update("请输入正确的邮箱地址");
            checkE.className = 'wrong_message';
            checkE.style.display ="";
            return false;
        }
     
        return true;
    },
    //email address 是否合法
    _isSingleEmail : function(s){
        var flag=true;
        var emailRegS=/^([a-zA-Z0-9_\-\.\+]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
        if (!emailRegS.exec(s))
            flag=false ;
        return flag ;
    },
    //保存事件
    contactsSaveEvent : function(){
       
        var phone = null;
        var fixphone = null;
        var email = null;
        var errorMsg = $("modifyContactDiv");
        //清除message
        Contacts._cleanMessage_Contact();
        //取值
        var contact_input = $('email_info_edit').select('input');
        contact_input.each(function(c){
            if(c.id =="phone_my")
                phone = c.getValue();
            if(c.id =="fixphone_my")
                fixphone = c.getValue();
            if(c.id == "email_text")
                email = c.getValue();
        });

        //check  Phone是否合法
        if(!Contacts.checkPhone())
            return;
  
        //check  Email是否合法
        if(!Contacts.checkEmail())
            return;
  
        var contactsJson = new Hash();
        contactsJson.set("phone",phone);
        contactsJson.set("fixphone",fixphone);
        contactsJson.set("email",email);
        new Ajax.Request('/Profile.do?action=CHANGE_PROFILE',{
            parameters : {
                "profileJson":contactsJson.toJSON()
            },
            requestHeaders :{
                Accept :"application/json"
            },
            onSuccess:function(transport){
                var json = transport.responseText.evalJSON(true);
                if(json.RETURN){
                    document.location.href="/profile/setting";
                }else{
                    errorMsg.style.display ="";
                    errorMsg.update(json.MESSAGE);
                    errorMsg.className = 'wrong_message';
                }
            }.bind(this)
        });
    }
}
//修改密码
var Password ={
    showEdit : function(){
        $("modifyPasswordDiv").style.display="none";
        $("Password").style.display="none";
        $("password_edit").style.display="";
    },
    hideEditInfo : function(){
        $("Password").style.display="";
        $("password_edit").style.display="none";
    },
    modifyPassword : function(){
        var oldPasswd = null;
        var newPasswd1 = null;
        var newPasswd2 = null;
        var birthYear = null;
        var birthMonth = null;
        var birth_day = null;
        var birthPlace = null;
        var locationId = null;
        var psErrorMsg = $("modifyPasswordDiv");
        
        PersonalInfo._cleanMessage_Personal();
        Password._cleanMessage_Passwork();
        var pass_input = $('password_edit').select('input');
        var param = new Hash();
        pass_input.each(function(p){
            if(p.type == "password"){
                if(p.id == "oldPasswd")
                    oldPasswd = p.getValue();
                if(p.id == "newPasswd1")
                    newPasswd1 = p.getValue();
                if(p.id == "newPasswd2")
                    newPasswd2 = p.getValue();
            }
             
        });
        if(oldPasswd == null ||oldPasswd==""){
            psErrorMsg.style.display ="";
            psErrorMsg.update("请输入当前密码");
            psErrorMsg.className = 'wrong_message';
            return;
        }
        if(newPasswd1 == null || newPasswd1 =="" ){
            psErrorMsg.style.display ="";
            psErrorMsg.update("请输入新密码");
            psErrorMsg.className = 'wrong_message';
            return;
        }
        if(newPasswd2 == null ||newPasswd2=="" ){
            psErrorMsg.style.display ="";
            psErrorMsg.update("请再次输入新密码");
            psErrorMsg.className = 'wrong_message';
            return;
        }
        if(newPasswd1 != newPasswd2){
            psErrorMsg.style.display ="";
            psErrorMsg.update("新密码 两次输入不同");
            psErrorMsg.className = 'wrong_message';
            return;
        }
        if(newPasswd1.length < 6){
            psErrorMsg.style.display ="";
            psErrorMsg.update("新密码 数字和字母的组合,不少于 6 个字符");
            psErrorMsg.className = 'wrong_message';
            return;
        }
        var personal_select = $('personal_info_edit').select('input');
        personal_select.each(function(p){
            if(p.id =="birthYear"){
                birthYear = p.getValue();
            }

            if(p.id == "birthMonth"){
                birthMonth = p.getValue();
            }

            if(p.id == "birth_day"){
                birth_day = p.getValue();
            }
        });
        var personal_input = $('personal_info_edit').select('input');
        personal_input.each(function(p){
            if(p.id == "birthPlace"){
                birthPlace = p.getValue();
                locationId = PersonalInfo._addPersoal_city.getId(birthPlace);
            }

        });
        //if(birthYear.strip() =="yyyy" && birthMonth.strip() =="mm" && birth_day.strip() =="dd"){
        //            psErrorMsg.style.display ="";
        //            psErrorMsg.update("请输入生日");
        //            psErrorMsg.className = 'wrong_message';
        //            return;
        //            birthYear = "1111";
        //            birthMonth = "11";
        //            birth_day = "11";
        //}else{
        if((birthYear != null && birthYear!="" && birthYear!='yyyy') || (birthMonth != null && birthMonth!="" && birthMonth!='mm') || (birth_day != null && birth_day!="" && birth_day!='dd') ){
            if(birthYear == null ||birthYear=="" ){
                psErrorMsg.style.display ="";
                psErrorMsg.update("请输入正确的日期格式");
                psErrorMsg.className = 'wrong_message';
                return;
            }
   
            if(birthMonth == null ||birthMonth=="" ){
                psErrorMsg.style.display ="";
                psErrorMsg.update("请输入正确的日期格式");
                psErrorMsg.className = 'wrong_message';
                return;
            }
            if(birth_day == null ||birth_day=="" ){
                psErrorMsg.style.display ="";
                psErrorMsg.update("请输入正确的日期格式");
                psErrorMsg.className = 'wrong_message';
                return;
            }
            if(isNaN(birthYear) || birthYear.strip().length !=4 || isNaN(birthMonth) ||birthMonth.length >2 ||isNaN(birth_day) || birth_day.length > 2){
                psErrorMsg.style.display ="";
                psErrorMsg.update("请输入正确的日期格式");
                psErrorMsg.className = 'wrong_message';
                return;
            }
            if(birthYear < 1500){
                psErrorMsg.style.display ="";
                psErrorMsg.update("请输入正确的年份");
                psErrorMsg.className = 'wrong_message';
                return;
            }
            if(birthMonth > 12){
                psErrorMsg.style.display ="";
                psErrorMsg.update("请输入正确的月份");
                psErrorMsg.className = 'wrong_message';
                return;
            }
            if(birth_day >31){
                psErrorMsg.style.display ="";
                psErrorMsg.update("请输入正确的日期");
                psErrorMsg.className = 'wrong_message';
                return;
            }
        }
        //        if(birthPlace == null ||birthPlace=="" ){
        //            psErrorMsg.style.display ="";
        //            psErrorMsg.update("请输入出生地");
        //            psErrorMsg.className = 'wrong_message';
        //            return;
        //        }
        param.set('oldPasswd', MD5(oldPasswd));
        param.set('newPasswd1', MD5(newPasswd1));
        param.set('newPasswd2', MD5(newPasswd2));
        var profileJson = new Hash();
        profileJson.set("birthYear",birthYear);
        profileJson.set("birthMonth",birthMonth);
        profileJson.set("birthDay",birth_day);
        profileJson.set("birthPlace",birthPlace.strip());
        if(locationId != null)
            profileJson.set("locationId",locationId);
        param.set('profileJson',profileJson.toJSON());
        new Ajax.Request('/Profile.do?action=CHANGE_PASSWD',{
            parameters : param.toQueryString(),
            requestHeaders :{
                Accept :"application/json"
            },
            onCreate: function(){
                $('profile_edit_loading').show();
                $('m_password_form').disable();
                psErrorMsg.style.display ="";
                psErrorMsg.update("Loading...");
                psErrorMsg.className = 'ok_message';
            },
            onSuccess:function(transport){
                var json = transport.responseText.evalJSON(true);
                if(json.RETURN){
                    //document.location.href = "/profile/setting";
                    psErrorMsg.style.display ="";
                    psErrorMsg.update("修改成功");
                    psErrorMsg.className = 'ok_message';
                }else{
                    psErrorMsg.style.display ="";
                    psErrorMsg.update(json.MESSAGE);
                    psErrorMsg.className = 'wrong_message';
                    $('m_password_form').enable();
                }
               
            },
            onComplete : function(){
                $('profile_edit_loading').hide();
            //$('pMoPassWord').style.display = "none";
            //$('m_password_form').enable();
            }
        });
      
    },
    //清除message
    _cleanMessage_Passwork : function(){
        var pass_div1 = $('password_edit').select('div');
        pass_div1.each(function(p){
            if(p.id == "modifyPasswordDiv"){
                p.style.display ="none";
            }
        });
    },
    newPassword_Message : function(isOk){
        var contact_span1 = $('password_edit').select('input');
        contact_span1.each(function(s){
            if(s.id =="newPasswd1")
                new Popup(s,"数字和字母的组合,不少于6个字符");
        // if(isOk)
        //   s.style.display ="";
        // else
        //  s.style.display ="none";
        });
    }
}

function addYear(ojb){
    for(i=1992;i>1950;i--){
        temp = document.createElement("option");
        temp.text=i;
        temp.value=i;
                   
        ojb.options.add(temp);
                    
    }
} 
function addMonth(ojb){  
    for(i=1;i<=12;i++){
        temp = document.createElement("option");
        temp.text=i;
        temp.value=i;
        ojb.options.add(temp);
    }
} 

function addDay(year,month,day){  
    var i=0;
    var personal_a5 = $('PersonalInformationEdit').select('select');
    //alert("size ="+personal_a5.size());
    personal_a5.each(function(sObj){
        if(sObj.id =="birth_day"){
            while(sObj.options.length > 0){
                sObj.removeChild(sObj.options[0]);
            }
        }
    });
           
    //var sObj=$('birth_day');//document.getElementById("birth_day");
               
                   
    if(month.value == '' || year.value == ''){
        return;
    }
                
    var isYear;
    if(year.value % 4 == 0 && year.value % 100 != 0){
        isYear = true;
    }else{
        isYear = false;
    }
    if(month.value == 1 || month.value == 3 || month.value == 5 || month.value == 7 || month.value == 8 ||
        month.value == 10 || month.value == 12){
        for(i=1;i<=31;i++){
            var temp = new Element("option");
            if(i<10)
                i ="0"+i;
            //temp = document.createElement("option");
            temp.text= i;
            temp.value=i;
            //day.options.add(temp);
            var personal_s11 = $('PersonalInformationEdit').select('select');
            //alert("size ="+personal_a5.size());
            personal_s11.each(function(sObj){
                if(sObj.id =="birth_day")
                    day.options.add(temp);
            });
        }
    }else if(month.value == 2){
        if(isYear == true){
            for(i=1;i<=29;i++){
                /* temp = document.createElement("option");
                            temp.text=i; 
                            temp.value=i; 
                            day.options.add(temp); 
                           
                           //temp = document.createElement("option"); */
                var temp1 = new Element("option");
                if(i<10)
                    i ="0"+i;
                temp1.text= i;
                temp1.value=i;
                //day.options.add(temp);
                var personal_s12 = $('PersonalInformationEdit').select('select');
                //alert("size ="+personal_a5.size());
                personal_s12.each(function(sObj){
                    if(sObj.id =="birth_day")
                        day.options.add(temp1);
                });
            }
        }else{
            for(i=1;i<=28;i++){
                /*temp = document.createElement("option");
                            temp.text=i; 
                            temp.value=i; 
                            day.options.add(temp); //*/
                var temp2 = new Element("option");
                if(i<10)
                    i ="0"+i;
                temp2.text= i;
                temp2.value=i;
                //day.options.add(temp);
                var personal_s13 = $('PersonalInformationEdit').select('select');
                //alert("size ="+personal_a5.size());
                personal_s13.each(function(sObj){
                    if(sObj.id =="birth_day")
                        day.options.add(temp2);
                });
            }
        }
    }else{
        for(i=1;i<=30;i++){
            /*temp = document.createElement("option");
                        temp.text=i; 
                        temp.value=i; 
                        day.options.add(temp); //*/
            var temp3 = new Element("option");
            if(i<10)
                i ="0"+i;
            temp3.text= i;
            temp3.value=i;
            //day.options.add(temp);
            var personal_s14 = $('PersonalInformationEdit').select('select');
            //alert("size ="+personal_a5.size());
            personal_s14.each(function(sObj){
                if(sObj.id =="birth_day")
                    day.options.add(temp3);
            });
        }
    }
} 

 
