// this variable stores html content of the profile details when it's replaced to send  a message
var orig_profile_html="";

function save_records(records) {
    call_remote('user.edit_user_media_records', new Array('records='+records), refresh_records, 'POST');
}

function refresh_records(html) {
    set_div('records', html);
}

function change_pic(picName,imgSrc) {
  if (document.images)
    document[picName].src= imgSrc;
}

function DoCityUpdate() {
    document.getElementById('basic_location_id').value='';

    if (document.getElementById('basic_country_code').value == 'US') {
        document.getElementById('location_label').innerHTML='Zip Code:';
    } else if (document.getElementById('basic_country_code').value == 'CA') {
        document.getElementById('location_label').innerHTML='Postal Code:';
    } else {
        document.getElementById('location_label').innerHTML='City Name:';
    }
}

function refresh_status_area(html) {
    if (typeof html == 'undefined')
        html = '';
    set_div('status_area', html);
}

function refresh_new_message(html) {
    if (typeof html == 'undefined' || html == '')
        html = orig_profile_html;
    set_div('profile_text_area', html);
}

var l_to_duid = 0;
function send_msg(thread_id, to_duid, msg_txt, ignore_ad) {
    args = new Array(
        'thread_id='+thread_id,
        'to_duid='+to_duid,
        't=send_message_from_profile',
        'msg_txt='+msg_txt,
        'process=1');
    var f_func = refresh_new_message;
    if (typeof(ignore_ad) == 'undefined' && typeof(ls_profile_page) !== 'undefined') {
        f_func = sent_message;
        l_to_duid = to_duid;
    }
    call_remote('messaging.send_message', args, f_func, 'POST');
}

function sent_message() {
    delete(o_tab_data[o_tabs_reverse.restrictions]);
    $("#content_area").css({display:"none", margin:"2000px 0"});
    $.post("/main.php", {a:"user.profile", sent:1}, function(s_data){
        $("#content_message_sent").css({display:"block"}).html(s_data);
    });
}

function close_sent_message() {
    $("#content_message_sent").css({display:"none"}).html("");
    $("#content_area").css({margin:"0", display:"block"});
    profile_reload_tab(o_tabs_reverse["info"], true);
}

function quick_msg(frmObj) {
  if (frmObj.qmsg.selectedIndex > 0) {
    if (frmObj.msg_txt.value.length > 0) {
      if (confirm('Overwrite your existing message?'))
        frmObj.msg_txt.value = frmObj.qmsg.options[frmObj.qmsg.selectedIndex].text;
    } else {
      frmObj.msg_txt.value = frmObj.qmsg.options[frmObj.qmsg.selectedIndex].text;
    }
  }
}

// username related functions
function view_user_header() {
    call_remote('user.view_profile_header', null, refresh_user_header);
    return false;
}

function edit_user_header() {
    call_remote('user.edit_profile_header', null, refresh_user_header, 'POST');
    return false;
}

function update_user_header() {
    if (document.getElementById) { // DOM3 = IE5, NS6
        params = new Array();
        params[0] = "username="+document.getElementById('username').value;
        params[1] = "quote="+document.getElementById('quote').value;
        params[2] = "item=header";
        params[3] = "existing_username="+document.getElementById('existing_username').value;
        call_remote('user.update_profile', params, refresh_user_header, 'POST');
    }
    return false;
}

function refresh_user_header(html) {
    set_div("manage_user_header", html);
}

function view_user_profile() {
    call_remote('user.view_profile_detail', null, refresh_user_profile);
    return false;
}

// displayes the view of the profile detail
function view_profile_detail() {
    call_remote('user.view_profile_detail', null, refresh_user_profile);
    return false;
}

// displayes the edit mode of the profile detail
function edit_profile_detail() {
    call_remote('user.edit_profile_detail', null, refresh_user_profile, 'POST');
    return false;
}

// calls the API to update all profile detail fields
function update_profile_detail() {
    params = new Array();
    params = process_form(document.getElementById('profile_detail_form'));
    contents = "";
    for(i=0;i<params.length;i++) {
        contents=contents+"&"+params[i];
    }
    call_remote('user.update_profile', params, refresh_user_profile, 'POST');
    return false;
}

function refresh_user_profile(html) {
    set_div("manage_user_profile", html);
}

function view_user_essays() {
    call_remote('user.view_profile_text', null, refresh_user_essays, 'POST');
    return false;
}

function edit_user_essays() {
    call_remote('user.edit_profile_text', null, refresh_user_essays, 'POST');
    return false;
}

function update_user_essays() {
    params = new Array();
    params[0] = "t6="+document.getElementById('t6').value;
    params[1] = "t7="+document.getElementById('t7').value;
    params[2] = "item=essays";
    call_remote('user.update_profile', params, refresh_user_essays, 'POST');
    return false;
}

function refresh_user_essays(html) {
    set_div("manage_user_essays", html);
}

function change_height_cm() {
    var height_cm;
    var height_ft;
    var height_in;
    height_cm = document.getElementById('basic_height_cm').value;
    height_ft = Math.floor(height_cm/2.5/12);
    height_in = Math.round(height_cm/2.5 - height_ft*12);
    document.getElementById('basic_height_ft').value=height_ft;
    document.getElementById('basic_height_in').value=height_in;
    return false;
}

function change_height_ft() {
    var height_ft = Math.round(document.getElementById('basic_height_ft').value);
    var height_in = Math.round(document.getElementById('basic_height_in').value);
    var height_cm = Math.round((height_ft*12+height_in)*2.5);
    document.getElementById('basic_height_cm').value=height_cm;
    return false;
}

function view_pictures() {
    call_remote('user.manage_pics', null, refresh_pictures);
    return false;
}

function remove_picture(pic_id) {
    if(confirm('Are you sure you want to remove this picture?')) {
        params = new Array();
        params[0]="action=remove";
        params[1]="picnum="+pic_id;
        call_remote('user.manage_pics', params, refresh_pictures);
    }
    return false;
}

function primary_picture(pic_id) {
    params = new Array();
    params[0]="action=primary";
    params[1]="primary="+pic_id;
    call_remote('user.manage_pics',params,refresh_pictures);
    return false;
}

function refresh_pictures(html) {
    set_div('thumbnotext', html);
}

function validate_profile_form() {
    var errors='';
    if(document.getElementById('height_ft').value=='' || document.getElementById('height_in').value=='' || document.getElementById('height_cm').value=='') errors+='\nHeight is not set';
    if(document.getElementById('birth_month').value=='' || document.getElementById('birth_day').value=='' || document.getElementById('birth_year').value=='') errors+='\nBirth date is not set';
    if(errors!='') {
        alert('The following errors occurred:\n'+errors);
        return false;
    } else {
        return true;
    }
}

function request_pictures(duid) {
    args = new Array();
    args.push('action=do_pic_request');
    args.push('to_duid='+duid);

    call_remote('user.profile', args, show_refresh_pic_request, 'GET');
}

function show_refresh_pic_request (data, args) {
    document.getElementById('no_pic_detail').innerHTML = data;
}

var b_toggle = false;
function toggle_top_left(b_show){
    if (typeof(b_show) != "undefined") {
        if (b_show) {
            b_toggle = true;
        } else {
            b_toggle = false;
        }
    }
    if (!b_toggle) {
        $("#profileimage").css({display:"none"});
        $("#profiledesc").css({display:"none"});
        $("#small_pics").css({display:"none"});
        $("#profileimagearea").css({height:"20px"});
        $("#toggletopleft").find("img").attr("src", "/images/max.gif");
        $("#maximizetopleft").css({display:"block"});
        $("#maximizetopleft").addClass("ui-state-default");
        b_toggle = true;
    } else {
        $("#maximizetopleft").css({display:"none"});
        $("#toggletopleft").find("img").attr("src", "/images/min.gif");
        $("#profileimagearea").css({height:$("#profileimagearea").attr("data-height")+"px"});
        $("#small_pics").css({display:"block"});
        $("#profileimage").css({display:"block"});
        $("#profiledesc").css({display:"block"});
        b_toggle = false;
    }
}
var b_ignore_next = false;
var o_tab_data = new Object();
function profile_reload_tab(n_index, b_tab_change, s_tab_custom, s_data_custom, b_force){
    if (b_ignore_next) {
        b_ignore_next = false;
        return;
    }
    if (b_tab_change) {
        b_ignore_next = true;
    }
    var n_index_raw = n_index;
    if (b_self_profile) {
        if (typeof(o_tabs[n_index + "s"]) != "undefined") {
            n_index += "s";
        }
    }
    /*toggle_top_left(o_tabs[n_index][1]);*/
    var s_tab = o_tabs[n_index][0];
    if (typeof(s_tab_custom) != "undefined" && s_tab_custom.length) {
        s_tab = s_tab_custom;
    }
    if (b_tab_change) {
        $("#tabs > ul").tabs("select", n_index_raw);
    }
    if (typeof(s_data_custom) != "undefined" && s_data_custom.length) {
        $("#fragment-" + (n_index_raw + 1)).html(s_data_custom);
        return;
    }
    if (!b_self_profile && typeof(o_tab_data[n_index_raw]) != 'undefined' &&
        typeof(b_force) == 'undefined') {
        $("#fragment-" + (n_index_raw + 1)).html(o_tab_data[n_index_raw]);
        return;
    }
    var s_loading = "<div style='height:150px;padding:75px;text-align:center;'><img src='/images/ajax_busy_big_rotate.gif'></div>";
    $("#fragment-" + (n_index_raw + 1)).html(s_loading);
    var o = {a:"user.profile", duid:n_profile_duid, tab:s_tab};
    $.post("/main.php", o, function(s_data){
        $("#fragment-" + (n_index_raw + 1)).html(s_data);
        if (!b_self_profile) {
            o_tab_data[n_index_raw] = s_data;
        }
    });
}
var b_slideshow_pause_button = true;
function slideshow_cntrl(s_cntrl, b_force){
    if (typeof(b_force) == "undefined" && b_slideshow_pause_button) {
        $("#slideshow").cycle(s_cntrl);
    } else if(typeof(b_force) != "undefined") {
        if (b_slideshow_pause_button) {
            b_slideshow_pause_button = false;
            s_cntrl = "pause";
            $("#user_image_control").children("img").attr("src", "/images/playicon.gif");
        } else {
            b_slideshow_pause_button = true;
            s_cntrl = "resume";
            $("#user_image_control").children("img").attr("src", "/images/pauseicon.gif");
        }
        $("#slideshow").cycle(s_cntrl);
    }
}

function add_friend(){
    $.post("/main.php", {a:"network.add_friend", fuid: n_profile_duid}, function(s_data){
        do_interaction_sent("Friend Request Sent");
        $("#user_friend").html("");
    });
}

function add_favorite() {
    $.post("/main.php", {a:"network.add_favorite", fuid: n_profile_duid}, function(s_data){
        do_interaction_sent("Favorite Added");
        $("#user_favorite").html("");
    });
}

function report_user(){
    do_interaction_sent("User Reported");
    $("#user_report").html("");
}

var s_status_string = " - <a href='#' onclick='do_interaction_ok();return false;'>OK</a>";
function do_interaction_sent(s_data){
    $("#reportabuse").css({display:"none"});
    $("#reportstatus").css({display:"block"}).html(s_data + s_status_string);
}

function do_interaction_ok(){
    $("#reportstatus").css({display:"none"});
    $("#reportabuse").css({display:"block"});
}

var b_tab_basic_table = null;
function toggle_info_favorites(o_div){
    if (b_tab_basic_table === null) {
        b_tab_basic_table = true;
        return;
    }
    if (!b_tab_basic_table) {
        $("#basictabletab").addClass("selected");
        $("#favtabletab").removeClass("selected");
    } else {
        $("#favtabletab").addClass("selected");
        $("#basictabletab").removeClass("selected");
    }
    b_tab_basic_table = !b_tab_basic_table;
}

function profile_do_favorites(n_page){
    profile_do_favorites_edit_list(false);
    $("#userstatus").css({display:"block"});
    $("#profileimagearea").css({height:$("#profileimagearea").attr("data-height")+"px"});
    $("#profiledesc").css({height:"340px"});
    var n_page_actual = 0;
    if (typeof(n_page) != 'undefined') {
        n_page_actual = n_page;
    }
    var o_param = {a:"user.profile", tab:"favorites", duid:n_profile_duid, page:n_page_actual};
    $.post("/main.php", o_param, function(s_data){
        $("#favtable").html(s_data);
    });
}

function profile_do_favorites_edit_list(b_show, n_page){
    if (!b_show) {
        $("#profileimagecover").css({display:"none"});
        return;
    }
    var n_page_actual = 0;
    if (typeof(n_page) != 'undefined') {
        n_page_actual = n_page;
    }
    $("#profileimagecover").css({display:"block"}).html("Loading");
    var o_param = {a:"user.profile", tab:"favorites-edit-list", page:n_page_actual};
    $.post("/main.php", o_param, function(s_data){
        $("#profileimagecover").html(s_data);
    });
}

function profile_request(s_field){
    var o = {a:"user.profile_request", duid:n_profile_duid, field:s_field};
    $.post("/main.php", o, function(s_data){
        $("#request_" + s_field).html("request sent");
    });
}

