function new_window(sLink, sName, iWidth, iHeight)
{
  window.open(sLink, sName, 'left=' + Math.ceil((screen.width - iWidth)/2) + ',top=' + Math.ceil((screen.height - iHeight)/2) + ',width=' + iWidth + ',height=' + iHeight + ',location=0,toolbar=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,channelmode=0,fullscreen=0');
  return false;
}

function setOrder(id, parent_id, order)
{
  var sform = document.moveform;
  sform.id.value = id;
  sform.parent_id.value = parent_id;
  sform.order.value = order;
  document.moveform.submit();
}

function viewBorough(lang_id)
{
    for (var i in data[lang_id])
    {
        document.getElementById('title_'+i).innerHTML = data[lang_id][i];
    }
}


function viewName(lang_id)
{
  for (var i in data[lang_id])
  {
    document.getElementById('title_'+i).innerHTML = data[lang_id][i];
  }
}


function submitForm(url)
{
    var sform = document.search;
    if (sform.search.value.length)
    {
        sform.action = url;
        if (checkSubmit('search',{'search':'Find '+name},[],[]))
        {
            sform.submit();
        }
    }
    else
    {
        window.location = url;
    }
}

function submitEmptyForm(url)
{
    var sform = document.search;
    sform.action = url;
    sform.submit();
}

function checkImageId(obj, name)
{
    if (obj.value == 0)
    {
        alert(name);
        return false;
    }
    return true;
}

function selectImage(id)
{
  return new_window('/admin/select_image.php/' + id, 'img_' + id, 800, 600);
}

function showImage(id)
{
  window.open('/admin/view_image.php?id=' + id, 'image_' + id, 'left=0,top=0,width=100,height=100,location=0,toolbar=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,channelmode=0,fullscreen=0');
  return false;
}

function checkMandatoryA(oField, sName)
{
  if (oField.value.length)
  {
    return true;
  }
  else
  {
    alert("Mandatory field '" + sName + "' is empty.");
    oField.focus();
    return false;
  }
}


function enterPopup(width, height)
{
  var exp = new Date();
  var period = exp.getTime() + 60*60*1000; // 1 hour
  exp.setTime (period);

  if (getCookieData("enter") != 1)
  {
    new_window('/popup.php/1', 'enter_popup', width, height);
  }

  document.cookie = "enter=1; path=/; expires=" + exp.toGMTString();

  return false;
}


var exit=true;

function leavePopup(width, height)
{
  if (exit && !window.opener)
  {
      win=window.open('/leave_popup.php/2', 'leave_popup', 'left=' + Math.ceil((screen.width - width)/2) + ',top=' + Math.ceil((screen.height - height)/2) + ',width=' + width + ',height=' + height + ',location=0,toolbar=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,channelmode=0,fullscreen=0');
      win.focus();
  }
}

function parseLinks()
{
    for (var i=0; i<document.links.length; i++)
    {
        if (document.links[i].href.indexOf(document.domain) == -1)
        {
            continue;
        }
        if (document.links[i].onclick)
        {
            if (document.links[i].attachEvent)
            {
        		document.links[i].attachEvent('onclick', exitChange);
            }
        	if (document.links[i].addEventListener)
            {
        		document.links[i].addEventListener('click', exitChange, false)
            }
        }
        else
        {
            document.links[i].onclick = function(){exit=false;};
        }
    }
}

function exitChange()
{
    exit=false;
}

function getCookieData(labelName)
{
  var labelLen = labelName.length;
  var cookieData = document.cookie;

  var cook = document.cookie.split(';');
  for (var i in cook)
  {
    var param = cook[i].split('=');
    str = param[0].replace(/\s/ig, "");

    if (param[0].replace(/\s/ig, "") == labelName)
    {
      return param[1];
    }
  }

  return "";
}

function carePopup(url)
{
  new_window(url, 'care_popup', 650, 600);
  return false;
}

function checkSubscribeForm()
{
    var sform = document.subscriptionform;
    if (!checkEmail(sform.email, 'E-mail'))
    {
        return false;
    }
    return true;
}

function checkCategoryNavigation(home, url, ext)
{
    var sform = document.navform;
    if (sform.cat.options.selectedIndex == 0)
    {
        alert('Please choose a category');
        sform.cat.focus();
        return false;
    }
    if (sform.cat.options[sform.cat.options.selectedIndex].value > 0)
    {
        window.location = url + sform.cat.options[sform.cat.options.selectedIndex].value + ext;
    }
    else
    {
        window.location = home;
    }
}

function catchKey(e)
{
    var isIE = (navigator.userAgent.indexOf("MSIE") != -1);
    key = (isIE) ? key = event.keyCode : e.which;
    if (key == 13)
    {
        if (checkSubmit('login',{'login':'E-mail', 'password':'Password'},{},[]))
        {
            document.login.submit();
        }
    }
}

function checkUpload(sform)
{
    if (!checkMandatoryA(sform.image, 'Image')) return false;
    if (!checkFolder(sform.upload_folder)) return false;
    return true;
}
function checkFolder(object)
{
    if (object.value == 0)
    {
        alert('Please choose a Folder');
        object.focus();
        return false;
    }
    return true;
}
