var SITE_URL = 'http://www.fluentfuture.com/';

function relocate(url) {
  window.location = url;
}

function preloading() {
	for ( i = 0; i < preloading.arguments.length; i++ ) {
		my_images[i] = new Image();
		my_images[i].src = preloading.arguments[i];
	}
}

function checkEmail(email) {
  var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  if ( !filter.test(email) ) {
    return false;
  } else {
    return true;
  }
}

function createCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires=" + date.toGMTString();
	} else {
    var expires = "";
  }

  document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');

	for(var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') {
      c = c.substring(1,c.length);
    }

		if (c.indexOf(nameEQ) == 0) {
      return c.substring(nameEQ.length,c.length);
    }
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name, "", -1);
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function start_chat_room() {
  if ( $('#choose_room_language').css('display') == 'none' ) {
    $('#choose_room_language').css('display', 'block');
    $('#choose_discussion_language').css('display', 'none');
  } else {
    $('#choose_room_language').css('display', 'none');
  }
}

function start_discusssion_room() {
  if ( $('#choose_discussion_language').css('display') == 'none' ) {
    $('#choose_discussion_language').css('display', 'block');
    $('#choose_room_language').css('display', 'none');
  } else {
    $('#choose_discussion_language').css('display', 'none');
  }
}

function post_console_message(room_id) {
  var message = $('#user_input_chat').val();

  if ( message != '' ) {
    $.ajax({
      type: "POST",
      url: SITE_URL + "post_message",
      data: "chat_room_id=" + room_id + "&message=" + message,
      success: function(msg){
        //alert( "Data Saved: " + msg );
        if ( msg == 1 ) {
        alert('Please login to write in chat');
        }
      }
    });
     
    $('#user_input_chat').val('');
  }
}

function get_remaining_time(time) {
  setInterval("calculate_time(" + time + ")", 30000);
}

function calculate_time(time) {
  //var hours = parseInt(time / 86400);
  //alert(time);
  //if ( time < 0 ) {
    //$('#time_lesson').html('Less than an hour');
  //} else {
    time = parseInt(time);
    var hours = Math.floor(time / 86400);

    time = time - hours * 86400;
    var minutes = Math.floor(time / 3600);

    if ( hours > 0 ) {
      var message = hours + ' days ' + minutes + ' hours';
    } else {
      time = time - minutes * 3600

      var seconds = Math.floor(time / 60);
      var message = minutes + ' hours ' + seconds + ' minutes';
    }

    $('#time_lesson').html(message);
  //}
}

function update_profile(action, variable_obj, place_to, place_to2, place_to3, name_div) {
  //aici
  alert(action);
  var variable = 0;
  if ( action == 6 || action == 7 || action == 8 ) {
    variable = $('#' + variable_obj).attr('value');
  } else {
    variable = $('#' + variable_obj).val();
  }

  $.ajax({
    type: "POST",
    url: SITE_URL + "update_student_profile",
    data: "action=" + action + "&variable=" + variable,
    success: function(msg){
      alert('Saved!');

      $('#' + place_to).css('display', 'none');
      $('#' + place_to2).css('display', 'none');

      write_update(place_to2, place_to, place_to3, 'deactivate');
      if ( action == 3 || action == 4 || action == 5 ) {
        var a = $('#' + variable_obj  + ' option:selected').text();
        
        $('#' + name_div).html(a);
      } else {
        $('#' + name_div).html(variable);
      }
    }
  });
}

function edit_profile(id, type) {
  if ( type == 1 ) {
    $('#' + id).css('display', 'block');
  } else {
    $('#' + id).css('display', 'none');
  }
}

function write_update(id, div, box_type, type) {
  if ( type == 'activate' ) {
    $('#' + id).css('display', 'block');
    //alert('aha');
    $('#' + box_type).mouseout(function() {edit_profile(div, 1);});
  } else {
    $('#' + id).css('display', 'none');

    $('#' + box_type).mouseout(function() {edit_profile(div, 0);});
  }
}

function checkAll() {
	var all_entries = $('#all_email').val();

	for ( i = 0; i < all_entries; i++ ) {
		$('#checkbox_' + i).attr('checked', true);
	}
}

function uncheckAll() {
	var all_entries = $('#all_email').val();

	for ( i = 0; i < all_entries; i++ ) {
		$('#checkbox_' + i).attr('checked', false);
	}
}

function check_deletes() {
	var all_entries = $('#all_email').val();
	var a = false;

	for ( i = 0; i < all_entries; i++ ) {
		if ( $('#checkbox_' + i).attr('checked') == true ) {
			a = true;
			break;
		}
	}

	if ( a ) {
		var r = confirm('Are you sure you want to delete?');

		if ( r == true ) {
			$('#selected_mails').submit();
		}
	}
	else {
		alert('Please choose a message');
	}
}

function mark_read() {
	var all_entries = $('#all_email').val();
	var a = false;

	for ( i = 0; i < all_entries; i++ ) {
		if ( $('#checkbox_' + i).attr('checked') == true ) {
			a = true;
			break;
		}
	}

	if ( a ) {
		//var r = confirm('Are you sure you want to mark as read?');

		//if ( r == true ) {
			$('#hidden_it').val('thats true');
			$('#type_it').val(0);
			$('#selected_mails').submit();
		//}
	}
	else {
		alert('Please choose a message');
	}
}

function mark_unread() {
	var all_entries = $('#all_email').val();
	var a = false;

	for ( i = 0; i < all_entries; i++ ) {
		if ( $('#checkbox_' + i).attr('checked') == true ) {
			a = true;
			break;
		}
	}

	if ( a ) {
		//var r = confirm('Are you sure you want to mark as unread?');

		//if ( r == true ) {
			$('#hidden_it').val('thats true');
			$('#type_it').val(1);
			$('#selected_mails').submit();
		//}
	}
	else {
		alert('Please choose a message');
	}
}

function mark_paied(project_id, user_type) {
  var r = confirm('Are you sure you want to mark as paid?');

  if ( r == true ) {
    if ( user_type == 0 ) {
      window.location = SITE_URL + 'account/teacher?action=mark_payed&id=' + project_id;
    } else {
      window.location = SITE_URL + 'account/student?action=mark_payed&id=' + project_id;
    }
  }
}

function mark_admin_paied(project_id, lesson_id) {
  var r = confirm('Are you sure you want to mark as paid?');

  if ( r == true ) {
    window.location = SITE_URL + 'my_account/teacher?action=mark_paid&id=' + project_id + "&lesson_id=" + lesson_id;
  }
}

function update_teacher_profile(action, variable_obj, place_to, place_to2, place_to3, name_div) {
  var variable = 0;
  if ( action == 6 || action == 7 || action == 8 ) {
    variable = $('#' + variable_obj).attr('value');
  } else {
    variable = $('#' + variable_obj).val();
  }

  $.ajax({
    type: "POST",
    url: SITE_URL + "update_teacher_profile",
    data: "action=" + action + "&variable=" + variable,
    success: function(msg){
      alert('Saved!');

      $('#' + place_to).css('display', 'none');
      $('#' + place_to2).css('display', 'none');

      write_update(place_to2, place_to, place_to3, 'deactivate');
      if ( action == 3 || action == 4 ) {
        var a = $('#' + variable_obj  + ' option:selected').text();

        $('#' + name_div).html(a);
      } else {
        $('#' + name_div).html(variable);
      }
    }
  });
}

function  add_friend(type) {
  if ( type == 0 ) {
    var r = confirm('Are you sure you want to add as friend?');

    if ( r == true ) {
      window.location = window.location + '&action=add_friend';
    }
  } else {
    var r = confirm('Are you sure you want to remove as friend?');

    if ( r == true ) {
      window.location = window.location + '&action=remove_friend';
    }
  }
}

function add_teacher(type) {
  if ( type == 0 ) {
    var r = confirm('Are you sure you want to add as teacher?');

    if ( r == true ) {
      window.location = window.location + '&action=add_teacher';
    }
  } else {
    var r = confirm('Are you sure you want to remove as teacher?');

    if ( r == true ) {
      window.location = window.location + '&action=remove_teacher';
    }
  }
}

function delete_friend(id, type) {
  var r = confirm('Are you sure you want to delete friend?');

  if ( r == true ) {
    window.location = window.location + '&action=remove_friend&id=' + id + "&type=" + type;
  }
}

function remove_blocklist(user_id, user_type) {
  var r = confirm('Are you sure you want to remove from blacklist?');

  if ( r == true ) {
    window.location = window.location + '?action=remove_blacklist&id=' + user_id + "&type=" + user_type;
  }
}

function cancel_proj(proj_id) {
  var r = confirm('Are you sure you want to cancel this lesson?');

  if ( r == true ) {
    window.location = window.location + '?action=cancel_project&id=' + proj_id;
  }
}

function translate() {
  var message = $('#text_traslate').val();
  var lang1 = $('#lang_1').val();
  var lang2 = $('#lang_2').val();
  
  if ( message != '' ) {
    $('#return_translate').html(return_loader());
    
    $.ajax({
      type: "POST",
      url: SITE_URL + "translate",
      data: "expression=" + message + "&lang1=" + lang1 + "&lang2=" + lang2,
      success: function(msg){
        $('#return_translate').html(msg);
      }
    });
  }
}

function thesaurus() {
  var message = $('#text_thesaurus').val();
  //var lang1 = $('#lang_1').val();
  //var lang2 = $('#lang_2').val();

  if ( message != '' ) {
    $('#return_thesaurus').html(return_loader());
    
    $.ajax({
      type: "POST",
      url: SITE_URL + "thesaurus",
      data: "word=" + message,
      success: function(msg){
        $('#return_thesaurus').html(msg);
      }
    });
  }
}

function explain() {
  var explain_text = $('#text_explain').val();

  $('#return_explain').html('<div align="center" style="line-height: 22px; height: 22px; font-size: 11px;">Loading...</div>');
  if ( explain_text.length > 0 ) {
    $.ajax({
      type: "POST",
      url: SITE_URL + "explain",
      data: "expression=" + explain_text,
      success: function(msg){
        $('#return_explain').html(msg);
      }
    });
  }
}

function display_resource(id) {
  if ( $('#resource_' + id).css('display') == 'none' ) {
    //$('#resource_' + id).slideDown(1000);
    $('#resource_' + id).css('display', 'block');
  } else {
    //$('#resource_' + id).slideUp(1000);
    $('#resource_' + id).css('display', 'none');
  }
}

function return_loader() {
  return '<table><tr><td><img src="' + SITE_URL + 'images/load1.gif"></td><td class="loading_class">Loading...</td></tr></table>';
}

function update_student() {
  
}

function show_student_profile(id, obj, act) {
  for ( var i = 1; i <= 1; i++ ) {
    $('#placeholder_' + i).css('display', 'none');
  }

  if ( act == 1 ) {
    $('#placeholder_' + id).css('display', 'block');
    $('#' + obj).css('border', '1px solid #CCCCCC');
    $('#' + obj).css('border-bottom', 'none');
  } else {
    $('#placeholder_' + id).css('display', 'none');
    $('#' + obj).css('border', 'none');
  }
}

function show_teacher_profile(id, obj, act) {
  for ( var i = 1; i <= 1; i++ ) {
    $('#placeholder_' + i).css('display', 'none');
  }

  if ( act == 1 ) {
    $('#placeholder_' + id).css('display', 'block');
    $('#' + obj).css('border', '1px solid #CCCCCC');
    $('#' + obj).css('border-bottom', 'none');
  } else {
    $('#placeholder_' + id).css('display', 'none');
    $('#' + obj).css('border', 'none');
  }
}

function save_student_profile(id, obj, act, type, obj_value) {
  show_student_profile(id, obj, act);

  var variable = 0;
  if ( type == 7 || type == 8 || type == 9 ) {
    variable = $('#' + obj_value).attr('value');
  } else {
    variable = $('#' + obj_value).val();
  }

  if ( type == 2 ) {
    variable = $('#day').val() + '.' + $('#month').val() + '.' + $("#year").val();

    if ( check_date($('#day').val(), $('#month').val(), $('#year').val()) ) {

    } else {
      alert('Please add a valid date');

      return;
    }
  }

  if ( type == 3 ) {
    variable = '';

    var from = $('#value_learn_lang2').val();
    variable = '##';
    for ( i = 1; i <= from; i++ ) {
      variable += $('#value_language_2' + i).val() + '##';
    }

    if ( variable == '##' ) {
      alert('Please add a language');

      return;
    }
  }

  if ( type == 4 ) {
    variable = '';

    var from2 = $('#value_learn_lang').val();
    variable = '##';
    for ( i = 1; i <= from2; i++ ) {
      variable += $('#value_language_' + i).val() + '##';
    }

    if ( variable == '##' ) {
      alert('Please add a language');

      return;
    }
  }

  $.ajax({
    type: "POST",
    url: SITE_URL + "update_student_profile",
    data: "action=" + type + "&variable=" + variable,
    success: function(msg){
      //alert('Saved!');
      if ( type == 5 || type == 9 || type == 10 || type == 15 ) {
        var a = $('#' + obj_value  + ' option:selected').text();

        if ( ( type == 5 || type == 15 ) && a == '' ) {
          a = 'No';
        }

        $('#' + obj).html(nl2br(a, false) + ' <span class="edit_link_profile">(edit)</span>');
      } else {
        if ( type == 2 ) {
          $('#' + obj).html(nl2br(msg, false) + ' <span class="edit_link_profile">(edit)</span>');
        } else {
          if ( type != 3 && type != 4 ) {
            $('#' + obj).html(nl2br(variable, false) + ' <span class="edit_link_profile">(edit)</span>');
          } else {
            $('#' + obj).html(nl2br(msg, false) + ' <span class="edit_link_profile">(edit)</span>');
          }
        }
      }
    }
  });
}

function nl2br (str, is_xhtml) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Philip Peterson
    // +   improved by: Onno Marsman
    // +   improved by: Atli Þór
    // +   bugfixed by: Onno Marsman
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: nl2br('Kevin\nvan\nZonneveld');
    // *     returns 1: 'Kevin<br />\nvan<br />\nZonneveld'
    // *     example 2: nl2br("\nOne\nTwo\n\nThree\n", false);
    // *     returns 2: '<br>\nOne<br>\nTwo<br>\n<br>\nThree<br>\n'
    // *     example 3: nl2br("\nOne\nTwo\n\nThree\n", true);
    // *     returns 3: '<br />\nOne<br />\nTwo<br />\n<br />\nThree<br />\n'

    var breakTag = '';

    breakTag = '<br />';
    if (typeof is_xhtml != 'undefined' && !is_xhtml) {
        breakTag = '<br>';
    }

    return (str + '').replace(/([^>]?)\n/g, '$1'+ breakTag +'\n');
}

function save_teacher_profile(id, obj, act, type, obj_value) {
  show_student_profile(id, obj, act);
  
  var variable = 0;
  if ( type == 7 || type == 8 || type == 9 ) {
    variable = $('#' + obj_value).attr('value');
  } else {
    variable = $('#' + obj_value).val();
  }
  
  if ( type == 2 ) {
    variable = $('#day').val() + '.' + $('#month').val() + '.' + $("#year").val();

    if ( check_date($('#day').val(), $('#month').val(), $('#year').val()) ) {
      
    } else {
      alert('Please add a valid date');
      
      return;
    }
  }
  
  if ( type == 3 ) {
    variable = '';

    var from = $('#value_learn_lang2').val();
    variable = '##';
    for ( i = 1; i <= from; i++ ) {
      variable += $('#value_language_2' + i).val() + '##';
    }

    if ( variable == '##' ) {
      alert('Please add a language');

      return;
    }
  }

  if ( type == 4 ) {
    variable = '';

    var from2 = $('#value_learn_lang').val();
    variable = '##';
    for ( i = 1; i <= from2; i++ ) {
      variable += $('#value_language_' + i).val() + '##';
    }

    if ( variable == '##' ) {
      alert('Please add a language');

      return;
    }
  }

  $.ajax({
    type: "POST",
    url: SITE_URL + "update_teacher_profile",
    data: "action=" + type + "&variable=" + variable,
    success: function(msg){
      if ( type == 9 || type == 10 || type == 5 || type == 26 || type == 25 || type == 15 ) {
        var a = $('#' + obj_value  + ' option:selected').text();

        if ( type == 26 || type == 25 || type == 15 ) {
          if ( a == '' ) {
            a = 'No';
          }
        }
        $('#' + obj).html(nl2br(a, false) + ' <span class="edit_link_profile">(edit)</span>');
      } else {
        if ( type == 2 ) {
          $('#' + obj).html(nl2br(msg, false) + ' <span class="edit_link_profile">(edit)</span>');
        } else {
          if ( type != 3 && type != 4 ) {
            $('#' + obj).html(nl2br(variable, false) + ' <span class="edit_link_profile">(edit)</span>');
          } else {
            $('#' + obj).html(nl2br(msg, false) + ' <span class="edit_link_profile">(edit)</span>');
          }
        }
      }
    }
  });
}

function save_teacher_profile_cost(id, obj, act, type, obj_value) {
  var variable = 0;
  variable = parseFloat($('#' + obj_value).val());

  if ( variable >= 2 ) {
    if ( variable <= 100 ) {
      show_student_profile(id, obj, act);

      $.ajax({
        type: "POST",
        url: SITE_URL + "update_teacher_profile",
        data: "action=" + type + "&variable=" + variable,
        success: function(msg){
          //alert('Saved!');
          $('#' + obj).html(nl2br(variable.toFixed(2), false) + ' <span class="edit_link_profile">(edit)</span>');
        }
      });
    } else {
      alert('Cost per hour must be maximum $100');
    }
  } else {
    alert('Cost per hour must be minimum $2');
  }
}

function view_all_group_members(group_id) {
  $('#members_div').html('');
  $('#members_div').html('<div align="center"><img src="' + SITE_URL + 'images/load1.gif" /></div>');
  $('#members_div').css('height', '350px');
  $('#members_div').css('overflow', 'auto');
  
  $.ajax({
    type: "POST",
    url: SITE_URL + "group_members",
    data: "group_id=" + group_id,
    success: function(msg){
      $('#members_div').html(msg);
    }
  });
}

function close_group_members() {
  window.location = window.location;
}

function view_all_group_images(group_id) {
  $('#images_div').html('');
  $('#images_div').html('<div align="center"><img src="' + SITE_URL + 'images/load1.gif" /></div>');
  $('#images_div').css('height', '300px');
  $('#images_div').css('overflow', 'auto');

  $.ajax({
    type: "POST",
    url: SITE_URL + "group_images",
    data: "group_id=" + group_id,
    success: function(msg){
      $('#images_div').html(msg);
    }
  });
}

function view_all_group_videos(group_id) {
  $('#videos_div').html('');
  $('#videos_div').html('<div align="center"><img src="' + SITE_URL + 'images/load1.gif" /></div>');
  $('#videos_div').css('height', '300px');
  $('#videos_div').css('overflow', 'auto');

  $.ajax({
    type: "POST",
    url: SITE_URL + "group_videos",
    data: "group_id=" + group_id,
    success: function(msg){
      $('#videos_div').html(msg);
    }
  });
}

function invite_user() {
  if ( $('#inv_member').css('display') == 'none' ) {
    $('#inv_member').css('display', 'block');
  } else {
    $('#inv_member').css('display', 'none');
  }
}

function remove_group_stuff(type, group_name, id) {
  var r = confirm('Are you sure you want to delete?');

  if ( r == true ) {
    window.location = SITE_URL + 'view/groups/' + group_name + '&action=delete&type=' + type + "&item_id=" + id;
  }
}

function change_log_div() {
  if ( $('#logo_div').css('display') == 'none' ) {
    $('#logo_div').fadeIn(500);
  } else {
    $('#logo_div').fadeOut(500);
  }
}

function disband_group() {
  var r = confirm('Are you sure you want to disband this group?');

  if ( r == true ) {
    window.location = window.location + '&action=disband';
  }
}

/*
$(document).ready(function() {
  var im = '';
  check_im();
  
  if ( document.getElementById("logged") != null ) {
    setInterval("check_im()", 30000);
    //setInterval("check_im()", 1000);
  }
});
*/

var c = false;

function send_im(user_id, user_type) {
  var message = $('#im_text').val();

  if ( message != '' ) {
    $.ajax({
      type: "POST",
      url: SITE_URL + "insert_message",
      data: "user_id=" + user_id + "&user_type=" + user_type + "&message=" + message,
      success: function(msg){
        $('#im_text').val('');
        $('#im_text').focus();

        if ( !c ) {
          window.location = window.location;
        }
      }
    });
  }
}

var opup = 0;

function check_im() {
  $.ajax({
    type: "POST",
    url: SITE_URL + "check_messages",
    success: function(msg){
      if ( msg == 1 ) {
        $('#new_im').css('display', 'block');

        if ( opup == 0 ) {
          window.open(SITE_URL + 'popup_im', 'chat_im', 'width=220,height=300');
          
          opup = 1;
        }
      } else {
        $('#new_im').css('display', 'none');
      }
    }
  });
}

function animate_down() {
  $('#big_t').animate({scrollTop: $('#big_t > #chat_area').outerHeight()}, -2000);
}

function check_bottom() {
    var elem = $('#big_t');
		var inner = $('#big_t > #chat_area');
		var msg = '';

		// check if we're at the bottom with scrollTop
		if (elem[0].scrollHeight - elem.scrollTop() == elem.outerHeight()) {
			msg += '[1] checked with scrollHeight.\n';
		}

		// check if we're at the bottom - equals does not work in FF2 hence we need to do >=
		if ( Math.abs(inner.offset().top) + elem.height() + elem.offset().top >= inner.outerHeight() ) {
			msg += '[2] checked with offset.\n';
		}
		if (msg) {
			//alert(msg);
      $('#a').html(msg);
		}

}

var q_im = true;

function read_im(user_id, user_type, type) {
  $.ajax({
    type: "POST",
    url: SITE_URL + "read_messages",
    data: "user_id=" + user_id + "&user_type=" + user_type,
    success: function(msg){
       if ( msg == '0' ) {
        alert('Chat session closed by other user. Thanks for the conversation!');

        if ( type == 1 ) {
          window.close();
        } else {
          window.location = window.location;
        }
      } else {
        //alert(msg);
        if ( msg == '10101' ) {
          var r = confirm('User wants to book a instant leeson for now');

          if ( r == true ) {
            window.location = SITE_URL + 'instant_book/' + user_id + '/' + user_type + '/' + type;
          }
        } else {
          $('#chat_area').html(msg);
        }
      }

      //if ( check_bottom() ) {
       //$('#chat_board').html(string_html + new_value);
      if ( q_im == true ) {
        animate_down();
      }
//       alert('da');
//      } else {
//        alert('nu');
//      }
    }
  });
}

var cq = true;

function read_chat_room(room_id) {
  $.ajax({
    type: "POST",
    url: SITE_URL + "read_chat_room",
    data: "room_id=" + room_id,
    success: function(msg){
      $('#chat_area').html(msg);
      
      //if ( check_bottom() ) {
       //$('#chat_board').html(string_html + new_value);
      if ( q_im == true ) {
        if ( cq ) {
          var objDiv = document.getElementById("big_t");
          objDiv.scrollTop = objDiv.scrollHeight;
          cq = false;
        } else {
          animate_down();
        }
      }
//       alert('da');
//      } else {
//        alert('nu');
//      }
    }
  });
}

function load_chat_room_users(room_id) {
  $.ajax({
    type: "POST",
    url: SITE_URL + "chat_room_users",
    data: "room_id=" + room_id,
    success: function(msg){
      $('#user_area').html(msg);
    }
  });
}

function display_div(obj) {
  if ( $('#' + obj).css('display') == 'none' ) {
    $('#' + obj).css('display', 'block');
  } else {
    $('#' + obj).css('display', 'none');
  }
}

function instant_book_lesson(user_id, user_type, type) {
  $.ajax({
    type: "POST",
    url: SITE_URL + "instant_book_lesson",
    data: "user_id=" + user_id + "&user_type=" + user_type,
    success: function(msg){
      
    }
  });
}

function lookup(inputString) {
  if(inputString.length == 0) {
    // Hide the suggestion box.
    $('#suggestions').hide();
  } else {
    $.post("ajax_search_friends.php", {queryString: ""+inputString+""}, function(data){
      if(data.length >0) {
        $('#suggestions').show();
        $('#autoSuggestionsList').html(data);
      }
    });
  }
} // lookup

function fill(thisValue) {
  $('#inputString').val(thisValue);

  setTimeout("$('#suggestions').hide();", 200);
}

function delete_teacher(id) {
  var r = confirm('Are you sure you want to remove teacher?');

  if ( r == true ) {
    window.location = window.location + '&action=remove_teacher&id=' + id;
  }
}

function delete_student(id) {
  var r = confirm('Are you sure you want to remove student?');

  if ( r == true ) {
    window.location = window.location + '&action=remove_student&id=' + id;
  }
}

function check_username() {
  var username = $('#username').val();
  
  if ( username != '' ) {
    if ( username.length >= 4 ) {
      $.ajax({
        type: "POST",
        url: SITE_URL + "check_username",
        data: "username=" + username,
        success: function(msg){
          if ( msg == '0' ) {
            $('#username_error').html('Username already registered');
          } else {
            $('#username_error').html('');
          }
        }
      });
    } else {
      $('#username_error').html('Username too short');
    }
  } else {
    $('#username_error').html('Please add username');
  }
}

function save_change(type, div, div_to, msg1, msg2) {
  var variable = $('#' + div).val();

  $.ajax({
    type: "POST",
    url: SITE_URL + "update_student_profile",
    data: "action=" + type + "&variable=" + variable,
    success: function(msg){
      if ( msg == 1 ) {
        $('#' + div_to).html(msg1);
        $('#' + div).val(1);
      } else {
        $('#' + div_to).html(msg2);
        $('#' + div).val(0);
      }
    }
  });
}

function save_change_teacher(type, div, div_to, msg1, msg2) {
  var variable = $('#' + div).val();

  $.ajax({
    type: "POST",
    url: SITE_URL + "update_teacher_profile",
    data: "action=" + type + "&variable=" + variable,
    success: function(msg){
      if ( msg == 1 ) {
        $('#' + div_to).html(msg1);
        $('#' + div).val(1);
      } else {
        $('#' + div_to).html(msg2);
        $('#' + div).val(0);
      }
    }
  });
}

function search_gmt(value) {
  if ( value != '' ) {
    $.ajax({
      type: "POST",
      url: SITE_URL + "get_gmt",
      data: "gmt_val=" + value,
      success: function(msg){
        $('#country_gmt').html(msg);
      }
    });
  }
}

function validate_location_add() {
  var t_new = $('#countries option:selected').text();
  var this_val = '';

  for ( var i = 1; i <= 5; i++ ) {
    if ( $('#lang_this_' + i).css('display') == 'block' ) {
      this_val = $('#value_language_' + i + ' option:selected').text();
      
      if ( this_val == t_new ) {
        return true;
      }
    }
  }

  return false;
}

function add_locations() {
  if ( $('#countries').val() != '' ) {
    if ( !validate_location_add() ) {
      var completed = 0;

      for ( var i = 1; i <= 5; i++ ) {
        if ( $('#lang_this_' + i).css('display') == 'block' ) {
          completed++;
        }
      }

      if ( completed < 5 ) {
        completed++;
        $('#lang_this_' + completed).css('display', 'block');
        //var t_new = $('#countries option:selected').text();
        //$('#value_language_' + completed + ' option:selected').text(t_new);
        var t_new = $('#countries').val();
        $('#value_language_' + completed + '').val(t_new);
      }

      if ( completed == 5 ) {
        $('#add_another_lang').css('display', 'none');
      }

      $('#value_learn_lang').val(completed);
    } else {
      alert('Language already added');
    }
  }
}

function del_locations(loc) {
  var completed = 0;

  for ( var i = 1; i <= 5; i++ ) {
    if ( $('#lang_this_' + i).css('display') == 'block' ) {
      completed++;
    }
  }
  
  if ( loc < completed ) {
    loc = parseInt(loc);
    var oa = parseInt(loc);
    var t_new = 0;
    var loc1 = loc + 1;
    var loc2 = loc1;

    for ( i = loc1; i <= completed; i++ ) {
      t_new = $('#value_language_' + loc2 + ' option:selected').text();
      
      $('#value_language_' + oa + ' option:selected').text(t_new);
      loc2++;
      oa++;
    }
  }

  if ( completed <= 5 ) {
    $('#lang_this_' + completed).css('display', 'none');
    completed--;
  }

  if ( completed == 4 ) {
    $('#add_another_lang').css('display', 'block');
  }

  $('#value_learn_lang').val(completed);
}

function validate_location_add2() {
  var t_new = $('#countries2 option:selected').text();
  var this_val = '';

  for ( var i = 1; i <= 5; i++ ) {
    if ( $('#lang_this_2' + i).css('display') == 'block' ) {
      this_val = $('#value_language_2' + i + ' option:selected').text();

      if ( this_val == t_new ) {
        return true;
      }
    }
  }

  return false;
}

function add_locations2() {
  if ( $('#countries2').val() != '' ) {
    if ( !validate_location_add2() ) {
      var completed = 0;

      for ( var i = 1; i <= 5; i++ ) {
        if ( $('#lang_this_2' + i).css('display') == 'block' ) {
          completed++;
        }
      }

      if ( completed < 5 ) {
        completed++;
        $('#lang_this_2' + completed).css('display', 'block');
        //var t_new = $('#countries option:selected').text();
        //$('#value_language_' + completed + ' option:selected').text(t_new);
        var t_new = $('#countries2').val();
        $('#value_language_2' + completed + '').val(t_new);
      }

      if ( completed == 5 ) {
        $('#add_another_lang2').css('display', 'none');
      }

      $('#value_learn_lang2').val(completed);
    } else {
      alert('Language already added');
    }
  }
}

function del_locations2(loc) {
  var completed = 0;

  for ( var i = 1; i <= 5; i++ ) {
    if ( $('#lang_this_2' + i).css('display') == 'block' ) {
      completed++;
    }
  }

  if ( loc < completed ) {
    loc = parseInt(loc);
    var oa = parseInt(loc);
    var t_new = 0;
    var loc1 = loc + 1;
    var loc2 = loc1;

    for ( i = loc1; i <= completed; i++ ) {
      t_new = $('#value_language_2' + loc2 + ' option:selected').text();

      $('#value_language_2' + oa + ' option:selected').text(t_new);
      loc2++;
      oa++;
    }
  }

  if ( completed <= 5 ) {
    $('#lang_this_2' + completed).css('display', 'none');
    completed--;
  }

  if ( completed == 4 ) {
    $('#add_another_lang2').css('display', 'block');
  }

  $('#value_learn_lang2').val(completed);
}

function focus_day() {
  var day = $('#day').val();
  if ( day == 'day' ) {
    $('#day').val('');
  }
}

function focus_year() {
  var year = $('#year').val();
  if ( year == 'year' ) {
    $('#year').val('');
  }
}

function check_date(day, month, year) {
  var pxf = new Array('', 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

  if ( month == 2 && year % 4 == 0 ) {
    pxf[2] = 29;
  }

  if ( day > 0 && day <= pxf[month] ) {
    if ( month > 0 && month <= 12 ) {
      return true;
    } else {
      return false;
    }
  } else {
    return false;
  }
}

function show_event(dicv, action) {
  if ( action == 1 ) {
    $('#' + dicv).css('display', 'block');
  }

  if ( action == 0 ) {
    $('#' + dicv).css('display', 'none');
  }
}

function choose_content() {
  var content = $('#content').val();
  $('#div_' + content).css('display', 'block');
  content = content == 0 ? 1 : 0;
  $('#div_' + content).css('display', 'none');
}

function remove_content(type, id) {
  var r = confirm('Are you sure you want to delete?');

		if ( r == true ) {
      window.location = SITE_URL + 'teacher_content?action=delete&type=' + type + '&id=' + id;
		}
}

function mark_free_lesson(project_id, user_type) {
  var r = confirm('Are you sure you want to mark as free lesson?');

  if ( r == true ) {
    if ( user_type == 0 ) {
      window.location = SITE_URL + 'account/teacher?action=free_lesson&id=' + project_id;
    } else {
      window.location = SITE_URL + 'account/student?action=free_lesson&id=' + project_id;
    }
  }
}

function show_menu(type) {
  if ( type == 1 ) {
    $('#student_div').css('display', 'block');
    $('#teacher_div').css('display', 'none');

    $('#search_table').css({'background': '#F4F3F1', 'border': '2px solid #E2E0DF'});
    $('#user_type').val('1');
  } else {
    $('#student_div').css('display', 'none');
    $('#teacher_div').css('display', 'block');

    $('#search_table').css({'background': '#EDF8FE', 'border': '2px solid #C9EAFC'});
    $('#user_type').val('0');
  }
}

function update_search_filters() {
  // for students
  if ( $('#user_type').val() == '1' ) {
    var gender = '';
    gender = $('#gender2').attr('checked') == true ? '0' : '';
    if ( gender == '' ) {
      gender = $('#gender3').attr('checked') == true ? 1 : ''
    }

    var age = '';
    if ( $('#age1').attr('checked') == true ) { age = 1; }
    if ( $('#age2').attr('checked') == true ) { age = 2; }
    if ( $('#age3').attr('checked') == true ) { age = 3; }
    if ( $('#age4').attr('checked') == true ) { age = 4; }
    var seeking_friends = $('#seeking_friends').attr('checked') == true ? 1 : '';
    var online_student = $('#online_student').attr('checked') == true ? 1 : '';

    window.location = SITE_URL + 'search?search_username=' + $('#search_username').val() + '&gender=' + gender + '&native_language=' + $('#native_language').val() + '&language=' + $('#language').val() + '&age=' + age + '&online=' + online_student + '&user_type=' + $('#user_type').val();
  } else {
    var cost = '';
    if ( $('#cost1').attr('checked') == true ) { cost = 1; }
    if ( $('#cost2').attr('checked') == true ) { cost = 2; }
    if ( $('#cost3').attr('checked') == true ) { cost = 3; }
    var gender = '';
    gender = $('#gender2_teacher').attr('checked') == true ? '0' : '';
    if ( gender == '' ) {
      gender = $('#gender3_teacher').attr('checked') == true ? 1 : ''
    }

    var age = '';
    if ( $('#age1_teacher').attr('checked') == true ) { age = 1; }
    if ( $('#age2_teacher').attr('checked') == true ) { age = 2; }
    if ( $('#age3_teacher').attr('checked') == true ) { age = 3; }
    if ( $('#age4_teacher').attr('checked') == true ) { age = 4; }
    //var rating = '';
    //if ( $('#rating1').attr('checked') == true ) { rating = 1; }
    //if ( $('#rating2').attr('checked') == true ) { rating = 3; }
    //if ( $('#rating3').attr('checked') == true ) { rating = 5; }
    //var call_quality = '';
    //if ( $('#call_quality1').attr('checked') == true ) { call_quality = 1; }
    //if ( $('#call_quality2').attr('checked') == true ) { call_quality = 3; }
    //if ( $('#call_quality3').attr('checked') == true ) { call_quality = 5; }
    //var feedback = '';
    //if ( $('#feedback1').attr('checked') == true ) { feedback = 1; }
    //if ( $('#feedback2').attr('checked') == true ) { feedback = 2; }
    //var lesson_free = '';
    //lesson_free = $('#lesson_free').attr('checked') == true ? 1 : '';
    var online_teacher = $('#online_teacher').attr('checked') == true ? 1 : '';

    //window.location = SITE_URL + 'search?search_username=' + $('#search_username').val() + '&user_type=' + $('#user_type').val() + "&native_language=" + $('#native_language_teacher').val() + '&language=' + $('#language_teacher').val() + '&cost=' + cost + '&rating=' + rating + '&call_quality=' + call_quality + '&feedback=' + feedback + '&lesson_free=' + lesson_free + '&online=' + online_teacher;
    window.location = SITE_URL + 'search?search_username=' + $('#search_username').val() + '&user_type=' + $('#user_type').val() + "&native_language=" + $('#native_language_teacher').val() + '&language=' + $('#language_teacher').val() + '&cost=' + cost + '&gender=' + gender + '&age=' + age + '&online=' + online_teacher;
  }
}

function update_learn_languages_filters() {
  // for students
  if ( $('#user_type').val() == '1' ) {
    var gender = '';
    gender = $('#gender2').attr('checked') == true ? '0' : '';
    if ( gender == '' ) {
      gender = $('#gender3').attr('checked') == true ? 1 : ''
    }

    var age = '';
    if ( $('#age1').attr('checked') == true ) { age = 1; }
    if ( $('#age2').attr('checked') == true ) { age = 2; }
    if ( $('#age3').attr('checked') == true ) { age = 3; }
    if ( $('#age4').attr('checked') == true ) { age = 4; }
    var seeking_friends = $('#seeking_friends').attr('checked') == true ? 1 : '';
    var online_student = $('#online_student').attr('checked') == true ? 1 : '';

    window.location = SITE_URL + 'learn/' + $('#get_language').val() + '&search_username=' + $('#search_username').val() + '&gender=' + gender + '&native_language=' + $('#native_language').val() + '&language=' + $('#language').val() + '&age=' + age + '&online=' + online_student + '&user_type=' + $('#user_type').val() + '&language_n=' + $('#get_language').val();
  } else {
    var cost = '';
    if ( $('#cost1').attr('checked') == true ) { cost = 1; }
    if ( $('#cost2').attr('checked') == true ) { cost = 2; }
    if ( $('#cost3').attr('checked') == true ) { cost = 3; }
    //var rating = '';
    //if ( $('#rating1').attr('checked') == true ) { rating = 1; }
    //if ( $('#rating2').attr('checked') == true ) { rating = 3; }
    //if ( $('#rating3').attr('checked') == true ) { rating = 5; }
    //var call_quality = '';
    //if ( $('#call_quality1').attr('checked') == true ) { call_quality = 1; }
    //if ( $('#call_quality2').attr('checked') == true ) { call_quality = 3; }
    //if ( $('#call_quality3').attr('checked') == true ) { call_quality = 5; }
    //var feedback = '';
    //if ( $('#feedback1').attr('checked') == true ) { feedback = 1; }
    //if ( $('#feedback2').attr('checked') == true ) { feedback = 2; }
    //var lesson_free = '';
    //lesson_free = $('#lesson_free').attr('checked') == true ? 1 : '';
    var online_teacher = $('#online_teacher').attr('checked') == true ? 1 : '';
    
    window.location = SITE_URL + 'learn/' + $('#get_language').val() + '&search_username=' + $('#search_username').val() + '&user_type=' + $('#user_type').val() + "&native_language=" + $('#native_language_teacher').val() + '&language=' + $('#language_teacher').val() + '&cost=' + cost + '&online=' + online_teacher + '&language_n=' + $('#get_language').val();
  }
}

function input_focus_search() {
  var search_username = $('#search_username').val();
  
  if ( search_username == 'Search' && $('#search_username').is(".my_search_input") ) {
    $('#search_username').removeClass('my_search_input');
    $('#search_username').addClass('my_search_input_highlighted');
    $('#search_username').val('');
  }
}

function input_blur_search() {
  var search_username = $('#search_username').val();
  
  if ( search_username == '' && $('#search_username').is(".my_search_input_highlighted") ) {
    $('#search_username').removeClass('my_search_input_highlighted');
    $('#search_username').addClass('my_search_input');
    $('#search_username').val('Search');
  }
}

function new_search_submit() {
  var search_username = $('#search_username').val();
  if ( search_username == 'Search' && $('#search_username').is(".my_search_input") ) {
    search_username = '';
  }
  
  if ( $('#user_type').val() == '1' ) {
    window.location = SITE_URL + 'search?search_username=' + search_username + '&user_type=' + $('#user_type').val() + '&age=' + $('#age').val() + '&gender=' + $('#gender').val() + '&online=' + $('#online').val() + '&language=' + $('#language_learning').val() + '&native_language=' + $('#native_language').val();
    //window.location = SITE_URL + 'search?search_username=' + $('#search_username').val() + '&gender=' + gender + '&native_language=' + $('#native_language').val() + '&language=' + $('#language').val() + '&age=' + age + '&online=' + online_student + '&user_type=' + $('#user_type').val();
  }
  
  if ( $('#user_type').val() == '0' ) {
    window.location = SITE_URL + 'search?search_username=' + search_username + '&user_type=' + $('#user_type').val() + '&age=' + $('#age').val() + '&gender=' + $('#gender').val() + '&online=' + $('#online').val() + '&cost=' + $('#cost').val() + '&language=' + $('#language').val();
    //window.location = SITE_URL + 'search?search_username=' + $('#search_username').val() + '&user_type=' + $('#user_type').val() + "&native_language=" + $('#native_language_teacher').val() + '&language=' + $('#language_teacher').val() + '&cost=' + cost + '&gender=' + gender + '&age=' + age + '&online=' + online_teacher;
  }
}

function change_type(type_name, type_id) {
  $('#select_type').html('&nbsp;&nbsp;&nbsp;' + type_name);
  $('#user_type').val(type_id);

  if ( type_id == 0 ) {
    $('#teacher_div_1').css('display', 'block');
    $('#teacher_div_2').css('display', 'block');

    $('#student_div_1').css('display', 'none');
    $('#student_div_2').css('display', 'none');
  }
  
  if ( type_id == 1 ) {
    $('#teacher_div_1').css('display', 'none');
    $('#teacher_div_2').css('display', 'none');

    $('#student_div_1').css('display', 'block');
    $('#student_div_2').css('display', 'block');
  }
  
  load_type();
}

function load_type() {
  if ( $('#display_type').css('display') == 'none' ) {
    $('#display_type').fadeIn(500);
  } else {
    $('#display_type').fadeOut(500);
  }
}

function change_age(type_name, type_id) {
  $('#select_age').html('&nbsp;&nbsp;&nbsp;' + type_name);
  $('#age').val(type_id);
  load_age();
}

function load_age() {
  if ( $('#display_age').css('display') == 'none' ) {
    $('#display_age').fadeIn(500);
  } else {
    $('#display_age').fadeOut(500);
  }
}

function change_gender(type_name, type_id) {
  $('#select_gender').html('&nbsp;&nbsp;&nbsp;' + type_name);
  $('#gender').val(type_id);
  load_gender();
}

function load_gender() {
  if ( $('#display_gender').css('display') == 'none' ) {
    $('#display_gender').fadeIn(500);
  } else {
    $('#display_gender').fadeOut(500);
  }
}

function change_picture() {
  if ( $('#select_picture').is(".checkbox_1") ) {
    $('#select_picture').removeClass('checkbox_1');
    $('#select_picture').addClass('checkbox_2');

    $('#online').val(1);
  } else {
    $('#select_picture').removeClass('checkbox_2');
    $('#select_picture').addClass('checkbox_1');

    $('#online').val('');
  }
}

function change_cost_per_hour(type_name, type_id) {
  $('#select_cost_per_hour').html('&nbsp;&nbsp;&nbsp;' + type_name);
  $('#cost').val(type_id);
  load_cost_per_hour();
}

function load_cost_per_hour() {
  if ( $('#display_cost_per_hour').css('display') == 'none' ) {
    $('#display_cost_per_hour').fadeIn(500);
  } else {
    $('#display_cost_per_hour').fadeOut(500);
  }
}

function change_teaching(type_name, type_id) {
  $('#select_teaching').html('&nbsp;&nbsp;&nbsp;' + type_name);
  $('#language').val(type_id);
  load_teaching();
}

function load_teaching() {
  if ( $('#display_teaching').css('display') == 'none' ) {
    $('#display_teaching').fadeIn(500);
  } else {
    $('#display_teaching').fadeOut(500);
  }
}

function change_learning(type_name, type_id) {
  $('#select_learning').html('&nbsp;&nbsp;&nbsp;' + type_name);
  $('#language_learning').val(type_id);
  load_learning();
}

function load_learning() {
  if ( $('#display_learning').css('display') == 'none' ) {
    $('#display_learning').fadeIn(500);
  } else {
    $('#display_learning').fadeOut(500);
  }
}

function change_native(type_name, type_id) {
  $('#select_native').html('&nbsp;&nbsp;&nbsp;' + type_name);
  $('#native_language').val(type_id);
  load_native();
}

function load_native() {
  if ( $('#display_native').css('display') == 'none' ) {
    $('#display_native').fadeIn(500);
  } else {
    $('#display_native').fadeOut(500);
  }
}

function delete_one_message(message_id) {
  var r = confirm('Are you sure you want to delete?');

  if ( r == true ) {
    window.location = SITE_URL + 'view/message/' + message_id + '&action=delete';
  }
}