function changeAvatar(user_name) {
    //var url = RESOURCE_HOST+"/avatar/upload/user?referInfo="+encodeURIComponent(BOOKMARK_HOST+"/user_mana2/upload_avatar_result");
    var url = RESOURCE_HOST+"/avatar/upload/user?id="+user_name+"&referInfo="+encodeURIComponent(BOOKMARK_HOST+"/user_mana2/upload_avatar_result");
    var w = window.open(url, null, "width=380,height=200,left=0,top=0,status=no,resizable=no");
}

function deleteAvatar() {
    try{
        var url = '/user_mana2/delete_avatar';
        new Ajax.Request(url,
        {
            asynchronous:true,
            evalScripts:true,
            onLoading:function(request){
                deleteAvatarLoading(request);
            },
            onComplete:function(request){
                deleteAvatarComplete(request);
            }
        });
        return false;
    }catch(e) {
        return false;
    }
}

function deleteAvatarLoading(request) {
    CentralNotice.show();
}

function deleteAvatarComplete(request) {
    if(request.status=="200") {
        refreshAvatarAfterDelete();
        CentralNotice.hide();
    }else {
        CentralNotice.hide();
    }
}
function refreshAvatar(avatar_url) {
    var avatar = $("avatar");
    avatar.src = avatar_url+"?"+new Date();
}

function renderGDisplay() {
    $("dRealName").innerHTML = $F("realName");
    $("dEmail").innerHTML = $F("email");
    $("dSignature").innerHTML = $F("signature");
}

function refreshAvatarAfterUpload(file_uri) {
    refreshAvatar(file_uri);
  
}
function refreshAvatarAfterDelete(){
    var file_uri = user_avatar_uri(USER_DEF_AVATAR, "96");
    refreshAvatar(file_uri);
}

function user_avatar_uri(fileName, size) {
    var uri = USER_AVATAR_URI+"/"+fileName+"_"+size+".jpg";
    return uri;
}

function changeCountry() {
    var country = $("countrySelector");
    if(country.value!="US" && country.value!="CN" && country.value!="JP") {
        $("regionInput").style.display = "";
        $("regionSelector").style.display = "none";
    }else {
        loadRegionCode(country.value);
    }
    $("regionInput").value = "";
}
function loadRegionCode(countryCode) {
    var url = "/profile_mana/load_region_code?c_code="+countryCode;
    loadRegionCodeLoading();
    new Ajax.Request(url,
    {
        asynchronous:true,
        evalScripts:false,
        requestHeaders: {
            Accept: 'application/json'
        },
        onComplete:function(request){
            loadRegionCodeComplete(request);
        }
    });
    return false;
}
function loadRegionCodeLoading() {
    $("regionSelector").style.display = "none";
    $("regionInput").style.display = "none";
    loading("regionLoader");
}
function loadRegionCodeComplete(request) {
    if(request.status=="200") {
        loadComplete("regionLoader");
        $("regionSelector").style.display = "";
        var regionCode = eval(request.responseText);
        //console.log(regionCode);
        makeRegionCode(regionCode);
    }else{
}
}
function makeRegionCode(regionCode) {
    $("regionSelector").options.length = 0;
    for(var i=0; i<regionCode.length; i++) {
        var option = document.createElement("option");
        option.value = regionCode[i][0];
        option.innerHTML = regionCode[i][1];
        $("regionSelector").appendChild(option);
    }
}
function changeRegion() {
    $("regionInput").value = $F("regionSelector");
}

function saveProfileBefore() {
    if (!isValidEmail("email")) return false;
    if (!isValidRealname()) return false;
    return true;
}

function isValidEmail(ID) {
    try{
        var email = $(ID);
        var validator = new Validator();
        if(!validator.IsEmail(email.value)) {
            email.focus();
            flashNotice2(ALE_8, "noticer_email");
            return false;
        }else {
            if($("noticer_email") && $("noticer_email").innerHTML==ALE_8) {
                hideNotice2("noticer_email");
            }
            return true
        }
    }catch(e) {
        return false;
    }
}
function isValidRealname() {
    try { 
        var realName_f = $("first_name");
        var realName_l = $("last_name");
        if(realName_f.value.trim()=="") {
            flashNotice2(ALE_14, "noticer_real_name");
            realName_f.focus();
            return false;
        }
        if(realName_l.value.trim()=="") {
            flashNotice2(ALE_14, "noticer_real_name");
            realName_l.focus();
            return false;
        }
        hideNotice2("noticer_real_name");
        return true
    }catch(e) {
        return false;
    }
}

function selectAvatar(index) {
    var url = "/profile_mana/select_avatar?avatar_index="+index;
    CentralNotice.show();
    new Ajax.Request(url,
    {
        asynchronous:true,
        evalScripts:false,
        onComplete:function(request){
            CentralNotice.hide();
            var avatarElement = $("avatar");
            avatarElement.src = avatarElement.src.replace(/user\/.*\.jpg/, "user/"+USER_DEF_AVATAR+"_"+index+"_96.jpg");
        }
    });
    return false;
}

function set_privacy(elem_id){
    $(elem_id).style.display = "none";
    var select_id = elem_id.replace("show_","set_");
    $(select_id).style.display = "";
    $(elem_id.replace("show_","cancel_")).style.display = "";
}
function save_privacy(elem_id){
    //only for IE6
    if($(elem_id).style.display == "none")return;
    //
    var privacy_value = $(elem_id).value;
    var id = elem_id.replace("set_privacy_","")
    var params =id + ":"+ privacy_value;
    CentralNotice.show();
    new Ajax.Request(save_privacy_url,{
        method:'post',
        parameters:{
            value:params
        },
        onSuccess:function(transport){
            CentralNotice.hide();
            $(elem_id.replace("set_","show_")).style.display = "";
            $(elem_id).style.display = "none";
            $(elem_id.replace("set_","cancel_")).style.display = "none";
            if(id == "birthday"){
                $(elem_id.replace("set_","show_")).innerHTML = PRIVACY_BIRTHDAY_CODE[transport.responseText];
            }
            else if(id == "wall_read"){
                wall_privacy = parseInt(transport.responseText);
                read_privacy = wall_privacy%10;
                $(elem_id.replace("set_","show_")).innerHTML = PRIVACY_WALL_CODE[read_privacy];
                write_privacy = parseInt(wall_privacy/10) * 10;
                $("show_privacy_wall_write").innerHTML = PRIVACY_WALL_CODE[write_privacy];
                set_wall_write_option(read_privacy);
            }
            else if(id == "wall_write"){
                $(elem_id.replace("set_","show_")).innerHTML = PRIVACY_WALL_CODE[transport.responseText];
            }else if(id=="common_stuff"){
                $(elem_id.replace("set_","show_")).innerHTML = PRIVACY_COMMON_STUFF[transport.responseText];
            }
            else{
                $(elem_id.replace("set_","show_")).innerHTML = PRIVACY_CODE[transport.responseText];
            }
        }
    }
    )
}
function cancel_change_privacy(elem_id){
    $(elem_id).style.display = "none";
    $(elem_id.replace("set_","cancel_")).style.display = "none";
    $(elem_id.replace("set_","show_")).style.display = "";    
}
function save_check_box_privacy(elem_id){
    var privacy_value = 1;
    if($(elem_id).checked)
        privacy_value = 0;
    var params =elem_id + ":"+ privacy_value;
    CentralNotice.show();
    new Ajax.Request(save_privacy_url,{
        method:'post',
        parameters:{
            value:params
        },
        onSuccess:function(transport){
            CentralNotice.hide();
        }
    }
    )
}
function set_wall_write_option(read_privacy){
    $("set_privacy_wall_write").innerHTML = "";
    for(var i=1;i <= read_privacy && i<4;i++){        
        insert_html = "<option value='"+i*10+"'>"+PRIVACY_WALL_CODE[i*10]+"</option>";
        new Insertion.Bottom("set_privacy_wall_write",insert_html);
    }
}
//----------shout box----------------
function post_new_msg(){
    msg_body = $("msg_input").value;
    if(msg_body.empty()){
        $("post_noticer").style.display = "";
        return false;
    }else{
        $("post_noticer").style.display = "none";
    }
    captcha_value = $("captcha").value;
    if(captcha_value.empty()){
        $("noticer_captcha").show();
        return false;
    }else{
        $("noticer_captcha").hide();
    }
    url = "/profile_mana/post_message";
    CentralNotice.show();
    new Ajax.Request(url,{
        method:'post',
        parameters:{
            to:owner_id,
            msg:msg_body,
            captcha:captcha_value
        },
        onSuccess:function(transport){
            $("captchaIframe").src=RESOURCE_HOST+"/captcha";
            $("captcha").value="";
            try{
                result = eval("(" + transport.responseText + ")");
                if(result == "-2"){
                    $("noticer_captcha").show();
                    CentralNotice.hide();
                    return;
                }
                msg_r = result["msg"];
                insert_html = "<li id='msg_"+msg_r["p_id"]+"'><a href='/profile/"+msg_r["poster_u_name"]+"' title='See "+msg_r["poster_real_name"]+"'s profile' class='avatarLink_48'"+
                "style='background-image:url("+RESOURCE_HOST+"/images/avatar/user/"+msg_r["poster_u_name"]+"_48.jpg)'>"+
                "<span id='status_"+msg_r["p_id"]+"' title='"+msg_r["poster_u_name"]+"' class='dUserName'>"+msg_r["poster_real_name"]+
                "</span></a>"+
                "<h3><a href='/profile/"+msg_r["poster_u_name"]+"' title='See "+msg_r["poster_real_name"]+"'s profile'>"+msg_r["poster_real_name"]+"</a><span class='hint'>wrote on "+msg_r["created_at"]+"</span></h3>"+
                "<blockquote>"+msg_r["body"]+"</blockquote>"+
                "<div class='opt'>"
                if(result["is_owner"] == 1){
                    insert_html = insert_html +
                    "<a class='linkOpt' href='javascript:void(0)' onclick='delete_post("+msg_r["p_id"]+")'>[ Delete ]</a> </div>"+
                    "</li>";
                }else{
                    insert_html = insert_html +"<a class='linkOpt' href='/profile/"+msg_r["poster_u_name"]+"/shout_box'>[ Write on "+msg_r["poster_real_name"]+"'s Wall ]</a></li>";
                                                  
                }
                new Insertion.Top("msgList",insert_html);
                $("msg_input").value = "";
                markOneUserStatus("status_"+msg_r["p_id"])
                CentralNotice.hide();
            }catch(e){
                CentralNotice.hide();
            }
        }
    })
}
function delete_post(p_id){
    if(!confirm(ALE_40)) return;
    self.location = "/profile_mana/delete_message?owner_id="+owner_id+"&post_id="+p_id+"&refer_info="+refer_info;
}    
function markOneUserStatus(id) {
    var usernameEls = [$(id)];
    var usernames = [];
    
    for(var i=0; i<usernameEls.length; i++) {
        var u = usernameEls[i];
        Try.these(
            function() {
                usernames.push(u.name.toLowerCase());
            },
            function() {
                usernames.push(u.title.toLowerCase());
            }
            );
    }
    var url="/user_mana2/check_user_status";
    var params = "json_infos="+usernames.toJSON();
    new Ajax.Request(url, 
    {
        asynchronous:true,
        evalScripts:false,
        method:'get',
        onComplete:function(request){
            checkUserStatusCompleted(request, usernameEls)
            },
        parameters:params
    });
//console.log(userNames);
}    
//-----------------------------------
//--------------edit other profile--------------------------------------------
function save_other_profile(name){
    value = $(name + "_input").value;
    if(value.empty()){
        return false;
    }
    CentralNotice.show();
    url = "/profile_mana/other_profile"
    new Ajax.Request(url,{
        method:'post',
        parameters:{
            key:name,
            value:value
        },
        onSuccess:function(transport){
            result = eval("(" + transport.responseText + ")");
            $(name+"_show").innerHTML = result["url"];
            $(name+"_show").style.display = "";
            $(name+"_show").href = result["url"];
            $(name + "_input").style.display = "none";
            $(name + "_cancel").style.display = "none";
            $(name + "_save").style.display = "none";
            $(name + "_edit").style.display = "";
            $(name+"_delete").style.display = "";
            CentralNotice.hide();
        }
    }
    )
    
}
function cancel_other_profile(name){
    $(name+"_show").style.display = "";
    $(name + "_input").style.display = "none";
    $(name + "_cancel").style.display = "none";
    $(name + "_save").style.display = "none";
    $(name + "_edit").style.display = "";
    $(name+"_delete").style.display = "";
}
function edit_other_profile(name){
    $(name+"_show").style.display = "none";
    $(name + "_input").style.display = "";
    $(name + "_input").value = $(name+"_show").innerHTML;
    $(name + "_cancel").style.display = "";
    $(name + "_save").style.display = "";
    $(name + "_edit").style.display = "none";
}
function delete_other_profile(name){
    if(!confirm(ALE_41)) return;
    CentralNotice.show();
    url = "/profile_mana/other_profile"
    new Ajax.Request(url,{
        method:'post',
        parameters:{
            key:name,
            value:""
        },
        onSuccess:function(transport){
            $(name+"_show").style.display="none";
            $(name+"_show").innerHTML = "";
            $(name+"_show").href = "javascript:void(0)";
            $(name + "_input").style.display = "";
            $(name + "_input").value = "";
            $(name + "_save").style.display = "";
            $(name + "_edit").style.display = "none";
            $(name + "_delete").style.display = "none";
            $(name + "_cancel").style.display = "none";
            CentralNotice.hide();
        }
    }
    )
    
}
//--------------end edit other profile----------------------------------------

//--------delete this visit record--------------------------------
function delete_visit(id){
    //if(!confirm(ALE_43)) return false ;
    CentralNotice.show();
    new Ajax.Request("/profile_mana/delete_visit_record",{
        method:'post',
        parameters:{
            vid:id
        },
        onSuccess:function(transport){
           CentralNotice.hide();
            $("record_"+id).remove();
        },
        onFailure:function(){
            CentralNotice.hide();
        },
        onException:function(){
            CentralNotice.hide();
        }
    }
    )
}
//----------------------------customize------------------
function initModuleSwitchAble() {
    var eocMarks = $("column").select(".eocMark");
    for(var i=0; i<eocMarks.length; i++){
        var eocMark = eocMarks[i];
        eocMark.onclick = switchSectionBox;
        id = eocMark.id.replace("eocMark_",'')
        content = $("sideBarBoxContent_"+id)
        if(content.style.display=="") {
            eocMark.className += " expand";
        }else {
            eocMark.className += " collapse";
        }
    }
}

function switchSectionBox() {
    var index = this.id.split("_")[1];
    var content = $("sideBarBoxContent_"+index);
    new Effect[Element.visible(content) ?
    'BlindUp' : 'BlindDown'](content, {
        duration: 0.25
    });
    var eocMark = $("eocMark_"+index);
    if(content.style.display=="") {
        eocMark.className = eocMark.className.replace("expand", "collapse");
    }else {
        eocMark.className = eocMark.className.replace("collapse", "expand");
    }
}
function initProfileSection(){     
    initSortable();
    setMoveHandler();
    new ContextMenu("leftColumn",{});
}
function initSortable(){
    if(!$("moveSection")) return;
    Sortable.create("moveSection", {
        containment: "moveSection",
        constraint: false,
        tag: 'div',
        only: "mainItem",
        dropOnEmpty: true,
        handle: "moveHandle",
        scroll:window,
        constraint:'vertical',
        scrollSensitivity:50,
        onUpdate:function (container) {
            updateProfileOrder()
        }    
    });
}
function updateProfileOrder(){
    //serialize module order
    var modules = $("moveSection").select(".mainItem")
    var modulesId = []
    for(var i=0; i<modules.length; i++){
        var module = modules[i];
        module_id = module.id.replace("mainItem_",'')
        modulesId.push(module_id)
    }
    var modules_order = modulesId.toJSON();
    var url = "/profile/update_order"
    new Ajax.Request(url,{
        method:'post',
        parameters:{
            order:modules_order
        },
        onSuccess:function(transport){
        },
        onFailure:function(){
        },
        onException:function(){
        }
        }
    )
}


function setMoveHandler(){
    //set bookmark section
    var handles = $("moveSection").select(".moveHandle");
    for(var i=0; i<handles.length; i++) {
        handles[i].addClassName("moveHandleCursor");
    }
        
}
function remove_main_section(id){
    if(!confirm(ALE_42)) return false ;
    $("mainItem_"+id).remove();
    $(id+"_title").removeClassName("containSubTitle");
    setTimeout(function(){
        updateProfileOrder();
    },200);
}
function add_main_section(id){
    if($("mainItem_"+id))return;
    CentralNotice.show();
    var url = "/profile/add_main_section";
    new Ajax.Request(url,{
        method:'post',
        parameters:{
            value:id
        },
        onSuccess:function(transport){
            if(!(transport.responseText == 0)){
                new Insertion.After("addSectionPosition",transport.responseText);
                setMoveHandler();
                $(id+"_title").addClassName("containSubTitle");
                setTimeout(function(){
                    initModuleSwitchAble();
                    initSortable();
                    updateProfileOrder();
                },200);
            }
            CentralNotice.hide();
        },
        onFailure:function(){
            CentralNotice.hide();
        },
        onException:function(){
            CentralNotice.hide();
        }
        }
    )
}

function contact_submit_check(){
    var validator = new Validator();
    var email = $("email").value;
    if (email!="" && !validator.IsEmail(email)) {
        $("email").focus();
        flashNotice2(ALE_8, "noticer_email",'',3000);
        return false;
    }
    var mobile = $("mobile_phone").value;
    if (mobile!="" && !validator.IsPhoneNum(mobile)) {
        $("mobile_phone").focus();
        flashNotice2(ALE_47, "noticer_mobile_phone",'',3000);
        return false;
    }
    var land = $("land_phone").value;
    if (land!="" && !validator.IsPhoneNum(land)) {
        $("land_phone").focus();
        flashNotice2(ALE_48, "noticer_land_phone",'',3000);
        return false;
    }
}