/* javascript function for generating username-password for diffrent mail servers for invite friends*/

function generate_email_block_single(email_type, ctr, callfrom) {
  //this code is for making and vanishing the facebook div
  var obj=document.getElementById('fbradio');
  if(obj.checked) {
    document.getElementById("facebookmodule").style.display='block';
    $('#email_block')[0].style.display='none';
  } else {
    document.getElementById("facebookmodule").style.display='none';
    var mail_suffix = "";
    if (email_type == 'orkut' || email_type == 'friendster' || email_type == 'myspace' || email_type == 'hi5') {
      var mail_suffix = email_type;
      var id_title = 'Email Id';
    } else {
      var mail_suffix = email_type;
      var id_title = 'User Name';
    }
    var email_txt = email_type;
    //var html="<dfn>"+__('User Name')+":</dfn><em><input  style='font-size:13px; height:20px; width:165px;' type='text'  name='username_"+email_txt+"' id='username_"+email_txt+"' class = 'error-textbox' /></em>";
    if ( email_type == 'myspace' || email_type == 'friendster' || email_type == 'hi5' || email_type == 'orkut' ) {
      var html="<dfn>" + mail_suffix + ' ' +__('User Name')+":</dfn><em><input  style='font-size:13px; height:20px; width:165px;' type='text'  name='username_"+email_txt+"' id='username_"+email_txt+"' class = 'error-textbox' /></em>";
      html += "<i class='grey'>&nbsp</i>";
      html += "<dfn>"+ mail_suffix + ' ' +__('Password')+": </dfn>";
    } else {
      var html="<dfn>"+__('User Name')+":</dfn><em><input  style='font-size:13px; height:20px; width:165px;' type='text'  name='username_"+email_txt+"' id='username_"+email_txt+"' class = 'error-textbox' /></em>";
      html += "<i class='grey'>&nbsp;@"+mail_suffix+".com</i>";
      html += "<dfn>"+__('Password')+": </dfn>";
    }
    html += "<em><input style='font-size:13px; height:20px; width:165px;' type='password' name= 'psw_"+email_txt+"' id='psw_"+email_txt+"' class = 'error-textbox'/></em><i style='padding:3px 0 0 20px;'><input type='submit' value='"+__('Find friends')+"' class='buttons'/></i>";
    $('#email_block').html('');
    var obj = document.createElement('DIV');
    obj.setAttribute("id","div_"+email_txt);
    obj.setAttribute("class","emailswiplogin");
    $(obj).css('margin', '0');
    $(obj).css('padding', '0');
    obj.innerHTML = html;
    $('#email_block')[0].appendChild(obj);
    $("#email_block")[0].style.display='block';
    window.setTimeout("clear_error_class('username_"+email_txt+"', 'error-textbox', 'normal-textbox')", 2000);
    window.setTimeout("clear_error_class('psw_"+email_txt+"', 'error-textbox', 'normal-textbox')", 2000);
  }
}

function generate_email_block(email_type, ctr, callfrom) {
  var mail_suffix = "";
  if (email_type == 'orkut') {
    mail_suffix = email_type+".com";
    id_title = __('Email Id');
  } else {
    mail_suffix = "@"+email_type+".com";
    id_title = __('User Name');
  }
  email_txt = email_type;
  if(ctr){
    if(ctr.checked == true) {
      $('#div_default').hide();
      $('#password_msg').show();
      $('#submit_btn').show();
      var obj = document.createElement('DIV');
      obj.setAttribute("id","div_"+email_txt);
      $(obj).css('margin', '0');
      $(obj).css('padding', '0');
      var block_style = "";
      if(email_cnt%2 == 1){
        block_style = "style='background:none;'";
      }
      var html = "<div class='new_bg' "+block_style+" ><div class='left'><label>"+id_title+" :</label><span><i><input type='text'  name='username_"+email_txt+"' id='username_"+email_txt+"' class='no_error'></i><i>"+mail_suffix+"</i><span id = 'err_"+email_txt+"' style = 'display:none'>invalid email address or password</span></span></div><div class='right'><label>"+__('Password')+" :</label><span ><i><input type='password' name= 'psw_"+email_txt+"' id= 'psw_"+email_txt+"' class='no_error'></i></span></div></div>";
      obj.innerHTML = html;
      document.getElementById('email_block').appendChild(obj);
      email_cnt++;
    } else if(ctr.checked == false) {
      document.getElementById('email_block').removeChild(document.getElementById('div_'+email_txt));
      email_cnt--;
      if(email_cnt%2 == 1){
        block_style = "style='border-top:none;background:none;'";
      } else {
        block_style = "style='border-top:none;'";
      }
      if(email_cnt ==0 ){
       if(callfrom == 'invite_friend'){
        //document.getElementById('div_default').style.display = "block";
       }
       document.getElementById('password_msg').style.display = 'none';
       document.getElementById('submit_btn').style.display = 'none';
      }
    }
  }
  else {
    email_cnt=0;
    block_style ="";
    //document.getElementById('div_default').style.back
    if(callfrom == 'invite_friend'){
      //document.getElementById('div_default').style.display = "block";
    }
    document.getElementById('password_msg').style.display = 'none';
    document.getElementById('submit_btn').style.display = 'none';
  }
  if(email_cnt){
    var i = 0;
    $('.new_bg').each(function() {
       if(i%2 == 0 && i >0){
        $(this).css('background', 'none');
        $(this).css('border-top', 'none');
      } else {
      $(this).css('background', '#EDF7F9');
      }
      i++;
    });
  }
}

function show_people_bubble (object, uid, key, call_from){
  if(call_from == "find_friends"  || call_from == "find_classmate_friends"){
    curr_key = key;
  }
  $('#invite_bubble_text').html("");
  var x = findPosX(object) + 0;
  var y = findPosY(object) + 0;
  if ( screen.width < (x + 302) ) {
    x = screen.width - 350;
  }

  $('#invite_bubble_body').css('display', 'block');
  $('#invite_people_iframe').css('display', 'block');
  var obj2 = $('#invite_bubble_text');
  $.get(
     base_url + "/ajax/show_invite_bubble.php?type=show_bubble&uid="+uid+"&call_from="+call_from+"&pos="+key,
     {
     },
     function(htmlData) { 
        $('#invite_bubble_text').html(htmlData);
     }
   );
  $('#invite_bubble_body').css('left',x-20 +'px');
  $('#invite_bubble_body').css('top', y+20 +'px');
  $('#invite_people_iframe').css('left',x-12 +'px');
  $('#invite_people_iframe').css('top', y+20 +'px');

}
function hide_people_bubble(){
 $('#invite_bubble_text').html("");
 $('#invite_bubble_body').css("display","none");
 $('#invite_people_iframe').css("display","none");
}
function user_not_show(id, uid, call_from){
  var user_list = new Array ;
  var i = 0;
  $('[@id^="pymk_user_list_"]').each(function() {
     user_list[i] = $(this).attr('value');
     i++;
  });
  if(i == 1) {
  $('[@id^="recom_div"]').each(function() {
     $(this).css("display","none");

  });
  $('[@id^="show_pymk"]').each(function() {
     $(this).css("display","inline");
     $('#show_pymk_mail_id_img').attr('src', skin_url+'/images/down-arrow.gif');
     $('#show_pymk_inst_id_img').attr('src', skin_url+'/images/down-arrow.gif');
  });
  }

  if(cross_frn) {
     cross_frn = false;
  $.get(
     base_url + "/ajax/show_invite_bubble.php?type=not_show&uid="+uid+"&pos="+id+ "&call_from="+call_from+"&pymk_user_list="+user_list,
     {
     },
     function(htmlData) {
        if(htmlData == "&nbsp;"){
          $('#individual_'+id).html(htmlData);
          j = 0;
          $('[@id^="individual_"]').each(function() {
            if($(this).attr('innerHTML') == "&nbsp;"){
             // $(this).css('border','0 none;');
              $(this).css('display','none');
            } else {
            j++;
            }
          });
          if(j == 1){
            $('[@id^="individual_"]').each(function() {
              $(this).css('border','0 none;');
            });
          }
        } else {
        $('#individual_'+id).html(htmlData);
        }
       cross_frn = true;
      }
   );
  }
}
function submit_invite(obj, uid, key, call_from, show_recommend) {
   $('#invite_bubble_text').html("<div class='top'><p class='add_as_friend'><big>Add as friend</big><small></small></p></div><div class='middle' style='float:center'><div class='celb_parent' style='padding:20px 0 0  120px;' ><img  src='"+base_url+"/images/ajax-loader.gif'/></div></div><div class='bottom'></div>");
   var params = {};
    for (var i=0; i < obj.elements.length; i++ ) {
    params[ obj.elements[i].name ] = obj.elements[i].value;
    }
   user_list = new Array;
   if(call_from == 'find_friends' || call_from == 'find_classmate_friends' || call_from == 'classmate_search' ||  call_from == 'pymk_search'){
    var j = 0;
    $('[@id^="pymk_user_list_"]').each(function() {
      user_list[j] = $(this).attr('value');
      j++;
    });
   }
    if(j == 1) {
    $('[@id^="recom_div"]').each(function() {
      $(this).css("display","none");

    });
    $('[@id^="show_pymk"]').each(function() {
      $(this).css("display","inline");
      $('#show_pymk_mail_id_img').attr('src', skin_url+'/images/down-arrow.gif');
      $('#show_pymk_inst_id_img').attr('src', skin_url+'/images/down-arrow.gif');
    });
    }
    var url ;
    if(call_from == 'find_friends' || call_from == 'find_classmate_friends' || call_from == 'classmate_search' || call_from == 'pymk_search'){
     url = base_url + "/ajax/show_invite_bubble.php?type=add_friend&call_from="+call_from+"&pos="+key+"&pymk_user_list="+user_list;
     }
     else {
     url = base_url + "/ajax/show_invite_bubble.php?type=add_friend&call_from="+call_from+"&pos="+key;
    }
   $.post(
    url,
     params,
     function(htmlData) {
        if(htmlData == ''){
         $('#invite_bubble_text').html("<div class='top'><p class='add_as_friend'><big>"+__('Add as friend')+"</big><small><img src='"+base_url+"/images/cross_cut.gif' alt='' onclick='hide_people_bubble()' /></small></p></div><div class='middle'><div class='celb_parent' ><p style='text-align:center;;padding-top:25px;'>"+__('Sorry, your invite could not be sent.')+"</p><p style='text-align:center;padding-top:5px;'>&nbsp;</p></div></div><div class='bottom'></div>");
        } else {
        var recommend_link = "";
        if(show_recommend){
          recommend_link = "<a href='javascript:hide_people_bubble();show_recommend_friend(this,\""+uid+"\");'>"+__('Recommend Friend')+"</a>";
        }
        $('#invite_bubble_text').html("<div class='top'><p class='add_as_friend'><big>"+__('Add as friend')+"</big><small><img src='"+base_url+"/images/cross_cut.gif' alt='' onclick='hide_people_bubble()' /></small></p></div><div class='middle'><div class='celb_parent' ><p style='text-align:center;padding-top:25px;'>"+__('Your invite has been sent successfully.')+"</p><p style='text-align:center;padding-top:5px;'>"+recommend_link+"</p></div></div><div class='bottom'></div>");
         $('#individual_'+key).html(htmlData);
         $('#add_friend_link'+uid).html("Request Sent");
         $('#add_friend_button'+uid).html('<img src="'+skin_url+'/images_landing/request_sent.gif">');
        }
        j = 0;
        $('[@id^="individual_"]').each(function() {
          if($(this).attr('innerHTML') == "&nbsp;"){
            $(this).css('display','none');
          } else {
          j++;
          }
        });
        if(j == 1){
          $('[@id^="individual_"]').each(function() {
            $(this).css('border','0 none;');
          });
        }
     }
   );
}
function block_user_name( frm ) {
  if( frm.searchfor_user.checked == false && frm.searchfor_comm.checked != false) {
    document.getElementById('user_name_text').style.display = 'none';
  } else if( frm.searchfor_comm.checked == false || frm.searchfor_user.checked == true) {
    document.getElementById('user_name_text').style.display = 'block';
  }
}
function swap_classmate_tab(mode) {
 document.search_classmate.mode.value = mode;
 document.search_classmate.submit();
}
function show_mutual_popup(object, uid){
  x = findPosX(object);
  y = findPosY(object);
  document.getElementById('common_friend_body').style.display = 'block';
  var obj2 = document.getElementById('common_friend_text');
  $.get(
     base_url + "/ajax/show_common_friend.php?uid="+uid+"&type=common",
     {
     },
     function(htmlData) {
        $('#common_friend_text').html(htmlData);
     }
   );
//   document.getElementById('common_friend_body').style.left = x +'px';
//   document.getElementById('common_friend_body').style.top = y +'px';
    document.getElementById('common_friend_body').style.left = 250+scrollX()+'px';
    document.getElementById('common_friend_body').style.top = 50+scrollY()+'px';
    document.getElementById('common_friend_iframe').style.display = 'block';
     document.getElementById('common_friend_iframe').style.left = 270+scrollX()+'px';
     document.getElementById('common_friend_iframe').style.top = 54+scrollY()+'px';
}
function show_recommend_friend(object,uid){
  x = findPosX(object);
  y = findPosY(object);
  document.getElementById('common_friend_body').style.display = 'block';
  var obj2 = document.getElementById('common_friend_text');
  $.get(
     base_url + "/ajax/show_common_friend.php?uid="+uid+"&type=recommend",
     {
     },
     function(htmlData) {
        $('#common_friend_text').html(htmlData);
     }
   );
//   document.getElementById('common_friend_body').style.left = x +'px';
//   document.getElementById('common_friend_body').style.top = y +'px';
     document.getElementById('common_friend_body').style.left = 250+scrollX()+'px';
     document.getElementById('common_friend_body').style.top = 50+scrollY()+'px';
     document.getElementById('common_friend_iframe').style.display = 'block';
     document.getElementById('common_friend_iframe').style.left = 270+scrollX()+'px';
     document.getElementById('common_friend_iframe').style.top = 54+scrollY()+'px';

}
function hide_common_friend_pop_up(){
 $('#common_friend_text').html("");
 document.getElementById('common_friend_body').style.display = 'none';
 document.getElementById('common_friend_iframe').style.display = 'none';

}
function sorting_friend(find_friend) {
  $('a[@id^="user_name_link_"]').each(function() {
    var user_name = $(this).html();
    var split_data = $(this).attr('id').split('_');
    id = split_data[3];
      if ( user_name.toLowerCase().indexOf(find_friend.toLowerCase()) == 0 ) {
      $('#recommend_user_'+id).css('display', 'block');
      }
      else {
      $('#recommend_user_'+id).css('display', 'none');
      }
  })
}
function show_selected_friend(obj,relation,show) {
 if(obj!=''){
    var obj_parent = $(obj).parent().parent().parent().parent();
    $('[@id^="tab_"]').attr('class', 'green_tab');
    $('#'+$(obj_parent).attr('id')).attr('class', 'selected_tab');
  }
  var cnt,selected_cnt=0;
  for(var i=0;i<relation;i++) {
  cnt = i;
   document.getElementById('show_tab').value = show;
  if(show=='ALL') {
   document.getElementById('recommend_user_' + cnt).style.display='block';
  } else if(show == 'SELECTED') {
      if(document.getElementById('rec_user_'+cnt).checked == true) {
      document.getElementById('recommend_user_'+cnt).style.display = 'block';
      } else {
      document.getElementById('recommend_user_'+cnt).style.display = 'none';
  }
  }
  if(document.getElementById('rec_user_'+cnt).checked == true) {
   selected_cnt++;
  }
 }
 if(1) {
 document.getElementById('selected_num').innerHTML = "("+selected_cnt+")";
 }

}
function submit_recommendation(obj){
  var params = {};
  var check_boxes = Array();
  j = 0;
  flag = 0;
  for (var i=0; i < obj.elements.length; i++ ) {
    if ( obj.elements[i].type == 'checkbox' && obj.elements[i].checked ) {
      flag = 1;
      check_boxes[j] = obj.elements[i].value;
      j++;
    }else {
    params[ obj.elements[i].name ] = obj.elements[i].value;
    }
  }
  if(flag == 0){
    document.getElementById('error_div').style.display = "block";
    return false;
  }
  else {
   document.getElementById('error_div').style.display = "none";
  }
  params['check'] = toJSON(check_boxes);
  $.post(
     base_url + "/ajax/show_invite_bubble.php?type=add_recommendation",
     params,
     function(htmlData) {
        $('#common_friend_text').html("");
        document.getElementById('common_friend_iframe').style.display = 'none';
        $('#individual_'+curr_key).html(htmlData);
     }
   );
}
function scrollX() {
  return window.pageXOffset ? window.pageXOffset : document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft;
}
function scrollY() {
  return window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
}

function action_on_community(group_id, user_action) {
  $.get(
    base_url + "/ajax/action_on_community.php",
    {
      'gid':group_id,
      'action':user_action
    },
    function(htmlData) {
      $('#group_user_status_'+group_id).html(htmlData);
    }
  );
}
function select_all_rec_user(ctr){
  $('input[@id^="rec_user_"]').each(function() {
    if(ctr.checked){
      $(this).attr('checked',true);
    } else {
      $(this).attr('checked',false);
    }
  });
}
function prompt_invite_bubble(e,call_from,arrow_possition){
//   var field_obj = object;
//   var leftPos = findPosX(field_obj) + a;
//   var rightPos = findPosY(field_obj) - b;
  if(show_invite == true){
  var obj = document.getElementById('bubble_tooltip_help');
  var obj2 = document.getElementById('bubble_tooltip_content_help');
  obj2.style.display = 'block';
  obj.style.display = 'block';
  if(document.all)e = event;
  $.get(
     base_url + "/ajax/prompt_invite_bubble.php?type=show_bubble&call_from="+call_from,
     {
     },
     function(htmlData) {
       obj2.innerHTML = htmlData;
     }
   );

  var st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
  if(navigator.userAgent.toLowerCase().indexOf('safari')>=0)st +=10;
  if(navigator.userAgent.toLowerCase().indexOf('chrome')>=0)st -=20;
  var leftPos = e.clientX;
  if ( leftPos < 0 ) {
    leftPos = 0;
  }
  if ( arrow_possition == 'left' ) {

    obj.style.left = e.clientX + 35+ 'px';
    obj.style.top = e.clientY - obj.offsetHeight -8 + st + 'px';
   $('#bottomrightarrow_help').css('display', 'none');
   $('#topleftarrow_help').css('display', 'block');
  } else {
    $('#bottomrightarrow_help').css('display', 'block');
    $('#topleftarrow_help').css('display', 'none');
    obj.style.left =  e.clientX - 170 + 'px';
    obj.style.top = e.clientY  - obj.offsetHeight -40 + st + 'px';
  }
  return false;
  //$('#close_link_help').css('display', 'none');
//   var hide_bubble = "document.getElementById('bubble_body').style.display='none';document.getElementById('bubble_iframe').style.display='none';";
//   var obj2 = $('#prompt_bubble_text');
}
}
function prompt_invite_bubble_by_id(obj_ref,call_from,arrow_possition){
  if(show_invite == true){
  var obj = document.getElementById('bubble_tooltip_help');
  var obj2 = document.getElementById('bubble_tooltip_content_help');
  obj2.style.display = 'block';
  obj.style.display = 'block';
  if(document.all)e = event;
  $.get(
     base_url + "/ajax/prompt_invite_bubble.php?type=show_bubble&call_from="+call_from,
     {
     },
     function(htmlData) {
       obj2.innerHTML = htmlData;
     }
   );
  var obj_width = obj_ref.width;
  var obj_height = obj_ref.height;
  x = findPosX(obj_ref);
  y = findPosY(obj_ref);
//   x += obj_width - 10;
//   y += obj_height - 10;
  obj.style.left = x - 160 + 'px';
  obj.style.top = y  - obj.offsetHeight - 50+ 'px';
  $('#bottomrightarrow_help').css('display', 'block');
  $('#topleftarrow_help').css('display', 'none');
  return false;
  }
}
function save_prompt_reply(ans,form_name){
  document.getElementById('bubble_tooltip_help').style.display='none';
  document.getElementById('bubble_tooltip_content_help').style.display='none';
  if (form_name == 'join_community') {
    if ( document.getElementById('link_gid')) {
      var gid = document.getElementById('link_gid').value ;
      window.location = base_url+"/main-group.php?gid="+gid+"&action=join&prompt="+ans;
    } else {
      window.location = document.URL+"&action=join&prompt="+ans;
    }
  } else if (form_name == 'invite_join_community'){
    var keyid = document.getElementById('keyid').value;
    window.location = base_url+"/send_invites_friends.php?keyid="+keyid+"&keytype=invite_join_community";
  } else if (form_name == 'share_app') {
    var keyid = document.getElementById('app_id').value;
    window.location = base_url+"/send_invites_friends.php?keyid="+keyid+"&keytype=share_app";
  } else {
    document.getElementById('prompt_reply').value  = ans;
    document.forms[form_name].submit();
  }
}
function alert_to_wait(frm_name,call_from){
   var obj = document.getElementById('bubble_tooltip_help');
  var obj2 = document.getElementById('bubble_tooltip_content_help');
  obj2.style.display = 'block';
  obj.style.display = 'block';
  $.get(
     base_url + "/ajax/prompt_invite_bubble.php?type=alert_to_wait&frm_name="+frm_name,
     {
     },
     function(htmlData) {
       obj2.innerHTML = htmlData;
     }
   );
  var obj_ref = document.getElementById('inv_btn');
  var obj_width = obj_ref.width;
  var obj_height = obj_ref.height;
  var x = findPosX(obj_ref);
  var y = findPosY(obj_ref);
//   x += obj_width - 10;
//   y += obj_height - 10;
  if(call_from == 'email_friends'){
	obj.style.left = x - 30 + 'px';
  obj.style.top = y  - 70+ 'px';
	$('#bottomleftarrow_help').css('display', 'block');
   $('#bottomrightarrow_help').css('display', 'none');
  } else {
  obj.style.left = x - 160 + 'px';
  obj.style.top = y   - 70+ 'px';
  $('#bottomleftarrow_help').css('display', 'none');
   $('#bottomrightarrow_help').css('display', 'block');
  }
  $('#topleftarrow_help').css('display', 'none');
  return false;
}

function save_wait_reply(ans, form_name){
  document.getElementById('bubble_tooltip_help').style.display='none';
  document.getElementById('bubble_tooltip_content_help').style.display='none';
  if(ans == 'YES'){
    document.forms[form_name].submit();
  }
}
function transfer_personal_text(val) {
  document.invitationform.my_msg.value = val.value;
  document.login.my_msg.value = val.value;
  document.csv_invite.my_msg.value = val.value;
  document.multiple_invite.my_msg.value = val.value;
}
function load_most_viewed_bubble(e,tag_id,pos) {
  var temp_html = $('#tag_container').html();
  if(send_request && prev_tag!=tag_id) {
  send_request = 0;
  $.get(
     base_url + "/ajax/prompt_invite_bubble.php?tag_id="+tag_id+"&type=tag_bubble&tag_type="+display_type+"&pos="+pos,
     {
     },
     function(htmlData) {
       if(htmlData != '') {
        $('#tag_container').html(htmlData);
       } else {
       $('#tag_container').html(temp_html);
       }
       send_request = 1;
       prev_tag = tag_id;
       load_tag_bubble(tag_id);
     }
   );
  }
  else {
   return true;
  }
}
function swap_tag_tab(tag_type) {
  send_request = 1;
  display_type = tag_type;
  prev_tag = '';
  if(tag_type == 'image') {
  $('#image_span').css('font-weight','bold');
  $('#text_span').css('font-weight','normal');
  $('#tag_container').html(default_tag_img);
  $('#top_div').html(first_block);
  } else {
  $('#text_span').css('font-weight','bold');
  $('#image_span').css('font-weight','normal');
  $('#tag_container').html(default_tag_text);
  $('#top_div').html(first_block);
  }
}
function load_default_tags() {
  prev_tag = '';
  if(display_type == 'text') {
    $('#tag_container').html(default_tag_text);
  } else {
  $('#tag_container').html(default_tag_img);
  }
  send_request = 1;
  $('#top_div').html(first_block);
}
function load_tag_bubble(cnt){
  if(send_request) {
     var innerhtml = $('#popup_div_'+cnt).html();
     if(innerhtml != '') {
     $('#top_div').html(innerhtml);
     }
  }
  return true;
}
function remove_tag_bubble() {
  $('#tag_bubble_body').css('display', 'none');
  $('#tag_bubble_iframe').css('display', 'none');
  $('#tag_bubble_text').html('');
}
