function logoUploadPopup(url) {
  win = window.open(url,"Logo","menubar=no,toolbar=no,titlebar=no,location=no,resizable=no,width=500,height=360,screenX=200,screenY=200");
  win.focus()
}

function confirmDeleteBox(url)
{
  answer = confirm("Та үнэхээр устгахыг хүсч байна уу?");

  if (!answer)
  {
    return false;
  }

  window.location = url;

  return true;
}

function hideElement(id) {
  //safe function to hide an element with a specified id
  if (document.getElementById) { // DOM3 = IE5, NS6
    if(document.getElementById(id)) {
      document.getElementById(id).style.display = 'none';
    }
  }
  else {
    if (document.layers) { // Netscape 4
      document.id.display = 'none';
    }
    else { // IE 4
      document.all.id.style.display = 'none';
    }
  }
}

function showElement(id) {
  //safe function to show an element with a specified id
  if (document.getElementById) { // DOM3 = IE5, NS6
    if(document.getElementById(id)) {
      document.getElementById(id).style.display = 'block';
    }
  }
  else {
    if (document.layers) { // Netscape 4
      document.id.display = 'block';
    }
    else { // IE 4
      document.all.id.style.display = 'block';
    }
  }
}

function checkIt(objName, swapper)
{
  var obj = document.getElementsByName(objName);
  var user_to = "";
  var user_name = "";
  var user = "";

  if(obj) {
    for(i = 0; i < obj.length; i++)
    {
      if(swapper.checked) {
        if(!obj[i].checked) {
          obj[i].checked = true;
          user = (obj[i].value).split("-");
          user_to += user[0] + ",";
          user_name += user[1] + ",";
        }
      }
      else {
        if(obj[i].checked) {
          obj[i].checked = false;
        }
      }
    }
  }

  if(swapper.checked) {
    document.getElementById('count').innerHTML = "<b>" + obj.length + "</b> сонгогдсон";
  }
  else {
    document.getElementById('count').innerHTML = "<b>0</b> сонгогдсон"
  }

  document.getElementById('user_to').value = user_to;
  document.getElementById('user_name').value = user_name;
}

function checkThis(objName, swapper, element)
{
  var obj = document.getElementsByName(objName);
  var counter = 0;

  var user_to = "";
  var user_name = "";
  var user = "";

  if(obj) {

    for(i = 0; i < obj.length; i++)
    {
      if(obj[i].checked) {
        user = (obj[i].value).split("-");
        user_to += user[0] + ",";
        user_name += user[1] + ",";
        counter++;
      }
    }

    if (obj.length == counter) {
      swapper.checked = true;
    }
    else {
      swapper.checked = false;
    }
  }

  document.getElementById('count').innerHTML = "<b>" + counter + "</b> сонгогдсон";
  document.getElementById('user_to').value = user_to;
  document.getElementById('user_name').value = user_name;
}

function setCookie(c_name,value,expiredays)
{
  var exdate = new Date();
  exdate.setDate(exdate.getDate()+expiredays);
  document.cookie=c_name+ "=" +escape(value)+
  ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name)
{
  if (document.cookie.length>0)
  {
    c_start=document.cookie.indexOf(c_name + "=");
    if (c_start!=-1)
    {
      c_start=c_start + c_name.length+1;
      c_end=document.cookie.indexOf(";",c_start);
      if (c_end==-1) c_end=document.cookie.length;
      return unescape(document.cookie.substring(c_start,c_end));
    }
  }
  return "";
}

/**
Clear default text from the text field
*/
function clearDefault ( thisfield, defaulttext)
{
  if (thisfield.value == defaulttext)
  {
    thisfield.value = "";
    thisfield.style.color = "";
  }
}


/**
Restore delault filed for input box
*/
function recallDefault( thisfield, defaulttext)
{
  if (thisfield.value == "")
  {
    thisfield.value = defaulttext;
    thisfield.style.color = "#afafaf";
  }
}


/**
Change realetsate fields
*/
function changeRealstateType(obj)
{
	document.getElementById('apartment').style.display = "none";
	document.getElementById('house').style.display = "none";
	document.getElementById('land').style.display = "none";
	document.getElementById('office').style.display = "none";
	document.getElementById('production').style.display = "none";
	document.getElementById('services').style.display = "none";

	switch(obj.value){
		case '1': document.getElementById('apartment').style.display = "block"; break;
		case '2': document.getElementById('house').style.display = "block"; break;
		case '3': document.getElementById('land').style.display = "block"; break;
		case '4': document.getElementById('office').style.display = "block"; break;
		case '5': document.getElementById('production').style.display = "block"; break;
		case '6': document.getElementById('services').style.display = "block"; break;
	}
}

/**
 * Update number of left chars
 * @param el
 * @param $counter_el
 * @param limit
 */
function checkLeftChars(el, $counter_el, limit)
{
  var $field = jQuery(el);
  var val = $field.val();

  if (val.length > limit) $field.val(val = val.substring(0, limit));
  $counter_el.html(limit - val.length);
}

function time_ago(time, to_time)
{
  var date;
  date = time.match(/(\d{4})-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})/);
  if (date === null) {
    date = time.match(/(\d{4})-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{1,2})/);
    if (date === null) {
      date = time.match(/(\d{4})-(\d{1,2})-(\d{1,2})/);
      if (date === null) return time;
      date[4] = date[5] = date[6] = 0;
    }else{
      date[6] = 0;
    }
  }
  for (var i in date) if (date[i][0] == 0) date[i] = date[i][1];

  var from_time = Math.round(new Date(parseInt(date[1]), parseInt(date[2]) - 1, parseInt(date[3]), parseInt(date[4]), parseInt(date[5]), parseInt(date[6])).getTime() / 1000);
  //console.log(new Date(parseInt(date[1]) - 1, parseInt(date[2]), parseInt(date[3]), parseInt(date[4]), parseInt(date[5]), parseInt(date[6])));
  //console.log(date);
  var diff = Math.abs(to_time - from_time);
  distance_in_minutes = Math.floor(diff / 60);
  distance_in_seconds = Math.floor(diff);

  string = '';
  if (distance_in_minutes <= 1)
  {
    if (distance_in_seconds <= 5)
    string = '5 секундийн өмнө';
    else if (distance_in_seconds >= 6 && distance_in_seconds <= 10)
    string = '10 секундийн өмнө';
    else if (distance_in_seconds >= 11 && distance_in_seconds <= 20)
    string = '20 секундийн өмнө';
    else if (distance_in_seconds >= 21 && distance_in_seconds <= 40)
    string = '30 секундийн өмнө';
    else if (distance_in_seconds >= 41 && distance_in_seconds <= 59)
    string = '50 секундийн өмнө';
    else
    string = '1 минутын өмнө';
  }
  else if (distance_in_minutes >= 2 && distance_in_minutes <= 44)
  string = distance_in_minutes + ' минутын өмнө';
  else if (distance_in_minutes >= 45 && distance_in_minutes <= 89)
  string = '1 цагийн өмнө';
  else if (distance_in_minutes >= 90 && distance_in_minutes <= 1439)
  string = Math.round(distance_in_minutes / 60) + ' цагийн өмнө';
  else if (distance_in_minutes >= 1440 && distance_in_minutes <= 2879)
  string = '1 өдрийн өмнө';
  else if (distance_in_minutes >= 2880 && distance_in_minutes <= 43199)
  string = Math.round(distance_in_minutes / 1440) + ' өдрийн өмнө';
  else if (distance_in_minutes >= 43200 && distance_in_minutes <= 86399)
  string = '1 сарын өмнө';
  else string = time;

  return string;
}

function applyTimeAgo()
{
  var now = Math.round(new Date().getTime() / 1000);
  jQuery('span.datetime').each(function() {
    var $this = jQuery(this);
    var old_s = $this.html();
    var s = time_ago(old_s, now);
    $this.removeClass('datetime').html(s);
    $this.data('switch', old_s);
    $this.click(function () {
      var $this = jQuery(this);
      var s = $this.data('switch');
      $this.data('switch', $this.html());
      $this.html(s);
    });
  });
}













