//Ajax functions
function fetchPage(which) {
  ajaxSend('index.php','current_page='+which,'GET');
  document.getElementById("page_content").innerHTML = '<img src="../images/ajax-loading.gif" alt="Loading Page" />';
}

function receivePage(returnObject) {
  content = eval('('+returnObject+')');
  if (content.pageTitle.length > 0) {
    document.title = content.pageTitle;
  }
  if (content.action == "update node") {
    //Update Content
    document.getElementById(content.targetNode).innerHTML = content.newContent;
    //Update Class
    if (content.className) {
      document.getElementById(content.targetNode).className = content.className;
    }
    //Update Text Input Dynamics
    ControlTextInputs(10,30,2);
    //Update Navigation
    if (content.currentPage.length > 0) {
      //Now update navigation
      links = document.getElementById("navigation").getElementsByTagName('a');
      cpRegEx = new RegExp(content.currentPage);
      for (i=0;i<links.length;i++) {
        links[i].className = links[i].className.replace(/chosen/,'');
        if (links[i].href.search(cpRegEx) != -1) {
          links[i].className += " chosen";
        }
      }
    }
  }
  if (content.action == "call function" || content.callFunction == "true") {
    //Used to call custom functions by add on scripts
    //Passes the value of "newContent" to the named function
    window[content.functionName](content);
  }
}

function editField(which) {
  which.readOnly = false;
  which.className = "";
}
function saveField(page,which) {
  if (which.tagName == "INPUT") {
    if (which.readOnly == false) {
      which.className = "inline_edit";
      which.readOnly = true;
      newValue = which.value;
    }
  } else if (which.tagName == "SELECT") {
    newValue = which.options[which.options.selectedIndex].value;
  }
  document.getElementById("ajax_message").style.visibility = "visible";
  ajaxSend('index.php?current_page='+page,"duplicate_override=true&"+which.id+"="+newValue,'POST');
}
function saveForm(formId) {
  return true;
  /**
   * NOT USED
  postString = ""; prefix = "";

  inputs = document.getElementById(formId).getElementsByTagName('INPUT');
  for (i=0;i<inputs.length;i++) {
    if (inputs[i].name.length > 0) {
      if (inputs[i].type != "checkbox" || (inputs[i].type == "checkbox" && inputs[i].checked))
      postString += prefix+inputs[i].name+"="+escape(inputs[i].value);
      prefix = "&";
    }
  }
  selects = document.getElementById(formId).getElementsByTagName('SELECT');
  for (i=0;i<selects.length;i++) {
    if (selects[i].name.length > 0) {
      postString += prefix+selects[i].name+"="+escape(selects[i].value);
      prefix = "&";
    }
  }
  textareas = document.getElementById(formId).getElementsByTagName('TEXTAREA');
  for (i=0;i<textareas.length;i++) {
    if (textareas[i].name.length > 0) {
      postString += prefix+textareas[i].name+"="+escape(textareas[i].value);
      prefix = "&";
    }
  }

  alert(document.getElementById(formId).action+"\n\n\n"+postString);
  ajaxSend(document.getElementById(formId).action,postString,'POST');
  */
}

function GeneratePassword(targetId) {
  ajaxSend('index.php','action=password&targetId='+targetId,'GET');
}
function showPassword(rValue) {
  document.getElementById(rValue.targetId).value = rValue.newContent;
  document.getElementById(rValue.targetId.replace(/password/,'confirm_password')).value = rValue.newContent;
  ControlTextInputs(10,30,2);
  showCurrentPassword(rValue.targetId);
}

function hideAjaxMessage(rValue) {
  setTimeout("setVisibilityHidden('ajax_message');",3000);
}
function setVisibilityHidden(box_id) {
  document.getElementById(box_id).style.visibility = "hidden";
}

//Show popup functions
function showPopup(text) {
  document.getElementById('popup_message').style.display = "block";
  document.getElementById('popup_message').style.top = (mousey+10)+"px";
  document.getElementById('popup_message').style.left = (mousex+10)+"px";

  document.getElementById('popup_message').innerHTML = text;
  setTimeout('hidePopup()',7000);
}

function hidePopup() {
  document.getElementById('popup_message').style.display = "none";
}

function showCurrentPassword(targetId) {
  if (document.getElementById(targetId).value.length > 0) {
    showPopup(document.getElementById(targetId).value);
  }
}

var mousex, mousey;

function getMouseXY(e)  {
  if (!e) e = window.event;
  if (e) {
    if (e.pageX || e.pageY) {
      mousex = e.pageX;
      mousey = e.pageY;
    } else if (e.clientX || e.clientY) {
      if (document.documentElement.scrollTop) {
        mousex = e.clientX + document.documentElement.scrollLeft;
        mousey = e.clientY + document.documentElement.scrollTop;
      } else {
        mousex = e.clientX ;
        if (document.body) {
          mousex += document.body.scrollLeft;
        }

        mousey = e.clientY;
        if (document.body) {
          mousey += document.body.scrollTop;
        }
      }
    }
  }
}
document.onmousemove = getMouseXY;

//FCKEditor function
function toFCK(source) {
  var oFCKeditor = new FCKeditor(source);
  oFCKeditor.BasePath = "../resources/fckeditor/";
  oFCKeditor.ReplaceTextarea();
}
