function openWindowFromAnchor(url, procName, elementName, options) {
  var paramVal = new Array;
  var paramHTML = window.document.all[elementName].innerHTML;
  // var element = window.document.all[elementName];
  if (paramHTML == null) {
    paramVal[0] = "";
  } else {
    paramVal[0] = paramHTML;
  }
  /*
  window.alert("paramVal[0]=" + paramVal[0]);
  window.alert("elementName=" + elementName);
  window.alert("element.name=" + element.id); */
  paramVal[1] = procName;
  paramVal[2] = elementName;
  var wnd = window.showModalDialog(url, paramVal, options);
  /* window.alert("here, wnd=" + wnd); */
  /*
  if (wnd != null) {
    wnd.document.all.editSpan.innerHTML = window.document.all[elementName].innerHTML;
    window.alert("here, editSpan=" + wnd.document.all.editSpan);
  } */
  return false;
}

function ctrlBtnHighlight(source) {
//window.alert("ctrlBtnHighlight");
if (source == null)
  return true;
//window.alert("source =" + source.tagName + ":" + source.name);
if (source.tagName == "TR" || source.tagName == "TABLE")
  return true;
if (source.tagName == "TD")
  if (source.id == "ctrlBtn") {
    source.className="highlight"
    window.cursor = "hand";
  }
  return true;
}

function ctrlBtnOver(source) {
//window.alert("ctrlBtnOver");
if (source == null)
  return true;
if (source.tagName != "TD" || source.id == "ignore")
  return true;
if (source) {
    source.className="btn";
    window.cursor = "default";
  }
  return true;
}

function changeApproveValue(form_name) {
  if (form_name == null) {
    alert("changeApproveValue() - form not found!");
  }
  var workForm = null;
  for (var i = 0; i < document.forms.length; i++) {
    workForm = document.forms[i];
    if (workForm.name == form_name) {
      break;
    } else {
      workForm = null;
    }
  }
  if (workForm == null) {
    return false;
  }
  workForm.approve.value = 'yes';
  return true;
}

function changeSendToApproveValue(form_name) {
  if (form_name == null) {
    alert("changeSendToApproveValue() - form not found!");
  }
  var workForm = null;
  for (var i = 0; i < document.forms.length; i++) {
    workForm = document.forms[i];
    if (workForm.name == form_name) {
      break;
    } else {
      workForm = null;
    }
  }
  if (workForm == null) {
    return false;
  }
  workForm.send_to_approve.value = 'yes';
  return true;
}

function getRejectComment(comment_value, button_value) {

  if (button_value == 1) {
    // reject pressed
    var workForm = document.forms["edit_document"];
    if (workForm == null)
      return false;
    workForm.reject.value = 'yes';
    workForm.reject_comment.value = comment_value;
    workForm.submit();
    return true;

  } else if (button_value == 0) {
    // cancel pressed
    return false;

  } else {
    return false;
  }
}

function changeRejectValue(form_name) {
  showRejectDialog("Reject document", getRejectComment);
}



function confirmUrl(targetUrl, dlgMsg) {
  if (targetUrl == null || targetUrl == "")
    return false;
      var realDlgMsg = 'Are you sure?';
      if (dlgMsg != null && dlgMsg != "") {
        realDlgMsg = dlgMsg;
      }
    if (window.event.srcElement.tagName == "A") {
      window.event.cancelBubble = true;
      return confirm(realDlgMsg);
    }
  if (confirm(realDlgMsg)) {
    location.href = targetUrl;
    return true;
  }
  return false;
}

/* Functions for backend*/
function userlistClick(element) {
  /* window.alert("Here! element.tag=" + element.tagName); */
  for (i = 0; i < document.all.length; i++) {
    if (document.all[i].tagName == "TD" && document.all[i].className == "" &&
    document.all[i].id == element.id) {
      document.all[i].className = "selected";
    } else {
      if (document.all[i].tagName == "TD" && document.all[i].className == "selected" &&
      document.all[i].id != element.id)
        document.all[i].className = "";
    }
  }
}

function userlistDblclick(element, targetUrl) {
  userlistClick(element);
  location.href = targetUrl;
}

function approvalClick(element) {
  /* window.alert("Here! element.tag=" + element.tagName); */
  for (i = 0; i < document.all.length; i++) {
    if (document.all[i].tagName == "TD" && document.all[i].className == "" &&
    document.all[i].id == element.id) {
      document.all[i].className = "selected";
    } else {
      if (document.all[i].tagName == "TD" && document.all[i].className == "selected" &&
      document.all[i].id != element.id)
        document.all[i].className = "";
    }
  }
}

function approvalDblclick(element, targetUrl) {
  approvalClick(element);
  location.href = targetUrl;
}


function checkCreatePageForm() {

  var workForm = document.forms["create_page"];
  if (workForm == null)
    return false;

  if (workForm.title.value.length == 0) {
    alert("Please, input title ...");
    workForm.title.focus();
    return false;
  }

/*
  if (workForm.pagename.value.length == 0) {
    alert("Please, input page name ...");
    workForm.pagename.focus();
    return false;
  }

  var slash = '\\';
  if (workForm.pagename.value.indexOf(slash) != -1) {
    alert("Symbol '\\' is not allowed in the page name");
    workForm.pagename.focus();
    return false;
  }

  if (workForm.pagename.value.indexOf("/") != -1) {
    alert("Symbol '/' is not allowed in the page name");
    workForm.pagename.focus();
    return false;
  }

  if (workForm.pagename.value.indexOf("%") != -1) {
    alert("Symbol '%' is not allowed in the page name");
    workForm.pagename.focus();
    return false;
  }

  if (workForm.pagename.value.indexOf("+") != -1) {
    alert("Symbol '+' is not allowed in the page name");
    workForm.pagename.focus();
    return false;
  }

  if (workForm.pagename.value.indexOf("'") != -1) {
    alert("Single quote symbol ' is not allowed in the page name");
    workForm.pagename.focus();
    return false;
  }

  var apos = '"';
  if (workForm.pagename.value.indexOf(apos) != -1) {
    alert('Double quote symbol '+ apos +' is not allowed in the page name');
    workForm.pagename.focus();
    return false;
  }
*/

  var listOut = document.getElementById('listyes');//workForm._selected_clients;

  if (listOut == null)
    return;

  for (var i = 0; i < listOut.options.length; i++) {
    var h = document.createElement("input");
    h.setAttribute("type", "hidden");
    h.setAttribute("name", "selected_clients");
    h.setAttribute("value", listOut.options[i].value);
    workForm.appendChild(h);
  }

  workForm.submit();
  return true;
}

function checkUpdatePageForm() {

  var workForm = document.forms["edit_page"];
  if (workForm == null)
    return false;

  if (workForm.title.value.length == 0) {
    alert("Please, input title ...");
    workForm.title.focus();
    return false;
  }

  var listOut = document.getElementById('listyes');//workForm._selected_clients;

  if (listOut == null)
    return;

  for (var i = 0; i < listOut.options.length; i++) {
    var h = document.createElement("input");
    h.setAttribute("type", "hidden");
    h.setAttribute("name", "selected_clients");
    h.setAttribute("value", listOut.options[i].value);
    workForm.appendChild(h);
  }
  workForm.submit();
  return true;
}


function checkCreateUserInBackend() {

  var workForm = document.forms["backend_userform"];
  if (workForm == null)
    return false;

  if (workForm.login.value.length == 0) {
    alert("Please, input username ...");
    workForm.login.focus();
    return false;
  }
  if (workForm.login.value.indexOf("'") != -1) {
    alert("Single quote symbol ' is not allowed in the username");
    workForm.login.focus();
    return false;
  }
  var apos = '"';
  if (workForm.login.value.indexOf(apos) != -1) {
    alert('Double quote symbol '+ apos +' is not allowed in the username');
    workForm.login.focus();
    return false;
  }

  if (workForm.password1.value.length == 0) {
    alert("Please, input password ...");
    workForm.password1.focus();
    return false;
  }

  if (workForm.password2.value.length == 0) {
    alert("Please, input password again ...");
    workForm.password2.focus();
    return false;
  }

  if (workForm.password1.value !== workForm.password2.value) {
    alert("[Password] does not match [Confirm password]. Please input client passwords again.");
    workForm.password1.value = "";
    workForm.password2.value = "";
    workForm.password1.focus();
    return false;
  }

  if (workForm.password1.value.length < 6) {
    alert("Password must be at least 6 symbols length. Please, input user passwords again.");
    workForm.password1.value = "";
    workForm.password2.value = "";
    workForm.password1.focus();
    return false;
  }

  if (workForm.name.value.length == 0) {
    alert("Please, input full name ...");
    workForm.name.focus();
    return false;
  }

  if (workForm.email.value.length == 0) {
    alert("Please, input email ...");
    workForm.email.focus();
    return false;
  }

  if (workForm.email.value.indexOf("@") == -1 ||
      workForm.email.value.indexOf(".") == -1) {
    alert("Please, input correct e-mail value ...");
    workForm.email.focus();
    return false;
  }
  workForm.submit();
  return true;
}


function checkUpdateUserInBackend() {

  var workForm = document.forms["backend_userform"];
  if (workForm == null)
    return false;

  if (workForm.password1.value.length > 0) {

    if (workForm.password2.value.length == 0) {
      alert("Please, confirm password ...");
      workForm.password2.focus();
      return false;
    }

    if (workForm.password1.value !== workForm.password2.value) {
      alert("[Password] does not match [Confirm password]. Please input client passwords again.");
      workForm.password1.value = "";
      workForm.password2.value = "";
      workForm.password1.focus();
      return false;
    }

    if (workForm.password1.value.length < 6) {
      alert("Password must be at least 6 symbols length. Please, input user passwords again.");
      workForm.password1.value = "";
      workForm.password2.value = "";
      workForm.password1.focus();
      return false;
    }

  }


  if (workForm.name.value.length == 0) {
    alert("Please, input full name ...");
    workForm.name.focus();
    return false;
  }

  if (workForm.email.value.length == 0) {
    alert("Please, input email ...");
    workForm.email.focus();
    return false;
  }

  if (workForm.email.value.indexOf("@") == -1 ||
      workForm.email.value.indexOf(".") == -1) {
    alert("Please, input correct e-mail value ...");
    workForm.email.focus();
    return false;
  }
  workForm.submit();
  return true;
}

function checkCreateSubscriberInBackend() {

  var workForm = document.forms["backend_subscriberform"];
  if (workForm == null)
    return false;
  if (workForm.name.value.length == 0) {
    alert("Please, input full name ...");
    workForm.name.focus();
    return false;
  }
  if (workForm.email.value.length == 0) {
    alert("Please, input email ...");
    workForm.email.focus();
    return false;
  }
  if (workForm.email.value.indexOf("@") == -1 ||
      workForm.email.value.indexOf(".") == -1) {
    alert("Please, input correct e-mail value ...");
    workForm.email.focus();
    return false;
  }
  workForm.submit();
  return true;
}

function checkUpdateSubscriberInBackend() {

  var workForm = document.forms["backend_subscriberform"];
  if (workForm == null)
    return false;

  if (workForm.name.value.length == 0) {
    alert("Please, input full name ...");
    workForm.name.focus();
    return false;
  }

  if (workForm.email.value.length == 0) {
    alert("Please, input email ...");
    workForm.email.focus();
    return false;
  }

  if (workForm.email.value.indexOf("@") == -1 ||
      workForm.email.value.indexOf(".") == -1) {
    alert("Please, input correct e-mail value ...");
    workForm.email.focus();
    return false;
  }
  workForm.submit();
  return true;
}

function checkCategoryInBackend() {

  var workForm = document.forms["backend_categoryform"];
  if (workForm == null)
    return false;

  if (workForm.title.value.length == 0) {
    alert("Please, input category title ...");
    workForm.title.focus();
    return false;
  }
  workForm.submit();
  return true;
}

function checkResourceInBackend() {

  var workForm = document.forms["backend_resourceform"];
  if (workForm == null)
    return false;
  if (workForm.file.value.length == 0) {
    alert("Please, select file for upload.");
    workForm.file.focus();
    return false;
  }
  workForm.submit();
  return true;
}

// clients "tuda - usda" :) list
var saveOutListVal = new Array();
var saveOutListTxt = new Array();
var saveInListVal = new Array();
var saveInListTxt = new Array();

function initLists() {
  var list, i;
  list = document.getElementById('listno');
  SortD(list);
  for(var i=0; i < list.options.length; i++)  {
    saveOutListVal[i] = list.options[i].value;
    saveOutListTxt[i] = list.options[i].text;
  }
  list = document.getElementById('listyes');
  SortD(list);
  for(var i=0; i<list.options.length; i++)  {
    saveInListVal[i] = list.options[i].value;
    saveInListTxt[i] = list.options[i].text;
  }
}

function SortD(box) {
  var temp_opts = new Array();
  var temptxt = new Object();
  var tempval = new Object();
  // Load temp array
  for(var i=0; i<box.options.length; i++)  {
    temp_opts[i] = box.options[i];
  }
  //sort temporary
  for(var x=0; x<temp_opts.length-1; x++)  {
    for(var y=(x+1); y<temp_opts.length; y++)  {
      if(temp_opts[x].text > temp_opts[y].text)  {
        temptxt = temp_opts[x].text;
        temp_opts[x].text = temp_opts[y].text;
        temp_opts[y].text = temptxt;

        tempval = temp_opts[x].value;
        temp_opts[x].value = temp_opts[y].value;
        temp_opts[y].value = tempval;
      }
    }
  }
  for(var i=0; i<box.options.length; i++)  {
    box.options[i].value = temp_opts[i].value;
    box.options[i].text = temp_opts[i].text;
  }
}

function changeListItems(op, co) {
  var i, j, k, listIn, listOut, tmpval, tmptxt;
  if(op == "add") {
    listIn = document.getElementById('listyes');
    listOut = document.getElementById('listno');
  }
  else {
    listIn = document.getElementById('listno');
    listOut = document.getElementById('listyes');
  }
  for(i=0; i<listOut.options.length; i++) {
    if(listOut.options[i].selected || co == "all") {
      listIn.options[listIn.options.length] = new Option("", "");
      for(j=0; j<listIn.options.length; j++) {
        if(listOut.options[i].text < listIn.options[j].text || j == listIn.options.length - 1) {
          for(k=listIn.options.length-1; k > j; k--) {
            listIn.options[k].value = listIn.options[k-1].value;
            listIn.options[k].text = listIn.options[k-1].text;
          }
          break;
        }
      }
      listIn.options[j].value = listOut.options[i].value;
      listIn.options[j].text = listOut.options[i].text;
      listOut.options[i] = null;
      i=-1;
    }
  }
}


function checkUpdateMenuInBackend () {

  var workForm = document.forms["backend_menuform"];
  if (workForm == null)
    return false;

  if (workForm.menu_title.value.length == 0) {
    alert("Please, input menu name ...");
    workForm.menu_title.focus();
    return false;
  }

  if (workForm.page_name.value == '0') {
    alert("Please, select page ...");
    workForm.page_name.focus();
    return false;
  }

  if (workForm.page_heading.value.length == 0) {
    alert("Please, input page heading ...");
    workForm.page_heading.focus();
    return false;
  }

//  if (workForm.page_url.value.length == 0) {
//    alert("Please, input page url ...");
//    workForm.page_url.focus();
//    return false;
//  }

  workForm.submit();
  return true;
}

function checkCreateMenuInBackend() {
  var workForm = document.forms["backend_menuform"];
  if (workForm == null)
    return false;


  if (workForm.menu_title.value.length == 0) {
    alert("Please, input menu name ...");
    workForm.menu_title.focus();
    return false;
  }

  if (getSelectedLocationType() == "after" || getSelectedLocationType() == "before") {
    if (workForm.parent_menu_id.value == 0) {
      alert("Please, select menu item ...");
      workForm.parent_menu_id.focus();
      return false;
    }
  }

  // page exist
  if (workForm.type_of_page[0].checked) {
    if (workForm.page_name.value == '0') {
      alert("Please, select page ...");
      workForm.page_name.focus();
      return false;
    }
  }
  // new page
  if (workForm.type_of_page[1].checked) {

    if (workForm.page_heading.value.length == 0) {
      alert("Please, input page heading ...");
      workForm.page_heading.focus();
      return false;
    }
  }

  if (getSelectedPageLayout() == -1) {
    alert("Please, select page layout ...");
    return false;
  }

  workForm.submit();
  return true;
}


var pages = new Array();

function Page(id, controller_id, heading, title, description, keywords) {
  this.id = id;
  this.controller_id = controller_id;
  this.heading = heading;
  this.title = title;
  this.description = description;
  this.keywords = keywords;
  return this;
}

function addPage(id, controller_id, heading, title, description, keywords) {
  pages[pages.length] = new Page(id, controller_id, heading, title, description, keywords);
}


function updatePage(id) {
  var workForm = document.forms["backend_menuform"];
  if (workForm == null)
    return false;

  if (workForm.type_of_page != null) {
    if (workForm.type_of_page[1].checked) {
      return;
    }
  }

  var page;

  for (i = 0; i < pages.length; i++) {

    if (pages[i].id == id) {
      page = pages[i];
      setSelectedPageLayout(page.controller_id);
      workForm.page_heading.value = page.heading;
      workForm.page_meta_title.value = page.title;
      workForm.page_meta_description.value = page.description;
      workForm.page_meta_keywords.value = page.keywords;
      break;
    }
  }
}

function setSelectedPageLayout(id) {

  var workForm = document.forms["backend_menuform"];
  if (workForm == null)
    return false;
  if (workForm.controller_id.length == null) {
    if (workForm.controller_id.value == id) {
      workForm.controller_id.checked = true;
      return;
    }
  }
  for (i = 0; i < workForm.controller_id.length; i++) {
    if (workForm.controller_id[i].value == id) {
      workForm.controller_id[i].checked = true;
      return;
    }
  }
  return true;
}


function resetPages() {
  var workForm = document.forms["backend_menuform"];
  if (workForm == null)
    return false;
  workForm.page_heading.value = "";
  workForm.page_meta_title.value = "";
  workForm.page_meta_description.value = "";
  workForm.page_meta_keywords.value = "";
}

function getSelectedLocationType() {
  var workForm = document.forms["backend_menuform"];
  if (workForm == null)
    return false;
  for (i = 0; i < workForm.location.length; i++) {
    if (workForm.location[i].checked) {
      return workForm.location[i].value;
    }
  }
  return -1;
}

function getSelectedPageLayout() {
  var workForm = document.forms["backend_menuform"];
  if (workForm == null)
    return false;
  if (workForm.controller_id.length == null) {
    return workForm.controller_id.checked ? 1 : -1;
  }
  for (i = 0; i < workForm.controller_id.length; i++) {
    if (workForm.controller_id[i].checked) {
      return i;
    }
  }
  return -1;
}


function checkPageLayout(id) {

  var workForm = document.forms["backend_menuform"];
  if (workForm == null)
    return false;

  var old_selected_id = workForm.selected_layout_id.value;

  if (old_selected_id != id) {
    result = confirm('You have selected a different page layout.\nThis will delete all current page content\nand create the new layout with empty content.\n\nThis operation can not be undone');
    if (result) {
      workForm.selected_layout_id.value = id;
      return true;
    } else {
      for (i = 0; i < workForm.controller_id.length; i++) {
        if (workForm.controller_id[i].value == old_selected_id) {
          workForm.controller_id[i].checked = true;
        }
      }
      return false;
    }
  }
  return false;
}

function escaper(text) {
  while (text.indexOf('"') != -1)  text = text.replace('"', '\&quot;');
  while (text.indexOf("'") != -1)  text = text.replace("'", '\&#39;');
  return text;
}

function collapse(id, menu_id) {
  var ctrl_span = document.getElementById('control-'+id);
  if (ctrl_span == null) {
    return;
  }

  var image = new Image();

  if (ctrl_span.innerText == '[-]') {
    ctrl_span.innerHTML = '[+]';
    image.src = "/structure/collapse.admin?menu_id=" + menu_id;
    ctrl_span.title = 'expand menu';
    i = 1;
    var str_id = id+"-1";
    while (document.getElementById(str_id) != null) {
      obj = document.getElementById(str_id);
      obj.style.display = 'none';
      i++;
      str_id = id+"-"+i;
    }
  } else {
    ctrl_span.innerHTML = '[-]';
    image.src = "/structure/expand.admin?menu_id=" + menu_id;
    ctrl_span.title = 'collapse menu';
    i = 1;
    var str_id = id+"-1";
    while (document.getElementById(str_id) != null) {
      document.getElementById(str_id).style.display = '';
      i++;
      str_id = id+"-"+i;
    }
  }
}

function initMenuBranch(collapse_id, is_collapse) {
  var i = 1;
  if (is_collapse == 'true') {
    var str_id = collapse_id+"-1";
    while (document.getElementById(str_id) != null) {
      obj = document.getElementById(str_id);
      obj.style.display = 'none';
      i++;
      str_id = collapse_id+"-"+i;
    }
  } else {
    var str_id = collapse_id+"-1";
    while (document.getElementById(str_id) != null) {
      obj = document.getElementById(str_id);
      obj.style.display = '';
      i++;
      str_id = collapse_id+"-"+i;
    }
  }
}
